Clover Coverage Report - magnolia-module-templating 4.4.5
Coverage timestamp: Mon Sep 12 2011 16:32:30 CEST
85   243   14   8.5
2   144   0.16   5
10     1.4  
2    
 
  JspParagraphRendererTest       Line # 68 83 0% 12 3 96.8% 0.9677419
  JspParagraphRendererTest.SkippableTestState       Line # 233 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 info.magnolia.cms.core.SystemProperty;
37    import info.magnolia.module.templating.ModelExecutionFilter;
38    import info.magnolia.module.templating.RenderableDefinition;
39    import info.magnolia.module.templating.RenderingModel;
40    import info.magnolia.module.templating.RenderingModelImpl;
41    import info.magnolia.cms.core.AggregationState;
42    import info.magnolia.cms.core.Content;
43    import info.magnolia.cms.util.ContentWrapper;
44    import info.magnolia.context.Context;
45    import info.magnolia.context.MgnlContext;
46    import info.magnolia.context.WebContext;
47    import info.magnolia.module.templating.AbstractRenderer;
48    import info.magnolia.module.templating.Paragraph;
49    import info.magnolia.module.templating.engine.DefaultRenderingEngine;
50    import info.magnolia.module.templating.engine.RenderingEngine;
51    import info.magnolia.test.ComponentsTestUtil;
52    import info.magnolia.test.mock.MockContent;
53    import junit.framework.TestCase;
54    import static org.easymock.EasyMock.*;
55    import static org.easymock.classextension.EasyMock.createNiceMock;
56    import static org.easymock.classextension.EasyMock.replay;
57   
58    import java.io.StringWriter;
59    import java.lang.reflect.InvocationTargetException;
60    import java.lang.reflect.Method;
61    import java.util.HashMap;
62    import java.util.Map;
63   
64    /**
65    * @author gjoseph
66    * @version $Revision: $ ($Author: $)
67    */
 
68    public class JspParagraphRendererTest extends TestCase {
69   
 
70  5 toggle protected void setUp() throws Exception {
71  5 super.setUp();
72   
73  5 MgnlContext.setInstance(null);
74   
75    // shunt log4j
76  5 org.apache.log4j.Logger.getRootLogger().setLevel(org.apache.log4j.Level.OFF);
77    }
78   
 
79  5 toggle protected void tearDown() throws Exception {
80  5 MgnlContext.setInstance(null);
81  5 ComponentsTestUtil.clear();
82  5 SystemProperty.getProperties().clear();
83  5 super.tearDown();
84    }
85   
 
86  1 toggle public void testExposesNodesAsMaps() throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
87  1 final WebContext magnoliaCtx = createStrictMock(WebContext.class);
88  1 MgnlContext.setInstance(magnoliaCtx);
89  1 ComponentsTestUtil.setImplementation(RenderingEngine.class, DefaultRenderingEngine.class);
90   
91  1 final Content page = createStrictMock(Content.class);
92    // we have 2 different nodes when rendering a paragraph, but getHandle() is only called on the page node, when using NodeMapWrapper
93  1 expect(page.getHandle()).andReturn("/myPage").times(2);
94  1 final Content paragraph = createStrictMock(Content.class);
95   
96  1 final AggregationState aggState = new AggregationState();
97  1 aggState.setMainContent(page);
98  1 expect(magnoliaCtx.getAggregationState()).andStubReturn(aggState);
99   
100  1 replay(magnoliaCtx, page, paragraph);
101  1 final Map templateCtx = new HashMap();
102   
103  1 final JspParagraphRenderer renderer = new JspParagraphRenderer();
104   
105    // ugly hack to exexute renderer.setupContext()
106  1 Method setupContextMethod = AbstractRenderer.class.getDeclaredMethod("setupContext", new Class[]{Map.class, Content.class, RenderableDefinition.class, RenderingModel.class, Object.class});
107  1 setupContextMethod.setAccessible(true);
108  1 setupContextMethod.invoke(renderer, new Object[]{templateCtx, paragraph, null, null, null});
109   
110    // other tests should verify the other objects !
111  1 assertEquals("Unexpected amount of objects in context", 7, templateCtx.size());
112  1 assertTrue(templateCtx.get("actpage") instanceof Map);
113  1 assertEquals(page, unwrap((Content) templateCtx.get("actpage")));
114  1 assertTrue(templateCtx.get("content") instanceof Map);
115  1 assertEquals(paragraph, unwrap((Content) templateCtx.get("content")));
116   
117  1 verify(magnoliaCtx, page, paragraph);
118    }
119   
120    /*
121    public void testIncludesPathWhenProvided() throws IOException, ServletException {
122    final Paragraph paragraph = new Paragraph();
123    paragraph.setName("plop");
124    paragraph.setTemplatePath("/foo/bar.jsp");
125    final WebContext ctx = createStrictMock(WebContext.class);
126    MgnlContext.setInstance(ctx);
127   
128    final StringWriter w = new StringWriter();
129    final JspParagraphRenderer renderer = new JspParagraphRenderer();
130    MockAggregationState mas = new MockAggregationState();
131    mas.setMainContent(new MockContent("bla"));
132    expect(ctx.get("content")).andReturn(null);
133    expect(ctx.get("result")).andReturn(null);
134    expect(ctx.get("action")).andReturn(null);
135    expect(ctx.get("paragraphDef")).andReturn(null);
136   
137    expect(ctx.put(eq("content"), isA(NodeMapWrapper.class))).andReturn(null);
138    expect(ctx.getAggregationState()).andReturn(mas);
139    ctx.setAttribute(eq("content"), isA(NodeMapWrapper.class), eq(1));
140    ctx.setAttribute(eq("paragraphDef"), isA(Paragraph.class), eq(1));
141    ctx.include("/foo/bar.jsp", w);
142    expect(ctx.put("content", null)).andReturn(null);
143    expect(ctx.put("paragraphDef", null)).andReturn(null);
144    replay(ctx);
145   
146    renderer.render(null, paragraph, w);
147   
148    verify(ctx);
149    }
150    */
151   
 
152  1 toggle public void testCantRenderWithoutParagraphPathCorrectlySet() throws Exception {
153  1 final WebContext webContext = createNiceMock(WebContext.class);
154  1 MgnlContext.setInstance(webContext);
155  1 final AggregationState aggState = new AggregationState();
156  1 expect(webContext.getAggregationState()).andReturn(aggState);
157  1 replay(webContext);
158  1 final Content c = new MockContent("pouet");
159  1 final Paragraph paragraph = new Paragraph();
160  1 paragraph.setName("plop");
161  1 final JspParagraphRenderer renderer = new JspParagraphRenderer();
162  1 try {
163  1 renderer.render(c, paragraph, new StringWriter());
164  0 fail("should have failed");
165    } catch (IllegalStateException e) {
166  1 assertEquals("Unable to render info.magnolia.module.templating.Paragraph plop in page /pouet: templatePath not set.", e.getMessage());
167    }
168  1 verify(webContext);
169    }
170   
171   
 
172  1 toggle public void testSkipRendering() throws Exception {
173  1 final WebContext webContext = createNiceMock(WebContext.class);
174  1 MgnlContext.setInstance(webContext);
175  1 final AggregationState aggState = new AggregationState();
176  1 expect(webContext.getAggregationState()).andReturn(aggState);
177  1 replay(webContext);
178  1 final Content c = new MockContent("pouet");
179  1 final Paragraph par = new Paragraph();
180  1 par.setName("plop");
181  1 par.setTemplatePath("do_not_render_me.jsp");
182  1 par.setModelClass(SkippableTestState.class);
183  1 final JspParagraphRenderer renderer = new JspParagraphRenderer();
184  1 final StringWriter out = new StringWriter();
185  1 renderer.render(c, par, out);
186  1 assertTrue(out.getBuffer().length() == 0);
187  1 verify(webContext);
188    }
189   
190   
 
191  1 toggle public void testShouldFailIfNoContextIsSet() throws Exception {
192  1 final JspParagraphRenderer renderer = new JspParagraphRenderer();
193  1 try {
194  1 final Paragraph p = new Paragraph();
195  1 p.setName("plop");
196  1 p.setTemplatePath("/foo/bar.jsp");
197  1 renderer.render(null, p, new StringWriter());
198  0 fail("should have failed");
199    } catch (IllegalStateException e) {
200  1 assertEquals("MgnlContext is not set for this thread", e.getMessage());
201    }
202    }
203   
 
204  1 toggle public void testShouldFailIfContextIsNotWebContext() throws Exception {
205  1 Content content = createStrictMock(Content.class);
206  1 expect(content.getUUID()).andReturn("content-uuid");
207   
208  1 Context context = createStrictMock(Context.class);
209  1 expect(context.getAttribute("info.magnolia.module.templating.RenderingModel")).andReturn(null);
210  1 expect(context.getAttribute(ModelExecutionFilter.MODEL_ATTRIBUTE_PREFIX + "content-uuid")).andReturn(null);
211  1 replay(content, context);
212   
213  1 MgnlContext.setInstance(context);
214  1 final JspParagraphRenderer renderer = new JspParagraphRenderer();
215  1 try {
216  1 final Paragraph p = new Paragraph();
217  1 p.setName("plop");
218  1 p.setTemplatePath("/foo/bar.jsp");
219  1 renderer.render(content, p, new StringWriter());
220  0 fail("should have failed");
221    } catch (IllegalStateException e) {
222  1 assertEquals("JspParagraphRenderer can only be used with a WebContext", e.getMessage());
223    }
224    }
225   
 
226  6 toggle private Content unwrap(Content c) {
227  6 if (c instanceof ContentWrapper) {
228  4 return unwrap(((ContentWrapper) c).getWrappedContent());
229    }
230  2 return c;
231    }
232   
 
233    public static final class SkippableTestState extends RenderingModelImpl {
234   
 
235  1 toggle public SkippableTestState(Content content, RenderableDefinition definition, RenderingModel parent) {
236  1 super(content, definition, parent);
237    }
 
238  1 toggle @Override
239    public String execute() {
240  1 return RenderingModel.SKIP_RENDERING;
241    }
242    }
243    }