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

File TemplateExporterTest.java

 

Code metrics

0
66
22
9
282
182
22
0.33
3
2.44
1

Classes

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