KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > enode > ExtensibleActionsTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Nokia. Portions Copyright 2003 Nokia.
17  * All Rights Reserved.
18  */

19
20 package org.netbeans.modules.enode;
21
22 import java.util.Arrays JavaDoc;
23 import javax.swing.Action JavaDoc;
24 import javax.swing.JMenuItem JavaDoc;
25 import javax.swing.JPopupMenu JavaDoc;
26
27 import junit.textui.TestRunner;
28
29 import org.netbeans.junit.NbTestCase;
30 import org.netbeans.junit.NbTestSuite;
31
32 import org.openide.ErrorManager;
33 import org.openide.modules.ModuleInfo;
34 import org.openide.nodes.*;
35 import org.openide.util.Lookup;
36 import org.openide.util.Utilities;
37 import org.openide.util.actions.SystemAction;
38
39 import org.netbeans.api.enode.ExtensibleNode;
40 import org.netbeans.api.registry.*;
41
42 /**
43  * This test should verify that the functionality of methods
44  * <code>ExtensibleNode.getActions(...)</code> is correct.
45  * @author David Strupl
46  */

47 public class ExtensibleActionsTest extends NbTestCase {
48     /** root Context */
49     private Context root;
50     /** root Context for the lookup tests*/
51     private Context rootLookup;
52     /** submenu Context for the lookup tests*/
53     private Context submenu;
54     
55     private int i = 0;
56
57     public ExtensibleActionsTest(String JavaDoc name) {
58         super(name);
59     }
60     
61     public static void main(String JavaDoc[] args) {
62         TestRunner.run(new NbTestSuite(ExtensibleActionsTest.class));
63     }
64
65     /**
66      * Sets up the testing environment by creating testing folders
67      * on the system file system.
68      */

69     protected void setUp () throws Exception JavaDoc {
70         Lookup.getDefault().lookup(ModuleInfo.class);
71         String JavaDoc baseFolder = ExtensibleNode.E_NODE_ACTIONS.substring(1, ExtensibleNode.E_NODE_ACTIONS.length()-1);
72         root = Context.getDefault().createSubcontext(baseFolder);
73         String JavaDoc baseFolderLookup = ExtensibleNode.E_NODE_LOOKUP.substring(1, ExtensibleNode.E_NODE_LOOKUP.length()-1);
74         rootLookup = Context.getDefault().createSubcontext(baseFolderLookup);
75         String JavaDoc submenuFolder = ExtensibleNode.E_NODE_SUBMENUS.substring(1, ExtensibleNode.E_NODE_SUBMENUS.length()-1);
76         submenu = Context.getDefault().createSubcontext(submenuFolder);
77     }
78     
79     /**
80      * Deletes the folders created in method setUp().
81      */

82     protected void tearDown() throws Exception JavaDoc {
83     }
84     
85     /**
86      * This test tests the presence of declarative actions from
87      * system file system without the hierarchical flag set (the ExtensibleNode
88      * instance is created with constructor ExtensibleNode("test", false).
89      * The tests performs following steps:
90      * <OL><LI> Create an instance of ExtensibleNode with folder set to "test"
91      * <LI> No actions should be returned by getActions since the "test" folder
92      * is not there
93      * <LI> Create one action in the testing folder
94      * <LI> The action should be visible in the result of getActions
95      * <LI> After deleting the action from the folder the action should
96      * not be returned from getActions().
97      * </OL>
98      */

99     public void testCreateAndDeleteAction() throws Exception JavaDoc {
100         try {
101             ExtensibleNode en1 = new ExtensibleNode("test", false);
102             assertEquals("No actions at the start " + i, 0, en1.getActions(false).length);
103             Context test = root.createSubcontext("test");
104
105             SystemAction sa = SystemAction.get(org.openide.actions.PropertiesAction.class);
106             test.putObject("ttt", sa);
107             Action JavaDoc [] res = en1.getActions(false);
108             assertEquals("There should be exactly one action. " + i , 1, res.length);
109             test.putObject("ttt", null);
110             assertEquals("No actions after deleting " + i, 0, en1.getActions(false).length);
111         } finally {
112             root.destroySubcontext("test");
113         }
114     }
115     
116     /**
117      * This test tests the presence of declarative actions from
118      * system file system with the hierarchical flag set (the ExtensibleNode
119      * instance is created with constructor ExtensibleNode("test/t1", true).
120      * The tests performs following steps:
121      * <OL><LI> Create an instance of ExtensibleNode with folder set as "test/t1"
122      * <LI> No actions should be returned from getActions now since the
123      * testing folders are not there
124      * <LI> Create one action in the folder "test"
125      * <LI> The action should be visible in the result of getActions because
126      * although the node has the folder set to "test/t1" with the hierarchical
127      * behaviour the content of folder "test" should be also returned
128      * from this ExtensibleNode
129      * <LI> Create another action in folder "test/t1"
130      * <LI> The second action should be also visible as in getActions together
131      * with the first one - now there should be total of two actions
132      * <LI> After deleting the actions from theirs folders the actions should
133      * not be returned from getActions().
134      * </OL>
135      */

136     public void testHierarchicalBehaviour() throws Exception JavaDoc {
137         try {
138             ExtensibleNode en1 = new ExtensibleNode("test/t1", true);
139             assertEquals("No actions at the start", 0, en1.getActions(false).length);
140             Context test = root.createSubcontext("test");
141
142             SystemAction sa = SystemAction.get(org.openide.actions.PropertiesAction.class);
143             test.putObject("ttt", sa);
144             Action JavaDoc [] res = en1.getActions(false);
145             assertEquals("There should be exactly one action.", 1, res.length);
146             Context t1 = test.createSubcontext("t1");
147             SystemAction a2 = SystemAction.get(org.openide.actions.CutAction.class);
148             t1.putObject("t2", sa);
149             assertEquals("There should 2 actions.", 2, en1.getActions(false).length);
150
151             test.putObject("ttt", null);
152             assertEquals("There should be one after first delete.", 1, en1.getActions(false).length);
153
154             t1.putObject("t2", null);
155             assertEquals("No actions after deleting both", 0, en1.getActions(false).length);
156         } finally {
157             root.destroySubcontext("test");
158         }
159     }
160     
161     /**
162      * An attempt to create a simple stress test. Just calls
163      * the <code>testCreateAndDeleteAction</code> 100 times.
164      */

165     public void testRepetitiveDeleting() throws Exception JavaDoc {
166         for (i = 0; i < 100; i++) {
167             testCreateAndDeleteAction();
168             Thread.sleep(100);
169         }
170     }
171     
172     /**
173      * This test should test behaviour of the getActions method when
174      * there is some alien object specified in the configuration folder.
175      * The testing object is of type Integer (instead of javax.swing.Action).
176      */

177     public void testWrongActionObjectInConfig() throws Exception JavaDoc {
178         try {
179             ExtensibleNode en1 = new ExtensibleNode("test", false);
180             assertEquals("No actions at the start", 0, en1.getActions(false).length);
181             Context test = root.createSubcontext("test");
182             test.putObject("ttt", "foobar");
183             Action JavaDoc [] res = en1.getActions(false);
184             assertEquals("There should be zero actions.", 0, res.length);
185         } finally {
186             root.destroySubcontext("test");
187         }
188     }
189     
190     /**
191      * This test checks whether the JSeparator added from the configuration
192      * file is reflected in the resulting popup.
193      * The tests performs following steps:
194      * <OL><LI> Create an instance of ExtensibleNode with folder set to "test"
195      * <LI> No actions should be returned by getActions since the "test" folder
196      * is not there
197      * <LI> Create two actions in the testing folder separated by JSeparator
198      * <LI> getActions should return 3 elements - null element for the separator
199      * <LI> Popup is created from the actions array - the null element
200      * should be replaced by a JSeparator again
201      * </OL>
202      */

203     public void testAddingSeparators() throws Exception JavaDoc {
204         try {
205             ExtensibleNode en1 = new ExtensibleNode("test", false);
206             assertEquals("No actions at the start", 0, en1.getActions(false).length);
207             Context test = root.createSubcontext("test");
208
209             SystemAction a1 = SystemAction.get(org.openide.actions.PropertiesAction.class);
210             Object JavaDoc sep = new javax.swing.JSeparator JavaDoc();
211             SystemAction a2 = SystemAction.get(org.openide.actions.CutAction.class);
212             test.putObject("a1", a1);
213             test.putObject("sep", sep);
214             test.putObject("a2", a2);
215             test.orderContext(Arrays.asList(new String JavaDoc[] { "a1", "sep", "a2" } ));
216
217             javax.swing.Action JavaDoc[] actions = en1.getActions(false);
218             assertEquals("Actions array should contain 3 elements", 3, actions.length);
219             assertNull("separator should create null element in the array but created 1." + actions[0] + " 2. " + actions[1] + " 3. " + actions[2], actions[1]);
220             JPopupMenu JavaDoc jp = Utilities.actionsToPopup(actions, org.openide.util.lookup.Lookups.singleton(en1));
221             assertEquals("Popup should contain 3 components", 3, jp.getComponentCount());
222             assertTrue("Separator should be second", jp.getComponent(1) instanceof javax.swing.JSeparator JavaDoc);
223         } finally {
224             root.destroySubcontext("test");
225         }
226     }
227     
228     /**
229      * This test checks whether adding folder from the configuration
230      * file is reflected in the resulting popup as a submenu.
231      * The tests performs following steps:
232      * <OL><LI> Create an instance of ExtensibleNode with folder set to "test"
233      * <LI> No actions should be returned by getActions since the "test" folder
234      * is not there
235      * <LI> Create a subfolder of the config folder containing two acitons files
236      * <LI> Check whether the folder is represented by an action
237      * <LI> Convert the action to popup
238      * <LI> The popup should contain one element (JMenu)
239      * <LI> The nested JMenu should have two subelements
240      * <LI> The text on the menu should be the name of the submenu folder
241      * </OL>
242      */

243     public void testSubMenuBehaviour() throws Exception JavaDoc {
244             ExtensibleNode en1 = new ExtensibleNode("test1", false);
245             Action JavaDoc[] actions = en1.getActions(false);
246             if (actions.length == 1) {
247                 fail("actions only contains " + actions[0]);
248             }
249             assertEquals("Actions array should contain 2 elements ", 2, actions.length);
250             JPopupMenu JavaDoc jp = Utilities.actionsToPopup(actions, org.openide.util.lookup.Lookups.singleton(en1));
251             assertEquals("Popup should contain 2 components", 2, jp.getComponentCount());
252             assertTrue("The first component should be menu", jp.getComponent(0) instanceof javax.swing.JMenu JavaDoc);
253             javax.swing.JMenu JavaDoc jm = (javax.swing.JMenu JavaDoc)jp.getComponent(0);
254             assertEquals("Submenu should contain two elements", 2, jm.getMenuComponentCount());
255             assertEquals("Submenu should have correct name", "Sub Menu1", jm.getText());
256     }
257
258     /**
259      * This test checks whether the JSeparator added from the configuration
260      * file is reflected in the resulting popup.
261      * The tests performs following steps:
262      * <OL><LI> Create an instance of ExtensibleNode with folder set to "test"
263      * <LI> No actions should be returned by getActions since the "test" folder
264      * is not there
265      * <LI> Create two actions in the testing folder separated by JSeparator
266      * <LI> getActions should return 1 elements - the submenu
267      * <LI> Popup is created from the actions array - the separator should
268      * come second according to the order.
269      * </OL>
270      */

271     public void testAddingSeparatorsToSubMenu() throws Exception JavaDoc {
272         ExtensibleNode en1 = new ExtensibleNode("test2", false);
273         javax.swing.Action JavaDoc[] actions = en1.getActions(false);
274         assertEquals("Actions array should contain 1 element", 1, actions.length);
275         JPopupMenu JavaDoc jp = Utilities.actionsToPopup(actions, org.openide.util.lookup.Lookups.singleton(en1));
276         assertEquals("Popup should contain 1 component", 1, jp.getComponentCount());
277         assertTrue("The component should be menu", jp.getComponent(0) instanceof javax.swing.JMenu JavaDoc);
278         javax.swing.JMenu JavaDoc jm = (javax.swing.JMenu JavaDoc)jp.getComponent(0);
279         assertEquals("Submenu should contain 3 elements", 3, jm.getMenuComponentCount());
280         assertTrue("Separator should be second", jm.getMenuComponent(1) instanceof javax.swing.JSeparator JavaDoc);
281     }
282
283     
284     /**
285      * This test tests the presence of declarative actions from
286      * system file system configured for declarative cookie instances.
287      * The tests performs following steps:
288      * <OL><LI> Create an instance of ExtensibleNode with folder set to "test"
289      * <LI> No actions should be returned by getActions since the "test" folder
290      * is not there
291      * <LI> Cookie.instance is added to the lookup folder
292      * and Cookie folder is added to the actions folder
293      * <LI> Create one action in the Cookie folder
294      * <LI> The action should be visible in the result of getActions
295      * <LI> After deleting the cookie the action should
296      * not be returned from getActions().
297      * </OL>
298      */

299     public void testCreateAndDeleteActionForCookie() throws Exception JavaDoc {
300         try {
301             ExtensibleNode en1 = new ExtensibleNode("test", false);
302             assertEquals("No actions at the start", 0, en1.getActions(false).length);
303             Context test = rootLookup.createSubcontext("test");
304             Context cFolder = root.createSubcontext("Cookie");
305
306             SystemAction a1 = SystemAction.get(org.openide.actions.PropertiesAction.class);
307             cFolder.putObject("a1", a1);
308             test.putObject("Cookie", "brumbrum");
309
310             Action JavaDoc [] res = en1.getActions(false);
311             assertEquals("There should be exactly one action.", 1, res.length);
312             test.putObject("Cookie", null);
313             assertEquals("No actions after deleting cookie", 0, en1.getActions(false).length);
314         } finally {
315             rootLookup.destroySubcontext("test");
316             root.destroySubcontext("Cookie");
317         }
318     }
319     
320     /**
321      * This test should ensure that when the user selects more nodes in the explorer
322      * that share common submenu the resulting submenu is really shown.
323      * The tests performs following steps:
324      * <OL><LI> Create two instances of ExtensibleNode with folder set to "test"
325      * <LI> No actions should be returned by getActions since the "test" folder
326      * is not there
327      * <LI> Create a subfolder of the config folder containing two acitons files
328      * <LI> Check whether the folder is represented by an action when both nodes
329      * are selected
330      * <LI> Convert the action to popup
331      * <LI> The popup should contain one element (JMenu)
332      * </OL>
333      */

334     public void testSubMenuOnMoreSelectedNodes() throws Exception JavaDoc {
335         ExtensibleNode en1 = new ExtensibleNode("test2", false);
336         ExtensibleNode en2 = new ExtensibleNode("test2", false);
337
338         Action JavaDoc[] actions = NodeOp.findActions(new Node[] { en1, en2 });
339         assertEquals("Actions array should contain 1 element", 1, actions.length);
340         JPopupMenu JavaDoc jp = Utilities.actionsToPopup(actions, org.openide.util.lookup.Lookups.singleton(en1));
341         assertEquals("Popup should contain 1 component", 1, jp.getComponentCount());
342         assertTrue("The component should be menu", jp.getComponent(0) instanceof javax.swing.JMenu JavaDoc);
343         javax.swing.JMenu JavaDoc jm = (javax.swing.JMenu JavaDoc)jp.getComponent(0);
344         assertEquals("Submenu should contain 3 elements", 3, jm.getMenuComponentCount());
345     }
346     
347     /**
348      * This test checks whether adding folder from the configuration
349      * file is reflected in the resulting popup as a submenu.
350      * The tests performs following steps:
351      * <OL><LI> Create an instance of ExtensibleNode with folder set to "Foo"
352      * <LI> Check whether the folder is represented by an action
353      * <LI> Convert the action to popup
354      * <LI> The popup should contain one element (JMenu)
355      * <LI> The nested JMenu should have one subelements
356      * </OL>
357      */

358     public void testOrderOfActions() throws Exception JavaDoc {
359         ExtensibleNode en1 = new ExtensibleNode(Children.LEAF,"TPFVWC_View/View", true);
360         javax.swing.Action JavaDoc[] actions = NodeOp.findActions(new Node[] { en1 });
361         assertEquals("Actions array should contain 2 elements ", 2, actions.length);
362         JPopupMenu JavaDoc jp = Utilities.actionsToPopup(actions, org.openide.util.lookup.Lookups.singleton(en1));
363         assertEquals("Popup should contain 2 components", 2, jp.getComponentCount());
364         assertEquals("The second component should be Delete", "Delete", ((JMenuItem JavaDoc)jp.getComponent(1)).getText());
365         
366         ExtensibleNode en2 = new ExtensibleNode(Children.LEAF,"TPFVWC_View/ViewFolder", true);
367         javax.swing.Action JavaDoc[] actions2 = NodeOp.findActions(new Node[] { en2 });
368         assertEquals("Actions array should contain 2 elements ", 2, actions2.length);
369         JPopupMenu JavaDoc jp2 = Utilities.actionsToPopup(actions2, org.openide.util.lookup.Lookups.singleton(en2));
370         assertEquals("Popup should contain 2 components", 2, jp2.getComponentCount());
371         assertEquals("The second component should be Delete", "Delete", ((JMenuItem JavaDoc)jp2.getComponent(1)).getText());
372     }
373     /**
374      * This test checks whether the JSeparator added from the configuration
375      * file is reflected in the resulting popup.
376      * The tests performs following steps:
377      * <OL><LI> Create an instance of ExtensibleNode with folder set to "test3"
378      * <LI> Create two actions in the testing folder separated by JSeparator
379      * <LI> getActions should return 1 elements - the submenu
380      * <LI> Popup is created from the actions array - the separator should
381      * come second according to the order.
382      * </OL>
383      */

384     public void testSubSubMenu() throws Exception JavaDoc {
385         ExtensibleNode en1 = new ExtensibleNode("test3", false);
386         javax.swing.Action JavaDoc[] actions = en1.getActions(false);
387         assertEquals("Actions array should contain 1 element", 1, actions.length);
388         JPopupMenu JavaDoc jp = Utilities.actionsToPopup(actions, org.openide.util.lookup.Lookups.singleton(en1));
389         assertEquals("Popup should contain 1 component", 1, jp.getComponentCount());
390         assertTrue("The component should be menu", jp.getComponent(0) instanceof javax.swing.JMenu JavaDoc);
391         javax.swing.JMenu JavaDoc jm = (javax.swing.JMenu JavaDoc)jp.getComponent(0);
392         assertEquals("Menu should have display name Nice", "Nice", jm.getText());
393         
394         assertEquals("Menu should contain 1 component", 1, jm.getMenuComponentCount());
395         assertTrue("The component should be menu", jm.getMenuComponent(0) instanceof javax.swing.JMenu JavaDoc);
396         javax.swing.JMenu JavaDoc jm2 = (javax.swing.JMenu JavaDoc)jm.getMenuComponent(0);
397         assertEquals("Menu should have display name Localized", "Localized", jm2.getText());
398         
399         assertEquals("Menu2 should contain 1 component", 1, jm2.getMenuComponentCount());
400         assertTrue("The component should be menu", jm2.getMenuComponent(0) instanceof javax.swing.JMenu JavaDoc);
401         javax.swing.JMenu JavaDoc jm3 = (javax.swing.JMenu JavaDoc)jm2.getMenuComponent(0);
402         assertEquals("Menu should have display name Menu", "Menu", jm3.getText());
403         
404         assertEquals("Submenu should contain 3 elements", 3, jm3.getMenuComponentCount());
405         assertTrue("Separator should be second", jm3.getMenuComponent(1) instanceof javax.swing.JSeparator JavaDoc);
406     }
407
408     /**
409      * This test checks whether the JSeparator added from the configuration
410      * file is reflected in the resulting popup.
411      * The tests performs following steps:
412      * <OL><LI> Create an instance of ExtensibleNode with folder set to "test3"
413      * <LI> Create two actions in the testing folder separated by JSeparator
414      * <LI> getActions should return 1 elements - the submenu
415      * <LI> Popup is created from the actions array - the separator should
416      * come second according to the order.
417      * </OL>
418      */

419     public void testMoreSeparators() throws Exception JavaDoc {
420         ExtensibleNode en1 = new ExtensibleNode("test4", false);
421         javax.swing.Action JavaDoc[] actions = en1.getActions(false);
422         assertEquals("Actions array should contain 6 elements", 6, actions.length);
423         JPopupMenu JavaDoc jp = Utilities.actionsToPopup(actions, org.openide.util.lookup.Lookups.singleton(en1));
424         assertEquals("Popup should contain 6 components", 6, jp.getComponentCount());
425         assertTrue("The 2nd component should be separator", jp.getComponent(1) instanceof javax.swing.JSeparator JavaDoc);
426         assertTrue("The 5nd component should be separator", jp.getComponent(4) instanceof javax.swing.JSeparator JavaDoc);
427         
428         assertTrue("The 3rd component should be menu", jp.getComponent(2) instanceof javax.swing.JMenu JavaDoc);
429         javax.swing.JMenu JavaDoc jm = (javax.swing.JMenu JavaDoc)jp.getComponent(2);
430         
431         assertEquals("Menu should contain 6 components", 6, jm.getMenuComponentCount());
432         assertTrue("The 2nd component should be separator", jm.getMenuComponent(1) instanceof javax.swing.JSeparator JavaDoc);
433         assertTrue("The 5nd component should be separator", jm.getMenuComponent(4) instanceof javax.swing.JSeparator JavaDoc);
434         assertTrue("The 3rd component should be menu", jm.getMenuComponent(2) instanceof javax.swing.JMenu JavaDoc);
435         javax.swing.JMenu JavaDoc jm2 = (javax.swing.JMenu JavaDoc)jm.getMenuComponent(2);
436         
437         assertEquals("Menu2 should contain 5 components", 5, jm2.getMenuComponentCount());
438         assertTrue("The 2nd component should be separator", jm2.getMenuComponent(1) instanceof javax.swing.JSeparator JavaDoc);
439         assertTrue("The 4nd component should be separator", jm2.getMenuComponent(3) instanceof javax.swing.JSeparator JavaDoc);
440     }
441     
442     /**
443      *
444      */

445     public void testfindExistingContext() throws Exception JavaDoc {
446         try {
447             String JavaDoc baseFolder = ExtensibleNode.E_NODE_ACTIONS.substring(1, ExtensibleNode.E_NODE_ACTIONS.length()-1);
448             Context test = root.createSubcontext("test");
449             Context res = ExtensibleLookupImpl.findExistingContext(baseFolder + "/test/ahoj");
450             String JavaDoc s = res.getAbsoluteContextName();
451             assertEquals("Should find the test folder ", ExtensibleNode.E_NODE_ACTIONS+"test", s);
452             Context res2 = ExtensibleLookupImpl.findExistingContext(baseFolder + "/test/ahoj/booooom/ba");
453             String JavaDoc s2 = res2.getAbsoluteContextName();
454             assertEquals("Should find the test folder ", ExtensibleNode.E_NODE_ACTIONS+"test", s2);
455         } finally {
456             root.destroySubcontext("test");
457         }
458     }
459 }
460     
461
462
Popular Tags