1 package org.jboss.cache.tests.loader; 2 3 import junit.framework.Test; 4 import junit.framework.TestCase; 5 import junit.framework.TestSuite; 6 import org.jboss.cache.*; 7 import org.jboss.cache.loader.CacheLoader; 8 import org.jboss.cache.transaction.DummyTransactionManager; 9 10 import javax.transaction.NotSupportedException ; 11 import javax.transaction.Transaction ; 12 import java.io.Serializable ; 13 import java.util.*; 14 15 20 abstract public class CacheLoaderTestsBase extends TestCase { 21 TreeCache cache; 22 CacheLoader loader=null; 23 Transaction tx=null; 24 static final Fqn FQN = new Fqn("key"); 25 26 27 protected void setUp() throws Exception { 28 super.setUp(); 29 cache=new TreeCache(); 30 cache.setCacheMode("local"); 31 configureCache(); 32 cache.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 34 cache.createService(); 35 cache.startService(); 36 loader=cache.getCacheLoader(); 37 } 38 39 abstract protected void configureCache() throws Exception ; 40 41 42 protected void tearDown() throws Exception { 43 super.tearDown(); 44 if(tx != null) { 45 try { 46 tx.commit(); 47 } 48 catch(Throwable e) { 49 e.printStackTrace(); 50 } 51 } 52 cache.remove("/"); 53 loader.remove(Fqn.fromString("/")); 54 cache.stopService(); 55 cache.destroyService(); 56 } 57 58 59 60 public void testPrint() throws CacheException { 61 final Fqn NODE=Fqn.fromString("/test"); 62 final String KEY="key"; 63 cache.put(NODE, KEY, new Integer (10)); 64 cache.evict(NODE); 65 String ret=cache.print(NODE); 66 assertNotNull(ret); 67 } 68 69 public void testPut() throws CacheException { 70 final String NODE="/test"; 71 final String KEY="key"; 72 Object retval=null; 73 cache.remove(NODE); 74 retval=cache.put(NODE, KEY, new Integer (10)); 75 assertNull(retval); 76 retval=cache.put(NODE, KEY, new Integer (20)); 77 assertEquals(new Integer (10), retval); 78 cache.evict(Fqn.fromString(NODE)); 80 retval=cache.put(NODE, KEY, new Integer (30)); 81 assertEquals(new Integer (20), retval); 82 } 83 84 public void testPut2() throws CacheException { 85 final String NODE="/a/b/c"; 86 final String KEY="key"; 87 Object retval=null; 88 cache.remove(NODE); 89 retval=cache.put(NODE, KEY, new Integer (10)); 90 assertNull(retval); 91 retval=cache.put(NODE, KEY, new Integer (20)); 92 assertEquals(new Integer (10), retval); 93 cache.evict(Fqn.fromString(NODE)); cache.evict(Fqn.fromString("/a/b")); 95 cache.evict(Fqn.fromString("/a")); 96 retval=cache.put(NODE, KEY, new Integer (30)); 97 assertEquals(new Integer (20), retval); 98 } 99 100 101 public void testSerialization() throws CacheException { 102 SamplePojo pojo=new SamplePojo(39, "Bela"); 103 pojo.getHobbies().add("Running"); 104 pojo.getHobbies().add("Beerathlon"); 105 pojo.getHobbies().add("Triathlon"); 106 cache.put("/mypojo", new Integer (322649), pojo); 107 assertNotNull(cache.get("/mypojo", new Integer (322649))); 108 cache.evict(Fqn.fromString("/mypojo")); 109 assertFalse(cache.exists("/mypojo")); 110 SamplePojo pojo2=(SamplePojo)cache.get("/mypojo", new Integer (322649)); assertNotNull(pojo2); 112 assertEquals(39, pojo2.getAge()); 113 assertEquals("Bela", pojo2.getName()); 114 assertEquals(3, pojo2.getHobbies().size()); 115 } 116 117 118 public void testPopulate() { 119 try { 120 Map m=new HashMap(); 121 for(int i=0; i < 10; i++) 122 m.put("key" + i, "val" + i); 123 cache.put("/a/b/c", m); 124 cache.load("/1/2/3/4/5"); 125 cache.put("/1/2/3/4/5", null); 126 cache.put("/1/2/3/4/5/a", null); 127 cache.put("/1/2/3/4/5/b", null); 128 cache.put("/1/2/3/4/5/c", null); 129 cache.put("/1/2/3/4/5/d", null); 130 cache.put("/1/2/3/4/5/e", null); 131 cache.put("/1/2/3/4/5/d/one", null); 132 cache.put("/1/2/3/4/5/d/two", null); 133 cache.put("/1/2/3/4/5/d/three", null); 134 System.out.println("cache: " + cache); 136 137 assertTrue(cache.exists("/1/2/3/4")); 138 assertTrue(cache.exists("/a/b/c")); 139 assertFalse(cache.exists("/a/b/c/d")); 140 } 141 catch(Exception e) { 142 fail(e.toString()); 143 } 144 } 145 146 147 public void testPreloading() throws CacheException { 148 cache.remove("/"); 149 cache.put("1/2/3/4/5/d", "key", "val"); 150 cache.evict(Fqn.fromString("1/2/3/4/5/d")); 151 System.out.println("-- checking for 1/2/3/4/5/d"); 152 assertFalse(cache.exists("1/2/3/4/5/d")); cache.get("1/2/3/4/5/d"); assertTrue(cache.exists("1/2/3/4/5/d")); 155 System.out.println("-- 1/2/3/4/5/d exists"); 156 } 157 158 159 160 public void testCacheLoading2() throws CacheException { 161 Set keys=null; 162 cache.put("/a/b/c", "key", "val"); 163 try { 164 keys=cache.getKeys("/a/b/c"); 165 assertNotNull(keys); 166 assertEquals(1, keys.size()); 167 } 168 catch(Exception e) { 169 fail(e.toString()); 170 } 171 172 try { 173 keys.add("myKey"); 174 } 175 catch(UnsupportedOperationException ex) { 176 fail("unsupported operation: " + ex); 177 } 178 } 179 180 181 public void testExists() throws Exception { 182 cache.put("/eins/zwei/drei", "key1", "val1"); 183 assertTrue(cache.exists("/eins/zwei/drei")); 184 assertTrue(cache.exists("/eins/zwei/drei", "key1")); 185 assertFalse(cache.exists("/eins/zwei/drei", "key2")); 186 assertFalse(cache.exists("/uno/due/tre")); 187 assertFalse(cache.exists("/une/due/tre", "key1")); 188 } 189 190 public void testGetChildren() { 191 try { 192 cache.put("/1/2/3/4/5/d/one", null); 193 cache.put("/1/2/3/4/5/d/two", null); 194 cache.put("/1/2/3/4/5/d/three", null); 195 Set children=cache.getChildrenNames("/1/2/3/4/5/d"); 196 assertNotNull(children); 197 assertEquals(3, children.size()); 198 assertTrue(children.contains("one")); 199 assertTrue(children.contains("two")); 200 assertTrue(children.contains("three")); 201 } 202 catch(Exception e) { 203 fail(e.toString()); 204 } 205 } 206 207 208 public void testGetChildrenWithEviction() throws CacheException { 209 cache.put("/a/b/c/1", null); 210 cache.put("/a/b/c/2", null); 211 cache.put("/a/b/c/3", null); 212 cache.evict(Fqn.fromString("/a/b/c/1")); 213 cache.evict(Fqn.fromString("/a/b/c/2")); 214 cache.evict(Fqn.fromString("/a/b/c/3")); 215 cache.evict(Fqn.fromString("/a/b/c")); 216 cache.evict(Fqn.fromString("/a/b")); 217 cache.evict(Fqn.fromString("/a")); 218 cache.evict(Fqn.fromString("/")); 219 Set children=cache.getChildrenNames("/a/b/c"); 220 assertNotNull(children); 221 assertEquals(3, children.size()); 222 assertTrue(children.contains("1")); 223 assertTrue(children.contains("2")); 224 assertTrue(children.contains("3")); 225 } 226 227 public void testGetChildren2() { 228 try { 229 cache.put("/1", null); 230 cache.put("a", null); 231 Set children=cache.getChildrenNames("/"); 232 assertNotNull(children); 233 assertEquals(2, children.size()); 234 assertTrue(children.contains("1")); 235 assertTrue(children.contains("a")); 236 } 237 catch(Exception e) { 238 fail(e.toString()); 239 } 240 } 241 242 public void testGetChildren3() { 243 try { 244 cache.put("/1", null); 245 cache.put("a", null); 246 Set children=cache.getChildrenNames(""); 247 assertNotNull(children); 248 assertEquals(2, children.size()); 249 assertTrue(children.contains("1")); 250 assertTrue(children.contains("a")); 251 } 252 catch(Exception e) { 253 fail(e.toString()); 254 } 255 } 256 257 public void testGetChildren4() { 258 try { 259 if(!cache.exists("/a/b/c")) 260 cache.put("/a/b/c", null); 261 Set children=cache.getChildrenNames((Fqn)null); 262 assertNull(children); 263 } 264 catch(Exception e) { 265 fail(e.toString()); 266 } 267 } 268 269 270 public void testGetChildren5() { 271 try { 272 cache.put("/a/1", null); 273 cache.put("/a/2", null); 274 cache.put("/a/3", null); 275 System.out.println("cache is " + cache.printLockInfo()); 276 277 Node n=cache.get("/a"); 278 assertNotNull(n); 279 280 Set children=cache.getChildrenNames("/a"); 281 assertNotNull(children); 282 assertEquals(3, children.size()); 283 } 284 catch(Exception e) { 285 fail(e.toString()); 286 } 287 } 288 289 290 public void testGetChildren6() { 291 try { 292 cache.put("/a/1", null); 293 cache.put("/a/2", null); 294 cache.put("/a/3", null); 295 System.out.println("cache is " + cache.printLockInfo()); 296 cache.evict(Fqn.fromString("/a/1")); 297 cache.evict(Fqn.fromString("/a/2")); 298 cache.evict(Fqn.fromString("/a/3")); 299 cache.evict(Fqn.fromString("/a")); 300 System.out.println("cache is " + cache.printLockInfo()); 301 302 assertNotNull(cache.get("/a")); 303 304 Set children=cache.getChildrenNames("/a"); 305 assertNotNull("No children were loaded", children); 306 System.out.println("children: " + children); 307 assertEquals("3 children weren't loaded", 3, children.size()); 308 } 309 catch(Exception e) { 310 fail(e.toString()); 311 } 312 } 313 314 public void testGetChildren7() { 315 try { 316 cache.put("/a/1", null); 317 cache.put("/a/2", null); 318 cache.put("/a/3", null); 319 cache.put("/a", "test", "test"); 320 System.out.println("cache is " + cache.printLockInfo()); 321 cache.evict(Fqn.fromString("/a/1")); 322 cache.evict(Fqn.fromString("/a/2")); 323 cache.evict(Fqn.fromString("/a/3")); 324 cache.evict(Fqn.fromString("/a")); 325 System.out.println("cache is " + cache.printLockInfo()); 326 327 Object val=cache.get("/a", "test"); 328 assertEquals("attributes weren't loaded", "test", val); 329 330 Set children=cache.getChildrenNames("/a"); 331 assertNotNull("No children were loaded", children); 332 System.out.println("children: " + children); 333 assertEquals("3 children weren't loaded", 3, children.size()); 334 } 335 catch(Exception e) { 336 fail(e.toString()); 337 } 338 } 339 340 public void testGetChildren8() { 341 try { 342 cache.put("/a/1", null); 343 cache.put("/a/2", null); 344 cache.put("/a/3", null); 345 System.out.println("cache is " + cache.printLockInfo()); 346 cache.evict(Fqn.fromString("/a/1")); 347 cache.evict(Fqn.fromString("/a/2")); 348 cache.evict(Fqn.fromString("/a/3")); 349 cache.evict(Fqn.fromString("/a")); 350 System.out.println("cache is " + cache.printLockInfo()); 351 352 assertNull(cache.get("/a", "test")); 353 354 cache.get("/a/1"); 355 Set children=cache.getChildrenNames("/a"); 356 assertNotNull("No children were loaded", children); 357 System.out.println("children: " + children); 358 assertEquals("3 children weren't loaded", 3, children.size()); 359 } 360 catch(Exception e) { 361 fail(e.toString()); 362 } 363 } 364 365 public void testGetChildren9() { 366 try { 367 cache.put("/a/1", null); 368 cache.put("/a/2", null); 369 cache.put("/a/3", null); 370 System.out.println("cache is " + cache.printLockInfo()); 371 cache.evict(Fqn.fromString("/a/1")); 372 cache.evict(Fqn.fromString("/a/2")); 373 cache.evict(Fqn.fromString("/a/3")); 374 cache.evict(Fqn.fromString("/a")); 375 System.out.println("cache is " + cache.printLockInfo()); 376 377 assertNull(cache.get("/a", "test")); 378 379 cache.get("/a/1"); 380 Set children=cache.getChildrenNames("/a"); 381 assertNotNull("No children were loaded", children); 382 System.out.println("children: " + children); 383 assertEquals("3 children weren't loaded", 3, children.size()); 384 385 cache.evict(Fqn.fromString("/a/1")); 386 cache.evict(Fqn.fromString("/a/2")); 387 cache.evict(Fqn.fromString("/a/3")); 388 cache.evict(Fqn.fromString("/a")); 389 System.out.println("cache is " + cache.printLockInfo()); 390 391 assertNull(cache.get("/a", "test")); 392 393 cache.get("/a/1"); 394 children=cache.getChildrenNames("/a"); 395 assertNotNull("No children were loaded", children); 396 System.out.println("children: " + children); 397 assertEquals("3 children weren't loaded", 3, children.size()); 398 } 399 catch(Exception e) { 400 fail(e.toString()); 401 } 402 } 403 404 405 public void testGetChildren10() { 406 try { 407 cache.put("/a/1", null); 408 cache.put("/a/2", null); 409 cache.put("/a/3", null); 410 System.out.println("cache is " + cache.printLockInfo()); 411 cache.evict(Fqn.fromString("/a/1")); 412 cache.evict(Fqn.fromString("/a/2")); 413 cache.evict(Fqn.fromString("/a/3")); 414 cache.evict(Fqn.fromString("/a")); 415 System.out.println("cache is " + cache.printLockInfo()); 416 417 assertNull(cache.get("/a", "test")); 418 419 cache.get("/a/1"); 420 Set children=cache.getChildrenNames("/a"); 421 assertNotNull("No children were loaded", children); 422 System.out.println("children: " + children); 423 assertEquals("3 children weren't loaded", 3, children.size()); 424 425 children=cache.getChildrenNames("/a"); 426 assertNotNull("No children were loaded", children); 427 System.out.println("children: " + children); 428 assertEquals("3 children weren't loaded", 3, children.size()); 429 } 430 catch(Exception e) { 431 fail(e.toString()); 432 } 433 } 434 435 public void testRemoveData() throws Exception { 436 String key="/x/y/z/"; 437 cache.put(key, "keyA", "valA"); 438 cache.put(key, "keyB", "valB"); 439 cache.put(key, "keyC", "valC"); 440 assertEquals(3, cache.getKeys(key).size()); 441 cache.removeData(key); 442 Set keys=cache.getKeys(key); 443 assertNull(keys); 444 cache.remove("/x"); 445 Object val=cache.get(key, "keyA"); 446 assertNull(val); 447 } 448 449 450 public void testRemoveData2() throws Exception { 451 Set keys; 452 Fqn key=Fqn.fromString("/x/y/z/"); 453 cache.put(key, "keyA", "valA"); 454 cache.put(key, "keyB", "valB"); 455 cache.put(key, "keyC", "valC"); 456 keys=cache.getKeys(key); 457 assertEquals(3, keys.size()); 458 cache.removeData(key); 459 cache.evict(key); 460 keys=cache.getKeys(key); 461 assertEquals(0, keys.size()); 462 } 463 464 public void testRemoveData3() throws Exception { 465 Set keys; 466 Fqn key=Fqn.fromString("/x/y/z/"); 467 cache.put(key, "keyA", "valA"); 468 cache.put(key, "keyB", "valB"); 469 cache.put(key, "keyC", "valC"); 470 keys=cache.getKeys(key); 471 assertEquals(3, keys.size()); 472 cache.evict(key); 473 cache.removeData(key); 474 keys=cache.getKeys(key); 475 assertNull(keys); 476 } 477 478 public void testRemoveKey() throws Exception { 479 String key="/x/y/z/"; 480 cache.put(key, "keyA", "valA"); 481 cache.put(key, "keyB", "valB"); 482 cache.put(key, "keyC", "valC"); 483 cache.remove(key, "keyA"); 484 assertEquals(2, cache.getKeys(key).size()); 485 cache.remove("/x"); 486 } 487 488 489 public void testRemoveKey2() throws CacheException { 490 final String NODE="/test"; 491 final String KEY="key"; 492 Object retval=null; 493 cache.remove(NODE); 494 retval=cache.put(NODE, KEY, new Integer (10)); 495 assertNull(retval); 496 retval=cache.remove(NODE, KEY); 497 assertEquals(new Integer (10), retval); 498 retval=cache.remove(NODE, KEY); 499 assertNull(retval); 500 } 501 502 public void testRemoveKey3() throws CacheException { 503 final String NODE="/test"; 504 final String KEY="key"; 505 Object retval=null; 506 cache.remove(NODE); 507 retval=cache.put(NODE, KEY, new Integer (10)); 508 assertNull(retval); 509 510 cache.evict(Fqn.fromString(NODE)); retval=cache.remove(NODE, KEY); 512 assertEquals(new Integer (10), retval); 513 514 cache.evict(Fqn.fromString(NODE)); retval=cache.remove(NODE, KEY); 516 assertNull(retval); 517 } 518 519 520 public void testRemove() throws Exception { 521 String key="/x/y/z/"; 522 cache.put(key, "keyA", "valA"); 523 cache.put(key, "keyB", "valB"); 524 cache.put(key, "keyC", "valC"); 525 cache.remove("/x"); 526 assertNull(cache.get(key, "keyA")); 527 Set keys=cache.getKeys(key); 528 assertNull(keys); 529 cache.remove("/x"); 530 } 531 532 533 public void testRemoveRoot() throws Exception { 534 assertNull(cache.getKeys("/")); 535 cache.put("/1/2/3/4/5", null); 536 cache.put("uno/due/tre", null); 537 cache.put("1/2/3/a", null); 538 cache.put("/eins/zwei/drei", null); 539 cache.put("/one/two/three", null); 540 cache.remove("/"); 541 assertEquals(null, cache.getKeys("/")); 542 } 543 544 545 public void testEvictionWithCacheLoader() throws Exception { 546 cache.put("/first/second", "key1", "val1"); cache.put("/first/second/third", "key2", "val2"); cache.evict(Fqn.fromString("/first/second")); assertTrue(cache.exists("/first/second/third")); 550 assertTrue(cache.exists("/first/second")); 551 assertTrue(cache.exists("/first")); 552 String val=(String )cache.get("/first/second", "key1"); assertEquals("val1", val); 554 assertTrue(cache.exists("/first/second/third")); 555 assertTrue(cache.exists("/first/second")); 556 assertTrue(cache.exists("/first")); 557 } 558 559 560 public void testEvictionWithCacheLoader2() throws Exception { 561 cache.put("/first/second/third", "key1", "val1"); cache.evict(Fqn.fromString("/first/second/third")); assertFalse(cache.exists("/first/second/third")); 564 assertTrue(cache.exists("/first/second")); 565 assertTrue(cache.exists("/first")); 566 String val=(String )cache.get("/first/second/third", "key1"); assertEquals("val1", val); 568 assertTrue(cache.exists("/first/second/third")); 569 assertTrue(cache.exists("/first/second")); 570 assertTrue(cache.exists("/first")); 571 } 572 573 574 575 public void testEvictionWithGetChildrenNames() throws Exception { 576 cache.put("/a/1", null); 577 cache.put("/a/2", null); 578 cache.put("/a/3", null); 579 cache.evict(Fqn.fromString("/a/1")); 581 cache.evict(Fqn.fromString("/a/2")); 582 cache.evict(Fqn.fromString("/a/3")); 583 cache.evict(Fqn.fromString("/a")); 584 585 DummyTransactionManager mgr=DummyTransactionManager.getInstance(); 586 mgr.begin(); 587 tx=mgr.getTransaction(); 588 Set children=cache.getChildrenNames("/a"); 589 assertEquals(3, children.size()); 590 assertTrue(children.contains("1")); 591 assertTrue(children.contains("2")); 592 assertTrue(children.contains("3")); 593 assertEquals(4, cache.getNumberOfLocksHeld()); 594 tx.commit(); 595 } 596 597 598 public void testTxPutCommit() throws Exception , NotSupportedException { 599 DummyTransactionManager mgr=DummyTransactionManager.getInstance(); 600 mgr.begin(); 601 tx=mgr.getTransaction(); 602 603 cache.put("/one/two/three", "key1", "val1"); 604 cache.put("/one/two/three/four", "key2", "val2"); 605 tx.commit(); 606 assertNotNull(cache.getKeys("/one/two/three")); 607 Set children=cache.getChildrenNames("/one"); 608 assertEquals(1, children.size()); 609 cache.remove("/"); 610 } 611 612 613 public void testTxPutRollback() throws Exception , NotSupportedException { 614 DummyTransactionManager mgr=DummyTransactionManager.getInstance(); 615 616 cache.remove("/one"); 617 618 mgr.begin(); 619 tx=mgr.getTransaction(); 620 621 cache.put("/one/two/three", "key1", "val1"); 622 cache.put("/one/two/three/four", "key2", "val2"); 623 tx.rollback(); 624 assertNull(cache.getKeys("/one/two/three")); 625 Set children=cache.getChildrenNames("/one"); 626 assertNull(children); 627 } 628 629 630 631 632 633 634 635 636 639 public void testBasicOperations() 640 throws Exception { 641 642 doTestBasicOperations(); 643 } 644 645 648 public void testBasicOperationsTransactional() 649 throws Exception { 650 651 DummyTransactionManager mgr=DummyTransactionManager.getInstance(); 652 mgr.begin(); 653 tx=mgr.getTransaction(); 654 doTestBasicOperations(); 655 tx.commit(); 656 } 657 658 661 private void doTestBasicOperations() throws Exception { 662 663 664 doPutTests(new Fqn("key")); 665 doRemoveTests(new Fqn("key")); 666 668 669 doPutTests(new Fqn("key1")); 670 doPutTests(new Fqn("key3")); 671 doPutTests(new Fqn("key2")); 672 assertEquals(4, loader.get(new Fqn("key1")).size()); 673 assertEquals(4, loader.get(new Fqn("key2")).size()); 674 assertEquals(4, loader.get(new Fqn("key3")).size()); 675 676 677 doRemoveTests(new Fqn("key2")); 678 doRemoveTests(new Fqn("key3")); 679 doRemoveTests(new Fqn("key1")); 680 assertEquals(null, loader.get(new Fqn("key1"))); 681 assertEquals(null, loader.get(new Fqn("key2"))); 682 assertEquals(null, loader.get(new Fqn("key3"))); 683 } 685 686 689 private void doPutTests(Fqn fqn) 690 throws Exception { 691 692 assertTrue(!loader.exists(fqn)); 693 694 695 Object oldVal; 696 oldVal = loader.put(fqn, "one", "two"); 697 assertNull(oldVal); 698 oldVal = loader.put(fqn, "three", "four"); 699 assertNull(oldVal); 700 assertEquals("two", loader.get(fqn, "one")); 701 assertEquals("four", loader.get(fqn, "three")); 702 oldVal = loader.put(fqn, "one", "xxx"); 703 assertEquals("two", oldVal); 704 oldVal = loader.put(fqn, "one", "two"); 705 assertEquals("xxx", oldVal); 706 707 708 Map map = loader.get(fqn); 709 assertEquals(2, map.size()); 710 assertEquals("two", map.get("one")); 711 assertEquals("four", map.get("three")); 712 713 714 map.put("five", "six"); 715 map.put("seven", "eight"); 716 loader.put(fqn, map); 717 assertEquals("six", loader.get(fqn, "five")); 718 assertEquals("eight", loader.get(fqn, "seven")); 719 assertEquals(map, loader.get(fqn)); 720 assertEquals(4, map.size()); 721 722 assertTrue(loader.exists(fqn)); 723 } 724 725 728 private void doRemoveTests(Fqn fqn) 729 throws Exception { 730 731 732 Object oldVal; 733 oldVal = loader.remove(fqn, "one"); 734 assertEquals("two", oldVal); 735 oldVal = loader.remove(fqn, "five"); 736 assertEquals("six", oldVal); 737 assertEquals(null, loader.get(fqn, "one")); 738 assertEquals(null, loader.get(fqn, "five")); 739 assertEquals("four", loader.get(fqn, "three")); 740 assertEquals("eight", loader.get(fqn, "seven")); 741 Map map = loader.get(fqn); 742 assertEquals(2, map.size()); 743 assertEquals("four", map.get("three")); 744 assertEquals("eight", map.get("seven")); 745 746 747 assertTrue(loader.exists(fqn)); 748 loader.remove(fqn); 749 assertEquals(null, loader.get(fqn, "three")); 750 assertEquals(null, loader.get(fqn, "seven")); 751 map = loader.get(fqn); 752 assertEquals(null, map); 753 assertTrue(!loader.exists(fqn)); 754 } 755 756 760 public void testMultiLevelTree() 761 throws Exception { 762 763 764 assertTrue(!loader.exists(new Fqn("key0"))); 765 loader.put(Fqn.fromString("/key0/level1/level2"), null); 766 assertTrue(loader.exists(Fqn.fromString("/key0/level1/level2"))); 767 assertTrue(loader.exists(Fqn.fromString("/key0/level1"))); 768 assertTrue(loader.exists(new Fqn("key0"))); 769 770 771 loader.put(Fqn.fromString("/key0/x/y"), null); 772 assertTrue(loader.exists(Fqn.fromString("/key0/x/y"))); 773 assertTrue(loader.exists(Fqn.fromString("/key0/x"))); 774 loader.remove(Fqn.fromString("/key0/x/y")); 775 assertTrue(!loader.exists(Fqn.fromString("/key0/x/y"))); 776 assertTrue(loader.exists(Fqn.fromString("/key0/x"))); 777 778 779 loader.remove(new Fqn("key0")); 780 assertTrue(!loader.exists(new Fqn("key0"))); 781 assertTrue(!loader.exists(Fqn.fromString("/key0/level1/level2"))); 782 assertTrue(!loader.exists(Fqn.fromString("/key0/level1"))); 783 assertTrue(!loader.exists(Fqn.fromString("/key0/x"))); 784 785 786 loader.put(new Fqn("key1"), null); 787 loader.put(new Fqn("key2"), null); 788 loader.put(new Fqn("key3"), null); 789 assertTrue(loader.exists(new Fqn("key1"))); 790 assertTrue(loader.exists(new Fqn("key2"))); 791 assertTrue(loader.exists(new Fqn("key3"))); 792 793 794 assertTrue(!loader.exists(Fqn.fromString("/key3/level1"))); 795 assertTrue(!loader.exists(Fqn.fromString("/key3/level1/level2"))); 796 loader.put(Fqn.fromString("/key3/level1/level2"), null); 797 assertTrue(loader.exists(Fqn.fromString("/key3/level1/level2"))); 798 assertTrue(loader.exists(Fqn.fromString("/key3/level1"))); 799 800 801 assertTrue(loader.exists(new Fqn("key1"))); 802 assertTrue(loader.exists(new Fqn("key2"))); 803 assertTrue(loader.exists(new Fqn("key3"))); 804 805 806 loader.remove(Fqn.fromString("/key3/level1")); 807 assertTrue(!loader.exists(Fqn.fromString("/key3/level1/level2"))); 808 assertTrue(!loader.exists(Fqn.fromString("/key3/level1"))); 809 810 811 assertTrue(loader.exists(new Fqn("key1"))); 812 assertTrue(loader.exists(new Fqn("key2"))); 813 assertTrue(loader.exists(new Fqn("key3"))); 814 815 816 loader.remove(new Fqn("key1")); 817 assertTrue(!loader.exists(new Fqn("key1"))); 818 assertTrue(loader.exists(new Fqn("key2"))); 819 assertTrue(loader.exists(new Fqn("key3"))); 820 821 822 loader.remove(new Fqn("key3")); 823 assertTrue(loader.exists(new Fqn("key2"))); 824 assertTrue(!loader.exists(new Fqn("key3"))); 825 826 827 loader.remove(new Fqn("key2")); 828 assertTrue(!loader.exists(new Fqn("key0"))); 829 assertTrue(!loader.exists(new Fqn("key1"))); 830 assertTrue(!loader.exists(new Fqn("key2"))); 831 assertTrue(!loader.exists(new Fqn("key3"))); 832 833 834 835 assertNull(loader.get(new Fqn("key0"))); 836 loader.put(Fqn.fromString("/key0/level1/level2"), "a", "b"); 837 assertNotNull(loader.get(Fqn.fromString("/key0/level1/level2"))); 838 assertNull(loader.get(Fqn.fromString("/key0/level1"))); 839 assertNull(loader.get(new Fqn("key0"))); 840 841 loader.put(Fqn.fromString("/key0/x/y"), "a", "b"); 842 assertNotNull(loader.get(Fqn.fromString("/key0/x/y"))); 843 assertNull(loader.get(Fqn.fromString("/key0/x"))); 844 loader.remove(Fqn.fromString("/key0/x/y")); 845 assertNull(loader.get(Fqn.fromString("/key0/x/y"))); 846 assertNull(loader.get(Fqn.fromString("/key0/x"))); 847 848 loader.remove(new Fqn("key0")); 849 assertNull(loader.get(new Fqn("key0"))); 850 assertNull(loader.get(Fqn.fromString("/key0/level1/level2"))); 851 assertNull(loader.get(Fqn.fromString("/key0/level1"))); 852 assertNull(loader.get(Fqn.fromString("/key0/x"))); 853 854 loader.put(new Fqn("key1"), "a", "b"); 855 loader.put(new Fqn("key2"), "a", "b"); 856 loader.put(new Fqn("key3"), "a", "b"); 857 assertNotNull(loader.get(new Fqn("key1"))); 858 assertNotNull(loader.get(new Fqn("key2"))); 859 assertNotNull(loader.get(new Fqn("key3"))); 860 861 assertNull(loader.get(Fqn.fromString("/key3/level1"))); 862 assertNull(loader.get(Fqn.fromString("/key3/level1/level2"))); 863 loader.put(Fqn.fromString("/key3/level1/level2"), "a", "b"); 864 assertNotNull(loader.get(Fqn.fromString("/key3/level1/level2"))); 865 assertNull(loader.get(Fqn.fromString("/key3/level1"))); 866 867 assertNotNull(loader.get(new Fqn("key1"))); 868 assertNotNull(loader.get(new Fqn("key2"))); 869 assertNotNull(loader.get(new Fqn("key3"))); 870 871 loader.remove(Fqn.fromString("/key3/level1")); 872 assertNull(loader.get(Fqn.fromString("/key3/level1/level2"))); 873 assertNull(loader.get(Fqn.fromString("/key3/level1"))); 874 875 assertNotNull(loader.get(new Fqn("key1"))); 876 assertNotNull(loader.get(new Fqn("key2"))); 877 assertNotNull(loader.get(new Fqn("key3"))); 878 879 loader.remove(new Fqn("key1")); 880 assertNull(loader.get(new Fqn("key1"))); 881 assertNotNull(loader.get(new Fqn("key2"))); 882 assertNotNull(loader.get(new Fqn("key3"))); 883 884 loader.remove(new Fqn("key3")); 885 assertNotNull(loader.get(new Fqn("key2"))); 886 assertNull(loader.get(new Fqn("key3"))); 887 888 loader.remove(new Fqn("key2")); 889 assertNull(loader.get(new Fqn("key0"))); 890 assertNull(loader.get(new Fqn("key1"))); 891 assertNull(loader.get(new Fqn("key2"))); 892 assertNull(loader.get(new Fqn("key3"))); 893 } 894 895 898 public void testGetChildrenNames() 899 throws Exception { 900 901 checkChildren(new Fqn(), null); 902 checkChildren(Fqn.fromString("/key0"), null); 903 904 loader.put(Fqn.fromString("/key0"), null); 905 checkChildren(new Fqn(), new String [] { "key0" }); 906 907 loader.put(Fqn.fromString("/key1/x"), null); 908 checkChildren(new Fqn(), new String [] { "key0", "key1" }); 909 checkChildren(Fqn.fromString("/key1"), new String [] { "x" }); 910 911 loader.remove(Fqn.fromString("/key1/x")); 912 checkChildren(new Fqn(), new String [] { "key0", "key1" }); 913 checkChildren(Fqn.fromString("/key0"), null); 914 checkChildren(Fqn.fromString("/key1"), null); 915 916 loader.put(Fqn.fromString("/key0/a"), null); 917 loader.put(Fqn.fromString("/key0/ab"), null); 918 loader.put(Fqn.fromString("/key0/abc"), null); 919 checkChildren(Fqn.fromString("/key0"), 920 new String [] { "a", "ab", "abc" }); 921 922 loader.put(Fqn.fromString("/key0/xxx"), null); 923 loader.put(Fqn.fromString("/key0/xx"), null); 924 loader.put(Fqn.fromString("/key0/x"), null); 925 checkChildren(Fqn.fromString("/key0"), 926 new String [] { "a", "ab", "abc", "x", "xx", "xxx" }); 927 928 loader.put(Fqn.fromString("/key0/a/1"), null); 929 loader.put(Fqn.fromString("/key0/a/2"), null); 930 loader.put(Fqn.fromString("/key0/a/2/1"), null); 931 checkChildren(Fqn.fromString("/key0/a/2"), new String [] { "1" }); 932 checkChildren(Fqn.fromString("/key0/a"), new String [] { "1", "2" }); 933 checkChildren(Fqn.fromString("/key0"), 934 new String [] { "a", "ab", "abc", "x", "xx", "xxx" }); 935 } 956 957 960 private void checkChildren(Fqn fqn, String [] names) 961 throws Exception { 962 963 Set set = loader.getChildrenNames(fqn); 964 if (names != null) { 965 assertEquals(names.length, set.size()); 966 for (int i = 0; i < names.length; i += 1) { 967 assertTrue(set.contains(names[i])); 968 } 969 } else { 970 assertNull(set); 971 } 972 } 973 974 977 public void testModifications() 978 throws Exception { 979 980 doTestModifications(); 981 } 982 983 986 public void testModificationsTransactional() 987 throws Exception { 988 DummyTransactionManager mgr=DummyTransactionManager.getInstance(); 989 mgr.begin(); 990 tx=mgr.getTransaction(); 991 doTestModifications(); 992 tx.commit(); 993 } 994 995 998 private void doTestModifications() 999 throws Exception { 1000 1001 1002 List list = createUpdates(); 1003 loader.put(list); 1004 checkModifications(list); 1005 1006 1007 list = new ArrayList(); 1008 Modification mod = new Modification(); 1009 mod.setType(Modification.REMOVE_KEY_VALUE); 1010 mod.setFqn(FQN); 1011 mod.setKey("one"); 1012 list.add(mod); 1013 loader.put(list); 1014 checkModifications(list); 1015 1016 1017 list = new ArrayList(); 1018 mod = new Modification(); 1019 mod.setType(Modification.REMOVE_NODE); 1020 mod.setFqn(FQN); 1021 list.add(mod); 1022 loader.put(list); 1023 checkModifications(list); 1024 assertEquals(null, loader.get(FQN)); 1025 1026 1027 loader.put(FQN, "one", "two"); 1028 list = new ArrayList(); 1029 mod = new Modification(); 1030 mod.setType(Modification.REMOVE_DATA); 1031 mod.setFqn(FQN); 1032 list.add(mod); 1033 loader.put(list); 1034 checkModifications(list); 1035 } 1036 1037 1040 public void testOnePhaseTransaction() 1041 throws Exception { 1042 List mods = createUpdates(); 1043 loader.prepare(null, mods, true); 1044 checkModifications(mods); 1045 } 1046 1047 1050 public void testTwoPhaseTransaction() 1051 throws Exception { 1052 1053 Object txnKey = new Object (); 1054 List mods = createUpdates(); 1055 loader.prepare(txnKey, mods, false); 1056 loader.commit(txnKey); 1061 checkModifications(mods); 1062 } 1063 1064 1067 public void testTransactionRollback() 1068 throws Exception { 1069 1070 loader.remove(Fqn.fromString("/")); 1071 1072 int num=loader.loadEntireState().length; 1073 1074 Object txnKey = new Object (); 1075 List mods = createUpdates(); 1076 loader.prepare(txnKey, mods, false); 1077 loader.rollback(txnKey); 1078 assertEquals(num, loader.loadEntireState().length); 1079 } 1080 1081 1084 private List createUpdates() { 1085 1086 List list = new ArrayList(); 1087 1088 Modification mod = new Modification(); 1089 mod.setType(Modification.PUT_KEY_VALUE); 1090 mod.setFqn(FQN); 1091 mod.setKey("one"); 1092 mod.setValue("two"); 1093 list.add(mod); 1094 1095 mod = new Modification(); 1096 mod.setType(Modification.PUT_KEY_VALUE); 1097 mod.setFqn(FQN); 1098 mod.setKey("three"); 1099 mod.setValue("four"); 1100 list.add(mod); 1101 1102 Map map = new HashMap(); 1103 map.put("five", "six"); 1104 map.put("seven", "eight"); 1105 mod = new Modification(); 1106 mod.setType(Modification.PUT_DATA); 1107 mod.setFqn(FQN); 1108 mod.setData(map); 1109 list.add(mod); 1110 1111 return list; 1112 } 1113 1114 1117 private void checkModifications(List list) 1118 throws Exception { 1119 1120 for (int i = 0; i < list.size(); i += 1) { 1121 Modification mod = (Modification) list.get(i); 1122 Fqn fqn = mod.getFqn(); 1123 switch (mod.getType()) { 1124 case Modification.PUT_KEY_VALUE: 1125 assertEquals(mod.getValue(), loader.get(fqn, mod.getKey())); 1126 break; 1127 case Modification.PUT_DATA: 1128 Map map = mod.getData(); 1129 for (Iterator iter = map.keySet().iterator(); iter.hasNext();) { 1130 Object key = iter.next(); 1131 assertEquals(map.get(key), loader.get(fqn, key)); 1132 } 1133 break; 1134 case Modification.REMOVE_KEY_VALUE: 1135 assertEquals(null, loader.get(fqn, mod.getKey())); 1136 break; 1137 case Modification.REMOVE_DATA: 1138 map = loader.get(fqn); 1139 assertNull(map); 1140 break; 1141 case Modification.REMOVE_NODE: 1142 assertEquals(null, loader.get(fqn)); 1143 break; 1144 default: 1145 fail("unknown type: " + mod); 1146 break; 1147 } 1148 } 1149 } 1150 1151 1152 1153 1154 1155 1156 1157 1160 public void testNullKeysAndValues() 1161 throws Exception { 1162 1163 loader.put(FQN, null, "x"); 1164 assertEquals("x", loader.get(FQN, null)); 1165 Map map = loader.get(FQN); 1166 assertEquals(1, map.size()); 1167 assertEquals("x", map.get(null)); 1168 1169 loader.put(FQN, "y", null); 1170 assertEquals(null, loader.get(FQN, "y")); 1171 map = loader.get(FQN); 1172 assertEquals(2, map.size()); 1173 assertEquals("x", map.get(null)); 1174 assertEquals(null, map.get("y")); 1175 1176 loader.remove(FQN, null); 1177 assertEquals(null, loader.get(FQN, null)); 1178 assertEquals(1, loader.get(FQN).size()); 1179 1180 loader.remove(FQN, "y"); 1181 assertEquals(null, loader.get(FQN, "y")); 1182 assertNull(loader.get(FQN)); 1183 1184 map = new HashMap(); 1185 map.put(null, null); 1186 loader.put(FQN, map); 1187 assertEquals(map, loader.get(FQN)); 1188 1189 loader.remove(FQN); 1190 assertEquals(null, loader.get(FQN)); 1191 1192 map = new HashMap(); 1193 map.put("xyz", null); 1194 map.put(null, "abc"); 1195 loader.put(FQN, map); 1196 assertEquals(map, loader.get(FQN)); 1197 1198 loader.remove(FQN); 1199 assertEquals(null, loader.get(FQN)); 1200 } 1201 1202 1205 public void testDatabaseName() 1206 throws Exception { 1207 1208 loader.put(FQN, "one", "two"); 1209 assertEquals("two", loader.get(FQN, "one")); 1210 } 1211 1212 1215 public void testLoadAndStore() 1216 throws Exception { 1217 1218 1219 loader.remove(Fqn.fromString("/")); 1220 1227 1228 Complex c1 = new Complex(); 1229 Complex c2 = new Complex(c1); 1230 1231 1232 loader.put(FQN, new Integer (1), c1); 1233 loader.put(FQN, new Integer (2), c2); 1234 assertEquals(c1, loader.get(FQN, new Integer (1))); 1235 assertEquals(c2, loader.get(FQN, new Integer (2))); 1236 assertEquals(2, loader.get(FQN).size()); 1237 1238 1239 byte[] state = loader.loadEntireState(); 1240 assertTrue(state.length > 0); 1241 1242 1243 1244 loader.storeEntireState(state); 1245 assertEquals(c1, loader.get(FQN, new Integer (1))); 1246 assertEquals(c2, loader.get(FQN, new Integer (2))); 1247 assertEquals(2, loader.get(FQN).size()); 1248 } 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1266 private static class Complex implements Serializable { 1267 1268 Complex nested; 1269 1270 Complex() { 1271 this(null); 1272 } 1273 1274 Complex(Complex nested) { 1275 this.nested = nested; 1276 } 1277 1278 public boolean equals(Object o) { 1279 try { 1280 Complex x = (Complex) o; 1281 return (nested != null) ? nested.equals(x.nested) 1282 : (x.nested == null); 1283 } catch (ClassCastException e) { 1284 return false; 1285 } 1286 } 1287 1288 public int hashCode() { 1289 if(nested == null) 1290 return super.hashCode(); 1291 else 1292 return 13 + nested.hashCode(); 1293 } 1294 } 1295 1296 1297 public static Test suite() { 1298 return new TestSuite(CacheLoaderTestsBase.class); 1299 } 1300 1301 public static void main(String [] args) { 1302 junit.textui.TestRunner.run(suite()); 1303 } 1304 1305} 1306 | Popular Tags |