Clover Coverage Report - magnolia-module-templating 4.4.5
Coverage timestamp: Mon Sep 12 2011 16:32:30 CEST
51   163   7   8.5
0   99   0.14   6
6     1.17  
1    
 
  ParagraphRenderingFacadeTest       Line # 57 51 0% 7 1 98.2% 0.98245615
 
  (3)
 
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;
35   
36    import info.magnolia.cms.core.Content;
37    import info.magnolia.cms.core.SystemProperty;
38    import info.magnolia.context.MgnlContext;
39    import info.magnolia.context.WebContext;
40    import info.magnolia.test.ComponentsTestUtil;
41    import info.magnolia.test.MgnlTestCase;
42    import info.magnolia.test.mock.MockUtil;
43   
44    import java.io.IOException;
45    import java.io.StringWriter;
46   
47    import javax.jcr.RepositoryException;
48    import javax.servlet.jsp.PageContext;
49   
50    import static org.easymock.classextension.EasyMock.*;
51   
52   
53    /**
54    * @author gjoseph
55    * @version $Revision: $ ($Author: $)
56    */
 
57    public class ParagraphRenderingFacadeTest extends MgnlTestCase {
58   
 
59  3 toggle protected void setUp() throws Exception {
60  3 MgnlContext.setInstance(null);
61  3 super.setUp();
62    }
63   
 
64  3 toggle protected void tearDown() throws Exception {
65  3 MgnlContext.setInstance(null);
66  3 ComponentsTestUtil.clear();
67  3 SystemProperty.getProperties().clear();
68  3 super.tearDown();
69    }
70   
 
71  1 toggle public void testRenderCallsTheAppropriateRenderer() throws Exception {
72  1 final ParagraphRendererManager prm = new ParagraphRendererManager();
73  1 prm.onRegister(getNode(CONFIGNODE1_RENDERER, "/modules/test/paragraph-renderers"));
74  1 final ParagraphRenderingFacade prf = new ParagraphRenderingFacade(prm, null);
75   
76  1 final Paragraph tra = new Paragraph();
77  1 tra.setName("para-one");
78  1 tra.setType("foo");
79  1 final StringWriter res = new StringWriter();
80  1 prf.render(null, tra, res);
81  1 assertEquals("tralala:para-one", res.toString());
82   
83  1 final Paragraph tru = new Paragraph();
84  1 tru.setName("para-two");
85  1 tru.setType("bar");
86  1 final StringWriter res2 = new StringWriter();
87  1 prf.render(null, tru, res2);
88  1 assertEquals("trululu:para-two", res2.toString());
89    }
90   
 
91  1 toggle public void testSetsJspPageContext() throws Exception {
92  1 final ParagraphRendererManager prm = new ParagraphRendererManager();
93  1 prm.onRegister(getNode(CONFIGNODE1_RENDERER, "/modules/test/paragraph-renderers"));
94  1 final ParagraphRenderingFacade prf = new ParagraphRenderingFacade(prm, null);
95   
96  1 final Paragraph tra = new Paragraph();
97  1 tra.setName("para-one");
98  1 tra.setType("foo");
99  1 final StringWriter res = new StringWriter();
100   
101  1 PageContext pageContext = createMock(PageContext.class);
102  1 WebContext webContext = createStrictMock(WebContext.class);
103   
104  1 webContext.setPageContext(pageContext);
105   
106  1 replay(pageContext, webContext);
107  1 MgnlContext.setInstance(webContext);
108  1 prf.render(null, tra, res, pageContext);
109  1 verify(pageContext, webContext);
110   
111  1 assertEquals("tralala:para-one", res.toString());
112    }
113   
 
114  1 toggle public void testUsesTheAppropriateParagraphWhenNotExplicitelyPassed() throws Exception {
115  1 final ParagraphRendererManager prm = new ParagraphRendererManager();
116  1 final ParagraphManager pm = new ParagraphManager();
117  1 final ParagraphRenderingFacade prf = new ParagraphRenderingFacade(prm, pm);
118   
119  1 prm.onRegister(getNode(CONFIGNODE1_RENDERER, "/modules/test/paragraph-renderers"));
120  1 final Content content = getNode(CONTENTNODE1, "/foo/bar/MyPage");
121   
122  1 try {
123  1 prf.render(content, new StringWriter());
124  0 fail("should have failed");
125    }
126    catch (IllegalStateException e) {
127  1 assertEquals("Paragraph para1 not found for page /foo/bar/MyPage", e.getMessage());
128    }
129   
130    // now register the paragraphs
131  1 pm.addParagraphToCache(getNode(CONFIG_PARAGRAPH1, "modules/test/paragraph/para1"));
132  1 pm.addParagraphToCache(getNode(CONFIG_PARAGRAPH2, "modules/test/paragraph/para2"));
133  1 final StringWriter out = new StringWriter();
134  1 prf.render(content, out);
135  1 assertEquals("tralala:para1", out.toString());
136    }
137   
138    private static final String CONFIGNODE1_RENDERER = ""
139    + "modules.test.paragraph-renderers.foo.@type=mgnl:contentNode\n"
140    + "modules.test.paragraph-renderers.foo.name=foo\n"
141    + "modules.test.paragraph-renderers.foo.class=info.magnolia.module.templating.ParagraphRendererManagerTest$DummyParagraphRenderer\n"
142    + "modules.test.paragraph-renderers.bar.@type=mgnl:contentNode\n"
143    + "modules.test.paragraph-renderers.bar.name=bar\n"
144    + "modules.test.paragraph-renderers.bar.class=info.magnolia.module.templating.ParagraphRendererManagerTest$OtherDummyParagraphRenderer";
145   
146    private static final String CONFIG_PARAGRAPH1 = ""
147    + "modules.test.paragraph.para1.@type=mgnl:contentNode\n"
148    + "modules.test.paragraph.para1.name=para1\n"
149    + "modules.test.paragraph.para1.type=foo";
150   
151    private static final String CONFIG_PARAGRAPH2 = ""
152    + "modules.test.paragraph.para2.@type=mgnl:contentNode\n"
153    + "modules.test.paragraph.para2.name=para2\n"
154    + "modules.test.paragraph.para2.type=foo";
155   
156    private static final String CONTENTNODE1 = ""
157    + "foo.bar.MyPage.text=hello\n"
158    + "foo.bar.MyPage.MetaData.mgnl\\:template=para1";
159   
 
160  6 toggle private Content getNode(String configNode, String path) throws IOException, RepositoryException {
161  6 return MockUtil.createHierarchyManager(configNode).getContent(path);
162    }
163    }