001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2000,2008 Oracle. All rights reserved.
005: *
006: * $Id: TupleSerialFactoryTest.java,v 1.39.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.StoredClassCatalog;
017: import com.sleepycat.bind.serial.test.MarshalledObject;
018: import com.sleepycat.collections.TransactionRunner;
019: import com.sleepycat.collections.TransactionWorker;
020: import com.sleepycat.collections.TupleSerialFactory;
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: import com.sleepycat.je.ForeignKeyDeleteAction;
028: import com.sleepycat.je.SecondaryConfig;
029: import com.sleepycat.je.SecondaryDatabase;
030:
031: /**
032: * @author Mark Hayes
033: */
034: public class TupleSerialFactoryTest extends TestCase implements
035: TransactionWorker {
036:
037: public static void main(String[] args) throws Exception {
038:
039: junit.framework.TestResult tr = junit.textui.TestRunner
040: .run(suite());
041: if (tr.errorCount() > 0 || tr.failureCount() > 0) {
042: System.exit(1);
043: } else {
044: System.exit(0);
045: }
046: }
047:
048: public static Test suite() throws Exception {
049:
050: TestSuite suite = new TestSuite();
051: for (int i = 0; i < TestEnv.ALL.length; i += 1) {
052: for (int sorted = 0; sorted < 2; sorted += 1) {
053: suite.addTest(new TupleSerialFactoryTest(
054: TestEnv.ALL[i], sorted != 0));
055: }
056: }
057: return suite;
058: }
059:
060: private TestEnv testEnv;
061: private Environment env;
062: private StoredClassCatalog catalog;
063: private TransactionRunner runner;
064: private TupleSerialFactory factory;
065: private Database store1;
066: private Database store2;
067: private SecondaryDatabase index1;
068: private SecondaryDatabase index2;
069: private boolean isSorted;
070: private Map storeMap1;
071: private Map storeMap2;
072: private Map indexMap1;
073: private Map indexMap2;
074:
075: public TupleSerialFactoryTest(TestEnv testEnv, boolean isSorted) {
076:
077: super (null);
078:
079: this .testEnv = testEnv;
080: this .isSorted = isSorted;
081:
082: String name = "TupleSerialFactoryTest-" + testEnv.getName();
083: name += isSorted ? "-sorted" : "-unsorted";
084: setName(name);
085: }
086:
087: public void setUp() throws Exception {
088:
089: DbTestUtil.printTestName(getName());
090: env = testEnv.open(getName());
091: runner = new TransactionRunner(env);
092:
093: createDatabase();
094: }
095:
096: public void tearDown() {
097:
098: try {
099: if (index1 != null) {
100: index1.close();
101: }
102: if (index2 != null) {
103: index2.close();
104: }
105: if (store1 != null) {
106: store1.close();
107: }
108: if (store2 != null) {
109: store2.close();
110: }
111: if (catalog != null) {
112: catalog.close();
113: }
114: if (env != null) {
115: env.close();
116: }
117: } catch (Exception e) {
118: System.out.println("Ignored exception during tearDown: "
119: + e);
120: } finally {
121: /* Ensure that GC can cleanup. */
122: index1 = null;
123: index2 = null;
124: store1 = null;
125: store2 = null;
126: catalog = null;
127: env = null;
128: testEnv = null;
129: runner = null;
130: factory = null;
131: storeMap1 = null;
132: storeMap2 = null;
133: indexMap1 = null;
134: indexMap2 = null;
135: }
136: }
137:
138: public void runTest() throws Exception {
139:
140: runner.run(this );
141: }
142:
143: public void doWork() throws Exception {
144:
145: createViews();
146: writeAndRead();
147: }
148:
149: private void createDatabase() throws Exception {
150:
151: catalog = new StoredClassCatalog(openDb("catalog.db"));
152: factory = new TupleSerialFactory(catalog);
153: assertSame(catalog, factory.getCatalog());
154:
155: store1 = openDb("store1.db");
156: store2 = openDb("store2.db");
157: index1 = openSecondaryDb(factory, "1", store1, "index1.db",
158: null);
159: index2 = openSecondaryDb(factory, "2", store2, "index2.db",
160: store1);
161: }
162:
163: private Database openDb(String file) throws Exception {
164:
165: DatabaseConfig config = new DatabaseConfig();
166: config.setTransactional(testEnv.isTxnMode());
167: config.setAllowCreate(true);
168:
169: return DbCompat.openDatabase(env, null, file, null, config);
170: }
171:
172: private SecondaryDatabase openSecondaryDb(
173: TupleSerialFactory factory, String keyName,
174: Database primary, String file, Database foreignStore)
175: throws Exception {
176:
177: SecondaryConfig secConfig = new SecondaryConfig();
178: secConfig.setTransactional(testEnv.isTxnMode());
179: secConfig.setAllowCreate(true);
180: secConfig.setKeyCreator(factory.getKeyCreator(
181: MarshalledObject.class, keyName));
182: if (foreignStore != null) {
183: secConfig.setForeignKeyDatabase(foreignStore);
184: secConfig
185: .setForeignKeyDeleteAction(ForeignKeyDeleteAction.CASCADE);
186: }
187:
188: return DbCompat.openSecondaryDatabase(env, null, file, null,
189: primary, secConfig);
190: }
191:
192: private void createViews() throws Exception {
193:
194: if (isSorted) {
195: storeMap1 = factory.newSortedMap(store1, String.class,
196: MarshalledObject.class, true);
197: storeMap2 = factory.newSortedMap(store2, String.class,
198: MarshalledObject.class, true);
199: indexMap1 = factory.newSortedMap(index1, String.class,
200: MarshalledObject.class, true);
201: indexMap2 = factory.newSortedMap(index2, String.class,
202: MarshalledObject.class, true);
203: } else {
204: storeMap1 = factory.newMap(store1, String.class,
205: MarshalledObject.class, true);
206: storeMap2 = factory.newMap(store2, String.class,
207: MarshalledObject.class, true);
208: indexMap1 = factory.newMap(index1, String.class,
209: MarshalledObject.class, true);
210: indexMap2 = factory.newMap(index2, String.class,
211: MarshalledObject.class, true);
212: }
213: }
214:
215: private void writeAndRead() throws Exception {
216:
217: MarshalledObject o1 = new MarshalledObject("data1", "pk1",
218: "ik1", "");
219: assertNull(storeMap1.put(null, o1));
220:
221: assertEquals(o1, storeMap1.get("pk1"));
222: assertEquals(o1, indexMap1.get("ik1"));
223:
224: MarshalledObject o2 = new MarshalledObject("data2", "pk2", "",
225: "pk1");
226: assertNull(storeMap2.put(null, o2));
227:
228: assertEquals(o2, storeMap2.get("pk2"));
229: assertEquals(o2, indexMap2.get("pk1"));
230:
231: /*
232: * store1 contains o1 with primary key "pk1" and index key "ik1"
233: * store2 contains o2 with primary key "pk2" and foreign key "pk1"
234: * which is the primary key of store1
235: */
236:
237: storeMap1.remove("pk1");
238: assertNull(storeMap1.get("pk1"));
239: assertNull(indexMap1.get("ik1"));
240: assertNull(storeMap2.get("pk2"));
241: assertNull(indexMap2.get("pk1"));
242: }
243: }
|