Source Code Cross Referenced for CollectionTest.java in  » JMX » je » com » sleepycat » collections » test » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » JMX » je » com.sleepycat.collections.test 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*-
0002:         * See the file LICENSE for redistribution information.
0003:         *
0004:         * Copyright (c) 2002,2008 Oracle.  All rights reserved.
0005:         *
0006:         * $Id: CollectionTest.java,v 1.53.2.6 2008/01/07 15:14:24 cwl Exp $
0007:         */
0008:
0009:        package com.sleepycat.collections.test;
0010:
0011:        import java.util.ArrayList;
0012:        import java.util.Collection;
0013:        import java.util.Collections;
0014:        import java.util.Enumeration;
0015:        import java.util.HashMap;
0016:        import java.util.Iterator;
0017:        import java.util.List;
0018:        import java.util.ListIterator;
0019:        import java.util.Map;
0020:        import java.util.NoSuchElementException;
0021:        import java.util.Set;
0022:        import java.util.SortedMap;
0023:        import java.util.SortedSet;
0024:
0025:        import junit.framework.Test;
0026:        import junit.framework.TestCase;
0027:        import junit.framework.TestSuite;
0028:
0029:        import com.sleepycat.bind.EntityBinding;
0030:        import com.sleepycat.bind.EntryBinding;
0031:        import com.sleepycat.collections.MapEntryParameter;
0032:        import com.sleepycat.collections.StoredCollection;
0033:        import com.sleepycat.collections.StoredCollections;
0034:        import com.sleepycat.collections.StoredContainer;
0035:        import com.sleepycat.collections.StoredEntrySet;
0036:        import com.sleepycat.collections.StoredIterator;
0037:        import com.sleepycat.collections.StoredKeySet;
0038:        import com.sleepycat.collections.StoredList;
0039:        import com.sleepycat.collections.StoredMap;
0040:        import com.sleepycat.collections.StoredSortedEntrySet;
0041:        import com.sleepycat.collections.StoredSortedKeySet;
0042:        import com.sleepycat.collections.StoredSortedMap;
0043:        import com.sleepycat.collections.StoredSortedValueSet;
0044:        import com.sleepycat.collections.StoredValueSet;
0045:        import com.sleepycat.collections.TransactionRunner;
0046:        import com.sleepycat.collections.TransactionWorker;
0047:        import com.sleepycat.compat.DbCompat;
0048:        import com.sleepycat.je.Database;
0049:        import com.sleepycat.je.DatabaseException;
0050:        import com.sleepycat.je.Environment;
0051:        import com.sleepycat.util.ExceptionUnwrapper;
0052:
0053:        /**
0054:         * @author Mark Hayes
0055:         */
0056:        public class CollectionTest extends TestCase {
0057:
0058:            private static final int NONE = 0;
0059:            private static final int SUB = 1;
0060:            private static final int HEAD = 2;
0061:            private static final int TAIL = 3;
0062:
0063:            /*
0064:             * For long tests we permute testStoredIterator to test both StoredIterator
0065:             * and BlockIterator.  When testing BlockIterator, we permute the maxKey
0066:             * over the array values below.  BlockIterator's block size is 10.  So we
0067:             * test below the block size (6), at the block size (10), and above it (14
0068:             * and 22).
0069:             */
0070:            private static final int DEFAULT_MAX_KEY = 6;
0071:            private static final int[] MAX_KEYS = { 6, 10, 14, 22 };
0072:
0073:            private boolean testStoredIterator;
0074:            private int maxKey; /* Must be a multiple of 2. */
0075:            private int beginKey = 1;
0076:            private int endKey;
0077:
0078:            private Environment env;
0079:            private Database store;
0080:            private Database index;
0081:            private boolean isEntityBinding;
0082:            private boolean isAutoCommit;
0083:            private TestStore testStore;
0084:            private String testName;
0085:            private EntryBinding keyBinding;
0086:            private EntryBinding valueBinding;
0087:            private EntityBinding entityBinding;
0088:            private TransactionRunner readRunner;
0089:            private TransactionRunner writeRunner;
0090:            private TransactionRunner writeIterRunner;
0091:            private TestEnv testEnv;
0092:
0093:            private StoredMap map;
0094:            private StoredMap imap; // insertable map (primary store for indexed map)
0095:            private StoredSortedMap smap; // sorted map (null or equal to map)
0096:            private StoredMap saveMap;
0097:            private StoredSortedMap saveSMap;
0098:            private int rangeType;
0099:            private StoredList list;
0100:            private StoredList ilist; // insertable list (primary store for index list)
0101:            private StoredList saveList;
0102:            private StoredKeySet keySet;
0103:            private StoredValueSet valueSet;
0104:
0105:            /**
0106:             * Runs a command line collection test.
0107:             * @see #usage
0108:             */
0109:            public static void main(String[] args) throws Exception {
0110:
0111:                if (args.length == 1
0112:                        && (args[0].equals("-h") || args[0].equals("-help"))) {
0113:                    usage();
0114:                } else {
0115:                    junit.framework.TestResult tr = junit.textui.TestRunner
0116:                            .run(suite(args));
0117:                    if (tr.errorCount() > 0 || tr.failureCount() > 0) {
0118:                        System.exit(1);
0119:                    } else {
0120:                        System.exit(0);
0121:                    }
0122:                }
0123:            }
0124:
0125:            private static void usage() {
0126:
0127:                System.out
0128:                        .println("Usage: java com.sleepycat.collections.test.CollectionTest\n"
0129:                                + "              -h | -help\n"
0130:                                + "              [testName]...\n"
0131:                                + "  where testName has the format:\n"
0132:                                + "    <env>-<store>-{entity|value}\n"
0133:                                + "  <env> is:\n"
0134:                                + "    bdb | cdb | txn\n"
0135:                                + "  <store> is:\n"
0136:                                + "    btree-uniq | btree-dup | btree-dupsort | btree-recnum |\n"
0137:                                + "    hash-uniq | hash-dup | hash-dupsort |\n"
0138:                                + "    queue | recno | recno-renum\n"
0139:                                + "  For example:  bdb-btree-uniq-entity\n"
0140:                                + "  If no arguments are given then all tests are run.");
0141:                System.exit(2);
0142:            }
0143:
0144:            public static Test suite() throws Exception {
0145:
0146:                return suite(null);
0147:            }
0148:
0149:            static Test suite(String[] args) throws Exception {
0150:
0151:                if ("true".equals(System.getProperty("longtest"))) {
0152:                    TestSuite suite = new TestSuite();
0153:
0154:                    /* StoredIterator tests. */
0155:                    permuteTests(args, suite, true, DEFAULT_MAX_KEY);
0156:
0157:                    /* BlockIterator tests with different maxKey values. */
0158:                    for (int i = 0; i < MAX_KEYS.length; i += 1) {
0159:                        permuteTests(args, suite, false, MAX_KEYS[i]);
0160:                    }
0161:
0162:                    return suite;
0163:                } else {
0164:                    return baseSuite(args);
0165:                }
0166:            }
0167:
0168:            private static void permuteTests(String[] args, TestSuite suite,
0169:                    boolean storedIter, int maxKey) throws Exception {
0170:
0171:                TestSuite baseTests = baseSuite(args);
0172:                Enumeration e = baseTests.tests();
0173:                while (e.hasMoreElements()) {
0174:                    CollectionTest t = (CollectionTest) e.nextElement();
0175:                    t.setParams(storedIter, maxKey);
0176:                    suite.addTest(t);
0177:                }
0178:            }
0179:
0180:            private static TestSuite baseSuite(String[] args) throws Exception {
0181:
0182:                TestSuite suite = new TestSuite();
0183:                for (int i = 0; i < TestEnv.ALL.length; i += 1) {
0184:                    for (int j = 0; j < TestStore.ALL.length; j += 1) {
0185:                        for (int k = 0; k < 2; k += 1) {
0186:                            boolean entityBinding = (k != 0);
0187:
0188:                            addTest(args, suite, new CollectionTest(
0189:                                    TestEnv.ALL[i], TestStore.ALL[j],
0190:                                    entityBinding, false));
0191:
0192:                            if (TestEnv.ALL[i].isTxnMode()) {
0193:                                addTest(args, suite, new CollectionTest(
0194:                                        TestEnv.ALL[i], TestStore.ALL[j],
0195:                                        entityBinding, true));
0196:                            }
0197:                        }
0198:                    }
0199:                }
0200:                return suite;
0201:            }
0202:
0203:            private static void addTest(String[] args, TestSuite suite,
0204:                    CollectionTest test) {
0205:
0206:                if (args == null || args.length == 0) {
0207:                    suite.addTest(test);
0208:                } else {
0209:                    for (int t = 0; t < args.length; t += 1) {
0210:                        if (args[t].equals(test.testName)) {
0211:                            suite.addTest(test);
0212:                            break;
0213:                        }
0214:                    }
0215:                }
0216:            }
0217:
0218:            public CollectionTest(TestEnv testEnv, TestStore testStore,
0219:                    boolean isEntityBinding, boolean isAutoCommit) {
0220:
0221:                super (null);
0222:
0223:                this .testEnv = testEnv;
0224:                this .testStore = testStore;
0225:                this .isEntityBinding = isEntityBinding;
0226:                this .isAutoCommit = isAutoCommit;
0227:
0228:                keyBinding = testStore.getKeyBinding();
0229:                valueBinding = testStore.getValueBinding();
0230:                entityBinding = testStore.getEntityBinding();
0231:
0232:                setParams(false, DEFAULT_MAX_KEY);
0233:            }
0234:
0235:            private void setParams(boolean storedIter, int maxKey) {
0236:
0237:                this .testStoredIterator = storedIter;
0238:                this .maxKey = maxKey;
0239:                this .endKey = maxKey;
0240:
0241:                testName = testEnv.getName()
0242:                        + '-'
0243:                        + testStore.getName()
0244:                        + (isEntityBinding ? "-entity" : "-value")
0245:                        + (isAutoCommit ? "-autoCommit" : "")
0246:                        + (testStoredIterator ? "-storedIter" : "")
0247:                        + ((maxKey != DEFAULT_MAX_KEY) ? ("-maxKey-" + maxKey)
0248:                                : "");
0249:            }
0250:
0251:            public void tearDown() throws Exception {
0252:
0253:                setName(testName);
0254:            }
0255:
0256:            public void runTest() throws Exception {
0257:
0258:                DbTestUtil.printTestName(DbTestUtil.qualifiedTestName(this ));
0259:                try {
0260:                    env = testEnv.open(testName);
0261:
0262:                    // For testing auto-commit, use a normal (transactional) runner for
0263:                    // all reading and for writing via an iterator, and a do-nothing
0264:                    // runner for writing via collections; if auto-commit is tested,
0265:                    // the per-collection auto-commit property will be set elsewhere.
0266:                    //
0267:                    TransactionRunner normalRunner = newTransactionRunner(env);
0268:                    normalRunner
0269:                            .setAllowNestedTransactions(DbCompat.NESTED_TRANSACTIONS);
0270:                    TransactionRunner nullRunner = new NullTransactionRunner(
0271:                            env);
0272:                    readRunner = nullRunner;
0273:                    if (isAutoCommit) {
0274:                        writeRunner = nullRunner;
0275:                        writeIterRunner = testStoredIterator ? normalRunner
0276:                                : nullRunner;
0277:                    } else {
0278:                        writeRunner = normalRunner;
0279:                        writeIterRunner = normalRunner;
0280:                    }
0281:
0282:                    store = testStore.open(env, "unindexed.db");
0283:                    testUnindexed();
0284:                    store.close();
0285:                    store = null;
0286:
0287:                    TestStore indexOf = testStore.getIndexOf();
0288:                    if (indexOf != null) {
0289:                        store = indexOf.open(env, "indexed.db");
0290:                        index = testStore.openIndex(store, "index.db");
0291:                        testIndexed();
0292:                        index.close();
0293:                        index = null;
0294:                        store.close();
0295:                        store = null;
0296:                    }
0297:                    env.close();
0298:                    env = null;
0299:                } catch (Exception e) {
0300:                    throw ExceptionUnwrapper.unwrap(e);
0301:                } finally {
0302:                    if (index != null) {
0303:                        try {
0304:                            index.close();
0305:                        } catch (Exception e) {
0306:                        }
0307:                    }
0308:                    if (store != null) {
0309:                        try {
0310:                            store.close();
0311:                        } catch (Exception e) {
0312:                        }
0313:                    }
0314:                    if (env != null) {
0315:                        try {
0316:                            env.close();
0317:                        } catch (Exception e) {
0318:                        }
0319:                    }
0320:                    /* Ensure that GC can cleanup. */
0321:                    index = null;
0322:                    store = null;
0323:                    env = null;
0324:                    readRunner = null;
0325:                    writeRunner = null;
0326:                    writeIterRunner = null;
0327:                    map = null;
0328:                    imap = null;
0329:                    smap = null;
0330:                    saveMap = null;
0331:                    saveSMap = null;
0332:                    list = null;
0333:                    ilist = null;
0334:                    saveList = null;
0335:                    keySet = null;
0336:                    valueSet = null;
0337:                    testEnv = null;
0338:                    testStore = null;
0339:                }
0340:            }
0341:
0342:            /**
0343:             * Is overridden in XACollectionTest.
0344:             */
0345:            protected TransactionRunner newTransactionRunner(Environment env)
0346:                    throws DatabaseException {
0347:
0348:                return new TransactionRunner(env);
0349:            }
0350:
0351:            void testCreation(StoredContainer cont, int expectSize)
0352:                    throws Exception {
0353:
0354:                assertEquals(index != null, cont.isSecondary());
0355:                assertEquals(testStore.isOrdered(), cont.isOrdered());
0356:                assertEquals(testStore.areKeysRenumbered(), cont
0357:                        .areKeysRenumbered());
0358:                assertEquals(testStore.areDuplicatesAllowed(), cont
0359:                        .areDuplicatesAllowed());
0360:                assertEquals(testEnv.isTxnMode(), cont.isTransactional());
0361:                assertEquals(expectSize, cont.size());
0362:            }
0363:
0364:            void testMapCreation(Map map) throws Exception {
0365:
0366:                assertTrue(map.values() instanceof  Set);
0367:                assertEquals(testStore.isOrdered(),
0368:                        map.keySet() instanceof  SortedSet);
0369:                assertEquals(testStore.isOrdered(),
0370:                        map.entrySet() instanceof  SortedSet);
0371:                assertEquals(testStore.isOrdered() && isEntityBinding, map
0372:                        .values() instanceof  SortedSet);
0373:            }
0374:
0375:            void testUnindexed() throws Exception {
0376:
0377:                // create primary map
0378:                if (testStore.isOrdered()) {
0379:                    if (isEntityBinding) {
0380:                        smap = new StoredSortedMap(store, keyBinding,
0381:                                entityBinding, testStore.getKeyAssigner());
0382:                        valueSet = new StoredSortedValueSet(store,
0383:                                entityBinding, true);
0384:                    } else {
0385:                        smap = new StoredSortedMap(store, keyBinding,
0386:                                valueBinding, testStore.getKeyAssigner());
0387:                        // sorted value set is not possible since key cannot be derived
0388:                        // for performing subSet, etc.
0389:                    }
0390:                    keySet = new StoredSortedKeySet(store, keyBinding, true);
0391:                    map = smap;
0392:                } else {
0393:                    if (isEntityBinding) {
0394:                        map = new StoredMap(store, keyBinding, entityBinding,
0395:                                testStore.getKeyAssigner());
0396:                        valueSet = new StoredValueSet(store, entityBinding,
0397:                                true);
0398:                    } else {
0399:                        map = new StoredMap(store, keyBinding, valueBinding,
0400:                                testStore.getKeyAssigner());
0401:                        valueSet = new StoredValueSet(store, valueBinding, true);
0402:                    }
0403:                    smap = null;
0404:                    keySet = new StoredKeySet(store, keyBinding, true);
0405:                }
0406:                imap = map;
0407:
0408:                // create primary list
0409:                if (testStore.hasRecNumAccess()) {
0410:                    if (isEntityBinding) {
0411:                        ilist = new StoredList(store, entityBinding, testStore
0412:                                .getKeyAssigner());
0413:                    } else {
0414:                        ilist = new StoredList(store, valueBinding, testStore
0415:                                .getKeyAssigner());
0416:                    }
0417:                    list = ilist;
0418:                } else {
0419:                    try {
0420:                        if (isEntityBinding) {
0421:                            ilist = new StoredList(store, entityBinding,
0422:                                    testStore.getKeyAssigner());
0423:                        } else {
0424:                            ilist = new StoredList(store, valueBinding,
0425:                                    testStore.getKeyAssigner());
0426:                        }
0427:                        fail();
0428:                    } catch (IllegalArgumentException expected) {
0429:                    }
0430:                }
0431:
0432:                testCreation(map, 0);
0433:                if (list != null) {
0434:                    testCreation(list, 0);
0435:                    assertNotNull(smap);
0436:                }
0437:                testMapCreation(map);
0438:                addAll();
0439:                testAll();
0440:            }
0441:
0442:            void testIndexed() throws Exception {
0443:
0444:                // create primary map
0445:                if (isEntityBinding) {
0446:                    map = new StoredMap(store, keyBinding, entityBinding,
0447:                            testStore.getKeyAssigner());
0448:                } else {
0449:                    map = new StoredMap(store, keyBinding, valueBinding,
0450:                            testStore.getKeyAssigner());
0451:                }
0452:                imap = map;
0453:                smap = null;
0454:                // create primary list
0455:                if (testStore.hasRecNumAccess()) {
0456:                    if (isEntityBinding) {
0457:                        list = new StoredList(store, entityBinding, testStore
0458:                                .getKeyAssigner());
0459:                    } else {
0460:                        list = new StoredList(store, valueBinding, testStore
0461:                                .getKeyAssigner());
0462:                    }
0463:                    ilist = list;
0464:                }
0465:
0466:                addAll();
0467:                readAll();
0468:
0469:                // create indexed map (keySet/valueSet)
0470:                if (testStore.isOrdered()) {
0471:                    if (isEntityBinding) {
0472:                        map = smap = new StoredSortedMap(index, keyBinding,
0473:                                entityBinding, true);
0474:                        valueSet = new StoredSortedValueSet(index,
0475:                                entityBinding, true);
0476:                    } else {
0477:                        map = smap = new StoredSortedMap(index, keyBinding,
0478:                                valueBinding, true);
0479:                        // sorted value set is not possible since key cannot be derived
0480:                        // for performing subSet, etc.
0481:                    }
0482:                    keySet = new StoredSortedKeySet(index, keyBinding, true);
0483:                } else {
0484:                    if (isEntityBinding) {
0485:                        map = new StoredMap(index, keyBinding, entityBinding,
0486:                                true);
0487:                        valueSet = new StoredValueSet(index, entityBinding,
0488:                                true);
0489:                    } else {
0490:                        map = new StoredMap(index, keyBinding, valueBinding,
0491:                                true);
0492:                        valueSet = new StoredValueSet(index, valueBinding, true);
0493:                    }
0494:                    smap = null;
0495:                    keySet = new StoredKeySet(index, keyBinding, true);
0496:                }
0497:
0498:                // create indexed list
0499:                if (testStore.hasRecNumAccess()) {
0500:                    if (isEntityBinding) {
0501:                        list = new StoredList(index, entityBinding, true);
0502:                    } else {
0503:                        list = new StoredList(index, valueBinding, true);
0504:                    }
0505:                } else {
0506:                    try {
0507:                        if (isEntityBinding) {
0508:                            list = new StoredList(index, entityBinding, true);
0509:                        } else {
0510:                            list = new StoredList(index, valueBinding, true);
0511:                        }
0512:                        fail();
0513:                    } catch (IllegalArgumentException expected) {
0514:                    }
0515:                }
0516:
0517:                testCreation(map, maxKey);
0518:                testCreation((StoredContainer) map.values(), maxKey);
0519:                testCreation((StoredContainer) map.keySet(), maxKey);
0520:                testCreation((StoredContainer) map.entrySet(), maxKey);
0521:                if (list != null) {
0522:                    testCreation(list, maxKey);
0523:                    assertNotNull(smap);
0524:                }
0525:                testMapCreation(map);
0526:                testAll();
0527:            }
0528:
0529:            void testAll() throws Exception {
0530:
0531:                checkKeySetAndValueSet();
0532:                readAll();
0533:                updateAll();
0534:                readAll();
0535:                if (!map.areKeysRenumbered()) {
0536:                    removeOdd();
0537:                    readEven();
0538:                    addOdd();
0539:                    readAll();
0540:                    removeOddIter();
0541:                    readEven();
0542:                    if (imap.areDuplicatesAllowed()) {
0543:                        addOddDup();
0544:                    } else {
0545:                        addOdd();
0546:                    }
0547:                    readAll();
0548:                    removeOddEntry();
0549:                    readEven();
0550:                    addOdd();
0551:                    readAll();
0552:                    if (isEntityBinding) {
0553:                        removeOddEntity();
0554:                        readEven();
0555:                        addOddEntity();
0556:                        readAll();
0557:                    }
0558:                    bulkOperations();
0559:                }
0560:                if (isListAddAllowed()) {
0561:                    removeOddList();
0562:                    readEvenList();
0563:                    addOddList();
0564:                    readAll();
0565:                    if (!isEntityBinding) {
0566:                        removeOddListValue();
0567:                        readEvenList();
0568:                        addOddList();
0569:                        readAll();
0570:                    }
0571:                }
0572:                if (list != null) {
0573:                    bulkListOperations();
0574:                } else {
0575:                    listOperationsNotAllowed();
0576:                }
0577:                if (smap != null) {
0578:                    readWriteRange(SUB, 1, 1);
0579:                    readWriteRange(HEAD, 1, 1);
0580:                    readWriteRange(SUB, 1, maxKey);
0581:                    readWriteRange(HEAD, 1, maxKey);
0582:                    readWriteRange(TAIL, 1, maxKey);
0583:                    readWriteRange(SUB, 1, 3);
0584:                    readWriteRange(HEAD, 1, 3);
0585:                    readWriteRange(SUB, 2, 2);
0586:                    readWriteRange(SUB, 2, maxKey);
0587:                    readWriteRange(TAIL, 2, maxKey);
0588:                    readWriteRange(SUB, maxKey, maxKey);
0589:                    readWriteRange(TAIL, maxKey, maxKey);
0590:                    readWriteRange(SUB, maxKey + 1, maxKey + 1);
0591:                    readWriteRange(TAIL, maxKey + 1, maxKey + 1);
0592:                    readWriteRange(SUB, 0, 0);
0593:                    readWriteRange(HEAD, 0, 0);
0594:                }
0595:                updateAll();
0596:                readAll();
0597:                if (map.areDuplicatesAllowed()) {
0598:                    readWriteDuplicates();
0599:                    readAll();
0600:                } else {
0601:                    duplicatesNotAllowed();
0602:                    readAll();
0603:                }
0604:                if (testEnv.isCdbMode()) {
0605:                    testCdbLocking();
0606:                }
0607:                removeAll();
0608:                if (isListAddAllowed()) {
0609:                    testIterAddList();
0610:                    clearAll();
0611:                }
0612:                if (imap.areDuplicatesAllowed()) {
0613:                    testIterAddDuplicates();
0614:                    clearAll();
0615:                }
0616:                if (isListAddAllowed()) {
0617:                    addAllList();
0618:                    readAll();
0619:                    removeAllList();
0620:                }
0621:                appendAll();
0622:            }
0623:
0624:            void checkKeySetAndValueSet() {
0625:
0626:                // use bulk operations to check that explicitly constructed
0627:                // keySet/valueSet are equivalent
0628:                assertTrue(imap.keySet().equals(keySet));
0629:                if (valueSet != null) {
0630:                    assertTrue(imap.values().equals(valueSet));
0631:                }
0632:            }
0633:
0634:            Iterator iterator(Collection storedCollection) {
0635:
0636:                if (testStoredIterator) {
0637:                    return ((StoredCollection) storedCollection)
0638:                            .storedIterator();
0639:                } else {
0640:                    return storedCollection.iterator();
0641:                }
0642:            }
0643:
0644:            void addAll() throws Exception {
0645:
0646:                writeRunner.run(new TransactionWorker() {
0647:                    public void doWork() throws Exception {
0648:                        assertTrue(imap.isEmpty());
0649:                        Iterator iter = iterator(imap.entrySet());
0650:                        try {
0651:                            assertTrue(!iter.hasNext());
0652:                        } finally {
0653:                            StoredIterator.close(iter);
0654:                        }
0655:                        assertEquals(0, imap.keySet().toArray().length);
0656:                        assertEquals(0,
0657:                                imap.keySet().toArray(new Object[0]).length);
0658:                        assertEquals(0, imap.entrySet().toArray().length);
0659:                        assertEquals(0,
0660:                                imap.entrySet().toArray(new Object[0]).length);
0661:                        assertEquals(0, imap.values().toArray().length);
0662:                        assertEquals(0,
0663:                                imap.values().toArray(new Object[0]).length);
0664:
0665:                        for (int i = beginKey; i <= endKey; i += 1) {
0666:                            Long key = makeKey(i);
0667:                            Object val = makeVal(i);
0668:                            assertNull(imap.get(key));
0669:                            assertTrue(!imap.keySet().contains(key));
0670:                            assertTrue(!imap.values().contains(val));
0671:                            assertNull(imap.put(key, val));
0672:                            assertEquals(val, imap.get(key));
0673:                            assertTrue(imap.keySet().contains(key));
0674:                            assertTrue(imap.values().contains(val));
0675:                            assertTrue(imap.duplicates(key).contains(val));
0676:                            if (!imap.areDuplicatesAllowed()) {
0677:                                assertEquals(val, imap.put(key, val));
0678:                            }
0679:                            checkDupsSize(1, imap.duplicates(key));
0680:                        }
0681:                        assertTrue(!imap.isEmpty());
0682:                    }
0683:                });
0684:            }
0685:
0686:            void appendAll() throws Exception {
0687:
0688:                writeRunner.run(new TransactionWorker() {
0689:                    public void doWork() throws Exception {
0690:                        assertTrue(imap.isEmpty());
0691:
0692:                        TestKeyAssigner keyAssigner = testStore
0693:                                .getKeyAssigner();
0694:                        if (keyAssigner != null) {
0695:                            keyAssigner.reset();
0696:                        }
0697:
0698:                        for (int i = beginKey; i <= endKey; i += 1) {
0699:                            boolean useList = (i & 1) == 0;
0700:                            Long key = makeKey(i);
0701:                            Object val = makeVal(i);
0702:                            assertNull(imap.get(key));
0703:                            if (keyAssigner != null) {
0704:                                if (useList && ilist != null) {
0705:                                    assertEquals(i - 1, ilist.append(val));
0706:                                } else {
0707:                                    assertEquals(key, imap.append(val));
0708:                                }
0709:                                assertEquals(val, imap.get(key));
0710:                            } else {
0711:                                Long recnoKey;
0712:                                if (useList && ilist != null) {
0713:                                    recnoKey = new Long(ilist.append(val) + 1);
0714:                                } else {
0715:                                    recnoKey = (Long) imap.append(val);
0716:                                }
0717:                                assertNotNull(recnoKey);
0718:                                Object recnoVal;
0719:                                if (isEntityBinding) {
0720:                                    recnoVal = makeEntity(recnoKey.intValue(),
0721:                                            i);
0722:                                } else {
0723:                                    recnoVal = val;
0724:                                }
0725:                                assertEquals(recnoVal, imap.get(recnoKey));
0726:                            }
0727:                        }
0728:                    }
0729:                });
0730:            }
0731:
0732:            void updateAll() throws Exception {
0733:
0734:                writeRunner.run(new TransactionWorker() {
0735:                    public void doWork() throws Exception {
0736:                        for (int i = beginKey; i <= endKey; i += 1) {
0737:                            Long key = makeKey(i);
0738:                            Object val = makeVal(i);
0739:                            if (!imap.areDuplicatesAllowed()) {
0740:                                assertEquals(val, imap.put(key, val));
0741:                            }
0742:                            if (isEntityBinding) {
0743:                                assertTrue(!imap.values().add(val));
0744:                            }
0745:                            checkDupsSize(1, imap.duplicates(key));
0746:                            if (ilist != null) {
0747:                                int idx = i - 1;
0748:                                assertEquals(val, ilist.set(idx, val));
0749:                            }
0750:                        }
0751:                        updateIter(map.entrySet());
0752:                        updateIter(map.values());
0753:                        if (beginKey <= endKey) {
0754:                            ListIterator iter = (ListIterator) iterator(map
0755:                                    .keySet());
0756:                            try {
0757:                                assertNotNull(iter.next());
0758:                                iter.set(makeKey(beginKey));
0759:                                fail();
0760:                            } catch (UnsupportedOperationException e) {
0761:                            } finally {
0762:                                StoredIterator.close(iter);
0763:                            }
0764:                        }
0765:                        if (list != null) {
0766:                            updateIter(list);
0767:                        }
0768:                    }
0769:                });
0770:            }
0771:
0772:            void updateIter(final Collection coll) throws Exception {
0773:
0774:                writeIterRunner.run(new TransactionWorker() {
0775:                    public void doWork() throws Exception {
0776:                        ListIterator iter = (ListIterator) iterator(coll);
0777:                        try {
0778:                            for (int i = beginKey; i <= endKey; i += 1) {
0779:                                assertTrue(iter.hasNext());
0780:                                Object obj = iter.next();
0781:                                assertEquals(i, intIter(coll, obj));
0782:                                if (index != null) {
0783:                                    try {
0784:                                        setValuePlusOne(iter, obj);
0785:                                        fail();
0786:                                    } catch (UnsupportedOperationException e) {
0787:                                    }
0788:                                } else if (((StoredCollection) coll)
0789:                                        .areDuplicatesOrdered()) {
0790:                                    try {
0791:                                        setValuePlusOne(iter, obj);
0792:                                        fail();
0793:                                    } catch (RuntimeException e) {
0794:                                        Exception e2 = ExceptionUnwrapper
0795:                                                .unwrap(e);
0796:                                        assertTrue(
0797:                                                e2.getClass().getName(),
0798:                                                e2 instanceof  IllegalArgumentException
0799:                                                        || e2 instanceof  DatabaseException);
0800:                                    }
0801:                                } else {
0802:                                    setValuePlusOne(iter, obj);
0803:                                    /* Ensure iterator position is correct. */
0804:                                    assertTrue(iter.hasPrevious());
0805:                                    obj = iter.previous();
0806:                                    assertEquals(i, intIter(coll, obj));
0807:                                    assertTrue(iter.hasNext());
0808:                                    obj = iter.next();
0809:                                    assertEquals(i, intIter(coll, obj));
0810:                                }
0811:                            }
0812:                            assertTrue(!iter.hasNext());
0813:                        } finally {
0814:                            StoredIterator.close(iter);
0815:                        }
0816:                    }
0817:                });
0818:            }
0819:
0820:            void setValuePlusOne(ListIterator iter, Object obj) {
0821:
0822:                if (obj instanceof  Map.Entry) {
0823:                    Map.Entry entry = (Map.Entry) obj;
0824:                    Long key = (Long) entry.getKey();
0825:                    Object oldVal = entry.getValue();
0826:                    Object val = makeVal(key.intValue() + 1);
0827:                    if (isEntityBinding) {
0828:                        try {
0829:                            // must fail on attempt to change the key via an entity
0830:                            entry.setValue(val);
0831:                            fail();
0832:                        } catch (IllegalArgumentException e) {
0833:                        }
0834:                        val = makeEntity(key.intValue(), key.intValue() + 1);
0835:                    }
0836:                    entry.setValue(val);
0837:                    assertEquals(val, entry.getValue());
0838:                    assertEquals(val, map.get(key));
0839:                    assertTrue(map.duplicates(key).contains(val));
0840:                    checkDupsSize(1, map.duplicates(key));
0841:                    entry.setValue(oldVal);
0842:                    assertEquals(oldVal, entry.getValue());
0843:                    assertEquals(oldVal, map.get(key));
0844:                    assertTrue(map.duplicates(key).contains(oldVal));
0845:                    checkDupsSize(1, map.duplicates(key));
0846:                } else {
0847:                    Object oldVal = obj;
0848:                    Long key = makeKey(intVal(obj));
0849:                    Object val = makeVal(key.intValue() + 1);
0850:                    if (isEntityBinding) {
0851:                        try {
0852:                            // must fail on attempt to change the key via an entity
0853:                            iter.set(val);
0854:                            fail();
0855:                        } catch (IllegalArgumentException e) {
0856:                        }
0857:                        val = makeEntity(key.intValue(), key.intValue() + 1);
0858:                    }
0859:                    iter.set(val);
0860:                    assertEquals(val, map.get(key));
0861:                    assertTrue(map.duplicates(key).contains(val));
0862:                    checkDupsSize(1, map.duplicates(key));
0863:                    iter.set(oldVal);
0864:                    assertEquals(oldVal, map.get(key));
0865:                    assertTrue(map.duplicates(key).contains(oldVal));
0866:                    checkDupsSize(1, map.duplicates(key));
0867:                }
0868:            }
0869:
0870:            void removeAll() throws Exception {
0871:
0872:                writeIterRunner.run(new TransactionWorker() {
0873:                    public void doWork() throws Exception {
0874:                        assertTrue(!map.isEmpty());
0875:                        ListIterator iter = null;
0876:                        try {
0877:                            if (list != null) {
0878:                                iter = (ListIterator) iterator(list);
0879:                            } else {
0880:                                iter = (ListIterator) iterator(map.values());
0881:                            }
0882:                            iteratorSetAndRemoveNotAllowed(iter);
0883:
0884:                            Object val = iter.next();
0885:                            assertNotNull(val);
0886:                            iter.remove();
0887:                            iteratorSetAndRemoveNotAllowed(iter);
0888:
0889:                            if (index == null) {
0890:                                val = iter.next();
0891:                                assertNotNull(val);
0892:                                iter.set(val);
0893:
0894:                                if (map.areDuplicatesAllowed()) {
0895:                                    iter.add(makeVal(intVal(val),
0896:                                            intVal(val) + 1));
0897:                                    iteratorSetAndRemoveNotAllowed(iter);
0898:                                }
0899:                            }
0900:                        } finally {
0901:                            StoredIterator.close(iter);
0902:                        }
0903:                        map.clear();
0904:                        assertTrue(map.isEmpty());
0905:                        assertTrue(map.entrySet().isEmpty());
0906:                        assertTrue(map.keySet().isEmpty());
0907:                        assertTrue(map.values().isEmpty());
0908:                        for (int i = beginKey; i <= endKey; i += 1) {
0909:                            Long key = makeKey(i);
0910:                            Object val = makeVal(i);
0911:                            assertNull(map.get(key));
0912:                            assertTrue(!map.duplicates(key).contains(val));
0913:                            checkDupsSize(0, map.duplicates(key));
0914:                        }
0915:                    }
0916:                });
0917:            }
0918:
0919:            void clearAll() throws Exception {
0920:
0921:                writeRunner.run(new TransactionWorker() {
0922:                    public void doWork() throws Exception {
0923:                        map.clear();
0924:                        assertTrue(map.isEmpty());
0925:                    }
0926:                });
0927:            }
0928:
0929:            /**
0930:             * Tests that removing while iterating works properly, especially when
0931:             * removing everything in the key range or everything from some point to
0932:             * the end of the range. [#15858]
0933:             */
0934:            void removeIter() throws Exception {
0935:
0936:                writeIterRunner.run(new TransactionWorker() {
0937:                    public void doWork() throws Exception {
0938:                        ListIterator iter;
0939:
0940:                        /* Save contents. */
0941:                        HashMap savedMap = new HashMap(map);
0942:                        assertTrue(map.equals(savedMap));
0943:
0944:                        /* Remove all moving forward. */
0945:                        iter = (ListIterator) iterator(map.keySet());
0946:                        try {
0947:                            while (iter.hasNext()) {
0948:                                assertNotNull(iter.next());
0949:                                iter.remove();
0950:                            }
0951:                            assertTrue(!iter.hasNext());
0952:                            assertTrue(!iter.hasPrevious());
0953:                            assertTrue(map.isEmpty());
0954:                        } finally {
0955:                            StoredIterator.close(iter);
0956:                        }
0957:
0958:                        /* Restore contents. */
0959:                        imap.putAll(savedMap);
0960:                        assertTrue(map.equals(savedMap));
0961:
0962:                        /* Remove all moving backward. */
0963:                        iter = (ListIterator) iterator(map.keySet());
0964:                        try {
0965:                            while (iter.hasNext()) {
0966:                                assertNotNull(iter.next());
0967:                            }
0968:                            while (iter.hasPrevious()) {
0969:                                assertNotNull(iter.previous());
0970:                                iter.remove();
0971:                            }
0972:                            assertTrue(!iter.hasNext());
0973:                            assertTrue(!iter.hasPrevious());
0974:                            assertTrue(map.isEmpty());
0975:                        } finally {
0976:                            StoredIterator.close(iter);
0977:                        }
0978:
0979:                        /* Restore contents. */
0980:                        imap.putAll(savedMap);
0981:                        assertTrue(map.equals(savedMap));
0982:
0983:                        int first = Math.max(1, beginKey);
0984:                        int last = Math.min(maxKey, endKey);
0985:
0986:                        /* Skip N forward, remove all from that point forward. */
0987:                        for (int readTo = first + 1; readTo <= last; readTo += 1) {
0988:                            iter = (ListIterator) iterator(map.keySet());
0989:                            try {
0990:                                for (int i = first; i < readTo; i += 1) {
0991:                                    assertTrue(iter.hasNext());
0992:                                    assertNotNull(iter.next());
0993:                                }
0994:                                for (int i = readTo; i <= last; i += 1) {
0995:                                    assertTrue(iter.hasNext());
0996:                                    assertNotNull(iter.next());
0997:                                    iter.remove();
0998:                                }
0999:                                assertTrue(!iter.hasNext());
1000:                                assertTrue(iter.hasPrevious());
1001:                                assertEquals(readTo - first, map.size());
1002:                            } finally {
1003:                                StoredIterator.close(iter);
1004:                            }
1005:
1006:                            /* Restore contents. */
1007:                            imap.putAll(savedMap);
1008:                            assertTrue(map.equals(savedMap));
1009:                        }
1010:
1011:                        /* Skip N backward, remove all from that point backward. */
1012:                        for (int readTo = last - 1; readTo >= first; readTo -= 1) {
1013:                            iter = (ListIterator) iterator(map.keySet());
1014:                            try {
1015:                                while (iter.hasNext()) {
1016:                                    assertNotNull(iter.next());
1017:                                }
1018:                                for (int i = last; i > readTo; i -= 1) {
1019:                                    assertTrue(iter.hasPrevious());
1020:                                    assertNotNull(iter.previous());
1021:                                }
1022:                                for (int i = readTo; i >= first; i -= 1) {
1023:                                    assertTrue(iter.hasPrevious());
1024:                                    assertNotNull(iter.previous());
1025:                                    iter.remove();
1026:                                }
1027:                                assertTrue(!iter.hasPrevious());
1028:                                assertTrue(iter.hasNext());
1029:                                assertEquals(last - readTo, map.size());
1030:                            } finally {
1031:                                StoredIterator.close(iter);
1032:                            }
1033:
1034:                            /* Restore contents. */
1035:                            imap.putAll(savedMap);
1036:                            assertTrue(map.equals(savedMap));
1037:                        }
1038:                    }
1039:                });
1040:            }
1041:
1042:            void iteratorSetAndRemoveNotAllowed(ListIterator i) {
1043:
1044:                try {
1045:                    i.remove();
1046:                    fail();
1047:                } catch (IllegalStateException e) {
1048:                }
1049:
1050:                if (index == null) {
1051:                    try {
1052:                        Object val = makeVal(1);
1053:                        i.set(val);
1054:                        fail();
1055:                    } catch (IllegalStateException e) {
1056:                    }
1057:                }
1058:            }
1059:
1060:            void removeOdd() throws Exception {
1061:
1062:                writeRunner.run(new TransactionWorker() {
1063:                    public void doWork() throws Exception {
1064:                        boolean toggle = false;
1065:                        for (int i = beginKey; i <= endKey; i += 2) {
1066:                            toggle = !toggle;
1067:                            Long key = makeKey(i);
1068:                            Object val = makeVal(i);
1069:                            if (toggle) {
1070:                                assertTrue(map.keySet().contains(key));
1071:                                assertTrue(map.keySet().remove(key));
1072:                                assertTrue(!map.keySet().contains(key));
1073:                            } else {
1074:                                assertTrue(map.containsValue(val));
1075:                                Object oldVal = map.remove(key);
1076:                                assertEquals(oldVal, val);
1077:                                assertTrue(!map.containsKey(key));
1078:                                assertTrue(!map.containsValue(val));
1079:                            }
1080:                            assertNull(map.get(key));
1081:                            assertTrue(!map.duplicates(key).contains(val));
1082:                            checkDupsSize(0, map.duplicates(key));
1083:                        }
1084:                    }
1085:                });
1086:            }
1087:
1088:            void removeOddEntity() throws Exception {
1089:
1090:                writeRunner.run(new TransactionWorker() {
1091:                    public void doWork() throws Exception {
1092:                        for (int i = beginKey; i <= endKey; i += 2) {
1093:                            Long key = makeKey(i);
1094:                            Object val = makeVal(i);
1095:                            assertTrue(map.values().contains(val));
1096:                            assertTrue(map.values().remove(val));
1097:                            assertTrue(!map.values().contains(val));
1098:                            assertNull(map.get(key));
1099:                            assertTrue(!map.duplicates(key).contains(val));
1100:                            checkDupsSize(0, map.duplicates(key));
1101:                        }
1102:                    }
1103:                });
1104:            }
1105:
1106:            void removeOddEntry() throws Exception {
1107:
1108:                writeRunner.run(new TransactionWorker() {
1109:                    public void doWork() throws Exception {
1110:                        for (int i = beginKey; i <= endKey; i += 2) {
1111:                            Long key = makeKey(i);
1112:                            Object val = mapEntry(i);
1113:                            assertTrue(map.entrySet().contains(val));
1114:                            assertTrue(map.entrySet().remove(val));
1115:                            assertTrue(!map.entrySet().contains(val));
1116:                            assertNull(map.get(key));
1117:                        }
1118:                    }
1119:                });
1120:            }
1121:
1122:            void removeOddIter() throws Exception {
1123:
1124:                writeIterRunner.run(new TransactionWorker() {
1125:                    public void doWork() throws Exception {
1126:                        Iterator iter = iterator(map.keySet());
1127:                        try {
1128:                            for (int i = beginKey; i <= endKey; i += 1) {
1129:                                assertTrue(iter.hasNext());
1130:                                Long key = (Long) iter.next();
1131:                                assertNotNull(key);
1132:                                if (map instanceof  SortedMap) {
1133:                                    assertEquals(makeKey(i), key);
1134:                                }
1135:                                if ((key.intValue() & 1) != 0) {
1136:                                    iter.remove();
1137:                                }
1138:                            }
1139:                        } finally {
1140:                            StoredIterator.close(iter);
1141:                        }
1142:                    }
1143:                });
1144:            }
1145:
1146:            void removeOddList() throws Exception {
1147:
1148:                writeRunner.run(new TransactionWorker() {
1149:                    public void doWork() throws Exception {
1150:                        for (int i = beginKey; i <= endKey; i += 2) {
1151:                            // remove by index
1152:                            // (with entity binding, embbeded keys in values are
1153:                            // being changed so we can't use values for comparison)
1154:                            int idx = (i - beginKey) / 2;
1155:                            Object val = makeVal(i);
1156:                            if (!isEntityBinding) {
1157:                                assertTrue(list.contains(val));
1158:                                assertEquals(val, list.get(idx));
1159:                                assertEquals(idx, list.indexOf(val));
1160:                            }
1161:                            assertNotNull(list.get(idx));
1162:                            if (isEntityBinding) {
1163:                                assertNotNull(list.remove(idx));
1164:                            } else {
1165:                                assertTrue(list.contains(val));
1166:                                assertEquals(val, list.remove(idx));
1167:                            }
1168:                            assertTrue(!list.remove(val));
1169:                            assertTrue(!list.contains(val));
1170:                            assertTrue(!val.equals(list.get(idx)));
1171:                        }
1172:                    }
1173:                });
1174:            }
1175:
1176:            void removeOddListValue() throws Exception {
1177:
1178:                writeRunner.run(new TransactionWorker() {
1179:                    public void doWork() throws Exception {
1180:                        for (int i = beginKey; i <= endKey; i += 2) {
1181:                            // for non-entity case remove by value
1182:                            // (with entity binding, embbeded keys in values are
1183:                            // being changed so we can't use values for comparison)
1184:                            int idx = (i - beginKey) / 2;
1185:                            Object val = makeVal(i);
1186:                            assertTrue(list.contains(val));
1187:                            assertEquals(val, list.get(idx));
1188:                            assertEquals(idx, list.indexOf(val));
1189:                            assertTrue(list.remove(val));
1190:                            assertTrue(!list.remove(val));
1191:                            assertTrue(!list.contains(val));
1192:                            assertTrue(!val.equals(list.get(idx)));
1193:                        }
1194:                    }
1195:                });
1196:            }
1197:
1198:            void addOdd() throws Exception {
1199:
1200:                writeRunner.run(new TransactionWorker() {
1201:                    public void doWork() throws Exception {
1202:                        // add using Map.put()
1203:                        for (int i = beginKey; i <= endKey; i += 2) {
1204:                            Long key = makeKey(i);
1205:                            Object val = makeVal(i);
1206:                            assertNull(imap.get(key));
1207:                            assertNull(imap.put(key, val));
1208:                            assertEquals(val, imap.get(key));
1209:                            assertTrue(imap.duplicates(key).contains(val));
1210:                            checkDupsSize(1, imap.duplicates(key));
1211:                            if (isEntityBinding) {
1212:                                assertTrue(!imap.values().add(val));
1213:                            }
1214:                            if (!imap.areDuplicatesAllowed()) {
1215:                                assertEquals(val, imap.put(key, val));
1216:                            }
1217:                        }
1218:                    }
1219:                });
1220:            }
1221:
1222:            void addOddEntity() throws Exception {
1223:
1224:                writeRunner.run(new TransactionWorker() {
1225:                    public void doWork() throws Exception {
1226:                        // add using Map.values().add()
1227:                        for (int i = beginKey; i <= endKey; i += 2) {
1228:                            Long key = makeKey(i);
1229:                            Object val = makeVal(i);
1230:                            assertNull(imap.get(key));
1231:                            assertTrue(!imap.values().contains(val));
1232:                            assertTrue(imap.values().add(val));
1233:                            assertEquals(val, imap.get(key));
1234:                            assertTrue(imap.values().contains(val));
1235:                            assertTrue(imap.duplicates(key).contains(val));
1236:                            checkDupsSize(1, imap.duplicates(key));
1237:                            if (isEntityBinding) {
1238:                                assertTrue(!imap.values().add(val));
1239:                            }
1240:                        }
1241:                    }
1242:                });
1243:            }
1244:
1245:            void addOddDup() throws Exception {
1246:
1247:                writeRunner.run(new TransactionWorker() {
1248:                    public void doWork() throws Exception {
1249:                        // add using Map.duplicates().add()
1250:                        for (int i = beginKey; i <= endKey; i += 2) {
1251:                            Long key = makeKey(i);
1252:                            Object val = makeVal(i);
1253:                            assertNull(imap.get(key));
1254:                            assertTrue(!imap.values().contains(val));
1255:                            assertTrue(imap.duplicates(key).add(val));
1256:                            assertEquals(val, imap.get(key));
1257:                            assertTrue(imap.values().contains(val));
1258:                            assertTrue(imap.duplicates(key).contains(val));
1259:                            checkDupsSize(1, imap.duplicates(key));
1260:                            assertTrue(!imap.duplicates(key).add(val));
1261:                            if (isEntityBinding) {
1262:                                assertTrue(!imap.values().add(val));
1263:                            }
1264:                        }
1265:                    }
1266:                });
1267:            }
1268:
1269:            void addOddList() throws Exception {
1270:
1271:                writeRunner.run(new TransactionWorker() {
1272:                    public void doWork() throws Exception {
1273:                        for (int i = beginKey; i <= endKey; i += 2) {
1274:                            int idx = i - beginKey;
1275:                            Object val = makeVal(i);
1276:                            assertTrue(!list.contains(val));
1277:                            assertTrue(!val.equals(list.get(idx)));
1278:                            list.add(idx, val);
1279:                            assertTrue(list.contains(val));
1280:                            assertEquals(val, list.get(idx));
1281:                        }
1282:                    }
1283:                });
1284:            }
1285:
1286:            void addAllList() throws Exception {
1287:
1288:                writeRunner.run(new TransactionWorker() {
1289:                    public void doWork() throws Exception {
1290:                        for (int i = beginKey; i <= endKey; i += 1) {
1291:                            int idx = i - beginKey;
1292:                            Object val = makeVal(i);
1293:                            assertTrue(!list.contains(val));
1294:                            assertTrue(list.add(val));
1295:                            assertTrue(list.contains(val));
1296:                            assertEquals(val, list.get(idx));
1297:                        }
1298:                    }
1299:                });
1300:            }
1301:
1302:            void removeAllList() throws Exception {
1303:
1304:                writeRunner.run(new TransactionWorker() {
1305:                    public void doWork() throws Exception {
1306:                        assertTrue(!list.isEmpty());
1307:                        list.clear();
1308:                        assertTrue(list.isEmpty());
1309:                        for (int i = beginKey; i <= endKey; i += 1) {
1310:                            int idx = i - beginKey;
1311:                            assertNull(list.get(idx));
1312:                        }
1313:                    }
1314:                });
1315:            }
1316:
1317:            void testIterAddList() throws Exception {
1318:
1319:                writeIterRunner.run(new TransactionWorker() {
1320:                    public void doWork() throws Exception {
1321:                        ListIterator i = (ListIterator) iterator(list);
1322:                        try {
1323:                            assertTrue(!i.hasNext());
1324:                            i.add(makeVal(3));
1325:                            assertTrue(!i.hasNext());
1326:                            assertTrue(i.hasPrevious());
1327:                            assertEquals(3, intVal(i.previous()));
1328:
1329:                            i.add(makeVal(1));
1330:                            assertTrue(i.hasPrevious());
1331:                            assertTrue(i.hasNext());
1332:                            assertEquals(1, intVal(i.previous()));
1333:                            assertTrue(i.hasNext());
1334:                            assertEquals(1, intVal(i.next()));
1335:                            assertTrue(i.hasNext());
1336:                            assertEquals(3, intVal(i.next()));
1337:                            assertEquals(3, intVal(i.previous()));
1338:
1339:                            assertTrue(i.hasNext());
1340:                            i.add(makeVal(2));
1341:                            assertTrue(i.hasNext());
1342:                            assertTrue(i.hasPrevious());
1343:                            assertEquals(2, intVal(i.previous()));
1344:                            assertTrue(i.hasNext());
1345:                            assertEquals(2, intVal(i.next()));
1346:                            assertTrue(i.hasNext());
1347:                            assertEquals(3, intVal(i.next()));
1348:
1349:                            assertTrue(!i.hasNext());
1350:                            i.add(makeVal(4));
1351:                            i.add(makeVal(5));
1352:                            assertTrue(!i.hasNext());
1353:                            assertEquals(5, intVal(i.previous()));
1354:                            assertEquals(4, intVal(i.previous()));
1355:                            assertEquals(3, intVal(i.previous()));
1356:                            assertEquals(2, intVal(i.previous()));
1357:                            assertEquals(1, intVal(i.previous()));
1358:                            assertTrue(!i.hasPrevious());
1359:                        } finally {
1360:                            StoredIterator.close(i);
1361:                        }
1362:                    }
1363:                });
1364:            }
1365:
1366:            void testIterAddDuplicates() throws Exception {
1367:
1368:                writeIterRunner.run(new TransactionWorker() {
1369:                    public void doWork() throws Exception {
1370:                        assertNull(imap.put(makeKey(1), makeVal(1)));
1371:                        ListIterator i = (ListIterator) iterator(imap
1372:                                .duplicates(makeKey(1)));
1373:                        try {
1374:                            if (imap.areDuplicatesOrdered()) {
1375:                                i.add(makeVal(1, 4));
1376:                                i.add(makeVal(1, 2));
1377:                                i.add(makeVal(1, 3));
1378:                                while (i.hasPrevious())
1379:                                    i.previous();
1380:                                assertEquals(1, intVal(i.next()));
1381:                                assertEquals(2, intVal(i.next()));
1382:                                assertEquals(3, intVal(i.next()));
1383:                                assertEquals(4, intVal(i.next()));
1384:                                assertTrue(!i.hasNext());
1385:                            } else {
1386:                                assertEquals(1, intVal(i.next()));
1387:                                i.add(makeVal(1, 2));
1388:                                i.add(makeVal(1, 3));
1389:                                assertTrue(!i.hasNext());
1390:                                assertTrue(i.hasPrevious());
1391:                                assertEquals(3, intVal(i.previous()));
1392:                                assertEquals(2, intVal(i.previous()));
1393:                                assertEquals(1, intVal(i.previous()));
1394:                                assertTrue(!i.hasPrevious());
1395:                                i.add(makeVal(1, 4));
1396:                                i.add(makeVal(1, 5));
1397:                                assertTrue(i.hasNext());
1398:                                assertEquals(5, intVal(i.previous()));
1399:                                assertEquals(4, intVal(i.previous()));
1400:                                assertTrue(!i.hasPrevious());
1401:                                assertEquals(4, intVal(i.next()));
1402:                                assertEquals(5, intVal(i.next()));
1403:                                assertEquals(1, intVal(i.next()));
1404:                                assertEquals(2, intVal(i.next()));
1405:                                assertEquals(3, intVal(i.next()));
1406:                                assertTrue(!i.hasNext());
1407:                            }
1408:                        } finally {
1409:                            StoredIterator.close(i);
1410:                        }
1411:                    }
1412:                });
1413:            }
1414:
1415:            void readAll() throws Exception {
1416:
1417:                readRunner.run(new TransactionWorker() {
1418:                    public void doWork() throws Exception {
1419:                        // map
1420:
1421:                        assertNotNull(map.toString());
1422:                        for (int i = beginKey; i <= endKey; i += 1) {
1423:                            Long key = makeKey(i);
1424:                            Object val = map.get(key);
1425:                            assertEquals(makeVal(i), val);
1426:                            assertTrue(map.containsKey(key));
1427:                            assertTrue(map.containsValue(val));
1428:                            assertTrue(map.keySet().contains(key));
1429:                            assertTrue(map.values().contains(val));
1430:                            assertTrue(map.duplicates(key).contains(val));
1431:                            checkDupsSize(1, map.duplicates(key));
1432:                        }
1433:                        assertNull(map.get(makeKey(-1)));
1434:                        assertNull(map.get(makeKey(0)));
1435:                        assertNull(map.get(makeKey(beginKey - 1)));
1436:                        assertNull(map.get(makeKey(endKey + 1)));
1437:                        checkDupsSize(0, map.duplicates(makeKey(-1)));
1438:                        checkDupsSize(0, map.duplicates(makeKey(0)));
1439:                        checkDupsSize(0, map.duplicates(makeKey(beginKey - 1)));
1440:                        checkDupsSize(0, map.duplicates(makeKey(endKey + 1)));
1441:
1442:                        // entrySet
1443:
1444:                        Set set = map.entrySet();
1445:                        assertNotNull(set.toString());
1446:                        assertEquals(beginKey > endKey, set.isEmpty());
1447:                        Iterator iter = iterator(set);
1448:                        try {
1449:                            for (int i = beginKey; i <= endKey; i += 1) {
1450:                                assertTrue(iter.hasNext());
1451:                                Map.Entry entry = (Map.Entry) iter.next();
1452:                                Long key = (Long) entry.getKey();
1453:                                Object val = entry.getValue();
1454:                                if (map instanceof  SortedMap) {
1455:                                    assertEquals(intKey(key), i);
1456:                                }
1457:                                assertEquals(intKey(key), intVal(val));
1458:                                assertTrue(set.contains(entry));
1459:                            }
1460:                            assertTrue(!iter.hasNext());
1461:                        } finally {
1462:                            StoredIterator.close(iter);
1463:                        }
1464:                        Map.Entry[] entries = (Map.Entry[]) set
1465:                                .toArray(new Map.Entry[0]);
1466:                        assertNotNull(entries);
1467:                        assertEquals(endKey - beginKey + 1, entries.length);
1468:                        for (int i = beginKey; i <= endKey; i += 1) {
1469:                            Map.Entry entry = entries[i - beginKey];
1470:                            assertNotNull(entry);
1471:                            if (map instanceof  SortedMap) {
1472:                                assertEquals(makeKey(i), entry.getKey());
1473:                                assertEquals(makeVal(i), entry.getValue());
1474:                            }
1475:                        }
1476:                        readIterator(set, iterator(set), beginKey, endKey);
1477:                        if (smap != null) {
1478:                            SortedSet sset = (SortedSet) set;
1479:                            if (beginKey == 1 && endKey >= 1) {
1480:                                readIterator(sset, iterator(sset.subSet(
1481:                                        mapEntry(1), mapEntry(2))), 1, 1);
1482:                            }
1483:                            if (beginKey <= 2 && endKey >= 2) {
1484:                                readIterator(sset, iterator(sset.subSet(
1485:                                        mapEntry(2), mapEntry(3))), 2, 2);
1486:                            }
1487:                            if (beginKey <= endKey) {
1488:                                readIterator(sset,
1489:                                        iterator(sset.subSet(mapEntry(endKey),
1490:                                                mapEntry(endKey + 1))), endKey,
1491:                                        endKey);
1492:                            }
1493:                            if (isSubMap()) {
1494:                                if (beginKey <= endKey) {
1495:                                    if (rangeType != TAIL) {
1496:                                        try {
1497:                                            sset.subSet(mapEntry(endKey + 1),
1498:                                                    mapEntry(endKey + 2));
1499:                                            fail();
1500:                                        } catch (IllegalArgumentException e) {
1501:                                        }
1502:                                    }
1503:                                    if (rangeType != HEAD) {
1504:                                        try {
1505:                                            sset.subSet(mapEntry(0),
1506:                                                    mapEntry(1));
1507:                                            fail();
1508:                                        } catch (IllegalArgumentException e) {
1509:                                        }
1510:                                    }
1511:                                }
1512:                            } else {
1513:                                readIterator(sset, iterator(sset.subSet(
1514:                                        mapEntry(endKey + 1),
1515:                                        mapEntry(endKey + 2))), endKey,
1516:                                        endKey - 1);
1517:                                readIterator(sset, iterator(sset.subSet(
1518:                                        mapEntry(0), mapEntry(1))), 0, -1);
1519:                            }
1520:                        }
1521:
1522:                        // keySet
1523:
1524:                        set = map.keySet();
1525:                        assertNotNull(set.toString());
1526:                        assertEquals(beginKey > endKey, set.isEmpty());
1527:                        iter = iterator(set);
1528:                        try {
1529:                            for (int i = beginKey; i <= endKey; i += 1) {
1530:                                assertTrue(iter.hasNext());
1531:                                Long key = (Long) iter.next();
1532:                                assertTrue(set.contains(key));
1533:                                Object val = map.get(key);
1534:                                if (map instanceof  SortedMap) {
1535:                                    assertEquals(key, makeKey(i));
1536:                                }
1537:                                assertEquals(intKey(key), intVal(val));
1538:                            }
1539:                            assertTrue("" + beginKey + ' ' + endKey, !iter
1540:                                    .hasNext());
1541:                        } finally {
1542:                            StoredIterator.close(iter);
1543:                        }
1544:                        Long[] keys = (Long[]) set.toArray(new Long[0]);
1545:                        assertNotNull(keys);
1546:                        assertEquals(endKey - beginKey + 1, keys.length);
1547:                        for (int i = beginKey; i <= endKey; i += 1) {
1548:                            Long key = keys[i - beginKey];
1549:                            assertNotNull(key);
1550:                            if (map instanceof  SortedMap) {
1551:                                assertEquals(makeKey(i), key);
1552:                            }
1553:                        }
1554:                        readIterator(set, iterator(set), beginKey, endKey);
1555:
1556:                        // values
1557:
1558:                        Collection coll = map.values();
1559:                        assertNotNull(coll.toString());
1560:                        assertEquals(beginKey > endKey, coll.isEmpty());
1561:                        iter = iterator(coll);
1562:                        try {
1563:                            for (int i = beginKey; i <= endKey; i += 1) {
1564:                                assertTrue(iter.hasNext());
1565:                                Object val = iter.next();
1566:                                if (map instanceof  SortedMap) {
1567:                                    assertEquals(makeVal(i), val);
1568:                                }
1569:                            }
1570:                            assertTrue(!iter.hasNext());
1571:                        } finally {
1572:                            StoredIterator.close(iter);
1573:                        }
1574:                        Object[] values = coll.toArray();
1575:                        assertNotNull(values);
1576:                        assertEquals(endKey - beginKey + 1, values.length);
1577:                        for (int i = beginKey; i <= endKey; i += 1) {
1578:                            Object val = values[i - beginKey];
1579:                            assertNotNull(val);
1580:                            if (map instanceof  SortedMap) {
1581:                                assertEquals(makeVal(i), val);
1582:                            }
1583:                        }
1584:                        readIterator(coll, iterator(coll), beginKey, endKey);
1585:
1586:                        // list
1587:
1588:                        if (list != null) {
1589:                            assertNotNull(list.toString());
1590:                            assertEquals(beginKey > endKey, list.isEmpty());
1591:                            for (int i = beginKey; i <= endKey; i += 1) {
1592:                                int idx = i - beginKey;
1593:                                Object val = list.get(idx);
1594:                                assertEquals(makeVal(i), val);
1595:                                assertTrue(list.contains(val));
1596:                                assertEquals(idx, list.indexOf(val));
1597:                                assertEquals(idx, list.lastIndexOf(val));
1598:                            }
1599:                            ListIterator li = (ListIterator) iterator(list);
1600:                            try {
1601:                                for (int i = beginKey; i <= endKey; i += 1) {
1602:                                    int idx = i - beginKey;
1603:                                    assertTrue(li.hasNext());
1604:                                    assertEquals(idx, li.nextIndex());
1605:                                    Object val = li.next();
1606:                                    assertEquals(makeVal(i), val);
1607:                                    assertEquals(idx, li.previousIndex());
1608:                                }
1609:                                assertTrue(!li.hasNext());
1610:                            } finally {
1611:                                StoredIterator.close(li);
1612:                            }
1613:                            if (beginKey < endKey) {
1614:                                li = list.listIterator(1);
1615:                                try {
1616:                                    for (int i = beginKey + 1; i <= endKey; i += 1) {
1617:                                        int idx = i - beginKey;
1618:                                        assertTrue(li.hasNext());
1619:                                        assertEquals(idx, li.nextIndex());
1620:                                        Object val = li.next();
1621:                                        assertEquals(makeVal(i), val);
1622:                                        assertEquals(idx, li.previousIndex());
1623:                                    }
1624:                                    assertTrue(!li.hasNext());
1625:                                } finally {
1626:                                    StoredIterator.close(li);
1627:                                }
1628:                            }
1629:                            values = list.toArray();
1630:                            assertNotNull(values);
1631:                            assertEquals(endKey - beginKey + 1, values.length);
1632:                            for (int i = beginKey; i <= endKey; i += 1) {
1633:                                Object val = values[i - beginKey];
1634:                                assertNotNull(val);
1635:                                assertEquals(makeVal(i), val);
1636:                            }
1637:                            readIterator(list, iterator(list), beginKey, endKey);
1638:                        }
1639:
1640:                        // first/last
1641:
1642:                        if (smap != null) {
1643:                            if (beginKey <= endKey && beginKey >= 1
1644:                                    && beginKey <= maxKey) {
1645:                                assertEquals(makeKey(beginKey), smap.firstKey());
1646:                                assertEquals(makeKey(beginKey),
1647:                                        ((SortedSet) smap.keySet()).first());
1648:                                Object entry = ((SortedSet) smap.entrySet())
1649:                                        .first();
1650:                                assertEquals(makeKey(beginKey),
1651:                                        ((Map.Entry) entry).getKey());
1652:                                if (smap.values() instanceof  SortedSet) {
1653:                                    assertEquals(makeVal(beginKey),
1654:                                            ((SortedSet) smap.values()).first());
1655:                                }
1656:                            } else {
1657:                                assertNull(smap.firstKey());
1658:                                assertNull(((SortedSet) smap.keySet()).first());
1659:                                assertNull(((SortedSet) smap.entrySet())
1660:                                        .first());
1661:                                if (smap.values() instanceof  SortedSet) {
1662:                                    assertNull(((SortedSet) smap.values())
1663:                                            .first());
1664:                                }
1665:                            }
1666:                            if (beginKey <= endKey && endKey >= 1
1667:                                    && endKey <= maxKey) {
1668:                                assertEquals(makeKey(endKey), smap.lastKey());
1669:                                assertEquals(makeKey(endKey), ((SortedSet) smap
1670:                                        .keySet()).last());
1671:                                Object entry = ((SortedSet) smap.entrySet())
1672:                                        .last();
1673:                                assertEquals(makeKey(endKey),
1674:                                        ((Map.Entry) entry).getKey());
1675:                                if (smap.values() instanceof  SortedSet) {
1676:                                    assertEquals(makeVal(endKey),
1677:                                            ((SortedSet) smap.values()).last());
1678:                                }
1679:                            } else {
1680:                                assertNull(smap.lastKey());
1681:                                assertNull(((SortedSet) smap.keySet()).last());
1682:                                assertNull(((SortedSet) smap.entrySet()).last());
1683:                                if (smap.values() instanceof  SortedSet) {
1684:                                    assertNull(((SortedSet) smap.values())
1685:                                            .last());
1686:                                }
1687:                            }
1688:                        }
1689:                    }
1690:                });
1691:            }
1692:
1693:            void readEven() throws Exception {
1694:
1695:                readRunner.run(new TransactionWorker() {
1696:                    public void doWork() throws Exception {
1697:                        int readBegin = ((beginKey & 1) != 0) ? (beginKey + 1)
1698:                                : beginKey;
1699:                        int readEnd = ((endKey & 1) != 0) ? (endKey - 1)
1700:                                : endKey;
1701:                        int readIncr = 2;
1702:
1703:                        // map
1704:
1705:                        for (int i = beginKey; i <= endKey; i += 1) {
1706:                            Long key = makeKey(i);
1707:                            if ((i & 1) == 0) {
1708:                                Object val = map.get(key);
1709:                                assertEquals(makeVal(i), val);
1710:                                assertTrue(map.containsKey(key));
1711:                                assertTrue(map.containsValue(val));
1712:                                assertTrue(map.keySet().contains(key));
1713:                                assertTrue(map.values().contains(val));
1714:                                assertTrue(map.duplicates(key).contains(val));
1715:                                checkDupsSize(1, map.duplicates(key));
1716:                            } else {
1717:                                Object val = makeVal(i);
1718:                                assertTrue(!map.containsKey(key));
1719:                                assertTrue(!map.containsValue(val));
1720:                                assertTrue(!map.keySet().contains(key));
1721:                                assertTrue(!map.values().contains(val));
1722:                                assertTrue(!map.duplicates(key).contains(val));
1723:                                checkDupsSize(0, map.duplicates(key));
1724:                            }
1725:                        }
1726:
1727:                        // entrySet
1728:
1729:                        Set set = map.entrySet();
1730:                        assertEquals(beginKey > endKey, set.isEmpty());
1731:                        Iterator iter = iterator(set);
1732:                        try {
1733:                            for (int i = readBegin; i <= readEnd; i += readIncr) {
1734:                                assertTrue(iter.hasNext());
1735:                                Map.Entry entry = (Map.Entry) iter.next();
1736:                                Long key = (Long) entry.getKey();
1737:                                Object val = entry.getValue();
1738:                                if (map instanceof  SortedMap) {
1739:                                    assertEquals(intKey(key), i);
1740:                                }
1741:                                assertEquals(intKey(key), intVal(val));
1742:                                assertTrue(set.contains(entry));
1743:                            }
1744:                            assertTrue(!iter.hasNext());
1745:                        } finally {
1746:                            StoredIterator.close(iter);
1747:                        }
1748:
1749:                        // keySet
1750:
1751:                        set = map.keySet();
1752:                        assertEquals(beginKey > endKey, set.isEmpty());
1753:                        iter = iterator(set);
1754:                        try {
1755:                            for (int i = readBegin; i <= readEnd; i += readIncr) {
1756:                                assertTrue(iter.hasNext());
1757:                                Long key = (Long) iter.next();
1758:                                assertTrue(set.contains(key));
1759:                                Object val = map.get(key);
1760:                                if (map instanceof  SortedMap) {
1761:                                    assertEquals(key, makeKey(i));
1762:                                }
1763:                                assertEquals(intKey(key), intVal(val));
1764:                            }
1765:                            assertTrue(!iter.hasNext());
1766:                        } finally {
1767:                            StoredIterator.close(iter);
1768:                        }
1769:
1770:                        // values
1771:
1772:                        Collection coll = map.values();
1773:                        assertEquals(beginKey > endKey, coll.isEmpty());
1774:                        iter = iterator(coll);
1775:                        try {
1776:                            for (int i = readBegin; i <= readEnd; i += readIncr) {
1777:                                assertTrue(iter.hasNext());
1778:                                Object val = iter.next();
1779:                                if (map instanceof  SortedMap) {
1780:                                    assertEquals(makeVal(i), val);
1781:                                }
1782:                            }
1783:                            assertTrue(!iter.hasNext());
1784:                        } finally {
1785:                            StoredIterator.close(iter);
1786:                        }
1787:
1788:                        // list not used since keys may not be renumbered for this
1789:                        // method to work in general
1790:
1791:                        // first/last
1792:
1793:                        if (smap != null) {
1794:                            if (readBegin <= readEnd && readBegin >= 1
1795:                                    && readBegin <= maxKey) {
1796:                                assertEquals(makeKey(readBegin), smap
1797:                                        .firstKey());
1798:                                assertEquals(makeKey(readBegin),
1799:                                        ((SortedSet) smap.keySet()).first());
1800:                                Object entry = ((SortedSet) smap.entrySet())
1801:                                        .first();
1802:                                assertEquals(makeKey(readBegin),
1803:                                        ((Map.Entry) entry).getKey());
1804:                                if (smap.values() instanceof  SortedSet) {
1805:                                    assertEquals(makeVal(readBegin),
1806:                                            ((SortedSet) smap.values()).first());
1807:                                }
1808:                            } else {
1809:                                assertNull(smap.firstKey());
1810:                                assertNull(((SortedSet) smap.keySet()).first());
1811:                                assertNull(((SortedSet) smap.entrySet())
1812:                                        .first());
1813:                                if (smap.values() instanceof  SortedSet) {
1814:                                    assertNull(((SortedSet) smap.values())
1815:                                            .first());
1816:                                }
1817:                            }
1818:                            if (readBegin <= readEnd && readEnd >= 1
1819:                                    && readEnd <= maxKey) {
1820:                                assertEquals(makeKey(readEnd), smap.lastKey());
1821:                                assertEquals(makeKey(readEnd),
1822:                                        ((SortedSet) smap.keySet()).last());
1823:                                Object entry = ((SortedSet) smap.entrySet())
1824:                                        .last();
1825:                                assertEquals(makeKey(readEnd),
1826:                                        ((Map.Entry) entry).getKey());
1827:                                if (smap.values() instanceof  SortedSet) {
1828:                                    assertEquals(makeVal(readEnd),
1829:                                            ((SortedSet) smap.values()).last());
1830:                                }
1831:                            } else {
1832:                                assertNull(smap.lastKey());
1833:                                assertNull(((SortedSet) smap.keySet()).last());
1834:                                assertNull(((SortedSet) smap.entrySet()).last());
1835:                                if (smap.values() instanceof  SortedSet) {
1836:                                    assertNull(((SortedSet) smap.values())
1837:                                            .last());
1838:                                }
1839:                            }
1840:                        }
1841:                    }
1842:                });
1843:            }
1844:
1845:            void readEvenList() throws Exception {
1846:
1847:                readRunner.run(new TransactionWorker() {
1848:                    public void doWork() throws Exception {
1849:                        int readBegin = ((beginKey & 1) != 0) ? (beginKey + 1)
1850:                                : beginKey;
1851:                        int readEnd = ((endKey & 1) != 0) ? (endKey - 1)
1852:                                : endKey;
1853:                        int readIncr = 2;
1854:
1855:                        assertEquals(beginKey > endKey, list.isEmpty());
1856:                        ListIterator iter = (ListIterator) iterator(list);
1857:                        try {
1858:                            int idx = 0;
1859:                            for (int i = readBegin; i <= readEnd; i += readIncr) {
1860:                                assertTrue(iter.hasNext());
1861:                                assertEquals(idx, iter.nextIndex());
1862:                                Object val = iter.next();
1863:                                assertEquals(idx, iter.previousIndex());
1864:                                if (isEntityBinding) {
1865:                                    assertEquals(i, intVal(val));
1866:                                } else {
1867:                                    assertEquals(makeVal(i), val);
1868:                                }
1869:                                idx += 1;
1870:                            }
1871:                            assertTrue(!iter.hasNext());
1872:                        } finally {
1873:                            StoredIterator.close(iter);
1874:                        }
1875:                    }
1876:                });
1877:            }
1878:
1879:            void readIterator(Collection coll, Iterator iter, int beginValue,
1880:                    int endValue) {
1881:
1882:                ListIterator li = (ListIterator) iter;
1883:                boolean isList = (coll instanceof  List);
1884:                Iterator clone = null;
1885:                try {
1886:                    // at beginning
1887:                    assertTrue(!li.hasPrevious());
1888:                    assertTrue(!li.hasPrevious());
1889:                    try {
1890:                        li.previous();
1891:                    } catch (NoSuchElementException e) {
1892:                    }
1893:                    if (isList) {
1894:                        assertEquals(-1, li.previousIndex());
1895:                    }
1896:                    if (endValue < beginValue) {
1897:                        // is empty
1898:                        assertTrue(!iter.hasNext());
1899:                        try {
1900:                            iter.next();
1901:                        } catch (NoSuchElementException e) {
1902:                        }
1903:                        if (isList) {
1904:                            assertEquals(Integer.MAX_VALUE, li.nextIndex());
1905:                        }
1906:                    }
1907:                    // loop thru all and collect in array
1908:                    int[] values = new int[endValue - beginValue + 1];
1909:                    for (int i = beginValue; i <= endValue; i += 1) {
1910:                        assertTrue(iter.hasNext());
1911:                        int idx = i - beginKey;
1912:                        if (isList) {
1913:                            assertEquals(idx, li.nextIndex());
1914:                        }
1915:                        int value = intIter(coll, iter.next());
1916:                        if (isList) {
1917:                            assertEquals(idx, li.previousIndex());
1918:                        }
1919:                        values[i - beginValue] = value;
1920:                        if (((StoredCollection) coll).isOrdered()) {
1921:                            assertEquals(i, value);
1922:                        } else {
1923:                            assertTrue(value >= beginValue);
1924:                            assertTrue(value <= endValue);
1925:                        }
1926:                    }
1927:                    // at end
1928:                    assertTrue(!iter.hasNext());
1929:                    try {
1930:                        iter.next();
1931:                    } catch (NoSuchElementException e) {
1932:                    }
1933:                    if (isList) {
1934:                        assertEquals(Integer.MAX_VALUE, li.nextIndex());
1935:                    }
1936:                    // clone at same position
1937:                    clone = StoredCollections.iterator(iter);
1938:                    assertTrue(!clone.hasNext());
1939:                    // loop thru in reverse
1940:                    for (int i = endValue; i >= beginValue; i -= 1) {
1941:                        assertTrue(li.hasPrevious());
1942:                        int idx = i - beginKey;
1943:                        if (isList) {
1944:                            assertEquals(idx, li.previousIndex());
1945:                        }
1946:                        int value = intIter(coll, li.previous());
1947:                        if (isList) {
1948:                            assertEquals(idx, li.nextIndex());
1949:                        }
1950:                        assertEquals(values[i - beginValue], value);
1951:                    }
1952:                    // clone should not have changed
1953:                    assertTrue(!clone.hasNext());
1954:                    // at beginning
1955:                    assertTrue(!li.hasPrevious());
1956:                    try {
1957:                        li.previous();
1958:                    } catch (NoSuchElementException e) {
1959:                    }
1960:                    if (isList) {
1961:                        assertEquals(-1, li.previousIndex());
1962:                    }
1963:                    // loop thru with some back-and-forth
1964:                    for (int i = beginValue; i <= endValue; i += 1) {
1965:                        assertTrue(iter.hasNext());
1966:                        int idx = i - beginKey;
1967:                        if (isList) {
1968:                            assertEquals(idx, li.nextIndex());
1969:                        }
1970:                        Object obj = iter.next();
1971:                        if (isList) {
1972:                            assertEquals(idx, li.previousIndex());
1973:                        }
1974:                        assertEquals(obj, li.previous());
1975:                        if (isList) {
1976:                            assertEquals(idx, li.nextIndex());
1977:                        }
1978:                        assertEquals(obj, iter.next());
1979:                        if (isList) {
1980:                            assertEquals(idx, li.previousIndex());
1981:                        }
1982:                        int value = intIter(coll, obj);
1983:                        assertEquals(values[i - beginValue], value);
1984:                    }
1985:                    // at end
1986:                    assertTrue(!iter.hasNext());
1987:                    try {
1988:                        iter.next();
1989:                    } catch (NoSuchElementException e) {
1990:                    }
1991:                    if (isList) {
1992:                        assertEquals(Integer.MAX_VALUE, li.nextIndex());
1993:                    }
1994:                } finally {
1995:                    StoredIterator.close(iter);
1996:                    StoredIterator.close(clone);
1997:                }
1998:            }
1999:
2000:            void bulkOperations() throws Exception {
2001:
2002:                writeRunner.run(new TransactionWorker() {
2003:                    public void doWork() throws Exception {
2004:                        HashMap hmap = new HashMap();
2005:                        for (int i = Math.max(1, beginKey); i <= Math.min(
2006:                                maxKey, endKey); i += 1) {
2007:                            hmap.put(makeKey(i), makeVal(i));
2008:                        }
2009:                        assertTrue(map.equals(hmap));
2010:                        assertTrue(map.entrySet().equals(hmap.entrySet()));
2011:                        assertTrue(map.keySet().equals(hmap.keySet()));
2012:                        assertTrue(map.values().equals(hmap.values()));
2013:
2014:                        assertTrue(map.entrySet().containsAll(hmap.entrySet()));
2015:                        assertTrue(map.keySet().containsAll(hmap.keySet()));
2016:                        assertTrue(map.values().containsAll(hmap.values()));
2017:
2018:                        map.clear();
2019:                        assertTrue(map.isEmpty());
2020:                        imap.putAll(hmap);
2021:                        assertTrue(map.equals(hmap));
2022:
2023:                        assertTrue(map.entrySet().removeAll(hmap.entrySet()));
2024:                        assertTrue(map.entrySet().isEmpty());
2025:                        assertTrue(!map.entrySet().removeAll(hmap.entrySet()));
2026:                        assertTrue(imap.entrySet().addAll(hmap.entrySet()));
2027:                        assertTrue(map.entrySet().containsAll(hmap.entrySet()));
2028:                        assertTrue(!imap.entrySet().addAll(hmap.entrySet()));
2029:                        assertTrue(map.equals(hmap));
2030:
2031:                        assertTrue(!map.entrySet().retainAll(hmap.entrySet()));
2032:                        assertTrue(map.equals(hmap));
2033:                        assertTrue(map.entrySet().retainAll(
2034:                                Collections.EMPTY_SET));
2035:                        assertTrue(map.isEmpty());
2036:                        imap.putAll(hmap);
2037:                        assertTrue(map.equals(hmap));
2038:
2039:                        assertTrue(map.values().removeAll(hmap.values()));
2040:                        assertTrue(map.values().isEmpty());
2041:                        assertTrue(!map.values().removeAll(hmap.values()));
2042:                        if (isEntityBinding) {
2043:                            assertTrue(imap.values().addAll(hmap.values()));
2044:                            assertTrue(map.values().containsAll(hmap.values()));
2045:                            assertTrue(!imap.values().addAll(hmap.values()));
2046:                        } else {
2047:                            imap.putAll(hmap);
2048:                        }
2049:                        assertTrue(map.equals(hmap));
2050:
2051:                        assertTrue(!map.values().retainAll(hmap.values()));
2052:                        assertTrue(map.equals(hmap));
2053:                        assertTrue(map.values()
2054:                                .retainAll(Collections.EMPTY_SET));
2055:                        assertTrue(map.isEmpty());
2056:                        imap.putAll(hmap);
2057:                        assertTrue(map.equals(hmap));
2058:
2059:                        assertTrue(map.keySet().removeAll(hmap.keySet()));
2060:                        assertTrue(map.keySet().isEmpty());
2061:                        assertTrue(!map.keySet().removeAll(hmap.keySet()));
2062:                        assertTrue(imap.keySet().addAll(hmap.keySet()));
2063:                        assertTrue(imap.keySet().containsAll(hmap.keySet()));
2064:                        if (index != null) {
2065:                            assertTrue(map.keySet().isEmpty());
2066:                        }
2067:                        assertTrue(!imap.keySet().addAll(hmap.keySet()));
2068:                        // restore values to non-null
2069:                        imap.keySet().removeAll(hmap.keySet());
2070:                        imap.putAll(hmap);
2071:                        assertTrue(map.equals(hmap));
2072:
2073:                        assertTrue(!map.keySet().retainAll(hmap.keySet()));
2074:                        assertTrue(map.equals(hmap));
2075:                        assertTrue(map.keySet()
2076:                                .retainAll(Collections.EMPTY_SET));
2077:                        assertTrue(map.isEmpty());
2078:                        imap.putAll(hmap);
2079:                        assertTrue(map.equals(hmap));
2080:                    }
2081:                });
2082:            }
2083:
2084:            void bulkListOperations() throws Exception {
2085:
2086:                writeRunner.run(new TransactionWorker() {
2087:                    public void doWork() throws Exception {
2088:                        ArrayList alist = new ArrayList();
2089:                        for (int i = beginKey; i <= endKey; i += 1) {
2090:                            alist.add(makeVal(i));
2091:                        }
2092:
2093:                        assertTrue(list.equals(alist));
2094:                        assertTrue(list.containsAll(alist));
2095:
2096:                        if (isListAddAllowed()) {
2097:                            list.clear();
2098:                            assertTrue(list.isEmpty());
2099:                            assertTrue(ilist.addAll(alist));
2100:                            assertTrue(list.equals(alist));
2101:                        }
2102:
2103:                        assertTrue(!list.retainAll(alist));
2104:                        assertTrue(list.equals(alist));
2105:
2106:                        if (isListAddAllowed()) {
2107:                            assertTrue(list.retainAll(Collections.EMPTY_SET));
2108:                            assertTrue(list.isEmpty());
2109:                            assertTrue(ilist.addAll(alist));
2110:                            assertTrue(list.equals(alist));
2111:                        }
2112:
2113:                        if (isListAddAllowed() && !isEntityBinding) {
2114:                            // deleting in a renumbered list with entity binding will
2115:                            // change the values dynamically, making it very difficult
2116:                            // to test
2117:                            assertTrue(list.removeAll(alist));
2118:                            assertTrue(list.isEmpty());
2119:                            assertTrue(!list.removeAll(alist));
2120:                            assertTrue(ilist.addAll(alist));
2121:                            assertTrue(list.containsAll(alist));
2122:                            assertTrue(list.equals(alist));
2123:                        }
2124:
2125:                        if (isListAddAllowed() && !isEntityBinding) {
2126:                            // addAll at an index is also very difficult to test with
2127:                            // an entity binding
2128:
2129:                            // addAll at first index
2130:                            ilist.addAll(beginKey, alist);
2131:                            assertTrue(list.containsAll(alist));
2132:                            assertEquals(2 * alist.size(), countElements(list));
2133:                            for (int i = beginKey; i <= endKey; i += 1)
2134:                                ilist.remove(beginKey);
2135:                            assertTrue(list.equals(alist));
2136:
2137:                            // addAll at last index
2138:                            ilist.addAll(endKey, alist);
2139:                            assertTrue(list.containsAll(alist));
2140:                            assertEquals(2 * alist.size(), countElements(list));
2141:                            for (int i = beginKey; i <= endKey; i += 1)
2142:                                ilist.remove(endKey);
2143:                            assertTrue(list.equals(alist));
2144:
2145:                            // addAll in the middle
2146:                            ilist.addAll(endKey - 1, alist);
2147:                            assertTrue(list.containsAll(alist));
2148:                            assertEquals(2 * alist.size(), countElements(list));
2149:                            for (int i = beginKey; i <= endKey; i += 1)
2150:                                ilist.remove(endKey - 1);
2151:                            assertTrue(list.equals(alist));
2152:                        }
2153:                    }
2154:                });
2155:            }
2156:
2157:            void readWriteRange(final int type, final int rangeBegin,
2158:                    final int rangeEnd) throws Exception {
2159:
2160:                writeRunner.run(new TransactionWorker() {
2161:                    public void doWork() throws Exception {
2162:                        setRange(type, rangeBegin, rangeEnd);
2163:                        createOutOfRange(rangeBegin, rangeEnd);
2164:                        if (rangeType != TAIL) {
2165:                            writeOutOfRange(new Long(rangeEnd + 1));
2166:                        }
2167:                        if (rangeType != HEAD) {
2168:                            writeOutOfRange(new Long(rangeBegin - 1));
2169:                        }
2170:                        if (rangeBegin <= rangeEnd) {
2171:                            updateAll();
2172:                        }
2173:                        if (rangeBegin < rangeEnd && !map.areKeysRenumbered()) {
2174:                            bulkOperations();
2175:                            removeIter();
2176:                        }
2177:                        readAll();
2178:                        clearRange();
2179:                    }
2180:                });
2181:            }
2182:
2183:            void setRange(int type, int rangeBegin, int rangeEnd) {
2184:
2185:                rangeType = type;
2186:                saveMap = map;
2187:                saveSMap = smap;
2188:                saveList = list;
2189:                int listBegin = rangeBegin - beginKey;
2190:                boolean canMakeSubList = (list != null && listBegin >= 0);
2191:                if (!canMakeSubList) {
2192:                    list = null;
2193:                }
2194:                if (list != null) {
2195:                    try {
2196:                        list.subList(-1, 0);
2197:                        fail();
2198:                    } catch (IndexOutOfBoundsException e) {
2199:                    }
2200:                }
2201:                switch (type) {
2202:
2203:                case SUB:
2204:                    smap = (StoredSortedMap) smap.subMap(makeKey(rangeBegin),
2205:                            makeKey(rangeEnd + 1));
2206:                    if (canMakeSubList) {
2207:                        list = (StoredList) list.subList(listBegin, rangeEnd
2208:                                + 1 - beginKey);
2209:                    }
2210:                    // check for equivalent ranges
2211:                    assertEquals(smap, ((StoredSortedMap) saveSMap).subMap(
2212:                            makeKey(rangeBegin), true, makeKey(rangeEnd + 1),
2213:                            false));
2214:                    assertEquals(smap.entrySet(),
2215:                            ((StoredSortedEntrySet) saveSMap.entrySet())
2216:                                    .subSet(mapEntry(rangeBegin), true,
2217:                                            mapEntry(rangeEnd + 1), false));
2218:                    assertEquals(smap.keySet(), ((StoredSortedKeySet) saveSMap
2219:                            .keySet()).subSet(makeKey(rangeBegin), true,
2220:                            makeKey(rangeEnd + 1), false));
2221:                    if (smap.values() instanceof  SortedSet) {
2222:                        assertEquals(smap.values(),
2223:                                ((StoredSortedValueSet) saveSMap.values())
2224:                                        .subSet(makeVal(rangeBegin), true,
2225:                                                makeVal(rangeEnd + 1), false));
2226:                    }
2227:                    break;
2228:                case HEAD:
2229:                    smap = (StoredSortedMap) smap
2230:                            .headMap(makeKey(rangeEnd + 1));
2231:                    if (canMakeSubList) {
2232:                        list = (StoredList) list.subList(0, rangeEnd + 1
2233:                                - beginKey);
2234:                    }
2235:                    // check for equivalent ranges
2236:                    assertEquals(smap, ((StoredSortedMap) saveSMap).headMap(
2237:                            makeKey(rangeEnd + 1), false));
2238:                    assertEquals(smap.entrySet(),
2239:                            ((StoredSortedEntrySet) saveSMap.entrySet())
2240:                                    .headSet(mapEntry(rangeEnd + 1), false));
2241:                    assertEquals(smap.keySet(), ((StoredSortedKeySet) saveSMap
2242:                            .keySet()).headSet(makeKey(rangeEnd + 1), false));
2243:                    if (smap.values() instanceof  SortedSet) {
2244:                        assertEquals(smap.values(),
2245:                                ((StoredSortedValueSet) saveSMap.values())
2246:                                        .headSet(makeVal(rangeEnd + 1), false));
2247:                    }
2248:                    break;
2249:                case TAIL:
2250:                    smap = (StoredSortedMap) smap.tailMap(makeKey(rangeBegin));
2251:                    if (canMakeSubList) {
2252:                        list = (StoredList) list.subList(listBegin, maxKey + 1
2253:                                - beginKey);
2254:                    }
2255:                    // check for equivalent ranges
2256:                    assertEquals(smap, ((StoredSortedMap) saveSMap).tailMap(
2257:                            makeKey(rangeBegin), true));
2258:                    assertEquals(smap.entrySet(),
2259:                            ((StoredSortedEntrySet) saveSMap.entrySet())
2260:                                    .tailSet(mapEntry(rangeBegin), true));
2261:                    assertEquals(smap.keySet(), ((StoredSortedKeySet) saveSMap
2262:                            .keySet()).tailSet(makeKey(rangeBegin), true));
2263:                    if (smap.values() instanceof  SortedSet) {
2264:                        assertEquals(smap.values(),
2265:                                ((StoredSortedValueSet) saveSMap.values())
2266:                                        .tailSet(makeVal(rangeBegin), true));
2267:                    }
2268:                    break;
2269:                default:
2270:                    throw new RuntimeException();
2271:                }
2272:                map = smap;
2273:                beginKey = rangeBegin;
2274:                if (rangeBegin < 1 || rangeEnd > maxKey) {
2275:                    endKey = rangeBegin - 1; // force empty range for readAll()
2276:                } else {
2277:                    endKey = rangeEnd;
2278:                }
2279:            }
2280:
2281:            void clearRange() {
2282:
2283:                rangeType = NONE;
2284:                beginKey = 1;
2285:                endKey = maxKey;
2286:                map = saveMap;
2287:                smap = saveSMap;
2288:                list = saveList;
2289:            }
2290:
2291:            void createOutOfRange(int rangeBegin, int rangeEnd)
2292:                    throws Exception {
2293:
2294:                // map
2295:
2296:                if (rangeType != TAIL) {
2297:                    try {
2298:                        smap.subMap(makeKey(rangeBegin), makeKey(rangeEnd + 2));
2299:                        fail();
2300:                    } catch (IllegalArgumentException e) {
2301:                    }
2302:                    try {
2303:                        smap.headMap(makeKey(rangeEnd + 2));
2304:                        fail();
2305:                    } catch (IllegalArgumentException e) {
2306:                    }
2307:                    checkDupsSize(0, smap.duplicates(makeKey(rangeEnd + 2)));
2308:                }
2309:                if (rangeType != HEAD) {
2310:                    try {
2311:                        smap.subMap(makeKey(rangeBegin - 1),
2312:                                makeKey(rangeEnd + 1));
2313:                        fail();
2314:                    } catch (IllegalArgumentException e) {
2315:                    }
2316:                    try {
2317:                        smap.tailMap(makeKey(rangeBegin - 1));
2318:                        fail();
2319:                    } catch (IllegalArgumentException e) {
2320:                    }
2321:                    checkDupsSize(0, smap.duplicates(makeKey(rangeBegin - 1)));
2322:                }
2323:
2324:                // keySet
2325:
2326:                if (rangeType != TAIL) {
2327:                    SortedSet sset = (SortedSet) map.keySet();
2328:                    try {
2329:                        sset.subSet(makeKey(rangeBegin), makeKey(rangeEnd + 2));
2330:                        fail();
2331:                    } catch (IllegalArgumentException e) {
2332:                    }
2333:                    try {
2334:                        sset.headSet(makeKey(rangeEnd + 2));
2335:                        fail();
2336:                    } catch (IllegalArgumentException e) {
2337:                    }
2338:                    try {
2339:                        iterator(sset.subSet(makeKey(rangeEnd + 1),
2340:                                makeKey(rangeEnd + 2)));
2341:                        fail();
2342:                    } catch (IllegalArgumentException e) {
2343:                    }
2344:                }
2345:                if (rangeType != HEAD) {
2346:                    SortedSet sset = (SortedSet) map.keySet();
2347:                    try {
2348:                        sset.subSet(makeKey(rangeBegin - 1),
2349:                                makeKey(rangeEnd + 1));
2350:                        fail();
2351:                    } catch (IllegalArgumentException e) {
2352:                    }
2353:                    try {
2354:                        sset.tailSet(makeKey(rangeBegin - 1));
2355:                        fail();
2356:                    } catch (IllegalArgumentException e) {
2357:                    }
2358:                    try {
2359:                        iterator(sset.subSet(makeKey(rangeBegin - 1),
2360:                                makeKey(rangeBegin)));
2361:                        fail();
2362:                    } catch (IllegalArgumentException e) {
2363:                    }
2364:                }
2365:
2366:                // entrySet
2367:
2368:                if (rangeType != TAIL) {
2369:                    SortedSet sset = (SortedSet) map.entrySet();
2370:                    try {
2371:                        sset.subSet(mapEntry(rangeBegin),
2372:                                mapEntry(rangeEnd + 2));
2373:                        fail();
2374:                    } catch (IllegalArgumentException e) {
2375:                    }
2376:                    try {
2377:                        sset.headSet(mapEntry(rangeEnd + 2));
2378:                        fail();
2379:                    } catch (IllegalArgumentException e) {
2380:                    }
2381:                    try {
2382:                        iterator(sset.subSet(mapEntry(rangeEnd + 1),
2383:                                mapEntry(rangeEnd + 2)));
2384:                        fail();
2385:                    } catch (IllegalArgumentException e) {
2386:                    }
2387:                }
2388:                if (rangeType != HEAD) {
2389:                    SortedSet sset = (SortedSet) map.entrySet();
2390:                    try {
2391:                        sset.subSet(mapEntry(rangeBegin - 1),
2392:                                mapEntry(rangeEnd + 1));
2393:                        fail();
2394:                    } catch (IllegalArgumentException e) {
2395:                    }
2396:                    try {
2397:                        sset.tailSet(mapEntry(rangeBegin - 1));
2398:                        fail();
2399:                    } catch (IllegalArgumentException e) {
2400:                    }
2401:                    try {
2402:                        iterator(sset.subSet(mapEntry(rangeBegin - 1),
2403:                                mapEntry(rangeBegin)));
2404:                        fail();
2405:                    } catch (IllegalArgumentException e) {
2406:                    }
2407:                }
2408:
2409:                // values
2410:
2411:                if (map.values() instanceof  SortedSet) {
2412:                    SortedSet sset = (SortedSet) map.values();
2413:                    if (rangeType != TAIL) {
2414:                        try {
2415:                            sset.subSet(makeVal(rangeBegin),
2416:                                    makeVal(rangeEnd + 2));
2417:                            fail();
2418:                        } catch (IllegalArgumentException e) {
2419:                        }
2420:                        try {
2421:                            sset.headSet(makeVal(rangeEnd + 2));
2422:                            fail();
2423:                        } catch (IllegalArgumentException e) {
2424:                        }
2425:                    }
2426:                    if (rangeType != HEAD) {
2427:                        try {
2428:                            sset.subSet(makeVal(rangeBegin - 1),
2429:                                    makeVal(rangeEnd + 1));
2430:                            fail();
2431:                        } catch (IllegalArgumentException e) {
2432:                        }
2433:                        try {
2434:                            sset.tailSet(makeVal(rangeBegin - 1));
2435:                            fail();
2436:                        } catch (IllegalArgumentException e) {
2437:                        }
2438:                    }
2439:                }
2440:
2441:                // list
2442:
2443:                if (list != null) {
2444:                    int size = rangeEnd - rangeBegin + 1;
2445:                    try {
2446:                        list.subList(0, size + 1);
2447:                        fail();
2448:                    } catch (IndexOutOfBoundsException e) {
2449:                    }
2450:                    try {
2451:                        list.subList(-1, size);
2452:                        fail();
2453:                    } catch (IndexOutOfBoundsException e) {
2454:                    }
2455:                    try {
2456:                        list.subList(2, 1);
2457:                        fail();
2458:                    } catch (IndexOutOfBoundsException e) {
2459:                    }
2460:                    try {
2461:                        list.subList(size, size);
2462:                        fail();
2463:                    } catch (IndexOutOfBoundsException e) {
2464:                    }
2465:                }
2466:            }
2467:
2468:            void writeOutOfRange(Long badNewKey) throws Exception {
2469:
2470:                try {
2471:                    map.put(badNewKey, makeVal(badNewKey));
2472:                    fail();
2473:                } catch (IllegalArgumentException e) {
2474:                    assertTrue(e.toString(), index == null);
2475:                } catch (UnsupportedOperationException e) {
2476:                    assertTrue(index != null);
2477:                }
2478:                try {
2479:                    map.keySet().add(badNewKey);
2480:                    fail();
2481:                } catch (IllegalArgumentException e) {
2482:                    assertTrue(index == null);
2483:                } catch (UnsupportedOperationException e) {
2484:                    assertTrue(index != null);
2485:                }
2486:                try {
2487:                    map.values().add(makeEntity(badNewKey));
2488:                    fail();
2489:                } catch (IllegalArgumentException e) {
2490:                    assertTrue(isEntityBinding && index == null);
2491:                } catch (UnsupportedOperationException e) {
2492:                    assertTrue(!(isEntityBinding && index == null));
2493:                }
2494:                if (list != null) {
2495:                    int i = badNewKey.intValue() - beginKey;
2496:                    try {
2497:                        list.set(i, makeVal(i));
2498:                        fail();
2499:                    } catch (IndexOutOfBoundsException e) {
2500:                        assertTrue(index == null);
2501:                    } catch (UnsupportedOperationException e) {
2502:                        assertTrue(index != null);
2503:                    }
2504:                    try {
2505:                        list.add(i, makeVal(badNewKey));
2506:                        fail();
2507:                    } catch (UnsupportedOperationException e) {
2508:                    }
2509:                }
2510:            }
2511:
2512:            void readWriteDuplicates() throws Exception {
2513:
2514:                writeRunner.run(new TransactionWorker() {
2515:                    public void doWork() throws Exception {
2516:                        if (index == null) {
2517:                            readWritePrimaryDuplicates(beginKey);
2518:                            readWritePrimaryDuplicates(beginKey + 1);
2519:                            readWritePrimaryDuplicates(endKey);
2520:                            readWritePrimaryDuplicates(endKey - 1);
2521:                        } else {
2522:                            readWriteIndexedDuplicates(beginKey);
2523:                            readWriteIndexedDuplicates(beginKey + 1);
2524:                            readWriteIndexedDuplicates(endKey);
2525:                            readWriteIndexedDuplicates(endKey - 1);
2526:                        }
2527:                    }
2528:                });
2529:            }
2530:
2531:            void readWritePrimaryDuplicates(int i) throws Exception {
2532:
2533:                Collection dups;
2534:                // make duplicate values
2535:                final Long key = makeKey(i);
2536:                final Object[] values = new Object[5];
2537:                for (int j = 0; j < values.length; j += 1) {
2538:                    values[j] = isEntityBinding ? makeEntity(i, i + j)
2539:                            : makeVal(i + j);
2540:                }
2541:                // add duplicates
2542:                outerLoop: for (int writeMode = 0;; writeMode += 1) {
2543:                    //System.out.println("write mode " + writeMode);
2544:                    switch (writeMode) {
2545:                    case 0:
2546:                    case 1: {
2547:                        // write with Map.put()
2548:                        for (int j = 1; j < values.length; j += 1) {
2549:                            map.put(key, values[j]);
2550:                        }
2551:                        break;
2552:                    }
2553:                    case 2: {
2554:                        // write with Map.duplicates().add()
2555:                        dups = map.duplicates(key);
2556:                        for (int j = 1; j < values.length; j += 1) {
2557:                            dups.add(values[j]);
2558:                        }
2559:                        break;
2560:                    }
2561:                    case 3: {
2562:                        // write with Map.duplicates().iterator().add()
2563:                        writeIterRunner.run(new TransactionWorker() {
2564:                            public void doWork() throws Exception {
2565:                                Collection dups = map.duplicates(key);
2566:                                Iterator iter = iterator(dups);
2567:                                assertEquals(values[0], iter.next());
2568:                                assertTrue(!iter.hasNext());
2569:                                try {
2570:                                    for (int j = 1; j < values.length; j += 1) {
2571:                                        ((ListIterator) iter).add(values[j]);
2572:                                    }
2573:                                } finally {
2574:                                    StoredIterator.close(iter);
2575:                                }
2576:                            }
2577:                        });
2578:                        break;
2579:                    }
2580:                    case 4: {
2581:                        // write with Map.values().add()
2582:                        if (!isEntityBinding) {
2583:                            continue;
2584:                        }
2585:                        Collection set = map.values();
2586:                        for (int j = 1; j < values.length; j += 1) {
2587:                            set.add(values[j]);
2588:                        }
2589:                        break;
2590:                    }
2591:                    default: {
2592:                        break outerLoop;
2593:                    }
2594:                    }
2595:                    checkDupsSize(values.length, map.duplicates(key));
2596:                    // read duplicates
2597:                    readDuplicates(i, key, values);
2598:                    // remove duplicates
2599:                    switch (writeMode) {
2600:                    case 0: {
2601:                        // remove with Map.remove()
2602:                        checkDupsSize(values.length, map.duplicates(key));
2603:                        map.remove(key); // remove all values
2604:                        checkDupsSize(0, map.duplicates(key));
2605:                        map.put(key, values[0]); // put back original value
2606:                        checkDupsSize(1, map.duplicates(key));
2607:                        break;
2608:                    }
2609:                    case 1: {
2610:                        // remove with Map.keySet().remove()
2611:                        map.keySet().remove(key); // remove all values
2612:                        map.put(key, values[0]); // put back original value
2613:                        break;
2614:                    }
2615:                    case 2: {
2616:                        // remove with Map.duplicates().clear()
2617:                        dups = map.duplicates(key);
2618:                        dups.clear(); // remove all values
2619:                        dups.add(values[0]); // put back original value
2620:                        break;
2621:                    }
2622:                    case 3: {
2623:                        // remove with Map.duplicates().iterator().remove()
2624:                        writeIterRunner.run(new TransactionWorker() {
2625:                            public void doWork() throws Exception {
2626:                                Collection dups = map.duplicates(key);
2627:                                Iterator iter = iterator(dups);
2628:                                try {
2629:                                    for (int j = 0; j < values.length; j += 1) {
2630:                                        assertEquals(values[j], iter.next());
2631:                                        if (j != 0) {
2632:                                            iter.remove();
2633:                                        }
2634:                                    }
2635:                                } finally {
2636:                                    StoredIterator.close(iter);
2637:                                }
2638:                            }
2639:                        });
2640:                        break;
2641:                    }
2642:                    case 4: {
2643:                        // remove with Map.values().remove()
2644:                        if (!isEntityBinding) {
2645:                            throw new IllegalStateException();
2646:                        }
2647:                        Collection set = map.values();
2648:                        for (int j = 1; j < values.length; j += 1) {
2649:                            set.remove(values[j]);
2650:                        }
2651:                        break;
2652:                    }
2653:                    default:
2654:                        throw new IllegalStateException();
2655:                    }
2656:                    // verify that only original value is present
2657:                    dups = map.duplicates(key);
2658:                    assertTrue(dups.contains(values[0]));
2659:                    for (int j = 1; j < values.length; j += 1) {
2660:                        assertTrue(!dups.contains(values[j]));
2661:                    }
2662:                    checkDupsSize(1, dups);
2663:                }
2664:            }
2665:
2666:            void readWriteIndexedDuplicates(int i) throws Exception {
2667:
2668:                Object key = makeKey(i);
2669:                Object[] values = new Object[3];
2670:                values[0] = makeVal(i);
2671:                for (int j = 1; j < values.length; j += 1) {
2672:                    values[j] = isEntityBinding ? makeEntity(endKey + j, i)
2673:                            : makeVal(i);
2674:                }
2675:                // add duplicates
2676:                for (int j = 1; j < values.length; j += 1) {
2677:                    imap.put(makeKey(endKey + j), values[j]);
2678:                }
2679:                // read duplicates
2680:                readDuplicates(i, key, values);
2681:                // remove duplicates
2682:                for (int j = 1; j < values.length; j += 1) {
2683:                    imap.remove(makeKey(endKey + j));
2684:                }
2685:                checkDupsSize(1, map.duplicates(key));
2686:            }
2687:
2688:            void readDuplicates(int i, Object key, Object[] values) {
2689:
2690:                boolean isOrdered = map.isOrdered();
2691:                Collection dups;
2692:                Iterator iter;
2693:                // read with Map.duplicates().iterator()
2694:                dups = map.duplicates(key);
2695:                checkDupsSize(values.length, dups);
2696:                iter = iterator(dups);
2697:                try {
2698:                    for (int j = 0; j < values.length; j += 1) {
2699:                        assertTrue(iter.hasNext());
2700:                        Object val = iter.next();
2701:                        assertEquals(values[j], val);
2702:                    }
2703:                    assertTrue(!iter.hasNext());
2704:                } finally {
2705:                    StoredIterator.close(iter);
2706:                }
2707:                // read with Map.values().iterator()
2708:                Collection clone = ((StoredCollection) map.values()).toList();
2709:                iter = iterator(map.values());
2710:                try {
2711:                    for (int j = beginKey; j < i; j += 1) {
2712:                        Object val = iter.next();
2713:                        assertTrue(clone.remove(makeVal(j)));
2714:                        if (isOrdered) {
2715:                            assertEquals(makeVal(j), val);
2716:                        }
2717:                    }
2718:                    for (int j = 0; j < values.length; j += 1) {
2719:                        Object val = iter.next();
2720:                        assertTrue(clone.remove(values[j]));
2721:                        if (isOrdered) {
2722:                            assertEquals(values[j], val);
2723:                        }
2724:                    }
2725:                    for (int j = i + 1; j <= endKey; j += 1) {
2726:                        Object val = iter.next();
2727:                        assertTrue(clone.remove(makeVal(j)));
2728:                        if (isOrdered) {
2729:                            assertEquals(makeVal(j), val);
2730:                        }
2731:                    }
2732:                    assertTrue(!iter.hasNext());
2733:                    assertTrue(clone.isEmpty());
2734:                } finally {
2735:                    StoredIterator.close(iter);
2736:                }
2737:                // read with Map.entrySet().iterator()
2738:                clone = ((StoredCollection) map.entrySet()).toList();
2739:                iter = iterator(map.entrySet());
2740:                try {
2741:                    for (int j = beginKey; j < i; j += 1) {
2742:                        Map.Entry entry = (Map.Entry) iter.next();
2743:                        assertTrue(clone.remove(mapEntry(j)));
2744:                        if (isOrdered) {
2745:                            assertEquals(makeVal(j), entry.getValue());
2746:                            assertEquals(makeKey(j), entry.getKey());
2747:                        }
2748:                    }
2749:                    for (int j = 0; j < values.length; j += 1) {
2750:                        Map.Entry entry = (Map.Entry) iter.next();
2751:                        assertTrue(clone
2752:                                .remove(mapEntry(makeKey(i), values[j])));
2753:                        if (isOrdered) {
2754:                            assertEquals(values[j], entry.getValue());
2755:                            assertEquals(makeKey(i), entry.getKey());
2756:                        }
2757:                    }
2758:                    for (int j = i + 1; j <= endKey; j += 1) {
2759:                        Map.Entry entry = (Map.Entry) iter.next();
2760:                        assertTrue(clone.remove(mapEntry(j)));
2761:                        if (isOrdered) {
2762:                            assertEquals(makeVal(j), entry.getValue());
2763:                            assertEquals(makeKey(j), entry.getKey());
2764:                        }
2765:                    }
2766:                    assertTrue(!iter.hasNext());
2767:                    assertTrue(clone.isEmpty());
2768:                } finally {
2769:                    StoredIterator.close(iter);
2770:                }
2771:                // read with Map.keySet().iterator()
2772:                clone = ((StoredCollection) map.keySet()).toList();
2773:                iter = iterator(map.keySet());
2774:                try {
2775:                    for (int j = beginKey; j < i; j += 1) {
2776:                        Object val = iter.next();
2777:                        assertTrue(clone.remove(makeKey(j)));
2778:                        if (isOrdered) {
2779:                            assertEquals(makeKey(j), val);
2780:                        }
2781:                    }
2782:                    if (true) {
2783:                        // only one key is iterated for all duplicates
2784:                        Object val = iter.next();
2785:                        assertTrue(clone.remove(makeKey(i)));
2786:                        if (isOrdered) {
2787:                            assertEquals(makeKey(i), val);
2788:                        }
2789:                    }
2790:                    for (int j = i + 1; j <= endKey; j += 1) {
2791:                        Object val = iter.next();
2792:                        assertTrue(clone.remove(makeKey(j)));
2793:                        if (isOrdered) {
2794:                            assertEquals(makeKey(j), val);
2795:                        }
2796:                    }
2797:                    assertTrue(!iter.hasNext());
2798:                    assertTrue(clone.isEmpty());
2799:                } finally {
2800:                    StoredIterator.close(iter);
2801:                }
2802:            }
2803:
2804:            void duplicatesNotAllowed() {
2805:
2806:                Collection dups = map.duplicates(makeKey(beginKey));
2807:                try {
2808:                    dups.add(makeVal(beginKey));
2809:                    fail();
2810:                } catch (UnsupportedOperationException expected) {
2811:                }
2812:                ListIterator iter = (ListIterator) iterator(dups);
2813:                try {
2814:                    iter.add(makeVal(beginKey));
2815:                    fail();
2816:                } catch (UnsupportedOperationException expected) {
2817:                } finally {
2818:                    StoredIterator.close(iter);
2819:                }
2820:            }
2821:
2822:            void listOperationsNotAllowed() {
2823:
2824:                ListIterator iter = (ListIterator) iterator(map.values());
2825:                try {
2826:                    try {
2827:                        iter.nextIndex();
2828:                        fail();
2829:                    } catch (UnsupportedOperationException expected) {
2830:                    }
2831:                    try {
2832:                        iter.previousIndex();
2833:                        fail();
2834:                    } catch (UnsupportedOperationException expected) {
2835:                    }
2836:                } finally {
2837:                    StoredIterator.close(iter);
2838:                }
2839:            }
2840:
2841:            void testCdbLocking() {
2842:
2843:                Iterator readIterator;
2844:                Iterator writeIterator;
2845:                StoredKeySet set = (StoredKeySet) map.keySet();
2846:
2847:                // can open two CDB read cursors
2848:                readIterator = set.storedIterator(false);
2849:                try {
2850:                    Iterator readIterator2 = set.storedIterator(false);
2851:                    StoredIterator.close(readIterator2);
2852:                } finally {
2853:                    StoredIterator.close(readIterator);
2854:                }
2855:
2856:                // can open two CDB write cursors
2857:                writeIterator = set.storedIterator(true);
2858:                try {
2859:                    Iterator writeIterator2 = set.storedIterator(true);
2860:                    StoredIterator.close(writeIterator2);
2861:                } finally {
2862:                    StoredIterator.close(writeIterator);
2863:                }
2864:
2865:                // cannot open CDB write cursor when read cursor is open,
2866:                readIterator = set.storedIterator(false);
2867:                try {
2868:                    writeIterator = set.storedIterator(true);
2869:                    fail();
2870:                    StoredIterator.close(writeIterator);
2871:                } catch (IllegalStateException e) {
2872:                } finally {
2873:                    StoredIterator.close(readIterator);
2874:                }
2875:
2876:                if (index == null) {
2877:                    // cannot put() with read cursor open
2878:                    readIterator = set.storedIterator(false);
2879:                    try {
2880:                        map.put(makeKey(1), makeVal(1));
2881:                        fail();
2882:                    } catch (IllegalStateException e) {
2883:                    } finally {
2884:                        StoredIterator.close(readIterator);
2885:                    }
2886:
2887:                    // cannot append() with write cursor open with RECNO/QUEUE only
2888:                    writeIterator = set.storedIterator(true);
2889:                    try {
2890:                        if (testStore.isQueueOrRecno()) {
2891:                            try {
2892:                                map.append(makeVal(1));
2893:                                fail();
2894:                            } catch (IllegalStateException e) {
2895:                            }
2896:                        } else {
2897:                            map.append(makeVal(1));
2898:                        }
2899:                    } finally {
2900:                        StoredIterator.close(writeIterator);
2901:                    }
2902:                }
2903:            }
2904:
2905:            Object makeVal(int key) {
2906:
2907:                if (isEntityBinding) {
2908:                    return makeEntity(key);
2909:                } else {
2910:                    return new Long(key + 100);
2911:                }
2912:            }
2913:
2914:            Object makeVal(int key, int val) {
2915:
2916:                if (isEntityBinding) {
2917:                    return makeEntity(key, val);
2918:                } else {
2919:                    return makeVal(val);
2920:                }
2921:            }
2922:
2923:            Object makeEntity(int key, int val) {
2924:
2925:                return new TestEntity(key, val + 100);
2926:            }
2927:
2928:            int intVal(Object val) {
2929:
2930:                if (isEntityBinding) {
2931:                    return ((TestEntity) val).value - 100;
2932:                } else {
2933:                    return ((Long) val).intValue() - 100;
2934:                }
2935:            }
2936:
2937:            int intKey(Object key) {
2938:
2939:                return ((Long) key).intValue();
2940:            }
2941:
2942:            Object makeVal(Long key) {
2943:
2944:                return makeVal(key.intValue());
2945:            }
2946:
2947:            Object makeEntity(int key) {
2948:
2949:                return makeEntity(key, key);
2950:            }
2951:
2952:            Object makeEntity(Long key) {
2953:
2954:                return makeEntity(key.intValue());
2955:            }
2956:
2957:            int intIter(Collection coll, Object value) {
2958:
2959:                if (coll instanceof  StoredKeySet) {
2960:                    return intKey(value);
2961:                } else {
2962:                    if (coll instanceof  StoredEntrySet) {
2963:                        value = ((Map.Entry) value).getValue();
2964:                    }
2965:                    return intVal(value);
2966:                }
2967:            }
2968:
2969:            Map.Entry mapEntry(Object key, Object val) {
2970:
2971:                return new MapEntryParameter(key, val);
2972:            }
2973:
2974:            Map.Entry mapEntry(int key) {
2975:
2976:                return new MapEntryParameter(makeKey(key), makeVal(key));
2977:            }
2978:
2979:            Long makeKey(int key) {
2980:
2981:                return new Long(key);
2982:            }
2983:
2984:            boolean isSubMap() {
2985:
2986:                return rangeType != NONE;
2987:            }
2988:
2989:            void checkDupsSize(int expected, Collection coll) {
2990:
2991:                assertEquals(expected, coll.size());
2992:                if (coll instanceof  StoredCollection) {
2993:                    StoredIterator i = ((StoredCollection) coll)
2994:                            .storedIterator(false);
2995:                    try {
2996:                        int actual = 0;
2997:                        if (i.hasNext()) {
2998:                            i.next();
2999:                            actual = i.count();
3000:                        }
3001:                        assertEquals(expected, actual);
3002:                    } finally {
3003:                        StoredIterator.close(i);
3004:                    }
3005:                }
3006:            }
3007:
3008:            private boolean isListAddAllowed() {
3009:
3010:                return list != null && testStore.isQueueOrRecno()
3011:                        && list.areKeysRenumbered();
3012:            }
3013:
3014:            private int countElements(Collection coll) {
3015:
3016:                int count = 0;
3017:                Iterator iter = iterator(coll);
3018:                try {
3019:                    while (iter.hasNext()) {
3020:                        iter.next();
3021:                        count += 1;
3022:                    }
3023:                } finally {
3024:                    StoredIterator.close(iter);
3025:                }
3026:                return count;
3027:            }
3028:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.