KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.*;
26 import org.openide.nodes.*;
27
28 import org.netbeans.junit.*;
29
30 /** Tests various aspects och chanfing nodes Children
31  *
32  * @author Petr Hrebejk
33  */

34 public class SetChildrenTest extends NbTestCase {
35
36     public SetChildrenTest(String JavaDoc name) {
37         super(name);
38     }
39
40     /** Tests whether the nodes get Changed.
41      */

42     public void testChldrenEvents () throws Exception JavaDoc {
43
44         Node[][] testNodes = createTestNodes();
45         
46         Children.Array chOld = new Children.Array ();
47         chOld.add( testNodes[0] );
48         chOld.getNodes(); // To get events from the children
49

50         Node root = new TestNodeHid( chOld, "rootNode" );
51         TestListenerHid nlRoot = new TestListenerHid();
52         root.addNodeListener( nlRoot );
53         
54         TestListenerHid nlOld = new TestListenerHid();
55         testNodes[0][0].addNodeListener( nlOld );
56         testNodes[0][1].addNodeListener( nlOld );
57         
58         Children.Array chNew = new Children.Array ();
59         chNew.add( testNodes[1] );
60         
61         TestListenerHid nlNew = new TestListenerHid();
62         testNodes[1][0].addNodeListener( nlNew );
63         testNodes[1][1].addNodeListener( nlNew );
64
65         root.setChildren( chNew );
66      
67         if ( root.getChildren() != chNew ) {
68             fail( "Change to new children did not succeed" );
69         }
70         
71         // Test events on old nodes
72

73         // System.out.println("OldNodes");
74
// GoldenEvent.printEvents( nlOld.getEvents() );
75

76         GoldenEvent[] oldGoldenEvents = new GoldenEvent[] {
77             new GoldenEvent( testNodes[0][0], Node.PROP_PARENT_NODE, root, null ),
78             new GoldenEvent( testNodes[0][1], Node.PROP_PARENT_NODE, root, null )
79         };
80         
81         GoldenEvent.assertEvents ( nlOld.getEvents(), oldGoldenEvents, null );
82         
83         // Test events on new nodes
84

85         //System.out.println("NewNodes");
86
//printEvents( nlNew.getEvents() );
87

88         GoldenEvent[] newGoldenEvents = new GoldenEvent[] {
89             new GoldenEvent( testNodes[1][0], Node.PROP_PARENT_NODE, null, root ),
90             new GoldenEvent( testNodes[1][1], Node.PROP_PARENT_NODE, null, root )
91         };
92         
93         GoldenEvent.assertEvents( nlNew.getEvents(), newGoldenEvents, null );
94         
95         // TestEvents on rootNode
96

97         // System.out.println("RootNode");
98
// printEvents( nlRoot.getEvents() );
99

100         GoldenEvent[] rootGoldenEvents = new GoldenEvent[] {
101             new GoldenEvent( root, false,
102                              testNodes[0],
103                              new int[] { 0, 1 } ),
104             new GoldenEvent( root, true,
105                              testNodes[1],
106                              new int[] { 0, 1 } )
107         };
108         
109         GoldenEvent.assertEvents ( nlRoot.getEvents(), rootGoldenEvents, null );
110     }
111     
112     public void testFreeParent() throws Exception JavaDoc {
113         Node root = new TestNodeHid(Children.LEAF, "rootNode" );
114         Children.Array ch = new Children.Array();
115         root.setChildren(ch);
116         root.setChildren(Children.LEAF);
117         root.setChildren(ch);
118     }
119     
120
121     public void testNotThatMuchChangesWhenSettingToLeaf () throws Exception JavaDoc {
122         
123         Node[][] testNodes = createTestNodes();
124         
125         Children.Array chOld = new Children.Array ();
126         chOld.add( testNodes[0] );
127         chOld.getNodes(); // To get events from the children
128

129         Node root = new TestNodeHid( chOld, "rootNode" );
130         TestListenerHid nlRoot = new TestListenerHid();
131         root.addNodeListener( nlRoot );
132         
133         TestListenerHid nlOld = new TestListenerHid();
134         testNodes[0][0].addNodeListener( nlOld );
135         testNodes[0][1].addNodeListener( nlOld );
136         
137         TestListenerHid nlNew = new TestListenerHid();
138         testNodes[1][0].addNodeListener( nlNew );
139         testNodes[1][1].addNodeListener( nlNew );
140
141         root.setChildren(Children.LEAF);
142      
143         if (root.getChildren() != Children.LEAF) {
144             fail( "Change to new children did not succeed" );
145         }
146         
147         // Test events on old nodes
148

149         // System.out.println("OldNodes");
150
// GoldenEvent.printEvents( nlOld.getEvents() );
151

152         GoldenEvent[] oldGoldenEvents = new GoldenEvent[] {
153             new GoldenEvent( testNodes[0][0], Node.PROP_PARENT_NODE, root, null ),
154             new GoldenEvent( testNodes[0][1], Node.PROP_PARENT_NODE, root, null )
155         };
156         
157         GoldenEvent.assertEvents ( nlOld.getEvents(), oldGoldenEvents, null );
158         
159         // Test events on new nodes
160

161         //System.out.println("NewNodes");
162
//printEvents( nlNew.getEvents() );
163

164         GoldenEvent[] newGoldenEvents = new GoldenEvent[0];
165         
166         GoldenEvent.assertEvents( nlNew.getEvents(), newGoldenEvents, null );
167         
168         // TestEvents on rootNode
169

170         // System.out.println("RootNode");
171
// printEvents( nlRoot.getEvents() );
172

173         GoldenEvent[] rootGoldenEvents = new GoldenEvent[] {
174             new GoldenEvent(root, Node.PROP_LEAF, Boolean.FALSE, Boolean.TRUE),
175             new GoldenEvent(root, false, testNodes[0], new int[] { 0, 1 }),
176         };
177         
178         GoldenEvent.assertEvents ( nlRoot.getEvents(), rootGoldenEvents, null );
179     }
180     
181     /** Tests whether PROP_LEAF is properly changed
182      */

183     public void testLeafPropChange() {
184         
185         Node[][] testNodes = createTestNodes();
186         
187         Children.Array chNew = new Children.Array();
188         chNew.add( testNodes[1] );
189         
190         Node root = new TestNodeHid( Children.LEAF, "rootNode" );
191         TestListenerHid nl = new TestListenerHid();
192         root.addNodeListener( nl );
193         
194         root.setChildren( chNew );
195         if ( root.getChildren() != chNew ) {
196             fail( "Children change to chNew didin't suceed" );
197         }
198                 
199         root.setChildren( Children.LEAF );
200         if ( root.getChildren() != Children.LEAF ) {
201             fail( "Children change to children.LEAF didin't suceed" );
202         }
203         
204         GoldenEvent[] goldenEvents = new GoldenEvent[] {
205             new GoldenEvent( root, Node.PROP_LEAF, Boolean.TRUE, Boolean.FALSE ),
206             new GoldenEvent( root, Node.PROP_LEAF, Boolean.FALSE, Boolean.TRUE )
207         };
208
209         //System.out.println("LEAF TEST");
210
//printEvents( nl.getEvents() );
211

212         GoldenEvent.assertEvents (nl.getEvents(), goldenEvents, PropertyChangeEvent.class );
213         
214     }
215     
216     
217     /** Tests whether PROP_LEAF on filter node is properly changed
218      * see issue #27450
219      */

220     public void testLeafPropChangeFilterNode() {
221         
222         Node[][] testNodes = createTestNodes();
223         
224         Children.Array chNew = new Children.Array();
225         chNew.add( testNodes[1] );
226         
227         Node root = new TestNodeHid( Children.LEAF, "rootNode" );
228         Node filter = new FilterNode( root );
229         TestListenerHid nl = new TestListenerHid();
230         filter.addNodeListener( nl );
231         
232         root.setChildren( chNew );
233         root.setChildren( Children.LEAF );
234         
235         GoldenEvent[] goldenEvents = new GoldenEvent[] {
236             new GoldenEvent( root, Node.PROP_LEAF, Boolean.TRUE, Boolean.FALSE ),
237             new GoldenEvent( root, Node.PROP_LEAF, Boolean.FALSE, Boolean.TRUE )
238         };
239         
240         GoldenEvent.assertEvents ( nl.getEvents(), goldenEvents, PropertyChangeEvent.class );
241     }
242     
243     
244     /** Tests property changes on old and new nodes
245      */

246
247     
248     // Private methods ---------------------------------------------------------
249

250     private Node[][] createTestNodes() {
251         return new Node[][] {
252             { new TestNodeHid( Children.LEAF, "Old1" ),
253               new TestNodeHid( Children.LEAF, "Old2" ) },
254             { new TestNodeHid( Children.LEAF, "New1" ),
255               new TestNodeHid( Children.LEAF, "New2" ) }
256         };
257     }
258     
259     
260     
261     // Private innerclasses ----------------------------------------------------
262

263     private static class TestNodeHid extends AbstractNode {
264                 
265         public TestNodeHid( Children ch, String JavaDoc name ) {
266             super( ch );
267             setName( name );
268         }
269     }
270     
271     /** Useful class for testing events.
272      */

273     
274     static class GoldenEvent {
275         
276         private String JavaDoc name;
277         private Object JavaDoc oldValue, newValue;
278         private Node source;
279         private boolean isAdd;
280         private Node[] delta;
281         private int[] indices;
282         
283         public GoldenEvent( Node source, String JavaDoc name, Object JavaDoc oldValue, Object JavaDoc newValue ) {
284             this.source = source;
285             this.name = name;
286             this.oldValue = oldValue;
287             this.newValue = newValue;
288         }
289         
290         public GoldenEvent( Node source, boolean isAdd, Node[] delta, int[] indices ) {
291             this.source = source;
292             this.isAdd = isAdd;
293             this.delta = delta;
294             this.indices = indices;
295         }
296         
297         public Class JavaDoc getRepresentedClass() {
298             if ( name == null ) {
299                 return NodeMemberEvent.class;
300             }
301             else {
302                 return PropertyChangeEvent.class;
303             }
304         }
305         
306         public String JavaDoc toString () {
307             StringBuffer JavaDoc sb = new StringBuffer JavaDoc (getRepresentedClass ().getName ());
308             sb.append ('[');
309             
310             if ( getRepresentedClass() == PropertyChangeEvent.class ) {
311                 sb.append ("source=");
312                 sb.append (source);
313                 /*
314                 return source.equals( pe.getSource() ) &&
315                        name.equals( pe.getPropertyName() ) &&
316                        oldValue == null ? pe.getOldValue() == null : oldValue.equals( pe.getOldValue() ) &&
317                        newValue == null ? pe.getNewValue() == null : newValue.equals( pe.getNewValue() );
318                  */

319             }
320             
321             else if ( getRepresentedClass() == NodeMemberEvent.class ) {
322                 /*
323                 NodeMemberEvent nme = (NodeMemberEvent) ev;
324                 
325                 return source.equals( nme.getNode() ) &&
326                        isAdd == nme.isAddEvent() &&
327                        Arrays.equals( delta, nme.getDelta() ) &&
328                        Arrays.equals( indices, nme.getDeltaIndices() );
329                  */

330             }
331             sb.append (']');
332             
333             return sb.toString();
334         }
335         
336         /* Compares the GoldenEvent against another event
337          */

338         
339         public boolean compareTo( Object JavaDoc ev ) {
340             if ( getRepresentedClass() != ev.getClass() ) {
341                 return false;
342             }
343             
344             if ( getRepresentedClass() == PropertyChangeEvent.class ) {
345                 PropertyChangeEvent pe = (PropertyChangeEvent)ev;
346                 return source.equals( pe.getSource() ) &&
347                        name.equals( pe.getPropertyName() ) &&
348                        oldValue == null ? pe.getOldValue() == null : oldValue.equals( pe.getOldValue() ) &&
349                        newValue == null ? pe.getNewValue() == null : newValue.equals( pe.getNewValue() );
350             }
351             
352             else if ( getRepresentedClass() == NodeMemberEvent.class ) {
353                 NodeMemberEvent nme = (NodeMemberEvent) ev;
354                 
355                 return source.equals( nme.getNode() ) &&
356                        isAdd == nme.isAddEvent() &&
357                        Arrays.equals( delta, nme.getDelta() ) &&
358                        Arrays.equals( indices, nme.getDeltaIndices() );
359             }
360             else {
361                 return false;
362             }
363         }
364        
365         /** Compares list of event with array of GoldenEvents. If the
366          * parameter. If the eventClass param is not null only events of
367          * given class are compared.
368          */

369         public static void assertEvents ( List events, GoldenEvent[] goldenEvents, Class JavaDoc eventClass ) {
370             
371             List filteredEvents = new ArrayList();
372             if ( eventClass != null ) {
373                 for ( Iterator it = events.iterator(); it.hasNext(); ) {
374                     Object JavaDoc e = it.next();
375                     if ( e.getClass() == eventClass ) {
376                         filteredEvents.add( e );
377                     }
378                 }
379             }
380             else {
381                 filteredEvents = events;
382             }
383             
384             if ( filteredEvents.size() != goldenEvents.length ) {
385                 fail (
386                     "Events have different sizes: expected: <" + goldenEvents.length + "> but was: <" + filteredEvents.size () + ">\n" +
387                     "the expected events are " + Arrays.asList (goldenEvents) + "\n" +
388                     "the fired events are " + filteredEvents
389                 );
390             }
391                         
392             for ( int i = 0; i < filteredEvents.size(); i++ ) {
393                 if ( !goldenEvents[i].compareTo( filteredEvents.get( i ) ) ) {
394                     fail (
395                         i + "th events are different: expected: " + goldenEvents[i] + " but was: " + filteredEvents.get (i) + "\n" +
396                         "the expected events are " + Arrays.asList (goldenEvents) + "\n" +
397                         "the fired events are " + filteredEvents
398                     );
399                 }
400             }
401         }
402         
403         private static void printEvents( List events ) {
404         
405             for ( Iterator it = events.iterator(); it.hasNext(); ) {
406                 Object JavaDoc e = it.next();
407
408                 if ( e instanceof PropertyChangeEvent ) {
409                     System.out.println("PCHG : " + ((PropertyChangeEvent)e).getPropertyName() + " : " + ((PropertyChangeEvent)e).getSource() );
410                     System.out.println(" new : " + ((PropertyChangeEvent)e).getOldValue() );
411                     System.out.println(" old : " + ((PropertyChangeEvent)e).getNewValue() );
412                 }
413
414                 if ( e instanceof NodeMemberEvent ) {
415                     NodeMemberEvent ne = (NodeMemberEvent) e;
416                     System.out.println( ( ne.isAddEvent() ? "cADD : " : "cRMV : " ) + ne.getNode().getName() );
417
418                     Node[] delta = ne.getDelta();
419                     if ( delta == null ) {
420                         System.out.println("d : " + null );
421                     }
422                     else {
423                         System.out.println("d : " );
424                         for( int i = 0; i < delta.length; i++ ) {
425                             System.out.println(" " + delta[i].getName() );
426                         }
427                     }
428
429                     int[] deltaIdx = ne.getDeltaIndices();
430                     if ( deltaIdx == null ) {
431                         System.out.println("di : " + null );
432                     }
433                     else {
434                         System.out.println("di : " );
435                         for( int i = 0; i < deltaIdx.length; i++ ) {
436                             System.out.println(" " + deltaIdx[i] );
437                         }
438                     }
439
440                 }
441
442             }
443         }
444         
445     }
446         
447     
448     private static class TestListenerHid implements NodeListener {
449         
450         private List events = new ArrayList();
451         
452         
453         public void propertyChange(PropertyChangeEvent evt) {
454             events.add( evt );
455         }
456                 
457         public void nodeDestroyed(NodeEvent evt) {
458             events.add( evt );
459         }
460                 
461         public void childrenReordered(NodeReorderEvent evt) {
462             events.add( evt );
463         }
464                 
465         public void childrenRemoved(NodeMemberEvent evt) {
466             events.add( evt );
467         }
468                 
469         public void childrenAdded(NodeMemberEvent evt) {
470             events.add( evt );
471         }
472         
473         public List getEvents() {
474             return events;
475         }
476         
477     }
478     
479 }
480
Popular Tags