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 ResourcesTextTemplateRendererTest.java

 

Code metrics

0
164
13
2
436
272
13
0.08
12.62
6.5
1

Classes

Class Line # Actions
ResourcesTextTemplateRendererTest 106 162 0% 11 0
1.0100%
ResourcesTextTemplateRendererTest.DummyModel 171 2 0% 2 0
1.0100%
 

Contributing tests

This file is covered by 8 tests. .

Source view

1    /**
2    * This file Copyright (c) 2011-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   
37    import static org.hamcrest.CoreMatchers.is;
38    import static org.junit.Assert.assertThat;
39    import static org.mockito.Mockito.*;
40   
41    import info.magnolia.cms.beans.config.ServerConfiguration;
42    import info.magnolia.cms.i18n.DefaultI18nContentSupport;
43    import info.magnolia.cms.i18n.I18nContentSupport;
44    import info.magnolia.context.Context;
45    import info.magnolia.context.MgnlContext;
46    import info.magnolia.freemarker.FreemarkerConfig;
47    import info.magnolia.freemarker.FreemarkerHelper;
48    import info.magnolia.i18nsystem.LocaleProvider;
49    import info.magnolia.i18nsystem.TranslationService;
50    import info.magnolia.init.MagnoliaConfigurationProperties;
51    import info.magnolia.jcr.util.NodeTypes;
52    import info.magnolia.jcr.wrapper.HTMLEscapingNodeWrapper;
53    import info.magnolia.link.LinkTransformerManager;
54    import info.magnolia.module.resources.ResourcesModule;
55    import info.magnolia.module.resources.loaders.ClasspathResourceLoader;
56    import info.magnolia.module.resources.loaders.FileSystemResourceLoader;
57    import info.magnolia.module.resources.loaders.ResourceLoader;
58    import info.magnolia.module.resources.templates.ResourceTemplate;
59    import info.magnolia.objectfactory.Components;
60    import info.magnolia.objectfactory.guice.GuiceUtils;
61    import info.magnolia.rendering.context.RenderingContext;
62    import info.magnolia.rendering.engine.DefaultRenderingEngine;
63    import info.magnolia.rendering.engine.RenderingEngine;
64    import info.magnolia.rendering.model.RenderingModel;
65    import info.magnolia.rendering.model.RenderingModelImpl;
66    import info.magnolia.rendering.template.RenderableDefinition;
67    import info.magnolia.rendering.template.assignment.MetaDataBasedTemplateDefinitionAssignment;
68    import info.magnolia.rendering.template.assignment.TemplateDefinitionAssignment;
69    import info.magnolia.rendering.template.variation.NoopVariationResolver;
70    import info.magnolia.rendering.template.variation.RenderableVariationResolver;
71    import info.magnolia.rendering.util.AppendableWriter;
72    import info.magnolia.test.ComponentsTestUtil;
73    import info.magnolia.test.MgnlTestCase;
74    import info.magnolia.test.mock.MockContent;
75    import info.magnolia.test.mock.MockWebContext;
76    import info.magnolia.test.mock.jcr.MockNode;
77   
78    import java.io.File;
79    import java.io.IOException;
80    import java.util.Arrays;
81    import java.util.LinkedHashMap;
82    import java.util.LinkedList;
83    import java.util.List;
84    import java.util.Locale;
85    import java.util.Map;
86   
87    import javax.inject.Provider;
88    import javax.jcr.Node;
89    import javax.servlet.ServletContext;
90    import javax.servlet.http.HttpServletRequest;
91    import javax.servlet.http.HttpServletResponse;
92   
93    import org.apache.commons.io.FileUtils;
94    import org.junit.Before;
95    import org.junit.Test;
96   
97    import freemarker.cache.ClassTemplateLoader;
98    import freemarker.cache.StringTemplateLoader;
99    import freemarker.cache.TemplateLoader;
100    import junit.framework.Assert;
101   
102   
103    /**
104    * Tests for {@link ResourcesTextTemplateRenderer}.
105    */
 
106    public class ResourcesTextTemplateRendererTest extends MgnlTestCase {
107   
108    // Init global
109    private ResourcesTextTemplateRenderer renderer = null;
110    private RenderingContext ctx = null;
111    private static final String CONTENT_IDENTIFIER = "12345";
112    private MockNode content;
113    private RenderingModel<?> parentModel;
114   
115    private HttpServletResponse response;
116   
117    private ResourcesModule resourcesModule;
118   
 
119  8 toggle @Override
120    @Before
121    public void setUp() throws Exception {
122  8 super.setUp();
123   
124  8 TemplateLoader tplLoader = new StringTemplateLoader();
125  8 FreemarkerConfig fmConfig = new FreemarkerConfig();
126  8 fmConfig.setTemplateLoaders(Arrays.asList(tplLoader, new ClassTemplateLoader(getClass(), "/")));
127   
128  8 final FreemarkerHelper fmHelper = new FreemarkerHelper(fmConfig);
129  8 ComponentsTestUtil.setImplementation(RenderingEngine.class, DefaultRenderingEngine.class);
130  8 ComponentsTestUtil.setImplementation(TemplateDefinitionAssignment.class, MetaDataBasedTemplateDefinitionAssignment.class);
131  8 ComponentsTestUtil.setImplementation(RenderableVariationResolver.class, NoopVariationResolver.class);
132  8 ComponentsTestUtil.setInstance(Provider.class, mock(Provider.class));
133  8 ComponentsTestUtil.setInstance(TranslationService.class, mock(TranslationService.class));
134  8 ComponentsTestUtil.setInstance(LocaleProvider.class, mock(LocaleProvider.class));
135   
136  8 final ServerConfiguration serverConfiguration = new ServerConfiguration();
137  8 serverConfiguration.setDefaultExtension("ext");
138  8 serverConfiguration.setDefaultBaseUrl("/magnolia");
139  8 ComponentsTestUtil.setInstance(ServerConfiguration.class, serverConfiguration);
140  8 ComponentsTestUtil.setInstance(LinkTransformerManager.class, new LinkTransformerManager());
141  8 ComponentsTestUtil.setInstance(I18nContentSupport.class, new DefaultI18nContentSupport());
142   
143  8 initMagnoliaWebContext();
144   
145  8 resourcesModule = new ResourcesModule();
146  8 renderer = new ResourcesTextTemplateRenderer(fmHelper, Components.getComponent(RenderingEngine.class), GuiceUtils.providerForInstance(resourcesModule));
147    }
148   
149    /**
150    * Complete the Initialization of the MgnlContext (Web Context).
151    */
 
152  8 toggle private void initMagnoliaWebContext(){
153    // Hack we use a WebContent --> still using internally content
154  8 MockContent mockContent = new MockContent("mockContent");
155   
156    // init MgnlContext(web context)
157    // int Agregation Main Content (still a Component)
158    // ini request
159  8 parentModel = mock(RenderingModel.class);
160  8 MgnlContext.getInstance().setAttribute(RenderingModel.class.getName(), parentModel, Context.APPLICATION_SCOPE);
161  8 MgnlContext.getAggregationState().setMainContent(mockContent);
162  8 response = mock(HttpServletResponse.class);
163  8 ((MockWebContext) MgnlContext.getWebContext()).setResponse(response);
164    // Set Servlet Context
165  8 ((MockWebContext) MgnlContext.getWebContext()).setServletContext(mock(ServletContext.class));
166    // Set Local
167  8 ((MockWebContext) MgnlContext.getWebContext()).setLocale(new Locale("en"));
168  8 ((MockWebContext) MgnlContext.getWebContext()).setRequest(mock(HttpServletRequest.class));
169    }
170   
 
171    public static class DummyModel extends RenderingModelImpl<RenderableDefinition> {
172   
 
173  8 toggle public DummyModel(Node content, RenderableDefinition definition, RenderingModel<?> parent) {
174  8 super(content, definition, parent);
175    }
176   
 
177  8 toggle @Override
178    public String execute() {
179  8 return "keepOnGoing";
180    }
181    }
182   
183    /**
184    * Init Rendering context.
185    */
 
186  8 toggle private StringBuilder initRenderingContext(ResourceTemplate resourceTemplate) throws Exception{
187    //init
188  8 StringBuilder builder = new StringBuilder();
189  8 AppendableWriter writer = new AppendableWriter(builder);
190   
191    //init ctx
192  8 ctx = mock(RenderingContext.class);
193  8 when(ctx.getAppendable()).thenReturn(writer);
194   
195  8 RenderableDefinition definition = resourceTemplate;
196  8 RenderingModel newModel = mock(RenderingModel.class);
197  8 when(definition.getModelClass()).thenReturn(DummyModel.class);
198  8 when(newModel.execute()).thenReturn("keepOnGoing");
199  8 when(ctx.getCurrentContent()).thenReturn(content);
200  8 when(ctx.getRenderableDefinition()).thenReturn(definition);
201   
202  8 return builder;
203    }
204   
205   
 
206  1 toggle @Test
207    public void testOnRenderProcessed() throws Exception{
208   
209    // GIVEN
210  1 Map<String, Object> contextObjects = new LinkedHashMap<String, Object>();
211    //init Node and define content
212  1 MockNode root = new MockNode();
213  1 content = (MockNode) root.addNode("content");
214  1 content.setIdentifier(CONTENT_IDENTIFIER);
215  1 content.setProperty("extension", "css");
216  1 content.setProperty("text", "[#assign textColor=\"#ff0099\"] color = ${textColor} and content.extension = ${content.extension}");
217  1 content.setProperty("modelClass", "");
218    //init resourceTemplate
219  1 ResourceTemplate resourceTemplate = mock(ResourceTemplate.class);
220  1 when(resourceTemplate.getContentType()).thenReturn("text/css");
221  1 when(resourceTemplate.isProcessed()).thenReturn(true);
222   
223  1 StringBuilder builder = initRenderingContext(resourceTemplate);
224   
225    //WHEN
226  1 renderer.render(ctx, contextObjects);
227   
228    //THEN
229  1 Assert.assertEquals("Output should be the following: "," color = #ff0099 and content.extension = css", builder.toString());
230    }
231   
 
232  1 toggle @Test
233    public void testOnRenderNoProcessed() throws Exception{
234    // GIVEN
235  1 Map<String, Object> contextObjects = new LinkedHashMap<String, Object>();
236    //init Node and define content
237  1 MockNode root = new MockNode();
238  1 content = (MockNode) root.addNode("content");
239  1 content.setIdentifier(CONTENT_IDENTIFIER);
240  1 content.setProperty("extension", "css");
241  1 content.setProperty("text", "[#assign textColor=\"#ff0099\"] color = ${textColor} and content.extension = ${content.extension}");
242  1 content.setProperty("modelClass", "");
243    //init resourceTemplate
244  1 ResourceTemplate resourceTemplate = mock(ResourceTemplate.class);
245  1 when(resourceTemplate.getContentType()).thenReturn("text/css");
246  1 when(resourceTemplate.isProcessed()).thenReturn(false);
247   
248  1 StringBuilder builder = initRenderingContext(resourceTemplate);
249   
250    //WHEN
251  1 renderer.render(ctx, contextObjects);
252   
253    //THEN
254  1 Assert.assertEquals("Output should be the following: ","[#assign textColor=\"#ff0099\"] color = ${textColor} and content.extension = ${content.extension}", builder.toString());
255  1 verify(response).setContentType("text/css");
256    }
257   
 
258  1 toggle @Test
259    public void testOnRenderProcessedByPass() throws Exception{
260    // GIVEN
261  1 Map<String, Object> contextObjects = new LinkedHashMap<String, Object>();
262    //init Node and define content
263  1 MockNode root = new MockNode();
264    // This is the path to a template that's also on the classpath
265  1 content = (MockNode) root.addNode("info").addNode("magnolia").addNode("module").addNode("resource").addNode("test").addNode("template");
266  1 content.setIdentifier(CONTENT_IDENTIFIER);
267  1 content.setProperty("extension", "ftl");
268  1 content.setProperty("text", "");
269  1 content.setProperty("modelClass", "");
270  1 content.setProperty("bypass",true);
271    //init resourceTemplate
272  1 ResourceTemplate resourceTemplate = mock(ResourceTemplate.class);
273  1 when(resourceTemplate.getContentType()).thenReturn("text/css");
274  1 when(resourceTemplate.isProcessed()).thenReturn(true);
275   
276  1 StringBuilder builder = initRenderingContext(resourceTemplate);
277   
278    //WHEN
279  1 renderer.render(ctx, contextObjects);
280   
281    //THEN
282  1 Assert.assertEquals("Output should be the following: "," color = #ff0099 and content.extension = ftl", builder.toString());
283    }
284   
 
285  1 toggle @Test
286    public void testOnRenderNoProcessedByPass() throws Exception{
287    // GIVEN
288  1 Map<String, Object> contextObjects = new LinkedHashMap<String, Object>();
289    //init Node and define content
290  1 MockNode root = new MockNode();
291  1 content = (MockNode) root.addNode("info").addNode("magnolia").addNode("module").addNode("resource").addNode("test").addNode("template");
292  1 content.setIdentifier(CONTENT_IDENTIFIER);
293  1 content.setProperty("extension", "ftl");
294  1 content.setProperty("text", "");
295  1 content.setProperty("modelClass", "");
296  1 content.setProperty("bypass",true);
297    //init resourceTemplate
298  1 ResourceTemplate resourceTemplate = mock(ResourceTemplate.class);
299  1 when(resourceTemplate.getContentType()).thenReturn("text/css");
300  1 when(resourceTemplate.isProcessed()).thenReturn(false);
301   
302  1 StringBuilder builder = initRenderingContext(resourceTemplate);
303   
304  1 resourcesModule.addResourceLoader(new ClasspathResourceLoader());
305   
306    //WHEN
307  1 renderer.render(ctx, contextObjects);
308   
309    //THEN
310  1 Assert.assertEquals("Output should be the following: ","[#assign textColor=\"#ff0099\"] color = ${textColor} and content.extension = ${content.extension}", builder.toString());
311    }
312   
 
313  1 toggle @Test
314    public void testRenderJavascriptDecoded() throws Exception {
315    // GIVEN
316  1 Map<String, Object> contextObjects = new LinkedHashMap<String, Object>();
317   
318  1 content = new MockNode();
319  1 content.setIdentifier(CONTENT_IDENTIFIER);
320  1 content.setProperty("text", "alert(\"Hello\");");
321  1 content.setProperty("modelClass", "");
322  1 content.setProperty("bypass", false);
323   
324  1 ResourceTemplate resourceTemplate = mock(ResourceTemplate.class);
325  1 when(resourceTemplate.getContentType()).thenReturn("x-application/javascript");
326  1 when(resourceTemplate.isProcessed()).thenReturn(false);
327   
328  1 StringBuilder builder = initRenderingContext(resourceTemplate);
329   
330  1 HTMLEscapingNodeWrapper wrapped = new HTMLEscapingNodeWrapper(content, true);
331   
332  1 when(ctx.getCurrentContent()).thenReturn(wrapped);
333   
334    // WHEN
335  1 renderer.render(ctx, contextObjects);
336   
337    // THEN
338  1 Assert.assertEquals("alert(\"Hello\");", builder.toString());
339    }
340   
 
341  1 toggle @Test
342    public void testRenderCssDecoded() throws Exception {
343    // GIVEN
344  1 Map<String, Object> contextObjects = new LinkedHashMap<String, Object>();
345   
346  1 content = new MockNode();
347  1 content.setIdentifier(CONTENT_IDENTIFIER);
348  1 content.setProperty("text", "font-family:\"Open Sans\",Verdana,\"Geneva CE\",lucida,sans-serif;");
349  1 content.setProperty("modelClass", "");
350  1 content.setProperty("bypass", false);
351   
352  1 ResourceTemplate resourceTemplate = mock(ResourceTemplate.class);
353  1 when(resourceTemplate.getContentType()).thenReturn("text/css");
354  1 when(resourceTemplate.isProcessed()).thenReturn(false);
355   
356  1 StringBuilder builder = initRenderingContext(resourceTemplate);
357   
358  1 HTMLEscapingNodeWrapper wrapped = new HTMLEscapingNodeWrapper(content, true);
359   
360  1 when(ctx.getCurrentContent()).thenReturn(wrapped);
361   
362    // WHEN
363  1 renderer.render(ctx, contextObjects);
364   
365    // THEN
366  1 Assert.assertEquals("font-family:\"Open Sans\",Verdana,\"Geneva CE\",lucida,sans-serif;", builder.toString());
367    }
368   
 
369  1 toggle @Test
370    public void testResourcesAreLoadedFromArbitraryPath() throws Exception {
371    // GIVEN
372  1 Map<String, Object> contextObjects = new LinkedHashMap<String, Object>();
373   
374  1 content = new MockNode("style");
375  1 content.setIdentifier(CONTENT_IDENTIFIER);
376  1 content.setProperty("modelClass", "");
377  1 content.setProperty("bypass", true);
378  1 content.setProperty("extension", "css");
379   
380  1 ResourceTemplate resourceTemplate = mock(ResourceTemplate.class);
381  1 when(resourceTemplate.getContentType()).thenReturn("text/css");
382  1 when(resourceTemplate.isProcessed()).thenReturn(false);
383   
384  1 StringBuilder builder = initRenderingContext(resourceTemplate);
385   
386  1 when(ctx.getCurrentContent()).thenReturn(content);
387   
388  1 FileSystemResourceLoader loader = new FileSystemResourceLoader(mock(MagnoliaConfigurationProperties.class));
389  1 loader.setPath(new File("").getAbsolutePath() + "/src/test/resources/my/resources/");
390  1 resourcesModule.addResourceLoader(loader);
391   
392    // WHEN
393  1 renderer.render(ctx, contextObjects);
394   
395    // THEN
396  1 Assert.assertEquals("font-family:\"Open Sans\",Verdana,\"Geneva CE\",lucida,sans-serif;", builder.toString());
397    }
398   
 
399  1 toggle @Test
400    public void testWhenResourceLoaderThrowsIOExceptionThatNextResourceLoaderIsUsed() throws Exception {
401    // GIVEN
402  1 Map<String, Object> contextObjects = new LinkedHashMap<String, Object>();
403   
404  1 Node root = new MockNode("templating-kit");
405  1 Node content = root.addNode("test", NodeTypes.Resource.NAME);
406  1 content.setProperty("modelClass", "");
407  1 content.setProperty("bypass", true);
408  1 content.setProperty("extension", "txt");
409   
410  1 ResourceTemplate resourceTemplate = mock(ResourceTemplate.class);
411  1 when(resourceTemplate.getContentType()).thenReturn("text/css");
412  1 when(resourceTemplate.isProcessed()).thenReturn(false);
413   
414  1 StringBuilder builder = initRenderingContext(resourceTemplate);
415   
416  1 when(ctx.getCurrentContent()).thenReturn(content);
417   
418  1 ResourceLoader loaderThrowingIOException = mock(ResourceLoader.class);
419  1 when(loaderThrowingIOException.getStream("/templating-kit/test.txt")).thenThrow(new IOException());
420  1 ResourceLoader loader = new ClasspathResourceLoader();
421  1 List<ResourceLoader> loaders = new LinkedList<ResourceLoader>();
422  1 loaders.add(loaderThrowingIOException);
423  1 loaders.add(loader);
424   
425  1 resourcesModule.setResourceLoaders(loaders);
426   
427    // WHEN
428  1 renderer.render(ctx, contextObjects);
429   
430    // THEN
431  1 verify(loaderThrowingIOException).getStream("/templating-kit/test.txt"); // check if loaderThrowingIOException was used
432  1 File file = new File("src/test/resources/templating-kit/test.txt");
433  1 assertThat(FileUtils.readFileToString(file), is(builder.toString()));
434    }
435   
436    }