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

File TemplateDefinitionBuilderAutoGeneratorTest.java

 

Code metrics

0
27
17
10
186
124
17
0.63
1.59
1.7
1

Classes

Class Line # Actions
TemplateDefinitionBuilderAutoGeneratorTest 58 27 0% 10 0
1.0100%
TemplateDefinitionBuilderAutoGeneratorTest.BaseArea 70 0 0% 1 1
0.00%
TemplateDefinitionBuilderAutoGeneratorTest.TestTemplate 77 0 - 0 0
-1.0 -
TemplateDefinitionBuilderAutoGeneratorTest.TestTemplate.SimpleArea 80 0 0% 1 1
0.00%
TemplateDefinitionBuilderAutoGeneratorTest.TestTemplate.InheritsAndOverrides 87 0 0% 1 1
0.00%
TemplateDefinitionBuilderAutoGeneratorTest.TestTemplate.InheritsAndReuses 94 0 - 0 0
-1.0 -
TemplateDefinitionBuilderAutoGeneratorTest.TestTemplate.InheritsAndOverridesWithAnnotation 98 0 0% 1 1
0.00%
TemplateDefinitionBuilderAutoGeneratorTest.TestTemplate.InheritsWithAnnotation 106 0 0% 1 1
0.00%
TemplateDefinitionBuilderAutoGeneratorTest.TemplateWithMultipleAutoGeneratorMethods 149 0 - 0 0
-1.0 -
TemplateDefinitionBuilderAutoGeneratorTest.TemplateWithMultipleAutoGeneratorMethods.AreaWithMultipleAutoGeneratorMethods 152 0 0% 2 2
0.00%
 

Contributing tests

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