1 7 package org.jboss.cache.tests; 8 9 import junit.framework.Test; 10 import junit.framework.TestCase; 11 import junit.framework.TestSuite; 12 import org.jboss.cache.CacheException; 13 import org.jboss.cache.Fqn; 14 import org.jboss.cache.Modification; 15 import org.jboss.cache.TreeCache; 16 import org.jboss.cache.loader.CacheLoader; 17 import org.jboss.cache.transaction.DummyTransactionManager; 18 19 import javax.transaction.NotSupportedException ; 20 import javax.transaction.Transaction ; 21 import java.io.Serializable ; 22 import java.util.*; 23 24 29 abstract public class CacheLoaderTests extends TestCase { 30 TreeCache cache; 31 CacheLoader loader=null; 32 Transaction tx=null; 33 static final Fqn FQN = new Fqn("key"); 34 35 36 protected void setUp() throws Exception { 37 super.setUp(); 38 cache=new TreeCache(); 39 cache.setCacheMode("local"); 40 configureCache(); 41 cache.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 43 cache.createService(); 44 cache.startService(); 45 loader=cache.getCacheLoader(); 46 } 47 48 abstract protected void configureCache() throws Exception ; 49 50 51 protected void tearDown() throws Exception { 52 super.tearDown(); 53 if(tx != null) { 54 try { 55 tx.commit(); 56 } 57 catch(Throwable e) { 58 e.printStackTrace(); 59 } 60 } 61 cache.remove("/"); 62 cache.stopService(); 63 cache.destroyService(); 64 } 65 66 67 68 public void testPrint() throws CacheException { 69 final Fqn NODE=Fqn.fromString("/test"); 70 final String KEY="key"; 71 cache.put(NODE, KEY, new Integer (10)); 72 cache.evict(NODE); 73 String ret=cache.print(NODE); 74 assertNotNull(ret); 75 } 76 77 public void testPut() throws CacheException { 78 final String NODE="/test"; 79 final String KEY="key"; 80 Object retval=null; 81 cache.remove(NODE); 82 retval=cache.put(NODE, KEY, new Integer (10)); 83 assertNull(retval); 84 retval=cache.put(NODE, KEY, new Integer (20)); 85 assertEquals(new Integer (10), retval); 86 cache.evict(Fqn.fromString(NODE)); 88 retval=cache.put(NODE, KEY, new Integer (30)); 89 assertEquals(new Integer (20), retval); 90 } 91 92 93 public void testSerialization() throws CacheException { 94 SamplePojo pojo=new SamplePojo(39, "Bela"); 95 pojo.getHobbies().add("Running"); 96 pojo.getHobbies().add("Beerathlon"); 97 pojo.getHobbies().add("Triathlon"); 98 cache.put("/mypojo", new Integer (322649), pojo); 99 assertNotNull(cache.get("/mypojo", new Integer (322649))); 100 cache.evict(Fqn.fromString("/mypojo")); 101 assertFalse(cache.exists("/mypojo")); 102 SamplePojo pojo2=(SamplePojo)cache.get("/mypojo", new Integer (322649)); assertNotNull(pojo2); 104 assertEquals(39, pojo2.getAge()); 105 assertEquals("Bela", pojo2.getName()); 106 assertEquals(3, pojo2.getHobbies().size()); 107 } 108 109 110 public void testPopulate() { 111 try { 112 Map m=new HashMap(); 113 for(int i=0; i < 10; i++) 114 m.put("key" + i, "val" + i); 115 cache.put("/a/b/c", m); 116 cache.load("/1/2/3/4/5"); 117 cache.put("/1/2/3/4/5", null); 118 cache.put("/1/2/3/4/5/a", null); 119 cache.put("/1/2/3/4/5/b", null); 120 cache.put("/1/2/3/4/5/c", null); 121 cache.put("/1/2/3/4/5/d", null); 122 cache.put("/1/2/3/4/5/e", null); 123 cache.put("/1/2/3/4/5/d/one", null); 124 cache.put("/1/2/3/4/5/d/two", null); 125 cache.put("/1/2/3/4/5/d/three", null); 126 System.out.println("cache: " + cache); 128 129 assertTrue(cache.exists("/1/2/3/4")); 130 assertTrue(cache.exists("/a/b/c")); 131 assertFalse(cache.exists("/a/b/c/d")); 132 } 133 catch(Exception e) { 134 fail(e.toString()); 135 } 136 } 137 138 139 public void testPreloading() throws CacheException { 140 cache.remove("/"); 141 cache.put("1/2/3/4/5/d", "key", "val"); 142 cache.evict(Fqn.fromString("1/2/3/4/5/d")); 143 System.out.println("-- checking for 1/2/3/4/5/d"); 144 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")); 147 System.out.println("-- 1/2/3/4/5/d exists"); 148 } 149 150 151 152 public void testCacheLoading2() throws CacheException { 153 Set keys=null; 154 cache.put("/a/b/c", "key", "val"); 155 try { 156 keys=cache.getKeys("/a/b/c"); 157 assertNotNull(keys); 158 assertEquals(1, keys.size()); 159 } 160 catch(Exception e) { 161 fail(e.toString()); 162 } 163 164 try { 165 keys.add("myKey"); 166 fail("adding a key to an unmodifiable set should fail"); 167 } 168 catch(UnsupportedOperationException ex) { 169 assertTrue("adding a key to an unmodifiable set failed (as expected)", true); 170 } 171 } 172 173 174 public void testExists() throws Exception { 175 cache.put("/eins/zwei/drei", "key1", "val1"); 176 assertTrue(cache.exists("/eins/zwei/drei")); 177 assertTrue(cache.exists("/eins/zwei/drei", "key1")); 178 assertFalse(cache.exists("/eins/zwei/drei", "key2")); 179 assertFalse(cache.exists("/uno/due/tre")); 180 assertFalse(cache.exists("/une/due/tre", "key1")); 181 } 182 183 public void testGetChildren() { 184 try { 185 cache.put("/1/2/3/4/5/d/one", null); 186 cache.put("/1/2/3/4/5/d/two", null); 187 cache.put("/1/2/3/4/5/d/three", null); 188 Set children=cache.getChildrenNames("/1/2/3/4/5/d"); 189 assertNotNull(children); 190 assertEquals(3, children.size()); 191 assertTrue(children.contains("one")); 192 assertTrue(children.contains("two")); 193 assertTrue(children.contains("three")); 194 } 195 catch(Exception e) { 196 fail(e.toString()); 197 } 198 } 199 200 public void testGetChildren2() { 201 try { 202 cache.put("/1", null); 203 cache.put("a", null); 204 Set children=cache.getChildrenNames("/"); 205 assertNotNull(children); 206 assertEquals(2, children.size()); 207 assertTrue(children.contains("1")); 208 assertTrue(children.contains("a")); 209 } 210 catch(Exception e) { 211 fail(e.toString()); 212 } 213 } 214 215 public void testGetChildren3() { 216 try { 217 cache.put("/1", null); 218 cache.put("a", null); 219 Set children=cache.getChildrenNames(""); 220 assertNotNull(children); 221 assertEquals(2, children.size()); 222 assertTrue(children.contains("1")); 223 assertTrue(children.contains("a")); 224 } 225 catch(Exception e) { 226 fail(e.toString()); 227 } 228 } 229 230 public void testGetChildren4() { 231 try { 232 if(!cache.exists("/a/b/c")) 233 cache.put("/a/b/c", null); 234 Set children=cache.getChildrenNames((Fqn)null); 235 assertNull(children); 236 } 237 catch(Exception e) { 238 fail(e.toString()); 239 } 240 } 241 242 243 244 public void testRemoveData() throws Exception { 245 String key="/x/y/z/"; 246 cache.put(key, "keyA", "valA"); 247 cache.put(key, "keyB", "valB"); 248 cache.put(key, "keyC", "valC"); 249 assertEquals(3, cache.getKeys(key).size()); 250 cache.removeData(key); 251 Set keys=cache.getKeys(key); 252 assertNull(keys); 253 cache.remove("/x"); 254 Object val=cache.get(key, "keyA"); 255 assertNull(val); 256 } 257 258 259 public void testRemoveData2() throws Exception { 260 Set keys; 261 Fqn key=Fqn.fromString("/x/y/z/"); 262 cache.put(key, "keyA", "valA"); 263 cache.put(key, "keyB", "valB"); 264 cache.put(key, "keyC", "valC"); 265 keys=cache.getKeys(key); 266 assertEquals(3, keys.size()); 267 cache.removeData(key); 268 cache.evict(key); 269 keys=cache.getKeys(key); 270 assertEquals(0, keys.size()); 271 } 272 273 public void testRemoveData3() throws Exception { 274 Set keys; 275 Fqn key=Fqn.fromString("/x/y/z/"); 276 cache.put(key, "keyA", "valA"); 277 cache.put(key, "keyB", "valB"); 278 cache.put(key, "keyC", "valC"); 279 keys=cache.getKeys(key); 280 assertEquals(3, keys.size()); 281 cache.evict(key); 282 cache.removeData(key); 283 keys=cache.getKeys(key); 284 assertNull(keys); 285 } 286 287 public void testRemoveKey() throws Exception { 288 String key="/x/y/z/"; 289 cache.put(key, "keyA", "valA"); 290 cache.put(key, "keyB", "valB"); 291 cache.put(key, "keyC", "valC"); 292 cache.remove(key, "keyA"); 293 assertEquals(2, cache.getKeys(key).size()); 294 cache.remove("/x"); 295 } 296 297 298 public void testRemoveKey2() throws CacheException { 299 final String NODE="/test"; 300 final String KEY="key"; 301 Object retval=null; 302 cache.remove(NODE); 303 retval=cache.put(NODE, KEY, new Integer (10)); 304 assertNull(retval); 305 retval=cache.remove(NODE, KEY); 306 assertEquals(new Integer (10), retval); 307 retval=cache.remove(NODE, KEY); 308 assertNull(retval); 309 } 310 311 public void testRemoveKey3() throws CacheException { 312 final String NODE="/test"; 313 final String KEY="key"; 314 Object retval=null; 315 cache.remove(NODE); 316 retval=cache.put(NODE, KEY, new Integer (10)); 317 assertNull(retval); 318 319 cache.evict(Fqn.fromString(NODE)); retval=cache.remove(NODE, KEY); 321 assertEquals(new Integer (10), retval); 322 323 cache.evict(Fqn.fromString(NODE)); retval=cache.remove(NODE, KEY); 325 assertNull(retval); 326 } 327 328 329 public void testRemove() throws Exception { 330 String key="/x/y/z/"; 331 cache.put(key, "keyA", "valA"); 332 cache.put(key, "keyB", "valB"); 333 cache.put(key, "keyC", "valC"); 334 cache.remove("/x"); 335 assertNull(cache.get(key, "keyA")); 336 Set keys=cache.getKeys(key); 337 assertNull(keys); 338 cache.remove("/x"); 339 } 340 341 342 public void testRemoveRoot() throws Exception { 343 assertEquals(0, cache.getKeys("/").size()); 344 cache.put("/1/2/3/4/5", null); 345 cache.put("uno/due/tre", null); 346 cache.put("1/2/3/a", null); 347 cache.put("/eins/zwei/drei", null); 348 cache.put("/one/two/three", null); 349 cache.remove("/"); 350 assertEquals(0, cache.getKeys("/").size()); 351 } 352 353 354 public void testEvictionWithCacheLoader() throws Exception { 355 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")); 359 assertTrue(cache.exists("/first/second")); 360 assertTrue(cache.exists("/first")); 361 String val=(String )cache.get("/first/second", "key1"); assertEquals("val1", val); 363 assertTrue(cache.exists("/first/second/third")); 364 assertTrue(cache.exists("/first/second")); 365 assertTrue(cache.exists("/first")); 366 } 367 368 369 public void testEvictionWithCacheLoader2() throws Exception { 370 cache.put("/first/second/third", "key1", "val1"); cache.evict(Fqn.fromString("/first/second/third")); assertFalse(cache.exists("/first/second/third")); 373 assertTrue(cache.exists("/first/second")); 374 assertTrue(cache.exists("/first")); 375 String val=(String )cache.get("/first/second/third", "key1"); assertEquals("val1", val); 377 assertTrue(cache.exists("/first/second/third")); 378 assertTrue(cache.exists("/first/second")); 379 assertTrue(cache.exists("/first")); 380 } 381 382 383 384 public void testEvictionWithGetChildrenNames() throws Exception { 385 cache.put("/a/1", null); 386 cache.put("/a/2", null); 387 cache.put("/a/3", null); 388 cache.evict(Fqn.fromString("/a/1")); 390 cache.evict(Fqn.fromString("/a/2")); 391 cache.evict(Fqn.fromString("/a/3")); 392 cache.evict(Fqn.fromString("/a")); 393 Set children=cache.getChildrenNames("/a"); 394 assertEquals(3, children.size()); 395 assertTrue(children.contains("1")); 396 assertTrue(children.contains("2")); 397 assertTrue(children.contains("3")); 398 } 399 400 401 public void testTxPutCommit() throws Exception , NotSupportedException { 402 DummyTransactionManager mgr=DummyTransactionManager.getInstance(); 403 mgr.begin(); 404 tx=mgr.getTransaction(); 405 406 cache.put("/one/two/three", "key1", "val1"); 407 cache.put("/one/two/three/four", "key2", "val2"); 408 tx.commit(); 409 assertNotNull(cache.getKeys("/one/two/three")); 410 Set children=cache.getChildrenNames("/one"); 411 assertEquals(1, children.size()); 412 cache.remove("/"); 413 } 414 415 416 public void testTxPutRollback() throws Exception , NotSupportedException { 417 DummyTransactionManager mgr=DummyTransactionManager.getInstance(); 418 419 cache.remove("/one"); 420 421 mgr.begin(); 422 tx=mgr.getTransaction(); 423 424 cache.put("/one/two/three", "key1", "val1"); 425 cache.put("/one/two/three/four", "key2", "val2"); 426 tx.rollback(); 427 assertNull(cache.getKeys("/one/two/three")); 428 Set children=cache.getChildrenNames("/one"); 429 assertNull(children); 430 } 431 432 433 434 435 436 437 438 439 442 public void testBasicOperations() 443 throws Exception { 444 445 doTestBasicOperations(); 446 } 447 448 451 public void testBasicOperationsTransactional() 452 throws Exception { 453 454 DummyTransactionManager mgr=DummyTransactionManager.getInstance(); 455 mgr.begin(); 456 tx=mgr.getTransaction(); 457 doTestBasicOperations(); 458 tx.commit(); 459 } 460 461 464 private void doTestBasicOperations() throws Exception { 465 466 467 doPutTests(new Fqn("key")); 468 doRemoveTests(new Fqn("key")); 469 471 472 doPutTests(new Fqn("key1")); 473 doPutTests(new Fqn("key3")); 474 doPutTests(new Fqn("key2")); 475 assertEquals(4, loader.get(new Fqn("key1")).size()); 476 assertEquals(4, loader.get(new Fqn("key2")).size()); 477 assertEquals(4, loader.get(new Fqn("key3")).size()); 478 479 480 doRemoveTests(new Fqn("key2")); 481 doRemoveTests(new Fqn("key3")); 482 doRemoveTests(new Fqn("key1")); 483 assertEquals(null, loader.get(new Fqn("key1"))); 484 assertEquals(null, loader.get(new Fqn("key2"))); 485 assertEquals(null, loader.get(new Fqn("key3"))); 486 } 488 489 492 private void doPutTests(Fqn fqn) 493 throws Exception { 494 495 assertTrue(!loader.exists(fqn)); 496 497 498 Object oldVal; 499 oldVal = loader.put(fqn, "one", "two"); 500 assertNull(oldVal); 501 oldVal = loader.put(fqn, "three", "four"); 502 assertNull(oldVal); 503 assertEquals("two", loader.get(fqn, "one")); 504 assertEquals("four", loader.get(fqn, "three")); 505 oldVal = loader.put(fqn, "one", "xxx"); 506 assertEquals("two", oldVal); 507 oldVal = loader.put(fqn, "one", "two"); 508 assertEquals("xxx", oldVal); 509 510 511 Map map = loader.get(fqn); 512 assertEquals(2, map.size()); 513 assertEquals("two", map.get("one")); 514 assertEquals("four", map.get("three")); 515 516 517 map.put("five", "six"); 518 map.put("seven", "eight"); 519 loader.put(fqn, map); 520 assertEquals("six", loader.get(fqn, "five")); 521 assertEquals("eight", loader.get(fqn, "seven")); 522 assertEquals(map, loader.get(fqn)); 523 assertEquals(4, map.size()); 524 525 assertTrue(loader.exists(fqn)); 526 } 527 528 531 private void doRemoveTests(Fqn fqn) 532 throws Exception { 533 534 535 Object oldVal; 536 oldVal = loader.remove(fqn, "one"); 537 assertEquals("two", oldVal); 538 oldVal = loader.remove(fqn, "five"); 539 assertEquals("six", oldVal); 540 assertEquals(null, loader.get(fqn, "one")); 541 assertEquals(null, loader.get(fqn, "five")); 542 assertEquals("four", loader.get(fqn, "three")); 543 assertEquals("eight", loader.get(fqn, "seven")); 544 Map map = loader.get(fqn); 545 assertEquals(2, map.size()); 546 assertEquals("four", map.get("three")); 547 assertEquals("eight", map.get("seven")); 548 549 550 assertTrue(loader.exists(fqn)); 551 loader.remove(fqn); 552 assertEquals(null, loader.get(fqn, "three")); 553 assertEquals(null, loader.get(fqn, "seven")); 554 map = loader.get(fqn); 555 assertEquals(null, map); 556 assertTrue(!loader.exists(fqn)); 557 } 558 559 563 public void testMultiLevelTree() 564 throws Exception { 565 566 567 assertTrue(!loader.exists(new Fqn("key0"))); 568 loader.put(Fqn.fromString("/key0/level1/level2"), null); 569 assertTrue(loader.exists(Fqn.fromString("/key0/level1/level2"))); 570 assertTrue(loader.exists(Fqn.fromString("/key0/level1"))); 571 assertTrue(loader.exists(new Fqn("key0"))); 572 573 574 loader.put(Fqn.fromString("/key0/x/y"), null); 575 assertTrue(loader.exists(Fqn.fromString("/key0/x/y"))); 576 assertTrue(loader.exists(Fqn.fromString("/key0/x"))); 577 loader.remove(Fqn.fromString("/key0/x/y")); 578 assertTrue(!loader.exists(Fqn.fromString("/key0/x/y"))); 579 assertTrue(loader.exists(Fqn.fromString("/key0/x"))); 580 581 582 loader.remove(new Fqn("key0")); 583 assertTrue(!loader.exists(new Fqn("key0"))); 584 assertTrue(!loader.exists(Fqn.fromString("/key0/level1/level2"))); 585 assertTrue(!loader.exists(Fqn.fromString("/key0/level1"))); 586 assertTrue(!loader.exists(Fqn.fromString("/key0/x"))); 587 588 589 loader.put(new Fqn("key1"), null); 590 loader.put(new Fqn("key2"), null); 591 loader.put(new Fqn("key3"), null); 592 assertTrue(loader.exists(new Fqn("key1"))); 593 assertTrue(loader.exists(new Fqn("key2"))); 594 assertTrue(loader.exists(new Fqn("key3"))); 595 596 597 assertTrue(!loader.exists(Fqn.fromString("/key3/level1"))); 598 assertTrue(!loader.exists(Fqn.fromString("/key3/level1/level2"))); 599 loader.put(Fqn.fromString("/key3/level1/level2"), null); 600 assertTrue(loader.exists(Fqn.fromString("/key3/level1/level2"))); 601 assertTrue(loader.exists(Fqn.fromString("/key3/level1"))); 602 603 604 assertTrue(loader.exists(new Fqn("key1"))); 605 assertTrue(loader.exists(new Fqn("key2"))); 606 assertTrue(loader.exists(new Fqn("key3"))); 607 608 609 loader.remove(Fqn.fromString("/key3/level1")); 610 assertTrue(!loader.exists(Fqn.fromString("/key3/level1/level2"))); 611 assertTrue(!loader.exists(Fqn.fromString("/key3/level1"))); 612 613 614 assertTrue(loader.exists(new Fqn("key1"))); 615 assertTrue(loader.exists(new Fqn("key2"))); 616 assertTrue(loader.exists(new Fqn("key3"))); 617 618 619 loader.remove(new Fqn("key1")); 620 assertTrue(!loader.exists(new Fqn("key1"))); 621 assertTrue(loader.exists(new Fqn("key2"))); 622 assertTrue(loader.exists(new Fqn("key3"))); 623 624 625 loader.remove(new Fqn("key3")); 626 assertTrue(loader.exists(new Fqn("key2"))); 627 assertTrue(!loader.exists(new Fqn("key3"))); 628 629 630 loader.remove(new Fqn("key2")); 631 assertTrue(!loader.exists(new Fqn("key0"))); 632 assertTrue(!loader.exists(new Fqn("key1"))); 633 assertTrue(!loader.exists(new Fqn("key2"))); 634 assertTrue(!loader.exists(new Fqn("key3"))); 635 636 637 638 assertNull(loader.get(new Fqn("key0"))); 639 loader.put(Fqn.fromString("/key0/level1/level2"), "a", "b"); 640 assertNotNull(loader.get(Fqn.fromString("/key0/level1/level2"))); 641 assertNull(loader.get(Fqn.fromString("/key0/level1"))); 642 assertNull(loader.get(new Fqn("key0"))); 643 644 loader.put(Fqn.fromString("/key0/x/y"), "a", "b"); 645 assertNotNull(loader.get(Fqn.fromString("/key0/x/y"))); 646 assertNull(loader.get(Fqn.fromString("/key0/x"))); 647 loader.remove(Fqn.fromString("/key0/x/y")); 648 assertNull(loader.get(Fqn.fromString("/key0/x/y"))); 649 assertNull(loader.get(Fqn.fromString("/key0/x"))); 650 651 loader.remove(new Fqn("key0")); 652 assertNull(loader.get(new Fqn("key0"))); 653 assertNull(loader.get(Fqn.fromString("/key0/level1/level2"))); 654 assertNull(loader.get(Fqn.fromString("/key0/level1"))); 655 assertNull(loader.get(Fqn.fromString("/key0/x"))); 656 657 loader.put(new Fqn("key1"), "a", "b"); 658 loader.put(new Fqn("key2"), "a", "b"); 659 loader.put(new Fqn("key3"), "a", "b"); 660 assertNotNull(loader.get(new Fqn("key1"))); 661 assertNotNull(loader.get(new Fqn("key2"))); 662 assertNotNull(loader.get(new Fqn("key3"))); 663 664 assertNull(loader.get(Fqn.fromString("/key3/level1"))); 665 assertNull(loader.get(Fqn.fromString("/key3/level1/level2"))); 666 loader.put(Fqn.fromString("/key3/level1/level2"), "a", "b"); 667 assertNotNull(loader.get(Fqn.fromString("/key3/level1/level2"))); 668 assertNull(loader.get(Fqn.fromString("/key3/level1"))); 669 670 assertNotNull(loader.get(new Fqn("key1"))); 671 assertNotNull(loader.get(new Fqn("key2"))); 672 assertNotNull(loader.get(new Fqn("key3"))); 673 674 loader.remove(Fqn.fromString("/key3/level1")); 675 assertNull(loader.get(Fqn.fromString("/key3/level1/level2"))); 676 assertNull(loader.get(Fqn.fromString("/key3/level1"))); 677 678 assertNotNull(loader.get(new Fqn("key1"))); 679 assertNotNull(loader.get(new Fqn("key2"))); 680 assertNotNull(loader.get(new Fqn("key3"))); 681 682 loader.remove(new Fqn("key1")); 683 assertNull(loader.get(new Fqn("key1"))); 684 assertNotNull(loader.get(new Fqn("key2"))); 685 assertNotNull(loader.get(new Fqn("key3"))); 686 687 loader.remove(new Fqn("key3")); 688 assertNotNull(loader.get(new Fqn("key2"))); 689 assertNull(loader.get(new Fqn("key3"))); 690 691 loader.remove(new Fqn("key2")); 692 assertNull(loader.get(new Fqn("key0"))); 693 assertNull(loader.get(new Fqn("key1"))); 694 assertNull(loader.get(new Fqn("key2"))); 695 assertNull(loader.get(new Fqn("key3"))); 696 } 697 698 701 public void testGetChildrenNames() 702 throws Exception { 703 704 checkChildren(new Fqn(), null); 705 checkChildren(Fqn.fromString("/key0"), null); 706 707 loader.put(Fqn.fromString("/key0"), null); 708 checkChildren(new Fqn(), new String [] { "key0" }); 709 710 loader.put(Fqn.fromString("/key1/x"), null); 711 checkChildren(new Fqn(), new String [] { "key0", "key1" }); 712 checkChildren(Fqn.fromString("/key1"), new String [] { "x" }); 713 714 loader.remove(Fqn.fromString("/key1/x")); 715 checkChildren(new Fqn(), new String [] { "key0", "key1" }); 716 checkChildren(Fqn.fromString("/key0"), null); 717 checkChildren(Fqn.fromString("/key1"), null); 718 719 loader.put(Fqn.fromString("/key0/a"), null); 720 loader.put(Fqn.fromString("/key0/ab"), null); 721 loader.put(Fqn.fromString("/key0/abc"), null); 722 checkChildren(Fqn.fromString("/key0"), 723 new String [] { "a", "ab", "abc" }); 724 725 loader.put(Fqn.fromString("/key0/xxx"), null); 726 loader.put(Fqn.fromString("/key0/xx"), null); 727 loader.put(Fqn.fromString("/key0/x"), null); 728 checkChildren(Fqn.fromString("/key0"), 729 new String [] { "a", "ab", "abc", "x", "xx", "xxx" }); 730 731 loader.put(Fqn.fromString("/key0/a/1"), null); 732 loader.put(Fqn.fromString("/key0/a/2"), null); 733 loader.put(Fqn.fromString("/key0/a/2/1"), null); 734 checkChildren(Fqn.fromString("/key0/a/2"), new String [] { "1" }); 735 checkChildren(Fqn.fromString("/key0/a"), new String [] { "1", "2" }); 736 checkChildren(Fqn.fromString("/key0"), 737 new String [] { "a", "ab", "abc", "x", "xx", "xxx" }); 738 } 759 760 763 private void checkChildren(Fqn fqn, String [] names) 764 throws Exception { 765 766 Set set = loader.getChildrenNames(fqn); 767 if (names != null) { 768 assertEquals(names.length, set.size()); 769 for (int i = 0; i < names.length; i += 1) { 770 assertTrue(set.contains(names[i])); 771 } 772 } else { 773 assertNull(set); 774 } 775 } 776 777 780 public void testModifications() 781 throws Exception { 782 783 doTestModifications(); 784 } 785 786 789 public void testModificationsTransactional() 790 throws Exception { 791 DummyTransactionManager mgr=DummyTransactionManager.getInstance(); 792 mgr.begin(); 793 tx=mgr.getTransaction(); 794 doTestModifications(); 795 tx.commit(); 796 } 797 798 801 private void doTestModifications() 802 throws Exception { 803 804 805 List list = createUpdates(); 806 loader.put(list); 807 checkModifications(list); 808 809 810 list = new ArrayList(); 811 Modification mod = new Modification(); 812 mod.setType(Modification.REMOVE_KEY_VALUE); 813 mod.setFqn(FQN); 814 mod.setKey("one"); 815 list.add(mod); 816 loader.put(list); 817 checkModifications(list); 818 819 820 list = new ArrayList(); 821 mod = new Modification(); 822 mod.setType(Modification.REMOVE_NODE); 823 mod.setFqn(FQN); 824 list.add(mod); 825 loader.put(list); 826 checkModifications(list); 827 assertEquals(null, loader.get(FQN)); 828 829 830 loader.put(FQN, "one", "two"); 831 list = new ArrayList(); 832 mod = new Modification(); 833 mod.setType(Modification.REMOVE_DATA); 834 mod.setFqn(FQN); 835 list.add(mod); 836 loader.put(list); 837 checkModifications(list); 838 } 839 840 843 public void testOnePhaseTransaction() 844 throws Exception { 845 List mods = createUpdates(); 846 loader.prepare(null, mods, true); 847 checkModifications(mods); 848 } 849 850 853 public void testTwoPhaseTransaction() 854 throws Exception { 855 856 Object txnKey = new Object (); 857 List mods = createUpdates(); 858 loader.prepare(txnKey, mods, false); 859 loader.commit(txnKey); 864 checkModifications(mods); 865 } 866 867 870 public void testTransactionRollback() 871 throws Exception { 872 873 loader.remove(Fqn.fromString("/")); 874 875 int num=loader.loadEntireState().length; 876 877 Object txnKey = new Object (); 878 List mods = createUpdates(); 879 loader.prepare(txnKey, mods, false); 880 loader.rollback(txnKey); 881 assertEquals(num, loader.loadEntireState().length); 882 } 883 884 887 private List createUpdates() { 888 889 List list = new ArrayList(); 890 891 Modification mod = new Modification(); 892 mod.setType(Modification.PUT_KEY_VALUE); 893 mod.setFqn(FQN); 894 mod.setKey("one"); 895 mod.setValue("two"); 896 list.add(mod); 897 898 mod = new Modification(); 899 mod.setType(Modification.PUT_KEY_VALUE); 900 mod.setFqn(FQN); 901 mod.setKey("three"); 902 mod.setValue("four"); 903 list.add(mod); 904 905 Map map = new HashMap(); 906 map.put("five", "six"); 907 map.put("seven", "eight"); 908 mod = new Modification(); 909 mod.setType(Modification.PUT_DATA); 910 mod.setFqn(FQN); 911 mod.setData(map); 912 list.add(mod); 913 914 return list; 915 } 916 917 920 private void checkModifications(List list) 921 throws Exception { 922 923 for (int i = 0; i < list.size(); i += 1) { 924 Modification mod = (Modification) list.get(i); 925 Fqn fqn = mod.getFqn(); 926 switch (mod.getType()) { 927 case Modification.PUT_KEY_VALUE: 928 assertEquals(mod.getValue(), loader.get(fqn, mod.getKey())); 929 break; 930 case Modification.PUT_DATA: 931 Map map = mod.getData(); 932 for (Iterator iter = map.keySet().iterator(); iter.hasNext();) { 933 Object key = iter.next(); 934 assertEquals(map.get(key), loader.get(fqn, key)); 935 } 936 break; 937 case Modification.REMOVE_KEY_VALUE: 938 assertEquals(null, loader.get(fqn, mod.getKey())); 939 break; 940 case Modification.REMOVE_DATA: 941 map = loader.get(fqn); 942 assertNull(map); 943 break; 944 case Modification.REMOVE_NODE: 945 assertEquals(null, loader.get(fqn)); 946 break; 947 default: 948 fail("unknown type: " + mod); 949 break; 950 } 951 } 952 } 953 954 955 956 957 958 959 960 963 public void testNullKeysAndValues() 964 throws Exception { 965 966 loader.put(FQN, null, "x"); 967 assertEquals("x", loader.get(FQN, null)); 968 Map map = loader.get(FQN); 969 assertEquals(1, map.size()); 970 assertEquals("x", map.get(null)); 971 972 loader.put(FQN, "y", null); 973 assertEquals(null, loader.get(FQN, "y")); 974 map = loader.get(FQN); 975 assertEquals(2, map.size()); 976 assertEquals("x", map.get(null)); 977 assertEquals(null, map.get("y")); 978 979 loader.remove(FQN, null); 980 assertEquals(null, loader.get(FQN, null)); 981 assertEquals(1, loader.get(FQN).size()); 982 983 loader.remove(FQN, "y"); 984 assertEquals(null, loader.get(FQN, "y")); 985 assertNull(loader.get(FQN)); 986 987 map = new HashMap(); 988 map.put(null, null); 989 loader.put(FQN, map); 990 assertEquals(map, loader.get(FQN)); 991 992 loader.remove(FQN); 993 assertEquals(null, loader.get(FQN)); 994 995 map = new HashMap(); 996 map.put("xyz", null); 997 map.put(null, "abc"); 998 loader.put(FQN, map); 999 assertEquals(map, loader.get(FQN)); 1000 1001 loader.remove(FQN); 1002 assertEquals(null, loader.get(FQN)); 1003 } 1004 1005 1008 public void testDatabaseName() 1009 throws Exception { 1010 1011 loader.put(FQN, "one", "two"); 1012 assertEquals("two", loader.get(FQN, "one")); 1013 } 1014 1015 1018 public void testLoadAndStore() 1019 throws Exception { 1020 1021 1022 loader.remove(Fqn.fromString("/")); 1023 1030 1031 Complex c1 = new Complex(); 1032 Complex c2 = new Complex(c1); 1033 1034 1035 loader.put(FQN, new Integer (1), c1); 1036 loader.put(FQN, new Integer (2), c2); 1037 assertEquals(c1, loader.get(FQN, new Integer (1))); 1038 assertEquals(c2, loader.get(FQN, new Integer (2))); 1039 assertEquals(2, loader.get(FQN).size()); 1040 1041 1042 byte[] state = loader.loadEntireState(); 1043 assertTrue(state.length > 0); 1044 1045 1046 1047 loader.storeEntireState(state); 1048 assertEquals(c1, loader.get(FQN, new Integer (1))); 1049 assertEquals(c2, loader.get(FQN, new Integer (2))); 1050 assertEquals(2, loader.get(FQN).size()); 1051 } 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1069 private static class Complex implements Serializable { 1070 static final long serialVersionUID = 3254750788071265158L; 1071 1072 Complex nested; 1073 1074 Complex() { 1075 this(null); 1076 } 1077 1078 Complex(Complex nested) { 1079 this.nested = nested; 1080 } 1081 1082 public boolean equals(Object o) { 1083 try { 1084 Complex x = (Complex) o; 1085 return (nested != null) ? nested.equals(x.nested) 1086 : (x.nested == null); 1087 } catch (ClassCastException e) { 1088 return false; 1089 } 1090 } 1091 1092 public int hashCode() { 1093 if(nested == null) 1094 return super.hashCode(); 1095 else 1096 return 13 + nested.hashCode(); 1097 } 1098 } 1099 1100 1101 public static Test suite() { 1102 return new TestSuite(CacheLoaderTests.class); 1103 } 1104 1105 public static void main(String [] args) { 1106 junit.textui.TestRunner.run(suite()); 1107 } 1108 1109} 1110 | Popular Tags |