1 8 9 package com.sleepycat.collections.test; 10 11 import java.util.ArrayList ; 12 import java.util.Collection ; 13 import java.util.Collections ; 14 import java.util.Enumeration ; 15 import java.util.HashMap ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.ListIterator ; 19 import java.util.Map ; 20 import java.util.NoSuchElementException ; 21 import java.util.Set ; 22 import java.util.SortedMap ; 23 import java.util.SortedSet ; 24 25 import junit.framework.Test; 26 import junit.framework.TestCase; 27 import junit.framework.TestSuite; 28 29 import com.sleepycat.bind.EntityBinding; 30 import com.sleepycat.bind.EntryBinding; 31 import com.sleepycat.collections.MapEntryParameter; 32 import com.sleepycat.collections.StoredCollection; 33 import com.sleepycat.collections.StoredCollections; 34 import com.sleepycat.collections.StoredContainer; 35 import com.sleepycat.collections.StoredEntrySet; 36 import com.sleepycat.collections.StoredIterator; 37 import com.sleepycat.collections.StoredKeySet; 38 import com.sleepycat.collections.StoredList; 39 import com.sleepycat.collections.StoredMap; 40 import com.sleepycat.collections.StoredSortedEntrySet; 41 import com.sleepycat.collections.StoredSortedKeySet; 42 import com.sleepycat.collections.StoredSortedMap; 43 import com.sleepycat.collections.StoredSortedValueSet; 44 import com.sleepycat.collections.StoredValueSet; 45 import com.sleepycat.collections.TransactionRunner; 46 import com.sleepycat.collections.TransactionWorker; 47 import com.sleepycat.compat.DbCompat; 48 import com.sleepycat.je.Database; 49 import com.sleepycat.je.DatabaseException; 50 import com.sleepycat.je.Environment; 51 import com.sleepycat.util.ExceptionUnwrapper; 52 53 56 public class CollectionTest extends TestCase { 57 58 private static final int NONE = 0; 59 private static final int SUB = 1; 60 private static final int HEAD = 2; 61 private static final int TAIL = 3; 62 63 70 private static final int DEFAULT_MAX_KEY = 6; 71 private static final int[] MAX_KEYS = {6, 10, 14, 22}; 72 73 private boolean testStoredIterator; 74 private int maxKey; 75 private int beginKey = 1; 76 private int endKey; 77 78 private Environment env; 79 private Database store; 80 private Database index; 81 private boolean isEntityBinding; 82 private boolean isAutoCommit; 83 private TestStore testStore; 84 private String testName; 85 private EntryBinding keyBinding; 86 private EntryBinding valueBinding; 87 private EntityBinding entityBinding; 88 private TransactionRunner readRunner; 89 private TransactionRunner writeRunner; 90 private TransactionRunner writeIterRunner; 91 private TestEnv testEnv; 92 93 private StoredMap map; 94 private StoredMap imap; private StoredSortedMap smap; private StoredMap saveMap; 97 private StoredSortedMap saveSMap; 98 private int rangeType; 99 private StoredList list; 100 private StoredList ilist; private StoredList saveList; 102 private StoredKeySet keySet; 103 private StoredValueSet valueSet; 104 105 109 public static void main(String [] args) 110 throws Exception { 111 112 if (args.length == 1 && 113 (args[0].equals("-h") || args[0].equals("-help"))) { 114 usage(); 115 } else { 116 junit.framework.TestResult tr = 117 junit.textui.TestRunner.run(suite(args)); 118 if (tr.errorCount() > 0 || 119 tr.failureCount() > 0) { 120 System.exit(1); 121 } else { 122 System.exit(0); 123 } 124 } 125 } 126 127 private static void usage() { 128 129 System.out.println( 130 "Usage: java com.sleepycat.collections.test.CollectionTest\n" + 131 " -h | -help\n" + 132 " [testName]...\n" + 133 " where testName has the format:\n" + 134 " <env>-<store>-{entity|value}\n" + 135 " <env> is:\n" + 136 " bdb | cdb | txn\n" + 137 " <store> is:\n" + 138 " btree-uniq | btree-dup | btree-dupsort | btree-recnum |\n" + 139 " hash-uniq | hash-dup | hash-dupsort |\n" + 140 " queue | recno | recno-renum\n" + 141 " For example: bdb-btree-uniq-entity\n" + 142 " If no arguments are given then all tests are run."); 143 System.exit(2); 144 } 145 146 public static Test suite() 147 throws Exception { 148 149 return suite(null); 150 } 151 152 static Test suite(String [] args) 153 throws Exception { 154 155 if ("true".equals(System.getProperty("longtest"))) { 156 TestSuite suite = new TestSuite(); 157 158 159 permuteTests(args, suite, true, DEFAULT_MAX_KEY); 160 161 162 for (int i = 0; i < MAX_KEYS.length; i += 1) { 163 permuteTests(args, suite, false, MAX_KEYS[i]); 164 } 165 166 return suite; 167 } else { 168 return baseSuite(args); 169 } 170 } 171 172 private static void permuteTests(String [] args, 173 TestSuite suite, 174 boolean storedIter, 175 int maxKey) 176 throws Exception { 177 178 TestSuite baseTests = baseSuite(args); 179 Enumeration e = baseTests.tests(); 180 while (e.hasMoreElements()) { 181 CollectionTest t = (CollectionTest) e.nextElement(); 182 t.setParams(storedIter, maxKey); 183 suite.addTest(t); 184 } 185 } 186 187 private static TestSuite baseSuite(String [] args) 188 throws Exception { 189 190 TestSuite suite = new TestSuite(); 191 for (int i = 0; i < TestEnv.ALL.length; i += 1) { 192 for (int j = 0; j < TestStore.ALL.length; j += 1) { 193 for (int k = 0; k < 2; k += 1) { 194 boolean entityBinding = (k != 0); 195 196 addTest(args, suite, new CollectionTest( 197 TestEnv.ALL[i], TestStore.ALL[j], 198 entityBinding, false)); 199 200 if (TestEnv.ALL[i].isTxnMode()) { 201 addTest(args, suite, new CollectionTest( 202 TestEnv.ALL[i], TestStore.ALL[j], 203 entityBinding, true)); 204 } 205 } 206 } 207 } 208 return suite; 209 } 210 211 private static void addTest(String [] args, TestSuite suite, 212 CollectionTest test) { 213 214 if (args == null || args.length == 0) { 215 suite.addTest(test); 216 } else { 217 for (int t = 0; t < args.length; t += 1) { 218 if (args[t].equals(test.testName)) { 219 suite.addTest(test); 220 break; 221 } 222 } 223 } 224 } 225 226 public CollectionTest(TestEnv testEnv, TestStore testStore, 227 boolean isEntityBinding, boolean isAutoCommit) { 228 229 super(null); 230 231 this.testEnv = testEnv; 232 this.testStore = testStore; 233 this.isEntityBinding = isEntityBinding; 234 this.isAutoCommit = isAutoCommit; 235 236 keyBinding = testStore.getKeyBinding(); 237 valueBinding = testStore.getValueBinding(); 238 entityBinding = testStore.getEntityBinding(); 239 240 setParams(false, DEFAULT_MAX_KEY); 241 } 242 243 private void setParams(boolean storedIter, int maxKey) { 244 245 this.testStoredIterator = storedIter; 246 this.maxKey = maxKey; 247 this.endKey = maxKey; 248 249 testName = testEnv.getName() + '-' + testStore.getName() + 250 (isEntityBinding ? "-entity" : "-value") + 251 (isAutoCommit ? "-autoCommit" : "") + 252 (testStoredIterator ? "-storedIter" : "") + 253 ((maxKey != DEFAULT_MAX_KEY) ? ("-maxKey-" + maxKey) : ""); 254 } 255 256 public void tearDown() 257 throws Exception { 258 259 setName(testName); 260 } 261 262 public void runTest() 263 throws Exception { 264 265 DbTestUtil.printTestName(DbTestUtil.qualifiedTestName(this)); 266 try { 267 env = testEnv.open(testName); 268 269 TransactionRunner normalRunner = newTransactionRunner(env); 275 normalRunner.setAllowNestedTransactions( 276 DbCompat.NESTED_TRANSACTIONS); 277 TransactionRunner nullRunner = new NullTransactionRunner(env); 278 readRunner = nullRunner; 279 writeIterRunner = normalRunner; 280 if (isAutoCommit) { 281 writeRunner = nullRunner; 282 } else { 283 writeRunner = normalRunner; 284 } 285 286 store = testStore.open(env, "unindexed.db"); 287 testUnindexed(); 288 store.close(); 289 store = null; 290 291 TestStore indexOf = testStore.getIndexOf(); 292 if (indexOf != null) { 293 store = indexOf.open(env, "indexed.db"); 294 index = testStore.openIndex(store, "index.db"); 295 testIndexed(); 296 index.close(); 297 index = null; 298 store.close(); 299 store = null; 300 } 301 env.close(); 302 env = null; 303 } catch (Exception e) { 304 throw ExceptionUnwrapper.unwrap(e); 305 } finally { 306 if (index != null) { 307 try { 308 index.close(); 309 } catch (Exception e) { 310 } 311 } 312 if (store != null) { 313 try { 314 store.close(); 315 } catch (Exception e) { 316 } 317 } 318 if (env != null) { 319 try { 320 env.close(); 321 } catch (Exception e) { 322 } 323 } 324 325 index = null; 326 store = null; 327 env = null; 328 readRunner = null; 329 writeRunner = null; 330 writeIterRunner = null; 331 map = null; 332 imap = null; 333 smap = null; 334 saveMap = null; 335 saveSMap = null; 336 list = null; 337 ilist = null; 338 saveList = null; 339 keySet = null; 340 valueSet = null; 341 testEnv = null; 342 testStore = null; 343 } 344 } 345 346 349 protected TransactionRunner newTransactionRunner(Environment env) 350 throws DatabaseException { 351 352 return new TransactionRunner(env); 353 } 354 355 void testCreation(StoredContainer cont, int expectSize) 356 throws Exception { 357 358 assertEquals(index != null, cont.isSecondary()); 359 assertEquals(testStore.isOrdered(), cont.isOrdered()); 360 assertEquals(testStore.areKeysRenumbered(), cont.areKeysRenumbered()); 361 assertEquals(testStore.areDuplicatesAllowed(), 362 cont.areDuplicatesAllowed()); 363 assertEquals(testEnv.isTxnMode(), cont.isTransactional()); 364 assertEquals(expectSize, cont.size()); 365 } 366 367 void testMapCreation(Map map) 368 throws Exception { 369 370 assertTrue(map.values() instanceof Set ); 371 assertEquals(testStore.isOrdered(), 372 map.keySet() instanceof SortedSet ); 373 assertEquals(testStore.isOrdered(), 374 map.entrySet() instanceof SortedSet ); 375 assertEquals(testStore.isOrdered() && isEntityBinding, 376 map.values() instanceof SortedSet ); 377 } 378 379 void testUnindexed() 380 throws Exception { 381 382 if (testStore.isOrdered()) { 384 if (isEntityBinding) { 385 smap = new StoredSortedMap(store, keyBinding, 386 entityBinding, 387 testStore.getKeyAssigner()); 388 valueSet = new StoredSortedValueSet(store, entityBinding, 389 true); 390 } else { 391 smap = new StoredSortedMap(store, keyBinding, 392 valueBinding, 393 testStore.getKeyAssigner()); 394 } 397 keySet = new StoredSortedKeySet(store, keyBinding, true); 398 map = smap; 399 } else { 400 if (isEntityBinding) { 401 map = new StoredMap(store, keyBinding, entityBinding, 402 testStore.getKeyAssigner()); 403 valueSet = new StoredValueSet(store, entityBinding, true); 404 } else { 405 map = new StoredMap(store, keyBinding, valueBinding, 406 testStore.getKeyAssigner()); 407 valueSet = new StoredValueSet(store, valueBinding, true); 408 } 409 smap = null; 410 keySet = new StoredKeySet(store, keyBinding, true); 411 } 412 imap = map; 413 414 if (testStore.hasRecNumAccess()) { 416 if (isEntityBinding) { 417 ilist = new StoredList(store, entityBinding, 418 testStore.getKeyAssigner()); 419 } else { 420 ilist = new StoredList(store, valueBinding, 421 testStore.getKeyAssigner()); 422 } 423 list = ilist; 424 } else { 425 try { 426 if (isEntityBinding) { 427 ilist = new StoredList(store, entityBinding, 428 testStore.getKeyAssigner()); 429 } else { 430 ilist = new StoredList(store, valueBinding, 431 testStore.getKeyAssigner()); 432 } 433 fail(); 434 } catch (IllegalArgumentException expected) {} 435 } 436 437 testCreation(map, 0); 438 if (list != null) { 439 testCreation(list, 0); 440 assertNotNull(smap); 441 } 442 testMapCreation(map); 443 addAll(); 444 testAll(); 445 } 446 447 void testIndexed() 448 throws Exception { 449 450 if (isEntityBinding) { 452 map = new StoredMap(store, keyBinding, entityBinding, 453 testStore.getKeyAssigner()); 454 } else { 455 map = new StoredMap(store, keyBinding, valueBinding, 456 testStore.getKeyAssigner()); 457 } 458 imap = map; 459 smap = null; 460 if (testStore.hasRecNumAccess()) { 462 if (isEntityBinding) { 463 list = new StoredList(store, entityBinding, 464 testStore.getKeyAssigner()); 465 } else { 466 list = new StoredList(store, valueBinding, 467 testStore.getKeyAssigner()); 468 } 469 ilist = list; 470 } 471 472 addAll(); 473 readAll(); 474 475 if (testStore.isOrdered()) { 477 if (isEntityBinding) { 478 map = smap = new StoredSortedMap(index, keyBinding, 479 entityBinding, true); 480 valueSet = new StoredSortedValueSet(index, entityBinding, 481 true); 482 } else { 483 map = smap = new StoredSortedMap(index, keyBinding, 484 valueBinding, true); 485 } 488 keySet = new StoredSortedKeySet(index, keyBinding, true); 489 } else { 490 if (isEntityBinding) { 491 map = new StoredMap(index, keyBinding, entityBinding, true); 492 valueSet = new StoredValueSet(index, entityBinding, true); 493 } else { 494 map = new StoredMap(index, keyBinding, valueBinding, true); 495 valueSet = new StoredValueSet(index, valueBinding, true); 496 } 497 smap = null; 498 keySet = new StoredKeySet(index, keyBinding, true); 499 } 500 501 if (testStore.hasRecNumAccess()) { 503 if (isEntityBinding) { 504 list = new StoredList(index, entityBinding, true); 505 } else { 506 list = new StoredList(index, valueBinding, true); 507 } 508 } else { 509 try { 510 if (isEntityBinding) { 511 list = new StoredList(index, entityBinding, true); 512 } else { 513 list = new StoredList(index, valueBinding, true); 514 } 515 fail(); 516 } 517 catch (IllegalArgumentException expected) {} 518 } 519 520 testCreation(map, maxKey); 521 testCreation((StoredContainer) map.values(), maxKey); 522 testCreation((StoredContainer) map.keySet(), maxKey); 523 testCreation((StoredContainer) map.entrySet(), maxKey); 524 if (list != null) { 525 testCreation(list, maxKey); 526 assertNotNull(smap); 527 } 528 testMapCreation(map); 529 testAll(); 530 } 531 532 void testAll() 533 throws Exception { 534 535 checkKeySetAndValueSet(); 536 readAll(); 537 updateAll(); 538 readAll(); 539 if (!map.areKeysRenumbered()) { 540 removeOdd(); 541 readEven(); 542 addOdd(); 543 readAll(); 544 removeOddIter(); 545 readEven(); 546 if (imap.areDuplicatesAllowed()) { 547 addOddDup(); 548 } else { 549 addOdd(); 550 } 551 readAll(); 552 removeOddEntry(); 553 readEven(); 554 addOdd(); 555 readAll(); 556 if (isEntityBinding) { 557 removeOddEntity(); 558 readEven(); 559 addOddEntity(); 560 readAll(); 561 } 562 bulkOperations(); 563 } 564 if (isListAddAllowed()) { 565 removeOddList(); 566 readEvenList(); 567 addOddList(); 568 readAll(); 569 if (!isEntityBinding) { 570 removeOddListValue(); 571 readEvenList(); 572 addOddList(); 573 readAll(); 574 } 575 } 576 if (list != null) { 577 bulkListOperations(); 578 } else { 579 listOperationsNotAllowed(); 580 } 581 if (smap != null) { 582 readWriteRange(SUB, 1, 1); 583 readWriteRange(HEAD, 1, 1); 584 readWriteRange(SUB, 1, maxKey); 585 readWriteRange(HEAD, 1, maxKey); 586 readWriteRange(TAIL, 1, maxKey); 587 readWriteRange(SUB, 1, 3); 588 readWriteRange(HEAD, 1, 3); 589 readWriteRange(SUB, 2, 2); 590 readWriteRange(SUB, 2, maxKey); 591 readWriteRange(TAIL, 2, maxKey); 592 readWriteRange(SUB, maxKey, maxKey); 593 readWriteRange(TAIL, maxKey, maxKey); 594 readWriteRange(SUB, maxKey + 1, maxKey + 1); 595 readWriteRange(TAIL, maxKey + 1, maxKey + 1); 596 readWriteRange(SUB, 0, 0); 597 readWriteRange(HEAD, 0, 0); 598 } 599 updateAll(); 600 readAll(); 601 if (map.areDuplicatesAllowed()) { 602 readWriteDuplicates(); 603 readAll(); 604 } else { 605 duplicatesNotAllowed(); 606 readAll(); 607 } 608 if (testEnv.isCdbMode()) { 609 testCdbLocking(); 610 } 611 removeAll(); 612 if (isListAddAllowed()) { 613 testIterAddList(); 614 clearAll(); 615 } 616 if (imap.areDuplicatesAllowed()) { 617 testIterAddDuplicates(); 618 clearAll(); 619 } 620 if (isListAddAllowed()) { 621 addAllList(); 622 readAll(); 623 removeAllList(); 624 } 625 appendAll(); 626 } 627 628 void checkKeySetAndValueSet() { 629 630 assertTrue(imap.keySet().equals(keySet)); 633 if (valueSet != null) { 634 assertTrue(imap.values().equals(valueSet)); 635 } 636 } 637 638 Iterator iterator(Collection storedCollection) { 639 640 if (testStoredIterator) { 641 return ((StoredCollection) storedCollection).storedIterator(); 642 } else { 643 return storedCollection.iterator(); 644 } 645 } 646 647 void addAll() 648 throws Exception { 649 650 writeRunner.run(new TransactionWorker() { 651 public void doWork() throws Exception { 652 assertTrue(imap.isEmpty()); 653 Iterator iter = iterator(imap.entrySet()); 654 try { 655 assertTrue(!iter.hasNext()); 656 } finally { 657 StoredIterator.close(iter); 658 } 659 assertEquals(0, imap.keySet().toArray().length); 660 assertEquals(0, imap.keySet().toArray(new Object [0]).length); 661 assertEquals(0, imap.entrySet().toArray().length); 662 assertEquals(0, imap.entrySet().toArray(new Object [0]).length); 663 assertEquals(0, imap.values().toArray().length); 664 assertEquals(0, imap.values().toArray(new Object [0]).length); 665 666 for (int i = beginKey; i <= endKey; i += 1) { 667 Long key = makeKey(i); 668 Object val = makeVal(i); 669 assertNull(imap.get(key)); 670 assertTrue(!imap.keySet().contains(key)); 671 assertTrue(!imap.values().contains(val)); 672 assertNull(imap.put(key, val)); 673 assertEquals(val, imap.get(key)); 674 assertTrue(imap.keySet().contains(key)); 675 assertTrue(imap.values().contains(val)); 676 assertTrue(imap.duplicates(key).contains(val)); 677 if (!imap.areDuplicatesAllowed()) { 678 assertEquals(val, imap.put(key, val)); 679 } 680 checkDupsSize(1, imap.duplicates(key)); 681 } 682 assertTrue(!imap.isEmpty()); 683 } 684 }); 685 } 686 687 void appendAll() 688 throws Exception { 689 690 writeRunner.run(new TransactionWorker() { 691 public void doWork() throws Exception { 692 assertTrue(imap.isEmpty()); 693 694 TestKeyAssigner keyAssigner = testStore.getKeyAssigner(); 695 if (keyAssigner != null) { 696 keyAssigner.reset(); 697 } 698 699 for (int i = beginKey; i <= endKey; i += 1) { 700 boolean useList = (i & 1) == 0; 701 Long key = makeKey(i); 702 Object val = makeVal(i); 703 assertNull(imap.get(key)); 704 if (keyAssigner != null) { 705 if (useList && ilist != null) { 706 assertEquals(i - 1, ilist.append(val)); 707 } else { 708 assertEquals(key, imap.append(val)); 709 } 710 assertEquals(val, imap.get(key)); 711 } else { 712 Long recnoKey; 713 if (useList && ilist != null) { 714 recnoKey = new Long (ilist.append(val) + 1); 715 } else { 716 recnoKey = (Long ) imap.append(val); 717 } 718 assertNotNull(recnoKey); 719 Object recnoVal; 720 if (isEntityBinding) { 721 recnoVal = makeEntity(recnoKey.intValue(), i); 722 } else { 723 recnoVal = val; 724 } 725 assertEquals(recnoVal, imap.get(recnoKey)); 726 } 727 } 728 } 729 }); 730 } 731 732 void updateAll() 733 throws Exception { 734 735 writeRunner.run(new TransactionWorker() { 736 public void doWork() throws Exception { 737 for (int i = beginKey; i <= endKey; i += 1) { 738 Long key = makeKey(i); 739 Object val = makeVal(i); 740 if (!imap.areDuplicatesAllowed()) { 741 assertEquals(val, imap.put(key, val)); 742 } 743 if (isEntityBinding) { 744 assertTrue(!imap.values().add(val)); 745 } 746 checkDupsSize(1, imap.duplicates(key)); 747 if (ilist != null) { 748 int idx = i - 1; 749 assertEquals(val, ilist.set(idx, val)); 750 } 751 } 752 updateIter(map.entrySet()); 753 updateIter(map.values()); 754 if (beginKey <= endKey) { 755 ListIterator iter = (ListIterator ) iterator(map.keySet()); 756 try { 757 assertNotNull(iter.next()); 758 iter.set(makeKey(beginKey)); 759 fail(); 760 } catch (UnsupportedOperationException e) { 761 } finally { 762 StoredIterator.close(iter); 763 } 764 } 765 if (list != null) { 766 updateIter(list); 767 } 768 } 769 }); 770 } 771 772 void updateIter(final Collection coll) 773 throws Exception { 774 775 writeIterRunner.run(new TransactionWorker() { 776 public void doWork() throws Exception { 777 ListIterator iter = (ListIterator ) iterator(coll); 778 try { 779 for (int i = beginKey; i <= endKey; i += 1) { 780 assertTrue(iter.hasNext()); 781 Object obj = iter.next(); 782 if (index != null) { 783 try { 784 setValuePlusOne(iter, obj); 785 fail(); 786 } 787 catch (UnsupportedOperationException e) {} 788 } else if 789 (((StoredCollection) coll).areDuplicatesOrdered()) { 790 try { 791 setValuePlusOne(iter, obj); 792 fail(); 793 } catch (RuntimeException e) { 794 Exception e2 = ExceptionUnwrapper.unwrap(e); 795 assertTrue(e2.getClass().getName(), 796 e2 instanceof IllegalArgumentException || 797 e2 instanceof DatabaseException); 798 } 799 } else { 800 setValuePlusOne(iter, obj); 801 } 802 } 803 assertTrue(!iter.hasNext()); 804 } finally { 805 StoredIterator.close(iter); 806 } 807 } 808 }); 809 } 810 811 void setValuePlusOne(ListIterator iter, Object obj) { 812 813 if (obj instanceof Map.Entry ) { 814 Map.Entry entry = (Map.Entry ) obj; 815 Long key = (Long ) entry.getKey(); 816 Object oldVal = entry.getValue(); 817 Object val = makeVal(key.intValue() + 1); 818 if (isEntityBinding) { 819 try { 820 entry.setValue(val); 822 fail(); 823 } 824 catch (IllegalArgumentException e) {} 825 val = makeEntity(key.intValue(), key.intValue() + 1); 826 } 827 entry.setValue(val); 828 assertEquals(val, entry.getValue()); 829 assertEquals(val, map.get(key)); 830 assertTrue(map.duplicates(key).contains(val)); 831 checkDupsSize(1, map.duplicates(key)); 832 entry.setValue(oldVal); 833 assertEquals(oldVal, entry.getValue()); 834 assertEquals(oldVal, map.get(key)); 835 assertTrue(map.duplicates(key).contains(oldVal)); 836 checkDupsSize(1, map.duplicates(key)); 837 } else { 838 Object oldVal = obj; 839 Long key = makeKey(intVal(obj)); 840 Object val = makeVal(key.intValue() + 1); 841 if (isEntityBinding) { 842 try { 843 iter.set(val); 845 fail(); 846 } 847 catch (IllegalArgumentException e) {} 848 val = makeEntity(key.intValue(), key.intValue() + 1); 849 } 850 iter.set(val); 851 assertEquals(val, map.get(key)); 852 assertTrue(map.duplicates(key).contains(val)); 853 checkDupsSize(1, map.duplicates(key)); 854 iter.set(oldVal); 855 assertEquals(oldVal, map.get(key)); 856 assertTrue(map.duplicates(key).contains(oldVal)); 857 checkDupsSize(1, map.duplicates(key)); 858 } 859 } 860 861 void removeAll() 862 throws Exception { 863 864 writeIterRunner.run(new TransactionWorker() { 865 public void doWork() throws Exception { 866 assertTrue(!map.isEmpty()); 867 ListIterator iter = null; 868 try { 869 if (list != null) { 870 iter = (ListIterator ) iterator(list); 871 } else { 872 iter = (ListIterator ) iterator(map.values()); 873 } 874 iteratorSetAndRemoveNotAllowed(iter); 875 876 Object val = iter.next(); 877 assertNotNull(val); 878 iter.remove(); 879 iteratorSetAndRemoveNotAllowed(iter); 880 881 if (index == null) { 882 val = iter.next(); 883 assertNotNull(val); 884 iter.set(val); 885 886 if (map.areDuplicatesAllowed()) { 887 iter.add(makeVal(intVal(val), intVal(val) + 1)); 888 iteratorSetAndRemoveNotAllowed(iter); 889 } 890 } 891 } finally { 892 StoredIterator.close(iter); 893 } 894 map.clear(); 895 assertTrue(map.isEmpty()); 896 assertTrue(map.entrySet().isEmpty()); 897 assertTrue(map.keySet().isEmpty()); 898 assertTrue(map.values().isEmpty()); 899 for (int i = beginKey; i <= endKey; i += 1) { 900 Long key = makeKey(i); 901 Object val = makeVal(i); 902 assertNull(map.get(key)); 903 assertTrue(!map.duplicates(key).contains(val)); 904 checkDupsSize(0, map.duplicates(key)); 905 } 906 } 907 }); 908 } 909 910 void clearAll() 911 throws Exception { 912 913 writeRunner.run(new TransactionWorker() { 914 public void doWork() throws Exception { 915 map.clear(); 916 assertTrue(map.isEmpty()); 917 } 918 }); 919 } 920 921 void iteratorSetAndRemoveNotAllowed(ListIterator i) { 922 923 try { 924 i.remove(); 925 fail(); 926 } catch (IllegalStateException e) {} 927 928 if (index == null) { 929 try { 930 Object val = makeVal(1); 931 i.set(val); 932 fail(); 933 } 934 catch (IllegalStateException e) {} 935 } 936 } 937 938 void removeOdd() 939 throws Exception { 940 941 writeRunner.run(new TransactionWorker() { 942 public void doWork() throws Exception { 943 boolean toggle = false; 944 for (int i = beginKey; i <= endKey; i += 2) { 945 toggle = !toggle; 946 Long key = makeKey(i); 947 Object val = makeVal(i); 948 if (toggle) { 949 assertTrue(map.keySet().contains(key)); 950 assertTrue(map.keySet().remove(key)); 951 assertTrue(!map.keySet().contains(key)); 952 } else { 953 assertTrue(map.containsValue(val)); 954 Object oldVal = map.remove(key); 955 assertEquals(oldVal, val); 956 assertTrue(!map.containsKey(key)); 957 assertTrue(!map.containsValue(val)); 958 } 959 assertNull(map.get(key)); 960 assertTrue(!map.duplicates(key).contains(val)); 961 checkDupsSize(0, map.duplicates(key)); 962 } 963 } 964 }); 965 } 966 967 void removeOddEntity() 968 throws Exception { 969 970 writeRunner.run(new TransactionWorker() { 971 public void doWork() throws Exception { 972 for (int i = beginKey; i <= endKey; i += 2) { 973 Long key = makeKey(i); 974 Object val = makeVal(i); 975 assertTrue(map.values().contains(val)); 976 assertTrue(map.values().remove(val)); 977 assertTrue(!map.values().contains(val)); 978 assertNull(map.get(key)); 979 assertTrue(!map.duplicates(key).contains(val)); 980 checkDupsSize(0, map.duplicates(key)); 981 } 982 } 983 }); 984 } 985 986 void removeOddEntry() 987 throws Exception { 988 989 writeRunner.run(new TransactionWorker() { 990 public void doWork() throws Exception { 991 for (int i = beginKey; i <= endKey; i += 2) { 992 Long key = makeKey(i); 993 Object val = mapEntry(i); 994 assertTrue(map.entrySet().contains(val)); 995 assertTrue(map.entrySet().remove(val)); 996 assertTrue(!map.entrySet().contains(val)); 997 assertNull(map.get(key)); 998 } 999 } 1000 }); 1001 } 1002 1003 void removeOddIter() 1004 throws Exception { 1005 1006 writeIterRunner.run(new TransactionWorker() { 1007 public void doWork() throws Exception { 1008 Iterator iter = iterator(map.keySet()); 1009 try { 1010 for (int i = beginKey; i <= endKey; i += 1) { 1011 assertTrue(iter.hasNext()); 1012 Long key = (Long ) iter.next(); 1013 assertNotNull(key); 1014 if (map instanceof SortedMap ) { 1015 assertEquals(makeKey(i), key); 1016 } 1017 if ((key.intValue() & 1) != 0) { 1018 iter.remove(); 1019 } 1020 } 1021 } finally { 1022 StoredIterator.close(iter); 1023 } 1024 } 1025 }); 1026 } 1027 1028 void removeOddList() 1029 throws Exception { 1030 1031 writeRunner.run(new TransactionWorker() { 1032 public void doWork() throws Exception { 1033 for (int i = beginKey; i <= endKey; i += 2) { 1034 int idx = (i - beginKey) / 2; 1038 Object val = makeVal(i); 1039 if (!isEntityBinding) { 1040 assertTrue(list.contains(val)); 1041 assertEquals(val, list.get(idx)); 1042 assertEquals(idx, list.indexOf(val)); 1043 } 1044 assertNotNull(list.get(idx)); 1045 if (isEntityBinding) { 1046 assertNotNull(list.remove(idx)); 1047 } else { 1048 assertTrue(list.contains(val)); 1049 assertEquals(val, list.remove(idx)); 1050 } 1051 assertTrue(!list.remove(val)); 1052 assertTrue(!list.contains(val)); 1053 assertTrue(!val.equals(list.get(idx))); 1054 } 1055 } 1056 }); 1057 } 1058 1059 void removeOddListValue() 1060 throws Exception { 1061 1062 writeRunner.run(new TransactionWorker() { 1063 public void doWork() throws Exception { 1064 for (int i = beginKey; i <= endKey; i += 2) { 1065 int idx = (i - beginKey) / 2; 1069 Object val = makeVal(i); 1070 assertTrue(list.contains(val)); 1071 assertEquals(val, list.get(idx)); 1072 assertEquals(idx, list.indexOf(val)); 1073 assertTrue(list.remove(val)); 1074 assertTrue(!list.remove(val)); 1075 assertTrue(!list.contains(val)); 1076 assertTrue(!val.equals(list.get(idx))); 1077 } 1078 } 1079 }); 1080 } 1081 1082 void addOdd() 1083 throws Exception { 1084 1085 writeRunner.run(new TransactionWorker() { 1086 public void doWork() throws Exception { 1087 for (int i = beginKey; i <= endKey; i += 2) { 1089 Long key = makeKey(i); 1090 Object val = makeVal(i); 1091 assertNull(imap.get(key)); 1092 assertNull(imap.put(key, val)); 1093 assertEquals(val, imap.get(key)); 1094 assertTrue(imap.duplicates(key).contains(val)); 1095 checkDupsSize(1, imap.duplicates(key)); 1096 if (isEntityBinding) { 1097 assertTrue(!imap.values().add(val)); 1098 } 1099 if (!imap.areDuplicatesAllowed()) { 1100 assertEquals(val, imap.put(key, val)); 1101 } 1102 } 1103 } 1104 }); 1105 } 1106 1107 void addOddEntity() 1108 throws Exception { 1109 1110 writeRunner.run(new TransactionWorker() { 1111 public void doWork() throws Exception { 1112 for (int i = beginKey; i <= endKey; i += 2) { 1114 Long key = makeKey(i); 1115 Object val = makeVal(i); 1116 assertNull(imap.get(key)); 1117 assertTrue(!imap.values().contains(val)); 1118 assertTrue(imap.values().add(val)); 1119 assertEquals(val, imap.get(key)); 1120 assertTrue(imap.values().contains(val)); 1121 assertTrue(imap.duplicates(key).contains(val)); 1122 checkDupsSize(1, imap.duplicates(key)); 1123 if (isEntityBinding) { 1124 assertTrue(!imap.values().add(val)); 1125 } 1126 } 1127 } 1128 }); 1129 } 1130 1131 void addOddDup() 1132 throws Exception { 1133 1134 writeRunner.run(new TransactionWorker() { 1135 public void doWork() throws Exception { 1136 for (int i = beginKey; i <= endKey; i += 2) { 1138 Long key = makeKey(i); 1139 Object val = makeVal(i); 1140 assertNull(imap.get(key)); 1141 assertTrue(!imap.values().contains(val)); 1142 assertTrue(imap.duplicates(key).add(val)); 1143 assertEquals(val, imap.get(key)); 1144 assertTrue(imap.values().contains(val)); 1145 assertTrue(imap.duplicates(key).contains(val)); 1146 checkDupsSize(1, imap.duplicates(key)); 1147 assertTrue(!imap.duplicates(key).add(val)); 1148 if (isEntityBinding) { 1149 assertTrue(!imap.values().add(val)); 1150 } 1151 } 1152 } 1153 }); 1154 } 1155 1156 void addOddList() 1157 throws Exception { 1158 1159 writeRunner.run(new TransactionWorker() { 1160 public void doWork() throws Exception { 1161 for (int i = beginKey; i <= endKey; i += 2) { 1162 int idx = i - beginKey; 1163 Object val = makeVal(i); 1164 assertTrue(!list.contains(val)); 1165 assertTrue(!val.equals(list.get(idx))); 1166 list.add(idx, val); 1167 assertTrue(list.contains(val)); 1168 assertEquals(val, list.get(idx)); 1169 } 1170 } 1171 }); 1172 } 1173 1174 void addAllList() 1175 throws Exception { 1176 1177 writeRunner.run(new TransactionWorker() { 1178 public void doWork() throws Exception { 1179 for (int i = beginKey; i <= endKey; i += 1) { 1180 int idx = i - beginKey; 1181 Object val = makeVal(i); 1182 assertTrue(!list.contains(val)); 1183 assertTrue(list.add(val)); 1184 assertTrue(list.contains(val)); 1185 assertEquals(val, list.get(idx)); 1186 } 1187 } 1188 }); 1189 } 1190 1191 void removeAllList() 1192 throws Exception { 1193 1194 writeRunner.run(new TransactionWorker() { 1195 public void doWork() throws Exception { 1196 assertTrue(!list.isEmpty()); 1197 list.clear(); 1198 assertTrue(list.isEmpty()); 1199 for (int i = beginKey; i <= endKey; i += 1) { 1200 int idx = i - beginKey; 1201 assertNull(list.get(idx)); 1202 } 1203 } 1204 }); 1205 } 1206 1207 void testIterAddList() 1208 throws Exception { 1209 1210 writeIterRunner.run(new TransactionWorker() { 1211 public void doWork() throws Exception { 1212 ListIterator i = (ListIterator ) iterator(list); 1213 try { 1214 assertTrue(!i.hasNext()); 1215 i.add(makeVal(3)); 1216 assertTrue(!i.hasNext()); 1217 assertTrue(i.hasPrevious()); 1218 assertEquals(3, intVal(i.previous())); 1219 1220 i.add(makeVal(1)); 1221 assertTrue(i.hasPrevious()); 1222 assertTrue(i.hasNext()); 1223 assertEquals(1, intVal(i.previous())); 1224 assertTrue(i.hasNext()); 1225 assertEquals(1, intVal(i.next())); 1226 assertTrue(i.hasNext()); 1227 assertEquals(3, intVal(i.next())); 1228 assertEquals(3, intVal(i.previous())); 1229 1230 assertTrue(i.hasNext()); 1231 i.add(makeVal(2)); 1232 assertTrue(i.hasNext()); 1233 assertTrue(i.hasPrevious()); 1234 assertEquals(2, intVal(i.previous())); 1235 assertTrue(i.hasNext()); 1236 assertEquals(2, intVal(i.next())); 1237 assertTrue(i.hasNext()); 1238 assertEquals(3, intVal(i.next())); 1239 1240 assertTrue(!i.hasNext()); 1241 i.add(makeVal(4)); 1242 i.add(makeVal(5)); 1243 assertTrue(!i.hasNext()); 1244 assertEquals(5, intVal(i.previous())); 1245 assertEquals(4, intVal(i.previous())); 1246 assertEquals(3, intVal(i.previous())); 1247 assertEquals(2, intVal(i.previous())); 1248 assertEquals(1, intVal(i.previous())); 1249 assertTrue(!i.hasPrevious()); 1250 } finally { 1251 StoredIterator.close(i); 1252 } 1253 } 1254 }); 1255 } 1256 1257 void testIterAddDuplicates() 1258 throws Exception { 1259 1260 writeIterRunner.run(new TransactionWorker() { 1261 public void doWork() throws Exception { 1262 assertNull(imap.put(makeKey(1), makeVal(1))); 1263 ListIterator i = 1264 (ListIterator ) iterator(imap.duplicates(makeKey(1))); 1265 try { 1266 if (imap.areDuplicatesOrdered()) { 1267 i.add(makeVal(1, 4)); 1268 i.add(makeVal(1, 2)); 1269 i.add(makeVal(1, 3)); 1270 while (i.hasPrevious()) i.previous(); 1271 assertEquals(1, intVal(i.next())); 1272 assertEquals(2, intVal(i.next())); 1273 assertEquals(3, intVal(i.next())); 1274 assertEquals(4, intVal(i.next())); 1275 assertTrue(!i.hasNext()); 1276 } else { 1277 assertEquals(1, intVal(i.next())); 1278 i.add(makeVal(1, 2)); 1279 i.add(makeVal(1, 3)); 1280 assertTrue(!i.hasNext()); 1281 assertTrue(i.hasPrevious()); 1282 assertEquals(3, intVal(i.previous())); 1283 assertEquals(2, intVal(i.previous())); 1284 assertEquals(1, intVal(i.previous())); 1285 assertTrue(!i.hasPrevious()); 1286 i.add(makeVal(1, 4)); 1287 i.add(makeVal(1, 5)); 1288 assertTrue(i.hasNext()); 1289 assertEquals(5, intVal(i.previous())); 1290 assertEquals(4, intVal(i.previous())); 1291 assertTrue(!i.hasPrevious()); 1292 assertEquals(4, intVal(i.next())); 1293 assertEquals(5, intVal(i.next())); 1294 assertEquals(1, intVal(i.next())); 1295 assertEquals(2, intVal(i.next())); 1296 assertEquals(3, intVal(i.next())); 1297 assertTrue(!i.hasNext()); 1298 } 1299 } finally { 1300 StoredIterator.close(i); 1301 } 1302 } 1303 }); 1304 } 1305 1306 void readAll() 1307 throws Exception { 1308 1309 readRunner.run(new TransactionWorker() { 1310 public void doWork() throws Exception { 1311 1313 assertNotNull(map.toString()); 1314 for (int i = beginKey; i <= endKey; i += 1) { 1315 Long key = makeKey(i); 1316 Object val = map.get(key); 1317 assertEquals(makeVal(i), val); 1318 assertTrue(map.containsKey(key)); 1319 assertTrue(map.containsValue(val)); 1320 assertTrue(map.keySet().contains(key)); 1321 assertTrue(map.values().contains(val)); 1322 assertTrue(map.duplicates(key).contains(val)); 1323 checkDupsSize(1, map.duplicates(key)); 1324 } 1325 assertNull(map.get(makeKey(-1))); 1326 assertNull(map.get(makeKey(0))); 1327 assertNull(map.get(makeKey(beginKey - 1))); 1328 assertNull(map.get(makeKey(endKey + 1))); 1329 checkDupsSize(0, map.duplicates(makeKey(-1))); 1330 checkDupsSize(0, map.duplicates(makeKey(0))); 1331 checkDupsSize(0, map.duplicates(makeKey(beginKey - 1))); 1332 checkDupsSize(0, map.duplicates(makeKey(endKey + 1))); 1333 1334 1336 Set set = map.entrySet(); 1337 assertNotNull(set.toString()); 1338 assertEquals(beginKey > endKey, set.isEmpty()); 1339 Iterator iter = iterator(set); 1340 try { 1341 for (int i = beginKey; i <= endKey; i += 1) { 1342 assertTrue(iter.hasNext()); 1343 Map.Entry entry = (Map.Entry ) iter.next(); 1344 Long key = (Long ) entry.getKey(); 1345 Object val = entry.getValue(); 1346 if (map instanceof SortedMap ) { 1347 assertEquals(intKey(key), i); 1348 } 1349 assertEquals(intKey(key), intVal(val)); 1350 assertTrue(set.contains(entry)); 1351 } 1352 assertTrue(!iter.hasNext()); 1353 } finally { 1354 StoredIterator.close(iter); 1355 } 1356 Map.Entry [] entries = 1357 (Map.Entry []) set.toArray(new Map.Entry [0]); 1358 assertNotNull(entries); 1359 assertEquals(endKey - beginKey + 1, entries.length); 1360 for (int i = beginKey; i <= endKey; i += 1) { 1361 Map.Entry entry = entries[i - beginKey]; 1362 assertNotNull(entry); 1363 if (map instanceof SortedMap ) { 1364 assertEquals(makeKey(i), entry.getKey()); 1365 assertEquals(makeVal(i), entry.getValue()); 1366 } 1367 } 1368 readIterator(set, iterator(set), beginKey, endKey); 1369 if (smap != null) { 1370 SortedSet sset = (SortedSet ) set; 1371 if (beginKey == 1 && endKey >= 1) { 1372 readIterator(sset, 1373 iterator(sset.subSet(mapEntry(1), 1374 mapEntry(2))), 1375 1, 1); 1376 } 1377 if (beginKey <= 2 && endKey >= 2) { 1378 readIterator(sset, 1379 iterator(sset.subSet(mapEntry(2), 1380 mapEntry(3))), 1381 2, 2); 1382 } 1383 if (beginKey <= endKey) { 1384 readIterator(sset, 1385 iterator(sset.subSet 1386 (mapEntry(endKey), 1387 mapEntry(endKey + 1))), 1388 endKey, endKey); 1389 } 1390 if (isSubMap()) { 1391 if (beginKey <= endKey) { 1392 if (rangeType != TAIL) { 1393 try { 1394 sset.subSet(mapEntry(endKey + 1), 1395 mapEntry(endKey + 2)); 1396 fail(); 1397 } catch (IllegalArgumentException e) {} 1398 } 1399 if (rangeType != HEAD) { 1400 try { 1401 sset.subSet(mapEntry(0), 1402 mapEntry(1)); 1403 fail(); 1404 } catch (IllegalArgumentException e) {} 1405 } 1406 } 1407 } else { 1408 readIterator(sset, 1409 iterator(sset.subSet 1410 (mapEntry(endKey + 1), 1411 mapEntry(endKey + 2))), 1412 endKey, endKey - 1); 1413 readIterator(sset, 1414 iterator(sset.subSet(mapEntry(0), 1415 mapEntry(1))), 1416 0, -1); 1417 } 1418 } 1419 1420 1422 set = map.keySet(); 1423 assertNotNull(set.toString()); 1424 assertEquals(beginKey > endKey, set.isEmpty()); 1425 iter = iterator(set); 1426 try { 1427 for (int i = beginKey; i <= endKey; i += 1) { 1428 assertTrue(iter.hasNext()); 1429 Long key = (Long ) iter.next(); 1430 assertTrue(set.contains(key)); 1431 Object val = map.get(key); 1432 if (map instanceof SortedMap ) { 1433 assertEquals(key, makeKey(i)); 1434 } 1435 assertEquals(intKey(key), intVal(val)); 1436 } 1437 assertTrue("" + beginKey + ' ' + endKey, !iter.hasNext()); 1438 } finally { 1439 StoredIterator.close(iter); 1440 } 1441 Long [] keys = (Long []) set.toArray(new Long [0]); 1442 assertNotNull(keys); 1443 assertEquals(endKey - beginKey + 1, keys.length); 1444 for (int i = beginKey; i <= endKey; i += 1) { 1445 Long key = keys[i - beginKey]; 1446 assertNotNull(key); 1447 if (map instanceof SortedMap ) { 1448 assertEquals(makeKey(i), key); 1449 } 1450 } 1451 readIterator(set, iterator(set), beginKey, endKey); 1452 1453 1455 Collection coll = map.values(); 1456 assertNotNull(coll.toString()); 1457 assertEquals(beginKey > endKey, coll.isEmpty()); 1458 iter = iterator(coll); 1459 try { 1460 for (int i = beginKey; i <= endKey; i += 1) { 1461 assertTrue(iter.hasNext()); 1462 Object val = iter.next(); 1463 if (map instanceof SortedMap ) { 1464 assertEquals(makeVal(i), val); 1465 } 1466 } 1467 assertTrue(!iter.hasNext()); 1468 } finally { 1469 StoredIterator.close(iter); 1470 } 1471 Object [] values = coll.toArray(); 1472 assertNotNull(values); 1473 assertEquals(endKey - beginKey + 1, values.length); 1474 for (int i = beginKey; i <= endKey; i += 1) { 1475 Object val = values[i - beginKey]; 1476 assertNotNull(val); 1477 if (map instanceof SortedMap ) { 1478 assertEquals(makeVal(i), val); 1479 } 1480 } 1481 readIterator(coll, iterator(coll), beginKey, endKey); 1482 1483 1485 if (list != null) { 1486 assertNotNull(list.toString()); 1487 assertEquals(beginKey > endKey, list.isEmpty()); 1488 for (int i = beginKey; i <= endKey; i += 1) { 1489 int idx = i - beginKey; 1490 Object val = list.get(idx); 1491 assertEquals(makeVal(i), val); 1492 assertTrue(list.contains(val)); 1493 assertEquals(idx, list.indexOf(val)); 1494 assertEquals(idx, list.lastIndexOf(val)); 1495 } 1496 ListIterator li = (ListIterator ) iterator(list); 1497 try { 1498 for (int i = beginKey; i <= endKey; i += 1) { 1499 int idx = i - beginKey; 1500 assertTrue(li.hasNext()); 1501 assertEquals(idx, li.nextIndex()); 1502 Object val = li.next(); 1503 assertEquals(makeVal(i), val); 1504 assertEquals(idx, li.previousIndex()); 1505 } 1506 assertTrue(!li.hasNext()); 1507 } finally { 1508 StoredIterator.close(li); 1509 } 1510 if (beginKey < endKey) { 1511 li = list.listIterator(1); 1512 try { 1513 for (int i = beginKey + 1; i <= endKey; i += 1) { 1514 int idx = i - beginKey; 1515 assertTrue(li.hasNext()); 1516 assertEquals(idx, li.nextIndex()); 1517 Object val = li.next(); 1518 assertEquals(makeVal(i), val); 1519 assertEquals(idx, li.previousIndex()); 1520 } 1521 assertTrue(!li.hasNext()); 1522 } finally { 1523 StoredIterator.close(li); 1524 } 1525 } 1526 values = list.toArray(); 1527 assertNotNull(values); 1528 assertEquals(endKey - beginKey + 1, values.length); 1529 for (int i = beginKey; i <= endKey; i += 1) { 1530 Object val = values[i - beginKey]; 1531 assertNotNull(val); 1532 assertEquals(makeVal(i), val); 1533 } 1534 readIterator(list, iterator(list), beginKey, endKey); 1535 } 1536 1537 1539 if (smap != null) { 1540 if (beginKey <= endKey && 1541 beginKey >= 1 && beginKey <= maxKey) { 1542 assertEquals(makeKey(beginKey), 1543 smap.firstKey()); 1544 assertEquals(makeKey(beginKey), 1545 ((SortedSet ) smap.keySet()).first()); 1546 Object entry = ((SortedSet ) smap.entrySet()).first(); 1547 assertEquals(makeKey(beginKey), 1548 ((Map.Entry ) entry).getKey()); 1549 if (smap.values() instanceof SortedSet ) { 1550 assertEquals(makeVal(beginKey), 1551 ((SortedSet ) smap.values()).first()); 1552 } 1553 } else { 1554 assertNull(smap.firstKey()); 1555 assertNull(((SortedSet ) smap.keySet()).first()); 1556 assertNull(((SortedSet ) smap.entrySet()).first()); 1557 if (smap.values() instanceof SortedSet ) { 1558 assertNull(((SortedSet ) smap.values()).first()); 1559 } 1560 } 1561 if (beginKey <= endKey && 1562 endKey >= 1 && endKey <= maxKey) { 1563 assertEquals(makeKey(endKey), 1564 smap.lastKey()); 1565 assertEquals(makeKey(endKey), 1566 ((SortedSet ) smap.keySet()).last()); 1567 Object entry = ((SortedSet ) smap.entrySet()).last(); 1568 assertEquals(makeKey(endKey), 1569 ((Map.Entry ) entry).getKey()); 1570 if (smap.values() instanceof SortedSet ) { 1571 assertEquals(makeVal(endKey), 1572 ((SortedSet ) smap.values()).last()); 1573 } 1574 } else { 1575 assertNull(smap.lastKey()); 1576 assertNull(((SortedSet ) smap.keySet()).last()); 1577 assertNull(((SortedSet ) smap.entrySet()).last()); 1578 if (smap.values() instanceof SortedSet ) { 1579 assertNull(((SortedSet ) smap.values()).last()); 1580 } 1581 } 1582 } 1583 } 1584 }); 1585 } 1586 1587 void readEven() 1588 throws Exception { 1589 1590 readRunner.run(new TransactionWorker() { 1591 public void doWork() throws Exception { 1592 int readBegin = ((beginKey & 1) != 0) ? 1593 (beginKey + 1) : beginKey; 1594 int readEnd = ((endKey & 1) != 0) ? (endKey - 1) : endKey; 1595 int readIncr = 2; 1596 1597 1599 for (int i = beginKey; i <= endKey; i += 1) { 1600 Long key = makeKey(i); 1601 if ((i & 1) == 0) { 1602 Object val = map.get(key); 1603 assertEquals(makeVal(i), val); 1604 assertTrue(map.containsKey(key)); 1605 assertTrue(map.containsValue(val)); 1606 assertTrue(map.keySet().contains(key)); 1607 assertTrue(map.values().contains(val)); 1608 assertTrue(map.duplicates(key).contains(val)); 1609 checkDupsSize(1, map.duplicates(key)); 1610 } else { 1611 Object val = makeVal(i); 1612 assertTrue(!map.containsKey(key)); 1613 assertTrue(!map.containsValue(val)); 1614 assertTrue(!map.keySet().contains(key)); 1615 assertTrue(!map.values().contains(val)); 1616 assertTrue(!map.duplicates(key).contains(val)); 1617 checkDupsSize(0, map.duplicates(key)); 1618 } 1619 } 1620 1621 1623 Set set = map.entrySet(); 1624 assertEquals(beginKey > endKey, set.isEmpty()); 1625 Iterator iter = iterator(set); 1626 try { 1627 for (int i = readBegin; i <= readEnd; i += readIncr) { 1628 assertTrue(iter.hasNext()); 1629 Map.Entry entry = (Map.Entry ) iter.next(); 1630 Long key = (Long ) entry.getKey(); 1631 Object val = entry.getValue(); 1632 if (map instanceof SortedMap ) { 1633 assertEquals(intKey(key), i); 1634 } 1635 assertEquals(intKey(key), intVal(val)); 1636 assertTrue(set.contains(entry)); 1637 } 1638 assertTrue(!iter.hasNext()); 1639 } finally { 1640 StoredIterator.close(iter); 1641 } 1642 1643 1645 set = map.keySet(); 1646 assertEquals(beginKey > endKey, set.isEmpty()); 1647 iter = iterator(set); 1648 try { 1649 for (int i = readBegin; i <= readEnd; i += readIncr) { 1650 assertTrue(iter.hasNext()); 1651 Long key = (Long ) iter.next(); 1652 assertTrue(set.contains(key)); 1653 Object val = map.get(key); 1654 if (map instanceof SortedMap ) { 1655 assertEquals(key, makeKey(i)); 1656 } 1657 assertEquals(intKey(key), intVal(val)); 1658 } 1659 assertTrue(!iter.hasNext()); 1660 } finally { 1661 StoredIterator.close(iter); 1662 } 1663 1664 1666 Collection coll = map.values(); 1667 assertEquals(beginKey > endKey, coll.isEmpty()); 1668 iter = iterator(coll); 1669 try { 1670 for (int i = readBegin; i <= readEnd; i += readIncr) { 1671 assertTrue(iter.hasNext()); 1672 Object val = iter.next(); 1673 if (map instanceof SortedMap ) { 1674 assertEquals(makeVal(i), val); 1675 } 1676 } 1677 assertTrue(!iter.hasNext()); 1678 } finally { 1679 StoredIterator.close(iter); 1680 } 1681 1682 1683 1686 1688 if (smap != null) { 1689 if (readBegin <= readEnd && 1690 readBegin >= 1 && readBegin <= maxKey) { 1691 assertEquals(makeKey(readBegin), 1692 smap.firstKey()); 1693 assertEquals(makeKey(readBegin), 1694 ((SortedSet ) smap.keySet()).first()); 1695 Object entry = ((SortedSet ) smap.entrySet()).first(); 1696 assertEquals(makeKey(readBegin), 1697 ((Map.Entry ) entry).getKey()); 1698 if (smap.values() instanceof SortedSet ) { 1699 assertEquals(makeVal(readBegin), 1700 ((SortedSet ) smap.values()).first()); 1701 } 1702 } else { 1703 assertNull(smap.firstKey()); 1704 assertNull(((SortedSet ) smap.keySet()).first()); 1705 assertNull(((SortedSet ) smap.entrySet()).first()); 1706 if (smap.values() instanceof SortedSet ) { 1707 assertNull(((SortedSet ) smap.values()).first()); 1708 } 1709 } 1710 if (readBegin <= readEnd && 1711 readEnd >= 1 && readEnd <= maxKey) { 1712 assertEquals(makeKey(readEnd), 1713 smap.lastKey()); 1714 assertEquals(makeKey(readEnd), 1715 ((SortedSet ) smap.keySet()).last()); 1716 Object entry = ((SortedSet ) smap.entrySet()).last(); 1717 assertEquals(makeKey(readEnd), 1718 ((Map.Entry ) entry).getKey()); 1719 if (smap.values() instanceof SortedSet ) { 1720 assertEquals(makeVal(readEnd), 1721 ((SortedSet ) smap.values()).last()); 1722 } 1723 } else { 1724 assertNull(smap.lastKey()); 1725 assertNull(((SortedSet ) smap.keySet()).last()); 1726 assertNull(((SortedSet ) smap.entrySet()).last()); 1727 if (smap.values() instanceof SortedSet ) { 1728 assertNull(((SortedSet ) smap.values()).last()); 1729 } 1730 } 1731 } 1732 } 1733 }); 1734 } 1735 1736 void readEvenList() 1737 throws Exception { 1738 1739 readRunner.run(new TransactionWorker() { 1740 public void doWork() throws Exception { 1741 int readBegin = ((beginKey & 1) != 0) ? 1742 (beginKey + 1) : beginKey; 1743 int readEnd = ((endKey & 1) != 0) ? (endKey - 1) : endKey; 1744 int readIncr = 2; 1745 1746 assertEquals(beginKey > endKey, list.isEmpty()); 1747 ListIterator iter = (ListIterator ) iterator(list); 1748 try { 1749 int idx = 0; 1750 for (int i = readBegin; i <= readEnd; i += readIncr) { 1751 assertTrue(iter.hasNext()); 1752 assertEquals(idx, iter.nextIndex()); 1753 Object val = iter.next(); 1754 assertEquals(idx, iter.previousIndex()); 1755 if (isEntityBinding) { 1756 assertEquals(i, intVal(val)); 1757 } else { 1758 assertEquals(makeVal(i), val); 1759 } 1760 idx += 1; 1761 } 1762 assertTrue(!iter.hasNext()); 1763 } finally { 1764 StoredIterator.close(iter); 1765 } 1766 } 1767 }); 1768 } 1769 1770 void readIterator(Collection coll, Iterator iter, 1771 int beginValue, int endValue) { 1772 1773 ListIterator li = (ListIterator ) iter; 1774 boolean isList = (coll instanceof List ); 1775 Iterator clone = null; 1776 try { 1777 assertTrue(!li.hasPrevious()); 1779 assertTrue(!li.hasPrevious()); 1780 try { li.previous(); } catch (NoSuchElementException e) {} 1781 if (isList) { 1782 assertEquals(-1, li.previousIndex()); 1783 } 1784 if (endValue < beginValue) { 1785 assertTrue(!iter.hasNext()); 1787 try { iter.next(); } catch (NoSuchElementException e) {} 1788 if (isList) { 1789 assertEquals(Integer.MAX_VALUE, li.nextIndex()); 1790 } 1791 } 1792 int[] values = new int[endValue - beginValue + 1]; 1794 for (int i = beginValue; i <= endValue; i += 1) { 1795 assertTrue(iter.hasNext()); 1796 int idx = i - beginKey; 1797 if (isList) { 1798 assertEquals(idx, li.nextIndex()); 1799 } 1800 int value = intIter(coll, iter.next()); 1801 if (isList) { 1802 assertEquals(idx, li.previousIndex()); 1803 } 1804 values[i - beginValue] = value; 1805 if (((StoredCollection) coll).isOrdered()) { 1806 assertEquals(i, value); 1807 } else { 1808 assertTrue(value >= beginValue); 1809 assertTrue(value <= endValue); 1810 } 1811 } 1812 assertTrue(!iter.hasNext()); 1814 try { iter.next(); } catch (NoSuchElementException e) {} 1815 if (isList) { 1816 assertEquals(Integer.MAX_VALUE, li.nextIndex()); 1817 } 1818 clone = StoredCollections.iterator(iter); 1820 assertTrue(!clone.hasNext()); 1821 for (int i = endValue; i >= beginValue; i -= 1) { 1823 assertTrue(li.hasPrevious()); 1824 int idx = i - beginKey; 1825 if (isList) { 1826 assertEquals(idx, li.previousIndex()); 1827 } 1828 int value = intIter(coll, li.previous()); 1829 if (isList) { 1830 assertEquals(idx, li.nextIndex()); 1831 } 1832 assertEquals(values[i - beginValue], value); 1833 } 1834 assertTrue(!clone.hasNext()); 1836 assertTrue(!li.hasPrevious()); 1838 try { li.previous(); } catch (NoSuchElementException e) {} 1839 if (isList) { 1840 assertEquals(-1, li.previousIndex()); 1841 } 1842 for (int i = beginValue; i <= endValue; i += 1) { 1844 assertTrue(iter.hasNext()); 1845 int idx = i - beginKey; 1846 if (isList) { 1847 assertEquals(idx, li.nextIndex()); 1848 } 1849 Object obj = iter.next(); 1850 if (isList) { 1851 assertEquals(idx, li.previousIndex()); 1852 } 1853 assertEquals(obj, li.previous()); 1854 if (isList) { 1855 assertEquals(idx, li.nextIndex()); 1856 } 1857 assertEquals(obj, iter.next()); 1858 if (isList) { 1859 assertEquals(idx, li.previousIndex()); 1860 } 1861 int value = intIter(coll, obj); 1862 assertEquals(values[i - beginValue], value); 1863 } 1864 assertTrue(!iter.hasNext()); 1866 try { iter.next(); } catch (NoSuchElementException e) {} 1867 if (isList) { 1868 assertEquals(Integer.MAX_VALUE, li.nextIndex()); 1869 } 1870 } finally { 1871 StoredIterator.close(iter); 1872 StoredIterator.close(clone); 1873 } 1874 } 1875 1876 void bulkOperations() 1877 throws Exception { 1878 1879 writeRunner.run(new TransactionWorker() { 1880 public void doWork() throws Exception { 1881 HashMap hmap = new HashMap (); 1882 for (int i = Math.max(1, beginKey); 1883 i <= Math.min(maxKey, endKey); 1884 i += 1) { 1885 hmap.put(makeKey(i), makeVal(i)); 1886 } 1887 assertTrue(map.equals(hmap)); 1888 assertTrue(map.entrySet().equals(hmap.entrySet())); 1889 assertTrue(map.keySet().equals(hmap.keySet())); 1890 assertTrue(map.values().equals(hmap.values())); 1891 1892 assertTrue(map.entrySet().containsAll(hmap.entrySet())); 1893 assertTrue(map.keySet().containsAll(hmap.keySet())); 1894 assertTrue(map.values().containsAll(hmap.values())); 1895 1896 map.clear(); 1897 assertTrue(map.isEmpty()); 1898 imap.putAll(hmap); 1899 assertTrue(map.equals(hmap)); 1900 1901 assertTrue(map.entrySet().removeAll(hmap.entrySet())); 1902 assertTrue(map.entrySet().isEmpty()); 1903 assertTrue(!map.entrySet().removeAll(hmap.entrySet())); 1904 assertTrue(imap.entrySet().addAll(hmap.entrySet())); 1905 assertTrue(map.entrySet().containsAll(hmap.entrySet())); 1906 assertTrue(!imap.entrySet().addAll(hmap.entrySet())); 1907 assertTrue(map.equals(hmap)); 1908 1909 assertTrue(!map.entrySet().retainAll(hmap.entrySet())); 1910 assertTrue(map.equals(hmap)); 1911 assertTrue(map.entrySet().retainAll(Collections.EMPTY_SET)); 1912 assertTrue(map.isEmpty()); 1913 imap.putAll(hmap); 1914 assertTrue(map.equals(hmap)); 1915 1916 assertTrue(map.values().removeAll(hmap.values())); 1917 assertTrue(map.values().isEmpty()); 1918 assertTrue(!map.values().removeAll(hmap.values())); 1919 if (isEntityBinding) { 1920 assertTrue(imap.values().addAll(hmap.values())); 1921 assertTrue(map.values().containsAll(hmap.values())); 1922 assertTrue(!imap.values().addAll(hmap.values())); 1923 } else { 1924 imap.putAll(hmap); 1925 } 1926 assertTrue(map.equals(hmap)); 1927 1928 assertTrue(!map.values().retainAll(hmap.values())); 1929 assertTrue(map.equals(hmap)); 1930 assertTrue(map.values().retainAll(Collections.EMPTY_SET)); 1931 assertTrue(map.isEmpty()); 1932 imap.putAll(hmap); 1933 assertTrue(map.equals(hmap)); 1934 1935 assertTrue(map.keySet().removeAll(hmap.keySet())); 1936 assertTrue(map.keySet().isEmpty()); 1937 assertTrue(!map.keySet().removeAll(hmap.keySet())); 1938 assertTrue(imap.keySet().addAll(hmap.keySet())); 1939 assertTrue(imap.keySet().containsAll(hmap.keySet())); 1940 if (index != null) { 1941 assertTrue(map.keySet().isEmpty()); 1942 } 1943 assertTrue(!imap.keySet().addAll(hmap.keySet())); 1944 imap.keySet().removeAll(hmap.keySet()); 1946 imap.putAll(hmap); 1947 assertTrue(map.equals(hmap)); 1948 1949 assertTrue(!map.keySet().retainAll(hmap.keySet())); 1950 assertTrue(map.equals(hmap)); 1951 assertTrue(map.keySet().retainAll(Collections.EMPTY_SET)); 1952 assertTrue(map.isEmpty()); 1953 imap.putAll(hmap); 1954 assertTrue(map.equals(hmap)); 1955 } 1956 }); 1957 } 1958 1959 void bulkListOperations() 1960 throws Exception { 1961 1962 writeRunner.run(new TransactionWorker() { 1963 public void doWork() throws Exception { 1964 ArrayList alist = new ArrayList (); 1965 for (int i = beginKey; i <= endKey; i += 1) { 1966 alist.add(makeVal(i)); 1967 } 1968 1969 assertTrue(list.equals(alist)); 1970 assertTrue(list.containsAll(alist)); 1971 1972 if (isListAddAllowed()) { 1973 list.clear(); 1974 assertTrue(list.isEmpty()); 1975 assertTrue(ilist.addAll(alist)); 1976 assertTrue(list.equals(alist)); 1977 } 1978 1979 assertTrue(!list.retainAll(alist)); 1980 assertTrue(list.equals(alist)); 1981 1982 if (isListAddAllowed()) { 1983 assertTrue(list.retainAll(Collections.EMPTY_SET)); 1984 assertTrue(list.isEmpty()); 1985 assertTrue(ilist.addAll(alist)); 1986 assertTrue(list.equals(alist)); 1987 } 1988 1989 if (isListAddAllowed() && !isEntityBinding) { 1990 assertTrue(list.removeAll(alist)); 1994 assertTrue(list.isEmpty()); 1995 assertTrue(!list.removeAll(alist)); 1996 assertTrue(ilist.addAll(alist)); 1997 assertTrue(list.containsAll(alist)); 1998 assertTrue(list.equals(alist)); 1999 } 2000 2001 if (isListAddAllowed() && !isEntityBinding) { 2002 2005 ilist.addAll(beginKey, alist); 2007 assertTrue(list.containsAll(alist)); 2008 assertEquals(2 * alist.size(), countElements(list)); 2009 for (int i = beginKey; i <= endKey; i += 1) 2010 ilist.remove(beginKey); 2011 assertTrue(list.equals(alist)); 2012 2013 ilist.addAll(endKey, alist); 2015 assertTrue(list.containsAll(alist)); 2016 assertEquals(2 * alist.size(), countElements(list)); 2017 for (int i = beginKey; i <= endKey; i += 1) 2018 ilist.remove(endKey); 2019 assertTrue(list.equals(alist)); 2020 2021 ilist.addAll(endKey - 1, alist); 2023 assertTrue(list.containsAll(alist)); 2024 assertEquals(2 * alist.size(), countElements(list)); 2025 for (int i = beginKey; i <= endKey; i += 1) 2026 ilist.remove(endKey - 1); 2027 assertTrue(list.equals(alist)); 2028 } 2029 } 2030 }); 2031 } 2032 2033 void readWriteRange(final int type, final int rangeBegin, 2034 final int rangeEnd) 2035 throws Exception { 2036 2037 writeRunner.run(new TransactionWorker() { 2038 public void doWork() throws Exception { 2039 setRange(type, rangeBegin, rangeEnd); 2040 createOutOfRange(rangeBegin, rangeEnd); 2041 if (rangeType != TAIL) { 2042 writeOutOfRange(new Long (rangeEnd + 1)); 2043 } 2044 if (rangeType != HEAD) { 2045 writeOutOfRange(new Long (rangeBegin - 1)); 2046 } 2047 if (rangeBegin <= rangeEnd) { 2048 updateAll(); 2049 } 2050 if (rangeBegin < rangeEnd && !map.areKeysRenumbered()) { 2051 bulkOperations(); 2052 } 2053 readAll(); 2054 clearRange(); 2055 } 2056 }); 2057 } 2058 2059 void setRange(int type, int rangeBegin, int rangeEnd) { 2060 2061 rangeType = type; 2062 saveMap = map; 2063 saveSMap = smap; 2064 saveList = list; 2065 int listBegin = rangeBegin - beginKey; 2066 boolean canMakeSubList = (list != null && listBegin>= 0); 2067 if (!canMakeSubList) { 2068 list = null; 2069 } 2070 if (list != null) { 2071 try { 2072 list.subList(-1, 0); 2073 fail(); 2074 } catch (IndexOutOfBoundsException e) { } 2075 } 2076 switch (type) { 2077 2078 case SUB: 2079 smap = (StoredSortedMap) smap.subMap(makeKey(rangeBegin), 2080 makeKey(rangeEnd + 1)); 2081 if (canMakeSubList) { 2082 list = (StoredList) list.subList(listBegin, 2083 rangeEnd + 1 - beginKey); 2084 } 2085 assertEquals(smap, 2087 ((StoredSortedMap) saveSMap).subMap( 2088 makeKey(rangeBegin), true, 2089 makeKey(rangeEnd + 1), false)); 2090 assertEquals(smap.entrySet(), 2091 ((StoredSortedEntrySet) saveSMap.entrySet()).subSet( 2092 mapEntry(rangeBegin), true, 2093 mapEntry(rangeEnd + 1), false)); 2094 assertEquals(smap.keySet(), 2095 ((StoredSortedKeySet) saveSMap.keySet()).subSet( 2096 makeKey(rangeBegin), true, 2097 makeKey(rangeEnd + 1), false)); 2098 if (smap.values() instanceof SortedSet ) { 2099 assertEquals(smap.values(), 2100 ((StoredSortedValueSet) saveSMap.values()).subSet( 2101 makeVal(rangeBegin), true, 2102 makeVal(rangeEnd + 1), false)); 2103 } 2104 break; 2105 case HEAD: 2106 smap = (StoredSortedMap) smap.headMap(makeKey(rangeEnd + 1)); 2107 if (canMakeSubList) { 2108 list = (StoredList) list.subList(0, 2109 rangeEnd + 1 - beginKey); 2110 } 2111 assertEquals(smap, 2113 ((StoredSortedMap) saveSMap).headMap( 2114 makeKey(rangeEnd + 1), false)); 2115 assertEquals(smap.entrySet(), 2116 ((StoredSortedEntrySet) saveSMap.entrySet()).headSet( 2117 mapEntry(rangeEnd + 1), false)); 2118 assertEquals(smap.keySet(), 2119 ((StoredSortedKeySet) saveSMap.keySet()).headSet( 2120 makeKey(rangeEnd + 1), false)); 2121 if (smap.values() instanceof SortedSet ) { 2122 assertEquals(smap.values(), 2123 ((StoredSortedValueSet) saveSMap.values()).headSet( 2124 makeVal(rangeEnd + 1), false)); 2125 } 2126 break; 2127 case TAIL: 2128 smap = (StoredSortedMap) smap.tailMap(makeKey(rangeBegin)); 2129 if (canMakeSubList) { 2130 list = (StoredList) list.subList(listBegin, 2131 maxKey + 1 - beginKey); 2132 } 2133 assertEquals(smap, 2135 ((StoredSortedMap) saveSMap).tailMap( 2136 makeKey(rangeBegin), true)); 2137 assertEquals(smap.entrySet(), 2138 ((StoredSortedEntrySet) saveSMap.entrySet()).tailSet( 2139 mapEntry(rangeBegin), true)); 2140 assertEquals(smap.keySet(), 2141 ((StoredSortedKeySet) saveSMap.keySet()).tailSet( 2142 makeKey(rangeBegin), true)); 2143 if (smap.values() instanceof SortedSet ) { 2144 assertEquals(smap.values(), 2145 ((StoredSortedValueSet) saveSMap.values()).tailSet( 2146 makeVal(rangeBegin), true)); 2147 } 2148 break; 2149 default: throw new RuntimeException (); 2150 } 2151 map = smap; 2152 beginKey = rangeBegin; 2153 if (rangeBegin < 1 || rangeEnd > maxKey) { 2154 endKey = rangeBegin - 1; } else { 2156 endKey = rangeEnd; 2157 } 2158 } 2159 2160 void clearRange() { 2161 2162 rangeType = NONE; 2163 beginKey = 1; 2164 endKey = maxKey; 2165 map = saveMap; 2166 smap = saveSMap; 2167 list = saveList; 2168 } 2169 2170 void createOutOfRange(int rangeBegin, int rangeEnd) 2171 throws Exception { 2172 2173 2175 if (rangeType != TAIL) { 2176 try { 2177 smap.subMap(makeKey(rangeBegin), makeKey(rangeEnd + 2)); 2178 fail(); 2179 } catch (IllegalArgumentException e) { } 2180 try { 2181 smap.headMap(makeKey(rangeEnd + 2)); 2182 fail(); 2183 } catch (IllegalArgumentException e) { } 2184 checkDupsSize(0, smap.duplicates(makeKey(rangeEnd + 2))); 2185 } 2186 if (rangeType != HEAD) { 2187 try { 2188 smap.subMap(makeKey(rangeBegin - 1), makeKey(rangeEnd + 1)); 2189 fail(); 2190 } catch (IllegalArgumentException e) { } 2191 try { 2192 smap.tailMap(makeKey(rangeBegin - 1)); 2193 fail(); 2194 } 2195 catch (IllegalArgumentException e) { } 2196 checkDupsSize(0, smap.duplicates(makeKey(rangeBegin - 1))); 2197 } 2198 2199 2201 if (rangeType != TAIL) { 2202 SortedSet sset = (SortedSet ) map.keySet(); 2203 try { 2204 sset.subSet(makeKey(rangeBegin), makeKey(rangeEnd + 2)); 2205 fail(); 2206 } catch (IllegalArgumentException e) { } 2207 try { 2208 sset.headSet(makeKey(rangeEnd + 2)); 2209 fail(); 2210 } catch (IllegalArgumentException e) { } 2211 try { 2212 iterator(sset.subSet(makeKey(rangeEnd + 1), 2213 makeKey(rangeEnd + 2))); 2214 fail(); 2215 } catch (IllegalArgumentException e) { } 2216 } 2217 if (rangeType != HEAD) { 2218 SortedSet sset = (SortedSet ) map.keySet(); 2219 try { 2220 sset.subSet(makeKey(rangeBegin - 1), makeKey(rangeEnd + 1)); 2221 fail(); 2222 } catch (IllegalArgumentException e) { } 2223 try { 2224 sset.tailSet(makeKey(rangeBegin - 1)); 2225 fail(); 2226 } 2227 catch (IllegalArgumentException e) { } 2228 try { 2229 iterator(sset.subSet(makeKey(rangeBegin - 1), 2230 makeKey(rangeBegin))); 2231 fail(); 2232 } 2233 catch (IllegalArgumentException e) { } 2234 } 2235 2236 2238 if (rangeType != TAIL) { 2239 SortedSet sset = (SortedSet ) map.entrySet(); 2240 try { 2241 sset.subSet(mapEntry(rangeBegin), mapEntry(rangeEnd + 2)); 2242 fail(); 2243 } catch (IllegalArgumentException e) { } 2244 try { 2245 sset.headSet(mapEntry(rangeEnd + 2)); 2246 fail(); 2247 } catch (IllegalArgumentException e) { } 2248 try { 2249 iterator(sset.subSet(mapEntry(rangeEnd + 1), 2250 mapEntry(rangeEnd + 2))); 2251 fail(); 2252 } catch (IllegalArgumentException e) { } 2253 } 2254 if (rangeType != HEAD) { 2255 SortedSet sset = (SortedSet ) map.entrySet(); 2256 try { 2257 sset.subSet(mapEntry(rangeBegin - 1), mapEntry(rangeEnd + 1)); 2258 fail(); 2259 } catch (IllegalArgumentException e) { } 2260 try { 2261 sset.tailSet(mapEntry(rangeBegin - 1)); 2262 fail(); 2263 } catch (IllegalArgumentException e) { } 2264 try { 2265 iterator(sset.subSet(mapEntry(rangeBegin - 1), 2266 mapEntry(rangeBegin))); 2267 fail(); 2268 } 2269 catch (IllegalArgumentException e) { } 2270 } 2271 2272 2274 if (map.values() instanceof SortedSet ) { 2275 SortedSet sset = (SortedSet ) map.values(); 2276 if (rangeType != TAIL) { 2277 try { 2278 sset.subSet(makeVal(rangeBegin), 2279 makeVal(rangeEnd + 2)); 2280 fail(); 2281 } catch (IllegalArgumentException e) { } 2282 try { 2283 sset.headSet(makeVal(rangeEnd + 2)); 2284 fail(); 2285 } catch (IllegalArgumentException e) { } 2286 } 2287 if (rangeType != HEAD) { 2288 try { 2289 sset.subSet(makeVal(rangeBegin - 1), 2290 makeVal(rangeEnd + 1)); 2291 fail(); 2292 } catch (IllegalArgumentException e) { } 2293 try { 2294 sset.tailSet(makeVal(rangeBegin - 1)); 2295 fail(); 2296 } 2297 catch (IllegalArgumentException e) { } 2298 } 2299 } 2300 2301 2303 if (list != null) { 2304 int size = rangeEnd - rangeBegin + 1; 2305 try { 2306 list.subList(0, size + 1); 2307 fail(); 2308 } catch (IndexOutOfBoundsException e) { } 2309 try { 2310 list.subList(-1, size); 2311 fail(); 2312 } catch (IndexOutOfBoundsException e) { } 2313 try { 2314 list.subList(2, 1); 2315 fail(); 2316 } catch (IndexOutOfBoundsException e) { } 2317 try { 2318 list.subList(size, size); 2319 fail(); 2320 } 2321 catch (IndexOutOfBoundsException e) { } 2322 } 2323 } 2324 2325 void writeOutOfRange(Long badNewKey) 2326 throws Exception { 2327 2328 try { 2329 map.put(badNewKey, makeVal(badNewKey)); 2330 fail(); 2331 } catch (IllegalArgumentException e) { 2332 assertTrue(e.toString(), index == null); 2333 } catch (UnsupportedOperationException e) { 2334 assertTrue(index != null); 2335 } 2336 try { 2337 map.keySet().add(badNewKey); 2338 fail(); 2339 } catch (IllegalArgumentException e) { 2340 assertTrue(index == null); 2341 } catch (UnsupportedOperationException e) { 2342 assertTrue(index != null); 2343 } 2344 try { 2345 map.values().add(makeEntity(badNewKey)); 2346 fail(); 2347 } catch (IllegalArgumentException e) { 2348 assertTrue(isEntityBinding && index == null); 2349 } catch (UnsupportedOperationException e) { 2350 assertTrue(!(isEntityBinding && index == null)); 2351 } 2352 if (list != null) { 2353 int i = badNewKey.intValue() - beginKey; 2354 try { 2355 list.set(i, makeVal(i)); 2356 fail(); 2357 } catch (IndexOutOfBoundsException e) { 2358 assertTrue(index == null); 2359 } catch (UnsupportedOperationException e) { 2360 assertTrue(index != null); 2361 } 2362 try { 2363 list.add(i, makeVal(badNewKey)); 2364 fail(); 2365 } 2366 catch (UnsupportedOperationException e) { 2367 } 2368 } 2369 } 2370 2371 void readWriteDuplicates() 2372 throws Exception { 2373 2374 writeRunner.run(new TransactionWorker() { 2375 public void doWork() throws Exception { 2376 if (index == null) { 2377 readWritePrimaryDuplicates(beginKey); 2378 readWritePrimaryDuplicates(beginKey + 1); 2379 readWritePrimaryDuplicates(endKey); 2380 readWritePrimaryDuplicates(endKey - 1); 2381 } else { 2382 readWriteIndexedDuplicates(beginKey); 2383 readWriteIndexedDuplicates(beginKey + 1); 2384 readWriteIndexedDuplicates(endKey); 2385 readWriteIndexedDuplicates(endKey - 1); 2386 } 2387 } 2388 }); 2389 } 2390 2391 void readWritePrimaryDuplicates(int i) 2392 throws Exception { 2393 2394 Collection dups; 2395 final Long key = makeKey(i); 2397 final Object [] values = new Object [5]; 2398 for (int j = 0; j < values.length; j += 1) { 2399 values[j] = isEntityBinding 2400 ? makeEntity(i, i + j) 2401 : makeVal(i + j); 2402 } 2403 outerLoop: for (int writeMode = 0;; writeMode += 1) { 2405 switch (writeMode) { 2407 case 0: 2408 case 1: { 2409 for (int j = 1; j < values.length; j += 1) { 2411 map.put(key, values[j]); 2412 } 2413 break; 2414 } 2415 case 2: { 2416 dups = map.duplicates(key); 2418 for (int j = 1; j < values.length; j += 1) { 2419 dups.add(values[j]); 2420 } 2421 break; 2422 } 2423 case 3: { 2424 writeIterRunner.run(new TransactionWorker() { 2426 public void doWork() throws Exception { 2427 Collection dups = map.duplicates(key); 2428 Iterator iter = iterator(dups); 2429 assertEquals(values[0], iter.next()); 2430 assertTrue(!iter.hasNext()); 2431 try { 2432 for (int j = 1; j < values.length; j += 1) { 2433 ((ListIterator ) iter).add(values[j]); 2434 } 2435 } finally { 2436 StoredIterator.close(iter); 2437 } 2438 } 2439 }); 2440 break; 2441 } 2442 case 4: { 2443 if (!isEntityBinding) { 2445 continue; 2446 } 2447 Collection set = map.values(); 2448 for (int j = 1; j < values.length; j += 1) { 2449 set.add(values[j]); 2450 } 2451 break; 2452 } 2453 default: { 2454 break outerLoop; 2455 } 2456 } 2457 checkDupsSize(values.length, map.duplicates(key)); 2458 readDuplicates(i, key, values); 2460 switch (writeMode) { 2462 case 0: { 2463 checkDupsSize(values.length, map.duplicates(key)); 2465 map.remove(key); checkDupsSize(0, map.duplicates(key)); 2467 map.put(key, values[0]); checkDupsSize(1, map.duplicates(key)); 2469 break; 2470 } 2471 case 1: { 2472 map.keySet().remove(key); map.put(key, values[0]); break; 2476 } 2477 case 2: { 2478 dups = map.duplicates(key); 2480 dups.clear(); dups.add(values[0]); break; 2483 } 2484 case 3: { 2485 writeIterRunner.run(new TransactionWorker() { 2487 public void doWork() throws Exception { 2488 Collection dups = map.duplicates(key); 2489 Iterator iter = iterator(dups); 2490 try { 2491 for (int j = 0; j < values.length; j += 1) { 2492 assertEquals(values[j], iter.next()); 2493 if (j != 0) { 2494 iter.remove(); 2495 } 2496 } 2497 } finally { 2498 StoredIterator.close(iter); 2499 } 2500 } 2501 }); 2502 break; 2503 } 2504 case 4: { 2505 if (!isEntityBinding) { 2507 throw new IllegalStateException (); 2508 } 2509 Collection set = map.values(); 2510 for (int j = 1; j < values.length; j += 1) { 2511 set.remove(values[j]); 2512 } 2513 break; 2514 } 2515 default: throw new IllegalStateException (); 2516 } 2517 dups = map.duplicates(key); 2519 assertTrue(dups.contains(values[0])); 2520 for (int j = 1; j < values.length; j += 1) { 2521 assertTrue(!dups.contains(values[j])); 2522 } 2523 checkDupsSize(1, dups); 2524 } 2525 } 2526 2527 void readWriteIndexedDuplicates(int i) 2528 throws Exception { 2529 2530 Object key = makeKey(i); 2531 Object [] values = new Object [3]; 2532 values[0] = makeVal(i); 2533 for (int j = 1; j < values.length; j += 1) { 2534 values[j] = isEntityBinding 2535 ? makeEntity(endKey + j, i) 2536 : makeVal(i); 2537 } 2538 for (int j = 1; j < values.length; j += 1) { 2540 imap.put(makeKey(endKey + j), values[j]); 2541 } 2542 readDuplicates(i, key, values); 2544 for (int j = 1; j < values.length; j += 1) { 2546 imap.remove(makeKey(endKey + j)); 2547 } 2548 checkDupsSize(1, map.duplicates(key)); 2549 } 2550 2551 void readDuplicates(int i, Object key, Object [] values) { 2552 2553 boolean isOrdered = map.isOrdered(); 2554 Collection dups; 2555 Iterator iter; 2556 dups = map.duplicates(key); 2558 checkDupsSize(values.length, dups); 2559 iter = iterator(dups); 2560 try { 2561 for (int j = 0; j < values.length; j += 1) { 2562 assertTrue(iter.hasNext()); 2563 Object val = iter.next(); 2564 assertEquals(values[j], val); 2565 } 2566 assertTrue(!iter.hasNext()); 2567 } finally { 2568 StoredIterator.close(iter); 2569 } 2570 Collection clone = ((StoredCollection) map.values()).toList(); 2572 iter = iterator(map.values()); 2573 try { 2574 for (int j = beginKey; j < i; j += 1) { 2575 Object val = iter.next(); 2576 assertTrue(clone.remove(makeVal(j))); 2577 if (isOrdered) { 2578 assertEquals(makeVal(j), val); 2579 } 2580 } 2581 for (int j = 0; j < values.length; j += 1) { 2582 Object val = iter.next(); 2583 assertTrue(clone.remove(values[j])); 2584 if (isOrdered) { 2585 assertEquals(values[j], val); 2586 } 2587 } 2588 for (int j = i + 1; j <= endKey; j += 1) { 2589 Object val = iter.next(); 2590 assertTrue(clone.remove(makeVal(j))); 2591 if (isOrdered) { 2592 assertEquals(makeVal(j), val); 2593 } 2594 } 2595 assertTrue(!iter.hasNext()); 2596 assertTrue(clone.isEmpty()); 2597 } finally { 2598 StoredIterator.close(iter); 2599 } 2600 clone = ((StoredCollection) map.entrySet()).toList(); 2602 iter = iterator(map.entrySet()); 2603 try { 2604 for (int j = beginKey; j < i; j += 1) { 2605 Map.Entry entry = (Map.Entry ) iter.next(); 2606 assertTrue(clone.remove(mapEntry(j))); 2607 if (isOrdered) { 2608 assertEquals(makeVal(j), entry.getValue()); 2609 assertEquals(makeKey(j), entry.getKey()); 2610 } 2611 } 2612 for (int j = 0; j < values.length; j += 1) { 2613 Map.Entry entry = (Map.Entry ) iter.next(); 2614 assertTrue(clone.remove(mapEntry(makeKey(i), values[j]))); 2615 if (isOrdered) { 2616 assertEquals(values[j], entry.getValue()); 2617 assertEquals(makeKey(i), entry.getKey()); 2618 } 2619 } 2620 for (int j = i + 1; j <= endKey; j += 1) { 2621 Map.Entry entry = (Map.Entry ) iter.next(); 2622 assertTrue(clone.remove(mapEntry(j))); 2623 if (isOrdered) { 2624 assertEquals(makeVal(j), entry.getValue()); 2625 assertEquals(makeKey(j), entry.getKey()); 2626 } 2627 } 2628 assertTrue(!iter.hasNext()); 2629 assertTrue(clone.isEmpty()); 2630 } finally { 2631 StoredIterator.close(iter); 2632 } 2633 clone = ((StoredCollection) map.keySet()).toList(); 2635 iter = iterator(map.keySet()); 2636 try { 2637 for (int j = beginKey; j < i; j += 1) { 2638 Object val = iter.next(); 2639 assertTrue(clone.remove(makeKey(j))); 2640 if (isOrdered) { 2641 assertEquals(makeKey(j), val); 2642 } 2643 } 2644 if (true) { 2645 Object val = iter.next(); 2647 assertTrue(clone.remove(makeKey(i))); 2648 if (isOrdered) { 2649 assertEquals(makeKey(i), val); 2650 } 2651 } 2652 for (int j = i + 1; j <= endKey; j += 1) { 2653 Object val = iter.next(); 2654 assertTrue(clone.remove(makeKey(j))); 2655 if (isOrdered) { 2656 assertEquals(makeKey(j), val); 2657 } 2658 } 2659 assertTrue(!iter.hasNext()); 2660 assertTrue(clone.isEmpty()); 2661 } finally { 2662 StoredIterator.close(iter); 2663 } 2664 } 2665 2666 void duplicatesNotAllowed() { 2667 2668 Collection dups = map.duplicates(makeKey(beginKey)); 2669 try { 2670 dups.add(makeVal(beginKey)); 2671 fail(); 2672 } catch (UnsupportedOperationException expected) { } 2673 ListIterator iter = (ListIterator ) iterator(dups); 2674 try { 2675 iter.add(makeVal(beginKey)); 2676 fail(); 2677 } catch (UnsupportedOperationException expected) { 2678 } finally { 2679 StoredIterator.close(iter); 2680 } 2681 } 2682 2683 void listOperationsNotAllowed() { 2684 2685 ListIterator iter = (ListIterator ) iterator(map.values()); 2686 try { 2687 try { 2688 iter.nextIndex(); 2689 fail(); 2690 } catch (UnsupportedOperationException expected) { } 2691 try { 2692 iter.previousIndex(); 2693 fail(); 2694 } catch (UnsupportedOperationException expected) { } 2695 } finally { 2696 StoredIterator.close(iter); 2697 } 2698 } 2699 2700 void testCdbLocking() { 2701 2702 Iterator readIterator; 2703 Iterator writeIterator; 2704 StoredKeySet set = (StoredKeySet) map.keySet(); 2705 2706 readIterator = set.iterator(false); 2708 try { 2709 Iterator readIterator2 = set.iterator(false); 2710 StoredIterator.close(readIterator2); 2711 } finally { 2712 StoredIterator.close(readIterator); 2713 } 2714 2715 writeIterator = set.iterator(true); 2717 try { 2718 Iterator writeIterator2 = set.iterator(true); 2719 StoredIterator.close(writeIterator2); 2720 } finally { 2721 StoredIterator.close(writeIterator); 2722 } 2723 2724 readIterator = set.iterator(false); 2726 try { 2727 writeIterator = set.iterator(true); 2728 fail(); 2729 StoredIterator.close(writeIterator); 2730 } catch (IllegalStateException e) { 2731 } finally { 2732 StoredIterator.close(readIterator); 2733 } 2734 2735 if (index == null) { 2736 readIterator = set.iterator(false); 2738 try { 2739 map.put(makeKey(1), makeVal(1)); 2740 fail(); 2741 } catch (IllegalStateException e) { 2742 } finally { 2743 StoredIterator.close(readIterator); 2744 } 2745 2746 writeIterator = set.iterator(true); 2748 try { 2749 if (testStore.isQueueOrRecno()) { 2750 try { 2751 map.append(makeVal(1)); 2752 fail(); 2753 } catch (IllegalStateException e) {} 2754 } else { 2755 map.append(makeVal(1)); 2756 } 2757 } finally { 2758 StoredIterator.close(writeIterator); 2759 } 2760 } 2761 } 2762 2763 Object makeVal(int key) { 2764 2765 if (isEntityBinding) { 2766 return makeEntity(key); 2767 } else { 2768 return new Long (key + 100); 2769 } 2770 } 2771 2772 Object makeVal(int key, int val) { 2773 2774 if (isEntityBinding) { 2775 return makeEntity(key, val); 2776 } else { 2777 return makeVal(val); 2778 } 2779 } 2780 2781 Object makeEntity(int key, int val) { 2782 2783 return new TestEntity(key, val + 100); 2784 } 2785 2786 int intVal(Object val) { 2787 2788 if (isEntityBinding) { 2789 return ((TestEntity) val).value - 100; 2790 } else { 2791 return ((Long ) val).intValue() - 100; 2792 } 2793 } 2794 2795 int intKey(Object key) { 2796 2797 return ((Long ) key).intValue(); 2798 } 2799 2800 Object makeVal(Long key) { 2801 2802 return makeVal(key.intValue()); 2803 } 2804 2805 Object makeEntity(int key) { 2806 2807 return makeEntity(key, key); 2808 } 2809 2810 Object makeEntity(Long key) { 2811 2812 return makeEntity(key.intValue()); 2813 } 2814 2815 int intIter(Collection coll, Object value) { 2816 2817 if (coll instanceof StoredKeySet) { 2818 return intKey(value); 2819 } else { 2820 if (coll instanceof StoredEntrySet) { 2821 value = ((Map.Entry ) value).getValue(); 2822 } 2823 return intVal(value); 2824 } 2825 } 2826 2827 Map.Entry mapEntry(Object key, Object val) { 2828 2829 return new MapEntryParameter(key, val); 2830 } 2831 2832 Map.Entry mapEntry(int key) { 2833 2834 return new MapEntryParameter(makeKey(key), makeVal(key)); 2835 } 2836 2837 Long makeKey(int key) { 2838 2839 return new Long (key); 2840 } 2841 2842 boolean isSubMap() { 2843 2844 return rangeType != NONE; 2845 } 2846 2847 void checkDupsSize(int expected, Collection coll) { 2848 2849 assertEquals(expected, coll.size()); 2850 if (coll instanceof StoredCollection) { 2851 StoredIterator i = ((StoredCollection) coll).storedIterator(false); 2852 try { 2853 int actual = 0; 2854 if (i.hasNext()) { 2855 i.next(); 2856 actual = i.count(); 2857 } 2858 assertEquals(expected, actual); 2859 } finally { 2860 StoredIterator.close(i); 2861 } 2862 } 2863 } 2864 2865 private boolean isListAddAllowed() { 2866 2867 return list != null && testStore.isQueueOrRecno() && 2868 list.areKeysRenumbered(); 2869 } 2870 2871 private int countElements(Collection coll) { 2872 2873 int count = 0; 2874 Iterator iter = iterator(coll); 2875 try { 2876 while (iter.hasNext()) { 2877 iter.next(); 2878 count += 1; 2879 } 2880 } finally { 2881 StoredIterator.close(iter); 2882 } 2883 return count; 2884 } 2885} 2886 | Popular Tags |