Clover Coverage Report - magnolia-module-templating 4.4.5
Coverage timestamp: Mon Sep 12 2011 16:32:30 CEST
34   123   6   6.8
2   63   0.18   5
5     1.2  
1    
 
  RenderableDefinitionModelTest       Line # 59 34 0% 6 2 95.1% 0.9512195
 
  (3)
 
1    /**
2    * This file Copyright (c) 2009-2011 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.templating.freemarker;
35   
36    import freemarker.ext.util.ModelFactory;
37    import freemarker.template.Configuration;
38    import freemarker.template.Template;
39    import freemarker.template.TemplateException;
40    import info.magnolia.module.templating.AbstractRenderable;
41    import info.magnolia.module.templating.RenderableDefinition;
42    import info.magnolia.module.templating.Paragraph;
43    import info.magnolia.freemarker.models.MagnoliaObjectWrapper;
44    import junit.framework.TestCase;
45   
46    import java.io.IOException;
47    import java.io.StringReader;
48    import java.io.StringWriter;
49    import java.util.HashMap;
50    import java.util.Map;
51   
52    /**
53    * These tests are a little larger than unit-level, since they actually test this model in
54    * the context of Freemarker rendering.
55    *
56    * @author gjoseph
57    * @version $Revision: $ ($Author: $)
58    */
 
59    public class RenderableDefinitionModelTest extends TestCase {
60   
 
61  1 toggle public void testRenderableDefinitionParametersAreAvailableAsTopLevelProperties() throws IOException, TemplateException {
62  1 final Map parameters = new HashMap();
63  1 parameters.put("foo", "bar");
64   
65  1 final AbstractRenderable def = new Paragraph();
66  1 def.setName("myname");
67  1 def.setParameters(parameters);
68   
69  1 Map root = new HashMap();
70  1 root.put("def", def);
71   
72  1 doTestFreemarkerRendering(":myname:bar:", ":${def.name}:${def.foo}:", root);
73    }
74   
 
75  1 toggle public void testRenderableDefinitionPropertiesHaveHigherPriorityThanParameters() throws IOException, TemplateException {
76  1 final Map parameters = new HashMap();
77  1 parameters.put("foo", "bar");
78  1 parameters.put("name", "should not appear");
79   
80  1 final AbstractRenderable def = new Paragraph();
81  1 def.setName("real name");
82  1 def.setParameters(parameters);
83   
84  1 final Map root = new HashMap();
85  1 root.put("def", def);
86   
87  1 doTestFreemarkerRendering(":real name:bar:", ":${def.name}:${def.foo}:", root);
88    }
89   
 
90  1 toggle public void testRenderableDefinitionPropertiesAreStillAvailableIfReallyNeeded() throws IOException, TemplateException {
91  1 final Map parameters = new HashMap();
92  1 parameters.put("foo", "bar");
93  1 parameters.put("name", "other name");
94   
95  1 final AbstractRenderable def = new Paragraph();
96  1 def.setName("real name");
97  1 def.setParameters(parameters);
98   
99  1 final Map root = new HashMap();
100  1 root.put("def", def);
101   
102  1 doTestFreemarkerRendering(":real name:other name:", ":${def.name}:${def.parameters.name}:", root);
103    }
104   
105    // TODO -- this could be moved elsewhere for reuse, if we let the model factory be a parameter of the test method for instance
 
106  3 toggle public static void doTestFreemarkerRendering(String expectedResult, String testTemplate, Map root) throws TemplateException, IOException {
107  3 final MagnoliaObjectWrapper objectWrapper = new MagnoliaObjectWrapper(null /* not needed in the context of this test*/) {
 
108  3 toggle protected ModelFactory getModelFactory(Class clazz) {
109  3 if (RenderableDefinition.class.isAssignableFrom(clazz)) {
110  3 return new RenderableDefinitionModel.Factory();
111    }
112  0 return super.getModelFactory(clazz);
113    }
114    };
115   
116  3 final Template template1 = new Template("test-template", new StringReader(testTemplate), new Configuration());
117  3 final StringWriter out1 = new StringWriter();
118  3 template1.process(root, out1, objectWrapper);
119  3 assertEquals(expectedResult, out1.toString());
120   
121    }
122   
123    }