1 package org.jboss.cache.loader; 2 3 import org.apache.commons.logging.Log; 4 import org.apache.commons.logging.LogFactory; 5 import org.jboss.cache.CacheException; 6 import org.jboss.cache.CacheImpl; 7 import org.jboss.cache.Fqn; 8 import org.jboss.cache.Modification; 9 import org.jboss.cache.Node; 10 import org.jboss.cache.buddyreplication.BuddyManager; 11 import org.jboss.cache.config.Configuration; 12 import org.jboss.cache.statetransfer.StateTransferManager; 13 import org.jboss.cache.transaction.DummyTransactionManager; 14 import org.jboss.util.stream.MarshalledValueInputStream; 15 import org.jboss.util.stream.MarshalledValueOutputStream; 16 17 import javax.transaction.Transaction ; 18 import java.io.ByteArrayInputStream ; 19 import java.io.ByteArrayOutputStream ; 20 import java.io.File ; 21 import java.io.Serializable ; 22 import java.util.ArrayList ; 23 import java.util.HashMap ; 24 import java.util.List ; 25 import java.util.Map ; 26 import java.util.Set ; 27 28 34 abstract public class CacheLoaderTestsBase extends AbstractCacheLoaderTestBase 35 { 36 37 private static final Log log = LogFactory.getLog(CacheLoaderTestsBase.class); 38 CacheImpl cache; 39 CacheLoader loader = null; 40 private Transaction tx = null; 41 static final Fqn FQN = new Fqn("key"); 42 private static final Fqn SUBTREE_FQN = new Fqn(FQN, "subtree"); 43 44 private static final Fqn BUDDY_BASE = new Fqn(BuddyManager.BUDDY_BACKUP_SUBTREE_FQN, "test"); 45 46 private static final Fqn BUDDY_PLUS_FQN = new Fqn(BUDDY_BASE, FQN); 47 48 private static final Fqn BUDDY_PLUS_SUBTREE_FQN = new Fqn(BUDDY_BASE, SUBTREE_FQN); 49 50 51 public CacheLoaderTestsBase(String name) 52 { 53 super(name); 54 } 55 56 public CacheLoaderTestsBase() 57 { 58 super(); 59 } 60 61 protected void setUp() throws Exception 62 { 63 super.setUp(); 64 log.debug("\nTest " + getName() + "\n"); 65 cache = new CacheImpl(); 66 Configuration c = new Configuration(); 67 cache.setConfiguration(c); 68 c.setCacheMode(Configuration.CacheMode.LOCAL); 69 c.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 70 configureCache(); 71 cache.start(); 72 loader = cache.getCacheLoaderManager().getCacheLoader(); 73 } 74 75 abstract protected void configureCache() throws Exception ; 76 77 78 protected void tearDown() throws Exception 79 { 80 super.tearDown(); 81 if (tx != null) 82 { 83 try 84 { 85 tx.commit(); 86 } 87 catch (Throwable e) 88 { 89 e.printStackTrace(); 90 } 91 } 92 cache.remove("/"); 93 loader.remove(Fqn.fromString("/")); 94 cache.stop(); 95 cache.destroy(); 96 } 97 98 protected void addDelay() 99 { 100 } 102 103 private void clean(File dir) 104 { 105 File [] files = dir.listFiles(); 106 if (files != null) 107 { 108 for (File file : files) 109 { 110 if (file.isDirectory()) 111 { 112 clean(file); 113 } 114 else 115 { 116 if (!file.delete()) file.deleteOnExit(); 117 } 118 } 119 } 120 } 121 122 123 public void testPrint() throws CacheException 124 { 125 final Fqn NODE = Fqn.fromString("/test"); 126 final String KEY = "key"; 127 cache.put(NODE, KEY, 10); 128 cache.evict(NODE); 129 addDelay(); 130 String ret = cache.print(NODE); 131 assertNotNull(ret); 132 } 133 134 public void testPut() throws CacheException 135 { 136 final String NODE = "/test"; 137 final String KEY = "key"; 138 Object retval; 139 cache.remove(NODE); 140 addDelay(); 141 retval = cache.put(NODE, KEY, 10); 142 assertEquals(null, retval); 143 retval = cache.put(NODE, KEY, 20); 144 addDelay(); 145 assertEquals(10, retval); 146 cache.evict(Fqn.fromString(NODE)); addDelay(); 148 log.debug("put 30, expect 20 back"); 149 retval = cache.put(NODE, KEY, 30); 150 assertEquals(20, retval); 151 } 152 153 public void testPut2() throws Exception 154 { 155 final String NODE = "/a/b/c"; 156 assertNull(loader.get(Fqn.fromString(NODE))); 157 final String KEY = "key"; 158 Object retval; 159 cache.remove(NODE); 160 assertNull(loader.get(Fqn.fromString(NODE))); 161 addDelay(); 162 retval = cache.put(NODE, KEY, 10); 163 assertNull(retval); 164 addDelay(); 165 retval = cache.put(NODE, KEY, 20); 166 assertEquals(10, retval); 167 cache.evict(Fqn.fromString(NODE)); cache.evict(Fqn.fromString("/a/b")); 169 cache.evict(Fqn.fromString("/a")); 170 addDelay(); 171 log.debug("replace KEY with 30, expect 20"); 172 retval = cache.put(NODE, KEY, 30); 173 assertEquals(20, retval); 174 } 175 176 179 public void testPut3() throws Exception 180 { 181 final Fqn NODE = Fqn.fromString("/a/b/c"); 182 183 cache.remove(NODE); 184 addDelay(); 185 Map m = new HashMap (); 186 m.put("a", "b"); 187 m.put("c", "d"); 188 Map m2 = new HashMap (); 189 m2.put("e", "f"); 190 m2.put("g", "h"); 191 192 cache.put(NODE, m); 193 194 addDelay(); 195 cache.get(NODE, "X"); 196 197 assertEquals(m, loader.get(NODE)); 198 assertEquals(m, cache.get(NODE).getData()); 199 cache.evict(NODE); 200 addDelay(); 201 cache.get(NODE, "X"); 202 assertEquals(m, cache.get(NODE).getData()); 203 cache.evict(NODE); 204 cache.get(NODE, "X"); 205 cache.put(NODE, m2); 206 assertEquals("combined", 4, cache.get(NODE).getData().size()); 207 } 208 209 public void testShallowMove() throws Exception 210 { 211 Fqn a = Fqn.fromString("/a"); 212 Fqn b = Fqn.fromString("/b"); 213 Fqn a_b = Fqn.fromString("/a/b"); 214 String key = "key", valueA = "A", valueB = "B"; 215 216 cache.put(a, key, valueA); 217 cache.put(b, key, valueB); 218 219 addDelay(); 220 221 CacheLoader loader = cache.getCacheLoaderManager().getCacheLoader(); 222 assertEquals(valueA, loader.get(a).get(key)); 223 assertEquals(valueB, loader.get(b).get(key)); 224 assertTrue(loader.getChildrenNames(Fqn.ROOT).contains("a")); 225 assertTrue(loader.getChildrenNames(Fqn.ROOT).contains("b")); 226 227 cache.move(b, a); 229 230 addDelay(); 231 232 assertEquals(valueA, loader.get(a).get(key)); 233 assertNull(loader.get(b)); 234 assertEquals(valueB, loader.get(a_b).get(key)); 235 236 } 237 238 public void testDeepMove() throws Exception 239 { 240 Fqn a = Fqn.fromString("/a"); 241 Fqn b = Fqn.fromString("/b"); 242 Fqn a_b = Fqn.fromString("/a/b"); 243 Fqn b_c = Fqn.fromString("/b/c"); 244 Fqn a_b_c = Fqn.fromString("/a/b/c"); 245 246 String key = "key", valueA = "A", valueB = "B", valueC = "C"; 247 248 cache.put(a, key, valueA); 249 cache.put(b, key, valueB); 250 cache.put(b_c, key, valueC); 251 252 253 addDelay(); 254 255 assertEquals(valueA, cache.getCacheLoaderManager().getCacheLoader().get(a).get(key)); 256 assertEquals(valueB, cache.getCacheLoaderManager().getCacheLoader().get(b).get(key)); 257 assertEquals(valueC, cache.getCacheLoaderManager().getCacheLoader().get(b_c).get(key)); 258 259 cache.move(b, a); 261 262 addDelay(); 263 264 assertEquals(valueA, cache.getCacheLoaderManager().getCacheLoader().get(a).get(key)); 265 assertNull(cache.getCacheLoaderManager().getCacheLoader().get(b)); 266 assertEquals(valueB, cache.getCacheLoaderManager().getCacheLoader().get(a_b).get(key)); 267 assertNull(cache.getCacheLoaderManager().getCacheLoader().get(b_c)); 268 assertEquals(valueC, cache.getCacheLoaderManager().getCacheLoader().get(a_b_c).get(key)); 269 270 } 271 272 273 276 public void testPutRemoveCombos() throws Exception 277 { 278 final String NODE = "/a/b/c"; 279 cache.remove(NODE); 280 Fqn fqn = Fqn.fromString(NODE); 281 addDelay(); 282 Map m = new HashMap (); 283 m.put("a", "b"); 284 m.put("c", "d"); 285 loader.put(fqn, m); 286 System.out.println("*** MANIK: LOADER " + loader.get(fqn)); 287 cache.put(NODE, "e", "f"); 288 System.out.println("*** MANIK: CACHE " + cache.get(NODE).getData()); 289 System.out.println("*** MANIK: LOADER " + loader.get(fqn)); 290 addDelay(); 291 System.out.println("*** MANIK: CACHE " + cache.get(NODE)); 292 cache.get(NODE, "X"); 293 System.out.println("*** MANIK: CACHE " + cache.get(NODE).getData()); 294 assertEquals(3, cache.get(NODE).getData().size()); 295 cache.evict(fqn); 296 cache.get(NODE, "X"); 297 cache.remove(NODE, "e"); 298 assertEquals(2, cache.get(NODE).getData().size()); 299 } 300 301 public void testGet() throws CacheException 302 { 303 final String NODE = "/a/b/c"; 304 Object retval; 305 cache.remove(NODE); 306 addDelay(); 307 retval = cache.put(NODE, "1", 10); 308 assertNull(retval); 309 addDelay(); 310 cache.put(NODE, "2", 20); 311 cache.evict(Fqn.fromString("/a/b/c")); 312 assertTrue("DataNode should not exisit ", !cache.exists("/a/b/c")); 313 addDelay(); 314 retval = cache.get(NODE, "1"); 315 assertEquals(10, retval); 316 retval = cache.get(NODE, "2"); 317 assertEquals(20, retval); 318 } 319 320 public void testGetNode() throws CacheException 321 { 322 final String NODE = "/a/b/c"; 323 Object retval; 324 cache.remove(NODE); 325 addDelay(); 326 cache.put(NODE, "1", 10); 327 cache.evict(Fqn.fromString(NODE)); 328 assertTrue("DataNode should not exisit ", !cache.exists("/a/b/c")); 329 addDelay(); 330 retval = cache.get(NODE); 331 332 assertNotNull("Should not be null", retval); 333 334 Node node = (Node) retval; 335 assertEquals(10, node.get("1")); 336 } 337 338 339 public void testSerialization() throws CacheException 340 { 341 SamplePojo pojo = new SamplePojo(39, "Bela"); 342 pojo.getHobbies().add("Running"); 343 pojo.getHobbies().add("Beerathlon"); 344 pojo.getHobbies().add("Triathlon"); 345 cache.put("/mypojo", 322649, pojo); 346 addDelay(); 347 assertNotNull(cache.get("/mypojo", 322649)); 348 cache.evict(Fqn.fromString("/mypojo")); 349 assertFalse(cache.exists("/mypojo")); 350 SamplePojo pojo2 = (SamplePojo) cache.get("/mypojo", 322649); assertNotNull(pojo2); 352 assertEquals(39, pojo2.getAge()); 353 assertEquals("Bela", pojo2.getName()); 354 assertEquals(3, pojo2.getHobbies().size()); 355 } 356 357 360 public void testPopulate() 361 { 362 try 363 { 364 Map m = new HashMap (); 365 for (int i = 0; i < 10; i++) 366 { 367 m.put("key" + i, "val" + i); 368 } 369 cache.put("/a/b/c", m); 370 cache.load("/1/2/3/4/5"); 371 cache.put("/1/2/3/4/5", null); 372 cache.put("/1/2/3/4/5/a", null); 373 cache.put("/1/2/3/4/5/b", null); 374 cache.put("/1/2/3/4/5/c", null); 375 cache.put("/1/2/3/4/5/d", null); 376 cache.put("/1/2/3/4/5/e", null); 377 cache.put("/1/2/3/4/5/d/one", null); 378 cache.put("/1/2/3/4/5/d/two", null); 379 cache.put("/1/2/3/4/5/d/three", null); 380 System.out.println("cache: " + cache); 382 383 assertTrue(cache.exists("/1/2/3/4")); 384 assertTrue(cache.exists("/a/b/c")); 385 assertFalse(cache.exists("/a/b/c/d")); 386 } 387 catch (Exception e) 388 { 389 fail(e.toString()); 390 } 391 } 392 393 394 public void testPreloading() throws CacheException 395 { 396 cache.remove("/"); 397 cache.put("1/2/3/4/5/d", "key", "val"); 398 cache.evict(Fqn.fromString("1/2/3/4/5/d")); 399 System.out.println("-- checking for 1/2/3/4/5/d"); 400 addDelay(); 401 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")); 404 System.out.println("-- 1/2/3/4/5/d exists"); 405 } 406 407 408 public void testCacheLoading2() throws CacheException 409 { 410 Set keys = null; 411 cache.put("/a/b/c", "key", "val"); 412 try 413 { 414 keys = cache.getKeys("/a/b/c"); 415 assertNotNull(keys); 416 assertEquals(1, keys.size()); 417 } 418 catch (Exception e) 419 { 420 fail(e.toString()); 421 } 422 423 try 424 { 425 keys.add("myKey"); 426 } 427 catch (UnsupportedOperationException ex) 428 { 429 fail("unsupported operation: " + ex); 430 } 431 } 432 433 434 public void testExists() 435 { 436 cache.put("/eins/zwei/drei", "key1", "val1"); 437 assertTrue(cache.exists("/eins/zwei/drei")); 438 assertTrue(cache.exists("/eins/zwei/drei", "key1")); 439 assertFalse(cache.exists("/eins/zwei/drei", "key2")); 440 assertFalse(cache.exists("/uno/due/tre")); 441 assertFalse(cache.exists("/une/due/tre", "key1")); 442 } 443 444 public void testGetChildren() 445 { 446 try 447 { 448 cache.put("/1/2/3/4/5/d/one", null); 449 cache.put("/1/2/3/4/5/d/two", null); 450 cache.put("/1/2/3/4/5/d/three", null); 451 Set children = cache.getChildrenNames("/1/2/3/4/5/d"); 452 assertNotNull(children); 453 assertEquals(3, children.size()); 454 assertTrue(children.contains("one")); 455 assertTrue(children.contains("two")); 456 assertTrue(children.contains("three")); 457 } 458 catch (Exception e) 459 { 460 fail(e.toString()); 461 } 462 } 463 464 465 public void testGetChildrenWithEviction() throws CacheException 466 { 467 cache.put("/a/b/c/1", null); 468 cache.put("/a/b/c/2", null); 469 cache.put("/a/b/c/3", null); 470 cache.evict(Fqn.fromString("/a/b/c/1")); 471 cache.evict(Fqn.fromString("/a/b/c/2")); 472 cache.evict(Fqn.fromString("/a/b/c/3")); 473 cache.evict(Fqn.fromString("/a/b/c")); 474 cache.evict(Fqn.fromString("/a/b")); 475 cache.evict(Fqn.fromString("/a")); 476 cache.evict(Fqn.fromString("/")); 477 addDelay(); 478 Set children = cache.getChildrenNames("/a/b/c"); 479 assertNotNull(children); 480 assertEquals(3, children.size()); 481 assertTrue(children.contains("1")); 482 assertTrue(children.contains("2")); 483 assertTrue(children.contains("3")); 484 } 485 486 public void testGetChildren2() 487 { 488 try 489 { 490 cache.put("/1", null); 491 cache.put("a", null); 492 Set children = cache.getChildrenNames("/"); 493 assertNotNull(children); 494 assertEquals(2, children.size()); 495 assertTrue(children.contains("1")); 496 assertTrue(children.contains("a")); 497 } 498 catch (Exception e) 499 { 500 fail(e.toString()); 501 } 502 } 503 504 public void testGetChildren3() 505 { 506 try 507 { 508 cache.put("/1", null); 509 cache.put("a", null); 510 Set children = cache.getChildrenNames(""); 511 assertNotNull(children); 512 assertEquals(2, children.size()); 513 assertTrue(children.contains("1")); 514 assertTrue(children.contains("a")); 515 } 516 catch (Exception e) 517 { 518 fail(e.toString()); 519 } 520 } 521 522 public void testGetChildren4() 523 { 524 try 525 { 526 if (!cache.exists("/a/b/c")) 527 { 528 cache.put("/a/b/c", null); 529 } 530 Set children = cache.getChildrenNames((Fqn) null); 531 assertTrue(children.isEmpty()); 532 } 533 catch (Exception e) 534 { 535 fail(e.toString()); 536 } 537 } 538 539 540 public void testGetChildren5() 541 { 542 try 543 { 544 cache.put("/a/1", null); 545 cache.put("/a/2", null); 546 cache.put("/a/3", null); 547 System.out.println("cache is " + cache.printLockInfo()); 548 549 Node n = cache.get("/a"); 550 assertNotNull(n); 551 552 Set children = cache.getChildrenNames("/a"); 553 assertNotNull(children); 554 assertEquals(3, children.size()); 555 } 556 catch (Exception e) 557 { 558 fail(e.toString()); 559 } 560 } 561 562 563 public void testGetChildren6() 564 { 565 try 566 { 567 cache.put("/a/1", null); 568 cache.put("/a/2", null); 569 cache.put("/a/3", null); 570 System.out.println("cache is " + cache.printLockInfo()); 571 cache.evict(Fqn.fromString("/a/1")); 572 cache.evict(Fqn.fromString("/a/2")); 573 cache.evict(Fqn.fromString("/a/3")); 574 cache.evict(Fqn.fromString("/a")); 575 System.out.println("cache is " + cache.printLockInfo()); 576 addDelay(); 577 assertNotNull(cache.get("/a")); 578 579 Set children = cache.getChildrenNames("/a"); 580 assertNotNull("No children were loaded", children); 581 System.out.println("children: " + children); 582 assertEquals("3 children weren't loaded", 3, children.size()); 583 } 584 catch (Exception e) 585 { 586 fail(e.toString()); 587 } 588 } 589 590 public void testGetChildren7() 591 { 592 try 593 { 594 cache.put("/a/1", null); 595 cache.put("/a/2", null); 596 cache.put("/a/3", null); 597 cache.put("/a", "test", "test"); 598 System.out.println("cache is " + cache.printLockInfo()); 599 cache.evict(Fqn.fromString("/a/1")); 600 cache.evict(Fqn.fromString("/a/2")); 601 cache.evict(Fqn.fromString("/a/3")); 602 cache.evict(Fqn.fromString("/a")); 603 System.out.println("cache is " + cache.printLockInfo()); 604 addDelay(); 605 Object val = cache.get("/a", "test"); 606 assertEquals("attributes weren't loaded", "test", val); 607 608 Set children = cache.getChildrenNames("/a"); 609 assertNotNull("No children were loaded", children); 610 System.out.println("children: " + children); 611 assertEquals("3 children weren't loaded", 3, children.size()); 612 } 613 catch (Exception e) 614 { 615 fail(e.toString()); 616 } 617 } 618 619 public void testGetChildren8() 620 { 621 cache.put("/a/1", null); 622 cache.put("/a/2", null); 623 cache.put("/a/3", null); 624 System.out.println("cache is " + cache.printLockInfo()); 625 cache.evict(Fqn.fromString("/a/1")); 626 cache.evict(Fqn.fromString("/a/2")); 627 cache.evict(Fqn.fromString("/a/3")); 628 cache.evict(Fqn.fromString("/a")); 629 System.out.println("cache is " + cache.printLockInfo()); 630 addDelay(); 631 assertNull(cache.get("/a", "test")); 632 633 cache.get("/a/1"); 634 Set children = cache.getChildrenNames("/a"); 635 assertNotNull("No children were loaded", children); 636 System.out.println("children: " + children); 637 assertEquals("3 children weren't loaded", 3, children.size()); 638 } 639 640 public void testGetChildren9() 641 { 642 try 643 { 644 cache.put("/a/1", null); 645 cache.put("/a/2", null); 646 cache.put("/a/3", null); 647 System.out.println("cache is " + cache.printLockInfo()); 648 cache.evict(Fqn.fromString("/a/1")); 649 cache.evict(Fqn.fromString("/a/2")); 650 cache.evict(Fqn.fromString("/a/3")); 651 cache.evict(Fqn.fromString("/a")); 652 System.out.println("cache is " + cache.printLockInfo()); 653 addDelay(); 654 assertNull(cache.get("/a", "test")); 655 656 cache.get("/a/1"); 657 Set children = cache.getChildrenNames("/a"); 658 assertNotNull("No children were loaded", children); 659 System.out.println("children: " + children); 660 assertEquals("3 children weren't loaded", 3, children.size()); 661 662 cache.evict(Fqn.fromString("/a/1")); 663 cache.evict(Fqn.fromString("/a/2")); 664 cache.evict(Fqn.fromString("/a/3")); 665 cache.evict(Fqn.fromString("/a")); 666 System.out.println("cache is " + cache.printLockInfo()); 667 668 assertNull(cache.get("/a", "test")); 669 670 cache.get("/a/1"); 671 children = cache.getChildrenNames("/a"); 672 assertNotNull("No children were loaded", children); 673 System.out.println("children: " + children); 674 assertEquals("3 children weren't loaded", 3, children.size()); 675 } 676 catch (Exception e) 677 { 678 fail(e.toString()); 679 } 680 } 681 682 683 public void testGetChildren10() 684 { 685 try 686 { 687 cache.put("/a/1", null); 688 cache.put("/a/2", null); 689 cache.put("/a/3", null); 690 System.out.println("cache is " + cache.printLockInfo()); 691 cache.evict(Fqn.fromString("/a/1")); 692 cache.evict(Fqn.fromString("/a/2")); 693 cache.evict(Fqn.fromString("/a/3")); 694 cache.evict(Fqn.fromString("/a")); 695 System.out.println("cache is " + cache.printLockInfo()); 696 addDelay(); 697 assertNull(cache.get("/a", "test")); 698 699 cache.get("/a/1"); 700 Set children = cache.getChildrenNames("/a"); 701 assertNotNull("No children were loaded", children); 702 System.out.println("children: " + children); 703 assertEquals("3 children weren't loaded", 3, children.size()); 704 705 children = cache.getChildrenNames("/a"); 706 assertNotNull("No children were loaded", children); 707 System.out.println("children: " + children); 708 assertEquals("3 children weren't loaded", 3, children.size()); 709 } 710 catch (Exception e) 711 { 712 fail(e.toString()); 713 } 714 } 715 716 717 public void testGetChildren11() 718 { 719 Set children; 720 try 721 { 722 cache.put("/a/b", "key", "val"); 723 cache.put("/a/b/1", "key", "val"); 724 cache.put("/a/b/2", "key", "val"); 725 cache.put("/a/b/3", "key", "val"); 726 cache.put("/a/b/1/tmp", "key", "val"); 727 cache.put("/a/b/2/tmp", "key", "val"); 728 cache.put("/a/b/3/tmp", "key", "val"); 729 730 cache.evict(Fqn.fromString("/a")); 731 cache.evict(Fqn.fromString("/a/b")); 732 cache.evict(Fqn.fromString("/a/b/1")); 733 cache.evict(Fqn.fromString("/a/b/2")); 734 cache.evict(Fqn.fromString("/a/b/3")); 735 736 children = cache.getChildrenNames("/a/b"); 738 assertEquals(3, children.size()); 739 740 cache.evict(Fqn.fromString("/a/b")); 741 cache.evict(Fqn.fromString(("/a/b/1/tmp"))); 742 cache.evict(Fqn.fromString(("/a/b/2/tmp"))); 743 cache.evict(Fqn.fromString(("/a/b/3/tmp"))); 744 cache.evict(Fqn.fromString(("/a/b/1"))); 745 cache.evict(Fqn.fromString(("/a/b/2"))); 746 cache.evict(Fqn.fromString(("/a/b/3"))); 747 cache.evict(Fqn.fromString("/a")); 748 749 children = cache.getChildrenNames("/a/b"); 750 assertEquals(3, children.size()); 751 } 752 catch (Exception e) 753 { 754 fail(e.toString()); 755 } 756 } 757 758 759 public void testGetChildren12() 760 { 761 Set children; 762 try 763 { 764 cache.put("/a/b", "key", "val"); 765 cache.put("/a/b/1", "key", "val"); 766 cache.put("/a/b/2", "key", "val"); 767 cache.put("/a/b/3", "key", "val"); 768 children = cache.getChildrenNames("/a/b"); 769 assertEquals(3, children.size()); 770 771 cache.evict(Fqn.fromString("/a/b/3")); 772 cache.evict(Fqn.fromString("/a/b/2")); 773 cache.evict(Fqn.fromString("/a/b")); 775 cache.evict(Fqn.fromString("/a")); 776 777 cache.getChildrenNames("/a/b"); 779 children = cache.getChildrenNames("/a/b"); 780 assertEquals(3, children.size()); 781 782 cache.evict(Fqn.fromString("/a/b/3")); 783 cache.evict(Fqn.fromString("/a/b/2")); 784 cache.evict(Fqn.fromString("/a/b")); 786 cache.evict(Fqn.fromString("/a")); 787 children = cache.getChildrenNames("/a/b"); 788 assertEquals(3, children.size()); 789 } 790 catch (Exception e) 791 { 792 fail(e.toString()); 793 } 794 } 795 796 public void testLoaderGetChildrenNames() throws Exception 797 { 798 Fqn f = Fqn.fromString("/a"); 799 cache.put(f, "k", "v"); 800 assertEquals("v", loader.get(f).get("k")); 801 assertNull(loader.getChildrenNames(f)); 802 } 803 804 805 public void testRemoveData() 806 { 807 String key = "/x/y/z/"; 808 cache.put(key, "keyA", "valA"); 809 cache.put(key, "keyB", "valB"); 810 cache.put(key, "keyC", "valC"); 811 assertEquals(3, cache.getKeys(key).size()); 812 cache.removeData(key); 813 Set keys = cache.getKeys(key); 814 assertEquals(0, keys.size()); 815 cache.remove("/x"); 816 Object val = cache.get(key, "keyA"); 817 assertNull(val); 818 } 819 820 821 public void testRemoveData2() 822 { 823 Set keys; 824 Fqn key = Fqn.fromString("/x/y/z/"); 825 cache.put(key, "keyA", "valA"); 826 cache.put(key, "keyB", "valB"); 827 cache.put(key, "keyC", "valC"); 828 addDelay(); 829 keys = cache.getKeys(key); 830 assertEquals(3, keys.size()); 831 cache.removeData(key); 832 cache.evict(key); 833 addDelay(); 834 keys = cache.getKeys(key); 835 assertNotNull(keys); 836 assertEquals(0, keys.size()); 837 } 838 839 public void testRemoveData3() 840 { 841 Set keys; 842 Fqn key = Fqn.fromString("/x/y/z/"); 843 cache.put(key, "keyA", "valA"); 844 cache.put(key, "keyB", "valB"); 845 cache.put(key, "keyC", "valC"); 846 keys = cache.getKeys(key); 847 assertEquals(3, keys.size()); 848 cache.evict(key); 849 cache.removeData(key); 850 keys = cache.getKeys(key); 851 assertEquals("no more keys", 0, keys.size()); 852 } 853 854 public void testRemoveKey() 855 { 856 String key = "/x/y/z/"; 857 cache.put(key, "keyA", "valA"); 858 assertEquals(1, cache.getKeys(key).size()); 859 cache.put(key, "keyB", "valB"); 860 assertEquals(2, cache.getKeys(key).size()); 861 cache.put(key, "keyC", "valC"); 862 assertEquals(3, cache.getKeys(key).size()); 863 cache.remove(key, "keyA"); 864 assertEquals(2, cache.getKeys(key).size()); 865 cache.remove("/x"); 866 } 867 868 869 public void testRemoveKey2() throws CacheException 870 { 871 final String NODE = "/test"; 872 final String KEY = "key"; 873 Object retval; 874 cache.remove(NODE); 875 retval = cache.put(NODE, KEY, 10); 876 assertNull(retval); 877 addDelay(); 878 retval = cache.remove(NODE, KEY); 879 assertEquals(10, retval); 880 addDelay(); 881 retval = cache.remove(NODE, KEY); 882 assertNull(retval); 883 } 884 885 public void testRemoveKey3() throws CacheException 886 { 887 final String NODE = "/test"; 888 final String KEY = "key"; 889 Object retval; 890 cache.remove(NODE); 891 retval = cache.put(NODE, KEY, 10); 892 assertNull(retval); 893 894 cache.evict(Fqn.fromString(NODE)); addDelay(); 896 retval = cache.remove(NODE, KEY); 897 assertEquals(10, retval); 898 899 cache.evict(Fqn.fromString(NODE)); addDelay(); 901 retval = cache.remove(NODE, KEY); 902 assertNull(retval); 903 } 904 905 906 public void testRemove() 907 { 908 String key = "/x/y/z/"; 909 cache.put(key, "keyA", "valA"); 910 cache.put(key, "keyB", "valB"); 911 cache.put(key, "keyC", "valC"); 912 cache.remove("/x"); 913 assertNull(cache.get(key, "keyA")); 914 addDelay(); 915 Set keys = cache.getKeys(key); 916 assertNull("got keys " + keys, keys); 917 cache.remove("/x"); 918 } 919 920 921 public void testRemoveRoot() 922 { 923 assertEquals(0, cache.getKeys("/").size()); 924 cache.put("/1/2/3/4/5", null); 925 cache.put("uno/due/tre", null); 926 cache.put("1/2/3/a", null); 927 cache.put("/eins/zwei/drei", null); 928 cache.put("/one/two/three", null); 929 cache.remove("/"); 930 assertEquals(0, cache.getKeys("/").size()); 931 } 932 933 934 public void testEvictionWithCacheLoader() 935 { 936 cache.put("/first/second", "key1", "val1"); cache.put("/first/second/third", "key2", "val2"); cache.evict(Fqn.fromString("/first/second")); addDelay(); 940 assertTrue(cache.exists("/first/second/third")); 941 assertTrue(cache.exists("/first/second")); 942 assertTrue(cache.exists("/first")); 943 String val = (String ) cache.get("/first/second", "key1"); assertEquals("val1", val); 945 assertTrue(cache.exists("/first/second/third")); 946 assertTrue(cache.exists("/first/second")); 947 assertTrue(cache.exists("/first")); 948 } 949 950 951 public void testEvictionWithCacheLoader2() 952 { 953 cache.put("/first/second/third", "key1", "val1"); cache.evict(Fqn.fromString("/first/second/third")); addDelay(); 956 assertFalse(cache.exists("/first/second/third")); 957 assertTrue(cache.exists("/first/second")); 958 assertTrue(cache.exists("/first")); 959 String val = (String ) cache.get("/first/second/third", "key1"); assertEquals("val1", val); 961 assertTrue(cache.exists("/first/second/third")); 962 assertTrue(cache.exists("/first/second")); 963 assertTrue(cache.exists("/first")); 964 } 965 966 967 public void testEvictionWithGetChildrenNames() throws Exception 968 { 969 cache.put("/a/1", null); 970 cache.put("/a/2", null); 971 cache.put("/a/3", null); 972 cache.evict(Fqn.fromString("/a/1")); 974 cache.evict(Fqn.fromString("/a/2")); 975 cache.evict(Fqn.fromString("/a/3")); 976 cache.evict(Fqn.fromString("/a")); 977 addDelay(); 978 979 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 980 981 mgr.begin(); 982 tx = mgr.getTransaction(); 983 Set children = cache.getChildrenNames("/a"); 984 985 System.out.println("**** " + cache.getTransactionManager()); 986 987 assertEquals(3, children.size()); 988 assertTrue(children.contains("1")); 989 assertTrue(children.contains("2")); 990 assertTrue(children.contains("3")); 991 992 System.out.println("lock info " + cache.printLockInfo()); 993 994 assertEquals(5, cache.getNumberOfLocksHeld()); 995 tx.commit(); 996 997 } 998 999 1000 public void testTxPutCommit() throws Exception 1001 { 1002 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 1003 mgr.begin(); 1004 tx = mgr.getTransaction(); 1005 1006 cache.put("/one/two/three", "key1", "val1"); 1007 cache.put("/one/two/three/four", "key2", "val2"); 1008 tx.commit(); 1009 assertNotNull("Cache has node /one/two/three", cache.getKeys("/one/two/three")); 1010 assertNotNull("Loader has node /one/two/three", loader.get(Fqn.fromString("/one/two/three"))); 1011 Set children = cache.getChildrenNames("/one"); 1012 assertEquals("Cache has correct number of children", 1, children.size()); 1013 children = loader.getChildrenNames(Fqn.fromString("/one")); 1014 assertEquals("Loader has correct number of children", 1, children.size()); 1015 cache.remove("/"); 1016 } 1017 1018 public void testTxPutRollback() throws Exception 1019 { 1020 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 1021 1022 cache.remove("/one"); 1023 addDelay(); 1024 mgr.begin(); 1025 tx = mgr.getTransaction(); 1026 1027 cache.put("/one/two/three", "key1", "val1"); 1028 cache.put("/one/two/three/four", "key2", "val2"); 1029 log.debug("NODE1 " + cache.get("/one/two/three").getData()); 1030 tx.rollback(); 1031 log.debug("NODE2 " + cache.get("/one/two/three")); 1032 assertEquals(null, cache.get("/one/two/three", "key1")); 1033 assertEquals(null, cache.get("/one/two/three/four", "key2")); 1034 addDelay(); 1035 assertNull("Loader does not have node /one/two/three", loader.get(Fqn.fromString("/one/two/three"))); 1036 assertEquals("Cache does not have node /one/two/three", null, cache.getKeys("/one/two/three")); 1037 Set children = cache.getChildrenNames("/one"); 1038 assertEquals("Cache has no children under /one", 0, children.size()); 1039 children = loader.getChildrenNames(Fqn.fromString("/one")); 1040 assertEquals("Loader has no children under /one", null, children); 1041 } 1042 1043 1044 1047 public void testBasicOperations() 1048 throws Exception 1049 { 1050 1051 doTestBasicOperations(); 1052 } 1053 1054 1057 public void testBasicOperationsTransactional() 1058 throws Exception 1059 { 1060 1061 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 1062 mgr.begin(); 1063 tx = mgr.getTransaction(); 1064 doTestBasicOperations(); 1065 tx.commit(); 1066 } 1067 1068 1071 private void doTestBasicOperations() throws Exception 1072 { 1073 1074 1075 doPutTests(new Fqn("key")); 1076 doRemoveTests(new Fqn("key")); 1077 1079 1080 doPutTests(new Fqn("key1")); 1081 doPutTests(new Fqn("key3")); 1082 doPutTests(new Fqn("key2")); 1083 assertEquals(4, loader.get(new Fqn("key1")).size()); 1084 assertEquals(4, loader.get(new Fqn("key2")).size()); 1085 assertEquals(4, loader.get(new Fqn("key3")).size()); 1086 1087 1088 doRemoveTests(new Fqn("key2")); 1089 doRemoveTests(new Fqn("key3")); 1090 doRemoveTests(new Fqn("key1")); 1091 assertNull(loader.get(new Fqn("key1"))); 1092 assertNull(loader.get(new Fqn("key2"))); 1093 assertNull(loader.get(new Fqn("key3"))); 1094 } 1096 1097 1100 private void doPutTests(Fqn fqn) 1101 throws Exception 1102 { 1103 1104 assertTrue(!loader.exists(fqn)); 1105 1106 1107 Object oldVal; 1108 oldVal = loader.put(fqn, "one", "two"); 1109 assertNull(oldVal); 1110 addDelay(); 1111 oldVal = loader.put(fqn, "three", "four"); 1112 assertNull(oldVal); 1113 addDelay(); 1114 assertEquals("two", loader.get(fqn).get("one")); 1115 assertEquals("four", loader.get(fqn).get("three")); 1116 addDelay(); 1117 oldVal = loader.put(fqn, "one", "xxx"); 1118 assertEquals("two", oldVal); 1119 addDelay(); 1120 oldVal = loader.put(fqn, "one", "two"); 1121 assertEquals("xxx", oldVal); 1122 1123 1124 addDelay(); 1125 Map map = new HashMap (loader.get(fqn)); 1126 assertEquals(2, map.size()); 1127 assertEquals("two", map.get("one")); 1128 assertEquals("four", map.get("three")); 1129 1130 1131 map.put("five", "six"); 1132 map.put("seven", "eight"); 1133 loader.put(fqn, map); 1134 addDelay(); 1135 assertEquals("six", loader.get(fqn).get("five")); 1136 assertEquals("eight", loader.get(fqn).get("seven")); 1137 assertEquals(map, loader.get(fqn)); 1138 assertEquals(4, map.size()); 1139 1140 assertTrue(loader.exists(fqn)); 1141 } 1142 1143 1146 private void doRemoveTests(Fqn fqn) 1147 throws Exception 1148 { 1149 1150 1151 Object oldVal; 1152 oldVal = loader.remove(fqn, "one"); 1153 assertEquals("two", oldVal); 1154 addDelay(); 1155 oldVal = loader.remove(fqn, "five"); 1156 assertEquals("six", oldVal); 1157 addDelay(); 1158 assertNull(loader.get(fqn).get("one")); 1159 assertNull(loader.get(fqn).get("five")); 1160 assertEquals("four", loader.get(fqn).get("three")); 1161 assertEquals("eight", loader.get(fqn).get("seven")); 1162 Map map = loader.get(fqn); 1163 assertEquals(2, map.size()); 1164 assertEquals("four", map.get("three")); 1165 assertEquals("eight", map.get("seven")); 1166 1167 1168 assertTrue(loader.exists(fqn)); 1169 loader.remove(fqn); 1170 addDelay(); 1171 map = loader.get(fqn); 1172 assertNull("Should be null", map); 1173 assertTrue(!loader.exists(fqn)); 1174 } 1175 1176 1180 public void testMultiLevelTree() 1181 throws Exception 1182 { 1183 1184 1185 assertTrue(!loader.exists(new Fqn("key0"))); 1186 loader.put(Fqn.fromString("/key0/level1/level2"), null); 1187 addDelay(); 1188 assertTrue(loader.exists(Fqn.fromString("/key0/level1/level2"))); 1189 assertTrue(loader.exists(Fqn.fromString("/key0/level1"))); 1190 assertTrue(loader.exists(new Fqn("key0"))); 1191 1192 1193 loader.put(Fqn.fromString("/key0/x/y"), null); 1194 addDelay(); 1195 assertTrue(loader.exists(Fqn.fromString("/key0/x/y"))); 1196 assertTrue(loader.exists(Fqn.fromString("/key0/x"))); 1197 loader.remove(Fqn.fromString("/key0/x/y")); 1198 addDelay(); 1199 assertTrue(!loader.exists(Fqn.fromString("/key0/x/y"))); 1200 assertTrue(loader.exists(Fqn.fromString("/key0/x"))); 1201 1202 1203 loader.remove(new Fqn("key0")); 1204 addDelay(); 1205 assertTrue(!loader.exists(new Fqn("key0"))); 1206 assertTrue(!loader.exists(Fqn.fromString("/key0/level1/level2"))); 1207 assertTrue(!loader.exists(Fqn.fromString("/key0/level1"))); 1208 assertTrue(!loader.exists(Fqn.fromString("/key0/x"))); 1209 1210 1211 loader.put(new Fqn("key1"), null); 1212 loader.put(new Fqn("key2"), null); 1213 loader.put(new Fqn("key3"), null); 1214 addDelay(); 1215 assertTrue(loader.exists(new Fqn("key1"))); 1216 assertTrue(loader.exists(new Fqn("key2"))); 1217 assertTrue(loader.exists(new Fqn("key3"))); 1218 1219 1220 assertTrue(!loader.exists(Fqn.fromString("/key3/level1"))); 1221 assertTrue(!loader.exists(Fqn.fromString("/key3/level1/level2"))); 1222 loader.put(Fqn.fromString("/key3/level1/level2"), null); 1223 addDelay(); 1224 assertTrue(loader.exists(Fqn.fromString("/key3/level1/level2"))); 1225 assertTrue(loader.exists(Fqn.fromString("/key3/level1"))); 1226 1227 1228 assertTrue(loader.exists(new Fqn("key1"))); 1229 assertTrue(loader.exists(new Fqn("key2"))); 1230 assertTrue(loader.exists(new Fqn("key3"))); 1231 1232 1233 loader.remove(Fqn.fromString("/key3/level1")); 1234 addDelay(); 1235 assertTrue(!loader.exists(Fqn.fromString("/key3/level1/level2"))); 1236 assertTrue(!loader.exists(Fqn.fromString("/key3/level1"))); 1237 1238 1239 assertTrue(loader.exists(new Fqn("key1"))); 1240 assertTrue(loader.exists(new Fqn("key2"))); 1241 assertTrue(loader.exists(new Fqn("key3"))); 1242 1243 1244 loader.remove(new Fqn("key1")); 1245 addDelay(); 1246 assertTrue(!loader.exists(new Fqn("key1"))); 1247 assertTrue(loader.exists(new Fqn("key2"))); 1248 assertTrue(loader.exists(new Fqn("key3"))); 1249 1250 1251 loader.remove(new Fqn("key3")); 1252 addDelay(); 1253 assertTrue(loader.exists(new Fqn("key2"))); 1254 assertTrue(!loader.exists(new Fqn("key3"))); 1255 1256 1257 loader.remove(new Fqn("key2")); 1258 addDelay(); 1259 assertTrue(!loader.exists(new Fqn("key0"))); 1260 assertTrue(!loader.exists(new Fqn("key1"))); 1261 assertTrue(!loader.exists(new Fqn("key2"))); 1262 assertTrue(!loader.exists(new Fqn("key3"))); 1263 1264 1265 1266 assertNull(loader.get(new Fqn("key0"))); 1267 loader.put(Fqn.fromString("/key0/level1/level2"), "a", "b"); 1268 addDelay(); 1269 assertNotNull(loader.get(Fqn.fromString("/key0/level1/level2"))); 1270 assertNotNull(loader.get(Fqn.fromString("/key0/level1"))); 1271 assertEquals(0, loader.get(Fqn.fromString("/key0/level1")).size()); 1272 assertEquals(0, loader.get(new Fqn("key0")).size()); 1273 1274 loader.put(Fqn.fromString("/key0/x/y"), "a", "b"); 1275 addDelay(); 1276 assertNotNull(loader.get(Fqn.fromString("/key0/x/y"))); 1277 assertNotNull(loader.get(Fqn.fromString("/key0/x"))); 1278 assertEquals(0, loader.get(Fqn.fromString("/key0/x")).size()); 1279 loader.remove(Fqn.fromString("/key0/x/y")); 1280 addDelay(); 1281 assertNull(loader.get(Fqn.fromString("/key0/x/y"))); 1282 assertNotNull(loader.get(Fqn.fromString("/key0/x"))); 1283 assertEquals(0, loader.get(Fqn.fromString("/key0/x")).size()); 1284 1285 loader.remove(new Fqn("key0")); 1286 addDelay(); 1287 assertNull(loader.get(new Fqn("key0"))); 1288 assertNull(loader.get(Fqn.fromString("/key0/level1/level2"))); 1289 assertNull(loader.get(Fqn.fromString("/key0/level1"))); 1290 assertNull(loader.get(Fqn.fromString("/key0/x"))); 1291 1292 loader.put(new Fqn("key1"), "a", "b"); 1293 loader.put(new Fqn("key2"), "a", "b"); 1294 loader.put(new Fqn("key3"), "a", "b"); 1295 addDelay(); 1296 assertNotNull(loader.get(new Fqn("key1"))); 1297 assertNotNull(loader.get(new Fqn("key2"))); 1298 assertNotNull(loader.get(new Fqn("key3"))); 1299 1300 assertNull(loader.get(Fqn.fromString("/key3/level1"))); 1301 assertNull(loader.get(Fqn.fromString("/key3/level1/level2"))); 1302 loader.put(Fqn.fromString("/key3/level1/level2"), "a", "b"); 1303 addDelay(); 1304 assertNotNull(loader.get(Fqn.fromString("/key3/level1/level2"))); 1305 assertNotNull(loader.get(Fqn.fromString("/key3/level1"))); 1306 assertEquals(0, loader.get(Fqn.fromString("/key3/level1")).size()); 1307 1308 assertNotNull(loader.get(new Fqn("key1"))); 1309 assertNotNull(loader.get(new Fqn("key2"))); 1310 assertNotNull(loader.get(new Fqn("key3"))); 1311 1312 loader.remove(Fqn.fromString("/key3/level1")); 1313 addDelay(); 1314 assertNull(loader.get(Fqn.fromString("/key3/level1/level2"))); 1315 assertNull(loader.get(Fqn.fromString("/key3/level1"))); 1316 1317 assertNotNull(loader.get(new Fqn("key1"))); 1318 assertNotNull(loader.get(new Fqn("key2"))); 1319 assertNotNull(loader.get(new Fqn("key3"))); 1320 1321 loader.remove(new Fqn("key1")); 1322 addDelay(); 1323 assertNull(loader.get(new Fqn("key1"))); 1324 assertNotNull(loader.get(new Fqn("key2"))); 1325 assertNotNull(loader.get(new Fqn("key3"))); 1326 1327 loader.remove(new Fqn("key3")); 1328 addDelay(); 1329 assertNotNull(loader.get(new Fqn("key2"))); 1330 assertNull(loader.get(new Fqn("key3"))); 1331 1332 loader.remove(new Fqn("key2")); 1333 addDelay(); 1334 assertNull(loader.get(new Fqn("key0"))); 1335 assertNull(loader.get(new Fqn("key1"))); 1336 assertNull(loader.get(new Fqn("key2"))); 1337 assertNull(loader.get(new Fqn("key3"))); 1338 } 1339 1340 1343 public void testGetChildrenNames() 1344 throws Exception 1345 { 1346 1347 checkChildren(new Fqn(), null); 1348 checkChildren(Fqn.fromString("/key0"), null); 1349 1350 loader.put(Fqn.fromString("/key0"), null); 1351 addDelay(); 1352 checkChildren(new Fqn(), new String []{"key0"}); 1353 1354 loader.put(Fqn.fromString("/key1/x"), null); 1355 addDelay(); 1356 checkChildren(new Fqn(), new String []{"key0", "key1"}); 1357 checkChildren(Fqn.fromString("/key1"), new String []{"x"}); 1358 1359 loader.remove(Fqn.fromString("/key1/x")); 1360 addDelay(); 1361 checkChildren(new Fqn(), new String []{"key0", "key1"}); 1362 checkChildren(Fqn.fromString("/key0"), null); 1363 checkChildren(Fqn.fromString("/key1"), null); 1364 1365 loader.put(Fqn.fromString("/key0/a"), null); 1366 loader.put(Fqn.fromString("/key0/ab"), null); 1367 loader.put(Fqn.fromString("/key0/abc"), null); 1368 addDelay(); 1369 checkChildren(Fqn.fromString("/key0"), 1370 new String []{"a", "ab", "abc"}); 1371 1372 loader.put(Fqn.fromString("/key0/xxx"), null); 1373 loader.put(Fqn.fromString("/key0/xx"), null); 1374 loader.put(Fqn.fromString("/key0/x"), null); 1375 addDelay(); 1376 checkChildren(Fqn.fromString("/key0"), 1377 new String []{"a", "ab", "abc", "x", "xx", "xxx"}); 1378 1379 loader.put(Fqn.fromString("/key0/a/1"), null); 1380 loader.put(Fqn.fromString("/key0/a/2"), null); 1381 loader.put(Fqn.fromString("/key0/a/2/1"), null); 1382 addDelay(); 1383 checkChildren(Fqn.fromString("/key0/a/2"), new String []{"1"}); 1384 checkChildren(Fqn.fromString("/key0/a"), new String []{"1", "2"}); 1385 checkChildren(Fqn.fromString("/key0"), 1386 new String []{"a", "ab", "abc", "x", "xx", "xxx"}); 1387 } 1408 1409 1412 private void checkChildren(Fqn fqn, String [] names) 1413 throws Exception 1414 { 1415 1416 Set set = loader.getChildrenNames(fqn); 1417 if (names != null) 1418 { 1419 assertEquals(names.length, set.size()); 1420 for (int i = 0; i < names.length; i += 1) 1421 { 1422 assertTrue(set.contains(names[i])); 1423 } 1424 } 1425 else 1426 { 1427 assertNull(set); 1428 } 1429 } 1430 1431 1434 public void testModifications() 1435 throws Exception 1436 { 1437 1438 doTestModifications(); 1439 } 1440 1441 1444 public void testModificationsTransactional() 1445 throws Exception 1446 { 1447 1448 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 1449 mgr.begin(); 1450 tx = mgr.getTransaction(); 1451 doTestModifications(); 1452 tx.commit(); 1453 } 1454 1455 1458 private void doTestModifications() 1459 throws Exception 1460 { 1461 1462 1463 List list = createUpdates(); 1464 loader.put(list); 1465 addDelay(); 1466 checkModifications(list); 1467 1468 1469 list = new ArrayList (); 1470 Modification mod = new Modification(); 1471 mod.setType(Modification.ModificationType.REMOVE_KEY_VALUE); 1472 mod.setFqn(FQN); 1473 mod.setKey("one"); 1474 list.add(mod); 1475 loader.put(list); 1476 addDelay(); 1477 checkModifications(list); 1478 1479 1480 list = new ArrayList (); 1481 mod = new Modification(); 1482 mod.setType(Modification.ModificationType.REMOVE_NODE); 1483 mod.setFqn(FQN); 1484 list.add(mod); 1485 loader.put(list); 1486 addDelay(); 1487 checkModifications(list); 1488 assertNull(loader.get(FQN)); 1489 1490 1491 loader.put(FQN, "one", "two"); 1492 list = new ArrayList (); 1493 mod = new Modification(); 1494 mod.setType(Modification.ModificationType.REMOVE_DATA); 1495 mod.setFqn(FQN); 1496 list.add(mod); 1497 loader.put(list); 1498 addDelay(); 1499 checkModifications(list); 1500 } 1501 1502 1505 public void testOnePhaseTransaction() 1506 throws Exception 1507 { 1508 List mods = createUpdates(); 1509 loader.prepare(null, mods, true); 1510 checkModifications(mods); 1511 } 1512 1513 1516 public void testTwoPhaseTransaction() 1517 throws Exception 1518 { 1519 1520 Object txnKey = new Object (); 1521 List mods = createUpdates(); 1522 loader.prepare(txnKey, mods, false); 1523 loader.commit(txnKey); 1524 addDelay(); 1525 checkModifications(mods); 1526 } 1527 1528 1531 public void testTransactionRollback() throws Exception 1532 { 1533 loader.remove(Fqn.fromString("/")); 1534 int num; 1535 try 1536 { 1537 ByteArrayOutputStream baos = new ByteArrayOutputStream (1024); 1538 MarshalledValueOutputStream os = new MarshalledValueOutputStream(baos); 1539 loader.loadEntireState(os); 1540 num = baos.size(); 1541 } 1542 catch (UnsupportedOperationException ex) 1543 { 1544 log.info("caught unsupported operation exception that's okay: ", ex); 1545 return; 1546 } 1547 1548 Object txnKey = new Object (); 1549 List mods = createUpdates(); 1550 loader.prepare(txnKey, mods, false); 1551 loader.rollback(txnKey); 1552 ByteArrayOutputStream baos = new ByteArrayOutputStream (1024); 1553 MarshalledValueOutputStream os = new MarshalledValueOutputStream(baos); 1554 loader.loadEntireState(os); 1555 assertEquals(num, baos.size()); 1556 } 1557 1558 1561 public void testIntegratedTransactionRollback() throws Exception 1562 { 1563 loader.remove(Fqn.fromString("/")); 1564 int num = 0; 1565 try 1566 { 1567 ByteArrayOutputStream baos = new ByteArrayOutputStream (1024); 1568 MarshalledValueOutputStream os = new MarshalledValueOutputStream(baos); 1569 loader.loadEntireState(os); 1570 num = baos.size(); 1571 } 1572 catch (UnsupportedOperationException ex) 1573 { 1574 System.out.println("caught unsupported operation exception that's okay: " + ex); 1575 return; 1576 } 1577 1578 Object txnKey = new Object (); 1579 List mods = createUpdates(); 1580 loader.prepare(txnKey, mods, false); 1581 loader.rollback(txnKey); 1582 ByteArrayOutputStream baos = new ByteArrayOutputStream (1024); 1583 MarshalledValueOutputStream os = new MarshalledValueOutputStream(baos); 1584 loader.loadEntireState(os); 1585 assertEquals(num, baos.size()); 1586 } 1587 1588 1591 private List createUpdates() 1592 { 1593 1594 List list = new ArrayList (); 1595 1596 Modification mod = new Modification(); 1597 mod.setType(Modification.ModificationType.PUT_KEY_VALUE); 1598 mod.setFqn(FQN); 1599 mod.setKey("one"); 1600 mod.setValue("two"); 1601 list.add(mod); 1602 1603 mod = new Modification(); 1604 mod.setType(Modification.ModificationType.PUT_KEY_VALUE); 1605 mod.setFqn(FQN); 1606 mod.setKey("three"); 1607 mod.setValue("four"); 1608 list.add(mod); 1609 1610 Map map = new HashMap (); 1611 map.put("five", "six"); 1612 map.put("seven", "eight"); 1613 mod = new Modification(); 1614 mod.setType(Modification.ModificationType.PUT_DATA); 1615 mod.setFqn(FQN); 1616 mod.setData(map); 1617 list.add(mod); 1618 1619 return list; 1620 } 1621 1622 1625 private void checkModifications(List list) 1626 throws Exception 1627 { 1628 1629 for (int i = 0; i < list.size(); i += 1) 1630 { 1631 Modification mod = (Modification) list.get(i); 1632 Fqn fqn = mod.getFqn(); 1633 switch (mod.getType()) 1634 { 1635 case PUT_KEY_VALUE: 1636 assertEquals(mod.getValue(), loader.get(fqn).get(mod.getKey())); 1637 break; 1638 case PUT_DATA: 1639 Map map = mod.getData(); 1640 for (Object key : map.keySet()) 1641 { 1642 assertEquals(map.get(key), loader.get(fqn).get(key)); 1643 } 1644 break; 1645 case REMOVE_KEY_VALUE: 1646 assertNull(loader.get(fqn).get(mod.getKey())); 1647 break; 1648 case REMOVE_DATA: 1649 map = loader.get(fqn); 1650 assertNotNull(map); 1651 assertEquals(0, map.size()); 1652 break; 1653 case REMOVE_NODE: 1654 assertNull(loader.get(fqn)); 1655 break; 1656 default: 1657 fail("unknown type: " + mod); 1658 break; 1659 } 1660 } 1661 } 1662 1663 1666 public void testNullKeysAndValues() 1667 throws Exception 1668 { 1669 1670 loader.put(FQN, null, "x"); 1671 addDelay(); 1672 assertEquals("x", loader.get(FQN).get(null)); 1673 Map map = loader.get(FQN); 1674 assertEquals(1, map.size()); 1675 assertEquals("x", map.get(null)); 1676 1677 loader.put(FQN, "y", null); 1678 addDelay(); 1679 assertNull(loader.get(FQN).get("y")); 1680 map = loader.get(FQN); 1681 assertEquals(2, map.size()); 1682 assertEquals("x", map.get(null)); 1683 assertNull(map.get("y")); 1684 1685 loader.remove(FQN, null); 1686 addDelay(); 1687 assertNull(loader.get(FQN).get(null)); 1688 assertEquals(1, loader.get(FQN).size()); 1689 1690 loader.remove(FQN, "y"); 1691 addDelay(); 1692 assertNotNull(loader.get(FQN)); 1693 assertEquals(0, loader.get(FQN).size()); 1694 1695 map = new HashMap (); 1696 map.put(null, null); 1697 loader.put(FQN, map); 1698 addDelay(); 1699 Map m = loader.get(FQN); 1700 m.toString(); 1701 1719 } 1720 1721 1724 public void testDatabaseName() 1725 throws Exception 1726 { 1727 1728 loader.put(FQN, "one", "two"); 1729 addDelay(); 1730 assertEquals("two", loader.get(FQN).get("one")); 1731 } 1732 1733 1736 public void testLoadAndStore() 1737 throws Exception 1738 { 1739 1740 loader.remove(Fqn.fromString("/")); 1742 1743 Complex c1 = new Complex(); 1745 Complex c2 = new Complex(c1); 1746 1747 loader.put(FQN, 1, c1); 1749 loader.put(FQN, 2, c2); 1750 addDelay(); 1751 assertEquals(c1, loader.get(FQN).get(1)); 1752 assertEquals(c2, loader.get(FQN).get(2)); 1753 assertEquals(2, loader.get(FQN).size()); 1754 1755 byte[] state; 1757 ByteArrayOutputStream baos = null; 1758 MarshalledValueOutputStream os = null; 1759 try 1760 { 1761 baos = new ByteArrayOutputStream (1024); 1762 os = new MarshalledValueOutputStream(baos); 1763 loader.loadEntireState(os); 1764 } 1765 catch (UnsupportedOperationException ex) 1766 { 1767 System.out.println("caught unsupported operation exception (this is expected): " + ex); 1768 } 1769 finally 1770 { 1771 os.writeObject(StateTransferManager.STREAMING_DELIMETER_NODE); 1772 os.close(); 1773 assertTrue(baos.size() > 0); 1774 state = baos.toByteArray(); 1775 } 1776 1777 1778 MarshalledValueInputStream is = null; 1779 try 1780 { 1781 ByteArrayInputStream bais = new ByteArrayInputStream (state); 1782 is = new MarshalledValueInputStream(bais); 1783 loader.storeEntireState(is); 1784 } 1785 catch (UnsupportedOperationException ex) 1786 { 1787 System.out.println("caught unsupported operation exception (this is expected): " + ex); 1788 } 1789 finally 1790 { 1791 is.close(); 1792 } 1793 1794 addDelay(); 1795 assertEquals(c1, loader.get(FQN).get(1)); 1796 assertEquals(c2, loader.get(FQN).get(2)); 1797 assertEquals(2, loader.get(FQN).size()); 1798 } 1799 1800 1803 public static class Complex implements Serializable 1804 { 1805 1808 private static final long serialVersionUID = -6810871775584708565L; 1809 1810 Complex nested; 1811 1812 Complex() 1813 { 1814 this(null); 1815 } 1816 1817 Complex(Complex nested) 1818 { 1819 this.nested = nested; 1820 } 1821 1822 public boolean equals(Object o) 1823 { 1824 if (!(o instanceof Complex)) 1825 { 1826 return false; 1827 } 1828 Complex x = (Complex) o; 1829 return (nested != null) ? nested.equals(x.nested) 1830 : (x.nested == null); 1831 } 1832 1833 public int hashCode() 1834 { 1835 if (nested == null) 1836 { 1837 return super.hashCode(); 1838 } 1839 else 1840 { 1841 return 13 + nested.hashCode(); 1842 } 1843 } 1844 } 1845 1846 public void testRemoveInTransactionCommit() throws Exception 1847 { 1848 Fqn fqn = Fqn.fromString("/a/b"); 1849 loader.remove(fqn); 1850 String key = "key"; 1851 String value = "value"; 1852 cache.put(fqn, key, value); 1853 loader = cache.getCacheLoaderManager().getCacheLoader(); 1854 1855 assertEquals(value, cache.get(fqn, key)); 1856 assertEquals(value, loader.get(fqn).get(key)); 1857 1858 cache.getTransactionManager().begin(); 1859 1860 cache.remove(fqn); 1861 1862 cache.get(fqn); cache.getTransactionManager().commit(); 1864 1865 log.debug("expect the cache and the loader to be null here."); 1866 assertEquals(null, cache.get(fqn, key)); 1867 assertEquals(null, loader.get(fqn)); 1868 } 1869 1870 1871 public void testRemoveInTransactionRollback() throws Exception 1872 { 1873 Fqn fqn = Fqn.fromString("/a/b"); 1874 loader.remove(fqn); 1875 String key = "key"; 1876 String value = "value"; 1877 1878 cache.put(fqn, key, value); 1879 loader = cache.getCacheLoaderManager().getCacheLoader(); 1880 1881 assertEquals(value, cache.get(fqn, key)); 1882 assertEquals(value, loader.get(fqn).get(key)); 1883 1884 cache.getTransactionManager().begin(); 1885 1886 cache.remove(fqn); 1887 cache.get(fqn); cache.getTransactionManager().rollback(); 1889 1890 assertEquals(value, cache.get(fqn, key)); 1891 assertEquals(value, loader.get(fqn).get(key)); 1892 } 1893 1894 1895 1898 public void testRemoveAndGetInTransaction() throws Exception 1899 { 1900 Fqn fqn = new Fqn("/a/b"); 1901 String key = "key"; 1902 String value = "value"; 1903 1904 cache.put(fqn, key, value); 1905 CacheLoader loader = cache.getCacheLoaderManager().getCacheLoader(); 1906 1907 assertEquals(value, cache.get(fqn, key)); 1908 assertEquals(value, loader.get(fqn).get(key)); 1909 1910 cache.getTransactionManager().begin(); 1911 1912 cache.remove(fqn); 1913 assertNull("Expecting a null since I have already called a remove() - see JBCACHE-352", cache.get(fqn, key)); 1914 cache.getTransactionManager().rollback(); 1915 1916 assertEquals(value, cache.get(fqn, key)); 1917 assertEquals(value, loader.get(fqn).get(key)); 1918 } 1919 1920 1921 1924 public void testPartialLoadAndStore() 1925 throws Exception 1926 { 1927 1928 1929 loader.remove(Fqn.fromString("/")); 1930 1937 1938 Complex c1 = new Complex(); 1939 Complex c2 = new Complex(c1); 1940 Complex c3 = new Complex(); 1941 Complex c4 = new Complex(c3); 1942 1943 1944 loader.put(FQN, 1, c1); 1945 loader.put(FQN, 2, c2); 1946 loader.put(SUBTREE_FQN, 1, c3); 1947 loader.put(SUBTREE_FQN, 2, c4); 1948 addDelay(); 1949 assertEquals(c1, loader.get(FQN).get(1)); 1950 assertEquals(c2, loader.get(FQN).get(2)); 1951 assertEquals(2, loader.get(FQN).size()); 1952 assertEquals(c3, loader.get(SUBTREE_FQN).get(1)); 1953 assertEquals(c4, loader.get(SUBTREE_FQN).get(2)); 1954 assertEquals(2, loader.get(SUBTREE_FQN).size()); 1955 1956 1957 ByteArrayOutputStream baos = new ByteArrayOutputStream (1024); 1958 MarshalledValueOutputStream os = new MarshalledValueOutputStream(baos); 1959 loader.loadState(SUBTREE_FQN, os); 1960 os.writeObject(StateTransferManager.STREAMING_DELIMETER_NODE); 1961 os.close(); 1962 assertTrue(baos.size() > 0); 1963 loader.remove(SUBTREE_FQN); 1964 1965 1966 1967 ByteArrayInputStream bais = new ByteArrayInputStream (baos.toByteArray()); 1968 MarshalledValueInputStream is = new MarshalledValueInputStream(bais); 1969 loader.storeState(SUBTREE_FQN, is); 1970 is.close(); 1971 addDelay(); 1972 assertEquals(c1, loader.get(FQN).get(1)); 1973 assertEquals(c2, loader.get(FQN).get(2)); 1974 assertEquals(2, loader.get(FQN).size()); 1975 assertEquals(c3, loader.get(SUBTREE_FQN).get(1)); 1976 assertEquals(c4, loader.get(SUBTREE_FQN).get(2)); 1977 assertEquals(2, loader.get(SUBTREE_FQN).size()); 1978 } 1979 1980 public void testBuddyBackupStore() throws Exception 1981 { 1982 1983 loader.remove(Fqn.ROOT); 1984 1985 1986 Complex c1 = new Complex(); 1987 Complex c2 = new Complex(c1); 1988 Complex c3 = new Complex(); 1989 Complex c4 = new Complex(c3); 1990 1991 1992 loader.put(FQN, 1, c1); 1993 loader.put(FQN, 2, c2); 1994 loader.put(SUBTREE_FQN, 1, c3); 1995 loader.put(SUBTREE_FQN, 2, c4); 1996 addDelay(); 1997 assertEquals(c1, loader.get(FQN).get(1)); 1998 assertEquals(c2, loader.get(FQN).get(2)); 1999 assertEquals(2, loader.get(FQN).size()); 2000 assertEquals(c3, loader.get(SUBTREE_FQN).get(1)); 2001 assertEquals(c4, loader.get(SUBTREE_FQN).get(2)); 2002 assertEquals(2, loader.get(SUBTREE_FQN).size()); 2003 2004 2005 ByteArrayOutputStream baos = new ByteArrayOutputStream (1024); 2006 MarshalledValueOutputStream os = new MarshalledValueOutputStream(baos); 2007 loader.loadState(FQN, os); 2008 os.writeObject(StateTransferManager.STREAMING_DELIMETER_NODE); 2009 os.close(); 2010 assertTrue(baos.size() > 0); 2011 2012 2013 ByteArrayInputStream bais = new ByteArrayInputStream (baos.toByteArray()); 2014 MarshalledValueInputStream is = new MarshalledValueInputStream(bais); 2015 loader.storeState(BUDDY_BASE, is); 2016 is.close(); 2017 addDelay(); 2018 assertEquals(c1, loader.get(BUDDY_PLUS_FQN).get(1)); 2019 assertEquals(c2, loader.get(BUDDY_PLUS_FQN).get(2)); 2020 assertEquals(2, loader.get(BUDDY_PLUS_FQN).size()); 2021 assertEquals(c3, loader.get(BUDDY_PLUS_SUBTREE_FQN).get(1)); 2022 assertEquals(c4, loader.get(BUDDY_PLUS_SUBTREE_FQN).get(2)); 2023 assertEquals(2, loader.get(BUDDY_PLUS_SUBTREE_FQN).size()); 2024 2025 } 2026 2027} 2028 | Popular Tags |