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.assertEquals; |
37 |
|
import static org.mockito.Mockito.*; |
38 |
|
import info.magnolia.cms.beans.config.MIMEMapping; |
39 |
|
import info.magnolia.cms.util.CustomServletConfig; |
40 |
|
import info.magnolia.context.MgnlContext; |
41 |
|
import info.magnolia.context.SystemContext; |
42 |
|
import info.magnolia.imaging.caching.CachingStrategy; |
43 |
|
import info.magnolia.repository.RepositoryConstants; |
44 |
|
import info.magnolia.test.ComponentsTestUtil; |
45 |
|
import info.magnolia.test.mock.MockContext; |
46 |
|
import info.magnolia.test.mock.MockUtil; |
47 |
|
import info.magnolia.test.mock.jcr.SessionTestUtil; |
48 |
|
|
49 |
|
import java.awt.image.BufferedImage; |
50 |
|
import java.io.ByteArrayOutputStream; |
51 |
|
import java.io.IOException; |
52 |
|
import java.util.HashMap; |
53 |
|
import java.util.Map; |
54 |
|
|
55 |
|
import javax.jcr.Session; |
56 |
|
import javax.servlet.ServletConfig; |
57 |
|
import javax.servlet.ServletOutputStream; |
58 |
|
import javax.servlet.http.HttpServletRequest; |
59 |
|
import javax.servlet.http.HttpServletResponse; |
60 |
|
|
61 |
|
import org.junit.After; |
62 |
|
import org.junit.Before; |
63 |
|
import org.junit.Test; |
64 |
|
|
65 |
|
|
66 |
|
@link |
67 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (48) |
Complexity: 7 |
Complexity Density: 0.17 |
|
68 |
|
public class ImagingServletTest { |
69 |
|
|
70 |
|
private final HttpServletRequest req = mock(HttpServletRequest.class); |
71 |
|
private final HttpServletResponse res = mock(HttpServletResponse.class); |
72 |
|
private ImagingServlet imagingServlet; |
73 |
|
private final ByteArrayOutputStream fakedOut = new ByteArrayOutputStream(); |
74 |
|
private final OutputFormat outputFormat = new OutputFormat(); |
75 |
|
private final ImagingModule imagingModule = new ImagingModule(); |
76 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 1 |
Complexity Density: 0.06 |
|
77 |
3 |
@Before... |
78 |
|
public void setUp() throws Exception { |
79 |
|
|
80 |
3 |
final ServletOutputStream servletOut = new ServletOutputStream() { |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
81 |
6 |
@Override... |
82 |
|
public void write(int b) throws IOException { |
83 |
6 |
fakedOut.write(b); |
84 |
|
} |
85 |
|
}; |
86 |
3 |
Session configMimeMapping = SessionTestUtil.createSession(RepositoryConstants.CONFIG, |
87 |
|
"/server/MIMEMapping/jpg\n" + |
88 |
|
"/server/MIMEMapping/jpg.extension=jpg\n" + |
89 |
|
"/server/MIMEMapping/jpg.mime-type=image/jpeg\n" |
90 |
|
); |
91 |
3 |
Session imagingSession = SessionTestUtil.createSession("imaging", |
92 |
|
"/myGenerator/path-to/dummyUri/generated\n" + |
93 |
|
"/myGenerator/path-to/dummyUri/generated.generated-image=" + new String(new byte[]{1,2,3}, "UTF-8") |
94 |
|
); |
95 |
|
|
96 |
3 |
final SystemContext ctx = new MockContext(); |
97 |
3 |
when(req.getRequestURI()).thenReturn("dummyUri"); |
98 |
3 |
when(res.getOutputStream()).thenReturn(servletOut); |
99 |
3 |
res.flushBuffer(); |
100 |
|
|
101 |
3 |
final ParameterProviderFactory<HttpServletRequest, String> ppFactory = new ParameterProviderFactory<HttpServletRequest, String>() { |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
102 |
3 |
@Override... |
103 |
|
public ParameterProvider<String> newParameterProviderFor(HttpServletRequest context) { |
104 |
3 |
return new StringParameterProvider(context.getRequestURI()); |
105 |
|
} |
106 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
107 |
|
@Override... |
108 |
|
public CachingStrategy<String> getCachingStrategy() { |
109 |
|
return new StringBasedCachingStrategy(); |
110 |
|
} |
111 |
|
}; |
112 |
|
|
113 |
3 |
final TestImageGenerator<StringParameterProvider> generator = new TestImageGenerator<StringParameterProvider>(ppFactory, outputFormat); |
114 |
|
|
115 |
3 |
imagingModule.addGenerator(generator.getName(), generator); |
116 |
3 |
imagingServlet = new ImagingServlet(imagingModule); |
117 |
|
|
118 |
3 |
MgnlContext.setInstance(ctx); |
119 |
|
|
120 |
3 |
ComponentsTestUtil.setInstance(SystemContext.class, ctx); |
121 |
3 |
MockUtil.setSystemContextSessionAndHierarchyManager(imagingSession); |
122 |
3 |
MockUtil.setSystemContextSessionAndHierarchyManager(configMimeMapping); |
123 |
|
|
124 |
3 |
MIMEMapping.init(); |
125 |
|
} |
126 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
127 |
3 |
@After... |
128 |
|
public void tearDown() throws Exception { |
129 |
3 |
MgnlContext.setInstance(null); |
130 |
|
} |
131 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1PASS
|
|
132 |
1 |
@Test... |
133 |
|
public void testRequestToFactoryToGeneratorToImage() throws Exception { |
134 |
|
|
135 |
1 |
when(req.getPathInfo()).thenReturn("/myGenerator/someWorkspace/some/path/to/a/node.png"); |
136 |
1 |
outputFormat.setFormatName("png"); |
137 |
|
|
138 |
|
|
139 |
1 |
imagingServlet.doGet(req, res); |
140 |
|
|
141 |
|
|
142 |
1 |
verify(res, atMost(2)).flushBuffer(); |
143 |
|
|
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
|
148 |
1 |
assertEquals(3, fakedOut.toByteArray().length); |
149 |
1 |
assertEquals(1, fakedOut.toByteArray()[0]); |
150 |
1 |
assertEquals(2, fakedOut.toByteArray()[1]); |
151 |
1 |
assertEquals(3, fakedOut.toByteArray()[2]); |
152 |
|
|
153 |
|
|
154 |
|
|
155 |
|
|
156 |
|
|
157 |
|
|
158 |
|
|
159 |
|
|
160 |
|
} |
161 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1PASS
|
|
162 |
1 |
@Test... |
163 |
|
public void testDoGetWrongExtension() throws Exception { |
164 |
|
|
165 |
1 |
when(req.getPathInfo()).thenReturn("/myGenerator/someWorkspace/some/path/to/a/node.wrongExtension"); |
166 |
1 |
outputFormat.setFormatName("png"); |
167 |
|
|
168 |
1 |
Map<String, String> initParameters = new HashMap<String, String>(); |
169 |
1 |
initParameters.put("validateFileExtension", "true"); |
170 |
1 |
ServletConfig config = new CustomServletConfig("ImagingServletConfig", null, initParameters); |
171 |
1 |
imagingServlet.init(config); |
172 |
|
|
173 |
|
|
174 |
1 |
imagingServlet.doGet(req, res); |
175 |
|
|
176 |
|
|
177 |
1 |
verify(res).sendError(HttpServletResponse.SC_NOT_FOUND); |
178 |
|
} |
179 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
180 |
1 |
@Test... |
181 |
|
public void testGetCorrectMimeType() throws Exception { |
182 |
|
|
183 |
1 |
when(req.getPathInfo()).thenReturn("/myGenerator/someWorkspace/some/path/to/a/node.JPG"); |
184 |
1 |
outputFormat.setFormatName("JPG"); |
185 |
|
|
186 |
1 |
ServletConfig config = new CustomServletConfig("ImagingServletConfig", null, new HashMap<String, String>()); |
187 |
1 |
imagingServlet.init(config); |
188 |
|
|
189 |
|
|
190 |
1 |
imagingServlet.doGet(req, res); |
191 |
|
|
192 |
|
|
193 |
1 |
verify(res).setContentType("image/jpeg"); |
194 |
|
} |
195 |
|
|
|
|
| 60% |
Uncovered Elements: 4 (10) |
Complexity: 3 |
Complexity Density: 0.43 |
|
196 |
|
private static class TestImageGenerator<P extends ParameterProvider<?>> implements ImageGenerator<P> { |
197 |
|
private final ParameterProviderFactory ppFactory; |
198 |
|
private final OutputFormat outputFormat; |
199 |
|
boolean imageGeneratorWasCalled; |
200 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
201 |
3 |
public TestImageGenerator(ParameterProviderFactory ppFactory, OutputFormat outputFormat) {... |
202 |
3 |
this.ppFactory = ppFactory; |
203 |
3 |
this.outputFormat = outputFormat; |
204 |
3 |
imageGeneratorWasCalled = false; |
205 |
|
} |
206 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
207 |
0 |
@Override... |
208 |
|
public BufferedImage generate(P params) { |
209 |
0 |
assertEquals("dummyUri", params.getParameter()); |
210 |
0 |
imageGeneratorWasCalled = true; |
211 |
0 |
return new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB); |
212 |
|
} |
213 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
214 |
|
@Override... |
215 |
|
public ParameterProviderFactory getParameterProviderFactory() { |
216 |
|
return ppFactory; |
217 |
|
} |
218 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
219 |
3 |
@Override... |
220 |
|
public OutputFormat getOutputFormat(P params) { |
221 |
3 |
return outputFormat; |
222 |
|
} |
223 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
224 |
|
@Override... |
225 |
|
public String getName() { |
226 |
|
return "myGenerator"; |
227 |
|
} |
228 |
|
} |
229 |
|
} |