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

File TemplateExporterTest.java

 

Code metrics

0
68
23
9
292
191
23
0.34
2.96
2.56
1

Classes

Class Line # Actions
TemplateExporterTest 73 60 0% 6 0
1.0100%
TemplateExporterTest.TestTemplate 82 0 0% 1 1
0.00%
TemplateExporterTest.TestTemplate.TestArea 85 0 0% 1 1
0.00%
TemplateExporterTest.TemplateWithPreRegisterCallbackThatReturnsNull 121 1 0% 2 1
0.666666766.7%
TemplateExporterTest.TemplateWithAreaWithPreRegisterCallbackThatReturnsNull 151 0 0% 1 1
0.00%
TemplateExporterTest.TemplateWithAreaWithPreRegisterCallbackThatReturnsNull.TestArea 154 1 0% 2 3
0.00%
TemplateExporterTest.TemplateWithPreRegisterCallbacks 193 2 0% 4 2
0.666666766.7%
TemplateExporterTest.TemplateWithPreRegisterCallbacks.AbstractArea 195 2 0% 2 0
1.0100%
TemplateExporterTest.TemplateWithPreRegisterCallbacks.TestArea 209 2 0% 4 2
0.666666766.7%
 

Contributing tests

This file is covered by 4 tests. .

Source view

1    /**
2    * This file Copyright (c) 2014-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.assertTrue;
38    import static org.mockito.Mockito.mock;
39   
40    import info.magnolia.module.blossom.annotation.Area;
41    import info.magnolia.module.blossom.annotation.PreRegister;
42    import info.magnolia.module.blossom.annotation.TabFactory;
43    import info.magnolia.module.blossom.annotation.Template;
44    import info.magnolia.module.blossom.dialog.BlossomDialogDescription;
45    import info.magnolia.module.blossom.dispatcher.BlossomDispatcher;
46    import info.magnolia.module.blossom.dispatcher.BlossomDispatcherInitializedEvent;
47    import info.magnolia.objectfactory.Components;
48    import info.magnolia.registry.RegistrationException;
49    import info.magnolia.rendering.template.AreaDefinition;
50    import info.magnolia.rendering.template.TemplateAvailability;
51    import info.magnolia.rendering.template.TemplateDefinition;
52    import info.magnolia.rendering.template.configured.DefaultTemplateAvailability;
53    import info.magnolia.rendering.template.registry.TemplateDefinitionRegistry;
54    import info.magnolia.test.ComponentsTestUtil;
55    import info.magnolia.test.MgnlTestCase;
56   
57    import java.util.ArrayList;
58    import java.util.HashMap;
59    import java.util.List;
60   
61    import org.junit.Before;
62    import org.junit.Test;
63    import org.springframework.context.ApplicationContext;
64    import org.springframework.context.support.StaticApplicationContext;
65    import org.springframework.web.bind.annotation.RequestMapping;
66    import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
67   
68    /**
69    * Test case for {@link info.magnolia.module.blossom.template.TemplateExporter}.
70    *
71    * @since 3.0.2
72    */
 
73    public class TemplateExporterTest extends MgnlTestCase {
74   
 
75  4 toggle @Before
76    public void setUp() throws Exception {
77  4 super.setUp();
78  4 ComponentsTestUtil.setImplementation(TemplateAvailability.class, DefaultTemplateAvailability.class);
79    }
80   
81    @Template(id = "module:TestTemplate", title = "")
 
82    public static class TestTemplate {
83   
84    @Area("testArea")
 
85    public static class TestArea {
86   
 
87  0 toggle @RequestMapping("/testTemplate/testArea")
88    public void render() {
89   
90    }
91    }
92   
 
93  0 toggle @RequestMapping("/testTemplate")
94    public void render() {
95    }
96    }
97   
 
98  1 toggle @Test
99    public void testExportTemplate() throws Exception {
100   
101  1 String templateId = "module:TestTemplate";
102   
103  1 StaticApplicationContext ctx = new StaticApplicationContext();
104  1 ctx.registerSingleton(TestTemplate.class.getSimpleName(), TestTemplate.class);
105  1 ctx.registerSingleton(TestTemplate.TestArea.class.getSimpleName(), TestTemplate.TestArea.class);
106  1 ctx.refresh();
107   
108  1 HashMap<String, Object> handlerMap = new HashMap<String, Object>();
109  1 handlerMap.put("/testTemplate", ctx.getBean(TestTemplate.class));
110  1 handlerMap.put("/testTemplate/testArea", ctx.getBean(TestTemplate.TestArea.class));
111   
112  1 exportTemplate(ctx, handlerMap);
113   
114  1 TemplateDefinition templateDefinition = Components.getComponent(TemplateDefinitionRegistry.class).getTemplateDefinition(templateId);
115  1 assertEquals(templateId, templateDefinition.getId());
116  1 assertEquals(1, templateDefinition.getAreas().size());
117  1 assertEquals("testArea", templateDefinition.getAreas().values().iterator().next().getName());
118    }
119   
120    @Template(id = "module:TemplateWithPreRegisterCallbackThatReturnsNull", title = "")
 
121    public static class TemplateWithPreRegisterCallbackThatReturnsNull {
122   
 
123  0 toggle @RequestMapping("/testTemplate")
124    public void render() {
125    }
126   
 
127  1 toggle @PreRegister
128    public BlossomTemplateDefinition register(BlossomTemplateDefinition templateDefinition) {
129  1 return null;
130    }
131    }
132   
 
133  1 toggle @Test(expected = RegistrationException.class)
134    public void testExportTemplateWithPreRegisterCallbackThatReturnsNull() throws Exception {
135   
136  1 String templateId = "module:TemplateWithPreRegisterCallbackThatReturnsNull";
137   
138  1 StaticApplicationContext ctx = new StaticApplicationContext();
139  1 ctx.registerSingleton(TemplateWithPreRegisterCallbackThatReturnsNull.class.getSimpleName(), TemplateWithPreRegisterCallbackThatReturnsNull.class);
140  1 ctx.refresh();
141   
142  1 HashMap<String, Object> handlerMap = new HashMap<String, Object>();
143  1 handlerMap.put("/testTemplate", ctx.getBean(TemplateWithPreRegisterCallbackThatReturnsNull.class));
144   
145  1 exportTemplate(ctx, handlerMap);
146   
147  1 Components.getComponent(TemplateDefinitionRegistry.class).getTemplateDefinition(templateId);
148    }
149   
150    @Template(id = "module:TemplateWithAreaWithPreRegisterCallbackThatReturnsNull", title = "")
 
151    public static class TemplateWithAreaWithPreRegisterCallbackThatReturnsNull {
152   
153    @Area("testArea")
 
154    public static class TestArea {
155   
 
156  0 toggle @RequestMapping("/testTemplate/testArea")
157    public void render() {
158   
159    }
160   
 
161  0 toggle @PreRegister
162    public BlossomAreaDefinition register(BlossomAreaDefinition areaDefinition) {
163  0 return null;
164    }
165    }
166   
 
167  0 toggle @RequestMapping("/testTemplate")
168    public void render() {
169    }
170    }
171   
 
172  1 toggle @Test
173    public void testExportTemplateWithAreaWithPreRegisterCallbackThatReturnsNull() throws Exception {
174   
175  1 String templateId = "module:TemplateWithAreaWithPreRegisterCallbackThatReturnsNull";
176   
177  1 StaticApplicationContext ctx = new StaticApplicationContext();
178  1 ctx.registerSingleton(TemplateWithAreaWithPreRegisterCallbackThatReturnsNull.class.getSimpleName(), TemplateWithAreaWithPreRegisterCallbackThatReturnsNull.class);
179  1 ctx.refresh();
180   
181  1 HashMap<String, Object> handlerMap = new HashMap<String, Object>();
182  1 handlerMap.put("/testTemplate", ctx.getBean(TemplateWithAreaWithPreRegisterCallbackThatReturnsNull.class));
183   
184  1 exportTemplate(ctx, handlerMap);
185   
186  1 TemplateDefinition templateDefinition = Components.getComponent(TemplateDefinitionRegistry.class).getTemplateDefinition(templateId);
187  1 assertEquals(0, templateDefinition.getAreas().size());
188    }
189   
190    private static List<String> events = new ArrayList<String>();
191   
192    @Template(id = "module:TemplateWithPreRegisterCallbacks", title = "")
 
193    public static class TemplateWithPreRegisterCallbacks {
194   
 
195    public static class AbstractArea {
196   
 
197  1 toggle @PreRegister
198    public void registerInAbstract(AreaDefinition areaDefinition) {
199  1 events.add("AbstractArea.register-area");
200    }
201   
 
202  1 toggle @PreRegister
203    public void registerInAbstract(BlossomDialogDescription dialogDescription) {
204  1 events.add("AbstractArea.register-dialog");
205    }
206    }
207   
208    @Area("testArea")
 
209    public static class TestArea extends AbstractArea {
210   
 
211  0 toggle @RequestMapping("/testTemplate/testArea")
212    public void render() {
213    }
214   
 
215  0 toggle @TabFactory("")
216    public void tab() {
217    }
218   
 
219  1 toggle @PreRegister
220    public void register(AreaDefinition areaDefinition) {
221  1 events.add("TestArea.register-area");
222    }
223   
 
224  1 toggle @PreRegister
225    public void register(BlossomDialogDescription dialogDescription) {
226  1 events.add("TestArea.register-dialog");
227    }
228    }
229   
 
230  0 toggle @RequestMapping("/testTemplate")
231    public void render() {
232    }
233   
 
234  0 toggle @TabFactory("")
235    public void tab() {
236    }
237   
 
238  1 toggle @PreRegister
239    public void register(TemplateDefinition templateDefinition) {
240  1 events.add("TemplateWithPreRegisterCallbacks.register-template");
241    }
242   
 
243  1 toggle @PreRegister
244    public void register(BlossomDialogDescription dialogDescription) {
245  1 events.add("TemplateWithPreRegisterCallbacks.register-dialog");
246    }
247    }
248   
 
249  1 toggle @Test
250    public void testTemplateAreaAndDialogRegistrationCallbacks() throws Exception {
251  1 String templateId = "module:TemplateWithPreRegisterCallbacks";
252   
253  1 StaticApplicationContext ctx = new StaticApplicationContext();
254  1 ctx.registerSingleton(TemplateWithPreRegisterCallbacks.class.getSimpleName(), TemplateWithPreRegisterCallbacks.class);
255  1 ctx.registerSingleton(TemplateWithPreRegisterCallbacks.TestArea.class.getSimpleName(), TemplateWithPreRegisterCallbacks.TestArea.class);
256  1 ctx.refresh();
257   
258  1 HashMap<String, Object> handlerMap = new HashMap<String, Object>();
259  1 handlerMap.put("/testTemplate", ctx.getBean(TemplateWithPreRegisterCallbacks.class));
260  1 handlerMap.put("/testTemplate/testArea", ctx.getBean(TemplateWithPreRegisterCallbacks.TestArea.class));
261   
262  1 exportTemplate(ctx, handlerMap);
263   
264  1 TemplateDefinition templateDefinition = Components.getComponent(TemplateDefinitionRegistry.class).getTemplateDefinition(templateId);
265  1 assertEquals(templateId, templateDefinition.getId());
266  1 assertEquals(1, templateDefinition.getAreas().size());
267  1 assertEquals("testArea", templateDefinition.getAreas().values().iterator().next().getName());
268   
269  1 assertTrue(events.contains("AbstractArea.register-area"));
270  1 assertTrue(events.contains("AbstractArea.register-dialog"));
271  1 assertTrue(events.contains("TestArea.register-area"));
272  1 assertTrue(events.contains("TestArea.register-dialog"));
273  1 assertTrue(events.contains("TemplateWithPreRegisterCallbacks.register-template"));
274  1 assertTrue(events.contains("TemplateWithPreRegisterCallbacks.register-dialog"));
275    }
276   
 
277  4 toggle private void exportTemplate(ApplicationContext ctx, HashMap<String, Object> handlerMap) throws Exception {
278   
279  4 SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
280  4 handlerMapping.setUrlMap(handlerMap);
281  4 handlerMapping.setApplicationContext(ctx);
282   
283  4 BlossomDispatcher dispatcher = mock(BlossomDispatcher.class);
284   
285  4 TemplateExporter templateExporter = new TemplateExporter();
286  4 templateExporter.setBlossomDispatcher(dispatcher);
287  4 templateExporter.afterPropertiesSet();
288   
289  4 templateExporter.postProcessAfterInitialization(handlerMapping, "handlerMapping");
290  4 templateExporter.onApplicationEvent(new BlossomDispatcherInitializedEvent(dispatcher));
291    }
292    }