KickJava   Java API By Example, From Geeks To Geeks.

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


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.beans.*;
23 import java.util.*;
24
25 import junit.textui.TestRunner;
26 import org.netbeans.junit.NbTestCase;
27 import org.netbeans.junit.NbTestSuite;
28
29 import org.openide.nodes.*;
30
31 /** Test Children.Array.
32  * @author Jesse Glick
33  */

34 public class ChildrenArrayTest extends NbTestCase {
35
36     public ChildrenArrayTest(String JavaDoc name) {
37         super(name);
38     }
39
40     public static void main(String JavaDoc[] args) {
41         TestRunner.run(new NbTestSuite(ChildrenArrayTest.class));
42     }
43
44     protected Children.Array createChildren () {
45         return new Children.Array ();
46     }
47     
48     public void testAdditionIsFiredWhenWeKnowTheSize () {
49         Children kids = createChildren ();
50         Node root = new AbstractNode (kids);
51         ChildrenKeysTest.Listener l = new ChildrenKeysTest.Listener ();
52         root.addNodeListener (l);
53         
54         assertEquals ("Empty", 0, root.getChildren ().getNodesCount ());
55         
56         kids.add (new Node[] { Node.EMPTY.cloneNode () });
57         
58         l.assertAddEvent ("One node added", 1);
59         assertEquals ("One", 1, root.getChildren ().getNodesCount ());
60        
61     }
62     
63
64     /** Tests that node membership events are fired even before getNodes is called.
65      * @see "#24851"
66      */

67     public void testNodeEventsFiredAtOnce() {
68         // Stage 1: call getNodes first, after addNodeListener.
69
Children kids = createChildren ();
70         Node root = new AbstractNode(kids);
71         WaitNL l = new WaitNL();
72         root.addNodeListener(l);
73         Node[] remember = kids.getNodes();
74         Node a = node("a");
75         kids.add(new Node[] {a});
76         assertTrue(l.didAdd(a));
77         Node b = node("b");
78         kids.add(new Node[] {b});
79         assertTrue(l.didAdd(b));
80         Node c = node("c");
81         kids.add(new Node[] {c});
82         assertTrue(l.didAdd(c));
83     }
84     
85     public void testCallBeforeAddNodeListener () throws Exception JavaDoc {
86         // Stage 2: call before addNodeListener.
87
Children kids = createChildren ();
88         Node root = new AbstractNode(kids);
89         kids.getNodes();
90         WaitNL l = new WaitNL();
91         root.addNodeListener(l);
92         Node a = node("a");
93         kids.add(new Node[] {a});
94         assertTrue(l.didAdd(a));
95         Node b = node("b");
96         kids.add(new Node[] {b});
97         assertTrue(l.didAdd(b));
98         Node c = node("c");
99         kids.add(new Node[] {c});
100         assertTrue(l.didAdd(c));
101     }
102     
103     public void testDontCallGetNodesExplicitly () throws Exception JavaDoc {
104         // Stage 3: don't call getNodes explicitly. Events should not be fired
105
Children kids = createChildren ();
106         Node root = new AbstractNode(kids);
107         WaitNL l = new WaitNL();
108         root.addNodeListener(l);
109         Node a = node("a");
110         kids.add(new Node[] {a});
111         assertFalse ("First node added w/o explicit getNodes() is notified", l.didAdd(a));
112         Node b = node("b");
113         kids.add(new Node[] {b});
114         assertFalse(l.didAdd(b));
115         Node c = node("c");
116         kids.add(new Node[] {c});
117         assertFalse(l.didAdd(c));
118     }
119     
120     public void testComplexReorderAndAddAndRemoveEvent () {
121         Children.Array k = createChildren ();
122         // warning this can actually assign FilterNode$Children.nodes!!!
123
k.nodes = new ArrayList ();
124         AbstractNode a1 = new AbstractNode(Children.LEAF);
125         a1.setName ("remove");
126         AbstractNode a2 = new AbstractNode(Children.LEAF);
127         a2.setName ("1");
128         AbstractNode a3 = new AbstractNode(Children.LEAF);
129         a3.setName ("0");
130         AbstractNode a4 = new AbstractNode(Children.LEAF);
131         a4.setName ("add");
132         k.nodes.add (a1);
133         k.nodes.add (a2);
134         k.nodes.add (a3);
135         
136         Node n = new AbstractNode (k);
137         ChildrenKeysTest.Listener l = new ChildrenKeysTest.Listener ();
138         n.addNodeListener (l);
139         assertEquals (3, k.getNodes ().length);
140         
141         k.nodes.clear ();
142         k.nodes.add (a3);
143         k.nodes.add (a2);
144         k.nodes.add (a4);
145         k.refresh ();
146
147         l.assertRemoveEvent ("Removed index 0", 1);
148         l.assertReorderEvent ("0->1 and 1->0", new int[] { 1, 0 });
149         l.assertAddEvent ("Adding at index 2", 1);
150         l.assertNoEvents ("And that is all");
151
152         Node[] arr = k.getNodes ();
153         assertEquals (3, arr.length);
154         assertEquals ("0", arr[0].getName ());
155         assertEquals ("1", arr[1].getName ());
156         assertEquals ("add", arr[2].getName ());
157     }
158     
159     
160     public void testParentAssigned() {
161         Children ch = createChildren ();
162         Node n1 = new AbstractNode( ch );
163         Node n2 = new AbstractNode( Children.LEAF );
164         ChildrenKeysTest.Listener l = new ChildrenKeysTest.Listener ();
165         n1.addNodeListener (l);
166
167         assertEquals ("Empty", 0, ch.getNodesCount ());
168         ch.add( new Node[] { n2 } );
169         
170         Node parent = n2.getParentNode();
171         assertNotNull ( "Parent is assigned", parent);
172         assertEquals ("One", 1, ch.getNodesCount ());
173         l.assertAddEvent ("One node added", 1);
174         
175         Node n3 = new AbstractNode( Children.LEAF );
176         ch.add (new Node[] { n3 });
177         Node p3 = n3.getParentNode();
178         assertSame ( "n3 parent is the same", parent, p3);
179         assertEquals ("Two", 2, ch.getNodesCount ());
180         l.assertAddEvent ("One node added", 1);
181         
182         assertContains ("n2 is in the list of nodes", true, n2, parent.getChildren ().getNodes ());
183         assertContains ("something equals to n2 is in this list", false, n2, ch.getNodes ());
184     }
185     
186     private static Node node(String JavaDoc name) {
187         Node n = new AbstractNode(Children.LEAF);
188         n.setName(name);
189         return n;
190     }
191
192     private static void assertContains (String JavaDoc msg, boolean same, Node n, Node[] arr) {
193         for (int i = 0; i < arr.length; i++) {
194             boolean is = same ? n == arr[i] : n.equals (arr[i]);
195             if (is) {
196                 return;
197             }
198         }
199         fail ("Node " + n + " not found in " + Arrays.asList (arr));
200     }
201     
202     
203     /** Node listener that will tell you if it gets a change.
204      */

205     private static final class WaitNL implements NodeListener {
206         
207         private final Set added = new HashSet(); // Set<Node>
208
private final Set removed = new HashSet(); // Set<Node>
209

210         public WaitNL() {}
211         
212         public synchronized boolean didAdd(Node n) {
213             if (added.contains(n)) return true;
214             try {
215                 wait(1500);
216             } catch (InterruptedException JavaDoc ie) {
217                 ie.printStackTrace();
218             }
219             return added.contains(n);
220         }
221         
222         public synchronized Set getAdded() {
223             return new HashSet(added);
224         }
225         public synchronized Set getRemoved() {
226             return new HashSet(removed);
227         }
228         
229         public synchronized void childrenAdded(NodeMemberEvent ev) {
230             added.addAll(Arrays.asList(ev.getDelta()));
231             notifyAll();
232         }
233         
234         public synchronized void childrenRemoved(NodeMemberEvent ev) {
235             removed.addAll(Arrays.asList(ev.getDelta()));
236             notifyAll();
237         }
238         
239         public void propertyChange(PropertyChangeEvent evt) {}
240         public void childrenReordered(NodeReorderEvent ev) {}
241         public void nodeDestroyed(NodeEvent ev) {}
242         
243     }
244     
245 }
246
Popular Tags