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

File DialogExporterTest.java

 

Code metrics

2
45
13
8
197
129
14
0.31
3.46
1.62
1.08

Classes

Class Line # Actions
DialogExporterTest 64 36 0% 9 1
0.978260997.8%
DialogExporterTest.TestDialogFactory 67 0 - 0 0
-1.0 -
DialogExporterTest.TestDialogFactoryWithPreRegisterThatReturnsNull 80 1 0% 1 0
1.0100%
DialogExporterTest.DialogFactoryWithPreRegisterCallback 97 2 0% 1 0
1.0100%
DialogExporterTest.DialogFactoryWithOverriddenPreRegisterCallback 119 0 0% 1 1
0.00%
DialogExporterTest.DialogFactoryWithPreRegisterThatAddsMessage 135 4 0% 1 0
1.0100%
DialogExporterTest.DialogFactoryWithPreRegisterThatAddsMessageToMessageFromSuperclass 147 2 0% 1 0
1.0100%
DialogExporterTest.BlossomDialogDescriptionWithMessage 194 0 - 0 0
-1.0 -
 

Contributing tests

This file is covered by 5 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.dialog;
35   
36    import static junit.framework.Assert.assertEquals;
37    import static junit.framework.Assert.assertNotNull;
38    import static org.junit.Assert.assertFalse;
39    import static org.junit.Assert.assertSame;
40    import static org.junit.Assert.assertTrue;
41    import static org.mockito.Mockito.mock;
42   
43    import info.magnolia.config.registry.DefinitionProvider;
44    import info.magnolia.module.blossom.annotation.DialogFactory;
45    import info.magnolia.module.blossom.annotation.PreRegister;
46    import info.magnolia.module.blossom.dispatcher.BlossomDispatcher;
47    import info.magnolia.module.blossom.dispatcher.BlossomDispatcherInitializedEvent;
48    import info.magnolia.objectfactory.Components;
49    import info.magnolia.registry.RegistrationException;
50    import info.magnolia.test.MgnlTestCase;
51    import info.magnolia.ui.dialog.definition.DialogDefinition;
52    import info.magnolia.ui.dialog.registry.DialogDefinitionRegistry;
53   
54    import java.util.concurrent.atomic.AtomicReference;
55   
56    import org.junit.Test;
57    import org.springframework.context.support.StaticApplicationContext;
58   
59    /**
60    * Test case for {@link DialogExporter}.
61    *
62    * @since 3.0.2
63    */
 
64    public class DialogExporterTest extends MgnlTestCase {
65   
66    @DialogFactory(value = "module:TestDialogFactory", label = "Test dialog factory")
 
67    public static class TestDialogFactory {
68   
69    }
70   
 
71  1 toggle @Test
72    public void testExportDialogFactory() throws Exception {
73  1 String dialogId = "module:TestDialogFactory";
74  1 registerDialogFactory(dialogId, createApplicationContext(TestDialogFactory.class));
75  1 DialogDefinitionRegistry registry = Components.getComponent(DialogDefinitionRegistry.class);
76  1 assertNotNull(registry.getDialogDefinition(dialogId));
77    }
78   
79    @DialogFactory(value = "module:TestDialogFactoryWithPreRegisterThatReturnsNull", label = "Test dialog factory")
 
80    public static class TestDialogFactoryWithPreRegisterThatReturnsNull {
81   
 
82  1 toggle @PreRegister
83    public BlossomDialogDescription register(BlossomDialogDescription description) {
84  1 return null;
85    }
86    }
87   
 
88  1 toggle @Test(expected = RegistrationException.class)
89    public void testExportDialogFactoryThatReturnsNull() throws Exception {
90  1 String dialogId = "module:TestDialogFactory";
91  1 registerDialogFactory(dialogId, createApplicationContext(TestDialogFactoryWithPreRegisterThatReturnsNull.class));
92  1 DialogDefinitionRegistry registry = Components.getComponent(DialogDefinitionRegistry.class);
93  1 registry.getDialogDefinition(dialogId);
94    }
95   
96    @DialogFactory(value = "module:DialogFactoryWithPreRegisterCallback", label = "Test dialog factory")
 
97    public static class DialogFactoryWithPreRegisterCallback {
98   
99    boolean invokedWithDescription;
100    BlossomDialogDescription description;
101   
 
102  1 toggle @PreRegister
103    public void register(BlossomDialogDescription description) {
104  1 this.description = description;
105  1 invokedWithDescription = true;
106    }
107    }
108   
 
109  1 toggle @Test
110    public void testExportDialogFactoryWithPreRegisterCallback() throws Exception {
111  1 StaticApplicationContext ctx = createApplicationContext(DialogFactoryWithPreRegisterCallback.class);
112  1 BlossomDialogDescription dialogDescription = registerDialogFactory("module:DialogFactoryWithPreRegisterCallback", ctx);
113  1 DialogFactoryWithPreRegisterCallback dialog = ctx.getBean(DialogFactoryWithPreRegisterCallback.class);
114  1 assertTrue(dialog.invokedWithDescription);
115  1 assertSame(dialogDescription, dialog.description);
116    }
117   
118    @DialogFactory(value = "module:DialogFactoryWithOverriddenPreRegisterCallback", label = "Test dialog factory")
 
119    public static class DialogFactoryWithOverriddenPreRegisterCallback extends DialogFactoryWithPreRegisterCallback {
120   
 
121  0 toggle @Override
122    public void register(BlossomDialogDescription description) {
123    }
124    }
125   
 
126  1 toggle @Test
127    public void testExportDialogFactoryWithOverriddenPreRegisterCallback() throws Exception {
128  1 StaticApplicationContext ctx = createApplicationContext(DialogFactoryWithOverriddenPreRegisterCallback.class);
129  1 BlossomDialogDescription dialogDescription = registerDialogFactory("module:DialogFactoryWithOverriddenPreRegisterCallback", ctx);
130  1 DialogFactoryWithOverriddenPreRegisterCallback dialog = ctx.getBean(DialogFactoryWithOverriddenPreRegisterCallback.class);
131  1 assertFalse(dialog.invokedWithDescription);
132    }
133   
134    @DialogFactory(value="module:DialogFactoryWithPreRegisterThatAddsMessage", label="")
 
135    public static class DialogFactoryWithPreRegisterThatAddsMessage {
136   
 
137  1 toggle @PreRegister
138    private BlossomDialogDescription register(BlossomDialogDescription description) {
139  1 BlossomDialogDescriptionWithMessage descriptionWithMessage = new BlossomDialogDescriptionWithMessage();
140  1 descriptionWithMessage.setId(description.getId());
141  1 descriptionWithMessage.message = "DialogFactoryWithPreRegisterThatAddsMessage";
142  1 return descriptionWithMessage;
143    }
144    }
145   
146    @DialogFactory(value="module:DialogFactoryWithPreRegisterThatAddsMessageToMessageFromSuperclass", label="")
 
147    public static class DialogFactoryWithPreRegisterThatAddsMessageToMessageFromSuperclass extends DialogFactoryWithPreRegisterThatAddsMessage {
148   
 
149  1 toggle @PreRegister
150    private BlossomDialogDescription register(BlossomDialogDescription description) {
151  1 ((BlossomDialogDescriptionWithMessage)description).message += " DialogFactoryWithPreRegisterThatAddsMessageToMessageFromSuperclass";
152  1 return description;
153    }
154    }
155   
 
156  1 toggle @Test
157    public void testExportDialogFactoryWithPreRegisterThatAddsMessageToMessageFromSuperclass() throws Exception {
158  1 StaticApplicationContext ctx = createApplicationContext(DialogFactoryWithPreRegisterThatAddsMessageToMessageFromSuperclass.class);
159  1 BlossomDialogDescriptionWithMessage descriptionWithMessage = (BlossomDialogDescriptionWithMessage) registerDialogFactory("module:DialogFactoryWithPreRegisterThatAddsMessageToMessageFromSuperclass", ctx);
160  1 ctx.getBean(DialogFactoryWithPreRegisterThatAddsMessageToMessageFromSuperclass.class);
161  1 assertEquals("DialogFactoryWithPreRegisterThatAddsMessage DialogFactoryWithPreRegisterThatAddsMessageToMessageFromSuperclass", descriptionWithMessage.message);
162    }
163   
 
164  5 toggle private BlossomDialogDescription registerDialogFactory(final String dialogId, StaticApplicationContext ctx) throws Exception {
165   
166  5 BlossomDispatcher dispatcher = mock(BlossomDispatcher.class);
167   
168  5 final AtomicReference<BlossomDialogDescription> reference = new AtomicReference<BlossomDialogDescription>();
169  5 DialogExporter dialogExporter = new DialogExporter() {
170   
 
171  4 toggle @Override
172    protected DefinitionProvider<DialogDefinition> createDialogDefinitionProvider(BlossomDialogDescription dialogDescription) {
173  4 if (dialogDescription.getId().equals(dialogId)) {
174  4 reference.set(dialogDescription);
175    }
176  4 return new BlossomDialogDefinitionProvider(dialogDescription);
177    }
178    };
179  5 dialogExporter.setBlossomDispatcher(dispatcher);
180  5 dialogExporter.setApplicationContext(ctx);
181  5 dialogExporter.afterPropertiesSet();
182  5 dialogExporter.onApplicationEvent(new BlossomDispatcherInitializedEvent(dispatcher));
183   
184  5 return reference.get();
185    }
186   
 
187  5 toggle private StaticApplicationContext createApplicationContext(Class<?> clazz) {
188  5 StaticApplicationContext applicationContext = new StaticApplicationContext();
189  5 applicationContext.registerSingleton(clazz.getSimpleName(), clazz);
190  5 applicationContext.refresh();
191  5 return applicationContext;
192    }
193   
 
194    public static class BlossomDialogDescriptionWithMessage extends BlossomDialogDescription {
195    String message;
196    }
197    }