Clover icon

Magnolia Imaging Module 3.2.3

  1. Project Clover database Fri Nov 6 2015 14:54:03 CET
  2. Package info.magnolia.imaging

File ImagingServletTest.java

 

Code metrics

0
48
10
2
229
137
10
0.21
4.8
5
1
9.4% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
ImagingServletTest 68 41 4% 7 0
1.0100%
ImagingServletTest.TestImageGenerator 196 7 28.6% 3 4
0.660%
 

Contributing tests

This file is covered by 3 tests. .

Source view

1    /**
2    * This file Copyright (c) 2009-2015 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
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    * Tests for {@link ImagingServlet}.
67    */
 
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   
 
77  3 toggle @Before
78    public void setUp() throws Exception {
79    // GIVEN
80  3 final ServletOutputStream servletOut = new ServletOutputStream() {
 
81  6 toggle @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>() {
 
102  3 toggle @Override
103    public ParameterProvider<String> newParameterProviderFor(HttpServletRequest context) {
104  3 return new StringParameterProvider(context.getRequestURI());
105    }
106   
 
107    toggle @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   
 
127  3 toggle @After
128    public void tearDown() throws Exception {
129  3 MgnlContext.setInstance(null);
130    }
131   
 
132  1 toggle @Test
133    public void testRequestToFactoryToGeneratorToImage() throws Exception {
134    // GIVEN
135  1 when(req.getPathInfo()).thenReturn("/myGenerator/someWorkspace/some/path/to/a/node.png");
136  1 outputFormat.setFormatName("png");
137   
138    // WHEN
139  1 imagingServlet.doGet(req, res);
140   
141    // THEN
142  1 verify(res, atMost(2)).flushBuffer();
143   
144    // TODO - disabled for now
145    // assertTrue("ImageGenerator.generate() wasn't called !", generator.imageGeneratorWasCalled);
146   
147    // TODO - instead we're checking we've output what was given by the cache
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    // TODO - disabled for now
154    // yes, we could instead feed the test with a FileOutputStream instead of doing the shenanigans of reading/saving, but this piece below shouldn't stay here
155    // it just generates a small black jpeg...
156    // final BufferedImage img = ImageIO.read(new ByteArrayInputStream(fakedOut.toByteArray()));
157    // final String filename = getClass().getSimpleName() + ".jpg";
158    // ImageIO.write(img, "jpg", new File(filename));
159    // Runtime.getRuntime().exec("open " + filename);
160    }
161   
 
162  1 toggle @Test
163    public void testDoGetWrongExtension() throws Exception {
164    // GIVEN
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    // WHEN
174  1 imagingServlet.doGet(req, res);
175   
176    // THEN
177  1 verify(res).sendError(HttpServletResponse.SC_NOT_FOUND);
178    }
179   
 
180  1 toggle @Test
181    public void testGetCorrectMimeType() throws Exception {
182    // GIVEN
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    // WHEN
190  1 imagingServlet.doGet(req, res);
191   
192    // THEN
193  1 verify(res).setContentType("image/jpeg");
194    }
195   
 
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   
 
201  3 toggle public TestImageGenerator(ParameterProviderFactory ppFactory, OutputFormat outputFormat) {
202  3 this.ppFactory = ppFactory;
203  3 this.outputFormat = outputFormat;
204  3 imageGeneratorWasCalled = false;
205    }
206   
 
207  0 toggle @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   
 
214    toggle @Override
215    public ParameterProviderFactory getParameterProviderFactory() {
216    return ppFactory;
217    }
218   
 
219  3 toggle @Override
220    public OutputFormat getOutputFormat(P params) {
221  3 return outputFormat;
222    }
223   
 
224    toggle @Override
225    public String getName() {
226    return "myGenerator";
227    }
228    }
229    }