1 9 package org.jboss.portal.test.core.tree; 10 11 import org.jboss.cache.tests.CacheLoaderTests; 12 import org.jboss.cache.tests.SamplePojo; 13 import org.jboss.cache.TreeCache; 14 import org.jboss.cache.Fqn; 15 import org.jboss.cache.Modification; 16 import org.jboss.cache.CacheException; 17 import org.jboss.cache.transaction.DummyTransactionManager; 18 import org.jboss.cache.loader.CacheLoader; 19 import org.jboss.portal.cms.CMS; 20 import org.w3c.dom.Document ; 21 import org.w3c.dom.Element ; 22 23 import java.util.Properties ; 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 import java.util.Map ; 27 import java.util.ArrayList ; 28 import java.util.List ; 29 import java.util.Set ; 30 import java.io.InputStream ; 31 import java.io.Serializable ; 32 33 import junit.framework.TestCase; 34 35 import javax.xml.parsers.DocumentBuilderFactory ; 36 import javax.transaction.Transaction ; 37 import javax.transaction.NotSupportedException ; 38 39 46 public class CMSCacheLoaderTestCase extends TestCase 47 { 48 49 public CMSCacheLoaderTestCase(String name) 50 { 51 super(name); 52 } 53 54 protected CacheLoaderTests testCase; 55 protected CMS cms; 56 57 protected TreeCache cache; 58 protected CacheLoader loader; 59 protected Transaction tx; 60 protected static final Fqn FQN = new Fqn("key"); 61 62 protected void setUp() throws Exception 63 { 64 ClassLoader tccl = Thread.currentThread().getContextClassLoader(); 65 InputStream in = tccl.getResourceAsStream("cache/domain.xml"); 66 Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in); 67 Element elt = doc.getDocumentElement(); 68 cms = new CMS(); 69 cms.setConfig(elt); 70 cms.create(); 71 cms.start(); 72 73 cache = new TreeCache(); 74 cache.setCacheMode("local"); 75 cache.setCacheLoaderClass("org.jboss.portal.core.impl.tree.loader.CMSCacheLoader"); 76 Properties props = new Properties (); 77 cache.setCacheLoaderConfig(props); 78 79 cache.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 80 cache.createService(); 81 cache.startService(); 82 loader = cache.getCacheLoader(); 83 } 84 85 protected void tearDown() throws Exception 86 { 87 try 88 { 89 if (tx != null) 90 { 91 try 92 { 93 tx.commit(); 94 } 95 catch (Throwable e) 96 { 97 e.printStackTrace(); 98 } 99 } 100 cache.remove("/"); 101 cache.stopService(); 102 cache.destroyService(); 103 cms.stop(); 104 cms.destroy(); 105 } 106 catch (Exception e) 107 { 108 e.printStackTrace(); 109 } 110 } 111 112 113 public void testPrint() throws CacheException 114 { 115 final Fqn NODE = Fqn.fromString("/test"); 116 final String KEY = "key"; 117 cache.put(NODE, KEY, new Integer (10)); 118 cache.evict(NODE); 119 String ret = cache.print(NODE); 120 assertNotNull(ret); 121 } 122 123 public void testPut() throws CacheException 124 { 125 final String NODE = "/test"; 126 final String KEY = "key"; 127 Object retval = null; 128 cache.remove(NODE); 129 retval = cache.put(NODE, KEY, new Integer (10)); 130 assertNull(retval); 131 retval = cache.put(NODE, KEY, new Integer (20)); 132 assertEquals(new Integer (10), retval); 133 cache.evict(Fqn.fromString(NODE)); 135 retval = cache.put(NODE, KEY, new Integer (30)); 136 assertEquals(new Integer (20), retval); 137 } 138 139 140 public void _testSerialization() throws CacheException 141 { 142 SamplePojo pojo = new SamplePojo(39, "Bela"); 143 pojo.getHobbies().add("Running"); 144 pojo.getHobbies().add("Beerathlon"); 145 pojo.getHobbies().add("Triathlon"); 146 cache.put("/mypojo", new Integer (322649), pojo); 147 assertNotNull(cache.get("/mypojo", new Integer (322649))); 148 cache.evict(Fqn.fromString("/mypojo")); 149 assertFalse(cache.exists("/mypojo")); 150 SamplePojo pojo2 = (SamplePojo)cache.get("/mypojo", new Integer (322649)); assertNotNull(pojo2); 152 assertEquals(39, pojo2.getAge()); 153 assertEquals("Bela", pojo2.getName()); 154 assertEquals(3, pojo2.getHobbies().size()); 155 } 156 157 160 public void testPopulate() 161 { 162 try 163 { 164 Map m = new HashMap (); 165 for (int i = 0; i < 10; i++) 166 { 167 m.put("key" + i, "val" + i); 168 } 169 cache.put("/a/b/c", m); 170 cache.load("/1/2/3/4/5"); 171 cache.put("/1/2/3/4/5", null); 172 cache.put("/1/2/3/4/5/a", null); 173 cache.put("/1/2/3/4/5/b", null); 174 cache.put("/1/2/3/4/5/c", null); 175 cache.put("/1/2/3/4/5/d", null); 176 cache.put("/1/2/3/4/5/e", null); 177 cache.put("/1/2/3/4/5/d/one", null); 178 cache.put("/1/2/3/4/5/d/two", null); 179 cache.put("/1/2/3/4/5/d/three", null); 180 System.out.println("cache: " + cache); 182 183 assertTrue(cache.exists("/1/2/3/4")); 184 assertTrue(cache.exists("/a/b/c")); 185 assertFalse(cache.exists("/a/b/c/d")); 186 } 187 catch (Exception e) 188 { 189 fail(e.toString()); 190 } 191 } 192 193 194 public void testPreloading() throws CacheException 195 { 196 cache.remove("/"); 197 cache.put("1/2/3/4/5/d", "key", "val"); 198 cache.evict(Fqn.fromString("1/2/3/4/5/d")); 199 System.out.println("-- checking for 1/2/3/4/5/d"); 200 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")); 203 System.out.println("-- 1/2/3/4/5/d exists"); 204 } 205 206 207 public void testCacheLoading2() throws CacheException 208 { 209 Set keys = null; 210 cache.put("/a/b/c", "key", "val"); 211 try 212 { 213 keys = cache.getKeys("/a/b/c"); 214 assertNotNull(keys); 215 assertEquals(1, keys.size()); 216 } 217 catch (Exception e) 218 { 219 fail(e.toString()); 220 } 221 222 try 223 { 224 keys.add("myKey"); 225 fail("adding a key to an unmodifiable set should fail"); 226 } 227 catch (UnsupportedOperationException ex) 228 { 229 assertTrue("adding a key to an unmodifiable set failed (as expected)", true); 230 } 231 } 232 233 234 public void testExists() throws Exception 235 { 236 cache.put("/eins/zwei/drei", "key1", "val1"); 237 assertTrue(cache.exists("/eins/zwei/drei")); 238 assertTrue(cache.exists("/eins/zwei/drei", "key1")); 239 assertFalse(cache.exists("/eins/zwei/drei", "key2")); 240 assertFalse(cache.exists("/uno/due/tre")); 241 assertFalse(cache.exists("/une/due/tre", "key1")); 242 } 243 244 public void testGetChildren() 245 { 246 try 247 { 248 cache.put("/1/2/3/4/5/d/one", null); 249 cache.put("/1/2/3/4/5/d/two", null); 250 cache.put("/1/2/3/4/5/d/three", null); 251 Set children = cache.getChildrenNames("/1/2/3/4/5/d"); 252 assertNotNull(children); 253 assertEquals(3, children.size()); 254 assertTrue(children.contains("one")); 255 assertTrue(children.contains("two")); 256 assertTrue(children.contains("three")); 257 } 258 catch (Exception e) 259 { 260 e.printStackTrace(); 261 fail(e.toString()); 262 } 263 } 264 265 public void testGetChildren2() 266 { 267 try 268 { 269 cache.put("/1", null); 270 cache.put("a", null); 271 Set children = cache.getChildrenNames("/"); 272 assertNotNull(children); 273 assertEquals(2, children.size()); 274 assertTrue(children.contains("1")); 275 assertTrue(children.contains("a")); 276 } 277 catch (Exception e) 278 { 279 fail(e.toString()); 280 } 281 } 282 283 public void testGetChildren3() 284 { 285 try 286 { 287 cache.put("/1", null); 288 cache.put("a", null); 289 Set children = cache.getChildrenNames(""); 290 assertNotNull(children); 291 assertEquals(2, children.size()); 292 assertTrue(children.contains("1")); 293 assertTrue(children.contains("a")); 294 } 295 catch (Exception e) 296 { 297 fail(e.toString()); 298 } 299 } 300 301 public void testGetChildren4() 302 { 303 try 304 { 305 if (!cache.exists("/a/b/c")) 306 { 307 cache.put("/a/b/c", null); 308 } 309 Set children = cache.getChildrenNames((Fqn)null); 310 assertNull(children); 311 } 312 catch (Exception e) 313 { 314 fail(e.toString()); 315 } 316 } 317 318 319 public void testRemoveData() throws Exception 320 { 321 String key = "/x/y/z/"; 322 cache.put(key, "keyA", "valA"); 323 cache.put(key, "keyB", "valB"); 324 cache.put(key, "keyC", "valC"); 325 assertEquals(3, cache.getKeys(key).size()); 326 cache.removeData(key); 327 Set keys = cache.getKeys(key); 328 assertNull(keys); 329 cache.remove("/x"); 330 Object val = cache.get(key, "keyA"); 331 assertNull(val); 332 } 333 334 335 public void testRemoveData2() throws Exception 336 { 337 Set keys; 338 Fqn key = Fqn.fromString("/x/y/z/"); 339 cache.put(key, "keyA", "valA"); 340 cache.put(key, "keyB", "valB"); 341 cache.put(key, "keyC", "valC"); 342 keys = cache.getKeys(key); 343 assertEquals(3, keys.size()); 344 cache.removeData(key); 345 cache.evict(key); 346 keys = cache.getKeys(key); 347 assertEquals(0, keys.size()); 348 } 349 350 public void testRemoveData3() throws Exception 351 { 352 Set keys; 353 Fqn key = Fqn.fromString("/x/y/z/"); 354 cache.put(key, "keyA", "valA"); 355 cache.put(key, "keyB", "valB"); 356 cache.put(key, "keyC", "valC"); 357 keys = cache.getKeys(key); 358 assertEquals(3, keys.size()); 359 cache.evict(key); 360 cache.removeData(key); 361 keys = cache.getKeys(key); 362 assertNull(keys); 363 } 364 365 public void testRemoveKey() throws Exception 366 { 367 String key = "/x/y/z/"; 368 cache.put(key, "keyA", "valA"); 369 cache.put(key, "keyB", "valB"); 370 cache.put(key, "keyC", "valC"); 371 cache.remove(key, "keyA"); 372 assertEquals(2, cache.getKeys(key).size()); 373 cache.remove("/x"); 374 } 375 376 377 public void testRemoveKey2() throws CacheException 378 { 379 final String NODE = "/test"; 380 final String KEY = "key"; 381 Object retval = null; 382 cache.remove(NODE); 383 retval = cache.put(NODE, KEY, new Integer (10)); 384 assertNull(retval); 385 retval = cache.remove(NODE, KEY); 386 assertEquals(new Integer (10), retval); 387 retval = cache.remove(NODE, KEY); 388 assertNull(retval); 389 } 390 391 public void testRemoveKey3() throws CacheException 392 { 393 final String NODE = "/test"; 394 final String KEY = "key"; 395 Object retval = null; 396 cache.remove(NODE); 397 retval = cache.put(NODE, KEY, new Integer (10)); 398 assertNull(retval); 399 400 cache.evict(Fqn.fromString(NODE)); retval = cache.remove(NODE, KEY); 402 assertEquals(new Integer (10), retval); 403 404 cache.evict(Fqn.fromString(NODE)); retval = cache.remove(NODE, KEY); 406 assertNull(retval); 407 } 408 409 410 public void testRemove() throws Exception 411 { 412 String key = "/x/y/z/"; 413 cache.put(key, "keyA", "valA"); 414 cache.put(key, "keyB", "valB"); 415 cache.put(key, "keyC", "valC"); 416 cache.remove("/x"); 417 assertNull(cache.get(key, "keyA")); 418 Set keys = cache.getKeys(key); 419 assertNull(keys); 420 cache.remove("/x"); 421 } 422 423 424 public void testRemoveRoot() throws Exception 425 { 426 assertEquals(0, cache.getKeys("/").size()); 427 cache.put("/1/2/3/4/5", null); 428 cache.put("uno/due/tre", null); 429 cache.put("1/2/3/a", null); 430 cache.put("/eins/zwei/drei", null); 431 cache.put("/one/two/three", null); 432 cache.remove("/"); 433 assertEquals(0, cache.getKeys("/").size()); 434 } 435 436 437 public void testEvictionWithCacheLoader() throws Exception 438 { 439 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")); 443 assertTrue(cache.exists("/first/second")); 444 assertTrue(cache.exists("/first")); 445 String val = (String )cache.get("/first/second", "key1"); assertEquals("val1", val); 447 assertTrue(cache.exists("/first/second/third")); 448 assertTrue(cache.exists("/first/second")); 449 assertTrue(cache.exists("/first")); 450 } 451 452 453 public void testEvictionWithCacheLoader2() throws Exception 454 { 455 cache.put("/first/second/third", "key1", "val1"); cache.evict(Fqn.fromString("/first/second/third")); assertFalse(cache.exists("/first/second/third")); 458 assertTrue(cache.exists("/first/second")); 459 assertTrue(cache.exists("/first")); 460 String val = (String )cache.get("/first/second/third", "key1"); assertEquals("val1", val); 462 assertTrue(cache.exists("/first/second/third")); 463 assertTrue(cache.exists("/first/second")); 464 assertTrue(cache.exists("/first")); 465 } 466 467 468 public void testEvictionWithGetChildrenNames() throws Exception 469 { 470 cache.put("/a/1", null); 471 cache.put("/a/2", null); 472 cache.put("/a/3", null); 473 cache.evict(Fqn.fromString("/a/1")); 475 cache.evict(Fqn.fromString("/a/2")); 476 cache.evict(Fqn.fromString("/a/3")); 477 cache.evict(Fqn.fromString("/a")); 478 Set children = cache.getChildrenNames("/a"); 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 487 public void testTxPutCommit() throws Exception , NotSupportedException 488 { 489 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 490 mgr.begin(); 491 tx = mgr.getTransaction(); 492 493 cache.put("/one/two/three", "key1", "val1"); 494 cache.put("/one/two/three/four", "key2", "val2"); 495 tx.commit(); 496 assertNotNull(cache.getKeys("/one/two/three")); 497 Set children = cache.getChildrenNames("/one"); 498 assertEquals(1, children.size()); 499 cache.remove("/"); 500 } 501 502 503 public void testTxPutRollback() throws Exception , NotSupportedException 504 { 505 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 506 507 cache.remove("/one"); 508 509 mgr.begin(); 510 tx = mgr.getTransaction(); 511 512 cache.put("/one/two/three", "key1", "val1"); 513 cache.put("/one/two/three/four", "key2", "val2"); 514 tx.rollback(); 515 assertNull(cache.getKeys("/one/two/three")); 516 Set children = cache.getChildrenNames("/one"); 517 assertNull(children); 518 } 519 520 521 524 public void testBasicOperations() 525 throws Exception 526 { 527 528 doTestBasicOperations(); 529 } 530 531 534 public void testBasicOperationsTransactional() 535 throws Exception 536 { 537 538 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 539 mgr.begin(); 540 tx = mgr.getTransaction(); 541 doTestBasicOperations(); 542 tx.commit(); 543 } 544 545 548 private void doTestBasicOperations() throws Exception 549 { 550 551 552 doPutTests(new Fqn("key")); 553 doRemoveTests(new Fqn("key")); 554 556 557 doPutTests(new Fqn("key1")); 558 doPutTests(new Fqn("key3")); 559 doPutTests(new Fqn("key2")); 560 assertEquals(4, loader.get(new Fqn("key1")).size()); 561 assertEquals(4, loader.get(new Fqn("key2")).size()); 562 assertEquals(4, loader.get(new Fqn("key3")).size()); 563 564 565 doRemoveTests(new Fqn("key2")); 566 doRemoveTests(new Fqn("key3")); 567 doRemoveTests(new Fqn("key1")); 568 assertEquals(null, loader.get(new Fqn("key1"))); 569 assertEquals(null, loader.get(new Fqn("key2"))); 570 assertEquals(null, loader.get(new Fqn("key3"))); 571 } 573 574 577 private void doPutTests(Fqn fqn) 578 throws Exception 579 { 580 581 assertTrue(!loader.exists(fqn)); 582 583 584 Object oldVal; 585 oldVal = loader.put(fqn, "one", "two"); 586 assertNull(oldVal); 587 oldVal = loader.put(fqn, "three", "four"); 588 assertNull(oldVal); 589 assertEquals("two", loader.get(fqn, "one")); 590 assertEquals("four", loader.get(fqn, "three")); 591 oldVal = loader.put(fqn, "one", "xxx"); 592 assertEquals("two", oldVal); 593 oldVal = loader.put(fqn, "one", "two"); 594 assertEquals("xxx", oldVal); 595 596 597 Map map = loader.get(fqn); 598 assertEquals(2, map.size()); 599 assertEquals("two", map.get("one")); 600 assertEquals("four", map.get("three")); 601 602 603 map.put("five", "six"); 604 map.put("seven", "eight"); 605 loader.put(fqn, map); 606 assertEquals("six", loader.get(fqn, "five")); 607 assertEquals("eight", loader.get(fqn, "seven")); 608 assertEquals(map, loader.get(fqn)); 609 assertEquals(4, map.size()); 610 611 assertTrue(loader.exists(fqn)); 612 } 613 614 617 private void doRemoveTests(Fqn fqn) 618 throws Exception 619 { 620 621 622 Object oldVal; 623 oldVal = loader.remove(fqn, "one"); 624 assertEquals("two", oldVal); 625 oldVal = loader.remove(fqn, "five"); 626 assertEquals("six", oldVal); 627 assertEquals(null, loader.get(fqn, "one")); 628 assertEquals(null, loader.get(fqn, "five")); 629 assertEquals("four", loader.get(fqn, "three")); 630 assertEquals("eight", loader.get(fqn, "seven")); 631 Map map = loader.get(fqn); 632 assertEquals(2, map.size()); 633 assertEquals("four", map.get("three")); 634 assertEquals("eight", map.get("seven")); 635 636 637 assertTrue(loader.exists(fqn)); 638 loader.remove(fqn); 639 assertEquals(null, loader.get(fqn, "three")); 640 assertEquals(null, loader.get(fqn, "seven")); 641 map = loader.get(fqn); 642 assertEquals(null, map); 643 assertTrue(!loader.exists(fqn)); 644 } 645 646 650 public void testMultiLevelTree() 651 throws Exception 652 { 653 654 655 assertTrue(!loader.exists(new Fqn("key0"))); 656 loader.put(Fqn.fromString("/key0/level1/level2"), null); 657 assertTrue(loader.exists(Fqn.fromString("/key0/level1/level2"))); 658 assertTrue(loader.exists(Fqn.fromString("/key0/level1"))); 659 assertTrue(loader.exists(new Fqn("key0"))); 660 661 662 loader.put(Fqn.fromString("/key0/x/y"), null); 663 assertTrue(loader.exists(Fqn.fromString("/key0/x/y"))); 664 assertTrue(loader.exists(Fqn.fromString("/key0/x"))); 665 loader.remove(Fqn.fromString("/key0/x/y")); 666 assertTrue(!loader.exists(Fqn.fromString("/key0/x/y"))); 667 assertTrue(loader.exists(Fqn.fromString("/key0/x"))); 668 669 670 loader.remove(new Fqn("key0")); 671 assertTrue(!loader.exists(new Fqn("key0"))); 672 assertTrue(!loader.exists(Fqn.fromString("/key0/level1/level2"))); 673 assertTrue(!loader.exists(Fqn.fromString("/key0/level1"))); 674 assertTrue(!loader.exists(Fqn.fromString("/key0/x"))); 675 676 677 loader.put(new Fqn("key1"), null); 678 loader.put(new Fqn("key2"), null); 679 loader.put(new Fqn("key3"), null); 680 assertTrue(loader.exists(new Fqn("key1"))); 681 assertTrue(loader.exists(new Fqn("key2"))); 682 assertTrue(loader.exists(new Fqn("key3"))); 683 684 685 assertTrue(!loader.exists(Fqn.fromString("/key3/level1"))); 686 assertTrue(!loader.exists(Fqn.fromString("/key3/level1/level2"))); 687 loader.put(Fqn.fromString("/key3/level1/level2"), null); 688 assertTrue(loader.exists(Fqn.fromString("/key3/level1/level2"))); 689 assertTrue(loader.exists(Fqn.fromString("/key3/level1"))); 690 691 692 assertTrue(loader.exists(new Fqn("key1"))); 693 assertTrue(loader.exists(new Fqn("key2"))); 694 assertTrue(loader.exists(new Fqn("key3"))); 695 696 697 loader.remove(Fqn.fromString("/key3/level1")); 698 assertTrue(!loader.exists(Fqn.fromString("/key3/level1/level2"))); 699 assertTrue(!loader.exists(Fqn.fromString("/key3/level1"))); 700 701 702 assertTrue(loader.exists(new Fqn("key1"))); 703 assertTrue(loader.exists(new Fqn("key2"))); 704 assertTrue(loader.exists(new Fqn("key3"))); 705 706 707 loader.remove(new Fqn("key1")); 708 assertTrue(!loader.exists(new Fqn("key1"))); 709 assertTrue(loader.exists(new Fqn("key2"))); 710 assertTrue(loader.exists(new Fqn("key3"))); 711 712 713 loader.remove(new Fqn("key3")); 714 assertTrue(loader.exists(new Fqn("key2"))); 715 assertTrue(!loader.exists(new Fqn("key3"))); 716 717 718 loader.remove(new Fqn("key2")); 719 assertTrue(!loader.exists(new Fqn("key0"))); 720 assertTrue(!loader.exists(new Fqn("key1"))); 721 assertTrue(!loader.exists(new Fqn("key2"))); 722 assertTrue(!loader.exists(new Fqn("key3"))); 723 724 725 726 assertNull(loader.get(new Fqn("key0"))); 727 loader.put(Fqn.fromString("/key0/level1/level2"), "a", "b"); 728 assertNotNull(loader.get(Fqn.fromString("/key0/level1/level2"))); 729 assertNull(loader.get(Fqn.fromString("/key0/level1"))); 730 assertNull(loader.get(new Fqn("key0"))); 731 732 loader.put(Fqn.fromString("/key0/x/y"), "a", "b"); 733 assertNotNull(loader.get(Fqn.fromString("/key0/x/y"))); 734 assertNull(loader.get(Fqn.fromString("/key0/x"))); 735 loader.remove(Fqn.fromString("/key0/x/y")); 736 assertNull(loader.get(Fqn.fromString("/key0/x/y"))); 737 assertNull(loader.get(Fqn.fromString("/key0/x"))); 738 739 loader.remove(new Fqn("key0")); 740 assertNull(loader.get(new Fqn("key0"))); 741 assertNull(loader.get(Fqn.fromString("/key0/level1/level2"))); 742 assertNull(loader.get(Fqn.fromString("/key0/level1"))); 743 assertNull(loader.get(Fqn.fromString("/key0/x"))); 744 745 loader.put(new Fqn("key1"), "a", "b"); 746 loader.put(new Fqn("key2"), "a", "b"); 747 loader.put(new Fqn("key3"), "a", "b"); 748 assertNotNull(loader.get(new Fqn("key1"))); 749 assertNotNull(loader.get(new Fqn("key2"))); 750 assertNotNull(loader.get(new Fqn("key3"))); 751 752 assertNull(loader.get(Fqn.fromString("/key3/level1"))); 753 assertNull(loader.get(Fqn.fromString("/key3/level1/level2"))); 754 loader.put(Fqn.fromString("/key3/level1/level2"), "a", "b"); 755 assertNotNull(loader.get(Fqn.fromString("/key3/level1/level2"))); 756 assertNull(loader.get(Fqn.fromString("/key3/level1"))); 757 758 assertNotNull(loader.get(new Fqn("key1"))); 759 assertNotNull(loader.get(new Fqn("key2"))); 760 assertNotNull(loader.get(new Fqn("key3"))); 761 762 loader.remove(Fqn.fromString("/key3/level1")); 763 assertNull(loader.get(Fqn.fromString("/key3/level1/level2"))); 764 assertNull(loader.get(Fqn.fromString("/key3/level1"))); 765 766 assertNotNull(loader.get(new Fqn("key1"))); 767 assertNotNull(loader.get(new Fqn("key2"))); 768 assertNotNull(loader.get(new Fqn("key3"))); 769 770 loader.remove(new Fqn("key1")); 771 assertNull(loader.get(new Fqn("key1"))); 772 assertNotNull(loader.get(new Fqn("key2"))); 773 assertNotNull(loader.get(new Fqn("key3"))); 774 775 loader.remove(new Fqn("key3")); 776 assertNotNull(loader.get(new Fqn("key2"))); 777 assertNull(loader.get(new Fqn("key3"))); 778 779 loader.remove(new Fqn("key2")); 780 assertNull(loader.get(new Fqn("key0"))); 781 assertNull(loader.get(new Fqn("key1"))); 782 assertNull(loader.get(new Fqn("key2"))); 783 assertNull(loader.get(new Fqn("key3"))); 784 } 785 786 789 public void testGetChildrenNames() 790 throws Exception 791 { 792 793 checkChildren(new Fqn(), null); 794 checkChildren(Fqn.fromString("/key0"), null); 795 796 loader.put(Fqn.fromString("/key0"), null); 797 checkChildren(new Fqn(), new String []{"key0"}); 798 799 loader.put(Fqn.fromString("/key1/x"), null); 800 checkChildren(new Fqn(), new String []{"key0", "key1"}); 801 checkChildren(Fqn.fromString("/key1"), new String []{"x"}); 802 803 loader.remove(Fqn.fromString("/key1/x")); 804 checkChildren(new Fqn(), new String []{"key0", "key1"}); 805 checkChildren(Fqn.fromString("/key0"), null); 806 checkChildren(Fqn.fromString("/key1"), null); 807 808 loader.put(Fqn.fromString("/key0/a"), null); 809 loader.put(Fqn.fromString("/key0/ab"), null); 810 loader.put(Fqn.fromString("/key0/abc"), null); 811 checkChildren(Fqn.fromString("/key0"), 812 new String []{"a", "ab", "abc"}); 813 814 loader.put(Fqn.fromString("/key0/xxx"), null); 815 loader.put(Fqn.fromString("/key0/xx"), null); 816 loader.put(Fqn.fromString("/key0/x"), null); 817 checkChildren(Fqn.fromString("/key0"), 818 new String []{"a", "ab", "abc", "x", "xx", "xxx"}); 819 820 loader.put(Fqn.fromString("/key0/a/1"), null); 821 loader.put(Fqn.fromString("/key0/a/2"), null); 822 loader.put(Fqn.fromString("/key0/a/2/1"), null); 823 checkChildren(Fqn.fromString("/key0/a/2"), new String []{"1"}); 824 checkChildren(Fqn.fromString("/key0/a"), new String []{"1", "2"}); 825 checkChildren(Fqn.fromString("/key0"), 826 new String []{"a", "ab", "abc", "x", "xx", "xxx"}); 827 } 848 849 852 private void checkChildren(Fqn fqn, String [] names) 853 throws Exception 854 { 855 856 Set set = loader.getChildrenNames(fqn); 857 if (names != null) 858 { 859 assertEquals(names.length, set.size()); 860 for (int i = 0; i < names.length; i += 1) 861 { 862 assertTrue(set.contains(names[i])); 863 } 864 } 865 else 866 { 867 assertNull(set); 868 } 869 } 870 871 874 public void testModifications() 875 throws Exception 876 { 877 878 doTestModifications(); 879 } 880 881 884 public void testModificationsTransactional() 885 throws Exception 886 { 887 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 888 mgr.begin(); 889 tx = mgr.getTransaction(); 890 doTestModifications(); 891 tx.commit(); 892 } 893 894 897 private void doTestModifications() 898 throws Exception 899 { 900 901 902 List list = createUpdates(); 903 loader.put(list); 904 checkModifications(list); 905 906 907 list = new ArrayList (); 908 Modification mod = new Modification(); 909 mod.setType(Modification.REMOVE_KEY_VALUE); 910 mod.setFqn(FQN); 911 mod.setKey("one"); 912 list.add(mod); 913 loader.put(list); 914 checkModifications(list); 915 916 917 list = new ArrayList (); 918 mod = new Modification(); 919 mod.setType(Modification.REMOVE_NODE); 920 mod.setFqn(FQN); 921 list.add(mod); 922 loader.put(list); 923 checkModifications(list); 924 assertEquals(null, loader.get(FQN)); 925 926 927 loader.put(FQN, "one", "two"); 928 list = new ArrayList (); 929 mod = new Modification(); 930 mod.setType(Modification.REMOVE_DATA); 931 mod.setFqn(FQN); 932 list.add(mod); 933 loader.put(list); 934 checkModifications(list); 935 } 936 937 940 public void testOnePhaseTransaction() 941 throws Exception 942 { 943 List mods = createUpdates(); 944 loader.prepare(null, mods, true); 945 checkModifications(mods); 946 } 947 948 951 public void testTwoPhaseTransaction() 952 throws Exception 953 { 954 955 Object txnKey = new Object (); 956 List mods = createUpdates(); 957 loader.prepare(txnKey, mods, false); 958 loader.commit(txnKey); 963 checkModifications(mods); 964 } 965 966 969 public void testTransactionRollback() 970 throws Exception 971 { 972 973 loader.remove(Fqn.fromString("/")); 974 975 int num = loader.loadEntireState().length; 976 977 Object txnKey = new Object (); 978 List mods = createUpdates(); 979 loader.prepare(txnKey, mods, false); 980 loader.rollback(txnKey); 981 assertEquals(num, loader.loadEntireState().length); 982 } 983 984 987 private List createUpdates() 988 { 989 990 List list = new ArrayList (); 991 992 Modification mod = new Modification(); 993 mod.setType(Modification.PUT_KEY_VALUE); 994 mod.setFqn(FQN); 995 mod.setKey("one"); 996 mod.setValue("two"); 997 list.add(mod); 998 999 mod = new Modification(); 1000 mod.setType(Modification.PUT_KEY_VALUE); 1001 mod.setFqn(FQN); 1002 mod.setKey("three"); 1003 mod.setValue("four"); 1004 list.add(mod); 1005 1006 Map map = new HashMap (); 1007 map.put("five", "six"); 1008 map.put("seven", "eight"); 1009 mod = new Modification(); 1010 mod.setType(Modification.PUT_DATA); 1011 mod.setFqn(FQN); 1012 mod.setData(map); 1013 list.add(mod); 1014 1015 return list; 1016 } 1017 1018 1021 private void checkModifications(List list) 1022 throws Exception 1023 { 1024 1025 for (int i = 0; i < list.size(); i += 1) 1026 { 1027 Modification mod = (Modification)list.get(i); 1028 Fqn fqn = mod.getFqn(); 1029 switch (mod.getType()) 1030 { 1031 case Modification.PUT_KEY_VALUE: 1032 assertEquals(mod.getValue(), loader.get(fqn, mod.getKey())); 1033 break; 1034 case Modification.PUT_DATA: 1035 Map map = mod.getData(); 1036 for (Iterator iter = map.keySet().iterator(); iter.hasNext();) 1037 { 1038 Object key = iter.next(); 1039 assertEquals(map.get(key), loader.get(fqn, key)); 1040 } 1041 break; 1042 case Modification.REMOVE_KEY_VALUE: 1043 assertEquals(null, loader.get(fqn, mod.getKey())); 1044 break; 1045 case Modification.REMOVE_DATA: 1046 map = loader.get(fqn); 1047 assertNull(map); 1048 break; 1049 case Modification.REMOVE_NODE: 1050 assertEquals(null, loader.get(fqn)); 1051 break; 1052 default: 1053 fail("unknown type: " + mod); 1054 break; 1055 } 1056 } 1057 } 1058 1059 1060 1063 public void _testNullKeysAndValues() 1064 throws Exception 1065 { 1066 1067 loader.put(FQN, null, "x"); 1068 assertEquals("x", loader.get(FQN, null)); 1069 Map map = loader.get(FQN); 1070 assertEquals(1, map.size()); 1071 assertEquals("x", map.get(null)); 1072 1073 loader.put(FQN, "y", null); 1074 assertEquals(null, loader.get(FQN, "y")); 1075 map = loader.get(FQN); 1076 assertEquals(2, map.size()); 1077 assertEquals("x", map.get(null)); 1078 assertEquals(null, map.get("y")); 1079 1080 loader.remove(FQN, null); 1081 assertEquals(null, loader.get(FQN, null)); 1082 assertEquals(1, loader.get(FQN).size()); 1083 1084 loader.remove(FQN, "y"); 1085 assertEquals(null, loader.get(FQN, "y")); 1086 assertNull(loader.get(FQN)); 1087 1088 map = new HashMap (); 1089 map.put(null, null); 1090 loader.put(FQN, map); 1091 assertEquals(map, loader.get(FQN)); 1092 1093 loader.remove(FQN); 1094 assertEquals(null, loader.get(FQN)); 1095 1096 map = new HashMap (); 1097 map.put("xyz", null); 1098 map.put(null, "abc"); 1099 loader.put(FQN, map); 1100 assertEquals(map, loader.get(FQN)); 1101 1102 loader.remove(FQN); 1103 assertEquals(null, loader.get(FQN)); 1104 } 1105 1106 1109 public void testDatabaseName() 1110 throws Exception 1111 { 1112 1113 loader.put(FQN, "one", "two"); 1114 assertEquals("two", loader.get(FQN, "one")); 1115 } 1116 1117 1120 public void testLoadAndStore() 1121 throws Exception 1122 { 1123 1124 1125 loader.remove(Fqn.fromString("/")); 1126 1133 1134 Complex c1 = new Complex(); 1135 Complex c2 = new Complex(c1); 1136 1137 1138 loader.put(FQN, "1", c1); 1139 loader.put(FQN, "2", c2); 1140 assertEquals(c1, loader.get(FQN, "1")); 1141 assertEquals(c2, loader.get(FQN, "2")); 1142 assertEquals(2, loader.get(FQN).size()); 1143 1144 1145 byte[] state = loader.loadEntireState(); 1146 assertTrue(state.length > 0); 1147 1148 1149 1150 loader.storeEntireState(state); 1151 assertEquals(c1, loader.get(FQN, "1")); 1152 assertEquals(c2, loader.get(FQN, "2")); 1153 assertEquals(2, loader.get(FQN).size()); 1154 } 1155 1156 1157 1160 private static class Complex implements Serializable 1161 { 1162 1163 Complex nested; 1164 1165 Complex() 1166 { 1167 this(null); 1168 } 1169 1170 Complex(Complex nested) 1171 { 1172 this.nested = nested; 1173 } 1174 1175 public boolean equals(Object o) 1176 { 1177 try 1178 { 1179 Complex x = (Complex)o; 1180 return (nested != null) ? nested.equals(x.nested) 1181 : (x.nested == null); 1182 } 1183 catch (ClassCastException e) 1184 { 1185 return false; 1186 } 1187 } 1188 1189 public int hashCode() 1190 { 1191 if (nested == null) 1192 { 1193 return super.hashCode(); 1194 } 1195 else 1196 { 1197 return 13 + nested.hashCode(); 1198 } 1199 } 1200 } 1201} 1202 | Popular Tags |