📄 collectiontest.java
字号:
/*- * See the file LICENSE for redistribution information. * * Copyright (c) 2002-2004 * Sleepycat Software. All rights reserved. * * $Id: CollectionTest.java,v 1.3 2004/09/22 18:01:06 bostic Exp $ */package com.sleepycat.collections.test;import java.util.ArrayList;import java.util.Collection;import java.util.Collections;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.ListIterator;import java.util.Map;import java.util.NoSuchElementException;import java.util.Set;import java.util.SortedMap;import java.util.SortedSet;import junit.framework.Test;import junit.framework.TestCase;import junit.framework.TestSuite;import com.sleepycat.bind.EntityBinding;import com.sleepycat.bind.EntryBinding;import com.sleepycat.collections.CurrentTransaction;import com.sleepycat.collections.MapEntryParameter;import com.sleepycat.collections.StoredCollection;import com.sleepycat.collections.StoredCollections;import com.sleepycat.collections.StoredContainer;import com.sleepycat.collections.StoredEntrySet;import com.sleepycat.collections.StoredIterator;import com.sleepycat.collections.StoredKeySet;import com.sleepycat.collections.StoredList;import com.sleepycat.collections.StoredMap;import com.sleepycat.collections.StoredSortedEntrySet;import com.sleepycat.collections.StoredSortedKeySet;import com.sleepycat.collections.StoredSortedMap;import com.sleepycat.collections.StoredSortedValueSet;import com.sleepycat.collections.StoredValueSet;import com.sleepycat.collections.TransactionRunner;import com.sleepycat.collections.TransactionWorker;import com.sleepycat.compat.DbCompat;import com.sleepycat.db.Database;import com.sleepycat.db.DatabaseException;import com.sleepycat.db.Environment;import com.sleepycat.util.ExceptionUnwrapper;/** * @author Mark Hayes */public class CollectionTest extends TestCase { private static final int NONE = 0; private static final int SUB = 1; private static final int HEAD = 2; private static final int TAIL = 3; private static final int MAX_KEY = 6; // must be a multiple of 2 private int beginKey = 1; private int endKey = MAX_KEY; private Environment env; private CurrentTransaction currentTxn; private Database store; private Database index; private boolean isEntityBinding; private boolean isAutoCommit; private TestStore testStore; private String testName; private EntryBinding keyBinding; private EntryBinding valueBinding; private EntityBinding entityBinding; private TransactionRunner readRunner; private TransactionRunner writeRunner; private TransactionRunner writeIterRunner; private TestEnv testEnv; private StoredMap map; private StoredMap imap; // insertable map (primary store for indexed map) private StoredSortedMap smap; // sorted map (null or equal to map) private StoredMap saveMap; private StoredSortedMap saveSMap; private int rangeType; private StoredList list; private StoredList ilist; // insertable list (primary store for index list) private StoredList saveList; private StoredKeySet keySet; private StoredValueSet valueSet; /** * Runs a command line collection test. * @see #usage */ public static void main(String[] args) throws Exception { if (args.length == 1 && (args[0].equals("-h") || args[0].equals("-help"))) { usage(); } else { junit.framework.TestResult tr = junit.textui.TestRunner.run(suite(args)); if (tr.errorCount() > 0 || tr.failureCount() > 0) { System.exit(1); } else { System.exit(0); } } } private static void usage() { System.out.println( "Usage: java com.sleepycat.collections.test.CollectionTest\n" + " -h | -help\n" + " [testName]...\n" + " where testName has the format:\n" + " <env>-<store>-{entity|value}\n" + " <env> is:\n" + " bdb | cdb | txn\n" + " <store> is:\n" + " btree-uniq | btree-dup | btree-dupsort | btree-recnum |\n" + " hash-uniq | hash-dup | hash-dupsort |\n" + " queue | recno | recno-renum\n" + " For example: bdb-btree-uniq-entity\n" + " If no arguments are given then all tests are run."); System.exit(2); } public static Test suite() throws Exception { return suite(null); } static Test suite(String[] args) throws Exception { TestSuite suite = new TestSuite(); for (int i = 0; i < TestEnv.ALL.length; i += 1) { for (int j = 0; j < TestStore.ALL.length; j += 1) { for (int k = 0; k < 2; k += 1) { boolean entityBinding = (k != 0); addTest(args, suite, new CollectionTest( TestEnv.ALL[i], TestStore.ALL[j], entityBinding, false)); if (TestEnv.ALL[i] == TestEnv.TXN) { addTest(args, suite, new CollectionTest( TestEnv.ALL[i], TestStore.ALL[j], entityBinding, true)); } } } } return suite; } private static void addTest(String[] args, TestSuite suite, CollectionTest test) { if (args == null || args.length == 0) { suite.addTest(test); } else { for (int t = 0; t < args.length; t += 1) { if (args[t].equals(test.testName)) { suite.addTest(test); break; } } } } public CollectionTest(TestEnv testEnv, TestStore testStore, boolean isEntityBinding, boolean isAutoCommit) { super(null); this.testEnv = testEnv; this.testStore = testStore; this.isEntityBinding = isEntityBinding; this.isAutoCommit = isAutoCommit; keyBinding = testStore.getKeyBinding(); valueBinding = testStore.getValueBinding(); entityBinding = testStore.getEntityBinding(); testName = testEnv.getName() + '-' + testStore.getName() + (isEntityBinding ? "-entity" : "-value") + (isAutoCommit ? "-autocommit" : ""); setName(testName); } public void runTest() throws Exception { DbTestUtil.printTestName(DbTestUtil.qualifiedTestName(this)); try { env = testEnv.open(testName); currentTxn = CurrentTransaction.getInstance(env); // For testing auto-commit, use a normal (transactional) runner for // all reading and for writing via an iterator, and a do-nothing // runner for writing via collections; if auto-commit is tested, // the per-collection auto-commit property will be set elsewhere. // TransactionRunner normalRunner = new TransactionRunner(env); normalRunner.setAllowNestedTransactions( DbCompat.NESTED_TRANSACTIONS); TransactionRunner nullRunner = new NullTransactionRunner(env); readRunner = nullRunner; writeIterRunner = normalRunner; if (isAutoCommit) { writeRunner = nullRunner; } else { writeRunner = normalRunner; } store = testStore.open(env, "unindexed.db"); testUnindexed(); store.close(); store = null; TestStore indexOf = testStore.getIndexOf(); if (indexOf != null) { store = indexOf.open(env, "indexed.db"); index = testStore.openIndex(store, "index.db"); testIndexed(); index.close(); index = null; store.close(); store = null; } env.close(); env = null; } catch (Exception e) { throw ExceptionUnwrapper.unwrap(e); } finally { if (index != null) { try { index.close(); } catch (Exception e) { } } if (store != null) { try { store.close(); } catch (Exception e) { } } if (env != null) { try { env.close(); } catch (Exception e) { } } /* Ensure that GC can cleanup. */ index = null; store = null; env = null; currentTxn = null; readRunner = null; writeRunner = null; writeIterRunner = null; map = null; imap = null; smap = null; saveMap = null; saveSMap = null; list = null; ilist = null; saveList = null; keySet = null; valueSet = null; testEnv = null; testStore = null; } } void testCreation(StoredContainer cont) throws Exception { assertEquals(index != null, cont.isSecondary()); assertEquals(testStore.isOrdered(), cont.isOrdered()); assertEquals(testStore.areKeysRenumbered(), cont.areKeysRenumbered()); assertEquals(testStore.areDuplicatesAllowed(), cont.areDuplicatesAllowed()); assertEquals(testEnv == TestEnv.TXN, cont.isTransactional()); try { cont.size(); fail(); } catch (UnsupportedOperationException expected) {} } void testMapCreation(Map map) throws Exception { assertTrue(map.values() instanceof Set); assertEquals(testStore.isOrdered(), map.keySet() instanceof SortedSet); assertEquals(testStore.isOrdered(), map.entrySet() instanceof SortedSet); assertEquals(testStore.isOrdered() && isEntityBinding, map.values() instanceof SortedSet); } void testUnindexed() throws Exception { // create primary map if (testStore.isOrdered()) { if (isEntityBinding) { smap = new StoredSortedMap(store, keyBinding, entityBinding, testStore.getKeyAssigner()); valueSet = new StoredSortedValueSet(store, entityBinding, true); } else { smap = new StoredSortedMap(store, keyBinding, valueBinding, testStore.getKeyAssigner()); // sorted value set is not possible since key cannot be derived // for performing subSet, etc. } keySet = new StoredSortedKeySet(store, keyBinding, true); map = smap; } else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -