KickJava   Java API By Example, From Geeks To Geeks.

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


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

8 package com.sleepycat.collections.test.serial;
9
10 import java.util.Map JavaDoc;
11
12 import junit.framework.Test;
13 import junit.framework.TestCase;
14 import junit.framework.TestSuite;
15
16 import com.sleepycat.bind.serial.SerialBinding;
17 import com.sleepycat.bind.serial.StoredClassCatalog;
18 import com.sleepycat.collections.StoredMap;
19 import com.sleepycat.collections.TransactionRunner;
20 import com.sleepycat.collections.TransactionWorker;
21 import com.sleepycat.collections.test.DbTestUtil;
22 import com.sleepycat.collections.test.TestEnv;
23 import com.sleepycat.compat.DbCompat;
24 import com.sleepycat.je.Database;
25 import com.sleepycat.je.DatabaseConfig;
26 import com.sleepycat.je.Environment;
27
28 /**
29  * Runs part one of the StoredClassCatalogTest. This part is run with the
30  * old/original version of TestSerial in the classpath. It creates a fresh
31  * environment and databases containing serialized versions of the old class.
32  * When StoredClassCatalogTest is run, it will read these objects from the
33  * database created here.
34  *
35  * @author Mark Hayes
36  */

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