1 8 9 package com.sleepycat.je; 10 11 import java.io.File ; 12 import java.io.IOException ; 13 import java.io.Serializable ; 14 import java.util.Comparator ; 15 import java.util.Set ; 16 17 import junit.framework.TestCase; 18 19 import com.sleepycat.je.util.TestUtils; 20 21 24 public class DatabaseConfigTest extends TestCase { 25 private static final boolean DEBUG = false; 26 27 private File envHome; 28 private Environment env; 29 30 public DatabaseConfigTest() { 31 envHome = new File (System.getProperty(TestUtils.DEST_DIR)); 32 } 33 34 public void setUp() 35 throws IOException { 36 37 TestUtils.removeLogFiles("Setup", envHome, false); 38 } 39 40 public void tearDown() 41 throws Exception { 42 43 try { 44 45 if (env != null) { 46 env.close(); 47 } 48 } catch (DatabaseException e) { 49 50 } 51 env = null; TestUtils.removeLogFiles("TearDown", envHome, false); 53 } 54 55 59 public void testConfig() 60 throws Throwable { 61 62 try { 63 EnvironmentConfig envConfig = TestUtils.initEnvConfig(); 64 envConfig.setAllowCreate(true); 65 env = new Environment(envHome, envConfig); 66 67 71 DatabaseConfig dbConfigA = new DatabaseConfig(); 72 dbConfigA.setAllowCreate(true); 73 Database dbA = env.openDatabase(null, "foo", dbConfigA); 74 75 76 dbConfigA.setAllowCreate(false); 77 DatabaseConfig getConfig1 = dbA.getConfig(); 78 assertEquals(true, getConfig1.getAllowCreate()); 79 assertEquals(false, getConfig1.getSortedDuplicates()); 80 81 85 getConfig1.setSortedDuplicates(true); 86 DatabaseConfig getConfig2 = dbA.getConfig(); 87 assertEquals(false, getConfig2.getSortedDuplicates()); 88 89 dbA.close(); 90 env.close(); 91 } catch (Throwable t) { 92 t.printStackTrace(); 93 throw t; 94 } 95 } 96 97 public void testConfigMatching() 98 throws Throwable { 99 100 try { 101 102 103 DatabaseConfig confA = new DatabaseConfig(); 104 DatabaseConfig confB = new DatabaseConfig(); 105 106 try { 107 confA.validate(confB); 108 } catch (Exception E) { 109 fail("expected valid match"); 110 } 111 112 try { 113 confB.validate(confA); 114 } catch (Exception E) { 115 fail("expected valid match"); 116 } 117 118 try { 119 confA.validate(null); } catch (Exception E) { 121 fail("expected valid match"); 122 } 123 124 confA.setReadOnly(true); 125 try { 126 confA.validate(confB); 127 fail("expected exception"); 128 } catch (DatabaseException E) { 129 } 131 132 confA.setReadOnly(false); 133 confA.setSortedDuplicates(true); 134 try { 135 confA.validate(confB); 136 fail("expected exception"); 137 } catch (DatabaseException E) { 138 } 140 confA.setSortedDuplicates(false); 141 142 confA.setOverrideBtreeComparator(true); 143 confA.setBtreeComparator(TestComparator.class); 144 confB.setOverrideBtreeComparator(true); 145 confB.setBtreeComparator(TestComparator2.class); 146 try { 147 confA.validate(confB); 148 fail("expected exception"); 149 } catch (DatabaseException E) { 150 } 152 confA.setBtreeComparator((Class ) null); 153 confA.setOverrideBtreeComparator(false); 154 confB.setBtreeComparator((Class ) null); 155 confB.setOverrideBtreeComparator(false); 156 157 confA.setOverrideDuplicateComparator(true); 158 confA.setDuplicateComparator(TestComparator.class); 159 confB.setOverrideDuplicateComparator(true); 160 confB.setDuplicateComparator(TestComparator2.class); 161 try { 162 confA.validate(confB); 163 fail("expected exception"); 164 } catch (DatabaseException E) { 165 } 167 168 169 170 confA.setOverrideBtreeComparator(true); 171 confA.setBtreeComparator(new TestSerialComparator()); 172 confB.setOverrideBtreeComparator(true); 173 confB.setBtreeComparator(new TestSerialComparator2()); 174 try { 175 confA.validate(confB); 176 fail("expected exception"); 177 } catch (DatabaseException E) { 178 } 180 confA.setBtreeComparator((Comparator ) null); 181 confA.setOverrideBtreeComparator(false); 182 confB.setBtreeComparator((Comparator ) null); 183 confB.setOverrideBtreeComparator(false); 184 185 confA.setOverrideDuplicateComparator(true); 186 confA.setDuplicateComparator(new TestSerialComparator()); 187 confB.setOverrideDuplicateComparator(true); 188 confB.setDuplicateComparator(new TestSerialComparator2()); 189 try { 190 confA.validate(confB); 191 fail("expected exception"); 192 } catch (DatabaseException E) { 193 } 195 196 197 198 SecondaryConfig confC = new SecondaryConfig(); 199 SecondaryConfig confD = new SecondaryConfig(); 200 confC.setKeyCreator(new SecKeyCreator1()); 201 confD.setKeyCreator(new SecKeyCreator1()); 202 203 try { 204 confC.validate(confD); 205 } catch (Exception E) { 206 E.printStackTrace(); 207 fail("expected valid match"); 208 } 209 210 try { 211 confD.validate(confC); 212 } catch (Exception E) { 213 fail("expected valid match"); 214 } 215 216 try { 217 confC.validate(null); 218 fail("expected exception"); 219 } catch (DatabaseException E) { 220 } 222 223 confD.setKeyCreator(new SecKeyCreator2()); 224 try { 225 confC.validate(confD); 226 fail("expected exception"); 227 } catch (DatabaseException E) { 228 } 230 confD.setKeyCreator(new SecKeyCreator1()); 231 232 confD.setMultiKeyCreator(new SecMultiKeyCreator1()); 233 try { 234 confC.validate(confD); 235 fail("expected exception"); 236 } catch (DatabaseException E) { 237 } 239 confD.setMultiKeyCreator(null); 240 241 confC.setForeignKeyDeleteAction(ForeignKeyDeleteAction.NULLIFY); 242 try { 243 confC.validate(confD); 244 fail("expected exception"); 245 } catch (DatabaseException E) { 246 } 248 confC.setForeignKeyDeleteAction(ForeignKeyDeleteAction.ABORT); 249 250 confC.setForeignKeyNullifier(new ForeignKeyNullifier1()); 251 try { 252 confC.validate(confD); 253 fail("expected exception"); 254 } catch (DatabaseException E) { 255 } 257 confC.setForeignKeyNullifier(null); 258 259 confC.setForeignMultiKeyNullifier(new ForeignMultiKeyNullifier1()); 260 try { 261 confC.validate(confD); 262 fail("expected exception"); 263 } catch (DatabaseException E) { 264 } 266 confC.setForeignMultiKeyNullifier(null); 267 268 confC.setImmutableSecondaryKey(true); 269 try { 270 confC.validate(confD); 271 fail("expected exception"); 272 } catch (DatabaseException E) { 273 } 275 confC.setImmutableSecondaryKey(false); 276 } catch (Throwable t) { 277 t.printStackTrace(); 278 throw t; 279 } 280 } 281 282 285 public void testComparator() 286 throws Throwable { 287 288 try { 289 290 try { 291 DatabaseConfig config = new DatabaseConfig(); 292 config.setBtreeComparator(BadComparator1.class); 293 fail("Comparator shouldn't be instantiated"); 294 } catch (IllegalArgumentException e) { 295 296 if (DEBUG) { 297 System.out.println(e); 298 } 299 } 300 301 302 try { 303 DatabaseConfig config = new DatabaseConfig(); 304 config.setBtreeComparator(BadComparator2.class); 305 fail("Comparator shouldn't be instantiated"); 306 } catch (IllegalArgumentException e) { 307 308 if (DEBUG) { 309 System.out.println(e); 310 } 311 } 312 313 314 try { 315 DatabaseConfig config = new DatabaseConfig(); 316 config.setBtreeComparator(new BadSerialComparator1()); 317 fail("Comparator shouldn't be instantiated"); 318 } catch (IllegalArgumentException e) { 319 320 if (DEBUG) { 321 System.out.println(e); 322 } 323 } 324 325 326 try { 327 DatabaseConfig config = new DatabaseConfig(); 328 config.setBtreeComparator(new BadSerialComparator2()); 329 fail("Comparator shouldn't be instantiated"); 330 } catch (IllegalArgumentException e) { 331 332 if (DEBUG) { 333 System.out.println(e); 334 } 335 } 336 337 338 DatabaseConfig config = new DatabaseConfig(); 339 config.setBtreeComparator(TestComparator.class); 340 config.setBtreeComparator(TestComparator2.class); 341 config.setBtreeComparator(new TestSerialComparator()); 342 config.setBtreeComparator(new TestSerialComparator2()); 343 344 } catch (Throwable t) { 345 t.printStackTrace(); 346 throw t; 347 } 348 } 349 350 354 public void testConfigConfict() 355 throws Throwable { 356 357 try { 358 EnvironmentConfig envConfig = TestUtils.initEnvConfig(); 359 envConfig.setAllowCreate(true); 360 env = new Environment(envHome, envConfig); 361 362 365 366 367 DatabaseConfig firstConfig = new DatabaseConfig(); 368 firstConfig.setAllowCreate(true); 369 firstConfig.setSortedDuplicates(true); 370 Database firstHandle = env.openDatabase(null, "fooDups", 371 firstConfig); 372 373 DatabaseConfig secondConfig = new DatabaseConfig(); 374 secondConfig.setSortedDuplicates(false); 375 try { 376 env.openDatabase(null, "fooDups", secondConfig); 377 fail("Conflict in duplicates allowed should be detected."); 378 } catch (DatabaseException e) { 379 } 380 381 firstHandle.close(); 382 env.removeDatabase(null, "fooDups"); 383 384 385 firstConfig.setSortedDuplicates(false); 386 firstHandle = env.openDatabase(null, "fooDups", firstConfig); 387 388 secondConfig.setSortedDuplicates(true); 389 try { 390 env.openDatabase(null, "fooDups", secondConfig); 391 fail("Conflict in duplicates allowed should be detected."); 392 } catch (DatabaseException e) { 393 } 394 firstHandle.close(); 395 396 402 DatabaseConfig readOnlyConfig = new DatabaseConfig(); 403 readOnlyConfig.setReadOnly(true); 404 Database roHandle = env.openDatabase(null, "fooDups", 405 readOnlyConfig); 406 roHandle.close(); 407 408 409 env.close(); 410 envConfig = TestUtils.initEnvConfig(); 411 envConfig.setReadOnly(true); 412 env = new Environment(envHome, envConfig); 413 414 415 roHandle = env.openDatabase(null, "fooDups", 416 readOnlyConfig); 417 roHandle.close(); 418 419 420 try { 421 env.openDatabase(null, "fooDups", null); 422 fail("Should not be able to open read/write"); 423 } catch (DatabaseException e) { 424 if (DEBUG) { 425 System.out.println(e); 426 } 427 } 428 env.close(); 429 430 433 434 env = new Environment(envHome, null); 435 firstConfig = new DatabaseConfig(); 436 firstConfig.setAllowCreate(true); 437 firstHandle = env.openDatabase(null, 438 "fooComparator", 439 firstConfig); 440 DatabaseConfig firstRetrievedConfig = firstHandle.getConfig(); 441 assertEquals(null, firstRetrievedConfig.getBtreeComparator()); 442 assertEquals(null, firstRetrievedConfig.getDuplicateComparator()); 443 444 448 secondConfig = new DatabaseConfig(); 449 Comparator btreeComparator = new TestComparator(); 450 Comparator dupComparator = new TestComparator(); 451 secondConfig.setBtreeComparator(btreeComparator.getClass()); 452 secondConfig.setDuplicateComparator(dupComparator.getClass()); 453 Database secondHandle = 454 env.openDatabase(null, "fooComparator", secondConfig); 455 DatabaseConfig retrievedConfig = secondHandle.getConfig(); 456 assertEquals(null, retrievedConfig.getBtreeComparator()); 457 assertEquals(null, retrievedConfig.getDuplicateComparator()); 458 secondHandle.close(); 459 460 461 secondConfig = new DatabaseConfig(); 462 btreeComparator = new TestSerialComparator(); 463 dupComparator = new TestSerialComparator(); 464 secondConfig.setBtreeComparator(btreeComparator); 465 secondConfig.setDuplicateComparator(dupComparator); 466 secondHandle = 467 env.openDatabase(null, "fooComparator", secondConfig); 468 retrievedConfig = secondHandle.getConfig(); 469 assertEquals(null, retrievedConfig.getBtreeComparator()); 470 assertEquals(null, retrievedConfig.getDuplicateComparator()); 471 secondHandle.close(); 472 473 474 secondConfig.setOverrideBtreeComparator(true); 475 secondConfig.setOverrideDuplicateComparator(true); 476 btreeComparator = new TestComparator(); 477 dupComparator = new TestComparator(); 478 secondConfig.setBtreeComparator(btreeComparator.getClass()); 479 secondConfig.setDuplicateComparator(dupComparator.getClass()); 480 secondHandle = env.openDatabase(null, 481 "fooComparator", 482 secondConfig); 483 484 retrievedConfig = secondHandle.getConfig(); 485 assertEquals(btreeComparator.getClass(), 486 retrievedConfig.getBtreeComparator().getClass()); 487 assertEquals(dupComparator.getClass(), 488 retrievedConfig.getDuplicateComparator().getClass()); 489 secondHandle.close(); 490 491 492 secondConfig.setOverrideBtreeComparator(true); 493 secondConfig.setOverrideDuplicateComparator(true); 494 btreeComparator = new TestSerialComparator(); 495 dupComparator = new TestSerialComparator(); 496 secondConfig.setBtreeComparator(btreeComparator); 497 secondConfig.setDuplicateComparator(dupComparator); 498 secondHandle = env.openDatabase(null, 499 "fooComparator", 500 secondConfig); 501 502 retrievedConfig = secondHandle.getConfig(); 503 assertEquals(btreeComparator, 504 retrievedConfig.getBtreeComparator()); 505 assertEquals(dupComparator, 506 retrievedConfig.getDuplicateComparator()); 507 secondHandle.close(); 508 509 firstHandle.close(); 510 env.close(); 511 } catch (Throwable t) { 512 t.printStackTrace(); 513 env.close(); 514 throw t; 515 } 516 } 517 518 public void testIsTransactional() 519 throws Throwable { 520 521 try { 522 523 EnvironmentConfig envConfig = TestUtils.initEnvConfig(); 524 envConfig.setTransactional(true); 525 envConfig.setAllowCreate(true); 526 env = new Environment(envHome, envConfig); 527 528 529 DatabaseConfig dbConfig = new DatabaseConfig(); 530 dbConfig.setAllowCreate(true); 531 dbConfig.setTransactional(true); 532 Database myDb = env.openDatabase(null, "testDB", dbConfig); 533 assertTrue(myDb.isTransactional()); 534 assertTrue(myDb.getConfig().getTransactional()); 535 myDb.close(); 536 537 538 dbConfig.setTransactional(false); 539 myDb = env.openDatabase(null, "testDB", null); 540 assertFalse(myDb.isTransactional()); 541 assertFalse(myDb.getConfig().getTransactional()); 542 myDb.close(); 543 544 545 dbConfig.setTransactional(true); 546 Transaction txn = env.beginTransaction(null, null); 547 myDb = env.openDatabase(txn, "testDB2", dbConfig); 548 assertTrue(myDb.isTransactional()); 549 assertTrue(myDb.getConfig().getTransactional()); 550 551 DatabaseEntry key = new DatabaseEntry(); 552 DatabaseEntry data = new DatabaseEntry(); 553 key.setData(TestUtils.getTestArray(0)); 554 data.setData(TestUtils.getTestArray(0)); 555 try { 556 myDb.put(null, key, data); 557 } catch (DatabaseException DBE) { 558 fail("didn't expect DatabaseException, implied autocommit"); 559 } 560 561 key.setData(TestUtils.getTestArray(1)); 562 data.setData(TestUtils.getTestArray(1)); 563 try { 564 myDb.put(txn, key, data); 565 } catch (DatabaseException DBE) { 566 fail("didn't expect DatabaseException with txn passed"); 567 } 568 569 try { 570 myDb.get(txn, key, data, LockMode.DEFAULT); 571 } catch (DatabaseException DBE) { 572 fail("didn't expect DatabaseException with txn passed"); 573 } 574 575 txn.commit(); 576 577 try { 578 myDb.get(null, key, data, LockMode.DEFAULT); 579 } catch (DatabaseException DBE) { 580 fail("didn't expect DatabaseException because no txn passed"); 581 } 582 583 myDb.close(); 584 585 env.close(); 586 } catch (Throwable t) { 587 t.printStackTrace(); 588 throw t; 589 } 590 } 591 592 public void testOpenReadOnly() 593 throws Throwable { 594 595 try { 596 EnvironmentConfig envConfig = TestUtils.initEnvConfig(); 597 envConfig.setTransactional(true); 598 envConfig.setAllowCreate(true); 599 env = new Environment(envHome, envConfig); 600 601 DatabaseEntry key = new DatabaseEntry(); 602 DatabaseEntry data = new DatabaseEntry(); 603 604 Transaction txn = env.beginTransaction(null, null); 605 DatabaseConfig dbConfig = new DatabaseConfig(); 606 dbConfig.setTransactional(true); 607 dbConfig.setAllowCreate(true); 608 Database myDb = env.openDatabase(txn, "testDB2", dbConfig); 609 610 key.setData(TestUtils.getTestArray(0)); 611 data.setData(TestUtils.getTestArray(0)); 612 try { 613 myDb.put(txn, key, data); 614 } catch (DatabaseException DBE) { 615 fail("unexpected DatabaseException during put"); 616 } 617 618 txn.commit(); 619 myDb.close(); 620 621 dbConfig = new DatabaseConfig(); 622 dbConfig.setTransactional(true); 623 dbConfig.setReadOnly(true); 624 txn = env.beginTransaction(null, null); 625 myDb = env.openDatabase(txn, "testDB2", dbConfig); 626 assertTrue(myDb.isTransactional()); 627 assertTrue(myDb.getConfig().getTransactional()); 628 629 key.setData(TestUtils.getTestArray(0)); 630 data.setData(TestUtils.getTestArray(0)); 631 try { 632 myDb.put(txn, key, data); 633 fail("expected DatabaseException because open RDONLY"); 634 } catch (DatabaseException DBE) { 635 } 636 637 key.setData(TestUtils.getTestArray(0)); 638 data.setData(TestUtils.getTestArray(0)); 639 assertEquals(OperationStatus.SUCCESS, 640 myDb.get(txn, key, data, LockMode.DEFAULT)); 641 642 Cursor cursor = myDb.openCursor(txn, null); 643 644 assertEquals(OperationStatus.SUCCESS, 645 cursor.getFirst(key, data, LockMode.DEFAULT)); 646 647 try { 648 cursor.delete(); 649 fail("expected Exception from delete on RD_ONLY db"); 650 } catch (DatabaseException DBE) { 651 } 652 653 key.setData(TestUtils.getTestArray(1)); 654 data.setData(TestUtils.getTestArray(1)); 655 try { 656 myDb.put(txn, key, data); 657 fail("expected DatabaseException because open RDONLY"); 658 } catch (DatabaseException DBE) { 659 } 660 661 cursor.close(); 662 txn.commit(); 663 myDb.close(); 664 665 env.close(); 666 } catch (Throwable t) { 667 t.printStackTrace(); 668 throw t; 669 } 670 } 671 672 675 public void testExclusive() 676 throws Throwable { 677 678 try { 679 EnvironmentConfig envConfig = TestUtils.initEnvConfig(); 680 681 685 envConfig.setAllowCreate(true); 686 env = new Environment(envHome, envConfig); 687 DatabaseConfig dbConfig = new DatabaseConfig(); 688 dbConfig.setAllowCreate(true); 689 dbConfig.setExclusiveCreate(true); 690 691 692 Database dbA = env.openDatabase(null, "foo", dbConfig); 693 dbA.close(); 694 695 696 try { 697 env.openDatabase(null, "foo", dbConfig); 698 fail("Database already exists"); 699 } catch (DatabaseException e) { 700 } 701 env.close(); 702 } catch (Throwable t) { 703 t.printStackTrace(); 704 throw t; 705 } 706 } 707 708 712 private class BadComparator1 implements Comparator { 713 public BadComparator1(int foo) { 714 } 715 716 public int compare(Object o1, Object o2) { 717 return 0; 718 } 719 } 720 721 725 public static class BadComparator2 implements Comparator { 726 public BadComparator2(int i) { 727 } 728 729 public int compare(Object o1, Object o2) { 730 return 0; 731 } 732 } 733 734 737 public static class TestComparator implements Comparator { 738 public TestComparator() { 739 } 740 741 public int compare(Object o1, Object o2) { 742 return 0; 743 } 744 } 745 746 749 public static class TestComparator2 implements Comparator { 750 public TestComparator2() { 751 } 752 753 public int compare(Object o1, Object o2) { 754 return 0; 755 } 756 } 757 758 761 public class BadSerialComparator1 implements Comparator { 762 763 public BadSerialComparator1() { 764 } 765 766 public int compare(Object o1, Object o2) { 767 return 0; 768 } 769 } 770 771 775 public class BadSerialComparator2 implements Comparator , Serializable { 776 777 private BadSerialComparator1 o = new BadSerialComparator1(); 778 779 public BadSerialComparator2() { 780 } 781 782 public int compare(Object o1, Object o2) { 783 return 0; 784 } 785 } 786 787 791 private static class TestSerialComparator 792 implements Comparator , Serializable { 793 794 private String s = "sss"; 795 796 private TestSerialComparator() { 797 } 798 799 public int compare(Object o1, Object o2) { 800 return 0; 801 } 802 803 public boolean equals(Object other) { 804 TestSerialComparator o = (TestSerialComparator) other; 805 return s.equals(o.s); 806 } 807 } 808 809 812 public static class TestSerialComparator2 813 implements Comparator , Serializable { 814 815 public int compare(Object o1, Object o2) { 816 return 0; 817 } 818 } 819 820 public static class SecKeyCreator1 implements SecondaryKeyCreator { 821 public boolean createSecondaryKey(SecondaryDatabase secondary, 822 DatabaseEntry key, 823 DatabaseEntry data, 824 DatabaseEntry result) 825 throws DatabaseException { 826 827 return true; 828 } 829 830 public boolean equals(Object o) { 831 if (o == null) { 832 return false; 833 } 834 return (o.getClass() == getClass()); 835 } 836 } 837 838 public static class SecKeyCreator2 implements SecondaryKeyCreator { 839 public boolean createSecondaryKey(SecondaryDatabase secondary, 840 DatabaseEntry key, 841 DatabaseEntry data, 842 DatabaseEntry result) 843 throws DatabaseException { 844 845 return true; 846 } 847 848 public boolean equals(Object o) { 849 if (o == null) { 850 return false; 851 } 852 return (o.getClass() == getClass()); 853 } 854 } 855 856 public static class SecMultiKeyCreator1 857 implements SecondaryMultiKeyCreator { 858 public void createSecondaryKeys(SecondaryDatabase secondary, 859 DatabaseEntry key, 860 DatabaseEntry data, 861 Set results) 862 throws DatabaseException { 863 } 864 865 public boolean equals(Object o) { 866 if (o == null) { 867 return false; 868 } 869 return (o.getClass() == getClass()); 870 } 871 } 872 873 public static class ForeignKeyNullifier1 implements ForeignKeyNullifier { 874 public boolean nullifyForeignKey(SecondaryDatabase secondary, 875 DatabaseEntry data) 876 throws DatabaseException { 877 878 return true; 879 } 880 } 881 882 public static class ForeignMultiKeyNullifier1 883 implements ForeignMultiKeyNullifier { 884 public boolean nullifyForeignKey(SecondaryDatabase secondary, 885 DatabaseEntry key, 886 DatabaseEntry data, 887 DatabaseEntry secKey) 888 throws DatabaseException { 889 890 return true; 891 } 892 } 893 } 894 | Popular Tags |