KickJava   Java API By Example, From Geeks To Geeks.

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


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 junit.framework.*;
23 import junit.textui.TestRunner;
24 import java.beans.*;
25 import java.beans.beancontext.*;
26 import java.util.*;
27 import org.openide.util.Mutex;
28
29 import org.netbeans.junit.*;
30
31 /** Test updating of bean children in proper circumstances, e.g.
32  * deleting nodes or beans.
33  * @author Jesse Glick
34  */

35 public class FilterChildrenEventsTest extends NbTestCase {
36
37     public FilterChildrenEventsTest(String JavaDoc name) {
38         super(name);
39     }
40
41     public static void main(String JavaDoc[] args) {
42         System.out.println("Running");
43         TestRunner.run(new NbTestSuite(BeanChildrenTest.class));
44     }
45     
46     
47     public void testNodesNodeDestroyed() throws Exception JavaDoc {
48         
49         Node[] chNodes = createTestNodes();
50         Children ch = new Children.Array();
51         ch.add( chNodes );
52         
53         Node n = new AbstractNode( ch );
54         FilterNode fn = new FilterNode( n );
55         n.setName( "X" );
56         MyListener ml = new MyListener();
57         
58         fn.addNodeListener( ml );
59         
60         n.setName( "Y" );
61         
62         List events = ml.getEvents();
63         
64         System.out.println( "Size " + events.size() );
65         
66
67         assertTrue("correct events", events.size() == 2 );
68     }
69     
70     public void testPropertyChange() throws Exception JavaDoc {
71         
72         /*
73         assertEquals("correct subnodes",
74             new HashSet(Arrays.asList(new String[] {"one", "three"})),
75             new HashSet(Arrays.asList(nodes2Names(nodes))));
76          */

77     }
78     
79     public void testChildrenAdded() throws Exception JavaDoc {
80         Node[] chNodes = createTestNodes();
81         Children ch = new Children.Array();
82         ch.add( chNodes );
83         
84         Node n = new AbstractNode( ch );
85         FilterNode fn = new FilterNode( n );
86         n.setName( "X" );
87         MyListener ml = new MyListener();
88         
89         fn.addNodeListener( ml );
90         Node[] hold = fn.getChildren().getNodes();
91         
92         ch.add( new Node[] { new AbstractNode( Children.LEAF) } );
93         
94         List events = ml.getEvents();
95         
96         assertEquals("correct events", 1, events.size() );
97     }
98     
99     
100     private static Node[] createTestNodes() {
101         
102         Node[] tNodes = new Node[] {
103             new AbstractNode( Children.LEAF ),
104             new AbstractNode( Children.LEAF ),
105             new AbstractNode( Children.LEAF )
106         };
107         
108         tNodes[0].setName( "A" );
109         tNodes[1].setName( "B" );
110         tNodes[2].setName( "C" );
111         
112         return tNodes;
113     }
114     
115     private static class MyListener implements NodeListener {
116         
117         ArrayList events = new ArrayList();
118         
119         
120         /** Fired when a set of new children is added.
121          * @param ev event describing the action
122          *
123          */

124         public void childrenAdded(NodeMemberEvent ev) {
125             events.add( ev );
126         }
127         
128         /** Fired when a set of children is removed.
129          * @param ev event describing the action
130          *
131          */

132         public void childrenRemoved(NodeMemberEvent ev) {
133             events.add( ev );
134         }
135         
136         /** Fired when the order of children is changed.
137          * @param ev event describing the change
138          *
139          */

140         public void childrenReordered(NodeReorderEvent ev) {
141             events.add( ev );
142         }
143         
144         /** Fired when the node is deleted.
145          * @param ev event describing the node
146          *
147          */

148         public void nodeDestroyed(NodeEvent ev) {
149             events.add( ev );
150         }
151         
152         /** This method gets called when a bound property is changed.
153          * @param evt A PropertyChangeEvent object describing the event source
154          * and the property that has changed.
155          *
156          */

157         public void propertyChange(PropertyChangeEvent ev) {
158             events.add( ev );
159         }
160         
161         public List getEvents() {
162             return events;
163         }
164         
165     }
166 }
167
Popular Tags