Clover Coverage Report - magnolia-module-templating 4.4.5
Coverage timestamp: Mon Sep 12 2011 16:32:30 CEST
67   196   13   7.44
0   131   0.19   3
9     1.44  
3    
 
  ParagraphRendererManagerTest       Line # 49 65 0% 11 4 94.4% 0.9444444
  ParagraphRendererManagerTest.DummyParagraphRenderer       Line # 150 1 0% 1 0 100% 1.0
  ParagraphRendererManagerTest.OtherDummyParagraphRenderer       Line # 156 1 0% 1 0 100% 1.0
 
  (9)
 
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.test.mock.MockUtil;
38    import junit.framework.TestCase;
39   
40    import javax.jcr.RepositoryException;
41    import java.io.IOException;
42    import java.io.Writer;
43    import java.util.Map;
44   
45    /**
46    * @author gjoseph
47    * @version $Revision: $ ($Author: $)
48    */
 
49    public class ParagraphRendererManagerTest extends TestCase {
50   
 
51  1 toggle public void testNotSpecifyingTheNamePropertyShouldMeanTheNodeNameIsUsedInstead() throws IOException, RepositoryException {
52  1 final Content node = getConfigNode(CONFIGNODE2, "/modules/test2/paragraph-renderers");
53  1 final ParagraphRendererManager prm = new ParagraphRendererManager();
54  1 prm.onRegister(node);
55  1 assertEquals(2, prm.getRenderers().size());
56  1 assertNotNull(prm.getRenderer("gazonk"));
57  1 assertNotNull(prm.getRenderer("baz"));
58    }
59   
 
60  1 toggle public void testUnknownParagraphRendererNamesThrowsException() throws IOException, RepositoryException {
61  1 final Content node = getConfigNode(CONFIGNODE1, "/modules/test/paragraph-renderers");
62  1 final ParagraphRendererManager prm = new ParagraphRendererManager();
63  1 prm.onRegister(node);
64  1 assertEquals(2, prm.getRenderers().size());
65  1 assertNotNull(prm.getRenderer("foo"));
66  1 assertNotNull(prm.getRenderer("bar"));
67  1 try {
68  1 prm.getRenderer("gazonk");
69  0 fail("Should have thrown an IllegalArgumentException");
70    } catch (IllegalArgumentException e) {
71  1 assertEquals("No paragraph renderer registered with name gazonk", e.getMessage());
72    }
73    }
74   
 
75  1 toggle public void testParagraphRenderersAreAddedProperly() throws IOException, RepositoryException {
76  1 Content node1 = getConfigNode(CONFIGNODE1, "/modules/test/paragraph-renderers");
77  1 Content node2 = getConfigNode(CONFIGNODE2, "/modules/test2/paragraph-renderers");
78   
79  1 final ParagraphRendererManager prm = new ParagraphRendererManager();
80  1 prm.onRegister(node1);
81  1 final Map renderers = prm.getRenderers();
82  1 assertEquals(2, renderers.size());
83  1 final ParagraphRenderer foo = (ParagraphRenderer) renderers.get("foo");
84  1 assertEquals(foo, prm.getRenderer("foo"));
85  1 assertTrue(renderers.get("bar") instanceof OtherDummyParagraphRenderer);
86   
87  1 prm.onRegister(node2);
88  1 assertEquals(4, prm.getRenderers().size());
89  1 assertEquals(4, renderers.size());
90  1 assertEquals(renderers, prm.getRenderers());
91  1 assertTrue(renderers.get("baz") instanceof DummyParagraphRenderer);
92  1 assertTrue(renderers.get("gazonk") instanceof DummyParagraphRenderer);
93   
94  1 prm.clear();
95  1 assertEquals(0, prm.getRenderers().size());
96  1 assertEquals(0, renderers.size());
97    }
98   
 
99  1 toggle public void testCanNotAddParagraphRenderersWithDuplicateNames() throws IOException, RepositoryException {
100  1 Content node1 = getConfigNode(CONFIGNODE1, "/modules/test/paragraph-renderers");
101  1 Content node3 = getConfigNode(CONFIGNODE3, "/modules/test3/paragraph-renderers");
102   
103  1 final ParagraphRendererManager prm = new ParagraphRendererManager();
104  1 prm.onRegister(node1);
105  1 final Map renderers = prm.getRenderers();
106  1 assertEquals(2, renderers.size());
107  1 final ParagraphRenderer foo = (ParagraphRenderer) renderers.get("foo");
108  1 assertEquals(foo, prm.getRenderer("foo"));
109  1 assertTrue(renderers.get("bar") instanceof OtherDummyParagraphRenderer);
110   
111  1 try {
112  1 prm.onRegister(node3);
113  0 fail("should have thrown an IllegalStateException");
114    } catch (IllegalStateException e) {
115  1 assertEquals("Duplicate paragraph name \"foo\"", e.getMessage());
116    // TODO : would be nice if the exception message also said "in path /modules/test3/paragraph-renderers/foo3"
117    }
118    }
119   
 
120  1 toggle public void testRenderersNamePropertyHasPriorityOverNodeName() throws IOException, RepositoryException {
121  1 Content node3 = getConfigNode(CONFIGNODE3, "/modules/test3/paragraph-renderers");
122  1 final ParagraphRendererManager prm = new ParagraphRendererManager();
123  1 prm.onRegister(node3);
124  1 final Map renderers = prm.getRenderers();
125  1 assertEquals(2, renderers.size());
126    // node name is "foo-node" will the name property is set to "foo"
127  1 assertTrue(renderers.get("foo") instanceof DummyParagraphRenderer);
128  1 try {
129  1 prm.getRenderer("foo-node");
130  0 fail("Should have thrown an IllegalArgumentException");
131    } catch (IllegalArgumentException e) {
132  1 assertEquals("No paragraph renderer registered with name foo-node", e.getMessage());
133    }
134    // just a sanity check
135  1 assertTrue(renderers.get("bar") instanceof DummyParagraphRenderer);
136    }
137   
 
138  1 toggle public void testRenderersWithFaultyClassNamesShouldNotBeIgnored() throws IOException, RepositoryException {
139  1 final Content node = getConfigNode(CONFIGNODE4, "/modules/test4/paragraph-renderers");
140  1 final ParagraphRendererManager prm = new ParagraphRendererManager();
141   
142  1 try {
143  1 prm.onRegister(node);
144  0 fail("should have thrown an IllegalStateException");
145    } catch (IllegalStateException e) {
146  1 assertEquals("Can't register paragraph renderer with name [foo] and class [com.foo.bar.baz.Gazonk] : java.lang.ClassNotFoundException : com.foo.bar.baz.Gazonk", e.getMessage());
147    }
148    }
149   
 
150    public static final class DummyParagraphRenderer implements ParagraphRenderer {
 
151  3 toggle public void render(Content content, Paragraph paragraph, Writer out) throws IOException {
152  3 out.write("tralala:" + paragraph.getName());
153    }
154    }
155   
 
156    public static final class OtherDummyParagraphRenderer implements ParagraphRenderer {
 
157  1 toggle public void render(Content content, Paragraph paragraph, Writer out) throws IOException {
158  1 out.write("trululu:" + paragraph.getName());
159    }
160    }
161   
162    private static final String CONFIGNODE1 = "" +
163    "modules.test.paragraph-renderers.foo.@type=mgnl:contentNode\n" +
164    "modules.test.paragraph-renderers.foo.name=foo\n" +
165    "modules.test.paragraph-renderers.foo.class=info.magnolia.module.templating.ParagraphRendererManagerTest$DummyParagraphRenderer\n" +
166    "modules.test.paragraph-renderers.bar.@type=mgnl:contentNode\n" +
167    "modules.test.paragraph-renderers.bar.name=bar\n" +
168    "modules.test.paragraph-renderers.bar.class=info.magnolia.module.templating.ParagraphRendererManagerTest$OtherDummyParagraphRenderer";
169   
170    private static final String CONFIGNODE2 = "" +
171    "modules.test2.paragraph-renderers.baz.@type=mgnl:contentNode\n" +
172    "modules.test2.paragraph-renderers.baz.name=baz\n" +
173    "modules.test2.paragraph-renderers.baz.class=info.magnolia.module.templating.ParagraphRendererManagerTest$DummyParagraphRenderer\n" +
174    "modules.test2.paragraph-renderers.gazonk.@type=mgnl:contentNode\n" +
175    // not specifying the name property should mean usage of the node's name "modules.test2.paragraph-renderers.gazonk.name=gazonk\n" +
176    "modules.test2.paragraph-renderers.gazonk.class=info.magnolia.module.templating.ParagraphRendererManagerTest$DummyParagraphRenderer";
177   
178    private static final String CONFIGNODE3 = "" +
179    "modules.test3.paragraph-renderers.foo-node.@type=mgnl:contentNode\n" +
180    "modules.test3.paragraph-renderers.foo-node.name=foo\n" +
181    "modules.test3.paragraph-renderers.foo-node.class=info.magnolia.module.templating.ParagraphRendererManagerTest$DummyParagraphRenderer\n" +
182    "modules.test3.paragraph-renderers.bar.@type=mgnl:contentNode\n" +
183    "modules.test3.paragraph-renderers.bar.name=bar\n" +
184    "modules.test3.paragraph-renderers.bar.class=info.magnolia.module.templating.ParagraphRendererManagerTest$DummyParagraphRenderer";
185   
186    private static final String CONFIGNODE4 = "" +
187    "modules.test4.paragraph-renderers.foo.@type=mgnl:contentNode\n" +
188    "modules.test4.paragraph-renderers.foo.name=foo\n" +
189    "modules.test4.paragraph-renderers.foo.class=com.foo.bar.baz.Gazonk";
190   
191   
 
192  8 toggle private Content getConfigNode(String configNode, String path) throws IOException, RepositoryException {
193  8 return MockUtil.createHierarchyManager(configNode).getContent(path);
194    }
195   
196    }