KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > collections > test > serial > StoredClassCatalogTest


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

8 package com.sleepycat.collections.test.serial;
9
10 import java.io.ObjectStreamClass JavaDoc;
11 import java.util.Map JavaDoc;
12
13 import junit.framework.Test;
14 import junit.framework.TestCase;
15 import junit.framework.TestSuite;
16
17 import com.sleepycat.bind.serial.SerialBinding;
18 import com.sleepycat.bind.serial.StoredClassCatalog;
19 import com.sleepycat.collections.StoredMap;
20 import com.sleepycat.collections.TransactionRunner;
21 import com.sleepycat.collections.TransactionWorker;
22 import com.sleepycat.collections.test.DbTestUtil;
23 import com.sleepycat.collections.test.TestEnv;
24 import com.sleepycat.compat.DbCompat;
25 import com.sleepycat.je.Database;
26 import com.sleepycat.je.DatabaseConfig;
27 import com.sleepycat.je.Environment;
28
29 /**
30  * Runs part two of the StoredClassCatalogTest. This part is run with the
31  * new/updated version of TestSerial in the classpath. It uses the
32  * environment and databases created by StoredClassCatalogTestInit. It
33  * verifies that it can read objects serialized using the old class format,
34  * and that it can create new objects with the new class format.
35  *
36  * @author Mark Hayes
37  */

38 public class StoredClassCatalogTest extends TestCase
39     implements TransactionWorker {
40
41     static final String JavaDoc CATALOG_FILE = "catalogtest-catalog.db";
42     static final String JavaDoc STORE_FILE = "catalogtest-store.db";
43
44     public static void main(String JavaDoc[] args)
45         throws Exception JavaDoc {
46
47         junit.framework.TestResult tr =
48             junit.textui.TestRunner.run(suite());
49         if (tr.errorCount() > 0 ||
50             tr.failureCount() > 0) {
51             System.exit(1);
52         } else {
53             System.exit(0);
54         }
55     }
56
57     public static Test suite()
58         throws Exception JavaDoc {
59
60         TestSuite suite = new TestSuite();
61         for (int i = 0; i < TestEnv.ALL.length; i += 1) {
62             suite.addTest(new StoredClassCatalogTest(TestEnv.ALL[i]));
63         }
64         return suite;
65     }
66
67     private TestEnv testEnv;
68     private Environment env;
69     private StoredClassCatalog catalog;
70     private StoredClassCatalog catalog2;
71     private Database store;
72     private Map JavaDoc map;
73     private TransactionRunner runner;
74
75     public StoredClassCatalogTest(TestEnv testEnv) {
76
77         super(makeTestName(testEnv));
78         this.testEnv = testEnv;
79     }
80
81     static String JavaDoc makeTestName(TestEnv testEnv) {
82         return "StoredClassCatalogTest-" + testEnv.getName();
83     }
84
85     public void setUp()
86         throws Exception JavaDoc {
87
88         DbTestUtil.printTestName(getName());
89         env = testEnv.open(makeTestName(testEnv), false);
90         runner = new TransactionRunner(env);
91
92         catalog = new StoredClassCatalog(openDb(CATALOG_FILE, false));
93         catalog2 = new StoredClassCatalog(openDb("catalog2.db", true));
94
95         SerialBinding keyBinding = new SerialBinding(catalog,
96                                                   String JavaDoc.class);
97         SerialBinding valueBinding = new SerialBinding(catalog,
98                                                     TestSerial.class);
99         store = openDb(STORE_FILE, false);
100
101         map = new StoredMap(store, keyBinding, valueBinding, true);
102     }
103
104     private Database openDb(String JavaDoc file, boolean create)
105         throws Exception JavaDoc {
106
107         DatabaseConfig config = new DatabaseConfig();
108         DbCompat.setTypeBtree(config);
109         config.setTransactional(testEnv.isTxnMode());
110         config.setAllowCreate(create);
111
112         return DbCompat.openDatabase(env, null, file, null, config);
113     }
114
115     public void tearDown() {
116
117         try {
118             if (catalog != null) {
119                 catalog.close();
120                 catalog.close(); // should have no effect
121
}
122             if (catalog2 != null) {
123                 catalog2.close();
124             }
125             if (store != null) {
126                 store.close();
127             }
128             if (env != null) {
129                 env.close();
130             }
131         } catch (Exception JavaDoc e) {
132             System.err.println("Ignored exception during tearDown: ");
133             e.printStackTrace();
134         } finally {
135             /* Ensure that GC can cleanup. */
136             catalog = null;
137             catalog2 = null;
138             store = null;
139             env = null;
140             testEnv = null;
141             map = null;
142             runner = null;
143         }
144     }
145
146     public void runTest()
147         throws Exception JavaDoc {
148
149         runner.run(this);
150     }
151
152     public void doWork()
153         throws Exception JavaDoc {
154
155         TestSerial one = (TestSerial) map.get("one");
156         TestSerial two = (TestSerial) map.get("two");
157         assertNotNull(one);
158         assertNotNull(two);
159         assertEquals(one, two.getOther());
160         assertNull(one.getStringField());
161         assertNull(two.getStringField());
162
163         TestSerial three = new TestSerial(two);
164         assertNotNull(three.getStringField());
165         map.put("three", three);
166         three = (TestSerial) map.get("three");
167         assertEquals(two, three.getOther());
168
169         ObjectStreamClass JavaDoc desc = ObjectStreamClass.lookup(TestSerial.class);
170
171         assertNotNull(catalog.getClassID(desc));
172         assertNotNull(catalog.getClassID(desc));
173
174         // test with empty catalog
175
assertNotNull(catalog2.getClassID(desc));
176         assertNotNull(catalog2.getClassID(desc));
177     }
178 }
179
Popular Tags