1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
package info.magnolia.module.resources.renderers; |
35 |
|
|
36 |
|
import static org.hamcrest.CoreMatchers.*; |
37 |
|
import static org.junit.Assert.*; |
38 |
|
import static org.mockito.Mockito.*; |
39 |
|
import info.magnolia.cms.beans.config.MIMEMapping; |
40 |
|
import info.magnolia.cms.core.MgnlNodeType; |
41 |
|
import info.magnolia.cms.util.ClasspathResourcesUtil; |
42 |
|
import info.magnolia.context.MgnlContext; |
43 |
|
import info.magnolia.context.SystemContext; |
44 |
|
import info.magnolia.jcr.util.NodeUtil; |
45 |
|
import info.magnolia.module.resources.ResourcesModule; |
46 |
|
import info.magnolia.module.resources.loaders.ClasspathResourceLoader; |
47 |
|
import info.magnolia.module.resources.loaders.ResourceLoader; |
48 |
|
import info.magnolia.objectfactory.guice.GuiceUtils; |
49 |
|
import info.magnolia.rendering.context.RenderingContext; |
50 |
|
import info.magnolia.repository.RepositoryConstants; |
51 |
|
import info.magnolia.test.ComponentsTestUtil; |
52 |
|
import info.magnolia.test.mock.MockUtil; |
53 |
|
import info.magnolia.test.mock.MockWebContext; |
54 |
|
import info.magnolia.test.mock.jcr.MockNode; |
55 |
|
import info.magnolia.test.mock.jcr.MockSession; |
56 |
|
import info.magnolia.test.mock.jcr.SessionTestUtil; |
57 |
|
|
58 |
|
import java.io.File; |
59 |
|
import java.io.IOException; |
60 |
|
import java.io.OutputStream; |
61 |
|
import java.io.PrintWriter; |
62 |
|
import java.util.LinkedList; |
63 |
|
import java.util.List; |
64 |
|
|
65 |
|
import javax.jcr.Node; |
66 |
|
import javax.servlet.http.HttpServletResponse; |
67 |
|
|
68 |
|
import org.apache.commons.io.FileUtils; |
69 |
|
import org.apache.commons.io.output.ByteArrayOutputStream; |
70 |
|
import org.apache.jackrabbit.JcrConstants; |
71 |
|
import org.apache.jackrabbit.value.BinaryImpl; |
72 |
|
import org.apache.jackrabbit.value.StringValue; |
73 |
|
import org.junit.After; |
74 |
|
import org.junit.Before; |
75 |
|
import org.junit.Test; |
76 |
|
|
77 |
|
|
78 |
|
@link |
79 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (83) |
Complexity: 8 |
Complexity Density: 0.11 |
|
80 |
|
public class ResourcesBinaryRendererTest { |
81 |
|
|
82 |
|
|
83 |
|
private ResourcesBinaryRenderer renderer; |
84 |
|
private RenderingContext ctx = mock(RenderingContext.class); |
85 |
|
private final String binaryContent = "binaryContent"; |
86 |
|
private HttpServletResponse response; |
87 |
|
private MockNode node; |
88 |
|
private ResourcesModule resourcesModule; |
89 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (20) |
Complexity: 1 |
Complexity Density: 0.05 |
|
90 |
6 |
@Before... |
91 |
|
public void setUp() throws Exception { |
92 |
6 |
MockSession session = SessionTestUtil.createSession(RepositoryConstants.CONFIG, |
93 |
|
"/server/MIMEMapping/ext\n" + |
94 |
|
"/server/MIMEMapping/ext.extension=ext\n" + |
95 |
|
"/server/MIMEMapping/ext.mime-type=mime/type\n"); |
96 |
|
|
97 |
6 |
node = (MockNode) NodeUtil.createPath(session.getRootNode(), "/templating-kit/img/someBinary", MgnlNodeType.NT_RESOURCE); |
98 |
6 |
Node binary = node.addNode(ResourcesBinaryRenderer.BINARY_NODE); |
99 |
6 |
binary.setProperty(JcrConstants.JCR_DATA, new BinaryImpl(binaryContent.getBytes())); |
100 |
6 |
binary.setProperty(JcrConstants.JCR_MIMETYPE, new StringValue("mime/type")); |
101 |
|
|
102 |
6 |
OutputStream outputStream = new ByteArrayOutputStream(); |
103 |
|
|
104 |
6 |
when(ctx.getCurrentContent()).thenReturn(node); |
105 |
6 |
when(ctx.getOutputStream()).thenReturn(outputStream); |
106 |
|
|
107 |
6 |
MockWebContext ctx = new MockWebContext(); |
108 |
6 |
response = mock(HttpServletResponse.class); |
109 |
6 |
ctx.setResponse(response); |
110 |
6 |
MgnlContext.setInstance(ctx); |
111 |
|
|
112 |
6 |
resourcesModule = new ResourcesModule(); |
113 |
6 |
renderer = new ResourcesBinaryRenderer(GuiceUtils.providerForInstance(resourcesModule)); |
114 |
6 |
ResourceLoader loader = new ClasspathResourceLoader(); |
115 |
6 |
resourcesModule.addResourceLoader(loader); |
116 |
6 |
ComponentsTestUtil.setInstance(ResourcesModule.class, resourcesModule); |
117 |
|
|
118 |
6 |
ComponentsTestUtil.setInstance(SystemContext.class, ctx); |
119 |
6 |
MockUtil.setSystemContextSessionAndHierarchyManager(session); |
120 |
6 |
MIMEMapping.init(); |
121 |
|
|
122 |
|
} |
123 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
124 |
6 |
@After... |
125 |
|
public void tearDown() { |
126 |
6 |
MgnlContext.setInstance(null); |
127 |
6 |
ComponentsTestUtil.clear(); |
128 |
|
} |
129 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
130 |
1 |
@Test... |
131 |
|
public void testResourceLoadedFromContentNode() throws Exception { |
132 |
|
|
133 |
|
|
134 |
|
|
135 |
1 |
renderer.render(ctx, null); |
136 |
|
|
137 |
|
|
138 |
1 |
assertThat(ctx.getOutputStream().toString(), is(binaryContent)); |
139 |
1 |
verify(response).setContentLength(binaryContent.length()); |
140 |
|
} |
141 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
142 |
1 |
@Test... |
143 |
|
public void testResourceLoadedFromClassPath() throws Exception { |
144 |
|
|
145 |
1 |
node.setProperty(ResourcesBinaryRenderer.EXTENSION_PROPERTY, "png"); |
146 |
1 |
node.setProperty(ResourcesBinaryRenderer.BYPASS_PROPERTY, true); |
147 |
|
|
148 |
|
|
149 |
1 |
renderer.render(ctx, null); |
150 |
|
|
151 |
|
|
152 |
1 |
File file = new File("src/test/resources/templating-kit/img/someBinary.png"); |
153 |
1 |
assertThat(ctx.getOutputStream().toString(), is(FileUtils.readFileToString(file))); |
154 |
1 |
verify(response).setContentLength(FileUtils.readFileToByteArray(file).length); |
155 |
|
} |
156 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 1 |
Complexity Density: 0.07 |
1PASS
|
|
157 |
1 |
@Test... |
158 |
|
public void testIfResourceLoaderThrowsIOExceptionThenUseNextResourceLoader() throws Exception { |
159 |
|
|
160 |
1 |
node.setProperty(ResourcesBinaryRenderer.EXTENSION_PROPERTY, "png"); |
161 |
1 |
node.setProperty(ResourcesBinaryRenderer.BYPASS_PROPERTY, true); |
162 |
|
|
163 |
1 |
ResourceLoader loaderThrowingIOException = mock(ResourceLoader.class); |
164 |
1 |
when(loaderThrowingIOException.getStream("/templating-kit/img/someBinary.png")).thenThrow(new IOException()); |
165 |
1 |
ResourceLoader loader = new ClasspathResourceLoader(); |
166 |
1 |
List<ResourceLoader> loaders = new LinkedList<ResourceLoader>(); |
167 |
1 |
loaders.add(loaderThrowingIOException); |
168 |
1 |
loaders.add(loader); |
169 |
1 |
resourcesModule.setResourceLoaders(loaders); |
170 |
|
|
171 |
|
|
172 |
1 |
renderer.render(ctx, null); |
173 |
|
|
174 |
|
|
175 |
1 |
verify(loaderThrowingIOException).getStream("/templating-kit/img/someBinary.png"); |
176 |
1 |
File file = new File("src/test/resources/templating-kit/img/someBinary.png"); |
177 |
1 |
assertThat(ctx.getOutputStream().toString(), is(FileUtils.readFileToString(file))); |
178 |
1 |
verify(response).setContentLength(FileUtils.readFileToByteArray(file).length); |
179 |
|
} |
180 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1PASS
|
|
181 |
1 |
@Test... |
182 |
|
public void testIfResourceLoaderReturnsContentType() throws Exception { |
183 |
|
|
184 |
1 |
node.setProperty(ResourcesBinaryRenderer.EXTENSION_PROPERTY, "png"); |
185 |
1 |
node.setProperty(ResourcesBinaryRenderer.BYPASS_PROPERTY, true); |
186 |
|
|
187 |
1 |
ResourceLoader loader = new ClasspathResourceLoader(); |
188 |
1 |
List<ResourceLoader> loaders = new LinkedList<ResourceLoader>(); |
189 |
1 |
loaders.add(loader); |
190 |
1 |
resourcesModule.setResourceLoaders(loaders); |
191 |
|
|
192 |
|
|
193 |
1 |
renderer.render(ctx, null); |
194 |
|
|
195 |
|
|
196 |
1 |
verify(response).setContentType("mime/type"); |
197 |
|
} |
198 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
199 |
1 |
@Test... |
200 |
|
public void testIfResourceWithExtensionLoadedFromClassPathReturnsNullContentType() throws Exception { |
201 |
|
|
202 |
1 |
Node noBinaryNode = (MockNode) NodeUtil.createPath(node.getSession().getRootNode(), "/templating-kit/img/binaryResource", MgnlNodeType.NT_RESOURCE); |
203 |
1 |
noBinaryNode.setProperty(ResourcesBinaryRenderer.EXTENSION_PROPERTY, "ext"); |
204 |
1 |
noBinaryNode.setProperty(ResourcesBinaryRenderer.BYPASS_PROPERTY, true); |
205 |
1 |
when(ctx.getCurrentContent()).thenReturn(noBinaryNode); |
206 |
|
|
207 |
1 |
String classPath = ClasspathResourcesUtil.getResource("/").getPath(); |
208 |
1 |
File file = new File(classPath + "templating-kit/img/binaryResource.ext"); |
209 |
1 |
PrintWriter writer = new PrintWriter(file); |
210 |
1 |
writer.println("content"); |
211 |
1 |
writer.close(); |
212 |
|
|
213 |
|
|
214 |
1 |
renderer.render(ctx, null); |
215 |
|
|
216 |
|
|
217 |
1 |
verify(response).setContentType("mime/type"); |
218 |
|
} |
219 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
220 |
1 |
@Test... |
221 |
|
public void testIfResourceWithoutExtensionLoadedFromClassPathReturnsNullContentType() throws Exception { |
222 |
|
|
223 |
1 |
Node noBinaryNode = (MockNode) NodeUtil.createPath(node.getSession().getRootNode(), "/templating-kit/img/binaryResource", MgnlNodeType.NT_RESOURCE); |
224 |
1 |
noBinaryNode.setProperty(ResourcesBinaryRenderer.BYPASS_PROPERTY, true); |
225 |
1 |
when(ctx.getCurrentContent()).thenReturn(noBinaryNode); |
226 |
|
|
227 |
1 |
String classPath = ClasspathResourcesUtil.getResource("/").getPath(); |
228 |
1 |
File file = new File(classPath + "templating-kit/img/binaryResource"); |
229 |
1 |
PrintWriter writer = new PrintWriter(file); |
230 |
1 |
writer.println("content"); |
231 |
1 |
writer.close(); |
232 |
|
|
233 |
|
|
234 |
1 |
renderer.render(ctx, null); |
235 |
|
|
236 |
|
|
237 |
1 |
verify(response, never()).setContentType(anyString()); |
238 |
1 |
assertTrue(file.delete()); |
239 |
|
} |
240 |
|
|
241 |
|
} |