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

File TemplateDefinitionBuilderAutoGeneratorTest.java

 

Code metrics

0
25
15
10
171
111
15
0.6
1.67
1.5
1

Classes

Class Line # Actions
TemplateDefinitionBuilderAutoGeneratorTest 53 25 0% 8 0
1.0100%
TemplateDefinitionBuilderAutoGeneratorTest.BaseArea 55 0 0% 1 1
0.00%
TemplateDefinitionBuilderAutoGeneratorTest.TestTemplate 62 0 - 0 0
-1.0 -
TemplateDefinitionBuilderAutoGeneratorTest.TestTemplate.SimpleArea 65 0 0% 1 1
0.00%
TemplateDefinitionBuilderAutoGeneratorTest.TestTemplate.InheritsAndOverrides 72 0 0% 1 1
0.00%
TemplateDefinitionBuilderAutoGeneratorTest.TestTemplate.InheritsAndReuses 79 0 - 0 0
-1.0 -
TemplateDefinitionBuilderAutoGeneratorTest.TestTemplate.InheritsAndOverridesWithAnnotation 83 0 0% 1 1
0.00%
TemplateDefinitionBuilderAutoGeneratorTest.TestTemplate.InheritsWithAnnotation 91 0 0% 1 1
0.00%
TemplateDefinitionBuilderAutoGeneratorTest.TemplateWithMultipleAutoGeneratorMethods 134 0 - 0 0
-1.0 -
TemplateDefinitionBuilderAutoGeneratorTest.TemplateWithMultipleAutoGeneratorMethods.AreaWithMultipleAutoGeneratorMethods 137 0 0% 2 2
0.00%
 

Contributing tests

This file is covered by 6 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 static org.junit.Assert.assertEquals;
37    import static org.junit.Assert.assertNotNull;
38    import static org.junit.Assert.assertNull;
39   
40    import info.magnolia.module.blossom.annotation.Area;
41    import info.magnolia.module.blossom.annotation.AutoGenerator;
42    import info.magnolia.module.blossom.annotation.Template;
43    import info.magnolia.rendering.template.AreaDefinition;
44   
45    import javax.jcr.Node;
46   
47    import org.junit.Test;
48    import org.springframework.beans.BeanUtils;
49   
50    /**
51    * @since 3.0.2
52    */
 
53    public class TemplateDefinitionBuilderAutoGeneratorTest {
54   
 
55    public static class BaseArea {
 
56  0 toggle @AutoGenerator
57    public void generate(Node node) {
58    }
59    }
60   
61    @Template(title = "Text", id = "base")
 
62    public static class TestTemplate {
63   
64    @Area("SimpleArea")
 
65    public static class SimpleArea {
 
66  0 toggle @AutoGenerator
67    public void generate(Node node) {
68    }
69    }
70   
71    @Area("InheritsAndOverrides")
 
72    public static class InheritsAndOverrides extends BaseArea {
 
73  0 toggle @Override
74    public void generate(Node node) {
75    }
76    }
77   
78    @Area("InheritsAndReuses")
 
79    public static class InheritsAndReuses extends BaseArea {
80    }
81   
82    @Area("InheritsAndOverridesWithAnnotation")
 
83    public static class InheritsAndOverridesWithAnnotation extends BaseArea {
 
84  0 toggle @Override
85    @AutoGenerator
86    public void generate(Node node) {
87    }
88    }
89   
90    @Area("InheritsWithAnnotation")
 
91    public static class InheritsWithAnnotation extends BaseArea {
 
92  0 toggle @AutoGenerator
93    public void generate2(Node node) {
94    }
95    }
96    }
97   
 
98  1 toggle @Test
99    public void testAutoGenerator() throws Exception {
100  1 BlossomTemplateDefinition description = buildTemplateDefinition(new TestTemplate(), TestTemplate.SimpleArea.class);
101  1 AreaDefinition area = description.getAreas().get("SimpleArea");
102  1 assertUsingAutoGeneratorMethod(area, TestTemplate.SimpleArea.class, TestTemplate.SimpleArea.class, "generate");
103    }
104   
 
105  1 toggle @Test
106    public void testAutoGeneratorOverriding() throws Exception {
107  1 BlossomTemplateDefinition description = buildTemplateDefinition(new TestTemplate(), TestTemplate.InheritsAndOverrides.class);
108  1 AreaDefinition area = description.getAreas().get("InheritsAndOverrides");
109  1 assertNull(area.getAutoGeneration());
110    }
111   
 
112  1 toggle @Test
113    public void testAutoGeneratorInheritance() throws Exception {
114  1 BlossomTemplateDefinition description = buildTemplateDefinition(new TestTemplate(), TestTemplate.InheritsAndReuses.class);
115  1 AreaDefinition area = description.getAreas().get("InheritsAndReuses");
116  1 assertUsingAutoGeneratorMethod(area, TestTemplate.InheritsAndReuses.class, BaseArea.class, "generate");
117    }
118   
 
119  1 toggle @Test
120    public void testAutoGeneratorOverridingWithAnnotation() throws Exception {
121  1 BlossomTemplateDefinition description = buildTemplateDefinition(new TestTemplate(), TestTemplate.InheritsAndOverridesWithAnnotation.class);
122  1 AreaDefinition area = description.getAreas().get("InheritsAndOverridesWithAnnotation");
123  1 assertUsingAutoGeneratorMethod(area, TestTemplate.InheritsAndOverridesWithAnnotation.class, TestTemplate.InheritsAndOverridesWithAnnotation.class, "generate");
124    }
125   
 
126  1 toggle @Test
127    public void testAutoGeneratorInheritsAndOverridesWithAnnotation() throws Exception {
128  1 BlossomTemplateDefinition description = buildTemplateDefinition(new TestTemplate(), TestTemplate.InheritsWithAnnotation.class);
129  1 AreaDefinition area = description.getAreas().get("InheritsWithAnnotation");
130  1 assertUsingAutoGeneratorMethod(area, TestTemplate.InheritsWithAnnotation.class, TestTemplate.InheritsWithAnnotation.class, "generate2");
131    }
132   
133    @Template(title = "Too many auto generator methods", id = "notUsed")
 
134    public static class TemplateWithMultipleAutoGeneratorMethods {
135   
136    @Area("AreaWithMultipleAutoGeneratorMethods")
 
137    public static class AreaWithMultipleAutoGeneratorMethods {
138   
 
139  0 toggle @AutoGenerator
140    public void generate1(Node content) {
141    }
142   
 
143  0 toggle @AutoGenerator
144    public void generate2(Node content) {
145    }
146    }
147    }
148   
 
149  1 toggle @Test(expected = IllegalStateException.class)
150    public void testMultipleAutoGenerators() {
151  1 buildTemplateDefinition(new TemplateWithMultipleAutoGeneratorMethods(), TemplateWithMultipleAutoGeneratorMethods.AreaWithMultipleAutoGeneratorMethods.class);
152    }
153   
 
154  4 toggle private void assertUsingAutoGeneratorMethod(AreaDefinition area, Class<?> areaClass, Class<?> methodClass, String methodName) {
155  4 assertNotNull(area.getAutoGeneration());
156  4 TemplateDefinitionBuilder.BlossomAutoGenerationConfiguration configuration = (TemplateDefinitionBuilder.BlossomAutoGenerationConfiguration) area.getAutoGeneration();
157  4 assertEquals(TemplateDefinitionBuilder.BlossomGenerator.class, configuration.getGeneratorClass());
158  4 assertEquals(areaClass, configuration.getHandler().getClass());
159  4 assertEquals(methodClass, configuration.getMethod().getDeclaringClass());
160  4 assertEquals(methodName, configuration.getMethod().getName());
161    }
162   
 
163  6 toggle private BlossomTemplateDefinition buildTemplateDefinition(Object template, Class<?> areaClass) {
164  6 DetectedHandlersMetaData detectedHandlers = new DetectedHandlersMetaData();
165  6 detectedHandlers.addArea(new HandlerMetaData(BeanUtils.instantiate(areaClass), "/foobar/area", areaClass));
166  6 return new TemplateDefinitionBuilder().buildTemplateDefinition(
167    null,
168    detectedHandlers,
169    new HandlerMetaData(template, "/foobar", template.getClass()));
170    }
171    }