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.imaging; |
35 |
|
|
36 |
|
import static org.junit.Assert.*; |
37 |
|
import static org.mockito.Mockito.*; |
38 |
|
|
39 |
|
import info.magnolia.cms.beans.config.MIMEMapping; |
40 |
|
import info.magnolia.cms.security.User; |
41 |
|
import info.magnolia.context.MgnlContext; |
42 |
|
import info.magnolia.context.SystemContext; |
43 |
|
import info.magnolia.imaging.caching.CachingStrategy; |
44 |
|
import info.magnolia.init.MagnoliaConfigurationProperties; |
45 |
|
import info.magnolia.repository.RepositoryConstants; |
46 |
|
import info.magnolia.test.ComponentsTestUtil; |
47 |
|
import info.magnolia.test.mock.MockUtil; |
48 |
|
import info.magnolia.test.mock.MockWebContext; |
49 |
|
import info.magnolia.test.mock.jcr.SessionTestUtil; |
50 |
|
|
51 |
|
import java.awt.image.BufferedImage; |
52 |
|
import java.io.ByteArrayOutputStream; |
53 |
|
import java.io.IOException; |
54 |
|
import java.util.Collections; |
55 |
|
|
56 |
|
import javax.inject.Provider; |
57 |
|
import javax.jcr.Session; |
58 |
|
import javax.servlet.ServletOutputStream; |
59 |
|
import javax.servlet.http.HttpServletRequest; |
60 |
|
import javax.servlet.http.HttpServletResponse; |
61 |
|
|
62 |
|
import org.junit.After; |
63 |
|
import org.junit.Before; |
64 |
|
import org.junit.Ignore; |
65 |
|
import org.junit.Test; |
66 |
|
|
67 |
|
@Ignore |
|
|
| 0% |
Uncovered Elements: 61 (61) |
Complexity: 10 |
Complexity Density: 0.2 |
|
68 |
|
public class ImagingServletTest { |
69 |
|
|
70 |
|
private HttpServletRequest req; |
71 |
|
private HttpServletResponse res; |
72 |
|
private ImagingServlet imagingServlet; |
73 |
|
private ByteArrayOutputStream fakedOut; |
74 |
|
private OutputFormat outputFormat; |
75 |
|
private ImagingModule imagingModule; |
76 |
|
private TestImageGenerator imageGenerator; |
77 |
|
|
|
|
| 0% |
Uncovered Elements: 23 (23) |
Complexity: 1 |
Complexity Density: 0.04 |
|
78 |
0 |
@Before... |
79 |
|
public void setUp() throws Exception { |
80 |
0 |
req = mock(HttpServletRequest.class); |
81 |
0 |
res = mock(HttpServletResponse.class); |
82 |
0 |
fakedOut = new ByteArrayOutputStream(); |
83 |
0 |
outputFormat = new OutputFormat(); |
84 |
0 |
imagingModule = new ImagingModule(); |
85 |
|
|
86 |
|
|
87 |
|
|
88 |
0 |
final ServletOutputStream servletOut = new ServletOutputStream() { |
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
89 |
0 |
@Override... |
90 |
|
public void write(int b) throws IOException { |
91 |
0 |
fakedOut.write(b); |
92 |
|
} |
93 |
|
}; |
94 |
0 |
Session configMimeMapping = SessionTestUtil.createSession(RepositoryConstants.CONFIG, |
95 |
|
"/server/MIMEMapping/jpg", |
96 |
|
"/server/MIMEMapping/jpg.extension=jpg", |
97 |
|
"/server/MIMEMapping/jpg.mime-type=image/jpeg", |
98 |
|
"/server/MIMEMapping/gif", |
99 |
|
"/server/MIMEMapping/gif.extension=gif", |
100 |
|
"/server/MIMEMapping/gif.mime-type=image/gif", |
101 |
|
"/server/MIMEMapping/png", |
102 |
|
"/server/MIMEMapping/png.extension=png", |
103 |
|
"/server/MIMEMapping/png.mime-type=image/png" |
104 |
|
); |
105 |
0 |
Session imagingSession = SessionTestUtil.createSession(ImagingModule.IMAGING, |
106 |
|
"/myGenerator/path-to/dummyUri/generated.@type=mgnl:resource", |
107 |
|
"/myGenerator/path-to/dummyUri/generated.jcr\\:data=" + new String(new byte[] { 1, 2, 3 }, "UTF-8"), |
108 |
|
"/myGenerator/path-to/dummyUri/generated.jcr\\:mimeType=image/gif", |
109 |
|
"/myGenerator/path-to/dummyUri/generated.jcr\\:lastModified=123" |
110 |
|
); |
111 |
|
|
112 |
|
|
113 |
0 |
final MockWebContext ctx = new MockWebContext(); |
114 |
0 |
when(req.getRequestURI()).thenReturn("dummyUri"); |
115 |
0 |
when(res.getOutputStream()).thenReturn(servletOut); |
116 |
|
|
117 |
0 |
final ParameterProviderFactory<HttpServletRequest, String> ppFactory = new ParameterProviderFactory<HttpServletRequest, String>() { |
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
118 |
0 |
@Override... |
119 |
|
public ParameterProvider<String> newParameterProviderFor(HttpServletRequest context) { |
120 |
0 |
return new StringParameterProvider(context.getRequestURI()); |
121 |
|
} |
122 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
123 |
|
@Override... |
124 |
|
public CachingStrategy<String> getCachingStrategy() { |
125 |
|
return new StringBasedCachingStrategy(); |
126 |
|
} |
127 |
|
}; |
128 |
|
|
129 |
0 |
imageGenerator = new TestImageGenerator(ppFactory, outputFormat); |
130 |
0 |
imagingModule.setGenerators(Collections.<String, ImageGenerator>singletonMap(imageGenerator.getName(), imageGenerator)); |
131 |
|
|
132 |
0 |
Provider<SystemContext> contextProvider = mock(Provider.class); |
133 |
0 |
when(contextProvider.get()).thenReturn(ctx); |
134 |
0 |
imagingServlet = new ImagingServlet(new Imaging(imagingModule, contextProvider), new Provider<ImagingModule>() { |
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
135 |
0 |
@Override... |
136 |
|
public ImagingModule get() { |
137 |
0 |
return imagingModule; |
138 |
|
} |
139 |
|
}, mock(MagnoliaConfigurationProperties.class)); |
140 |
|
|
141 |
0 |
ctx.setUser(mock(User.class)); |
142 |
|
|
143 |
0 |
MgnlContext.setInstance(ctx); |
144 |
|
|
145 |
0 |
ComponentsTestUtil.setInstance(SystemContext.class, ctx); |
146 |
0 |
MockUtil.setSystemContextSessionAndHierarchyManager(imagingSession); |
147 |
0 |
MockUtil.setSystemContextSessionAndHierarchyManager(configMimeMapping); |
148 |
|
|
149 |
0 |
MIMEMapping.init(); |
150 |
|
} |
151 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
152 |
0 |
@After... |
153 |
|
public void tearDown() throws Exception { |
154 |
0 |
MgnlContext.setInstance(null); |
155 |
0 |
ComponentsTestUtil.clear(); |
156 |
|
} |
157 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
4-
|
|
158 |
0 |
@Test... |
159 |
|
public void requestToFactoryToGeneratorToImage() throws Exception { |
160 |
|
|
161 |
0 |
when(req.getPathInfo()).thenReturn("/myGenerator/someWorkspace/some/path/to/a/node.png"); |
162 |
0 |
outputFormat.setFormatName("png"); |
163 |
|
|
164 |
|
|
165 |
0 |
imagingServlet.doGet(req, res); |
166 |
|
|
167 |
|
|
168 |
0 |
verify(res, times(1)).flushBuffer(); |
169 |
|
|
170 |
0 |
assertTrue("ImageGenerator.generate() wasn't called !", imageGenerator.imageGeneratorWasCalled); |
171 |
|
|
172 |
|
|
173 |
|
|
174 |
|
|
175 |
|
|
176 |
|
|
177 |
|
|
178 |
|
|
179 |
|
} |
180 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
4-
|
|
181 |
0 |
@Test... |
182 |
|
public void mimeTypeIsSetWhenCachingGeneratedImages() throws Exception { |
183 |
|
|
184 |
0 |
when(req.getPathInfo()).thenReturn("/myGenerator/someWorkspace/some/path/to/a/node.JPG"); |
185 |
0 |
outputFormat.setFormatName("JPG"); |
186 |
|
|
187 |
0 |
imagingModule.setStoreGeneratedImages(true); |
188 |
|
|
189 |
|
|
190 |
0 |
imagingServlet.doGet(req, res); |
191 |
|
|
192 |
|
|
193 |
0 |
verify(res).setContentType("image/jpeg"); |
194 |
|
} |
195 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
4-
|
|
196 |
0 |
@Test... |
197 |
|
public void mimeTypeIsSetWhenNotCachingGeneratedImages() throws Exception { |
198 |
|
|
199 |
0 |
when(req.getPathInfo()).thenReturn("/myGenerator/someWorkspace/some/path/to/a/node.JPG"); |
200 |
0 |
outputFormat.setFormatName("JPG"); |
201 |
|
|
202 |
0 |
imagingModule.setStoreGeneratedImages(false); |
203 |
|
|
204 |
|
|
205 |
0 |
imagingServlet.doGet(req, res); |
206 |
|
|
207 |
|
|
208 |
0 |
verify(res).setContentType("image/jpeg"); |
209 |
|
} |
210 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
4-
|
|
211 |
0 |
@Test... |
212 |
|
public void notFoundWhenRequestingNonExistingImage() throws Exception { |
213 |
|
|
214 |
0 |
when(req.getPathInfo()).thenReturn("/myGenerator/someWorkspace/some/path/to/a/nodeDoesNotExist.jpg"); |
215 |
0 |
imagingModule.setStoreGeneratedImages(false); |
216 |
|
|
217 |
|
|
218 |
0 |
imagingServlet.doGet(req, res); |
219 |
|
|
220 |
|
|
221 |
0 |
verify(res).sendError(404); |
222 |
|
} |
223 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
4-
|
|
224 |
0 |
@Test... |
225 |
|
public void notFoundWhenRequestingNonExistingPath() throws Exception { |
226 |
|
|
227 |
0 |
when(req.getPathInfo()).thenReturn("/nonsense.abc"); |
228 |
0 |
imagingModule.setStoreGeneratedImages(false); |
229 |
|
|
230 |
|
|
231 |
0 |
imagingServlet.doGet(req, res); |
232 |
|
|
233 |
|
|
234 |
0 |
verify(res).sendError(404); |
235 |
|
} |
236 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 3 |
Complexity Density: 0.43 |
|
237 |
|
private static class TestImageGenerator<P extends ParameterProvider<?>> implements ImageGenerator<P> { |
238 |
|
private final ParameterProviderFactory ppFactory; |
239 |
|
private final OutputFormat outputFormat; |
240 |
|
boolean imageGeneratorWasCalled; |
241 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
242 |
0 |
public TestImageGenerator(ParameterProviderFactory ppFactory, OutputFormat outputFormat) {... |
243 |
0 |
this.ppFactory = ppFactory; |
244 |
0 |
this.outputFormat = outputFormat; |
245 |
0 |
imageGeneratorWasCalled = false; |
246 |
|
} |
247 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
248 |
0 |
@Override... |
249 |
|
public BufferedImage generate(P params) { |
250 |
0 |
assertEquals("dummyUri", params.getParameter()); |
251 |
0 |
imageGeneratorWasCalled = true; |
252 |
0 |
return new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB); |
253 |
|
} |
254 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
255 |
|
@Override... |
256 |
|
public ParameterProviderFactory getParameterProviderFactory() { |
257 |
|
return ppFactory; |
258 |
|
} |
259 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
260 |
0 |
@Override... |
261 |
|
public OutputFormat getOutputFormat(P params) { |
262 |
0 |
return outputFormat; |
263 |
|
} |
264 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
265 |
|
@Override... |
266 |
|
public String getName() { |
267 |
|
return "myGenerator"; |
268 |
|
} |
269 |
|
} |
270 |
|
} |