Clover Coverage Report - magnolia-module-templating 4.4.5
Coverage timestamp: Mon Sep 12 2011 16:32:30 CEST
95   233   15   6.79
0   169   0.16   4.67
14     1.07  
3    
 
  FreemarkerParagraphRendererTest       Line # 72 87 0% 7 1 98.9% 0.9892473
  FreemarkerParagraphRendererTest.SimpleTestState       Line # 194 6 0% 6 2 83.3% 0.8333333
  FreemarkerParagraphRendererTest.SkippableTestState       Line # 223 2 0% 2 0 100% 1.0
 
  (5)
 
1    /**
2    * This file Copyright (c) 2003-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.paragraphs;
35   
36    import static org.easymock.EasyMock.expect;
37    import static org.easymock.EasyMock.verify;
38    import static org.easymock.classextension.EasyMock.createNiceMock;
39    import static org.easymock.classextension.EasyMock.replay;
40    import freemarker.cache.StringTemplateLoader;
41    import info.magnolia.cms.beans.config.ServerConfiguration;
42    import info.magnolia.cms.core.AggregationState;
43    import info.magnolia.cms.core.Content;
44    import info.magnolia.cms.i18n.DefaultI18nContentSupport;
45    import info.magnolia.cms.i18n.EmptyMessages;
46    import info.magnolia.cms.i18n.I18nContentSupport;
47    import info.magnolia.context.MgnlContext;
48    import info.magnolia.context.WebContext;
49    import info.magnolia.freemarker.FreemarkerConfig;
50    import info.magnolia.freemarker.FreemarkerHelper;
51    import info.magnolia.link.LinkTransformerManager;
52    import info.magnolia.module.templating.Paragraph;
53    import info.magnolia.module.templating.RenderableDefinition;
54    import info.magnolia.module.templating.RenderingModel;
55    import info.magnolia.module.templating.RenderingModelImpl;
56    import info.magnolia.module.templating.engine.DefaultRenderingEngine;
57    import info.magnolia.module.templating.engine.RenderingEngine;
58    import info.magnolia.test.ComponentsTestUtil;
59    import info.magnolia.test.MgnlTestCase;
60    import info.magnolia.test.mock.MockContent;
61    import info.magnolia.test.mock.MockNodeData;
62   
63    import java.io.StringWriter;
64    import java.util.HashMap;
65    import java.util.Locale;
66    import java.util.Map;
67   
68    /**
69    * @author gjoseph
70    * @version $Revision: $ ($Author: $)
71    */
 
72    public class FreemarkerParagraphRendererTest extends MgnlTestCase {
73    private StringTemplateLoader tplLoader;
74    private FreemarkerParagraphRenderer renderer;
75   
 
76  5 toggle protected void setUp() throws Exception {
77  5 super.setUp();
78   
79  5 tplLoader = new StringTemplateLoader();
80  5 final FreemarkerConfig fmTemplateLoader = new FreemarkerConfig();
81  5 fmTemplateLoader.addTemplateLoader(tplLoader);
82  5 ComponentsTestUtil.setInstance(FreemarkerConfig.class, fmTemplateLoader);
83   
84  5 final FreemarkerHelper freemarkerHelper = new FreemarkerHelper();
85  5 renderer = new FreemarkerParagraphRenderer(freemarkerHelper);
86   
87  5 final ServerConfiguration serverConfiguration = new ServerConfiguration();
88  5 serverConfiguration.setDefaultBaseUrl("http://myTests:1234/yay");
89  5 ComponentsTestUtil.setInstance(ServerConfiguration.class, serverConfiguration);
90  5 ComponentsTestUtil.setInstance(LinkTransformerManager.class, new LinkTransformerManager());
91  5 ComponentsTestUtil.setImplementation(I18nContentSupport.class, DefaultI18nContentSupport.class);
92  5 ComponentsTestUtil.setImplementation(RenderingEngine.class, DefaultRenderingEngine.class);
93   
94  5 final WebContext context = createNiceMock(WebContext.class);
95  5 AggregationState state = new AggregationState();
96  5 state.setLocale(Locale.ENGLISH);
97  5 expect(context.getAggregationState()).andStubReturn(state);
98  5 expect(context.getLocale()).andReturn(Locale.ENGLISH);
99   
100  5 MgnlContext.setInstance(context);
101  5 replay(context);
102    }
103   
 
104  1 toggle public void testWorksWithNonActionParagraphAndContentIsExposedToFreemarker() throws Exception {
105  1 tplLoader.putTemplate("test_noclass.ftl", "This is a test template, rendering the content node under ${content.@handle} with UUID ${content.@uuid}.\n" +
106    "The value of the foo property is ${content.foo}.");
107   
108  1 final MockContent c = new MockContent("plop");
109  1 c.setUUID("123");
110  1 c.addNodeData(new MockNodeData("foo", "bar"));
111   
112  1 final StringWriter out = new StringWriter();
113  1 final Paragraph p = new Paragraph();
114  1 p.setName("test-para");
115  1 p.setTemplatePath("test_noclass.ftl");
116  1 renderer.render(c, p, out);
117   
118  1 assertEquals("This is a test template, rendering the content node under /plop with UUID 123.\n" +
119    "The value of the foo property is bar.", out.toString());
120    }
121   
 
122  1 toggle public void testActionClassGetsExecutedAndIsPutOnContextAlongWithResultAndContent() throws Exception {
123  1 tplLoader.putTemplate("test_action.ftl", "${content.boo} : ${model.pouet} : ${actionResult}");
124  1 final Paragraph par = new Paragraph();
125  1 par.setName("test-with-action");
126  1 par.setTemplatePath("test_action.ftl");
127  1 par.setModelClass(SimpleTestState.class);
128  1 final MockContent c = new MockContent("plop");
129  1 c.addNodeData(new MockNodeData("boo", "yay"));
130  1 final StringWriter out = new StringWriter();
131  1 renderer.render(c, par, out);
132  1 assertEquals("yay : it works : success", out.toString());
133    }
134   
 
135  1 toggle public void testActionGetsPopulated() throws Exception {
136  1 final WebContext context = createNiceMock(WebContext.class);
137  1 Map<String,String> params=new HashMap<String,String>();
138  1 params.put("blah", "tralala");
139  1 params.put("foo", "bar");
140  1 expect(context.getParameters()).andReturn(params);
141  1 expect(context.getMessages("testmessages")).andReturn(new EmptyMessages());
142  1 expect(context.getMessages()).andReturn(new EmptyMessages());
143  1 expect(context.getLocale()).andStubReturn(Locale.ENGLISH);
144  1 expect(context.getContextPath()).andReturn("/pouet");
145  1 expect(context.getServletContext()).andReturn(null);
146  1 expect(context.getAggregationState()).andStubReturn(new AggregationState());
147  1 replay(context);
148  1 MgnlContext.setInstance(context);
149   
150  1 tplLoader.putTemplate("test_action.ftl", "${content.boo} : ${model.pouet} : ${model.blah} : ${actionResult}");
151  1 final Paragraph par = new Paragraph();
152  1 par.setName("test-with-action");
153  1 par.setI18nBasename("testmessages");
154  1 par.setTemplatePath("test_action.ftl");
155  1 par.setModelClass(SimpleTestState.class);
156  1 final MockContent c = new MockContent("plop");
157  1 c.addNodeData(new MockNodeData("boo", "yay"));
158  1 final StringWriter out = new StringWriter();
159  1 renderer.render(c, par, out);
160  1 assertEquals("yay : it works : tralala : success", out.toString());
161    }
162   
 
163  1 toggle public void testCantRenderWithoutParagraphPathCorrectlySet() throws Exception {
164  1 tplLoader.putTemplate("foo", "");
165  1 final Content c = new MockContent("pouet");
166  1 final Paragraph paragraph = new Paragraph();
167  1 paragraph.setName("plop");
168  1 try {
169  1 renderer.render(c, paragraph, new StringWriter());
170  0 fail("should have failed");
171    } catch (IllegalStateException e) {
172  1 assertEquals("Unable to render info.magnolia.module.templating.Paragraph plop in page /pouet: templatePath not set.", e.getMessage());
173    }
174    }
175   
 
176  1 toggle public void testSkipRendering() throws Exception {
177  1 final WebContext webContext = createNiceMock(WebContext.class);
178  1 MgnlContext.setInstance(webContext);
179  1 final AggregationState aggState = new AggregationState();
180  1 expect(webContext.getAggregationState()).andReturn(aggState);
181  1 replay(webContext);
182  1 final Content c = new MockContent("pouet");
183  1 final Paragraph par = new Paragraph();
184  1 par.setName("plop");
185  1 par.setTemplatePath("do_not_render_me.ftl");
186  1 par.setModelClass(SkippableTestState.class);
187  1 final FreemarkerParagraphRenderer renderer = new FreemarkerParagraphRenderer();
188  1 final StringWriter out = new StringWriter();
189  1 renderer.render(c, par, out);
190  1 assertTrue(out.getBuffer().length() == 0);
191  1 verify(webContext);
192    }
193   
 
194    public static final class SimpleTestState extends RenderingModelImpl{
 
195  2 toggle public SimpleTestState(Content content, RenderableDefinition definition, RenderingModel parent) {
196  2 super(content, definition, parent);
197    }
198   
199    private String pouet = "it works";
200    private String blah;
201   
 
202  2 toggle public String execute() {
203  2 return "success";
204    }
205   
 
206  2 toggle public String getPouet() {
207  2 return pouet;
208    }
209   
 
210  0 toggle public void setPouet(String pouet) {
211  0 this.pouet = pouet;
212    }
213   
 
214  1 toggle public String getBlah() {
215  1 return blah;
216    }
217   
 
218  1 toggle public void setBlah(String blah) {
219  1 this.blah = blah;
220    }
221    }
222   
 
223    public static final class SkippableTestState extends RenderingModelImpl {
224   
 
225  1 toggle public SkippableTestState(Content content, RenderableDefinition definition, RenderingModel parent) {
226  1 super(content, definition, parent);
227    }
 
228  1 toggle @Override
229    public String execute() {
230  1 return RenderingModel.SKIP_RENDERING;
231    }
232    }
233    }