KickJava   Java API By Example, From Geeks To Geeks.

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


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 (and AbstractNode).
36  * @author Jaroslav Tulach, Jesse Glick
37  */

38 public class NodeTest extends NbTestCase {
39
40     public NodeTest(String JavaDoc name) {
41         super(name);
42     }
43     
44     public static void main(String JavaDoc[] args) {
45         TestRunner.run(new NbTestSuite(NodeTest.class));
46     }
47
48     public void testGetActions () throws Exception JavaDoc {
49         final SystemAction[] arr1 = {
50             SystemAction.get (PropertiesAction.class)
51         };
52         final SystemAction[] arr2 = {
53         };
54         
55         AbstractNode an = new AbstractNode (Children.LEAF) {
56             public SystemAction[] getActions () {
57                 return arr1;
58             }
59             
60             public SystemAction[] getContextActions () {
61                 return arr2;
62             }
63         };
64         
65         
66         assertEquals ("getActions(false) properly delegates to getActions()", arr1, an.getActions (false));
67         assertEquals ("getActions(true) properly delegates to getContextActions()", arr2, an.getActions (true));
68         
69     }
70     
71     public void testPreferredAction() throws Exception JavaDoc {
72         final SystemAction a1 = SystemAction.get(PropertiesAction.class);
73         final Action JavaDoc a2 = new AbstractAction JavaDoc() {
74             public void actionPerformed(ActionEvent JavaDoc ev) {}
75         };
76         final SystemAction a3 = SystemAction.get(OpenAction.class);
77         final Action JavaDoc a4 = new AbstractAction JavaDoc() {
78             public void actionPerformed(ActionEvent JavaDoc ev) {}
79         };
80         // Old code:
81
Node n1 = new AbstractNode(Children.LEAF) {
82             {
83                 setDefaultAction(a1);
84             }
85         };
86         Node n2 = new AbstractNode(Children.LEAF) {
87             public SystemAction getDefaultAction() {
88                 return a1;
89             }
90         };
91         // New code:
92
Node n4 = new AbstractNode(Children.LEAF) {
93             public Action JavaDoc getPreferredAction() {
94                 return a1;
95             }
96         };
97         // emulation of DataNode
98
Node n5 = new AbstractNode(Children.LEAF) {
99             {
100                 setDefaultAction (a1);
101             }
102             
103             public SystemAction getDefaultAction () {
104                 return super.getDefaultAction ();
105             }
106         };
107         Node n6 = new AbstractNode(Children.LEAF) {
108             public Action JavaDoc getPreferredAction() {
109                 return a2;
110             }
111         };
112         // Wacko code:
113
Node n7 = new AbstractNode(Children.LEAF) {
114             {
115                 setDefaultAction(a1);
116             }
117             public SystemAction getDefaultAction() {
118                 return a3;
119             }
120         };
121         assertEquals(a1, n1.getDefaultAction());
122         assertEquals(a1, n1.getPreferredAction());
123         assertEquals(a1, n2.getDefaultAction());
124         assertEquals(a1, n2.getPreferredAction());
125         assertEquals(a1, n4.getDefaultAction());
126         assertEquals(a1, n4.getPreferredAction());
127         assertEquals(a1, n5.getPreferredAction());
128         assertEquals(a1, n5.getDefaultAction());
129         assertEquals(null, n6.getDefaultAction());
130         assertEquals(a2, n6.getPreferredAction());
131         assertEquals(a3, n7.getDefaultAction());
132         assertEquals(a3, n7.getPreferredAction());
133     }
134
135     public void testShortDescriptionCanBeSetToNull () {
136         class PCL extends NodeAdapter {
137             public int cnt;
138             
139             public void propertyChange (PropertyChangeEvent ev) {
140                 if (Node.PROP_SHORT_DESCRIPTION.equals (ev.getPropertyName ())) {
141                     cnt++;
142                 }
143             }
144         }
145         
146         AbstractNode an = new AbstractNode (Children.LEAF);
147         an.setDisplayName ("My name");
148         
149         PCL pcl = new PCL ();
150         an.addNodeListener (pcl);
151         assertEquals ("My name", an.getShortDescription ());
152         
153         an.setShortDescription ("Ahoj");
154         assertEquals ("Ahoj", an.getShortDescription ());
155         assertEquals ("One change", 1, pcl.cnt);
156         
157         an.setShortDescription (null);
158         assertEquals ("My name", an.getShortDescription ());
159         assertEquals ("Second change", 2, pcl.cnt);
160     }
161     
162     /** Another sample action */
163     public static final class PropertiesAction extends OpenAction {
164         
165     }
166 }
167
Popular Tags