1 19 20 package org.openide.nodes; 21 22 import com.sun.org.apache.bcel.internal.generic.ARRAYLENGTH; 23 import junit.framework.*; 24 import junit.textui.TestRunner; 25 import java.util.*; 26 import org.openide.cookies.EditCookie; 27 import org.openide.cookies.OpenCookie; 28 import org.openide.cookies.SaveCookie; 29 import org.openide.nodes.*; 30 31 import org.netbeans.junit.*; 32 import javax.swing.event.ChangeListener ; 33 import org.openide.util.Lookup; 34 import org.openide.util.LookupEvent; 35 import org.openide.util.LookupListener; 36 import org.openide.util.lookup.InstanceContent; 37 38 42 public class CookieSetTest extends NbTestCase { 43 public CookieSetTest(String name) { 44 super(name); 45 } 46 47 48 public void testAddRemove () throws Exception { 49 CookieSet set = new CookieSet (); 50 L l = new L (); 51 set.addChangeListener(l); 52 53 54 C1 a1 = new C1 (); 55 C1 c1 = new C1 (); 56 C2 c2 = new C2 (); 57 58 set.add (c1); 59 60 assertEquals ("One change expected", l.cnt (), 1); 61 assertEquals ("Node.Cookie", c1, set.getCookie (Node.Cookie.class)); 62 63 64 set.add (a1); 66 67 assertEquals ("One change expected", l.cnt (), 1); 68 assertEquals ("Node.Cookie", a1, set.getCookie (Node.Cookie.class)); 69 70 set.remove (a1); 72 assertEquals ("One change expected", l.cnt (), 1); 73 assertNull ("Node.Cookie", set.getCookie (Node.Cookie.class)); 74 75 76 set.add (c1); 78 assertEquals ("One change expected", l.cnt (), 1); 79 assertEquals ("Node.Cookie", c1, set.getCookie (Node.Cookie.class)); 80 81 set.add (c2); 83 assertEquals ("One change expected", l.cnt (), 1); 84 assertEquals ("C1 cookie", c1, set.getCookie (Node.Cookie.class)); 85 assertEquals ("C2 index", c2, set.getCookie (Index.class)); 86 87 set.remove (c2); 89 assertEquals ("One change expected", l.cnt (), 1); 90 assertEquals ("C1 cookie", c1, set.getCookie (Node.Cookie.class)); 91 assertNull ("Null index", set.getCookie (Index.class)); 92 93 assertCookieSet(set); 94 } 95 96 98 public void testAddSBremoveS () { 99 CookieSet set = new CookieSet (); 100 C1 c1 = new C1 (); 101 C2 c2 = new C2 (); 102 103 set.add (c1); 105 106 set.add (c2); 108 set.remove (c1); 109 110 assertEquals ("C2 index", c2, set.getCookie (Index.class)); 111 assertEquals ("C2 cookie", c2, set.getCookie (Node.Cookie.class)); 112 assertCookieSet(set); 113 } 114 115 117 public void testAddBSremoveB () { 118 CookieSet set = new CookieSet (); 119 C1 c1 = new C1 (); 120 C2 c2 = new C2 (); 121 122 set.add (c2); 124 assertEquals ("Bigger registered", c2, set.getCookie (Node.Cookie.class)); 125 126 set.add (c1); 128 129 assertEquals ("Smaller takes preceedence", c1, set.getCookie (Node.Cookie.class)); 130 131 set.remove (c2); 132 133 assertEquals ("C1 cookie", c1, set.getCookie (Node.Cookie.class)); 134 assertEquals ("Null index", null, set.getCookie (Index.class)); 135 136 assertCookieSet(set); 137 } 138 139 141 public void testFactoryAddRemove () { 142 L l = new L (); 143 CookieSet set = new CookieSet (); 144 set.addChangeListener(l); 145 146 set.add (C1.class, l); 147 148 assertEquals ("One change", l.cnt (), 1); 149 Node.Cookie obj = set.getCookie (C1.class); 150 if (! (obj instanceof C1)) { 151 fail ("Instance created by factory is wrong"); 152 } 153 154 if (obj != set.getCookie (C1.class)) { 155 fail ("New cookie created even it should not"); 156 } 157 158 if (obj != set.getCookie (Node.Cookie.class)) { 159 fail ("C1 is not registered as cookie"); 160 } 161 162 set.add (C1.class, l); 164 assertEquals ("One change", l.cnt (), 1); 165 166 if (obj == set.getCookie (C1.class)) { 167 fail ("Factory changed, but cookie remains"); 168 } 169 170 obj = set.getCookie (C1.class); 171 172 set.remove (obj); 174 assertNotNull("Factory cookies cannot directly be removed", set.getCookie (C1.class)); 175 176 set.remove(C1.class, l); 178 assertNull("Removed factory still returns a cookie", set.getCookie (C1.class)); 179 180 assertCookieSet(set); 181 } 182 183 185 public void testFactoryAddRemoveInherit () { 186 L l = new L (); 187 CookieSet set = new CookieSet (); 188 set.addChangeListener(l); 189 190 set.add (C1.class, l); 191 set.add (Node.Cookie.class, l); 192 193 assertNull ("Nobody registered as C2", set.getCookie (C2.class)); 194 195 { 196 Node.Cookie cookie = set.getCookie (Node.Cookie.class); 197 if (! (cookie instanceof C2)) { 198 fail ("factory provides cookie C2 for Node.Cookie"); 199 } 200 } 201 202 { 203 Node.Cookie c1 = set.getCookie (C1.class); 204 assertNotNull (c1); 205 assertEquals ("Factory provides C1 for C1 class", c1.getClass (), C1.class); 206 } 207 208 assertNull ("Still nobody registered as C2", set.getCookie (C2.class)); 209 210 assertCookieSet(set); 211 } 212 213 public void testCookieSetThruLookupReturnsTheSame () throws Exception { 214 doCookieSetTestsToSimulateIssue47411 (Node.Cookie.class, false); 215 } 216 217 public void testCookieSetThruLookupReturnsTheSameEvenWhenQueriedForLarger () throws Exception { 218 doCookieSetTestsToSimulateIssue47411 (Index.class, false); 219 } 220 221 public void testCookieSetThruLookupReturnsTheSameWithFilter () throws Exception { 222 doCookieSetTestsToSimulateIssue47411 (Node.Cookie.class, true); 223 } 224 225 public void testCookieSetThruLookupReturnsTheSameEvenWhenQueriedForLargerWithFilter () throws Exception { 226 doCookieSetTestsToSimulateIssue47411 (Index.class, true); 227 } 228 229 private void doCookieSetTestsToSimulateIssue47411 (Class firstQuery, boolean filter) throws Exception { 230 AbstractNode an = new AbstractNode (Children.LEAF); 231 CookieSet set = new CookieSet (); 232 an.setCookieSet (set); 233 234 Node n = filter ? new FilterNode(an) : an; 235 236 C1 c1 = new C1 (); 237 C2 c2 = new C2 (); 238 239 set.add (c2); 241 assertEquals ("Bigger registered", c2, set.getCookie (firstQuery)); 242 assertEquals ("Bigger in lookup", c2, n.getLookup ().lookup (firstQuery)); 243 244 set.add (c1); 246 assertEquals ("Smaller takes preceedence", c1, set.getCookie (Node.Cookie.class)); 247 assertEquals ("Smaller even in lookup", c1, n.getLookup ().lookup (Node.Cookie.class)); 248 assertCookieSet(set); 249 } 250 251 public void testCookieSetThruLookupImprovedVersionIssue47411 () throws Exception { 252 doCookieSetThruLookupImprovedVersionIssue47411 (false); 253 } 254 255 public void testCookieSetThruLookupImprovedVersionWithFitlerIssue47411 () throws Exception { 256 doCookieSetThruLookupImprovedVersionIssue47411 (true); 257 } 258 259 private void doCookieSetThruLookupImprovedVersionIssue47411 (boolean filter) throws Exception { 260 AbstractNode node = new AbstractNode (Children.LEAF); 261 CookieSet set = new CookieSet (); 262 node.setCookieSet (set); 263 264 Node an = filter ? new FilterNode(node) : node; 265 266 267 class X implements org.openide.cookies.OpenCookie, org.openide.cookies.EditCookie { 268 public void open () { 269 } 270 public void edit () { 271 } 272 } 273 X x = new X (); 274 275 class A implements org.openide.cookies.OpenCookie { 276 public void open () { } 277 } 278 A a = new A (); 279 280 set.add (a); 281 set.add (x); 282 283 Object edit = an.getLookup ().lookup (org.openide.cookies.EditCookie.class); 284 assertEquals ("X has edit", x, edit); 285 286 Object open = an.getLookup ().lookup (org.openide.cookies.OpenCookie.class); 287 288 assertEquals ("Just verify that CookieSet returns A", a, set.getCookie (org.openide.cookies.OpenCookie.class)); 289 assertEquals ("A has open", a, open); 290 291 assertEquals (null, an.getLookup ().lookup (SaveCookie.class)); 292 assertEquals (a, an.getLookup ().lookup (OpenCookie.class)); 293 assertEquals (x, an.getLookup ().lookup (EditCookie.class)); 294 assertCookieSet(set); 295 } 296 297 private static void assertCookieSet(CookieSet en) { 298 if (en.getCookie(Node.Cookie.class) == null) { 299 assertEquals("Should be empty", 0, en.getLookup().lookupAll(Node.Cookie.class).size()); 300 return; 301 } 302 303 Lookup.Result<Node.Cookie> all = en.getLookup().lookupResult(Node.Cookie.class); 304 int cnt = 0; 305 for (Class <? extends Node.Cookie> c : all.allClasses()) { 306 Object o = en.getLookup().lookup(c); 307 assertEquals("Query for " + c, o, en.getCookie(c)); 308 cnt++; 309 } 310 311 if (cnt == 0) { 312 fail("There shall be at least one object in lookup: " + cnt); 313 } 314 } 315 316 public void testOneChangeInLookupWhenAddingMultipleElements() throws Exception { 317 CookieSet general = CookieSet.createGeneric(null); 318 319 L listener = new L(); 320 Lookup.Result<String > res = general.getLookup().lookupResult(String .class); 321 res.addLookupListener(listener); 322 res.allItems(); 323 324 assertEquals("No change", 0, listener.cnt()); 325 326 general.assign(String .class, "Ahoj", "Jardo"); 327 328 assertEquals("One change", 1, listener.cnt()); 329 assertEquals("Two items", 2, res.allItems().size()); 330 331 general.assign(String .class, "Ahoj", "Jardo"); 332 assertEquals("No change", 0, listener.cnt()); 333 assertEquals("Still two items", 2, res.allItems().size()); 334 335 general.assign(String .class, "Ahoj"); 336 assertEquals("Yet one change", 1, listener.cnt()); 337 assertEquals("One item", 1, res.allItems().size()); 338 } 339 340 public void testOneChangeInLookupWhenAddingMultipleElementsWithBefore() throws Exception { 341 class B implements CookieSet.Before { 342 CookieSet general; 343 private String [] asg; 344 public void assign(String ... arr) { 345 asg = arr; 346 } 347 348 public void beforeLookup(Class <?> c) { 349 if (asg != null) { 350 general.assign(String .class, asg); 351 asg = null; 352 } 353 } 354 } 355 B b = new B(); 356 357 b.general = CookieSet.createGeneric(b); 358 359 L listener = new L(); 360 Lookup.Result<String > res = b.general.getLookup().lookupResult(String .class); 361 res.addLookupListener(listener); 362 res.allItems(); 363 364 assertEquals("No change", 0, listener.cnt()); 365 366 b.assign("Ahoj", "Jardo"); 367 368 assertEquals("Two items", 2, res.allItems().size()); 369 assertEquals("One change", 1, listener.cnt()); 370 371 b.assign("Ahoj", "Jardo"); 372 assertEquals("Still two items", 2, res.allItems().size()); 373 assertEquals("No change", 0, listener.cnt()); 374 375 b.assign("Ahoj"); 376 assertEquals("One item", 1, res.allItems().size()); 377 assertEquals("Yet one change", 1, listener.cnt()); 378 } 379 380 382 private static final class L extends Object 383 implements LookupListener, ChangeListener , CookieSet.Factory { 384 private int count; 385 386 public void stateChanged(javax.swing.event.ChangeEvent changeEvent) { 387 count++; 388 } 389 390 392 public int cnt () { 393 int c = count; 394 count = 0; 395 return c; 396 } 397 398 401 public Node.Cookie createCookie(Class klass) { 402 if (klass == C1.class) { 403 return new C1 (); 404 } 405 return new C2 (); 406 } 407 408 public void resultChanged(LookupEvent ev) { 409 count++; 410 } 411 412 } 413 414 416 private static class C1 implements Node.Cookie { 417 } 418 419 421 private static final class C2 extends C1 implements Index { 422 423 427 public int indexOf(Node node) { 428 return 0; 429 } 430 431 435 public void moveUp(int x) { 436 } 437 438 441 public Node[] getNodes() { 442 return null; 443 } 444 445 449 public void moveDown(int x) { 450 } 451 452 454 public void reorder() { 455 } 456 457 462 public void exchange(int x, int y) { 463 } 464 465 469 public void removeChangeListener(ChangeListener chl) { 470 } 471 472 475 public int getNodesCount() { 476 return 0; 477 } 478 479 484 public void addChangeListener(ChangeListener chl) { 485 } 486 487 491 public void reorder(int[] perm) { 492 } 493 494 501 public void move(int x, int y) { 502 } 503 }; 504 } 505 | Popular Tags |