1 7 package org.jboss.cache.tests; 8 9 import com.sleepycat.je.Database; 10 import com.sleepycat.je.DatabaseConfig; 11 import com.sleepycat.je.DatabaseException; 12 import com.sleepycat.je.DeadlockException; 13 import com.sleepycat.je.Environment; 14 15 import junit.framework.Test; 16 import junit.framework.TestCase; 17 import junit.framework.TestSuite; 18 import org.jboss.cache.Fqn; 19 import org.jboss.cache.Modification; 20 import org.jboss.cache.TreeCache; 21 import org.jboss.cache.loader.CacheLoader; 22 23 import java.io.File ; 24 import java.io.Serializable ; 25 import java.io.FileFilter ; 26 import java.util.*; 27 28 37 public class BdbjeUnitTestCase extends TestCase { 38 39 private static final String envHome = "."; 40 private static final Fqn FQN = new Fqn("key"); 41 42 private TreeCache treeCache; 43 private CacheLoader loader; 44 45 48 public BdbjeUnitTestCase(String name) { 49 super(name); 50 } 51 52 55 public void setUp() throws Exception { 56 super.setUp(); 57 58 File dir = new File (envHome); 59 60 class MyFilter implements FileFilter { 61 public boolean accept(File file) { 62 return file.getName().endsWith(".jdb"); 63 } 64 } 65 66 File [] files=dir.listFiles(new MyFilter()); 67 68 if (files != null) { 69 for (int i = 0; i < files.length; i += 1) { 70 File file = files[i]; 71 if (file.isFile()) { 72 if (!file.delete()) { 73 System.err.println("Unable to delete: " + file); 74 } 75 } 76 } 77 } 78 } 79 80 84 public void tearDown() throws Exception { 85 super.tearDown(); 86 87 if (loader != null) { 88 try { 89 loader.stop(); 90 } catch (Exception ignored) {} 91 loader = null; 92 } 93 if (treeCache != null) { 94 try { 95 treeCache.stopService(); 96 } catch (Exception ignored) {} 97 treeCache = null; 98 } 99 } 100 101 107 private void startLoader(boolean transactional, String dbName) 108 throws Exception { 109 110 115 treeCache = new TreeCache(); 116 treeCache.setClusterName("myCluster"); 117 if (transactional) { 118 treeCache.setTransactionManagerLookupClass( 119 "org.jboss.cache.DummyTransactionManagerLookup"); 120 } 121 treeCache.startService(); 122 123 124 String configStr; 125 if (dbName != null) { 126 configStr = envHome + '#' + dbName; 127 } else { 128 configStr = envHome; 129 dbName = "myCluster"; 130 } 131 132 instantiateLoader(); 133 134 135 loader.setCache(treeCache); 136 Properties props=new Properties(); 137 props.setProperty("location", configStr); 138 loader.setConfig(props); 139 loader.create(); 140 loader.start(); 141 142 143 Environment env = new Environment(new File (envHome), null); 144 DatabaseConfig dbConfig = new DatabaseConfig(); 145 dbConfig.setTransactional(transactional); 146 Database db = env.openDatabase(null, dbName, dbConfig); 147 db.close(); 148 env.close(); 149 } 150 151 154 private void instantiateLoader() 155 throws Exception { 156 157 158 Class cls = 159 Class.forName("org.jboss.cache.loader.bdbje.BdbjeCacheLoader"); 160 loader = (CacheLoader) cls.newInstance(); 161 } 162 163 166 private void stopLoader() 167 throws Exception { 168 169 loader.stop(); 170 loader.destroy(); 171 } 172 173 176 public void testBasicOperations() 177 throws Exception { 178 179 doTestBasicOperations(false); 180 } 181 182 185 public void testBasicOperationsTransactional() 186 throws Exception { 187 188 doTestBasicOperations(true); 189 } 190 191 194 private void doTestBasicOperations(boolean transactional) 195 throws Exception { 196 197 startLoader(transactional, null); 198 199 200 doPutTests(new Fqn("key")); 201 doRemoveTests(new Fqn("key")); 202 assertEquals(0, loader.loadEntireState().length); 203 204 205 doPutTests(new Fqn("key1")); 206 doPutTests(new Fqn("key3")); 207 doPutTests(new Fqn("key2")); 208 assertEquals(4, loader.get(new Fqn("key1")).size()); 209 assertEquals(4, loader.get(new Fqn("key2")).size()); 210 assertEquals(4, loader.get(new Fqn("key3")).size()); 211 212 213 doRemoveTests(new Fqn("key2")); 214 doRemoveTests(new Fqn("key3")); 215 doRemoveTests(new Fqn("key1")); 216 assertEquals(null, loader.get(new Fqn("key1"))); 217 assertEquals(null, loader.get(new Fqn("key2"))); 218 assertEquals(null, loader.get(new Fqn("key3"))); 219 assertEquals(0, loader.loadEntireState().length); 220 221 stopLoader(); 222 } 223 224 227 private void doPutTests(Fqn fqn) 228 throws Exception { 229 230 assertTrue(!loader.exists(fqn)); 231 232 233 Object oldVal; 234 oldVal = loader.put(fqn, "one", "two"); 235 assertNull(oldVal); 236 oldVal = loader.put(fqn, "three", "four"); 237 assertNull(oldVal); 238 assertEquals("two", loader.get(fqn, "one")); 239 assertEquals("four", loader.get(fqn, "three")); 240 oldVal = loader.put(fqn, "one", "xxx"); 241 assertEquals("two", oldVal); 242 oldVal = loader.put(fqn, "one", "two"); 243 assertEquals("xxx", oldVal); 244 245 246 Map map = loader.get(fqn); 247 assertEquals(2, map.size()); 248 assertEquals("two", map.get("one")); 249 assertEquals("four", map.get("three")); 250 251 252 map.put("five", "six"); 253 map.put("seven", "eight"); 254 loader.put(fqn, map); 255 assertEquals("six", loader.get(fqn, "five")); 256 assertEquals("eight", loader.get(fqn, "seven")); 257 assertEquals(map, loader.get(fqn)); 258 assertEquals(4, map.size()); 259 260 assertTrue(loader.exists(fqn)); 261 } 262 263 266 private void doRemoveTests(Fqn fqn) 267 throws Exception { 268 269 270 Object oldVal; 271 oldVal = loader.remove(fqn, "one"); 272 assertEquals("two", oldVal); 273 oldVal = loader.remove(fqn, "five"); 274 assertEquals("six", oldVal); 275 assertEquals(null, loader.get(fqn, "one")); 276 assertEquals(null, loader.get(fqn, "five")); 277 assertEquals("four", loader.get(fqn, "three")); 278 assertEquals("eight", loader.get(fqn, "seven")); 279 Map map = loader.get(fqn); 280 assertEquals(2, map.size()); 281 assertEquals("four", map.get("three")); 282 assertEquals("eight", map.get("seven")); 283 284 285 assertTrue(loader.exists(fqn)); 286 loader.remove(fqn); 287 assertEquals(null, loader.get(fqn, "three")); 288 assertEquals(null, loader.get(fqn, "seven")); 289 map = loader.get(fqn); 290 assertEquals(null, map); 291 assertTrue(!loader.exists(fqn)); 292 } 293 294 298 public void testMultiLevelTree() 299 throws Exception { 300 301 startLoader(false, null); 302 303 304 assertTrue(!loader.exists(new Fqn("key0"))); 305 loader.put(Fqn.fromString("/key0/level1/level2"), null); 306 assertTrue(loader.exists(Fqn.fromString("/key0/level1/level2"))); 307 assertTrue(loader.exists(Fqn.fromString("/key0/level1"))); 308 assertTrue(loader.exists(new Fqn("key0"))); 309 310 311 loader.put(Fqn.fromString("/key0/x/y"), null); 312 assertTrue(loader.exists(Fqn.fromString("/key0/x/y"))); 313 assertTrue(loader.exists(Fqn.fromString("/key0/x"))); 314 loader.remove(Fqn.fromString("/key0/x/y")); 315 assertTrue(!loader.exists(Fqn.fromString("/key0/x/y"))); 316 assertTrue(loader.exists(Fqn.fromString("/key0/x"))); 317 318 319 loader.remove(new Fqn("key0")); 320 assertTrue(!loader.exists(new Fqn("key0"))); 321 assertTrue(!loader.exists(Fqn.fromString("/key0/level1/level2"))); 322 assertTrue(!loader.exists(Fqn.fromString("/key0/level1"))); 323 assertTrue(!loader.exists(Fqn.fromString("/key0/x"))); 324 325 326 loader.put(new Fqn("key1"), null); 327 loader.put(new Fqn("key2"), null); 328 loader.put(new Fqn("key3"), null); 329 assertTrue(loader.exists(new Fqn("key1"))); 330 assertTrue(loader.exists(new Fqn("key2"))); 331 assertTrue(loader.exists(new Fqn("key3"))); 332 333 334 assertTrue(!loader.exists(Fqn.fromString("/key3/level1"))); 335 assertTrue(!loader.exists(Fqn.fromString("/key3/level1/level2"))); 336 loader.put(Fqn.fromString("/key3/level1/level2"), null); 337 assertTrue(loader.exists(Fqn.fromString("/key3/level1/level2"))); 338 assertTrue(loader.exists(Fqn.fromString("/key3/level1"))); 339 340 341 assertTrue(loader.exists(new Fqn("key1"))); 342 assertTrue(loader.exists(new Fqn("key2"))); 343 assertTrue(loader.exists(new Fqn("key3"))); 344 345 346 loader.remove(Fqn.fromString("/key3/level1")); 347 assertTrue(!loader.exists(Fqn.fromString("/key3/level1/level2"))); 348 assertTrue(!loader.exists(Fqn.fromString("/key3/level1"))); 349 350 351 assertTrue(loader.exists(new Fqn("key1"))); 352 assertTrue(loader.exists(new Fqn("key2"))); 353 assertTrue(loader.exists(new Fqn("key3"))); 354 355 356 loader.remove(new Fqn("key1")); 357 assertTrue(!loader.exists(new Fqn("key1"))); 358 assertTrue(loader.exists(new Fqn("key2"))); 359 assertTrue(loader.exists(new Fqn("key3"))); 360 361 362 loader.remove(new Fqn("key3")); 363 assertTrue(loader.exists(new Fqn("key2"))); 364 assertTrue(!loader.exists(new Fqn("key3"))); 365 366 367 loader.remove(new Fqn("key2")); 368 assertTrue(!loader.exists(new Fqn("key0"))); 369 assertTrue(!loader.exists(new Fqn("key1"))); 370 assertTrue(!loader.exists(new Fqn("key2"))); 371 assertTrue(!loader.exists(new Fqn("key3"))); 372 373 374 375 assertNull(loader.get(new Fqn("key0"))); 376 loader.put(Fqn.fromString("/key0/level1/level2"), "a", "b"); 377 assertNotNull(loader.get(Fqn.fromString("/key0/level1/level2"))); 378 assertNull(loader.get(Fqn.fromString("/key0/level1"))); 379 assertNull(loader.get(new Fqn("key0"))); 380 381 loader.put(Fqn.fromString("/key0/x/y"), "a", "b"); 382 assertNotNull(loader.get(Fqn.fromString("/key0/x/y"))); 383 assertNull(loader.get(Fqn.fromString("/key0/x"))); 384 loader.remove(Fqn.fromString("/key0/x/y")); 385 assertNull(loader.get(Fqn.fromString("/key0/x/y"))); 386 assertNull(loader.get(Fqn.fromString("/key0/x"))); 387 388 loader.remove(new Fqn("key0")); 389 assertNull(loader.get(new Fqn("key0"))); 390 assertNull(loader.get(Fqn.fromString("/key0/level1/level2"))); 391 assertNull(loader.get(Fqn.fromString("/key0/level1"))); 392 assertNull(loader.get(Fqn.fromString("/key0/x"))); 393 394 loader.put(new Fqn("key1"), "a", "b"); 395 loader.put(new Fqn("key2"), "a", "b"); 396 loader.put(new Fqn("key3"), "a", "b"); 397 assertNotNull(loader.get(new Fqn("key1"))); 398 assertNotNull(loader.get(new Fqn("key2"))); 399 assertNotNull(loader.get(new Fqn("key3"))); 400 401 assertNull(loader.get(Fqn.fromString("/key3/level1"))); 402 assertNull(loader.get(Fqn.fromString("/key3/level1/level2"))); 403 loader.put(Fqn.fromString("/key3/level1/level2"), "a", "b"); 404 assertNotNull(loader.get(Fqn.fromString("/key3/level1/level2"))); 405 assertNull(loader.get(Fqn.fromString("/key3/level1"))); 406 407 assertNotNull(loader.get(new Fqn("key1"))); 408 assertNotNull(loader.get(new Fqn("key2"))); 409 assertNotNull(loader.get(new Fqn("key3"))); 410 411 loader.remove(Fqn.fromString("/key3/level1")); 412 assertNull(loader.get(Fqn.fromString("/key3/level1/level2"))); 413 assertNull(loader.get(Fqn.fromString("/key3/level1"))); 414 415 assertNotNull(loader.get(new Fqn("key1"))); 416 assertNotNull(loader.get(new Fqn("key2"))); 417 assertNotNull(loader.get(new Fqn("key3"))); 418 419 loader.remove(new Fqn("key1")); 420 assertNull(loader.get(new Fqn("key1"))); 421 assertNotNull(loader.get(new Fqn("key2"))); 422 assertNotNull(loader.get(new Fqn("key3"))); 423 424 loader.remove(new Fqn("key3")); 425 assertNotNull(loader.get(new Fqn("key2"))); 426 assertNull(loader.get(new Fqn("key3"))); 427 428 loader.remove(new Fqn("key2")); 429 assertNull(loader.get(new Fqn("key0"))); 430 assertNull(loader.get(new Fqn("key1"))); 431 assertNull(loader.get(new Fqn("key2"))); 432 assertNull(loader.get(new Fqn("key3"))); 433 434 stopLoader(); 435 } 436 437 440 public void testGetChildrenNames() 441 throws Exception { 442 443 startLoader(false, null); 444 445 checkChildren(new Fqn(), null); 446 checkChildren(Fqn.fromString("/key0"), null); 447 448 loader.put(Fqn.fromString("/key0"), null); 449 checkChildren(new Fqn(), new String [] { "key0" }); 450 451 loader.put(Fqn.fromString("/key1/x"), null); 452 checkChildren(new Fqn(), new String [] { "key0", "key1" }); 453 checkChildren(Fqn.fromString("/key1"), new String [] { "x" }); 454 455 loader.remove(Fqn.fromString("/key1/x")); 456 checkChildren(new Fqn(), new String [] { "key0", "key1" }); 457 checkChildren(Fqn.fromString("/key0"), null); 458 checkChildren(Fqn.fromString("/key1"), null); 459 460 loader.put(Fqn.fromString("/key0/a"), null); 461 loader.put(Fqn.fromString("/key0/ab"), null); 462 loader.put(Fqn.fromString("/key0/abc"), null); 463 checkChildren(Fqn.fromString("/key0"), 464 new String [] { "a", "ab", "abc" }); 465 466 loader.put(Fqn.fromString("/key0/xxx"), null); 467 loader.put(Fqn.fromString("/key0/xx"), null); 468 loader.put(Fqn.fromString("/key0/x"), null); 469 checkChildren(Fqn.fromString("/key0"), 470 new String [] { "a", "ab", "abc", "x", "xx", "xxx" }); 471 472 loader.put(Fqn.fromString("/key0/a/1"), null); 473 loader.put(Fqn.fromString("/key0/a/2"), null); 474 loader.put(Fqn.fromString("/key0/a/2/1"), null); 475 checkChildren(Fqn.fromString("/key0/a/2"), new String [] { "1" }); 476 checkChildren(Fqn.fromString("/key0/a"), new String [] { "1", "2" }); 477 checkChildren(Fqn.fromString("/key0"), 478 new String [] { "a", "ab", "abc", "x", "xx", "xxx" }); 479 480 loader.put(Fqn.fromString("/key0/\u0000"), null); 481 loader.put(Fqn.fromString("/key0/\u0001"), null); 482 checkChildren(Fqn.fromString("/key0"), 483 new String [] { "a", "ab", "abc", "x", "xx", "xxx", 484 "\u0000", "\u0001"}); 485 486 loader.put(Fqn.fromString("/\u0001"), null); 487 checkChildren(new Fqn(), new String [] { "key0", "key1", "\u0001" }); 488 489 loader.put(Fqn.fromString("/\u0001/\u0001"), null); 490 checkChildren(Fqn.fromString("/\u0001"), new String [] { "\u0001" }); 491 492 loader.put(Fqn.fromString("/\u0001/\uFFFF"), null); 493 checkChildren(Fqn.fromString("/\u0001"), 494 new String [] { "\u0001", "\uFFFF" }); 495 496 loader.put(Fqn.fromString("/\u0001/\uFFFF/\u0001"), null); 497 checkChildren(Fqn.fromString("/\u0001/\uFFFF"), 498 new String [] { "\u0001" }); 499 500 stopLoader(); 501 } 502 503 506 private void checkChildren(Fqn fqn, String [] names) 507 throws Exception { 508 509 Set set = loader.getChildrenNames(fqn); 510 if (names != null) { 511 assertEquals(names.length, set.size()); 512 for (int i = 0; i < names.length; i += 1) { 513 assertTrue(set.contains(names[i])); 514 } 515 } else { 516 assertNull(set); 517 } 518 } 519 520 523 public void testModifications() 524 throws Exception { 525 526 doTestModifications(false); 527 } 528 529 532 public void testModificationsTransactional() 533 throws Exception { 534 535 doTestModifications(true); 536 } 537 538 541 private void doTestModifications(boolean transactional) 542 throws Exception { 543 544 startLoader(transactional, null); 545 546 547 List list = createUpdates(); 548 loader.put(list); 549 checkModifications(list); 550 551 552 list = new ArrayList(); 553 Modification mod = new Modification(); 554 mod.setType(Modification.REMOVE_KEY_VALUE); 555 mod.setFqn(FQN); 556 mod.setKey("one"); 557 list.add(mod); 558 loader.put(list); 559 checkModifications(list); 560 561 562 list = new ArrayList(); 563 mod = new Modification(); 564 mod.setType(Modification.REMOVE_NODE); 565 mod.setFqn(FQN); 566 list.add(mod); 567 loader.put(list); 568 checkModifications(list); 569 assertEquals(null, loader.get(FQN)); 570 571 572 loader.put(FQN, "one", "two"); 573 list = new ArrayList(); 574 mod = new Modification(); 575 mod.setType(Modification.REMOVE_DATA); 576 mod.setFqn(FQN); 577 list.add(mod); 578 loader.put(list); 579 checkModifications(list); 580 assertEquals(null, loader.get(FQN)); 581 582 stopLoader(); 583 } 584 585 588 public void testOnePhaseTransaction() 589 throws Exception { 590 591 startLoader(true, null); 592 593 List mods = createUpdates(); 594 loader.prepare(null, mods, true); 595 checkModifications(mods); 596 597 stopLoader(); 598 } 599 600 603 public void testTwoPhaseTransaction() 604 throws Exception { 605 606 startLoader(true, null); 607 608 Object txnKey = new Object (); 609 List mods = createUpdates(); 610 loader.prepare(txnKey, mods, false); 611 try { 612 checkModifications(mods); 613 fail("Expected lock timeout"); 614 } catch (DeadlockException expected) {} 615 loader.commit(txnKey); 616 checkModifications(mods); 617 618 stopLoader(); 619 } 620 621 624 public void testTransactionRollback() 625 throws Exception { 626 627 startLoader(true, null); 628 629 Object txnKey = new Object (); 630 List mods = createUpdates(); 631 loader.prepare(txnKey, mods, false); 632 loader.rollback(txnKey); 633 assertEquals(0, loader.loadEntireState().length); 634 635 stopLoader(); 636 } 637 638 641 private List createUpdates() { 642 643 List list = new ArrayList(); 644 645 Modification mod = new Modification(); 646 mod.setType(Modification.PUT_KEY_VALUE); 647 mod.setFqn(FQN); 648 mod.setKey("one"); 649 mod.setValue("two"); 650 list.add(mod); 651 652 mod = new Modification(); 653 mod.setType(Modification.PUT_KEY_VALUE); 654 mod.setFqn(FQN); 655 mod.setKey("three"); 656 mod.setValue("four"); 657 list.add(mod); 658 659 Map map = new HashMap(); 660 map.put("five", "six"); 661 map.put("seven", "eight"); 662 mod = new Modification(); 663 mod.setType(Modification.PUT_DATA); 664 mod.setFqn(FQN); 665 mod.setData(map); 666 list.add(mod); 667 668 return list; 669 } 670 671 674 private void checkModifications(List list) 675 throws Exception { 676 677 for (int i = 0; i < list.size(); i += 1) { 678 Modification mod = (Modification) list.get(i); 679 Fqn fqn = mod.getFqn(); 680 switch (mod.getType()) { 681 case Modification.PUT_KEY_VALUE: 682 assertEquals(mod.getValue(), loader.get(fqn, mod.getKey())); 683 break; 684 case Modification.PUT_DATA: 685 Map map = mod.getData(); 686 for (Iterator iter = map.keySet().iterator(); iter.hasNext();) { 687 Object key = iter.next(); 688 assertEquals(map.get(key), loader.get(fqn, key)); 689 } 690 break; 691 case Modification.REMOVE_KEY_VALUE: 692 assertEquals(null, loader.get(fqn, mod.getKey())); 693 break; 694 case Modification.REMOVE_DATA: 695 assertTrue(loader.exists(fqn)); 696 assertEquals(null, loader.get(fqn)); 697 break; 698 case Modification.REMOVE_NODE: 699 assertTrue(!loader.exists(fqn)); 700 assertEquals(null, loader.get(fqn)); 701 break; 702 default: 703 fail("unknown type: " + mod); 704 break; 705 } 706 } 707 } 708 709 712 public void testBasicExceptions() 713 throws Exception { 714 715 instantiateLoader(); 716 checkPreCreateExceptions(); 717 718 startLoader(false, null); 719 checkPostCreateExceptions(); 720 loader.put(FQN, "one", "two"); 721 assertEquals("two", loader.get(FQN, "one")); 722 723 stopLoader(); 724 checkPreCreateExceptions(); 725 726 startLoader(false, null); 727 checkPostCreateExceptions(); 728 loader.put(FQN, "one", "two"); 729 assertEquals("two", loader.get(FQN, "one")); 730 731 stopLoader(); 732 checkPreCreateExceptions(); 733 } 734 735 738 private void checkPreCreateExceptions() 739 throws Exception { 740 741 loader.setCache(new TreeCache()); 742 loader.setConfig(null); 743 try { 744 loader.start(); 745 fail(); 746 } catch (IllegalStateException expected) {} 747 748 loader.setCache(null); 749 Properties props=new Properties(); 750 props.setProperty("location", "xyz"); 751 loader.setConfig(props); 752 try { 753 loader.start(); 754 fail(); 755 } catch (IllegalStateException expected) {} 756 757 loader.setCache(new TreeCache()); 758 props=new Properties(); 759 props.setProperty("location", "directory_that_does_not_exist"); 760 loader.setConfig(props); 761 try { 762 loader.start(); 763 fail(); 764 } catch (DatabaseException expected) {} 765 766 try { 767 loader.put(FQN, "one", "two"); 768 fail(); 769 } catch (IllegalStateException expected) {} 770 771 try { 772 loader.put(FQN, new HashMap()); 773 fail(); 774 } catch (IllegalStateException expected) {} 775 776 try { 777 loader.put(new ArrayList()); 778 fail(); 779 } catch (IllegalStateException expected) {} 780 781 try { 782 loader.get(FQN, "one"); 783 fail(); 784 } catch (IllegalStateException expected) {} 785 786 try { 787 loader.get(FQN); 788 fail(); 789 } catch (IllegalStateException expected) {} 790 791 try { 792 loader.remove(FQN); 793 fail(); 794 } catch (IllegalStateException expected) {} 795 796 try { 797 loader.remove(FQN, "one"); 798 fail(); 799 } catch (IllegalStateException expected) {} 800 801 try { 802 loader.prepare(new Object (), new ArrayList(), false); 803 fail(); 804 } catch (IllegalStateException expected) {} 805 806 try { 807 loader.commit(new Object ()); 808 fail(); 809 } catch (IllegalStateException expected) {} 810 811 try { 812 loader.rollback(new Object ()); 813 fail(); 814 } catch (IllegalStateException expected) {} 815 816 try { 817 loader.loadEntireState(); 818 fail(); 819 } catch (IllegalStateException expected) {} 820 821 try { 822 loader.storeEntireState(new byte[0]); 823 fail(); 824 } catch (IllegalStateException expected) {} 825 826 827 loader.stop(); 828 loader.destroy(); 829 } 830 831 834 private void checkPostCreateExceptions() 835 throws Exception { 836 837 try { 838 loader.create(); 839 fail(); 840 } catch (IllegalStateException expected) {} 841 842 try { 843 loader.start(); 844 fail(); 845 } catch (IllegalStateException expected) {} 846 847 try { 848 loader.put(null, "one", "two"); 849 fail(); 850 } catch (NullPointerException expected) {} 851 852 try { 853 loader.put(null, new HashMap()); 854 fail(); 855 } catch (NullPointerException expected) {} 856 857 try { 858 loader.put(null); 859 fail(); 860 } catch (NullPointerException expected) {} 861 862 try { 863 loader.get(null, "one"); 864 fail(); 865 } catch (NullPointerException expected) {} 866 867 try { 868 loader.get(null); 869 fail(); 870 } catch (NullPointerException expected) {} 871 872 try { 873 loader.remove(null); 874 fail(); 875 } catch (NullPointerException expected) {} 876 877 try { 878 loader.remove(null, "one"); 879 fail(); 880 } catch (NullPointerException expected) {} 881 882 try { 883 loader.prepare(null, new ArrayList(), false); 884 fail(); 885 } catch (NullPointerException expected) {} 886 try { 887 loader.prepare(new Object (), null, false); 888 fail(); 889 } catch (NullPointerException expected) {} 890 891 try { 892 loader.commit(null); 893 fail(); 894 } catch (NullPointerException expected) {} 895 896 try { 897 loader.rollback(null); 898 fail(); 899 } catch (NullPointerException expected) {} 900 } 901 902 905 public void testTransactionExceptions() 906 throws Exception { 907 908 List mods = createUpdates(); 909 910 911 startLoader(false, null); 912 try { 913 loader.prepare(new Object (), mods, false); 914 fail(); 915 } catch (UnsupportedOperationException expected) {} 916 stopLoader(); 917 918 startLoader(true, null); 919 920 921 try { 922 loader.commit(new Object ()); 923 fail(); 924 } catch (IllegalArgumentException expected) {} 925 try { 926 loader.rollback(new Object ()); 927 fail(); 928 } catch (IllegalArgumentException expected) {} 929 930 931 loader.storeEntireState(null); 932 Object txnKey = new Object (); 933 loader.prepare(txnKey, mods, false); 934 loader.commit(txnKey); 935 try { 936 loader.commit(txnKey); 937 fail(); 938 } catch (IllegalArgumentException expected) {} 939 try { 940 loader.rollback(txnKey); 941 fail(); 942 } catch (IllegalArgumentException expected) {} 943 944 945 loader.storeEntireState(null); 946 txnKey = new Object (); 947 loader.prepare(txnKey, mods, false); 948 loader.rollback(txnKey); 949 try { 950 loader.rollback(txnKey); 951 fail(); 952 } catch (IllegalArgumentException expected) {} 953 try { 954 loader.rollback(txnKey); 955 fail(); 956 } catch (IllegalArgumentException expected) {} 957 958 stopLoader(); 959 } 960 961 964 public void testNullKeysAndValues() 965 throws Exception { 966 967 startLoader(false, null); 968 969 loader.put(FQN, null, "x"); 970 assertEquals("x", loader.get(FQN, null)); 971 Map map = loader.get(FQN); 972 assertEquals(1, map.size()); 973 assertEquals("x", map.get(null)); 974 975 loader.put(FQN, "y", null); 976 assertEquals(null, loader.get(FQN, "y")); 977 map = loader.get(FQN); 978 assertEquals(2, map.size()); 979 assertEquals("x", map.get(null)); 980 assertEquals(null, map.get("y")); 981 982 loader.remove(FQN, null); 983 assertEquals(null, loader.get(FQN, null)); 984 assertEquals(1, loader.get(FQN).size()); 985 986 loader.remove(FQN, "y"); 987 assertEquals(null, loader.get(FQN, "y")); 988 assertEquals(null, loader.get(FQN)); 989 990 map = new HashMap(); 991 map.put(null, null); 992 loader.put(FQN, map); 993 assertEquals(map, loader.get(FQN)); 994 995 loader.remove(FQN); 996 assertEquals(null, loader.get(FQN)); 997 998 map = new HashMap(); 999 map.put("xyz", null); 1000 map.put(null, "abc"); 1001 loader.put(FQN, map); 1002 assertEquals(map, loader.get(FQN)); 1003 1004 loader.remove(FQN); 1005 assertEquals(null, loader.get(FQN)); 1006 1007 stopLoader(); 1008 } 1009 1010 1013 public void testDatabaseName() 1014 throws Exception { 1015 1016 startLoader(false, "nonDefaultDbName"); 1017 loader.put(FQN, "one", "two"); 1018 assertEquals("two", loader.get(FQN, "one")); 1019 stopLoader(); 1020 } 1021 1022 1025 public void testLoadAndStore() 1026 throws Exception { 1027 1028 startLoader(false, null); 1029 1030 1031 assertEquals(0, loader.loadEntireState().length); 1032 loader.storeEntireState(new byte[0]); 1033 assertEquals(0, loader.loadEntireState().length); 1034 loader.storeEntireState(null); 1035 assertEquals(0, loader.loadEntireState().length); 1036 assertEquals(null, loader.get(FQN)); 1037 1038 1039 Complex c1 = new Complex(); 1040 Complex c2 = new Complex(c1); 1041 1042 1043 loader.put(FQN, new Integer (1), c1); 1044 loader.put(FQN, new Integer (2), c2); 1045 assertEquals(c1, loader.get(FQN, new Integer (1))); 1046 assertEquals(c2, loader.get(FQN, new Integer (2))); 1047 assertEquals(2, loader.get(FQN).size()); 1048 1049 1050 byte[] state = loader.loadEntireState(); 1051 assertTrue(state.length > 0); 1052 1053 1054 loader.storeEntireState(null); 1055 assertEquals(0, loader.loadEntireState().length); 1056 assertEquals(null, loader.get(FQN)); 1057 1058 1059 loader.storeEntireState(state); 1060 assertEquals(c1, loader.get(FQN, new Integer (1))); 1061 assertEquals(c2, loader.get(FQN, new Integer (2))); 1062 assertEquals(2, loader.get(FQN).size()); 1063 1064 stopLoader(); 1065 } 1066 1067 public static Test suite() 1068 throws Exception { 1069 1070 return new TestSuite(BdbjeUnitTestCase.class); 1071 } 1072 1073 public static void main(String [] args) 1074 throws Exception { 1075 1076 junit.textui.TestRunner.run(suite()); 1077 } 1078 1079 1082 private static class Complex implements Serializable { 1083 1084 Complex nested; 1085 1086 Complex() { 1087 this(null); 1088 } 1089 1090 Complex(Complex nested) { 1091 this.nested = nested; 1092 } 1093 1094 public boolean equals(Object o) { 1095 try { 1096 Complex x = (Complex) o; 1097 return (nested != null) ? nested.equals(x.nested) 1098 : (x.nested == null); 1099 } catch (ClassCastException e) { 1100 return false; 1101 } 1102 } 1103 } 1104} 1105 | Popular Tags |