1. Project Clover database Fri Mar 6 2015 14:07:48 CET
  2. Package info.magnolia.module.blossom.template

File TemplateDefinitionBuilderTest.java

 

Code metrics

6
48
15
12
236
161
19
0.4
3.2
1.25
1.27

Classes

Class Line # Actions
TemplateDefinitionBuilderTest 65 48 0% 19 11
0.840579784.1%
TemplateDefinitionBuilderTest.TestTemplate 70 0 - 0 0
-1.0 -
TemplateDefinitionBuilderTest.TemplateWithoutTemplateAnnotation 105 0 - 0 0
-1.0 -
TemplateDefinitionBuilderTest.AreaWithMaxComponentsSet 119 0 - 0 0
-1.0 -
TemplateDefinitionBuilderTest.AreaWithoutMaxComponentsSet 129 0 - 0 0
-1.0 -
TemplateDefinitionBuilderTest.AbstractTestTemplate 138 0 - 0 0
-1.0 -
TemplateDefinitionBuilderTest.AbstractTestTemplate.AreaInSuperclass 141 0 - 0 0
-1.0 -
TemplateDefinitionBuilderTest.TestTemplateWithAreasFromSuperclass 146 0 - 0 0
-1.0 -
TemplateDefinitionBuilderTest.TestTemplateWithAreasFromSuperclass.AreaInClass 149 0 - 0 0
-1.0 -
TemplateDefinitionBuilderTest.AreaWithoutCreateAreaNodeSetToTrue 177 0 - 0 0
-1.0 -
TemplateDefinitionBuilderTest.AreaWithoutCreateAreaNodeSetToFalse 181 0 - 0 0
-1.0 -
TemplateDefinitionBuilderTest.AreaWithoutCreateAreaNodeNotSet 185 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 = "base")
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("base", description.getId());
97  1 assertEquals("/test/test/", description.getHandlerPath());
98  1 assertFalse(description.getVisible());
99  1 assertEquals("Text template", description.getDescription());
100  1 assertSame(dispatcher, description.getDispatcher());
101  1 assertSame(template, description.getHandler());
102  1 assertEquals("some-base-name", description.getI18nBasename());
103    }
104   
 
105    public static class TemplateWithoutTemplateAnnotation {
106    }
107   
 
108  1 toggle @Test
109    public void testMissingTemplateAnnotation() {
110  1 try {
111  1 buildTemplateDefinition(new TemplateWithoutTemplateAnnotation());
112  0 fail();
113    } catch (IllegalArgumentException e) {
114  1 assertEquals("Could not resolve template id, @Template is not present on class [info.magnolia.module.blossom.template.TemplateDefinitionBuilderTest$TemplateWithoutTemplateAnnotation]", e.getMessage());
115    }
116    }
117   
118    @Area(value = "Area", maxComponents = 3)
 
119    public static class AreaWithMaxComponentsSet {
120    }
121   
 
122  1 toggle @Test
123    public void testMaximumComponents() {
124  1 BlossomAreaDefinition definition = buildAreaDefinition(new AreaWithMaxComponentsSet());
125  1 assertEquals(new Integer(3), definition.getMaxComponents());
126    }
127   
128    @Area(value = "Area")
 
129    public static class AreaWithoutMaxComponentsSet {
130    }
131   
 
132  1 toggle @Test
133    public void testMaximumComponentsNullWhenNotSet() {
134  1 BlossomAreaDefinition definition = buildAreaDefinition(new AreaWithoutMaxComponentsSet());
135  1 assertNull(definition.getMaxComponents());
136    }
137   
 
138    public static class AbstractTestTemplate {
139   
140    @Area("areaInSuperclass")
 
141    public static class AreaInSuperclass {
142    }
143    }
144   
145    @Template(id = "", title = "")
 
146    public static class TestTemplateWithAreasFromSuperclass extends AbstractTestTemplate {
147   
148    @Area("areaInClass")
 
149    public static class AreaInClass {
150    }
151    }
152   
 
153  1 toggle @Test
154    public void testFindsAreasInSuperclass() {
155   
156  1 TestTemplateWithAreasFromSuperclass template = new TestTemplateWithAreasFromSuperclass();
157  1 AbstractTestTemplate.AreaInSuperclass areaInSuperclass = new AbstractTestTemplate.AreaInSuperclass();
158  1 TestTemplateWithAreasFromSuperclass.AreaInClass areaInClass = new TestTemplateWithAreasFromSuperclass.AreaInClass();
159   
160  1 DetectedHandlersMetaData detectedHandlers = new DetectedHandlersMetaData();
161  1 detectedHandlers.addArea(new HandlerMetaData(areaInSuperclass, ""));
162  1 detectedHandlers.addArea(new HandlerMetaData(areaInClass, ""));
163  1 HandlerMetaData templateMetaData = new HandlerMetaData(template, "");
164  1 detectedHandlers.addTemplate(templateMetaData);
165   
166  1 BlossomTemplateDefinition templateDefinition = new TemplateDefinitionBuilder().buildTemplateDefinition(
167    null,
168    detectedHandlers,
169    templateMetaData);
170   
171  1 assertEquals(2, templateDefinition.getAreas().size());
172  1 assertSame(areaInClass, ((BlossomAreaDefinition) templateDefinition.getAreas().get("areaInClass")).getHandler());
173  1 assertSame(areaInSuperclass, ((BlossomAreaDefinition) templateDefinition.getAreas().get("areaInSuperclass")).getHandler());
174    }
175   
176    @Area(value = "Area", createAreaNode = TernaryBoolean.TRUE)
 
177    public static class AreaWithoutCreateAreaNodeSetToTrue {
178    }
179   
180    @Area(value = "Area", createAreaNode = TernaryBoolean.FALSE)
 
181    public static class AreaWithoutCreateAreaNodeSetToFalse {
182    }
183   
184    @Area(value = "Area")
 
185    public static class AreaWithoutCreateAreaNodeNotSet {
186    }
187   
 
188  1 toggle @Test
189    public void testCreateAreaNodeSetToTrue() throws InvocationTargetException, IllegalAccessException {
190  1 BlossomAreaDefinition definition = buildAreaDefinition(new AreaWithoutCreateAreaNodeSetToTrue());
191  1 Method method = ReflectionUtils.findMethod(definition.getClass(), "setCreateAreaNode", Boolean.class);
192  1 if (method != null) {
193  0 assertEquals(Boolean.TRUE, ReflectionUtils.findMethod(definition.getClass(), "getCreateAreaNode").invoke(definition));
194    }
195    }
196   
 
197  1 toggle @Test
198    public void testCreateAreaNodeSetToFalse() throws InvocationTargetException, IllegalAccessException {
199  1 BlossomAreaDefinition definition = buildAreaDefinition(new AreaWithoutCreateAreaNodeSetToFalse());
200  1 Method method = ReflectionUtils.findMethod(definition.getClass(), "setCreateAreaNode", Boolean.class);
201  1 if (method != null) {
202  0 assertEquals(Boolean.FALSE, ReflectionUtils.findMethod(definition.getClass(), "getCreateAreaNode").invoke(definition));
203    }
204    }
205   
 
206  1 toggle @Test
207    public void testCreateAreaNodeNotSet() throws InvocationTargetException, IllegalAccessException {
208  1 BlossomAreaDefinition definition = buildAreaDefinition(new AreaWithoutCreateAreaNodeNotSet());
209  1 Method method = ReflectionUtils.findMethod(definition.getClass(), "setCreateAreaNode", Boolean.class);
210  1 if (method != null) {
211  0 assertNull(ReflectionUtils.findMethod(definition.getClass(), "getCreateAreaNode").invoke(definition));
212    }
213    }
214   
 
215  7 toggle public static BlossomTemplateDefinition buildTemplateDefinition(Object template) {
216  7 return buildTemplateDefinition(null, template, "/foobar");
217    }
218   
 
219  8 toggle public static BlossomTemplateDefinition buildTemplateDefinition(BlossomDispatcher dispatcher, Object template, String handlerPath) {
220  8 return new TemplateDefinitionBuilder().buildTemplateDefinition(
221    dispatcher,
222    new DetectedHandlersMetaData(),
223    new HandlerMetaData(template, handlerPath, template.getClass()));
224    }
225   
 
226  5 toggle public static BlossomAreaDefinition buildAreaDefinition(Object template) {
227  5 return buildAreaDefinition(null, template, "/foobar");
228    }
229   
 
230  5 toggle public static BlossomAreaDefinition buildAreaDefinition(BlossomDispatcher dispatcher, Object template, String handlerPath) {
231  5 return new TemplateDefinitionBuilder().buildAreaDefinition(
232    dispatcher,
233    new DetectedHandlersMetaData(),
234    new HandlerMetaData(template, handlerPath, template.getClass()));
235    }
236    }