1. Project Clover database Fri Jul 3 2015 14:17:25 CEST
  2. Package info.magnolia.module.blossom.template

File TemplateDefinitionBuilderTest.java

 

Code metrics

6
49
15
12
237
162
19
0.39
3.27
1.25
1.27

Classes

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