Clover icon

Magnolia Resources Module 2.4.2

  1. Project Clover database Fri Nov 6 2015 16:15:26 CET
  2. Package info.magnolia.module.resources.renderers

File ResourcesBinaryRendererTest.java

 

Code metrics

0
75
8
1
241
148
8
0.11
9.38
8
1

Classes

Class Line # Actions
ResourcesBinaryRendererTest 80 75 0% 8 0
1.0100%
 

Contributing tests

This file is covered by 6 tests. .

Source view

1    /**
2    * This file Copyright (c) 2014-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.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    * Tests for {@link ResourcesTextTemplateRenderer}.
79    */
 
80    public class ResourcesBinaryRendererTest {
81   
82    // Init global
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   
 
90  6 toggle @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   
 
124  6 toggle @After
125    public void tearDown() {
126  6 MgnlContext.setInstance(null);
127  6 ComponentsTestUtil.clear();
128    }
129   
 
130  1 toggle @Test
131    public void testResourceLoadedFromContentNode() throws Exception {
132    // GIVEN
133   
134    // WHEN
135  1 renderer.render(ctx, null);
136   
137    // THEN
138  1 assertThat(ctx.getOutputStream().toString(), is(binaryContent));
139  1 verify(response).setContentLength(binaryContent.length());
140    }
141   
 
142  1 toggle @Test
143    public void testResourceLoadedFromClassPath() throws Exception {
144    // GIVEN
145  1 node.setProperty(ResourcesBinaryRenderer.EXTENSION_PROPERTY, "png"); // test node name including extension
146  1 node.setProperty(ResourcesBinaryRenderer.BYPASS_PROPERTY, true);
147   
148    // WHEN
149  1 renderer.render(ctx, null);
150   
151    // THEN
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   
 
157  1 toggle @Test
158    public void testIfResourceLoaderThrowsIOExceptionThenUseNextResourceLoader() throws Exception {
159    // GIVEN
160  1 node.setProperty(ResourcesBinaryRenderer.EXTENSION_PROPERTY, "png"); // test node name including extension
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    // WHEN
172  1 renderer.render(ctx, null);
173   
174    // THEN
175  1 verify(loaderThrowingIOException).getStream("/templating-kit/img/someBinary.png"); // check if loaderThrowingIOException was used
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   
 
181  1 toggle @Test
182    public void testIfResourceLoaderReturnsContentType() throws Exception {
183    // GIVEN
184  1 node.setProperty(ResourcesBinaryRenderer.EXTENSION_PROPERTY, "png"); // test node name including extension
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    // WHEN
193  1 renderer.render(ctx, null);
194   
195    // THEN
196  1 verify(response).setContentType("mime/type");
197    }
198   
 
199  1 toggle @Test
200    public void testIfResourceWithExtensionLoadedFromClassPathReturnsNullContentType() throws Exception {
201    // GIVEN
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"); // test node name including extension
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    // WHEN
214  1 renderer.render(ctx, null);
215   
216    // THEN
217  1 verify(response).setContentType("mime/type");
218    }
219   
 
220  1 toggle @Test
221    public void testIfResourceWithoutExtensionLoadedFromClassPathReturnsNullContentType() throws Exception {
222    // GIVEN
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    // WHEN
234  1 renderer.render(ctx, null);
235   
236    // THEN
237  1 verify(response, never()).setContentType(anyString());
238  1 assertTrue(file.delete());
239    }
240   
241    }