1. Project Clover database Fri Mar 6 2015 14:07:48 CET
  2. Package info.magnolia.module.blossom.dialog

File DialogExporterTest.java

 

Code metrics

2
46
14
9
227
152
15
0.33
3.29
1.56
1.07
11.4% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
DialogExporterTest 66 36 0% 9 1
0.978260997.8%
DialogExporterTest.TestDialogFactory 69 0 - 0 0
-1.0 -
DialogExporterTest.TestDialogFactoryWithPreRegisterThatReturnsNull 82 1 0% 1 0
1.0100%
DialogExporterTest.DialogFactoryWithPreRegisterCallback 99 2 0% 1 0
1.0100%
DialogExporterTest.DialogFactoryWithOverriddenPreRegisterCallback 121 0 0% 1 1
0.00%
DialogExporterTest.DialogFactoryWithPreRegisterThatAddsMessage 137 4 0% 1 0
1.0100%
DialogExporterTest.DialogFactoryWithPreRegisterThatAddsMessageToMessageFromSuperclass 149 2 0% 1 0
1.0100%
DialogExporterTest.TestDialogDefinitionProvider 196 1 80% 1 0
1.0100%
DialogExporterTest.BlossomDialogDescriptionWithMessage 224 0 - 0 0
-1.0 -
 

Contributing tests

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