001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2000,2008 Oracle. All rights reserved.
005: *
006: * $Id: StoredClassCatalogTestInit.java,v 1.25.2.2 2008/01/07 15:14:24 cwl Exp $
007: */
008: package com.sleepycat.collections.test.serial;
009:
010: import java.util.Map;
011:
012: import junit.framework.Test;
013: import junit.framework.TestCase;
014: import junit.framework.TestSuite;
015:
016: import com.sleepycat.bind.serial.SerialBinding;
017: import com.sleepycat.bind.serial.StoredClassCatalog;
018: import com.sleepycat.collections.StoredMap;
019: import com.sleepycat.collections.TransactionRunner;
020: import com.sleepycat.collections.TransactionWorker;
021: import com.sleepycat.collections.test.DbTestUtil;
022: import com.sleepycat.collections.test.TestEnv;
023: import com.sleepycat.compat.DbCompat;
024: import com.sleepycat.je.Database;
025: import com.sleepycat.je.DatabaseConfig;
026: import com.sleepycat.je.Environment;
027:
028: /**
029: * Runs part one of the StoredClassCatalogTest. This part is run with the
030: * old/original version of TestSerial in the classpath. It creates a fresh
031: * environment and databases containing serialized versions of the old class.
032: * When StoredClassCatalogTest is run, it will read these objects from the
033: * database created here.
034: *
035: * @author Mark Hayes
036: */
037: public class StoredClassCatalogTestInit extends TestCase implements
038: TransactionWorker {
039:
040: static final String CATALOG_FILE = StoredClassCatalogTest.CATALOG_FILE;
041: static final String STORE_FILE = StoredClassCatalogTest.STORE_FILE;
042:
043: public static void main(String[] args) throws Exception {
044:
045: junit.framework.TestResult tr = junit.textui.TestRunner
046: .run(suite());
047: if (tr.errorCount() > 0 || tr.failureCount() > 0) {
048: System.exit(1);
049: } else {
050: System.exit(0);
051: }
052: }
053:
054: public static Test suite() throws Exception {
055:
056: TestSuite suite = new TestSuite();
057: for (int i = 0; i < TestEnv.ALL.length; i += 1) {
058: suite
059: .addTest(new StoredClassCatalogTestInit(
060: TestEnv.ALL[i]));
061: }
062: return suite;
063: }
064:
065: private TestEnv testEnv;
066: private Environment env;
067: private StoredClassCatalog catalog;
068: private Database store;
069: private Map map;
070: private TransactionRunner runner;
071:
072: public StoredClassCatalogTestInit(TestEnv testEnv) {
073:
074: super ("StoredClassCatalogTestInit-" + testEnv.getName());
075: this .testEnv = testEnv;
076: }
077:
078: public void setUp() throws Exception {
079:
080: DbTestUtil.printTestName(getName());
081: env = testEnv
082: .open(StoredClassCatalogTest.makeTestName(testEnv));
083: runner = new TransactionRunner(env);
084:
085: catalog = new StoredClassCatalog(openDb(CATALOG_FILE));
086:
087: SerialBinding keyBinding = new SerialBinding(catalog,
088: String.class);
089: SerialBinding valueBinding = new SerialBinding(catalog,
090: TestSerial.class);
091: store = openDb(STORE_FILE);
092:
093: map = new StoredMap(store, keyBinding, valueBinding, true);
094: }
095:
096: private Database openDb(String file) throws Exception {
097:
098: DatabaseConfig config = new DatabaseConfig();
099: DbCompat.setTypeBtree(config);
100: config.setTransactional(testEnv.isTxnMode());
101: config.setAllowCreate(true);
102:
103: return DbCompat.openDatabase(env, null, file, null, config);
104: }
105:
106: public void tearDown() {
107:
108: try {
109: if (catalog != null) {
110: catalog.close();
111: catalog.close(); // should have no effect
112: }
113: if (store != null) {
114: store.close();
115: }
116: if (env != null) {
117: env.close();
118: }
119: } catch (Exception e) {
120: System.err.println("Ignored exception during tearDown: ");
121: e.printStackTrace();
122: } finally {
123: /* Ensure that GC can cleanup. */
124: catalog = null;
125: store = null;
126: env = null;
127: testEnv = null;
128: map = null;
129: runner = null;
130: }
131: }
132:
133: public void runTest() throws Exception {
134:
135: runner.run(this );
136: }
137:
138: public void doWork() throws Exception {
139:
140: TestSerial one = new TestSerial(null);
141: TestSerial two = new TestSerial(one);
142: assertNull(
143: "Likely the classpath contains the wrong version of the"
144: + " TestSerial class, the 'original' version is required",
145: one.getStringField());
146: assertNull(two.getStringField());
147: map.put("one", one);
148: map.put("two", two);
149: one = (TestSerial) map.get("one");
150: two = (TestSerial) map.get("two");
151: assertEquals(one, two.getOther());
152: assertNull(one.getStringField());
153: assertNull(two.getStringField());
154: }
155: }
|