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