1 19 20 package org.openide.nodes; 21 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.Collections ; 25 import java.util.HashSet ; 26 import java.util.Iterator ; 27 import java.util.Set ; 28 import junit.framework.Test; 29 import org.netbeans.junit.NbTestCase; 30 import org.netbeans.junit.NbTestSuite; 31 import org.openide.cookies.SaveCookie; 32 import org.openide.util.Lookup; 33 import org.openide.util.LookupListener; 34 import org.openide.util.lookup.AbstractLookup; 35 import org.openide.util.lookup.InstanceContent; 36 37 41 public class NodeLookupTest extends NbTestCase { 42 public NodeLookupTest(String name) { 43 super(name); 44 } 45 46 public static Test suite() { 47 return new NbTestSuite(NodeLookupTest.class); 49 } 50 51 public void testChangesAreFiredFromLookup () { 52 CountableLookup lkp = new CountableLookup (); 53 Node node = new AbstractNode (createChildren (), lkp); 54 55 checkGetNodesDoesNotInitializeLookup (node, lkp.queries); 56 checkInstanceInGetCookie (new Node.Cookie () {}, lkp.ic, node); 57 checkInstanceInGetLookup (new Node.Cookie () {}, lkp.ic, node, true); 58 checkInstanceInGetLookup ("Some string", lkp.ic, node, true); 59 } 60 61 public void testChangesAreFiredFromLookupThruFilterNode () { 62 CountableLookup lkp = new CountableLookup (); 63 Node node = new FilterNode (new AbstractNode (createChildren (), lkp)); 64 65 checkGetNodesDoesNotInitializeLookup (node, lkp.queries); 66 checkInstanceInGetLookup (new Node.Cookie () {}, lkp.ic, node, true); 68 checkInstanceInGetLookup ("Some string", lkp.ic, node, true); 69 } 70 71 public void testChangesAreFiredFromLookupAssociatedToFilterNode () { 72 CountableLookup lkp = new CountableLookup (); 73 Node node = new FilterNode (Node.EMPTY, Children.LEAF, lkp); 74 75 checkGetNodesDoesNotInitializeLookup (node, lkp.queries); 76 checkInstanceInGetCookie (new Node.Cookie () {}, lkp.ic, node); 77 checkInstanceInGetLookup (new Node.Cookie () {}, lkp.ic, node, true); 78 checkInstanceInGetLookup ("Some string", lkp.ic, node, true); 79 } 80 81 public void testChangesAreFiredFromLookupThruFilterNodeWithOverWrittenGetCookie () { 82 final Node.Cookie myInstance = new Node.Cookie () { }; 83 final ArrayList queries = new ArrayList (); 84 85 InstanceContent ic = new InstanceContent (); 86 AbstractLookup lookup = new AbstractLookup (ic); 87 Node node = new FilterNode (new AbstractNode (createChildren (), lookup)) { 88 public Node.Cookie getCookie (Class clazz) { 89 queries.add (clazz); 90 91 if (clazz == myInstance.getClass ()) { 92 return myInstance; 93 } 94 return super.getCookie (clazz); 95 } 96 }; 97 98 checkGetNodesDoesNotInitializeLookup (node, queries); 99 checkInstanceInGetCookie (new Node.Cookie () {}, ic, node); 100 checkInstanceInGetLookup (new Node.Cookie () {}, ic, node, true); 101 checkInstanceInGetLookup ("Some string", ic, node, false); 104 105 assertEquals ("It is possible to get myInstance from getCookie", myInstance, node.getCookie (myInstance.getClass ())); 106 assertEquals ("It also possible to get it from getLookup", myInstance, node.getLookup ().lookup (myInstance.getClass ())); 107 } 108 109 private void checkInstanceInGetCookie (Node.Cookie obj, InstanceContent ic, Node node) { 110 assertNull("The node does not contain the object yet", node.getCookie(obj.getClass())); 111 112 Listener listener = new Listener (); 113 node.addNodeListener(listener); 114 115 ic.add (obj); 116 listener.assertEvents ("One change in node", 1, -1); 117 118 assertEquals("Can access cookie in the content", obj, node.getCookie(obj.getClass())); 119 120 ic.remove (obj); 121 listener.assertEvents ("One change in node", 1, -1); 122 } 123 124 private void checkInstanceInGetLookup (Object obj, InstanceContent ic, Node node, boolean shouldBeThere) { 125 Listener listener = new Listener (); 126 Lookup.Result res = node.getLookup().lookupResult(obj.getClass()); 127 Collection ignore = res.allItems (); 128 res.addLookupListener(listener); 129 130 ic.add (obj); 131 if (shouldBeThere) { 132 listener.assertEvents ("One change in node's lookup", -1, 1); 133 assertEquals ("Can access object in content via lookup", obj, node.getLookup ().lookup (obj.getClass ())); 134 } else { 135 assertNull ("Cannot access object in content via lookup", node.getLookup ().lookup (obj.getClass ())); 136 } 137 138 139 ic.remove (obj); 140 if (shouldBeThere) { 141 listener.assertEvents ("One change in node's lookup", -1, 1); 142 } 143 assertNull ("Cookie is removed", node.getLookup ().lookup (obj.getClass ())); 144 } 145 146 public void testNoPropertyChangeWhenQueryingForAnExistingCookieBug40734 () throws Exception { 147 class MyN extends AbstractNode { 148 MyN () { 149 super (Children.LEAF); 150 } 151 152 private Node.Cookie my = new Node.Cookie () { 153 public String toString () { 154 return "PlainCookie"; 155 } 156 }; 157 private Node.Cookie save = new SaveCookie () { 158 public void save () { 159 } 160 161 public String toString () { 162 return "SaveCookie"; 163 } 164 }; 165 public Node.Cookie getCookie (Class c) { 166 if (c == Node.Cookie.class) return my; 167 if (c == SaveCookie.class) return save; 168 169 return null; 170 } 171 172 public String toString () { 173 return "Node"; 174 } 175 }; 176 FilterNode fn = new FilterNode (new MyN ()); 177 178 class L extends NodeAdapter implements LookupListener { 179 public String ev; 180 public org.openide.util.LookupEvent lookup; 181 182 public void propertyChange (java.beans.PropertyChangeEvent ev) { 183 this.ev = ev.getPropertyName (); 184 } 185 186 public void resultChanged (org.openide.util.LookupEvent ev) { 187 lookup = ev; 188 } 189 } 190 L l = new L (); 191 192 fn.addNodeListener (l); 193 Lookup.Result res = fn.getLookup().lookupResult(Node.Cookie.class); 194 assertEquals ("No event fired0", null, l.ev); 195 res.addLookupListener (l); 196 Collection items = res.allItems (); 197 if (1 != items.size ()) { 198 fail ("Wrong items: " + items + " instances: " + res.allInstances ()); 199 } 200 assertEquals ("No event fired1", null, l.ev); 201 202 Lookup.Template templSave = new Lookup.Template (SaveCookie.class); 203 assertNotNull ("The save cookie is there", fn.getLookup ().lookupItem (templSave)); 204 assertEquals ("No event fired2", null, l.ev); 205 206 assertNotNull ("There is change in all cookies, so event should be fired", l.lookup); 207 items = res.allItems (); 208 if (2 != items.size ()) { 209 fail ("Wrong items: " + items + " instances: " + res.allInstances ()); 210 } 211 212 assertEquals ("No event fired3", null, l.ev); 213 } 214 215 216 220 public void testNodeIsInItsLookup () { 221 CookieNode n = new CookieNode (); 222 assertEquals ("Node is there", n, n.getLookup ().lookup (Node.class)); 223 } 224 225 public void testFilterNodeWithOverridenGetCookieIsInTheLookup () { 226 doTestFilterNodeWithOverridenGetCookieIsInTheLookup (new CookieNode (), false); 227 } 228 229 public void testFilterNodeWithOverridenGetCookieIsInTheLookupWitSaveCookieWithoutQuery () { 230 doTestFilterNodeWithOverridenGetCookieIsInTheLookup (new CookieNodeWithCookie (), false); 231 } 232 233 public void testFilterNodeWithOverridenGetCookieIsInTheLookupWitSaveCookie () { 234 doTestFilterNodeWithOverridenGetCookieIsInTheLookup (new CookieNodeWithCookie (), true); 235 } 236 private void doTestFilterNodeWithOverridenGetCookieIsInTheLookup (CookieNode n, boolean queryForCookie) { 237 final ArrayList queries = new ArrayList (); 238 class MyFN extends FilterNode { 239 public MyFN (Node n) { 240 super (n); 241 } 242 243 public Node.Cookie getCookie (Class clazz) { 244 queries.add (clazz); 245 return super.getCookie (clazz); 246 } 247 } 248 249 FilterNode fn = new MyFN (n); 250 checkGetNodesDoesNotInitializeLookup (fn, queries); 251 checkGetNodesDoesNotInitializeLookup (fn, n.queries); 252 253 Lookup l = fn.getLookup (); 254 255 if (queryForCookie) { 256 l.lookup (SaveCookie.class); 257 } 258 259 assertTrue ("Node is there", fn == l.lookup (Node.class)); 261 Collection c = l.lookupAll(Node.class); 262 263 if (!queryForCookie) { 264 assertEquals ("Just one node", 1, c.size ()); 265 } else { 266 assertEquals ("The cookie is implemented by the node, thus there are two nodes", 2, c.size ()); 267 } 268 assertTrue ("And the first is the filter", c.iterator ().next () == fn); 269 } 270 271 public void testFilterNodeThatDoesNotOverrideGetCookie () { 272 doTestFilterNodeThatDoesNotOverrideGetCookie (new CookieNode ()); 273 } 274 275 public void testFilterNodeThatDoesNotOverrideGetCookieWithSaveCookie () { 276 doTestFilterNodeThatDoesNotOverrideGetCookie (new CookieNodeWithCookie ()); 277 } 278 279 private void doTestFilterNodeThatDoesNotOverrideGetCookie (CookieNode n) { 280 FilterNode fn = new FilterNode (n); 281 checkGetNodesDoesNotInitializeLookup (fn, n.queries); 282 283 Lookup l = fn.getLookup (); 284 285 assertTrue ("Node is there", fn == l.lookup (Node.class)); 287 Collection c = l.lookupAll(Node.class); 288 assertEquals ("Just one node", 1, c.size ()); 289 assertTrue ("And it is the one", c.iterator ().next () == fn); 290 } 291 292 public void testChangeInObjectVisibleInLookup () { 293 CookieNode n = new CookieNode (); 294 n.setSet(CookieSet.createGeneric(null)); 295 checkInstanceInLookup (new Integer (1), n.cookieSet(), n.getLookup ()); 296 checkInstanceInLookup (new Node.Cookie() {}, n.cookieSet(), n.getLookup ()); 297 } 298 public void testChangeInCookieVisibleInLookup () { 299 CookieNode n = new CookieNode (); 300 checkInstanceInLookup (new Node.Cookie() {}, n.cookieSet(), n.getLookup ()); 301 } 302 303 public void testChangeInCookieVisibleInLookupThruFilterNode () { 304 CookieNode n = new CookieNode (); 305 FilterNode f = new FilterNode (n); 306 checkInstanceInLookup (new Node.Cookie() {}, n.cookieSet(), f.getLookup ()); 307 } 308 309 public void testChangeInObjectVisibleInLookupThruFilterNode () { 310 CookieNode n = new CookieNode (); 311 n.setSet(CookieSet.createGeneric(null)); 312 FilterNode f = new FilterNode (n); 313 checkInstanceInLookup (new Node.Cookie() {}, n.cookieSet(), f.getLookup ()); 314 checkInstanceInLookup (new Integer (2), n.cookieSet(), f.getLookup ()); 315 } 316 317 public void testChangeInCookieVisibleInLookupThruFilterNodeWhenItOverridesGetCookie () { 318 CookieNode n = new CookieNode (); 319 320 MyFilterNode f = new MyFilterNode (n, false); 321 322 checkInstanceInLookup (new Node.Cookie() {}, n.cookieSet(), f.getLookup ()); 323 checkInstanceInLookup (new Node.Cookie() {}, f.set, f.getLookup ()); 324 } 325 public void testChangeInObjectVisibleInLookupThruFilterNodeWhenItOverridesGetCookie () { 326 CookieNode n = new CookieNode (); 327 n.setSet(CookieSet.createGeneric(null)); 328 329 MyFilterNode f = new MyFilterNode (n, true); 330 331 checkInstanceInLookup (new Integer (3), n.cookieSet(), f.getLookup ()); 332 checkInstanceInLookup (new Integer (4), f.set, f.getLookup ()); 333 } 334 335 public void testFilterNodeDelegatesCorrectly () { 336 class CN extends CookieNode implements SaveCookie { 337 public CN () { 338 getCookieSet ().add (this); 339 } 340 public void save () { 341 } 342 } 343 344 CN save = new CN (); 345 FilterNode node = new FilterNode (save); 346 347 Class [] classs={ SaveCookie.class, CN.class }; 348 349 for( int i = 0; i < classs.length; i++ ) { 350 Lookup.Template t = new Lookup.Template (classs[i]); 351 Object cookie = node.getLookup ().lookup (t.getType ()); 352 assertTrue ("it is the right class: " + classs[i] + " : " + cookie, classs[i].isInstance(cookie)); 353 assertTrue (cookie + " == " + save, cookie == save); 354 355 Lookup.Item item = node.getLookup ().lookupItem (t); 356 assertTrue ("Should be " + classs[i] + " is " + item.getType(), classs[i].isAssignableFrom(item.getType())); 357 assertTrue ("value is the same", item.getInstance() == save); 358 359 Collection c = node.getLookup ().lookup (t).allInstances (); 360 assertEquals ("One save cookie", 1, c.size ()); 361 assertTrue ("It is the cookie", c.iterator ().next () == save); 362 363 c = node.getLookup ().lookup (t).allItems (); 364 assertEquals ("One save cookie", 1, c.size ()); 365 item = (Lookup.Item)c.iterator().next (); 366 assertTrue ("Should be " + classs[i] + " is " + item.getType(), classs[i].isAssignableFrom(item.getType())); 367 assertTrue ("value is the same", item.getInstance() == save); 368 } 369 } 370 371 private void checkInstanceInLookup (Object obj, CookieSet ic, Lookup l) { 372 Listener listener = new Listener (); 373 Lookup.Result res = l.lookupResult(Object .class); 374 Collection justToEnsureChangesToListenerWillBeFired = res.allItems (); 375 res.addLookupListener(listener); 376 377 ic.assign(obj.getClass(), obj); 378 listener.assertEvents ("One change in lookup", -1, 1); 379 380 assertEquals ("Can access cookie in the content", obj, l.lookup (obj.getClass ())); 381 382 ic.assign(obj.getClass()); 383 listener.assertEvents ("One change in lookup", -1, 1); 384 385 ic.assign(obj.getClass(), obj); 386 listener.assertEvents ("One change in lookup", -1, 1); 387 388 assertEquals ("Can access cookie in the content", obj, l.lookup (obj.getClass ())); 389 390 ic.assign(obj.getClass()); 391 listener.assertEvents ("One change in lookup", -1, 1); 392 } 393 394 private static void checkGetNodesDoesNotInitializeLookup (final org.openide.nodes.Node n, java.util.List queried) { 395 assertEquals ("No queries before", Collections.EMPTY_LIST, queried); 396 397 class MyCh extends Children.Keys { 398 protected void addNotify () { 399 setKeys (java.util.Collections.singleton(n)); 400 } 401 402 public void clear () { 403 setKeys (java.util.Collections.EMPTY_LIST); 404 } 405 406 protected Node[] createNodes (Object key) { 407 return new Node[] { n }; 408 } 409 }; 410 MyCh ch = new MyCh (); 411 412 new AbstractNode (ch).getChildren().getNodes (); 414 415 assertEquals ("No queries after", Collections.EMPTY_LIST, queried); 416 ch.clear (); 417 assertEquals ("No queries after clean either", Collections.EMPTY_LIST, queried); 418 419 } 420 421 425 public void testAbstractNodeWithoutLookupHasCookieSet () { 426 CookieNode n = new CookieNode (); 427 try { 428 n.cookieSet (); 429 } catch (RuntimeException ex) { 430 fail ("cannot obtain cookie set"); 431 } 432 } 433 434 public void testAbstractNodeWithLookupDoesNotHaveCookieSet () { 435 CookieNode n = new CookieNode (Lookup.EMPTY); 436 try { 437 n.cookieSet (); 438 fail ("It should not be possible to obtain cookieSet it should throw an exception"); 439 } catch (RuntimeException ex) { 440 } 441 try { 442 n.setSet (null); 443 fail ("It should not be possible to obtain setCookieSet it should throw an exception"); 444 } catch (RuntimeException ex) { 445 } 446 } 447 448 449 public void testBackwardCompatibleAbstractNodeLookupCanBeGarbageCollected () { 450 AbstractNode n = new AbstractNode (createChildren ()); 451 452 Lookup l = n.getLookup (); 453 assertEquals ("Two invocations share the same lookup", l, n.getLookup ()); 454 455 java.lang.ref.WeakReference ref = new java.lang.ref.WeakReference (l); 456 l = null; 457 assertGC ("Lookup can be GCed", ref); 458 } 459 460 public void testLazyCookieSet35856 () { 461 CookieNode n = new CookieNode (); 462 463 class CF implements CookieSet.Factory, org.openide.cookies.OpenCookie, org.openide.cookies.EditCookie, org.openide.cookies.ViewCookie { 464 public int cnt; 465 public Node.Cookie createCookie(Class klass) { 466 cnt++; 467 return this; 468 } 469 470 public void open () {} 471 public void edit () {} 472 public void view () {} 473 } 474 CF cf = new CF (); 475 n.cookieSet (); 477 478 Lookup lookup = n.getLookup (); 479 Lookup.Item item; 480 Listener l = new Listener (); 481 Lookup.Result res = lookup.lookupResult(Node.Cookie.class); 482 assertEquals ("Empty", 0, res.allItems ().size ()); 483 res.addLookupListener(l); 484 485 n.cookieSet ().add (org.openide.cookies.OpenCookie.class, cf); 486 l.assertEvents ("Changes in lookup as we added Open", -1, 1); 487 assertEquals ("One", 1, res.allItems ().size ()); 488 assertEquals ("Nothing created", 0, cf.cnt); 489 490 n.cookieSet ().add (org.openide.cookies.ViewCookie.class, cf); 491 item = n.getLookup ().lookupItem (new Lookup.Template (org.openide.nodes.Node.Cookie.class)); 492 assertEquals ("First is still OpenCookie", org.openide.cookies.OpenCookie.class, item.getType ()); 493 assertEquals ("Still one result as we do not listen on ViewCookie", 1, res.allItems ().size ()); 494 l.assertEvents ("Added View, but we are not listening to it", -1, 0); 495 item = n.getLookup ().lookupItem (new Lookup.Template (org.openide.cookies.ViewCookie.class)); 496 l.assertEvents ("Now we listen and here is a change", -1, 1); 497 assertEquals ("Included in result", 2, res.allItems ().size ()); 498 assertEquals ("Included as second", org.openide.cookies.ViewCookie.class, ((Lookup.Item)res.allItems ().toArray ()[1]).getType ()); 499 assertEquals ("First remain OpenCookie", org.openide.cookies.OpenCookie.class, ((Lookup.Item)res.allItems ().toArray ()[0]).getType ()); 500 assertEquals ("Still nothing created by factory", 0, cf.cnt); 501 502 503 n.cookieSet ().add (org.openide.cookies.EditCookie.class, cf); 504 l.assertEvents ("No change yet", -1, 0); 505 assertNotNull ("This triggers the EditCookie listening and creates instance", n.getLookup ().lookup (org.openide.cookies.EditCookie.class)); 506 l.assertEvents ("Another change", -1, 1); 507 assertEquals ("One entry created", 1, cf.cnt); 508 assertEquals ("Included in result", 3, res.allItems ().size ()); 509 Lookup.Item[] arr = (Lookup.Item[])res.allItems ().toArray (new Lookup.Item[0]); 510 assertEquals ("Included as second", org.openide.cookies.ViewCookie.class, (arr[1]).getType ()); 511 assertEquals ("First remain OpenCookie", org.openide.cookies.OpenCookie.class, (arr[0]).getType ()); 512 assertEquals ("Edit is last", org.openide.cookies.EditCookie.class, (arr[2]).getType ()); 513 514 n.cookieSet ().remove (org.openide.cookies.OpenCookie.class, cf); 515 l.assertEvents ("This is a change for sure", -1, 1); 516 assertEquals ("Just two there", 2, res.allItems ().size ()); 517 assertEquals ("View is first", org.openide.cookies.ViewCookie.class, ((Lookup.Item)res.allItems ().toArray ()[0]).getType ()); 518 assertEquals ("Edit is second", org.openide.cookies.EditCookie.class, ((Lookup.Item)res.allItems ().toArray ()[1]).getType ()); 519 520 } 521 522 public void testItIsPossibleToWrapACookieSet () { 523 final int[] cnt = { 0, 0 }; 524 CookieNode n = new CookieNode () { 525 { 526 class F implements CookieSet.Factory, org.openide.cookies.OpenCookie { 527 public Node.Cookie createCookie(Class klass) { 528 return this; 529 } 530 531 public void open () { 532 cnt[1]++; 533 } 534 } 535 getCookieSet ().add (org.openide.cookies.OpenCookie.class, new F ()); 536 } 537 public Node.Cookie getCookie (Class c) { 538 if (org.openide.cookies.OpenCookie.class == c) { 539 return new org.openide.cookies.OpenCookie () { 540 public void open () { 541 Object s = getCookieSet ().getCookie (org.openide.cookies.OpenCookie.class); 542 cnt[0]++; 543 ((org.openide.cookies.OpenCookie)s).open (); 544 } 545 }; 546 } 547 return super.getCookie (c); 548 } 549 }; 550 551 Lookup.Item item = n.getLookup().lookupItem (new Lookup.Template (org.openide.cookies.OpenCookie.class)); 552 assertNotNull (item); 553 assertEquals ("No factory", 0, cnt[1]); 554 assertEquals ("No call", 0, cnt[0]); 555 556 org.openide.cookies.OpenCookie oc = (org.openide.cookies.OpenCookie)item.getInstance (); 557 assertEquals ("No factory", 0, cnt[1]); 558 assertEquals ("No call", 0, cnt[0]); 559 560 oc.open (); 561 assertEquals ("Once factory", 1, cnt[1]); 562 assertEquals ("Once call", 1, cnt[0]); 563 564 } 565 566 public void testGetInstancesWorksFineOnNodeWithCookieSet () throws Exception { 567 doGetInstancesWorksFineOnNodeWithCookieSet (false); 568 } 569 570 public void testGetInstancesWorksFineOnNodeWithCookieSetWhenAskedBefore () throws Exception { 571 doGetInstancesWorksFineOnNodeWithCookieSet (true); 572 } 573 574 private void doGetInstancesWorksFineOnNodeWithCookieSet (boolean askBefore) throws Exception { 575 CookieNode n = new CookieNode (); 576 577 578 class X implements SaveCookie { 579 public void save () {} 580 } 581 582 class Y implements org.openide.cookies.OpenCookie { 583 public void open () {} 584 } 585 586 class Z implements org.openide.cookies.EditCookie { 587 public void edit () {} 588 } 589 Class [] expect = { 590 org.openide.cookies.OpenCookie.class, 591 org.openide.cookies.EditCookie.class, 592 org.openide.cookies.SaveCookie.class 593 }; 594 595 n.cookieSet ().add (new X ()); 596 597 class Fact implements CookieSet.Factory { 598 public Node.Cookie createCookie (Class c) { 599 if (c == org.openide.cookies.OpenCookie.class) { 600 return new Y (); 601 } 602 if (c == org.openide.cookies.EditCookie.class) { 603 return new Z (); 604 } 605 return null; 606 } 607 } 608 n.cookieSet ().add (org.openide.cookies.OpenCookie.class, new Fact ()); 609 n.cookieSet ().add (new Class [] { org.openide.cookies.EditCookie.class }, new Fact ()); 610 611 612 if (askBefore) { 613 for (int i = 0; i < expect.length; i++) { 614 assertNotNull ("Class " + expect[i], n.getLookup().lookup (expect[i])); 615 } 616 } 617 618 Lookup.Template all = new Lookup.Template (Object .class); 619 Set s = n.getLookup ().lookup (all).allClasses(); 620 assertTrue ("Contains X ", s.contains (X.class)); 621 assertTrue ("Contains EditCookie " + s, s.contains (org.openide.cookies.EditCookie.class)); 622 assertTrue ("Contains OpenCookie " + s, s.contains (org.openide.cookies.OpenCookie.class)); 623 624 s = new HashSet (n.getLookup ().lookup (all).allInstances()); 625 if (s.size () < 3) { 626 fail ("At least three objects should be there: " + s); 627 } 628 629 BIG: for (int i = 0; i < expect.length; i++) { 630 Iterator it = s.iterator(); 631 while (it.hasNext()) { 632 if (expect[i].isInstance(it.next ())) { 633 continue BIG; 634 } 635 } 636 fail ("Class " + expect[i] + " not found in " + s); 637 } 638 } 639 640 641 public void testLookupClass () { 642 Node n = new NodeWhichHasItselfInLookup (); 643 doTestNodeLookup (n, Node.class); 644 doTestNodeLookup (n, n.getClass ()); 645 } 646 647 public void testLookupClassIfFilterNode () { 648 Node n = new FilterNode (new NodeWhichHasItselfInLookup ()); 649 doTestNodeLookup (n, Node.class); 650 doTestNodeLookup (n, n.getClass()); 651 } 652 653 private void doTestNodeLookup (Node n, Class query) { 654 Object o1 = n.getLookup ().lookup (query); 655 assertEquals ("Found itself in own lookup(<" + query +">).", n, o1); 656 Lookup.Result r = n.getLookup().lookupResult(query); 657 assertEquals ("Only one instance in result.", 1, r.allInstances ().size ()); 658 Object o2 = r.allInstances ().iterator ().next (); 659 assertEquals ("Found itself in own lookup(<Lookup.Template(" + query +")>).", n, o1); 660 assertEquals ("Same node in both results.", o1, o2); 661 662 Lookup.Item item = n.getLookup ().lookupItem (new Lookup.Template (query)); 663 assertNotNull (item); 664 assertEquals ("Same node in all results", o2, item.getInstance ()); 665 } 666 667 private static class NodeWhichHasItselfInLookup extends AbstractNode { 668 public NodeWhichHasItselfInLookup () { 669 super(Children.LEAF); 670 } 671 } 672 673 674 private static Children createChildren () { 675 return Children.LEAF; 676 } 677 678 679 private static class Listener extends Object 680 implements LookupListener, NodeListener { 681 private int cookies; 682 private int lookups; 683 684 public void assertEvents (String txt, int cookies, int lookups) { 685 686 if (cookies != -1) 687 assertEquals (txt + " cookies", cookies, this.cookies); 688 if (lookups != -1) 689 assertEquals (txt + " lookups", lookups, this.lookups); 690 691 this.cookies = 0; 692 this.lookups = 0; 693 } 694 695 public void childrenAdded(NodeMemberEvent ev) { 696 } 697 698 public void childrenRemoved(NodeMemberEvent ev) { 699 } 700 701 public void childrenReordered(NodeReorderEvent ev) { 702 } 703 704 public void nodeDestroyed(NodeEvent ev) { 705 } 706 707 public void propertyChange(java.beans.PropertyChangeEvent evt) { 708 if (Node.PROP_COOKIE == evt.getPropertyName()) { 709 cookies++; 710 } 711 } 712 713 public void resultChanged(org.openide.util.LookupEvent ev) { 714 lookups++; 715 } 716 717 } 719 private static class CookieNode extends AbstractNode { 720 public ArrayList queries = new ArrayList (); 721 722 public CookieNode () { 723 super (createChildren ()); 724 } 725 public CookieNode (Lookup l) { 726 super (createChildren (), l); 727 } 728 729 public CookieSet cookieSet () { 730 return getCookieSet (); 731 } 732 public void setSet (CookieSet s) { 733 super.setCookieSet (s); 734 } 735 736 public Node.Cookie getCookie (Class c) { 737 queries.add (c); 738 return super.getCookie (c); 739 } 740 741 } 743 private static class CookieNodeWithCookie extends CookieNode implements SaveCookie { 744 public CookieNodeWithCookie () { 745 cookieSet ().add (this); 746 } 747 748 public void save () { 749 } 750 } 751 752 private static class CountableLookup extends AbstractLookup { 753 public final InstanceContent ic; 754 public final ArrayList queries; 755 756 public CountableLookup () { 757 this (new InstanceContent (), new ArrayList ()); 758 } 759 760 private CountableLookup (InstanceContent ic, ArrayList queries) { 761 super (ic); 762 this.ic = ic; 763 this.queries = queries; 764 } 765 766 protected void beforeLookup (Lookup.Template t) { 767 super.beforeLookup (t); 768 queries.add (t.getType ()); 769 } 770 } 771 class MyFilterNode extends FilterNode implements javax.swing.event.ChangeListener { 772 public final CookieSet set; 773 774 public MyFilterNode (Node n, boolean generalCookieSet) { 775 super (n); 776 set = generalCookieSet ? CookieSet.createGeneric(null) : new CookieSet(); 777 set.addChangeListener(this); 778 } 779 780 public Node.Cookie getCookie (Class cl) { 781 Node.Cookie c = super.getCookie (cl); 782 if (c != null) { 783 return c; 784 } 785 return set.getCookie (cl); 786 } 787 788 public void stateChanged (javax.swing.event.ChangeEvent ev) { 789 fireCookieChange (); 790 } 791 } 792 } 793 794 | Popular Tags |