1. Project Clover database Fri Apr 29 2016 13:24:33 CEST
  2. Package info.magnolia.module.blossom.template

File TemplateDefinitionBuilderTest.java

 

Code metrics

6
63
19
13
279
196
23
0.37
3.32
1.46
1.21

Classes

Class Line # Actions
TemplateDefinitionBuilderTest 71 63 0% 23 8
0.9090909490.9%
TemplateDefinitionBuilderTest.TestTemplate 86 0 - 0 0
-1.0 -
TemplateDefinitionBuilderTest.TestTemplateWithFlags 114 0 - 0 0
-1.0 -
TemplateDefinitionBuilderTest.TemplateWithoutTemplateAnnotation 130 0 - 0 0
-1.0 -
TemplateDefinitionBuilderTest.AreaWithMaxComponentsSet 144 0 - 0 0
-1.0 -
TemplateDefinitionBuilderTest.AreaWithoutMaxComponentsSet 154 0 - 0 0
-1.0 -
TemplateDefinitionBuilderTest.AbstractTestTemplate 163 0 - 0 0
-1.0 -
TemplateDefinitionBuilderTest.AbstractTestTemplate.AreaInSuperclass 166 0 - 0 0
-1.0 -
TemplateDefinitionBuilderTest.TestTemplateWithAreasFromSuperclass 171 0 - 0 0
-1.0 -
TemplateDefinitionBuilderTest.TestTemplateWithAreasFromSuperclass.AreaInClass 174 0 - 0 0
-1.0 -
TemplateDefinitionBuilderTest.AreaWithoutCreateAreaNodeSetToTrue 202 0 - 0 0
-1.0 -
TemplateDefinitionBuilderTest.AreaWithoutCreateAreaNodeSetToFalse 206 0 - 0 0
-1.0 -
TemplateDefinitionBuilderTest.AreaWithoutCreateAreaNodeNotSet 210 0 - 0 0
-1.0 -
 

Contributing tests

This file is covered by 19 tests. .

Source view

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