1 package org.jboss.cache.passivation; 2 3 import junit.framework.Test; 4 import junit.framework.TestCase; 5 import junit.framework.TestSuite; 6 import org.apache.commons.logging.Log; 7 import org.apache.commons.logging.LogFactory; 8 import org.jboss.cache.CacheException; 9 import org.jboss.cache.CacheImpl; 10 import org.jboss.cache.Fqn; 11 import org.jboss.cache.Modification; 12 import org.jboss.cache.Node; 13 import org.jboss.cache.config.CacheLoaderConfig; 14 import org.jboss.cache.factories.XmlConfigurationParser; 15 import org.jboss.cache.loader.CacheLoader; 16 import org.jboss.cache.loader.SamplePojo; 17 import org.jboss.cache.lock.IsolationLevel; 18 import org.jboss.cache.statetransfer.StateTransferManager; 19 import org.jboss.cache.transaction.DummyTransactionManager; 20 import org.jboss.cache.xml.XmlHelper; 21 import org.jboss.util.stream.MarshalledValueInputStream; 22 import org.jboss.util.stream.MarshalledValueOutputStream; 23 import org.w3c.dom.Element ; 24 25 import javax.transaction.NotSupportedException ; 26 import javax.transaction.Transaction ; 27 import java.io.ByteArrayInputStream ; 28 import java.io.ByteArrayOutputStream ; 29 import java.io.Serializable ; 30 import java.util.ArrayList ; 31 import java.util.HashMap ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 import java.util.Map ; 35 import java.util.Set ; 36 37 43 abstract public class PassivationTestsBase extends TestCase 44 { 45 46 Log log = LogFactory.getLog(getClass()); 47 48 CacheImpl cache; 50 CacheLoader loader = null; 51 Transaction tx = null; 52 static final Fqn FQN = new Fqn("key"); 53 54 55 protected CacheLoaderConfig getCacheLoaderConfig(String preload, String cacheloaderClass, String properties, boolean async, boolean fetchPersistentState) throws Exception 56 { 57 String xml = "<config>\n" + 58 "<passivation>true</passivation>\n" + 59 "<preload>" + preload + "</preload>\n" + 60 "<cacheloader>\n" + 61 "<class>" + cacheloaderClass + "</class>\n" + 62 "<properties>" + properties + "</properties>\n" + 63 "<async>" + async + "</async>\n" + 64 "<fetchPersistentState>" + fetchPersistentState + "</fetchPersistentState>\n" + 65 "</cacheloader>\n" + 66 "</config>"; 67 Element element = XmlHelper.stringToElement(xml); 68 return XmlConfigurationParser.parseCacheLoaderConfig(element); 69 } 70 71 protected void setUp() throws Exception 72 { 73 super.setUp(); 74 log.debug("Testing " + getName()); 75 log.debug(""); 76 cache = new CacheImpl(); 77 cache.getConfiguration().setCacheMode("local"); 78 configureCache(); 79 cache.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 81 cache.getConfiguration().setIsolationLevel(IsolationLevel.SERIALIZABLE); 82 cache.create(); 83 cache.start(); 84 loader = cache.getCacheLoader(); 85 } 86 87 abstract protected void configureCache() throws Exception ; 88 89 90 protected void tearDown() throws Exception 91 { 92 super.tearDown(); 93 if (tx != null) 94 { 95 try 96 { 97 tx.commit(); 98 } 99 catch (Throwable e) 100 { 101 e.printStackTrace(); 102 } 103 } 104 cache.remove("/"); 105 loader.remove(Fqn.fromString("/")); 106 cache.stop(); 107 cache.destroy(); 108 } 109 110 111 protected void addDelay() 112 { 113 ; } 115 116 public void testPrintPassivation() throws Exception 117 { 118 final Fqn NODE = Fqn.fromString("/test"); 119 final String KEY = "key"; 120 loader.remove(NODE); 121 cache.put(NODE, KEY, new Integer (10)); 122 cache.evict(NODE); 123 assertTrue(loader.exists(NODE)); 124 addDelay(); 125 log.info("print node " + NODE); 126 String ret = cache.print(NODE); 127 assertNotNull(ret); 128 log.info("loader exists " + NODE); 129 assertTrue(!loader.exists(NODE)); 130 cache.get(NODE, KEY); 131 assertFalse(loader.exists(NODE)); 132 } 133 134 public void testPutPassivation() throws Exception 135 { 136 final String NODE = "/test"; 137 final String KEY = "key"; 138 Object retval = null; 139 cache.remove(NODE); addDelay(); 141 retval = cache.put(NODE, KEY, 10); assertNull(retval); 143 retval = cache.put(NODE, KEY, 20); addDelay(); 145 assertEquals(10, retval); cache.evict(Fqn.fromString(NODE)); addDelay(); 148 log.debug("______________"); 149 retval = cache.put(NODE, KEY, 30); assertFalse(loader.exists(Fqn.fromString(NODE))); 151 assertEquals(20, retval); 152 } 153 154 public void testPut2Passivation() throws CacheException 155 { 156 final String NODE = "/a/b/c"; 157 final String KEY = "key"; 158 Object retval = null; 159 cache.remove(NODE); addDelay(); 161 retval = cache.put(NODE, KEY, 10); assertNull(retval); 163 addDelay(); 164 retval = cache.put(NODE, KEY, 20); assertEquals(10, retval); 166 cache.evict(Fqn.fromString(NODE)); cache.evict(Fqn.fromString("/a/b")); cache.evict(Fqn.fromString("/a")); addDelay(); 170 try 171 { 172 assertTrue(loader.exists(Fqn.fromString("/a/b/c"))); 173 } 174 catch (Exception e) 175 { 176 fail(e.toString()); 177 } 178 retval = cache.put(NODE, KEY, 30); try 180 { 181 assertFalse(loader.exists(Fqn.fromString(NODE))); 182 } 183 catch (Exception e) 184 { 185 fail(e.toString()); 186 } 187 assertEquals(20, retval); 188 } 189 190 191 public void testSerializationPassivation() throws CacheException 192 { 193 SamplePojo pojo = new SamplePojo(39, "Hany"); 194 pojo.getHobbies().add("Running"); 195 pojo.getHobbies().add("Beerathlon"); 196 pojo.getHobbies().add("Triathlon"); 197 cache.put("/mypojo", 322649, pojo); addDelay(); 199 assertNotNull(cache.get("/mypojo", 322649)); cache.evict(Fqn.fromString("/mypojo")); try 202 { 203 assertTrue(loader.exists(Fqn.fromString("/mypojo"))); 204 } 205 catch (Exception e) 206 { 207 fail(e.toString()); 208 } 209 SamplePojo pojo2 = (SamplePojo) cache.get("/mypojo", 322649); try 211 { 212 assertFalse(loader.exists(Fqn.fromString("/mypojo"))); 213 } 214 catch (Exception e) 215 { 216 fail(e.toString()); 217 } 218 assertNotNull(pojo2); 219 assertEquals(39, pojo2.getAge()); 220 assertEquals("Hany", pojo2.getName()); 221 assertEquals(3, pojo2.getHobbies().size()); 222 } 223 224 227 public void testPopulate() 228 { 229 try 230 { 231 Map m = new HashMap (); 232 for (int i = 0; i < 10; i++) 233 { 234 m.put("key" + i, "val" + i); 235 } 236 cache.put("/a/b/c", m); 237 cache.load("/1/2/3/4/5"); 238 cache.put("/1/2/3/4/5", null); 239 cache.put("/1/2/3/4/5/a", null); 240 cache.put("/1/2/3/4/5/b", null); 241 cache.put("/1/2/3/4/5/c", null); 242 cache.put("/1/2/3/4/5/d", null); 243 cache.put("/1/2/3/4/5/e", null); 244 cache.put("/1/2/3/4/5/d/one", null); 245 cache.put("/1/2/3/4/5/d/two", null); 246 cache.put("/1/2/3/4/5/d/three", null); 247 System.out.println("cache: " + cache); 249 250 assertTrue(cache.exists("/1/2/3/4")); 251 assertTrue(cache.exists("/a/b/c")); 252 assertFalse(cache.exists("/a/b/c/d")); 253 } 254 catch (Exception e) 255 { 256 fail(e.toString()); 257 } 258 } 259 260 261 public void testPreloadingPassivation() throws Exception 262 { 263 cache.remove("/"); cache.put("1/2/3/4/5/d", "key", "val"); cache.evict(Fqn.fromString("1/2/3/4/5/d")); System.out.println("-- checking for 1/2/3/4/5/d"); 267 addDelay(); 268 try 269 { 270 assertTrue(loader.exists(Fqn.fromString("1/2/3/4/5/d"))); 271 } 272 catch (Exception e) 273 { 274 fail(e.toString()); 275 } 276 cache.get("1/2/3/4/5/d"); assertEquals(false, loader.exists(Fqn.fromString("1/2/3/4/5/d"))); 278 assertTrue(cache.exists("1/2/3/4/5/d")); 279 System.out.println("-- 1/2/3/4/5/d exists"); 280 cache.get("1/2/3/4/5/d", "key"); assertEquals(false, loader.exists(Fqn.fromString("1/2/3/4/5/d"))); 282 } 283 284 285 public void testCacheLoading2() throws CacheException 286 { 287 Set keys = null; 288 cache.put("/a/b/c", "key", "val"); 289 try 290 { 291 keys = cache.getKeys("/a/b/c"); 292 assertNotNull(keys); 293 assertEquals(1, keys.size()); 294 } 295 catch (Exception e) 296 { 297 fail(e.toString()); 298 } 299 300 try 301 { 302 keys.add("myKey"); 303 } 304 catch (UnsupportedOperationException ex) 305 { 306 fail("unsupported operation: " + ex); 307 } 308 } 309 310 311 public void testExists() throws Exception 312 { 313 cache.put("/eins/zwei/drei", "key1", "val1"); 314 assertTrue(cache.exists("/eins/zwei/drei")); 315 assertTrue(cache.exists("/eins/zwei/drei", "key1")); 316 assertFalse(cache.exists("/eins/zwei/drei", "key2")); 317 assertFalse(cache.exists("/uno/due/tre")); 318 assertFalse(cache.exists("/une/due/tre", "key1")); 319 } 320 321 public void testGetChildren() throws Exception 322 { 323 cache.put("/d/one", null); 324 cache.put("/d/two", null); 325 cache.put("/d/three", null); 326 cache.get("/d"); 327 Set children = cache.getChildrenNames("/d"); 328 assertNotNull(children); 329 assertEquals(3, children.size()); 330 assertTrue(children.contains("one")); 331 assertTrue(children.contains("two")); 332 assertTrue(children.contains("three")); 333 } 334 335 336 public void testGetChildrenWithEvictionPassivation() throws Exception 337 { 338 cache.put("/a/b/c/1", null); 339 cache.put("/a/b/c/2", null); 340 cache.put("/a/b/c/3", null); 341 cache.evict(Fqn.fromString("/a/b/c/1")); cache.evict(Fqn.fromString("/a/b/c/2")); cache.evict(Fqn.fromString("/a/b/c/3")); cache.evict(Fqn.fromString("/a/b/c")); cache.evict(Fqn.fromString("/a/b")); cache.evict(Fqn.fromString("/a")); cache.evict(Fqn.fromString("/")); addDelay(); 349 Set children = cache.getChildrenNames("/a/b/c"); assertNotNull(children); 351 assertEquals(3, children.size()); 352 assertTrue(children.contains("1")); 353 assertTrue(children.contains("2")); 354 assertTrue(children.contains("3")); 355 356 assertTrue(loader.exists(Fqn.fromString("/a/b/c"))); 357 358 cache.get("/a/b/c/1", "test"); cache.get("/a/b/c/2", "test"); cache.get("/a/b/c/3", "test"); cache.get("/a/b/c", "test"); 363 assertFalse(loader.exists(Fqn.fromString("/a/b/c/1"))); 364 assertFalse(loader.exists(Fqn.fromString("/a/b/c/2"))); 365 assertFalse(loader.exists(Fqn.fromString("/a/b/c/3"))); 366 assertFalse(loader.exists(Fqn.fromString("/a/b/c"))); 367 } 368 369 public void testGetChildren2() 370 { 371 try 372 { 373 cache.put("/1", null); 374 cache.put("a", null); 375 Set children = cache.getChildrenNames("/"); assertNotNull(children); 377 assertEquals(2, children.size()); 378 assertTrue(children.contains("1")); 379 assertTrue(children.contains("a")); 380 } 381 catch (Exception e) 382 { 383 fail(e.toString()); 384 } 385 } 386 387 public void testGetChildren3() 388 { 389 try 390 { 391 cache.put("/1", null); 392 cache.put("a", null); 393 Set children = cache.getChildrenNames(""); assertNotNull(children); 395 assertEquals(2, children.size()); 396 assertTrue(children.contains("1")); 397 assertTrue(children.contains("a")); 398 } 399 catch (Exception e) 400 { 401 fail(e.toString()); 402 } 403 } 404 405 public void testGetChildren4() 406 { 407 try 408 { 409 if (!cache.exists("/a/b/c")) 410 { 411 cache.put("/a/b/c", null); 412 } 413 Set children = cache.getChildrenNames((Fqn) null); assertTrue(children.isEmpty()); 415 } 416 catch (Exception e) 417 { 418 fail(e.toString()); 419 } 420 } 421 422 423 public void testGetChildren5() 424 { 425 try 426 { 427 cache.put("/a/1", null); 428 cache.put("/a/2", null); 429 cache.put("/a/3", null); 430 System.out.println("cache is " + cache.printLockInfo()); 431 432 Node n = cache.get("/a"); 433 assertNotNull(n); 434 435 Set children = cache.getChildrenNames("/a"); 436 assertNotNull(children); 437 assertEquals(3, children.size()); 438 } 439 catch (Exception e) 440 { 441 fail(e.toString()); 442 } 443 } 444 445 446 public void testGetChildren6Passivation() throws Exception 447 { 448 cache.put("/a/1", null); cache.put("/a/2", null); cache.put("/a/3", null); System.out.println("cache is " + cache.printLockInfo()); 452 cache.evict(Fqn.fromString("/a/1")); cache.evict(Fqn.fromString("/a/2")); cache.evict(Fqn.fromString("/a/3")); cache.evict(Fqn.fromString("/a")); assertTrue(loader.exists(Fqn.fromString("/a"))); 457 System.out.println("cache is " + cache.printLockInfo()); 458 addDelay(); 459 assertNotNull(cache.get("/a")); assertTrue(loader.exists(Fqn.fromString("/a"))); Set children = cache.getChildrenNames("/a"); 462 assertNotNull("No children were loaded", children); 463 System.out.println("children: " + children); 464 assertEquals("3 children weren't loaded", 3, children.size()); 465 cache.get("/a/1", "test"); assertFalse(loader.exists(Fqn.fromString("/a/1"))); 467 cache.get("/a/2", "test"); assertFalse(loader.exists(Fqn.fromString("/a/2"))); 469 cache.get("/a/3", "test"); assertFalse(loader.exists(Fqn.fromString("/a/3"))); 471 cache.get("/a", "test"); assertFalse(loader.exists(Fqn.fromString("/a"))); 473 } 474 475 public void testGetChildren7Passivation() throws Exception 476 { 477 cache.put("/a/1", null); 478 cache.put("/a/2", null); 479 cache.put("/a/3", null); 480 cache.put("/a", "test", "test"); 481 System.out.println("cache is " + cache.printLockInfo()); 482 cache.evict(Fqn.fromString("/a/1")); cache.evict(Fqn.fromString("/a/2")); cache.evict(Fqn.fromString("/a/3")); cache.evict(Fqn.fromString("/a")); assertTrue(loader.exists(Fqn.fromString("/a"))); 487 System.out.println("cache is " + cache.printLockInfo()); 488 addDelay(); 489 Object val = cache.get("/a", "test"); assertEquals("attributes weren't loaded", "test", val); 491 492 Set children = cache.getChildrenNames("/a"); assertNotNull("No children were loaded", children); 494 System.out.println("children: " + children); 495 assertEquals("3 children weren't loaded", 3, children.size()); 496 cache.get("/a/1", "test"); assertFalse(loader.exists(Fqn.fromString("/a/1"))); 498 cache.get("/a/2", "test"); assertFalse(loader.exists(Fqn.fromString("/a/2"))); 500 cache.get("/a/3", "test"); assertFalse(loader.exists(Fqn.fromString("/a/3"))); 502 assertTrue(loader.exists(Fqn.fromString("/a"))); 503 } 504 505 public void testGetChildren8Passivation() throws Exception 506 { 507 cache.put("/a/1", null); 508 cache.put("/a/2", null); 509 cache.put("/a/3", null); 510 System.out.println("cache is " + cache.printLockInfo()); 511 cache.evict(Fqn.fromString("/a/1")); cache.evict(Fqn.fromString("/a/2")); cache.evict(Fqn.fromString("/a/3")); cache.evict(Fqn.fromString("/a")); 516 System.out.println("cache is " + cache.printLockInfo()); 517 addDelay(); 518 assertNull(cache.get("/a", "test")); assertTrue(loader.exists(Fqn.fromString("/a"))); 521 assertNull(cache.get("/a/1", "test")); assertFalse(loader.exists(Fqn.fromString("/a/1"))); Set children = cache.getChildrenNames("/a"); assertNotNull("No children were loaded", children); 525 System.out.println("children: " + children); 526 assertEquals("3 children weren't loaded", 3, children.size()); 527 assertTrue(loader.exists(Fqn.fromString("/a"))); } 529 530 public void testGetChildren9Passivation() throws Exception 531 { 532 cache.put("/a/1", null); 533 cache.put("/a/2", null); 534 cache.put("/a/3", null); 535 System.out.println("cache is " + cache.printLockInfo()); 536 cache.evict(Fqn.fromString("/a/1")); cache.evict(Fqn.fromString("/a/2")); cache.evict(Fqn.fromString("/a/3")); cache.evict(Fqn.fromString("/a")); assertTrue(loader.exists(Fqn.fromString("/a"))); 541 System.out.println("cache is " + cache.printLockInfo()); 542 addDelay(); 543 544 cache.get("/a/1", "test"); assertFalse(loader.exists(Fqn.fromString("/a/1"))); 546 cache.get("/a/2", "test"); assertFalse(loader.exists(Fqn.fromString("/a/2"))); 548 cache.get("/a/3", "test"); assertFalse(loader.exists(Fqn.fromString("/a/3"))); 550 Set children = cache.getChildrenNames("/a"); assertNotNull("No children were loaded", children); 552 System.out.println("children: " + children); 553 assertEquals("3 children weren't loaded", 3, children.size()); 554 assertNull(cache.get("/a", "test")); assertFalse(loader.exists(Fqn.fromString("/a"))); 556 557 cache.evict(Fqn.fromString("/a/1")); cache.evict(Fqn.fromString("/a/2")); cache.evict(Fqn.fromString("/a/3")); cache.evict(Fqn.fromString("/a")); System.out.println("cache is " + cache.printLockInfo()); 562 assertTrue(loader.exists(Fqn.fromString("/a"))); 563 564 cache.get("/a/1", "test"); assertFalse(loader.exists(Fqn.fromString("/a/1"))); 566 cache.get("/a/2", "test"); assertFalse(loader.exists(Fqn.fromString("/a/2"))); 568 cache.get("/a/3", "test"); assertFalse(loader.exists(Fqn.fromString("/a/3"))); 570 children = cache.getChildrenNames("/a"); assertNotNull("No children were loaded", children); 572 System.out.println("children: " + children); 573 assertEquals("3 children weren't loaded", 3, children.size()); 574 assertNull(cache.get("/a", "test")); assertFalse(loader.exists(Fqn.fromString("/a"))); 576 } 577 578 579 public void testGetChildren10Passivation() throws Exception 580 { 581 cache.put("/a/1", null); 582 cache.put("/a/2", null); 583 cache.put("/a/3", null); 584 System.out.println("cache is " + cache.printLockInfo()); 585 cache.evict(Fqn.fromString("/a/1")); cache.evict(Fqn.fromString("/a/2")); cache.evict(Fqn.fromString("/a/3")); cache.evict(Fqn.fromString("/a")); System.out.println("cache is " + cache.printLockInfo()); 590 addDelay(); 591 assertTrue(loader.exists(Fqn.fromString("/a"))); 592 assertNull(cache.get("/a", "test")); 594 cache.get("/a/1", "test"); assertFalse(loader.exists(Fqn.fromString("/a/1"))); 596 cache.get("/a/2", "test"); assertFalse(loader.exists(Fqn.fromString("/a/2"))); 598 cache.get("/a/3", "test"); assertFalse(loader.exists(Fqn.fromString("/a/3"))); 600 Set children = cache.getChildrenNames("/a"); 601 assertNotNull("No children were loaded", children); 602 System.out.println("children: " + children); 603 assertEquals("3 children weren't loaded", 3, children.size()); 604 605 assertNull(cache.get("/a", "test")); assertFalse(loader.exists(Fqn.fromString("/a"))); 607 } 608 609 public void testRemoveData() throws Exception 610 { 611 String key = "/x/y/z/"; 612 cache.put(key, "keyA", "valA"); 613 cache.put(key, "keyB", "valB"); 614 cache.put(key, "keyC", "valC"); 615 assertEquals(3, cache.getKeys(key).size()); 616 cache.removeData(key); 617 Set keys = cache.getKeys(key); 618 assertEquals(0, keys.size()); 619 cache.remove("/x"); 620 Object val = cache.get(key, "keyA"); 621 assertNull(val); 622 } 623 624 625 public void testRemoveData2Passivation() throws Exception 626 { 627 Set keys; 628 Fqn key = Fqn.fromString("/x/y/z/"); 629 cache.put(key, "keyA", "valA"); 630 cache.put(key, "keyB", "valB"); 631 cache.put(key, "keyC", "valC"); 632 addDelay(); 633 keys = cache.getKeys(key); 634 assertEquals(3, keys.size()); 635 cache.removeData(key); 636 cache.evict(key); 638 addDelay(); 639 keys = cache.getKeys(key); assertFalse(loader.exists(key)); 641 assertEquals(0, keys.size()); 642 } 643 644 public void testRemoveData3Passivation() throws Exception 645 { 646 Set keys; 647 Fqn key = Fqn.fromString("/x/y/z/"); 648 cache.put(key, "keyA", "valA"); 649 cache.put(key, "keyB", "valB"); 650 cache.put(key, "keyC", "valC"); 651 keys = cache.getKeys(key); 652 assertEquals(3, keys.size()); 653 cache.evict(key); assertTrue(loader.exists(key)); 655 cache.removeData(key); 656 keys = cache.getKeys(key); assertFalse(loader.exists(key)); 658 assertEquals(0, keys.size()); 659 } 660 661 public void testRemoveKey() throws Exception 662 { 663 String key = "/x/y/z/"; 664 cache.put(key, "keyA", "valA"); 665 cache.put(key, "keyB", "valB"); 666 cache.put(key, "keyC", "valC"); 667 cache.remove(key, "keyA"); 668 assertEquals(2, cache.getKeys(key).size()); 669 cache.remove("/x"); 670 } 671 672 673 public void testRemoveKey2() throws CacheException 674 { 675 final String NODE = "/test"; 676 final String KEY = "key"; 677 Object retval = null; 678 cache.remove(NODE); 679 retval = cache.put(NODE, KEY, 10); 680 assertNull(retval); 681 addDelay(); 682 retval = cache.remove(NODE, KEY); 683 assertEquals(10, retval); 684 addDelay(); 685 retval = cache.remove(NODE, KEY); 686 assertNull(retval); 687 } 688 689 public void testRemoveKey3Passivation() throws Exception 690 { 691 final String NODE = "/test"; 692 final String KEY = "key"; 693 Object retval = null; 694 cache.remove(NODE); 695 retval = cache.put(NODE, KEY, 10); 696 assertNull(retval); 697 698 cache.evict(Fqn.fromString(NODE)); addDelay(); 700 assertTrue(loader.exists(Fqn.fromString(NODE))); 701 assertEquals(10, loader.get(Fqn.fromString(NODE)).get(KEY)); 702 retval = cache.remove(NODE, KEY); assertEquals(10, retval); 704 assertFalse(loader.exists(Fqn.fromString(NODE))); 705 706 cache.evict(Fqn.fromString(NODE)); addDelay(); 708 retval = cache.remove(NODE, KEY); assertFalse(loader.exists(Fqn.fromString(NODE))); 710 assertNull(retval); 711 } 712 713 714 public void testRemove() throws Exception 715 { 716 String key = "/x/y/z/"; 717 cache.put(key, "keyA", "valA"); 718 cache.put(key, "keyB", "valB"); 719 cache.put(key, "keyC", "valC"); 720 cache.remove("/x"); 721 assertNull(cache.get(key, "keyA")); 722 addDelay(); 723 Set keys = cache.getKeys(key); 724 assertNull(keys); 725 cache.remove("/x"); 726 } 727 728 729 public void testRemoveRoot() throws Exception 730 { 731 assertEquals(0, cache.getKeys("/").size()); 732 cache.put("/1/2/3/4/5", null); 733 cache.put("uno/due/tre", null); 734 cache.put("1/2/3/a", null); 735 cache.put("/eins/zwei/drei", null); 736 cache.put("/one/two/three", null); 737 cache.remove("/"); 738 assertEquals(0, cache.getKeys("/").size()); 739 } 740 741 742 public void testEvictionWithCacheLoaderPassivation() throws Exception 743 { 744 cache.put("/first/second", "key1", "val1"); 745 cache.put("/first/second/third", "key2", "val2"); 746 cache.evict(Fqn.fromString("/first/second")); addDelay(); 748 assertTrue(loader.exists(Fqn.fromString("/first/second"))); 749 assertTrue(cache.exists("/first")); 750 String val = (String ) cache.get("/first/second", "key1"); 751 assertTrue(loader.exists(Fqn.fromString("/first/second"))); 752 assertEquals("val1", val); 753 String val2 = (String ) cache.get("/first/second/third", "key2"); assertFalse(loader.exists(Fqn.fromString("/first/second/third"))); 755 assertEquals("val2", val2); 756 assertTrue(cache.exists("/first/second/third")); 757 assertTrue(cache.exists("/first/second")); 758 assertTrue(cache.exists("/first")); 759 } 760 761 762 public void testEvictionWithCacheLoaderPassivation2() throws Exception 763 { 764 cache.put("/first/second/third", "key1", "val1"); cache.evict(Fqn.fromString("/first/second/third")); addDelay(); 767 assertTrue(loader.exists(Fqn.fromString("/first/second/third"))); 768 assertTrue(cache.exists("/first/second")); 769 assertTrue(cache.exists("/first")); 770 String val = (String ) cache.get("/first/second/third", "key1"); assertFalse(loader.exists(Fqn.fromString("/first/second/third"))); 772 assertEquals("val1", val); 773 assertTrue(cache.exists("/first/second/third")); 774 assertTrue(cache.exists("/first/second")); 775 assertTrue(cache.exists("/first")); 776 } 777 778 779 public void testEvictionWithGetChildrenNamesPassivation() throws Exception 780 { 781 cache.put("/a/1", null); 782 cache.put("/a/2", null); 783 cache.put("/a/3", null); 784 cache.evict(Fqn.fromString("/a/1")); assertTrue(loader.exists(Fqn.fromString("/a/1"))); 786 cache.evict(Fqn.fromString("/a/2")); assertTrue(loader.exists(Fqn.fromString("/a/2"))); 788 cache.evict(Fqn.fromString("/a/3")); assertTrue(loader.exists(Fqn.fromString("/a/3"))); 790 cache.evict(Fqn.fromString("/a")); assertTrue(loader.exists(Fqn.fromString("/a"))); 792 addDelay(); 793 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 794 mgr.begin(); 795 tx = mgr.getTransaction(); 796 Set children = cache.getChildrenNames("/a"); 797 assertEquals(3, children.size()); 798 assertTrue(children.contains("1")); 799 assertTrue(children.contains("2")); 800 assertTrue(children.contains("3")); 801 assertEquals(5, cache.getNumberOfLocksHeld()); 802 tx.commit(); 803 } 804 805 806 public void testTxPutCommit() throws Exception , NotSupportedException 807 { 808 809 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 810 mgr.begin(); 811 tx = mgr.getTransaction(); 812 813 cache.put("/one/two/three", "key1", "val1"); 814 cache.put("/one/two/three/four", "key2", "val2"); 815 816 tx.commit(); 817 818 assertNotNull(cache.getKeys("/one/two/three")); 819 assertEquals("val1", cache.get(Fqn.fromString("/one/two/three"), "key1")); 820 mgr.begin(); 821 tx = mgr.getTransaction(); 822 823 cache.evict(Fqn.fromString("/one/two/three")); 824 cache.evict(Fqn.fromString("/one/two/three/four")); 825 826 tx.commit(); 827 assertTrue(loader.exists(Fqn.fromString("/one/two/three"))); 828 assertTrue(loader.exists(Fqn.fromString("/one/two/three/four"))); 829 assertNotNull(cache.getKeys("/one/two/three")); 830 Set children = cache.getChildrenNames("/one"); 831 assertEquals(1, children.size()); 832 cache.remove("/"); 833 } 834 835 836 public void testTxPutRollback() throws Exception 837 { 838 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 839 840 cache.remove("/one"); 841 addDelay(); 842 mgr.begin(); 843 tx = mgr.getTransaction(); 844 845 cache.put("/one/two/three", "key1", "val1"); 846 cache.put("/one/two/three/four", "key2", "val2"); 847 tx.rollback(); 848 addDelay(); 849 assertNull(cache.getKeys("/one/two/three")); 850 Set children = cache.getChildrenNames("/one"); 851 assertTrue(children.isEmpty()); 852 assertFalse(loader.exists(Fqn.fromString("/one/two/three"))); 853 assertFalse(loader.exists(Fqn.fromString("/one/two/three/four"))); 854 } 855 856 857 public void testPassivationAndActivation() throws Exception 858 { 859 Object val, val2; 860 Fqn NODE = Fqn.fromString("/test"); 861 loader.remove(Fqn.fromString("/")); 862 cache.put(NODE, "key", "val"); 863 assertNull("value cannot be passivated yet (only on eviction)", loader.get(NODE)); 865 cache.evict(NODE); 866 assertEquals(0, cache.getNumberOfNodes()); 867 assertEquals(0, cache.getNumberOfAttributes()); 868 val = loader.get(NODE).get("key"); 869 assertNotNull("value must have been passivated on evict()", val); 870 assertEquals(val, "val"); 871 val2 = cache.get(NODE, "key"); 872 assertNotNull(val2); 873 assertEquals(val, val2); 874 assertNull("value should have been deleted from store on activation", loader.get(NODE)); 876 } 877 878 879 882 public void testBasicOperations() 883 throws Exception 884 { 885 886 doTestBasicOperations(); 887 } 888 889 892 public void testBasicOperationsTransactional() 893 throws Exception 894 { 895 896 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 897 mgr.begin(); 898 tx = mgr.getTransaction(); 899 doTestBasicOperations(); 900 tx.commit(); 901 } 902 903 906 private void doTestBasicOperations() throws Exception 907 { 908 909 910 doPutTests(new Fqn("key")); 911 doRemoveTests(new Fqn("key")); 912 914 915 doPutTests(new Fqn("key1")); 916 doPutTests(new Fqn("key3")); 917 doPutTests(new Fqn("key2")); 918 assertEquals(4, loader.get(new Fqn("key1")).size()); 919 assertEquals(4, loader.get(new Fqn("key2")).size()); 920 assertEquals(4, loader.get(new Fqn("key3")).size()); 921 922 923 doRemoveTests(new Fqn("key2")); 924 doRemoveTests(new Fqn("key3")); 925 doRemoveTests(new Fqn("key1")); 926 assertEquals(null, loader.get(new Fqn("key1"))); 927 assertEquals(null, loader.get(new Fqn("key2"))); 928 assertEquals(null, loader.get(new Fqn("key3"))); 929 } 931 932 935 private void doPutTests(Fqn fqn) 936 throws Exception 937 { 938 939 assertTrue(!loader.exists(fqn)); 940 941 942 Object oldVal; 943 oldVal = loader.put(fqn, "one", "two"); 944 assertNull(oldVal); 945 addDelay(); 946 oldVal = loader.put(fqn, "three", "four"); 947 assertNull(oldVal); 948 addDelay(); 949 assertEquals("two", loader.get(fqn).get("one")); 950 assertEquals("four", loader.get(fqn).get("three")); 951 addDelay(); 952 oldVal = loader.put(fqn, "one", "xxx"); 953 assertEquals("two", oldVal); 954 addDelay(); 955 oldVal = loader.put(fqn, "one", "two"); 956 assertEquals("xxx", oldVal); 957 958 959 addDelay(); 960 Map map = loader.get(fqn); 961 assertEquals(2, map.size()); 962 assertEquals("two", map.get("one")); 963 assertEquals("four", map.get("three")); 964 965 Map map2 = new HashMap (map); 966 967 map2.put("five", "six"); 968 map2.put("seven", "eight"); 969 loader.put(fqn, map2); 970 addDelay(); 971 assertEquals("six", loader.get(fqn).get("five")); 972 assertEquals("eight", loader.get(fqn).get("seven")); 973 assertEquals(map2, loader.get(fqn)); 974 assertEquals(4, map2.size()); 975 976 assertTrue(loader.exists(fqn)); 977 } 978 979 982 private void doRemoveTests(Fqn fqn) 983 throws Exception 984 { 985 986 987 Object oldVal; 988 oldVal = loader.remove(fqn, "one"); 989 assertEquals("two", oldVal); 990 addDelay(); 991 oldVal = loader.remove(fqn, "five"); 992 assertEquals("six", oldVal); 993 addDelay(); 994 assertEquals(null, loader.get(fqn).get("one")); 995 assertEquals(null, loader.get(fqn).get("five")); 996 assertEquals("four", loader.get(fqn).get("three")); 997 assertEquals("eight", loader.get(fqn).get("seven")); 998 Map map = loader.get(fqn); 999 assertEquals(2, map.size()); 1000 assertEquals("four", map.get("three")); 1001 assertEquals("eight", map.get("seven")); 1002 1003 1004 assertTrue(loader.exists(fqn)); 1005 loader.remove(fqn); 1006 addDelay(); 1007 assertNull(loader.get(fqn)); 1008 assertTrue(!loader.exists(fqn)); 1009 } 1010 1011 1015 public void testMultiLevelTree() 1016 throws Exception 1017 { 1018 1019 1020 assertTrue(!loader.exists(new Fqn("key0"))); 1021 loader.put(Fqn.fromString("/key0/level1/level2"), null); 1022 addDelay(); 1023 assertTrue(loader.exists(Fqn.fromString("/key0/level1/level2"))); 1024 assertTrue(loader.exists(Fqn.fromString("/key0/level1"))); 1025 assertTrue(loader.exists(new Fqn("key0"))); 1026 1027 1028 loader.put(Fqn.fromString("/key0/x/y"), null); 1029 addDelay(); 1030 assertTrue(loader.exists(Fqn.fromString("/key0/x/y"))); 1031 assertTrue(loader.exists(Fqn.fromString("/key0/x"))); 1032 loader.remove(Fqn.fromString("/key0/x/y")); 1033 addDelay(); 1034 assertTrue(!loader.exists(Fqn.fromString("/key0/x/y"))); 1035 assertTrue(loader.exists(Fqn.fromString("/key0/x"))); 1036 1037 1038 loader.remove(new Fqn("key0")); 1039 addDelay(); 1040 assertTrue(!loader.exists(new Fqn("key0"))); 1041 assertTrue(!loader.exists(Fqn.fromString("/key0/level1/level2"))); 1042 assertTrue(!loader.exists(Fqn.fromString("/key0/level1"))); 1043 assertTrue(!loader.exists(Fqn.fromString("/key0/x"))); 1044 1045 1046 loader.put(new Fqn("key1"), null); 1047 loader.put(new Fqn("key2"), null); 1048 loader.put(new Fqn("key3"), null); 1049 addDelay(); 1050 assertTrue(loader.exists(new Fqn("key1"))); 1051 assertTrue(loader.exists(new Fqn("key2"))); 1052 assertTrue(loader.exists(new Fqn("key3"))); 1053 1054 1055 assertTrue(!loader.exists(Fqn.fromString("/key3/level1"))); 1056 assertTrue(!loader.exists(Fqn.fromString("/key3/level1/level2"))); 1057 loader.put(Fqn.fromString("/key3/level1/level2"), null); 1058 addDelay(); 1059 assertTrue(loader.exists(Fqn.fromString("/key3/level1/level2"))); 1060 assertTrue(loader.exists(Fqn.fromString("/key3/level1"))); 1061 1062 1063 assertTrue(loader.exists(new Fqn("key1"))); 1064 assertTrue(loader.exists(new Fqn("key2"))); 1065 assertTrue(loader.exists(new Fqn("key3"))); 1066 1067 1068 loader.remove(Fqn.fromString("/key3/level1")); 1069 addDelay(); 1070 assertTrue(!loader.exists(Fqn.fromString("/key3/level1/level2"))); 1071 assertTrue(!loader.exists(Fqn.fromString("/key3/level1"))); 1072 1073 1074 assertTrue(loader.exists(new Fqn("key1"))); 1075 assertTrue(loader.exists(new Fqn("key2"))); 1076 assertTrue(loader.exists(new Fqn("key3"))); 1077 1078 1079 loader.remove(new Fqn("key1")); 1080 addDelay(); 1081 assertTrue(!loader.exists(new Fqn("key1"))); 1082 assertTrue(loader.exists(new Fqn("key2"))); 1083 assertTrue(loader.exists(new Fqn("key3"))); 1084 1085 1086 loader.remove(new Fqn("key3")); 1087 addDelay(); 1088 assertTrue(loader.exists(new Fqn("key2"))); 1089 assertTrue(!loader.exists(new Fqn("key3"))); 1090 1091 1092 loader.remove(new Fqn("key2")); 1093 addDelay(); 1094 assertTrue(!loader.exists(new Fqn("key0"))); 1095 assertTrue(!loader.exists(new Fqn("key1"))); 1096 assertTrue(!loader.exists(new Fqn("key2"))); 1097 assertTrue(!loader.exists(new Fqn("key3"))); 1098 1099 1100 1101 assertNull(loader.get(new Fqn("key0"))); 1102 loader.put(Fqn.fromString("/key0/level1/level2"), "a", "b"); 1103 addDelay(); 1104 assertNotNull(loader.get(Fqn.fromString("/key0/level1/level2"))); 1105 assertNotNull(loader.get(Fqn.fromString("/key0/level1"))); 1106 assertTrue(loader.get(Fqn.fromString("/key0/level1")).isEmpty()); 1107 assertNotNull(loader.get(new Fqn("key0"))); 1108 assertTrue(loader.get(Fqn.fromString("/key0")).isEmpty()); 1109 1110 loader.put(Fqn.fromString("/key0/x/y"), "a", "b"); 1111 addDelay(); 1112 assertNotNull(loader.get(Fqn.fromString("/key0/x/y"))); 1113 assertNotNull(loader.get(Fqn.fromString("/key0/x"))); 1114 assertTrue(loader.get(Fqn.fromString("/key0/x")).isEmpty()); 1115 loader.remove(Fqn.fromString("/key0/x/y")); 1116 addDelay(); 1117 assertNull(loader.get(Fqn.fromString("/key0/x/y"))); 1118 assertNotNull(loader.get(Fqn.fromString("/key0/x"))); 1119 assertTrue(loader.get(Fqn.fromString("/key0/x")).isEmpty()); 1120 1121 loader.remove(new Fqn("key0")); 1122 addDelay(); 1123 assertNull(loader.get(new Fqn("key0"))); 1124 assertNull(loader.get(Fqn.fromString("/key0/level1/level2"))); 1125 assertNull(loader.get(Fqn.fromString("/key0/level1"))); 1126 assertNull(loader.get(Fqn.fromString("/key0/x"))); 1127 1128 loader.put(new Fqn("key1"), "a", "b"); 1129 loader.put(new Fqn("key2"), "a", "b"); 1130 loader.put(new Fqn("key3"), "a", "b"); 1131 addDelay(); 1132 assertNotNull(loader.get(new Fqn("key1"))); 1133 assertNotNull(loader.get(new Fqn("key2"))); 1134 assertNotNull(loader.get(new Fqn("key3"))); 1135 1136 assertNull(loader.get(Fqn.fromString("/key3/level1"))); 1137 assertNull(loader.get(Fqn.fromString("/key3/level1/level2"))); 1138 loader.put(Fqn.fromString("/key3/level1/level2"), "a", "b"); 1139 addDelay(); 1140 assertNotNull(loader.get(Fqn.fromString("/key3/level1/level2"))); 1141 assertNotNull(loader.get(Fqn.fromString("/key3/level1"))); 1142 assertTrue(loader.get(Fqn.fromString("/key3/level1")).isEmpty()); 1143 1144 assertNotNull(loader.get(new Fqn("key1"))); 1145 assertNotNull(loader.get(new Fqn("key2"))); 1146 assertNotNull(loader.get(new Fqn("key3"))); 1147 1148 loader.remove(Fqn.fromString("/key3/level1")); 1149 addDelay(); 1150 assertNull(loader.get(Fqn.fromString("/key3/level1/level2"))); 1151 assertNull(loader.get(Fqn.fromString("/key3/level1"))); 1152 1153 assertNotNull(loader.get(new Fqn("key1"))); 1154 assertNotNull(loader.get(new Fqn("key2"))); 1155 assertNotNull(loader.get(new Fqn("key3"))); 1156 1157 loader.remove(new Fqn("key1")); 1158 addDelay(); 1159 assertNull(loader.get(new Fqn("key1"))); 1160 assertNotNull(loader.get(new Fqn("key2"))); 1161 assertNotNull(loader.get(new Fqn("key3"))); 1162 1163 loader.remove(new Fqn("key3")); 1164 addDelay(); 1165 assertNotNull(loader.get(new Fqn("key2"))); 1166 assertNull(loader.get(new Fqn("key3"))); 1167 1168 loader.remove(new Fqn("key2")); 1169 addDelay(); 1170 assertNull(loader.get(new Fqn("key0"))); 1171 assertNull(loader.get(new Fqn("key1"))); 1172 assertNull(loader.get(new Fqn("key2"))); 1173 assertNull(loader.get(new Fqn("key3"))); 1174 } 1175 1176 1179 public void testGetChildrenNames() 1180 throws Exception 1181 { 1182 1183 checkChildren(new Fqn(), null); 1184 checkChildren(Fqn.fromString("/key0"), null); 1185 1186 loader.put(Fqn.fromString("/key0"), null); 1187 addDelay(); 1188 checkChildren(new Fqn(), new String []{"key0"}); 1189 1190 loader.put(Fqn.fromString("/key1/x"), null); 1191 addDelay(); 1192 checkChildren(new Fqn(), new String []{"key0", "key1"}); 1193 checkChildren(Fqn.fromString("/key1"), new String []{"x"}); 1194 1195 loader.remove(Fqn.fromString("/key1/x")); 1196 addDelay(); 1197 checkChildren(new Fqn(), new String []{"key0", "key1"}); 1198 checkChildren(Fqn.fromString("/key0"), null); 1199 checkChildren(Fqn.fromString("/key1"), null); 1200 1201 loader.put(Fqn.fromString("/key0/a"), null); 1202 loader.put(Fqn.fromString("/key0/ab"), null); 1203 loader.put(Fqn.fromString("/key0/abc"), null); 1204 addDelay(); 1205 checkChildren(Fqn.fromString("/key0"), 1206 new String []{"a", "ab", "abc"}); 1207 1208 loader.put(Fqn.fromString("/key0/xxx"), null); 1209 loader.put(Fqn.fromString("/key0/xx"), null); 1210 loader.put(Fqn.fromString("/key0/x"), null); 1211 addDelay(); 1212 checkChildren(Fqn.fromString("/key0"), 1213 new String []{"a", "ab", "abc", "x", "xx", "xxx"}); 1214 1215 loader.put(Fqn.fromString("/key0/a/1"), null); 1216 loader.put(Fqn.fromString("/key0/a/2"), null); 1217 loader.put(Fqn.fromString("/key0/a/2/1"), null); 1218 addDelay(); 1219 checkChildren(Fqn.fromString("/key0/a/2"), new String []{"1"}); 1220 checkChildren(Fqn.fromString("/key0/a"), new String []{"1", "2"}); 1221 checkChildren(Fqn.fromString("/key0"), 1222 new String []{"a", "ab", "abc", "x", "xx", "xxx"}); 1223 } 1244 1245 1248 private void checkChildren(Fqn fqn, String [] names) 1249 throws Exception 1250 { 1251 1252 Set set = loader.getChildrenNames(fqn); 1253 if (names != null) 1254 { 1255 assertEquals(names.length, set.size()); 1256 for (int i = 0; i < names.length; i += 1) 1257 { 1258 assertTrue(set.contains(names[i])); 1259 } 1260 } 1261 else 1262 { 1263 assertNull(set); 1264 } 1265 } 1266 1267 1270 public void testModifications() 1271 throws Exception 1272 { 1273 1274 doTestModifications(); 1275 } 1276 1277 1280 public void testModificationsTransactional() 1281 throws Exception 1282 { 1283 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 1284 mgr.begin(); 1285 tx = mgr.getTransaction(); 1286 doTestModifications(); 1287 tx.commit(); 1288 } 1289 1290 1293 private void doTestModifications() 1294 throws Exception 1295 { 1296 1297 1298 List list = createUpdates(); 1299 loader.put(list); 1300 addDelay(); 1301 checkModifications(list); 1302 1303 1304 list = new ArrayList (); 1305 Modification mod = new Modification(); 1306 mod.setType(Modification.ModificationType.REMOVE_KEY_VALUE); 1307 mod.setFqn(FQN); 1308 mod.setKey("one"); 1309 list.add(mod); 1310 loader.put(list); 1311 addDelay(); 1312 checkModifications(list); 1313 1314 1315 list = new ArrayList (); 1316 mod = new Modification(); 1317 mod.setType(Modification.ModificationType.REMOVE_NODE); 1318 mod.setFqn(FQN); 1319 list.add(mod); 1320 loader.put(list); 1321 addDelay(); 1322 checkModifications(list); 1323 assertEquals(null, loader.get(FQN)); 1324 1325 1326 loader.put(FQN, "one", "two"); 1327 list = new ArrayList (); 1328 mod = new Modification(); 1329 mod.setType(Modification.ModificationType.REMOVE_DATA); 1330 mod.setFqn(FQN); 1331 list.add(mod); 1332 loader.put(list); 1333 addDelay(); 1334 checkModifications(list); 1335 } 1336 1337 1340 public void testOnePhaseTransaction() 1341 throws Exception 1342 { 1343 List mods = createUpdates(); 1344 loader.prepare(null, mods, true); 1345 checkModifications(mods); 1346 } 1347 1348 1351 public void testTwoPhaseTransactionPassivation() 1352 throws Exception 1353 { 1354 1355 Object txnKey = new Object (); 1356 List mods = createUpdates(); 1357 loader.prepare(txnKey, mods, false); 1358 loader.commit(txnKey); 1363 addDelay(); 1364 checkModifications(mods); 1365 } 1366 1367 1370 public void testTransactionRollbackPassivation() 1371 throws Exception 1372 { 1373 1374 loader.remove(Fqn.fromString("/")); 1375 1376 ByteArrayOutputStream baos = new ByteArrayOutputStream (1024); 1377 MarshalledValueOutputStream os = new MarshalledValueOutputStream(baos); 1378 loader.loadEntireState(os); 1379 os.close(); 1380 int num = baos.size(); 1381 1382 Object txnKey = new Object (); 1383 List mods = createUpdates(); 1384 loader.prepare(txnKey, mods, false); 1385 loader.rollback(txnKey); 1386 1387 baos = new ByteArrayOutputStream (1024); 1388 os = new MarshalledValueOutputStream(baos); 1389 loader.loadEntireState(os); 1390 os.close(); 1391 1392 assertEquals(num, baos.size()); 1393 } 1394 1395 1398 private List createUpdates() 1399 { 1400 1401 List list = new ArrayList (); 1402 1403 Modification mod = new Modification(); 1404 mod.setType(Modification.ModificationType.PUT_KEY_VALUE); 1405 mod.setFqn(FQN); 1406 mod.setKey("one"); 1407 mod.setValue("two"); 1408 list.add(mod); 1409 1410 mod = new Modification(); 1411 mod.setType(Modification.ModificationType.PUT_KEY_VALUE); 1412 mod.setFqn(FQN); 1413 mod.setKey("three"); 1414 mod.setValue("four"); 1415 list.add(mod); 1416 1417 Map map = new HashMap (); 1418 map.put("five", "six"); 1419 map.put("seven", "eight"); 1420 mod = new Modification(); 1421 mod.setType(Modification.ModificationType.PUT_DATA); 1422 mod.setFqn(FQN); 1423 mod.setData(map); 1424 list.add(mod); 1425 1426 return list; 1427 } 1428 1429 1432 private void checkModifications(List list) 1433 throws Exception 1434 { 1435 1436 for (int i = 0; i < list.size(); i += 1) 1437 { 1438 Modification mod = (Modification) list.get(i); 1439 Fqn fqn = mod.getFqn(); 1440 switch (mod.getType()) 1441 { 1442 case PUT_KEY_VALUE: 1443 assertEquals(mod.getValue(), loader.get(fqn).get(mod.getKey())); 1444 break; 1445 case PUT_DATA: 1446 Map map = mod.getData(); 1447 for (Iterator iter = map.keySet().iterator(); iter.hasNext();) 1448 { 1449 Object key = iter.next(); 1450 assertEquals(map.get(key), loader.get(fqn).get(key)); 1451 } 1452 break; 1453 case REMOVE_KEY_VALUE: 1454 assertEquals(null, loader.get(fqn).get(mod.getKey())); 1455 break; 1456 case REMOVE_DATA: 1457 map = loader.get(fqn); 1458 assertNotNull(map); 1459 assertTrue(map.isEmpty()); 1460 break; 1461 case REMOVE_NODE: 1462 assertEquals(null, loader.get(fqn)); 1463 break; 1464 default: 1465 fail("unknown type: " + mod); 1466 break; 1467 } 1468 } 1469 } 1470 1471 1472 1475 public void testNullKeysAndValues() 1476 throws Exception 1477 { 1478 1479 loader.put(FQN, null, "x"); 1480 addDelay(); 1481 assertEquals("x", loader.get(FQN).get(null)); 1482 Map map = loader.get(FQN); 1483 assertEquals(1, map.size()); 1484 assertEquals("x", map.get(null)); 1485 1486 loader.put(FQN, "y", null); 1487 addDelay(); 1488 assertEquals(null, loader.get(FQN).get("y")); 1489 map = loader.get(FQN); 1490 assertEquals(2, map.size()); 1491 assertEquals("x", map.get(null)); 1492 assertEquals(null, map.get("y")); 1493 1494 loader.remove(FQN, null); 1495 addDelay(); 1496 assertEquals(null, loader.get(FQN).get(null)); 1497 assertEquals(1, loader.get(FQN).size()); 1498 1499 loader.remove(FQN, "y"); 1500 addDelay(); 1501 assertNotNull(loader.get(FQN)); 1502 assertNull(loader.get(FQN).get("y")); 1503 assertEquals(0, loader.get(FQN).size()); 1504 1505 map = new HashMap (); 1506 map.put(null, null); 1507 loader.put(FQN, map); 1508 addDelay(); 1509 assertEquals(map, loader.get(FQN)); 1510 1511 loader.remove(FQN); 1512 addDelay(); 1513 assertNull(loader.get(FQN)); 1514 1515 map = new HashMap (); 1516 map.put("xyz", null); 1517 map.put(null, "abc"); 1518 loader.put(FQN, map); 1519 addDelay(); 1520 assertEquals(map, loader.get(FQN)); 1521 1522 loader.remove(FQN); 1523 addDelay(); 1524 assertNull(loader.get(FQN)); 1525 } 1526 1527 1530 public void testDatabaseNamePassivation() 1531 throws Exception 1532 { 1533 1534 loader.put(FQN, "one", "two"); 1535 addDelay(); 1536 assertEquals("two", loader.get(FQN).get("one")); 1537 } 1538 1539 1542 public void testLoadAndStore() 1543 throws Exception 1544 { 1545 1546 1547 loader.remove(Fqn.fromString("/")); 1548 1555 1556 Complex c1 = new Complex(); 1557 Complex c2 = new Complex(c1); 1558 1559 1560 loader.put(FQN, 1, c1); 1561 loader.put(FQN, 2, c2); 1562 addDelay(); 1563 assertEquals(c1, loader.get(FQN).get(1)); 1564 assertEquals(c2, loader.get(FQN).get(2)); 1565 assertEquals(2, loader.get(FQN).size()); 1566 1567 1568 ByteArrayOutputStream baos = new ByteArrayOutputStream (1024); 1569 MarshalledValueOutputStream os = new MarshalledValueOutputStream(baos); 1570 loader.loadEntireState(os); 1571 os.writeObject(StateTransferManager.STREAMING_DELIMETER_NODE); 1572 os.close(); 1573 assertTrue(baos.size() > 0); 1574 1575 byte[] savedState = baos.toByteArray(); 1576 1577 1578 ByteArrayInputStream bais = new ByteArrayInputStream (savedState); 1579 MarshalledValueInputStream is = new MarshalledValueInputStream(bais); 1580 loader.storeEntireState(is); 1581 is.close(); 1582 1583 addDelay(); 1584 assertEquals(c1, loader.get(FQN).get(1)); 1585 assertEquals(c2, loader.get(FQN).get(2)); 1586 assertEquals(2, loader.get(FQN).size()); 1587 } 1588 1589 1590 1593 private static class Complex implements Serializable 1594 { 1595 1598 private static final long serialVersionUID = 8950692199236424832L; 1599 1600 Complex nested; 1601 1602 Complex() 1603 { 1604 this(null); 1605 } 1606 1607 Complex(Complex nested) 1608 { 1609 this.nested = nested; 1610 } 1611 1612 public boolean equals(Object o) 1613 { 1614 try 1615 { 1616 Complex x = (Complex) o; 1617 return (nested != null) ? nested.equals(x.nested) 1618 : (x.nested == null); 1619 } 1620 catch (ClassCastException e) 1621 { 1622 return false; 1623 } 1624 } 1625 1626 public int hashCode() 1627 { 1628 if (nested == null) 1629 { 1630 return super.hashCode(); 1631 } 1632 else 1633 { 1634 return 13 + nested.hashCode(); 1635 } 1636 } 1637 } 1638 1639 1640 public static Test suite() 1641 { 1642 return new TestSuite(PassivationTestsBase.class); 1643 } 1644 1645 public static void main(String [] args) 1646 { 1647 junit.textui.TestRunner.run(suite()); 1648 } 1649 1650} 1651
| Popular Tags
|