KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > collections > test > CollectionTest


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: CollectionTest.java,v 1.53 2006/10/30 21:14:38 bostic Exp $
7  */

8
9 package com.sleepycat.collections.test;
10
11 import java.util.ArrayList JavaDoc;
12 import java.util.Collection JavaDoc;
13 import java.util.Collections JavaDoc;
14 import java.util.Enumeration JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.ListIterator JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.NoSuchElementException JavaDoc;
21 import java.util.Set JavaDoc;
22 import java.util.SortedMap JavaDoc;
23 import java.util.SortedSet JavaDoc;
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 /**
54  * @author Mark Hayes
55  */

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     /*
64      * For long tests we permute testStoredIterator to test both StoredIterator
65      * and BlockIterator. When testing BlockIterator, we permute the maxKey
66      * over the array values below. BlockIterator's block size is 10. So we
67      * test below the block size (6), at the block size (10), and above it (14
68      * and 22).
69      */

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; /* Must be a multiple of 2. */
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 JavaDoc 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; // insertable map (primary store for indexed map)
95
private StoredSortedMap smap; // sorted map (null or equal to map)
96
private StoredMap saveMap;
97     private StoredSortedMap saveSMap;
98     private int rangeType;
99     private StoredList list;
100     private StoredList ilist; // insertable list (primary store for index list)
101
private StoredList saveList;
102     private StoredKeySet keySet;
103     private StoredValueSet valueSet;
104
105     /**
106      * Runs a command line collection test.
107      * @see #usage
108      */

109     public static void main(String JavaDoc[] args)
110         throws Exception JavaDoc {
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 JavaDoc {
148
149         return suite(null);
150     }
151
152     static Test suite(String JavaDoc[] args)
153         throws Exception JavaDoc {
154
155         if ("true".equals(System.getProperty("longtest"))) {
156             TestSuite suite = new TestSuite();
157
158             /* StoredIterator tests. */
159             permuteTests(args, suite, true, DEFAULT_MAX_KEY);
160
161             /* BlockIterator tests with different maxKey values. */
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 JavaDoc[] args,
173                                      TestSuite suite,
174                                      boolean storedIter,
175                                      int maxKey)
176         throws Exception JavaDoc {
177
178         TestSuite baseTests = baseSuite(args);
179         Enumeration JavaDoc 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 JavaDoc[] args)
188         throws Exception JavaDoc {
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 JavaDoc[] 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 JavaDoc {
258
259         setName(testName);
260     }
261
262     public void runTest()
263         throws Exception JavaDoc {
264
265         DbTestUtil.printTestName(DbTestUtil.qualifiedTestName(this));
266         try {
267             env = testEnv.open(testName);
268
269             // For testing auto-commit, use a normal (transactional) runner for
270
// all reading and for writing via an iterator, and a do-nothing
271
// runner for writing via collections; if auto-commit is tested,
272
// the per-collection auto-commit property will be set elsewhere.
273
//
274
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 JavaDoc e) {
304             throw ExceptionUnwrapper.unwrap(e);
305         } finally {
306             if (index != null) {
307                 try {
308             index.close();
309         } catch (Exception JavaDoc e) {
310         }
311             }
312             if (store != null) {
313                 try {
314             store.close();
315         } catch (Exception JavaDoc e) {
316         }
317             }
318             if (env != null) {
319                 try {
320             env.close();
321         } catch (Exception JavaDoc e) {
322         }
323             }
324             /* Ensure that GC can cleanup. */
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     /**
347      * Is overridden in XACollectionTest.
348      */

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 JavaDoc {
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 JavaDoc map)
368         throws Exception JavaDoc {
369
370         assertTrue(map.values() instanceof Set JavaDoc);
371         assertEquals(testStore.isOrdered(),
372                      map.keySet() instanceof SortedSet JavaDoc);
373         assertEquals(testStore.isOrdered(),
374                      map.entrySet() instanceof SortedSet JavaDoc);
375         assertEquals(testStore.isOrdered() && isEntityBinding,
376                      map.values() instanceof SortedSet JavaDoc);
377     }
378
379     void testUnindexed()
380         throws Exception JavaDoc {
381
382         // create primary map
383
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                 // sorted value set is not possible since key cannot be derived
395
// for performing subSet, etc.
396
}
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         // create primary list
415
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 JavaDoc 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 JavaDoc {
449
450         // create primary map
451
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         // create primary list
461
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         // create indexed map (keySet/valueSet)
476
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                 // sorted value set is not possible since key cannot be derived
486
// for performing subSet, etc.
487
}
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         // create indexed list
502
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 JavaDoc 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 JavaDoc {
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         // use bulk operations to check that explicitly constructed
631
// keySet/valueSet are equivalent
632
assertTrue(imap.keySet().equals(keySet));
633         if (valueSet != null) {
634             assertTrue(imap.values().equals(valueSet));
635         }
636     }
637
638     Iterator JavaDoc iterator(Collection JavaDoc 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 JavaDoc {
649
650         writeRunner.run(new TransactionWorker() {
651             public void doWork() throws Exception JavaDoc {
652                 assertTrue(imap.isEmpty());
653                 Iterator JavaDoc 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 JavaDoc[0]).length);
661                 assertEquals(0, imap.entrySet().toArray().length);
662                 assertEquals(0, imap.entrySet().toArray(new Object JavaDoc[0]).length);
663                 assertEquals(0, imap.values().toArray().length);
664                 assertEquals(0, imap.values().toArray(new Object JavaDoc[0]).length);
665
666                 for (int i = beginKey; i <= endKey; i += 1) {
667                     Long JavaDoc key = makeKey(i);
668                     Object JavaDoc 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 JavaDoc {
689
690         writeRunner.run(new TransactionWorker() {
691             public void doWork() throws Exception JavaDoc {
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 JavaDoc key = makeKey(i);
702                     Object JavaDoc 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 JavaDoc recnoKey;
713                         if (useList && ilist != null) {
714                             recnoKey = new Long JavaDoc(ilist.append(val) + 1);
715                         } else {
716                             recnoKey = (Long JavaDoc) imap.append(val);
717                         }
718                         assertNotNull(recnoKey);
719                         Object JavaDoc 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 JavaDoc {
734
735         writeRunner.run(new TransactionWorker() {
736             public void doWork() throws Exception JavaDoc {
737                 for (int i = beginKey; i <= endKey; i += 1) {
738                     Long JavaDoc key = makeKey(i);
739                     Object JavaDoc 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 JavaDoc iter = (ListIterator JavaDoc) iterator(map.keySet());
756                     try {
757                         assertNotNull(iter.next());
758                         iter.set(makeKey(beginKey));
759                         fail();
760                     } catch (UnsupportedOperationException JavaDoc 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 JavaDoc coll)
773         throws Exception JavaDoc {
774
775         writeIterRunner.run(new TransactionWorker() {
776             public void doWork() throws Exception JavaDoc {
777                 ListIterator JavaDoc iter = (ListIterator JavaDoc) iterator(coll);
778                 try {
779                     for (int i = beginKey; i <= endKey; i += 1) {
780                         assertTrue(iter.hasNext());
781                         Object JavaDoc obj = iter.next();
782                         if (index != null) {
783                             try {
784                                 setValuePlusOne(iter, obj);
785                                 fail();
786                             }
787                             catch (UnsupportedOperationException JavaDoc e) {}
788                         } else if
789                            (((StoredCollection) coll).areDuplicatesOrdered()) {
790                             try {
791                                 setValuePlusOne(iter, obj);
792                                 fail();
793                             } catch (RuntimeException JavaDoc e) {
794                                 Exception JavaDoc e2 = ExceptionUnwrapper.unwrap(e);
795                                 assertTrue(e2.getClass().getName(),
796                                       e2 instanceof IllegalArgumentException JavaDoc ||
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 JavaDoc iter, Object JavaDoc obj) {
812
813         if (obj instanceof Map.Entry JavaDoc) {
814             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) obj;
815             Long JavaDoc key = (Long JavaDoc) entry.getKey();
816             Object JavaDoc oldVal = entry.getValue();
817             Object JavaDoc val = makeVal(key.intValue() + 1);
818             if (isEntityBinding) {
819                 try {
820                     // must fail on attempt to change the key via an entity
821
entry.setValue(val);
822                     fail();
823                 }
824                 catch (IllegalArgumentException JavaDoc 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 JavaDoc oldVal = obj;
839             Long JavaDoc key = makeKey(intVal(obj));
840             Object JavaDoc val = makeVal(key.intValue() + 1);
841             if (isEntityBinding) {
842                 try {
843                     // must fail on attempt to change the key via an entity
844
iter.set(val);
845                     fail();
846                 }
847                 catch (IllegalArgumentException JavaDoc 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 JavaDoc {
863
864         writeIterRunner.run(new TransactionWorker() {
865             public void doWork() throws Exception JavaDoc {
866                 assertTrue(!map.isEmpty());
867                 ListIterator JavaDoc iter = null;
868                 try {
869                     if (list != null) {
870                         iter = (ListIterator JavaDoc) iterator(list);
871                     } else {
872                         iter = (ListIterator JavaDoc) iterator(map.values());
873                     }
874                     iteratorSetAndRemoveNotAllowed(iter);
875
876                     Object JavaDoc 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 JavaDoc key = makeKey(i);
901                     Object JavaDoc 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 JavaDoc {
912
913         writeRunner.run(new TransactionWorker() {
914             public void doWork() throws Exception JavaDoc {
915                 map.clear();
916                 assertTrue(map.isEmpty());
917             }
918         });
919     }
920
921     void iteratorSetAndRemoveNotAllowed(ListIterator JavaDoc i) {
922
923         try {
924             i.remove();
925             fail();
926         } catch (IllegalStateException JavaDoc e) {}
927
928         if (index == null) {
929             try {
930                 Object JavaDoc val = makeVal(1);
931                 i.set(val);
932                 fail();
933             }
934             catch (IllegalStateException JavaDoc e) {}
935         }
936     }
937
938     void removeOdd()
939         throws Exception JavaDoc {
940
941         writeRunner.run(new TransactionWorker() {
942             public void doWork() throws Exception JavaDoc {
943                 boolean toggle = false;
944                 for (int i = beginKey; i <= endKey; i += 2) {
945                     toggle = !toggle;
946                     Long JavaDoc key = makeKey(i);
947                     Object JavaDoc 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 JavaDoc 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 JavaDoc {
969
970         writeRunner.run(new TransactionWorker() {
971             public void doWork() throws Exception JavaDoc {
972                 for (int i = beginKey; i <= endKey; i += 2) {
973                     Long JavaDoc key = makeKey(i);
974                     Object JavaDoc 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 JavaDoc {
988
989         writeRunner.run(new TransactionWorker() {
990             public void doWork() throws Exception JavaDoc {
991                 for (int i = beginKey; i <= endKey; i += 2) {
992                     Long JavaDoc key = makeKey(i);
993                     Object JavaDoc 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 JavaDoc {
1005
1006        writeIterRunner.run(new TransactionWorker() {
1007            public void doWork() throws Exception JavaDoc {
1008                Iterator JavaDoc iter = iterator(map.keySet());
1009                try {
1010                    for (int i = beginKey; i <= endKey; i += 1) {
1011                        assertTrue(iter.hasNext());
1012                        Long JavaDoc key = (Long JavaDoc) iter.next();
1013                        assertNotNull(key);
1014                        if (map instanceof SortedMap JavaDoc) {
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 JavaDoc {
1030
1031        writeRunner.run(new TransactionWorker() {
1032            public void doWork() throws Exception JavaDoc {
1033                for (int i = beginKey; i <= endKey; i += 2) {
1034                    // remove by index
1035
// (with entity binding, embbeded keys in values are
1036
// being changed so we can't use values for comparison)
1037
int idx = (i - beginKey) / 2;
1038                    Object JavaDoc 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 JavaDoc {
1061
1062        writeRunner.run(new TransactionWorker() {
1063            public void doWork() throws Exception JavaDoc {
1064                for (int i = beginKey; i <= endKey; i += 2) {
1065                    // for non-entity case remove by value
1066
// (with entity binding, embbeded keys in values are
1067
// being changed so we can't use values for comparison)
1068
int idx = (i - beginKey) / 2;
1069                    Object JavaDoc 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 JavaDoc {
1084
1085        writeRunner.run(new TransactionWorker() {
1086            public void doWork() throws Exception JavaDoc {
1087                // add using Map.put()
1088
for (int i = beginKey; i <= endKey; i += 2) {
1089                    Long JavaDoc key = makeKey(i);
1090                    Object JavaDoc 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 JavaDoc {
1109
1110        writeRunner.run(new TransactionWorker() {
1111            public void doWork() throws Exception JavaDoc {
1112                // add using Map.values().add()
1113
for (int i = beginKey; i <= endKey; i += 2) {
1114                    Long JavaDoc key = makeKey(i);
1115                    Object JavaDoc 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 JavaDoc {
1133
1134        writeRunner.run(new TransactionWorker() {
1135            public void doWork() throws Exception JavaDoc {
1136                // add using Map.duplicates().add()
1137
for (int i = beginKey; i <= endKey; i += 2) {
1138                    Long JavaDoc key = makeKey(i);
1139                    Object JavaDoc 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 JavaDoc {
1158
1159        writeRunner.run(new TransactionWorker() {
1160            public void doWork() throws Exception JavaDoc {
1161                for (int i = beginKey; i <= endKey; i += 2) {
1162                    int idx = i - beginKey;
1163                    Object JavaDoc 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 JavaDoc {
1176
1177        writeRunner.run(new TransactionWorker() {
1178            public void doWork() throws Exception JavaDoc {
1179                for (int i = beginKey; i <= endKey; i += 1) {
1180                    int idx = i - beginKey;
1181                    Object JavaDoc 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 JavaDoc {
1193
1194        writeRunner.run(new TransactionWorker() {
1195            public void doWork() throws Exception JavaDoc {
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 JavaDoc {
1209
1210        writeIterRunner.run(new TransactionWorker() {
1211            public void doWork() throws Exception JavaDoc {
1212                ListIterator JavaDoc i = (ListIterator JavaDoc) 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 JavaDoc {
1259
1260        writeIterRunner.run(new TransactionWorker() {
1261            public void doWork() throws Exception JavaDoc {
1262                assertNull(imap.put(makeKey(1), makeVal(1)));
1263                ListIterator JavaDoc i =
1264                    (ListIterator JavaDoc) 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 JavaDoc {
1308
1309        readRunner.run(new TransactionWorker() {
1310            public void doWork() throws Exception JavaDoc {
1311                // map
1312

1313                assertNotNull(map.toString());
1314                for (int i = beginKey; i <= endKey; i += 1) {
1315                    Long JavaDoc key = makeKey(i);
1316                    Object JavaDoc 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                // entrySet
1335

1336                Set JavaDoc set = map.entrySet();
1337                assertNotNull(set.toString());
1338                assertEquals(beginKey > endKey, set.isEmpty());
1339                Iterator JavaDoc iter = iterator(set);
1340                try {
1341                    for (int i = beginKey; i <= endKey; i += 1) {
1342                        assertTrue(iter.hasNext());
1343                        Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
1344                        Long JavaDoc key = (Long JavaDoc) entry.getKey();
1345                        Object JavaDoc val = entry.getValue();
1346                        if (map instanceof SortedMap JavaDoc) {
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 JavaDoc[] entries =
1357                    (Map.Entry JavaDoc[]) set.toArray(new Map.Entry JavaDoc[0]);
1358                assertNotNull(entries);
1359                assertEquals(endKey - beginKey + 1, entries.length);
1360                for (int i = beginKey; i <= endKey; i += 1) {
1361                    Map.Entry JavaDoc entry = entries[i - beginKey];
1362                    assertNotNull(entry);
1363                    if (map instanceof SortedMap JavaDoc) {
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 JavaDoc sset = (SortedSet JavaDoc) 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 JavaDoc e) {}
1398                            }
1399                            if (rangeType != HEAD) {
1400                                try {
1401                                    sset.subSet(mapEntry(0),
1402                                                mapEntry(1));
1403                                    fail();
1404                                } catch (IllegalArgumentException JavaDoc 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                // keySet
1421

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 JavaDoc key = (Long JavaDoc) iter.next();
1430                        assertTrue(set.contains(key));
1431                        Object JavaDoc val = map.get(key);
1432                        if (map instanceof SortedMap JavaDoc) {
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 JavaDoc[] keys = (Long JavaDoc[]) set.toArray(new Long JavaDoc[0]);
1442                assertNotNull(keys);
1443                assertEquals(endKey - beginKey + 1, keys.length);
1444                for (int i = beginKey; i <= endKey; i += 1) {
1445                    Long JavaDoc key = keys[i - beginKey];
1446                    assertNotNull(key);
1447                    if (map instanceof SortedMap JavaDoc) {
1448                        assertEquals(makeKey(i), key);
1449                    }
1450                }
1451                readIterator(set, iterator(set), beginKey, endKey);
1452
1453                // values
1454

1455                Collection JavaDoc 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 JavaDoc val = iter.next();
1463                        if (map instanceof SortedMap JavaDoc) {
1464                            assertEquals(makeVal(i), val);
1465                        }
1466                    }
1467                    assertTrue(!iter.hasNext());
1468                } finally {
1469                    StoredIterator.close(iter);
1470                }
1471                Object JavaDoc[] values = coll.toArray();
1472                assertNotNull(values);
1473                assertEquals(endKey - beginKey + 1, values.length);
1474                for (int i = beginKey; i <= endKey; i += 1) {
1475                    Object JavaDoc val = values[i - beginKey];
1476                    assertNotNull(val);
1477                    if (map instanceof SortedMap JavaDoc) {
1478                        assertEquals(makeVal(i), val);
1479                    }
1480                }
1481                readIterator(coll, iterator(coll), beginKey, endKey);
1482
1483                // list
1484

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 JavaDoc 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 JavaDoc li = (ListIterator JavaDoc) 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 JavaDoc 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 JavaDoc 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 JavaDoc val = values[i - beginKey];
1531                        assertNotNull(val);
1532                        assertEquals(makeVal(i), val);
1533                    }
1534                    readIterator(list, iterator(list), beginKey, endKey);
1535                }
1536
1537                // first/last
1538

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 JavaDoc) smap.keySet()).first());
1546                        Object JavaDoc entry = ((SortedSet JavaDoc) smap.entrySet()).first();
1547                        assertEquals(makeKey(beginKey),
1548                                     ((Map.Entry JavaDoc) entry).getKey());
1549                        if (smap.values() instanceof SortedSet JavaDoc) {
1550                            assertEquals(makeVal(beginKey),
1551                                         ((SortedSet JavaDoc) smap.values()).first());
1552                        }
1553                    } else {
1554                        assertNull(smap.firstKey());
1555                        assertNull(((SortedSet JavaDoc) smap.keySet()).first());
1556                        assertNull(((SortedSet JavaDoc) smap.entrySet()).first());
1557                        if (smap.values() instanceof SortedSet JavaDoc) {
1558                            assertNull(((SortedSet JavaDoc) 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 JavaDoc) smap.keySet()).last());
1567                        Object JavaDoc entry = ((SortedSet JavaDoc) smap.entrySet()).last();
1568                        assertEquals(makeKey(endKey),
1569                                     ((Map.Entry JavaDoc) entry).getKey());
1570                        if (smap.values() instanceof SortedSet JavaDoc) {
1571                            assertEquals(makeVal(endKey),
1572                                         ((SortedSet JavaDoc) smap.values()).last());
1573                        }
1574                    } else {
1575                        assertNull(smap.lastKey());
1576                        assertNull(((SortedSet JavaDoc) smap.keySet()).last());
1577                        assertNull(((SortedSet JavaDoc) smap.entrySet()).last());
1578                        if (smap.values() instanceof SortedSet JavaDoc) {
1579                            assertNull(((SortedSet JavaDoc) smap.values()).last());
1580                        }
1581                    }
1582                }
1583            }
1584        });
1585    }
1586
1587    void readEven()
1588        throws Exception JavaDoc {
1589
1590        readRunner.run(new TransactionWorker() {
1591            public void doWork() throws Exception JavaDoc {
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                // map
1598

1599                for (int i = beginKey; i <= endKey; i += 1) {
1600                    Long JavaDoc key = makeKey(i);
1601                    if ((i & 1) == 0) {
1602                        Object JavaDoc 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 JavaDoc 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                // entrySet
1622

1623                Set JavaDoc set = map.entrySet();
1624                assertEquals(beginKey > endKey, set.isEmpty());
1625                Iterator JavaDoc iter = iterator(set);
1626                try {
1627                    for (int i = readBegin; i <= readEnd; i += readIncr) {
1628                        assertTrue(iter.hasNext());
1629                        Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
1630                        Long JavaDoc key = (Long JavaDoc) entry.getKey();
1631                        Object JavaDoc val = entry.getValue();
1632                        if (map instanceof SortedMap JavaDoc) {
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                // keySet
1644

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 JavaDoc key = (Long JavaDoc) iter.next();
1652                        assertTrue(set.contains(key));
1653                        Object JavaDoc val = map.get(key);
1654                        if (map instanceof SortedMap JavaDoc) {
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                // values
1665

1666                Collection JavaDoc 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 JavaDoc val = iter.next();
1673                        if (map instanceof SortedMap JavaDoc) {
1674                            assertEquals(makeVal(i), val);
1675                        }
1676                    }
1677                    assertTrue(!iter.hasNext());
1678                } finally {
1679                    StoredIterator.close(iter);
1680                }
1681
1682
1683                // list not used since keys may not be renumbered for this
1684
// method to work in general
1685

1686                // first/last
1687

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 JavaDoc) smap.keySet()).first());
1695                        Object JavaDoc entry = ((SortedSet JavaDoc) smap.entrySet()).first();
1696                        assertEquals(makeKey(readBegin),
1697                                     ((Map.Entry JavaDoc) entry).getKey());
1698                        if (smap.values() instanceof SortedSet JavaDoc) {
1699                            assertEquals(makeVal(readBegin),
1700                                         ((SortedSet JavaDoc) smap.values()).first());
1701                        }
1702                    } else {
1703                        assertNull(smap.firstKey());
1704                        assertNull(((SortedSet JavaDoc) smap.keySet()).first());
1705                        assertNull(((SortedSet JavaDoc) smap.entrySet()).first());
1706                        if (smap.values() instanceof SortedSet JavaDoc) {
1707                            assertNull(((SortedSet JavaDoc) 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 JavaDoc) smap.keySet()).last());
1716                        Object JavaDoc entry = ((SortedSet JavaDoc) smap.entrySet()).last();
1717                        assertEquals(makeKey(readEnd),
1718                                     ((Map.Entry JavaDoc) entry).getKey());
1719                        if (smap.values() instanceof SortedSet JavaDoc) {
1720                            assertEquals(makeVal(readEnd),
1721                                         ((SortedSet JavaDoc) smap.values()).last());
1722                        }
1723                    } else {
1724                        assertNull(smap.lastKey());
1725                        assertNull(((SortedSet JavaDoc) smap.keySet()).last());
1726                        assertNull(((SortedSet JavaDoc) smap.entrySet()).last());
1727                        if (smap.values() instanceof SortedSet JavaDoc) {
1728                            assertNull(((SortedSet JavaDoc) smap.values()).last());
1729                        }
1730                    }
1731                }
1732            }
1733        });
1734    }
1735
1736    void readEvenList()
1737        throws Exception JavaDoc {
1738
1739        readRunner.run(new TransactionWorker() {
1740            public void doWork() throws Exception JavaDoc {
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 JavaDoc iter = (ListIterator JavaDoc) 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 JavaDoc 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 JavaDoc coll, Iterator JavaDoc iter,
1771                      int beginValue, int endValue) {
1772
1773        ListIterator JavaDoc li = (ListIterator JavaDoc) iter;
1774        boolean isList = (coll instanceof List JavaDoc);
1775        Iterator JavaDoc clone = null;
1776        try {
1777            // at beginning
1778
assertTrue(!li.hasPrevious());
1779            assertTrue(!li.hasPrevious());
1780            try { li.previous(); } catch (NoSuchElementException JavaDoc e) {}
1781            if (isList) {
1782                assertEquals(-1, li.previousIndex());
1783            }
1784            if (endValue < beginValue) {
1785                // is empty
1786
assertTrue(!iter.hasNext());
1787                try { iter.next(); } catch (NoSuchElementException JavaDoc e) {}
1788                if (isList) {
1789                    assertEquals(Integer.MAX_VALUE, li.nextIndex());
1790                }
1791            }
1792            // loop thru all and collect in array
1793
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            // at end
1813
assertTrue(!iter.hasNext());
1814            try { iter.next(); } catch (NoSuchElementException JavaDoc e) {}
1815            if (isList) {
1816                assertEquals(Integer.MAX_VALUE, li.nextIndex());
1817            }
1818            // clone at same position
1819
clone = StoredCollections.iterator(iter);
1820            assertTrue(!clone.hasNext());
1821            // loop thru in reverse
1822
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            // clone should not have changed
1835
assertTrue(!clone.hasNext());
1836            // at beginning
1837
assertTrue(!li.hasPrevious());
1838            try { li.previous(); } catch (NoSuchElementException JavaDoc e) {}
1839            if (isList) {
1840                assertEquals(-1, li.previousIndex());
1841            }
1842            // loop thru with some back-and-forth
1843
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 JavaDoc 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            // at end
1865
assertTrue(!iter.hasNext());
1866            try { iter.next(); } catch (NoSuchElementException JavaDoc 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 JavaDoc {
1878
1879        writeRunner.run(new TransactionWorker() {
1880            public void doWork() throws Exception JavaDoc {
1881                HashMap JavaDoc hmap = new HashMap JavaDoc();
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                // restore values to non-null
1945
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 JavaDoc {
1961
1962        writeRunner.run(new TransactionWorker() {
1963            public void doWork() throws Exception JavaDoc {
1964                ArrayList JavaDoc alist = new ArrayList JavaDoc();
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                    // deleting in a renumbered list with entity binding will
1991
// change the values dynamically, making it very difficult
1992
// to test
1993
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                    // addAll at an index is also very difficult to test with
2003
// an entity binding
2004

2005                    // addAll at first index
2006
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                    // addAll at last index
2014
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                    // addAll in the middle
2022
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 JavaDoc {
2036
2037        writeRunner.run(new TransactionWorker() {
2038            public void doWork() throws Exception JavaDoc {
2039                setRange(type, rangeBegin, rangeEnd);
2040                createOutOfRange(rangeBegin, rangeEnd);
2041                if (rangeType != TAIL) {
2042                    writeOutOfRange(new Long JavaDoc(rangeEnd + 1));
2043                }
2044                if (rangeType != HEAD) {
2045                    writeOutOfRange(new Long JavaDoc(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 JavaDoc 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            // check for equivalent ranges
2086
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 JavaDoc) {
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            // check for equivalent ranges
2112
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 JavaDoc) {
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            // check for equivalent ranges
2134
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 JavaDoc) {
2144                assertEquals(smap.values(),
2145                            ((StoredSortedValueSet) saveSMap.values()).tailSet(
2146                                makeVal(rangeBegin), true));
2147            }
2148            break;
2149        default: throw new RuntimeException JavaDoc();
2150        }
2151        map = smap;
2152        beginKey = rangeBegin;
2153        if (rangeBegin < 1 || rangeEnd > maxKey) {
2154            endKey = rangeBegin - 1; // force empty range for readAll()
2155
} 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 JavaDoc {
2172
2173        // map
2174

2175        if (rangeType != TAIL) {
2176            try {
2177                smap.subMap(makeKey(rangeBegin), makeKey(rangeEnd + 2));
2178                fail();
2179            } catch (IllegalArgumentException JavaDoc e) { }
2180            try {
2181                smap.headMap(makeKey(rangeEnd + 2));
2182                fail();
2183            } catch (IllegalArgumentException JavaDoc 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 JavaDoc e) { }
2191            try {
2192                smap.tailMap(makeKey(rangeBegin - 1));
2193                fail();
2194            }
2195            catch (IllegalArgumentException JavaDoc e) { }
2196            checkDupsSize(0, smap.duplicates(makeKey(rangeBegin - 1)));
2197        }
2198
2199        // keySet
2200

2201        if (rangeType != TAIL) {
2202            SortedSet JavaDoc sset = (SortedSet JavaDoc) map.keySet();
2203            try {
2204                sset.subSet(makeKey(rangeBegin), makeKey(rangeEnd + 2));
2205                fail();
2206            } catch (IllegalArgumentException JavaDoc e) { }
2207            try {
2208                sset.headSet(makeKey(rangeEnd + 2));
2209                fail();
2210            } catch (IllegalArgumentException JavaDoc e) { }
2211            try {
2212                iterator(sset.subSet(makeKey(rangeEnd + 1),
2213                                     makeKey(rangeEnd + 2)));
2214                fail();
2215            } catch (IllegalArgumentException JavaDoc e) { }
2216        }
2217        if (rangeType != HEAD) {
2218            SortedSet JavaDoc sset = (SortedSet JavaDoc) map.keySet();
2219            try {
2220                sset.subSet(makeKey(rangeBegin - 1), makeKey(rangeEnd + 1));
2221                fail();
2222            } catch (IllegalArgumentException JavaDoc e) { }
2223            try {
2224                sset.tailSet(makeKey(rangeBegin - 1));
2225                fail();
2226            }
2227            catch (IllegalArgumentException JavaDoc e) { }
2228            try {
2229                iterator(sset.subSet(makeKey(rangeBegin - 1),
2230                                     makeKey(rangeBegin)));
2231                fail();
2232            }
2233            catch (IllegalArgumentException JavaDoc e) { }
2234        }
2235
2236        // entrySet
2237

2238        if (rangeType != TAIL) {
2239            SortedSet JavaDoc sset = (SortedSet JavaDoc) map.entrySet();
2240            try {
2241                sset.subSet(mapEntry(rangeBegin), mapEntry(rangeEnd + 2));
2242                fail();
2243            } catch (IllegalArgumentException JavaDoc e) { }
2244            try {
2245                sset.headSet(mapEntry(rangeEnd + 2));
2246                fail();
2247            } catch (IllegalArgumentException JavaDoc e) { }
2248            try {
2249                iterator(sset.subSet(mapEntry(rangeEnd + 1),
2250                                     mapEntry(rangeEnd + 2)));
2251                fail();
2252            } catch (IllegalArgumentException JavaDoc e) { }
2253        }
2254        if (rangeType != HEAD) {
2255            SortedSet JavaDoc sset = (SortedSet JavaDoc) map.entrySet();
2256            try {
2257                sset.subSet(mapEntry(rangeBegin - 1), mapEntry(rangeEnd + 1));
2258                fail();
2259            } catch (IllegalArgumentException JavaDoc e) { }
2260            try {
2261                sset.tailSet(mapEntry(rangeBegin - 1));
2262                fail();
2263            } catch (IllegalArgumentException JavaDoc e) { }
2264            try {
2265                iterator(sset.subSet(mapEntry(rangeBegin - 1),
2266                                     mapEntry(rangeBegin)));
2267                fail();
2268            }
2269            catch (IllegalArgumentException JavaDoc e) { }
2270        }
2271
2272        // values
2273

2274        if (map.values() instanceof SortedSet JavaDoc) {
2275            SortedSet JavaDoc sset = (SortedSet JavaDoc) map.values();
2276            if (rangeType != TAIL) {
2277                try {
2278                    sset.subSet(makeVal(rangeBegin),
2279                                makeVal(rangeEnd + 2));
2280                    fail();
2281                } catch (IllegalArgumentException JavaDoc e) { }
2282                try {
2283                    sset.headSet(makeVal(rangeEnd + 2));
2284                    fail();
2285                } catch (IllegalArgumentException JavaDoc e) { }
2286            }
2287            if (rangeType != HEAD) {
2288                try {
2289                    sset.subSet(makeVal(rangeBegin - 1),
2290                                makeVal(rangeEnd + 1));
2291                    fail();
2292                } catch (IllegalArgumentException JavaDoc e) { }
2293                try {
2294                    sset.tailSet(makeVal(rangeBegin - 1));
2295                    fail();
2296                }
2297                catch (IllegalArgumentException JavaDoc e) { }
2298            }
2299        }
2300
2301        // list
2302

2303        if (list != null) {
2304            int size = rangeEnd - rangeBegin + 1;
2305            try {
2306                list.subList(0, size + 1);
2307                fail();
2308            } catch (IndexOutOfBoundsException JavaDoc e) { }
2309            try {
2310                list.subList(-1, size);
2311                fail();
2312            } catch (IndexOutOfBoundsException JavaDoc e) { }
2313            try {
2314                list.subList(2, 1);
2315                fail();
2316            } catch (IndexOutOfBoundsException JavaDoc e) { }
2317            try {
2318                list.subList(size, size);
2319                fail();
2320            }
2321            catch (IndexOutOfBoundsException JavaDoc e) { }
2322        }
2323    }
2324
2325    void writeOutOfRange(Long JavaDoc badNewKey)
2326        throws Exception JavaDoc {
2327
2328        try {
2329            map.put(badNewKey, makeVal(badNewKey));
2330            fail();
2331        } catch (IllegalArgumentException JavaDoc e) {
2332            assertTrue(e.toString(), index == null);
2333        } catch (UnsupportedOperationException JavaDoc e) {
2334            assertTrue(index != null);
2335        }
2336        try {
2337            map.keySet().add(badNewKey);
2338            fail();
2339        } catch (IllegalArgumentException JavaDoc e) {
2340            assertTrue(index == null);
2341        } catch (UnsupportedOperationException JavaDoc e) {
2342            assertTrue(index != null);
2343        }
2344        try {
2345            map.values().add(makeEntity(badNewKey));
2346            fail();
2347        } catch (IllegalArgumentException JavaDoc e) {
2348            assertTrue(isEntityBinding && index == null);
2349        } catch (UnsupportedOperationException JavaDoc 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 JavaDoc e) {
2358                assertTrue(index == null);
2359            } catch (UnsupportedOperationException JavaDoc e) {
2360                assertTrue(index != null);
2361            }
2362            try {
2363                list.add(i, makeVal(badNewKey));
2364                fail();
2365            }
2366            catch (UnsupportedOperationException JavaDoc e) {
2367            }
2368        }
2369    }
2370
2371    void readWriteDuplicates()
2372        throws Exception JavaDoc {
2373
2374        writeRunner.run(new TransactionWorker() {
2375            public void doWork() throws Exception JavaDoc {
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 JavaDoc {
2393
2394        Collection JavaDoc dups;
2395        // make duplicate values
2396
final Long JavaDoc key = makeKey(i);
2397        final Object JavaDoc[] values = new Object JavaDoc[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        // add duplicates
2404
outerLoop: for (int writeMode = 0;; writeMode += 1) {
2405            //System.out.println("write mode " + writeMode);
2406
switch (writeMode) {
2407                case 0:
2408                case 1: {
2409                    // write with Map.put()
2410
for (int j = 1; j < values.length; j += 1) {
2411                        map.put(key, values[j]);
2412                    }
2413                    break;
2414                }
2415                case 2: {
2416                    // write with Map.duplicates().add()
2417
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                    // write with Map.duplicates().iterator().add()
2425
writeIterRunner.run(new TransactionWorker() {
2426                        public void doWork() throws Exception JavaDoc {
2427                            Collection JavaDoc dups = map.duplicates(key);
2428                            Iterator JavaDoc 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 JavaDoc) iter).add(values[j]);
2434                                }
2435                            } finally {
2436                                StoredIterator.close(iter);
2437                            }
2438                        }
2439                    });
2440                    break;
2441                }
2442                case 4: {
2443                    // write with Map.values().add()
2444
if (!isEntityBinding) {
2445                        continue;
2446                    }
2447                    Collection JavaDoc 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            // read duplicates
2459
readDuplicates(i, key, values);
2460            // remove duplicates
2461
switch (writeMode) {
2462                case 0: {
2463                    // remove with Map.remove()
2464
checkDupsSize(values.length, map.duplicates(key));
2465                    map.remove(key); // remove all values
2466
checkDupsSize(0, map.duplicates(key));
2467                    map.put(key, values[0]); // put back original value
2468
checkDupsSize(1, map.duplicates(key));
2469                    break;
2470                }
2471                case 1: {
2472                    // remove with Map.keySet().remove()
2473
map.keySet().remove(key); // remove all values
2474
map.put(key, values[0]); // put back original value
2475
break;
2476                }
2477                case 2: {
2478                    // remove with Map.duplicates().clear()
2479
dups = map.duplicates(key);
2480                    dups.clear(); // remove all values
2481
dups.add(values[0]); // put back original value
2482
break;
2483                }
2484                case 3: {
2485                    // remove with Map.duplicates().iterator().remove()
2486
writeIterRunner.run(new TransactionWorker() {
2487                        public void doWork() throws Exception JavaDoc {
2488                            Collection JavaDoc dups = map.duplicates(key);
2489                            Iterator JavaDoc 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                    // remove with Map.values().remove()
2506
if (!isEntityBinding) {
2507                        throw new IllegalStateException JavaDoc();
2508                    }
2509                    Collection JavaDoc 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 JavaDoc();
2516            }
2517            // verify that only original value is present
2518
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 JavaDoc {
2529
2530        Object JavaDoc key = makeKey(i);
2531        Object JavaDoc[] values = new Object JavaDoc[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        // add duplicates
2539
for (int j = 1; j < values.length; j += 1) {
2540            imap.put(makeKey(endKey + j), values[j]);
2541        }
2542        // read duplicates
2543
readDuplicates(i, key, values);
2544        // remove duplicates
2545
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 JavaDoc key, Object JavaDoc[] values) {
2552
2553        boolean isOrdered = map.isOrdered();
2554        Collection JavaDoc dups;
2555        Iterator JavaDoc iter;
2556        // read with Map.duplicates().iterator()
2557
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 JavaDoc val = iter.next();
2564                assertEquals(values[j], val);
2565            }
2566            assertTrue(!iter.hasNext());
2567        } finally {
2568            StoredIterator.close(iter);
2569        }
2570        // read with Map.values().iterator()
2571
Collection JavaDoc clone = ((StoredCollection) map.values()).toList();
2572        iter = iterator(map.values());
2573        try {
2574            for (int j = beginKey; j < i; j += 1) {
2575                Object JavaDoc 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 JavaDoc 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 JavaDoc 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        // read with Map.entrySet().iterator()
2601
clone = ((StoredCollection) map.entrySet()).toList();
2602        iter = iterator(map.entrySet());
2603        try {
2604            for (int j = beginKey; j < i; j += 1) {
2605                Map.Entry JavaDoc entry = (Map.Entry JavaDoc) 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 JavaDoc entry = (Map.Entry JavaDoc) 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 JavaDoc entry = (Map.Entry JavaDoc) 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        // read with Map.keySet().iterator()
2634
clone = ((StoredCollection) map.keySet()).toList();
2635        iter = iterator(map.keySet());
2636        try {
2637            for (int j = beginKey; j < i; j += 1) {
2638                Object JavaDoc val = iter.next();
2639                assertTrue(clone.remove(makeKey(j)));
2640                if (isOrdered) {
2641                    assertEquals(makeKey(j), val);
2642                }
2643            }
2644            if (true) {
2645                // only one key is iterated for all duplicates
2646
Object JavaDoc 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 JavaDoc 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 JavaDoc dups = map.duplicates(makeKey(beginKey));
2669        try {
2670            dups.add(makeVal(beginKey));
2671            fail();
2672        } catch (UnsupportedOperationException JavaDoc expected) { }
2673        ListIterator JavaDoc iter = (ListIterator JavaDoc) iterator(dups);
2674        try {
2675            iter.add(makeVal(beginKey));
2676            fail();
2677        } catch (UnsupportedOperationException JavaDoc expected) {
2678        } finally {
2679            StoredIterator.close(iter);
2680        }
2681    }
2682
2683    void listOperationsNotAllowed() {
2684
2685        ListIterator JavaDoc iter = (ListIterator JavaDoc) iterator(map.values());
2686        try {
2687            try {
2688                iter.nextIndex();
2689                fail();
2690            } catch (UnsupportedOperationException JavaDoc expected) { }
2691            try {
2692                iter.previousIndex();
2693                fail();
2694            } catch (UnsupportedOperationException JavaDoc expected) { }
2695        } finally {
2696            StoredIterator.close(iter);
2697        }
2698    }
2699
2700    void testCdbLocking() {
2701
2702        Iterator JavaDoc readIterator;
2703        Iterator JavaDoc writeIterator;
2704        StoredKeySet set = (StoredKeySet) map.keySet();
2705
2706        // can open two CDB read cursors
2707
readIterator = set.iterator(false);
2708        try {
2709            Iterator JavaDoc readIterator2 = set.iterator(false);
2710            StoredIterator.close(readIterator2);
2711        } finally {
2712            StoredIterator.close(readIterator);
2713        }
2714
2715        // can open two CDB write cursors
2716
writeIterator = set.iterator(true);
2717        try {
2718            Iterator JavaDoc writeIterator2 = set.iterator(true);
2719            StoredIterator.close(writeIterator2);
2720        } finally {
2721            StoredIterator.close(writeIterator);
2722        }
2723
2724        // cannot open CDB write cursor when read cursor is open,
2725
readIterator = set.iterator(false);
2726        try {
2727            writeIterator = set.iterator(true);
2728            fail();
2729            StoredIterator.close(writeIterator);
2730        } catch (IllegalStateException JavaDoc e) {
2731        } finally {
2732            StoredIterator.close(readIterator);
2733        }
2734
2735        if (index == null) {
2736            // cannot put() with read cursor open
2737
readIterator = set.iterator(false);
2738            try {
2739                map.put(makeKey(1), makeVal(1));
2740                fail();
2741            } catch (IllegalStateException JavaDoc e) {
2742            } finally {
2743                StoredIterator.close(readIterator);
2744            }
2745
2746            // cannot append() with write cursor open with RECNO/QUEUE only
2747
writeIterator = set.iterator(true);
2748            try {
2749                if (testStore.isQueueOrRecno()) {
2750                    try {
2751                        map.append(makeVal(1));
2752                        fail();
2753                    } catch (IllegalStateException JavaDoc e) {}
2754                } else {
2755                    map.append(makeVal(1));
2756                }
2757            } finally {
2758                StoredIterator.close(writeIterator);
2759            }
2760        }
2761    }
2762
2763    Object JavaDoc makeVal(int key) {
2764
2765        if (isEntityBinding) {
2766            return makeEntity(key);
2767        } else {
2768            return new Long JavaDoc(key + 100);
2769        }
2770    }
2771
2772    Object JavaDoc 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 JavaDoc makeEntity(int key, int val) {
2782
2783        return new TestEntity(key, val + 100);
2784    }
2785
2786    int intVal(Object JavaDoc val) {
2787
2788        if (isEntityBinding) {
2789            return ((TestEntity) val).value - 100;
2790        } else {
2791            return ((Long JavaDoc) val).intValue() - 100;
2792        }
2793    }
2794
2795    int intKey(Object JavaDoc key) {
2796
2797        return ((Long JavaDoc) key).intValue();
2798    }
2799
2800    Object JavaDoc makeVal(Long JavaDoc key) {
2801
2802        return makeVal(key.intValue());
2803    }
2804
2805    Object JavaDoc makeEntity(int key) {
2806
2807        return makeEntity(key, key);
2808    }
2809
2810    Object JavaDoc makeEntity(Long JavaDoc key) {
2811
2812        return makeEntity(key.intValue());
2813    }
2814
2815    int intIter(Collection JavaDoc coll, Object JavaDoc value) {
2816
2817        if (coll instanceof StoredKeySet) {
2818            return intKey(value);
2819        } else {
2820            if (coll instanceof StoredEntrySet) {
2821                value = ((Map.Entry JavaDoc) value).getValue();
2822            }
2823            return intVal(value);
2824        }
2825    }
2826
2827    Map.Entry JavaDoc mapEntry(Object JavaDoc key, Object JavaDoc val) {
2828
2829        return new MapEntryParameter(key, val);
2830    }
2831
2832    Map.Entry JavaDoc mapEntry(int key) {
2833
2834        return new MapEntryParameter(makeKey(key), makeVal(key));
2835    }
2836
2837    Long JavaDoc makeKey(int key) {
2838
2839        return new Long JavaDoc(key);
2840    }
2841
2842    boolean isSubMap() {
2843
2844        return rangeType != NONE;
2845    }
2846
2847    void checkDupsSize(int expected, Collection JavaDoc 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 JavaDoc coll) {
2872
2873        int count = 0;
2874        Iterator JavaDoc 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