1. Project Clover database Fri Nov 20 2015 15:10:00 CET
  2. Package info.magnolia.module.blossom.template

File TemplateDefinitionBuilderTest.java

 

Code metrics

6
53
17
12
254
177
21
0.4
3.12
1.42
1.24

Classes

Class Line # Actions
TemplateDefinitionBuilderTest 70 53 0% 21 8
0.894736889.5%
TemplateDefinitionBuilderTest.TestTemplate 85 0 - 0 0
-1.0 -
TemplateDefinitionBuilderTest.TemplateWithoutTemplateAnnotation 123 0 - 0 0
-1.0 -
TemplateDefinitionBuilderTest.AreaWithMaxComponentsSet 137 0 - 0 0
-1.0 -
TemplateDefinitionBuilderTest.AreaWithoutMaxComponentsSet 147 0 - 0 0
-1.0 -
TemplateDefinitionBuilderTest.AbstractTestTemplate 156 0 - 0 0
-1.0 -
TemplateDefinitionBuilderTest.AbstractTestTemplate.AreaInSuperclass 159 0 - 0 0
-1.0 -
TemplateDefinitionBuilderTest.TestTemplateWithAreasFromSuperclass 164 0 - 0 0
-1.0 -
TemplateDefinitionBuilderTest.TestTemplateWithAreasFromSuperclass.AreaInClass 167 0 - 0 0
-1.0 -
TemplateDefinitionBuilderTest.AreaWithoutCreateAreaNodeSetToTrue 195 0 - 0 0
-1.0 -
TemplateDefinitionBuilderTest.AreaWithoutCreateAreaNodeSetToFalse 199 0 - 0 0
-1.0 -
TemplateDefinitionBuilderTest.AreaWithoutCreateAreaNodeNotSet 203 0 - 0 0
-1.0 -
 

Contributing tests

This file is covered by 14 tests. .

Source view

1    /**
2    * This file Copyright (c) 2010-2015 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.blossom.template;
35   
36    import java.io.IOException;
37    import java.lang.reflect.InvocationTargetException;
38    import java.lang.reflect.Method;
39   
40    import javax.servlet.ServletException;
41    import javax.servlet.http.HttpServletRequest;
42    import javax.servlet.http.HttpServletResponse;
43   
44    import org.junit.After;
45    import org.junit.Before;
46    import org.junit.Test;
47    import org.springframework.util.ReflectionUtils;
48    import org.springframework.web.servlet.HandlerExecutionChain;
49    import org.springframework.web.servlet.ModelAndView;
50   
51    import static junit.framework.Assert.assertNull;
52    import static org.junit.Assert.assertEquals;
53    import static org.junit.Assert.assertFalse;
54    import static org.junit.Assert.assertSame;
55    import static org.junit.Assert.fail;
56   
57    import info.magnolia.module.blossom.annotation.Area;
58    import info.magnolia.module.blossom.annotation.I18nBasename;
59    import info.magnolia.module.blossom.annotation.Template;
60    import info.magnolia.module.blossom.annotation.TemplateDescription;
61    import info.magnolia.module.blossom.annotation.TernaryBoolean;
62    import info.magnolia.module.blossom.dispatcher.BlossomDispatcher;
63    import info.magnolia.rendering.template.TemplateAvailability;
64    import info.magnolia.rendering.template.configured.DefaultTemplateAvailability;
65    import info.magnolia.test.ComponentsTestUtil;
66   
67    /**
68    * @since 1.1
69    */
 
70    public class TemplateDefinitionBuilderTest {
71   
 
72  8 toggle @Before
73    public void setUp() {
74  8 ComponentsTestUtil.setImplementation(TemplateAvailability.class, DefaultTemplateAvailability.class);
75    }
76   
 
77  8 toggle @After
78    public void tearDown() {
79  8 ComponentsTestUtil.clear();
80    }
81   
82    @I18nBasename("some-base-name")
83    @Template(title = "Text", visible = false, id = "my-module:pages/folder/name", type="some-type", subtype = "some-subtype")
84    @TemplateDescription("Text template")
 
85    public static class TestTemplate {
86    }
87   
 
88  1 toggle @Test
89    public void testTemplateDescription() throws Exception {
90   
91  1 TestTemplate template = new TestTemplate();
92  1 BlossomDispatcher dispatcher = new BlossomDispatcher() {
93   
 
94  0 toggle @Override
95    public ModelAndView executeChain(HandlerExecutionChain mappedHandler, HttpServletRequest request, HttpServletResponse response) throws Exception {
96  0 return null;
97    }
98   
 
99  0 toggle @Override
100    public void forward(String path, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
101    }
102   
 
103  0 toggle @Override
104    public void include(String path, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
105    }
106    };
107   
108  1 BlossomTemplateDefinition description = buildTemplateDefinition(dispatcher, template, "/test/test/");
109   
110  1 assertEquals("Text", description.getTitle());
111  1 assertEquals("my-module:pages/folder/name", description.getId());
112  1 assertEquals("name", description.getName());
113  1 assertEquals("/test/test/", description.getHandlerPath());
114  1 assertFalse(description.getVisible());
115  1 assertEquals("some-type", description.getType());
116  1 assertEquals("some-subtype", description.getSubtype());
117  1 assertEquals("Text template", description.getDescription());
118  1 assertSame(dispatcher, description.getDispatcher());
119  1 assertSame(template, description.getHandler());
120  1 assertEquals("some-base-name", description.getI18nBasename());
121    }
122   
 
123    public static class TemplateWithoutTemplateAnnotation {
124    }
125   
 
126  1 toggle @Test
127    public void testMissingTemplateAnnotation() {
128  1 try {
129  1 buildTemplateDefinition(new TemplateWithoutTemplateAnnotation());
130  0 fail();
131    } catch (IllegalArgumentException e) {
132  1 assertEquals("Could not resolve template id, @Template is not present on class [info.magnolia.module.blossom.template.TemplateDefinitionBuilderTest$TemplateWithoutTemplateAnnotation]", e.getMessage());
133    }
134    }
135   
136    @Area(value = "Area", maxComponents = 3)
 
137    public static class AreaWithMaxComponentsSet {
138    }
139   
 
140  1 toggle @Test
141    public void testMaximumComponents() {
142  1 BlossomAreaDefinition definition = buildAreaDefinition(new AreaWithMaxComponentsSet());
143  1 assertEquals(new Integer(3), definition.getMaxComponents());
144    }
145   
146    @Area(value = "Area")
 
147    public static class AreaWithoutMaxComponentsSet {
148    }
149   
 
150  1 toggle @Test
151    public void testMaximumComponentsNullWhenNotSet() {
152  1 BlossomAreaDefinition definition = buildAreaDefinition(new AreaWithoutMaxComponentsSet());
153  1 assertNull(definition.getMaxComponents());
154    }
155   
 
156    public static class AbstractTestTemplate {
157   
158    @Area("areaInSuperclass")
 
159    public static class AreaInSuperclass {
160    }
161    }
162   
163    @Template(id = "", title = "")
 
164    public static class TestTemplateWithAreasFromSuperclass extends AbstractTestTemplate {
165   
166    @Area("areaInClass")
 
167    public static class AreaInClass {
168    }
169    }
170   
 
171  1 toggle @Test
172    public void testFindsAreasInSuperclass() {
173   
174  1 TestTemplateWithAreasFromSuperclass template = new TestTemplateWithAreasFromSuperclass();
175  1 AbstractTestTemplate.AreaInSuperclass areaInSuperclass = new AbstractTestTemplate.AreaInSuperclass();
176  1 TestTemplateWithAreasFromSuperclass.AreaInClass areaInClass = new TestTemplateWithAreasFromSuperclass.AreaInClass();
177   
178  1 DetectedHandlersMetaData detectedHandlers = new DetectedHandlersMetaData();
179  1 detectedHandlers.addArea(new HandlerMetaData(areaInSuperclass, ""));
180  1 detectedHandlers.addArea(new HandlerMetaData(areaInClass, ""));
181  1 HandlerMetaData templateMetaData = new HandlerMetaData(template, "");
182  1 detectedHandlers.addTemplate(templateMetaData);
183   
184  1 BlossomTemplateDefinition templateDefinition = new TemplateDefinitionBuilder().buildTemplateDefinition(
185    null,
186    detectedHandlers,
187    templateMetaData);
188   
189  1 assertEquals(2, templateDefinition.getAreas().size());
190  1 assertSame(areaInClass, ((BlossomAreaDefinition) templateDefinition.getAreas().get("areaInClass")).getHandler());
191  1 assertSame(areaInSuperclass, ((BlossomAreaDefinition) templateDefinition.getAreas().get("areaInSuperclass")).getHandler());
192    }
193   
194    @Area(value = "Area", createAreaNode = TernaryBoolean.TRUE)
 
195    public static class AreaWithoutCreateAreaNodeSetToTrue {
196    }
197   
198    @Area(value = "Area", createAreaNode = TernaryBoolean.FALSE)
 
199    public static class AreaWithoutCreateAreaNodeSetToFalse {
200    }
201   
202    @Area(value = "Area")
 
203    public static class AreaWithoutCreateAreaNodeNotSet {
204    }
205   
 
206  1 toggle @Test
207    public void testCreateAreaNodeSetToTrue() throws InvocationTargetException, IllegalAccessException {
208  1 BlossomAreaDefinition definition = buildAreaDefinition(new AreaWithoutCreateAreaNodeSetToTrue());
209  1 Method method = ReflectionUtils.findMethod(definition.getClass(), "setCreateAreaNode", Boolean.class);
210  1 if (method != null) {
211  1 assertEquals(Boolean.TRUE, ReflectionUtils.findMethod(definition.getClass(), "getCreateAreaNode").invoke(definition));
212    }
213    }
214   
 
215  1 toggle @Test
216    public void testCreateAreaNodeSetToFalse() throws InvocationTargetException, IllegalAccessException {
217  1 BlossomAreaDefinition definition = buildAreaDefinition(new AreaWithoutCreateAreaNodeSetToFalse());
218  1 Method method = ReflectionUtils.findMethod(definition.getClass(), "setCreateAreaNode", Boolean.class);
219  1 if (method != null) {
220  1 assertEquals(Boolean.FALSE, ReflectionUtils.findMethod(definition.getClass(), "getCreateAreaNode").invoke(definition));
221    }
222    }
223   
 
224  1 toggle @Test
225    public void testCreateAreaNodeNotSet() throws InvocationTargetException, IllegalAccessException {
226  1 BlossomAreaDefinition definition = buildAreaDefinition(new AreaWithoutCreateAreaNodeNotSet());
227  1 Method method = ReflectionUtils.findMethod(definition.getClass(), "setCreateAreaNode", Boolean.class);
228  1 if (method != null) {
229  1 assertNull(ReflectionUtils.findMethod(definition.getClass(), "getCreateAreaNode").invoke(definition));
230    }
231    }
232   
 
233  7 toggle public static BlossomTemplateDefinition buildTemplateDefinition(Object template) {
234  7 return buildTemplateDefinition(null, template, "/foobar");
235    }
236   
 
237  8 toggle public static BlossomTemplateDefinition buildTemplateDefinition(BlossomDispatcher dispatcher, Object template, String handlerPath) {
238  8 return new TemplateDefinitionBuilder().buildTemplateDefinition(
239    dispatcher,
240    new DetectedHandlersMetaData(),
241    new HandlerMetaData(template, handlerPath, template.getClass()));
242    }
243   
 
244  5 toggle public static BlossomAreaDefinition buildAreaDefinition(Object template) {
245  5 return buildAreaDefinition(null, template, "/foobar");
246    }
247   
 
248  5 toggle public static BlossomAreaDefinition buildAreaDefinition(BlossomDispatcher dispatcher, Object template, String handlerPath) {
249  5 return new TemplateDefinitionBuilder().buildAreaDefinition(
250    dispatcher,
251    new DetectedHandlersMetaData(),
252    new HandlerMetaData(template, handlerPath, template.getClass()));
253    }
254    }