1 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 34 public class SetChildrenTest extends NbTestCase { 35 36 public SetChildrenTest(String name) { 37 super(name); 38 } 39 40 42 public void testChldrenEvents () throws Exception { 43 44 Node[][] testNodes = createTestNodes(); 45 46 Children.Array chOld = new Children.Array (); 47 chOld.add( testNodes[0] ); 48 chOld.getNodes(); 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 73 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 85 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 97 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 { 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 { 122 123 Node[][] testNodes = createTestNodes(); 124 125 Children.Array chOld = new Children.Array (); 126 chOld.add( testNodes[0] ); 127 chOld.getNodes(); 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 149 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 161 164 GoldenEvent[] newGoldenEvents = new GoldenEvent[0]; 165 166 GoldenEvent.assertEvents( nlNew.getEvents(), newGoldenEvents, null ); 167 168 170 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 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 212 GoldenEvent.assertEvents (nl.getEvents(), goldenEvents, PropertyChangeEvent.class ); 213 214 } 215 216 217 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 246 247 248 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 263 private static class TestNodeHid extends AbstractNode { 264 265 public TestNodeHid( Children ch, String name ) { 266 super( ch ); 267 setName( name ); 268 } 269 } 270 271 273 274 static class GoldenEvent { 275 276 private String name; 277 private Object 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 name, Object oldValue, Object 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 getRepresentedClass() { 298 if ( name == null ) { 299 return NodeMemberEvent.class; 300 } 301 else { 302 return PropertyChangeEvent.class; 303 } 304 } 305 306 public String toString () { 307 StringBuffer sb = new StringBuffer (getRepresentedClass ().getName ()); 308 sb.append ('['); 309 310 if ( getRepresentedClass() == PropertyChangeEvent.class ) { 311 sb.append ("source="); 312 sb.append (source); 313 319 } 320 321 else if ( getRepresentedClass() == NodeMemberEvent.class ) { 322 330 } 331 sb.append (']'); 332 333 return sb.toString(); 334 } 335 336 338 339 public boolean compareTo( Object 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 369 public static void assertEvents ( List events, GoldenEvent[] goldenEvents, Class eventClass ) { 370 371 List filteredEvents = new ArrayList(); 372 if ( eventClass != null ) { 373 for ( Iterator it = events.iterator(); it.hasNext(); ) { 374 Object 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 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 |