KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > nodes > NodeOpTest


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 Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.nodes;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.beans.*;
24 import java.util.*;
25 import javax.swing.AbstractAction JavaDoc;
26 import javax.swing.Action JavaDoc;
27
28 import junit.textui.TestRunner;
29 import org.netbeans.junit.NbTestCase;
30 import org.netbeans.junit.NbTestSuite;
31
32 import org.openide.nodes.*;
33 import org.openide.util.actions.SystemAction;
34
35 /** Checking some of the behaviour of Node.
36  * @author Jaroslav Tulach
37  */

38 public class NodeOpTest extends NbTestCase {
39
40     public NodeOpTest(String JavaDoc name) {
41         super(name);
42     }
43
44     public static void main(String JavaDoc[] args) {
45         TestRunner.run(new NbTestSuite(NodeOpTest.class));
46     }
47
48
49     private static final class A extends AbstractAction JavaDoc {
50         public void actionPerformed(ActionEvent JavaDoc ev) {
51         }
52     }
53     
54     public void testFindActions () throws Exception JavaDoc {
55         class N extends AbstractNode {
56             private Action JavaDoc[] arr;
57             
58             N (Action JavaDoc[] arr) {
59                 super (org.openide.nodes.Children.LEAF);
60                 this.arr = arr;
61             }
62             
63             public Action JavaDoc[] getActions (boolean f) {
64                 return arr;
65             }
66         }
67         
68         Action JavaDoc[] arr = { new A(), new A(), new A(), new A() };
69         
70         assertArray (
71             "Finding actions for one node is simple",
72             arr,
73             NodeOp.findActions(new Node[] { new N (arr) })
74         );
75
76         assertArray (
77             "Finding actions for two nodes with same actions",
78             arr,
79             NodeOp.findActions(new Node[] { new N (arr), new N (arr) })
80         );
81             
82         assertArray (
83             "Finding actions for three nodes with same actions",
84             arr,
85             NodeOp.findActions(new Node[] { new N (arr), new N (arr), new N (arr) })
86         );
87           
88             
89         assertArray (
90             "Otherwise only common actions are taken",
91             new Action JavaDoc[] { arr[3] },
92             NodeOp.findActions(new Node[] { new N (arr), new N (new Action JavaDoc[] { arr[3], null }) })
93         );
94             
95     }
96     
97     /**
98      * Test that it is OK to return a different list each time.
99      * There was a bug in NodeOp preventing this.
100      */

101     public void testFindActions2() throws Exception JavaDoc {
102         class N extends AbstractNode {
103             private Action JavaDoc a1 = new A();
104             private Action JavaDoc a3 = new A();
105             N() {
106                 super (org.openide.nodes.Children.LEAF);
107             }
108             public Action JavaDoc[] getActions(boolean f) {
109                 return new Action JavaDoc[] {
110                     a1,
111                     new A(),
112                     a3,
113                 };
114             }
115         }
116         Action JavaDoc[] actions = NodeOp.findActions(new Node[] {new N()});
117         assertEquals("NodeOp.findActions does not gratuitously remove nonconstant actions", 3, actions.length);
118     }
119     
120     private static void assertArray (String JavaDoc msg, Object JavaDoc[] a1, Object JavaDoc[] a2) {
121         assertEquals(msg, Arrays.asList(a1), Arrays.asList(a2));
122     }
123 }
124
Popular Tags