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

File DefaultDialogCreatorTest.java

 

Code metrics

4
87
33
14
394
244
35
0.4
2.64
2.36
1.06

Classes

Class Line # Actions
DefaultDialogCreatorTest 67 63 0% 10 0
1.0100%
DefaultDialogCreatorTest.AnnotatedInterface 70 0 - 0 0
-1.0 -
DefaultDialogCreatorTest.SimpleFactory 77 0 0% 3 1
0.666666766.7%
DefaultDialogCreatorTest.InheritingFactory 94 0 0% 2 0
1.0100%
DefaultDialogCreatorTest.BogusTemplate 133 1 0% 1 0
1.0100%
DefaultDialogCreatorTest.TemplateWithDialogFactoryThatNeedsItem 156 2 0% 2 1
0.880%
DefaultDialogCreatorTest.DialogFactoryWithTabFactoryThatNeedsItem 183 2 0% 2 1
0.880%
DefaultDialogCreatorTest.DialogFactoryWithPostCreateMethod 208 1 0% 2 0
1.0100%
DefaultDialogCreatorTest.DialogFactoryWithPostCreateMethodExtendingSuperclass 219 1 0% 1 0
1.0100%
DefaultDialogCreatorTest.DialogFactoryWithPostCreateMethodOverridingMethodInSuperclass 243 2 0% 3 0
1.0100%
DefaultDialogCreatorTest.DialogFactoryWithPostCreateMethodOverridingMethodInSuperclassWithoutRepeatingAnnotation 276 1 0% 1 2
0.00%
DefaultDialogCreatorTest.DialogFactoryWithPostCreateMethodTakingArguments 299 6 0% 1 0
1.0100%
DefaultDialogCreatorTest.DialogFactoryWithPostCreateMethodAfterSubclasses 328 3 0% 3 0
1.0100%
DefaultDialogCreatorTest.DialogFactoryWithPostCreateMethodAfterSubclassesExtendingSuperclass 346 5 0% 4 0
1.0100%
 

Contributing tests

This file is covered by 10 tests. .

Source view

1    /**
2    * This file Copyright (c) 2010-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 org.junit.Assert.assertEquals;
37    import static org.junit.Assert.assertFalse;
38    import static org.junit.Assert.assertNotNull;
39    import static org.junit.Assert.assertTrue;
40    import static org.mockito.Mockito.mock;
41   
42    import info.magnolia.module.blossom.annotation.DialogFactory;
43    import info.magnolia.module.blossom.annotation.PostCreate;
44    import info.magnolia.module.blossom.annotation.TabFactory;
45    import info.magnolia.module.blossom.annotation.TabOrder;
46    import info.magnolia.ui.dialog.config.DialogBuilder;
47    import info.magnolia.ui.dialog.definition.ConfiguredFormDialogDefinition;
48    import info.magnolia.ui.dialog.definition.DialogDefinition;
49    import info.magnolia.ui.form.config.TabBuilder;
50    import info.magnolia.ui.form.config.TextFieldBuilder;
51    import info.magnolia.ui.framework.config.UiConfig;
52   
53    import java.util.ArrayList;
54    import java.util.List;
55   
56    import javax.jcr.Node;
57   
58    import org.junit.Test;
59   
60    import com.vaadin.data.Item;
61   
62    /**
63    * Test case for {@link DefaultDialogCreator}.
64    *
65    * @since 1.0
66    */
 
67    public class DefaultDialogCreatorTest extends DialogTestSupport {
68   
69    // Factories from interfaces are not supported
 
70    public interface AnnotatedInterface {
71   
72    @TabFactory("FromInterface")
73    void foo();
74    }
75   
76    @DialogFactory(value = "some-dialog", label = "TestDialog")
 
77    public static class SimpleFactory implements AnnotatedInterface {
78   
 
79  2 toggle @TabFactory("Properties")
80    public void properties() {
81    }
82   
 
83  1 toggle @TabFactory("Settings")
84    public void settings() {
85    }
86   
 
87  0 toggle @Override
88    public void foo() {
89    }
90    }
91   
92    @DialogFactory("some-dialog")
93    @TabOrder({"Margins", "Sizes", "Properties"})
 
94    public static class InheritingFactory extends SimpleFactory {
95   
 
96  1 toggle @TabFactory("Margins")
97    public void margins(TabBuilder builder, DialogCreationContext context, DialogDefinition dialog, UiConfig cfg) {
98    }
99   
 
100  1 toggle @TabFactory("Sizes")
101    @Override
102    public void settings() {
103    }
104    }
105   
106    private DefaultDialogCreator creator = new DefaultDialogCreator();
107   
 
108  1 toggle @Test
109    public void testSimpleFactory() throws Exception {
110   
111  1 BlossomDialogDescription description = new DialogDescriptionBuilder().buildDescription(new SimpleFactory(), null);
112   
113  1 assertEquals("some-dialog", description.getId());
114  1 assertEquals("TestDialog", description.getFactoryMetaData().getLabel());
115   
116  1 final DialogCreationContext context = new DialogCreationContext();
117  1 creator.createDialog(description.getFactoryMetaData(), context);
118   
119  1 assertHasTabs(context.getDialog(), new String[]{"Settings", "Properties"});
120    }
121   
 
122  1 toggle @Test
123    public void testInheritingFactory() throws Exception {
124   
125  1 BlossomDialogDescription description = new DialogDescriptionBuilder().buildDescription(new InheritingFactory(), null);
126   
127  1 DialogCreationContext context = new DialogCreationContext();
128  1 creator.createDialog(description.getFactoryMetaData(), context);
129   
130  1 assertHasTabsInOrder(context.getDialog(), new String[]{"Margins", "Sizes", "Properties"});
131    }
132   
 
133    public static class BogusTemplate {
134   
 
135  1 toggle @DialogFactory("some-dialog")
136    public void someDialog(DialogBuilder builder, DialogDefinition dialog, DialogCreationContext context) {
137  1 builder.form().tab("settings").label("Settings");
138    }
139    }
140   
 
141  1 toggle @Test
142    public void testDialogFactoryOnMethod() throws Exception {
143   
144  1 List<BlossomDialogDescription> descriptions = new DialogDescriptionBuilder().buildDescriptions(new BogusTemplate());
145   
146  1 assertEquals(1, descriptions.size());
147   
148  1 BlossomDialogDescription description = descriptions.get(0);
149   
150  1 DialogCreationContext context = new DialogCreationContext();
151  1 creator.createDialog(description.getFactoryMetaData(), context);
152   
153  1 assertHasTabs(context.getDialog(), new String[]{"Settings"});
154    }
155   
 
156    public static class TemplateWithDialogFactoryThatNeedsItem {
157   
 
158  1 toggle @DialogFactory("some-dialog")
159    public void someDialog(DialogBuilder builder, Item item) {
160  1 if (item != null) {
161  1 builder.form().tab("gotItem").label("gotItem");
162    }
163    }
164    }
165   
 
166  1 toggle @Test
167    public void testDialogFactoryThatNeedsItem() throws Exception {
168   
169  1 List<BlossomDialogDescription> descriptions = new DialogDescriptionBuilder().buildDescriptions(new TemplateWithDialogFactoryThatNeedsItem());
170   
171  1 assertEquals(1, descriptions.size());
172   
173  1 BlossomDialogDescription description = descriptions.get(0);
174   
175  1 Item mockItem = mock(Item.class);
176  1 DialogCreationContext context = new DialogCreationContext();
177  1 context.setItem(mockItem);
178  1 creator.createDialog(description.getFactoryMetaData(), context);
179   
180  1 assertHasTabs(context.getDialog(), new String[]{"gotItem"});
181    }
182   
 
183    public static class DialogFactoryWithTabFactoryThatNeedsItem {
184   
 
185  1 toggle @TabFactory("Content")
186    public void someDialog(TabBuilder builder, Item item) {
187  1 if (item != null) {
188  1 builder.fields(new TextFieldBuilder("gotItem"));
189    }
190    }
191    }
192   
 
193  1 toggle @Test
194    public void testDialogFactoryWithTabFactoryThatNeedsItem() throws Exception {
195   
196  1 BlossomDialogDescription description = new DialogDescriptionBuilder().buildDescription("id", "label", new DialogFactoryWithTabFactoryThatNeedsItem());
197   
198  1 Item mockItem = mock(Item.class);
199  1 DialogCreationContext context = new DialogCreationContext();
200  1 context.setItem(mockItem);
201  1 creator.createDialog(description.getFactoryMetaData(), context);
202   
203  1 assertEquals("gotItem", context.getDialog().getForm().getTabs().get(0).getFields().get(0).getName());
204    }
205   
206    private static List<String> events = new ArrayList<String>();
207   
 
208    public static class DialogFactoryWithPostCreateMethod {
209   
 
210  1 toggle @PostCreate
211    public void postCreate() {
212  1 events.add("DialogFactoryWithPostCreateMethod");
213    }
214   
 
215  3 toggle @TabFactory("a")
216    public void a() {}
217    }
218   
 
219    public static class DialogFactoryWithPostCreateMethodExtendingSuperclass extends DialogFactoryWithPostCreateMethod {
220   
 
221  1 toggle @PostCreate
222    public void postCreate2() {
223  1 events.add("DialogFactoryWithPostCreateMethodExtendingSuperclass");
224    }
225    }
226   
 
227  1 toggle @Test
228    public void testPostCreateIsInvokedOnSuperclassesFirst() throws Exception {
229   
230    // GIVEN
231  1 BlossomDialogDescription description = new DialogDescriptionBuilder().buildDescription("id", "label", new DialogFactoryWithPostCreateMethodExtendingSuperclass());
232  1 DialogCreationContext context = new DialogCreationContext();
233   
234    // WHEN
235  1 creator.createDialog(description.getFactoryMetaData(), context);
236   
237    // THEN
238  1 assertEquals("DialogFactoryWithPostCreateMethod", events.remove(0));
239  1 assertEquals("DialogFactoryWithPostCreateMethodExtendingSuperclass", events.remove(0));
240  1 assertTrue(events.isEmpty());
241    }
242   
 
243    public static class DialogFactoryWithPostCreateMethodOverridingMethodInSuperclass extends DialogFactoryWithPostCreateMethod {
244   
 
245  1 toggle @PostCreate
246    public void postCreate2() {
247  1 events.add("DialogFactoryWithPostCreateMethodOverridingMethodInSuperclass#postCreate2");
248    }
249   
 
250  1 toggle @Override
251    @PostCreate
252    public void postCreate() {
253  1 events.add("DialogFactoryWithPostCreateMethodOverridingMethodInSuperclass#postCreate");
254    }
255   
 
256  1 toggle @TabFactory("b")
257    public void b() {}
258    }
259   
 
260  1 toggle @Test
261    public void testPostCreateOnMethodInSuperclassThatIsOverriddenIsInvokedFirst() throws Exception {
262   
263    // GIVEN
264  1 BlossomDialogDescription description = new DialogDescriptionBuilder().buildDescription("id", "label", new DialogFactoryWithPostCreateMethodOverridingMethodInSuperclass());
265  1 DialogCreationContext context = new DialogCreationContext();
266   
267    // WHEN
268  1 creator.createDialog(description.getFactoryMetaData(), context);
269   
270    // THEN
271  1 assertEquals("DialogFactoryWithPostCreateMethodOverridingMethodInSuperclass#postCreate", events.remove(0));
272  1 assertEquals("DialogFactoryWithPostCreateMethodOverridingMethodInSuperclass#postCreate2", events.remove(0));
273  1 assertTrue(events.isEmpty());
274    }
275   
 
276    public static class DialogFactoryWithPostCreateMethodOverridingMethodInSuperclassWithoutRepeatingAnnotation extends DialogFactoryWithPostCreateMethod {
277   
 
278  0 toggle @Override
279    public void postCreate() {
280  0 events.add("DialogFactoryWithPostCreateMethodOverridingMethodInSuperclassWithoutRepeatingAnnotation#postCreate");
281    }
282    }
283   
 
284  1 toggle @Test
285    public void testPostCreateIsNotInvokedIfMethodIsOverriddenWithoutRepeatingAnnotation() throws Exception {
286   
287    // GIVEN
288  1 BlossomDialogDescription description = new DialogDescriptionBuilder().buildDescription("id", "label", new DialogFactoryWithPostCreateMethodOverridingMethodInSuperclassWithoutRepeatingAnnotation());
289  1 DialogCreationContext context = new DialogCreationContext();
290   
291    // WHEN
292  1 creator.createDialog(description.getFactoryMetaData(), context);
293   
294    // THEN
295  1 assertFalse(events.contains("DialogFactoryWithPostCreateMethodOverridingMethodInSuperclassWithoutRepeatingAnnotation#postCreate"));
296  1 assertTrue(events.isEmpty());
297    }
298   
 
299    public static class DialogFactoryWithPostCreateMethodTakingArguments {
300   
 
301  1 toggle @PostCreate
302    public void postCreate(DialogBuilder dialogBuilder, DialogCreationContext context, ConfiguredFormDialogDefinition definition, Node node, Item item, UiConfig cfg) {
303  1 assertNotNull(dialogBuilder);
304  1 assertNotNull(context);
305  1 assertNotNull(definition);
306  1 assertNotNull(node);
307  1 assertNotNull(item);
308  1 assertNotNull(cfg);
309    }
310    }
311   
 
312  1 toggle @Test
313    public void testPostCreateIsInvokedWithArguments() throws Exception {
314   
315    // GIVEN
316  1 BlossomDialogDescription description = new DialogDescriptionBuilder().buildDescription("id", "label", new DialogFactoryWithPostCreateMethodTakingArguments());
317  1 DialogCreationContext context = new DialogCreationContext();
318  1 context.setItem(mock(Item.class));
319  1 context.setContentNode(mock(Node.class));
320   
321    // WHEN
322  1 creator.createDialog(description.getFactoryMetaData(), context);
323   
324    // THEN
325    // no exception thrown
326    }
327   
 
328    public static class DialogFactoryWithPostCreateMethodAfterSubclasses {
329   
 
330  1 toggle @TabFactory("tab")
331    public void tabFactory() {
332  1 events.add("DialogFactoryWithPostCreateMethodAfterSubclasses#tabFactory"); // 1
333    }
334   
 
335  1 toggle @PostCreate
336    public void postCreate() {
337  1 events.add("DialogFactoryWithPostCreateMethodAfterSubclasses#postCreate"); // 2
338    }
339   
 
340  1 toggle @PostCreate(PostCreate.Phase.AFTER_SUB_CLASSES)
341    public void postCreateAfterSubclasses() {
342  1 events.add("DialogFactoryWithPostCreateMethodAfterSubclasses#postCreateAfterSubclasses"); // 6
343    }
344    }
345   
 
346    public static class DialogFactoryWithPostCreateMethodAfterSubclassesExtendingSuperclass extends DialogFactoryWithPostCreateMethodAfterSubclasses {
347   
 
348  1 toggle @TabFactory("tabInSubClass")
349    public void tabFactory2() {
350  1 events.add("DialogFactoryWithPostCreateMethodAfterSubclassesExtendingSuperclass#tabInSubClass"); // 3
351    }
352   
 
353  1 toggle @PostCreate
354    public void postCreate2() {
355  1 events.add("DialogFactoryWithPostCreateMethodAfterSubclassesExtendingSuperclass#postCreate2"); // 4
356    }
357   
 
358  1 toggle @PostCreate(PostCreate.Phase.AFTER_SUB_CLASSES)
359    public void postCreateAfterSubclasses() {
360  1 events.add("DialogFactoryWithPostCreateMethodAfterSubclassesExtendingSuperclass#postCreateAfterSubclasses"); // 5
361  1 super.postCreateAfterSubclasses();
362    }
363   
 
364  1 toggle @PostCreate(PostCreate.Phase.AFTER_SUB_CLASSES)
365    public void postCreateAfterSubclasses2() {
366  1 events.add("DialogFactoryWithPostCreateMethodAfterSubclassesExtendingSuperclass#postCreateAfterSubclasses2"); // 7
367    }
368    }
369   
 
370  1 toggle @Test
371    public void testPostCreateForAfterSubclassesPhaseIsInvokedLast() throws Exception {
372   
373    // GIVEN
374  1 BlossomDialogDescription description = new DialogDescriptionBuilder().buildDescription("id", "label", new DialogFactoryWithPostCreateMethodAfterSubclassesExtendingSuperclass());
375  1 DialogCreationContext context = new DialogCreationContext();
376   
377    // WHEN
378  1 creator.createDialog(description.getFactoryMetaData(), context);
379   
380    // THEN
381  1 assertEquals("DialogFactoryWithPostCreateMethodAfterSubclasses#tabFactory", events.remove(0));
382  1 assertEquals("DialogFactoryWithPostCreateMethodAfterSubclasses#postCreate", events.remove(0));
383   
384  1 assertEquals("DialogFactoryWithPostCreateMethodAfterSubclassesExtendingSuperclass#tabInSubClass", events.remove(0));
385  1 assertEquals("DialogFactoryWithPostCreateMethodAfterSubclassesExtendingSuperclass#postCreate2", events.remove(0));
386   
387  1 assertEquals("DialogFactoryWithPostCreateMethodAfterSubclassesExtendingSuperclass#postCreateAfterSubclasses", events.remove(0));
388  1 assertEquals("DialogFactoryWithPostCreateMethodAfterSubclasses#postCreateAfterSubclasses", events.remove(0));
389   
390  1 assertEquals("DialogFactoryWithPostCreateMethodAfterSubclassesExtendingSuperclass#postCreateAfterSubclasses2", events.remove(0));
391   
392  1 assertTrue(events.isEmpty());
393    }
394    }