KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2000,2006 Oracle. All rights reserved.
5  *
6  * $Id: TupleSerialFactoryTest.java,v 1.39 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.StoredClassCatalog;
17 import com.sleepycat.bind.serial.test.MarshalledObject;
18 import com.sleepycat.collections.TransactionRunner;
19 import com.sleepycat.collections.TransactionWorker;
20 import com.sleepycat.collections.TupleSerialFactory;
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 import com.sleepycat.je.ForeignKeyDeleteAction;
28 import com.sleepycat.je.SecondaryConfig;
29 import com.sleepycat.je.SecondaryDatabase;
30
31 /**
32  * @author Mark Hayes
33  */

34 public class TupleSerialFactoryTest extends TestCase
35     implements TransactionWorker {
36
37     public static void main(String JavaDoc[] args)
38         throws Exception JavaDoc {
39
40         junit.framework.TestResult tr =
41             junit.textui.TestRunner.run(suite());
42         if (tr.errorCount() > 0 ||
43             tr.failureCount() > 0) {
44             System.exit(1);
45         } else {
46             System.exit(0);
47         }
48     }
49
50     public static Test suite()
51         throws Exception JavaDoc {
52
53         TestSuite suite = new TestSuite();
54         for (int i = 0; i < TestEnv.ALL.length; i += 1) {
55             for (int sorted = 0; sorted < 2; sorted += 1) {
56                 suite.addTest(new TupleSerialFactoryTest(TestEnv.ALL[i],
57                                                            sorted != 0));
58             }
59         }
60         return suite;
61     }
62
63     private TestEnv testEnv;
64     private Environment env;
65     private StoredClassCatalog catalog;
66     private TransactionRunner runner;
67     private TupleSerialFactory factory;
68     private Database store1;
69     private Database store2;
70     private SecondaryDatabase index1;
71     private SecondaryDatabase index2;
72     private boolean isSorted;
73     private Map JavaDoc storeMap1;
74     private Map JavaDoc storeMap2;
75     private Map JavaDoc indexMap1;
76     private Map JavaDoc indexMap2;
77
78     public TupleSerialFactoryTest(TestEnv testEnv, boolean isSorted) {
79
80         super(null);
81
82         this.testEnv = testEnv;
83         this.isSorted = isSorted;
84
85         String JavaDoc name = "TupleSerialFactoryTest-" + testEnv.getName();
86         name += isSorted ? "-sorted" : "-unsorted";
87         setName(name);
88     }
89
90     public void setUp()
91         throws Exception JavaDoc {
92
93         DbTestUtil.printTestName(getName());
94         env = testEnv.open(getName());
95         runner = new TransactionRunner(env);
96
97         createDatabase();
98     }
99
100     public void tearDown() {
101
102         try {
103             if (index1 != null) {
104                 index1.close();
105             }
106             if (index2 != null) {
107                 index2.close();
108             }
109             if (store1 != null) {
110                 store1.close();
111             }
112             if (store2 != null) {
113                 store2.close();
114             }
115             if (catalog != null) {
116                 catalog.close();
117             }
118             if (env != null) {
119                 env.close();
120             }
121         } catch (Exception JavaDoc e) {
122             System.out.println("Ignored exception during tearDown: " + e);
123         } finally {
124             /* Ensure that GC can cleanup. */
125             index1 = null;
126             index2 = null;
127             store1 = null;
128             store2 = null;
129             catalog = null;
130             env = null;
131             testEnv = null;
132             runner = null;
133             factory = null;
134             storeMap1 = null;
135             storeMap2 = null;
136             indexMap1 = null;
137             indexMap2 = null;
138         }
139     }
140
141     public void runTest()
142         throws Exception JavaDoc {
143
144         runner.run(this);
145     }
146
147     public void doWork()
148         throws Exception JavaDoc {
149
150         createViews();
151         writeAndRead();
152     }
153
154     private void createDatabase()
155         throws Exception JavaDoc {
156
157         catalog = new StoredClassCatalog(openDb("catalog.db"));
158         factory = new TupleSerialFactory(catalog);
159         assertSame(catalog, factory.getCatalog());
160
161         store1 = openDb("store1.db");
162         store2 = openDb("store2.db");
163         index1 = openSecondaryDb(factory, "1", store1, "index1.db", null);
164         index2 = openSecondaryDb(factory, "2", store2, "index2.db", store1);
165     }
166
167     private Database openDb(String JavaDoc file)
168         throws Exception JavaDoc {
169
170         DatabaseConfig config = new DatabaseConfig();
171         config.setTransactional(testEnv.isTxnMode());
172         config.setAllowCreate(true);
173
174         return DbCompat.openDatabase(env, null, file, null, config);
175     }
176
177     private SecondaryDatabase openSecondaryDb(TupleSerialFactory factory,
178                                               String JavaDoc keyName,
179                                               Database primary,
180                                               String JavaDoc file,
181                                               Database foreignStore)
182         throws Exception JavaDoc {
183
184         SecondaryConfig secConfig = new SecondaryConfig();
185         secConfig.setTransactional(testEnv.isTxnMode());
186         secConfig.setAllowCreate(true);
187         secConfig.setKeyCreator(factory.getKeyCreator(MarshalledObject.class,
188                                                       keyName));
189         if (foreignStore != null) {
190             secConfig.setForeignKeyDatabase(foreignStore);
191             secConfig.setForeignKeyDeleteAction(
192                     ForeignKeyDeleteAction.CASCADE);
193         }
194
195         return DbCompat.openSecondaryDatabase(env, null,
196                                               file, null,
197                                               primary, secConfig);
198     }
199
200     private void createViews()
201         throws Exception JavaDoc {
202
203         if (isSorted) {
204             storeMap1 = factory.newSortedMap(store1, String JavaDoc.class,
205                                              MarshalledObject.class, true);
206             storeMap2 = factory.newSortedMap(store2, String JavaDoc.class,
207                                              MarshalledObject.class, true);
208             indexMap1 = factory.newSortedMap(index1, String JavaDoc.class,
209                                              MarshalledObject.class, true);
210             indexMap2 = factory.newSortedMap(index2, String JavaDoc.class,
211                                              MarshalledObject.class, true);
212         } else {
213             storeMap1 = factory.newMap(store1, String JavaDoc.class,
214                                        MarshalledObject.class, true);
215             storeMap2 = factory.newMap(store2, String JavaDoc.class,
216                                        MarshalledObject.class, true);
217             indexMap1 = factory.newMap(index1, String JavaDoc.class,
218                                        MarshalledObject.class, true);
219             indexMap2 = factory.newMap(index2, String JavaDoc.class,
220                                        MarshalledObject.class, true);
221         }
222     }
223
224     private void writeAndRead()
225         throws Exception JavaDoc {
226
227         MarshalledObject o1 = new MarshalledObject("data1", "pk1", "ik1", "");
228         assertNull(storeMap1.put(null, o1));
229
230         assertEquals(o1, storeMap1.get("pk1"));
231         assertEquals(o1, indexMap1.get("ik1"));
232
233         MarshalledObject o2 = new MarshalledObject("data2", "pk2", "", "pk1");
234         assertNull(storeMap2.put(null, o2));
235
236         assertEquals(o2, storeMap2.get("pk2"));
237         assertEquals(o2, indexMap2.get("pk1"));
238
239         /*
240          * store1 contains o1 with primary key "pk1" and index key "ik1"
241          * store2 contains o2 with primary key "pk2" and foreign key "pk1"
242          * which is the primary key of store1
243          */

244
245         storeMap1.remove("pk1");
246         assertNull(storeMap1.get("pk1"));
247         assertNull(indexMap1.get("ik1"));
248         assertNull(storeMap2.get("pk2"));
249         assertNull(indexMap2.get("pk1"));
250     }
251 }
252
Popular Tags