KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > objectserver > persistence > sleepycat > SleepycatSerializationTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.objectserver.persistence.sleepycat;
5
6 import com.sleepycat.je.DatabaseException;
7 import com.tc.io.serializer.api.StringIndex;
8 import com.tc.logging.TCLogger;
9 import com.tc.logging.TCLogging;
10 import com.tc.object.ObjectID;
11 import com.tc.object.SerializationUtil;
12 import com.tc.object.tx.TransactionID;
13 import com.tc.objectserver.api.ObjectInstanceMonitor;
14 import com.tc.objectserver.core.api.ManagedObject;
15 import com.tc.objectserver.core.api.ManagedObjectState;
16 import com.tc.objectserver.core.api.TestDNA;
17 import com.tc.objectserver.core.api.TestDNACursor;
18 import com.tc.objectserver.impl.ObjectInstanceMonitorImpl;
19 import com.tc.objectserver.managedobject.BackReferences;
20 import com.tc.objectserver.managedobject.ManagedObjectImpl;
21 import com.tc.objectserver.managedobject.ManagedObjectStateFactory;
22 import com.tc.objectserver.managedobject.NullManagedObjectChangeListenerProvider;
23 import com.tc.objectserver.persistence.api.ManagedObjectPersistor;
24 import com.tc.objectserver.persistence.api.PersistenceTransaction;
25 import com.tc.objectserver.persistence.api.PersistenceTransactionProvider;
26 import com.tc.test.TCTestCase;
27
28 import java.io.File JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Date JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.HashSet JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.LinkedHashMap JavaDoc;
36 import java.util.Random JavaDoc;
37 import java.util.Set JavaDoc;
38 import java.util.TreeMap JavaDoc;
39 import java.util.TreeSet JavaDoc;
40
41 /**
42  * XXX: This test needs to test more of the persistor interface.
43  */

44 public class SleepycatSerializationTest extends TCTestCase {
45   private SleepycatPersistor persistor;
46   private PersistenceTransactionProvider ptp;
47   private DBEnvironment env;
48   private ManagedObjectPersistor mop;
49   private TCLogger logger;
50   private StringIndex stringIndex;
51   private int transactionSequence;
52   private int objectIDSequence;
53   private Set JavaDoc rootIDs;
54   private Set JavaDoc rootNames;
55   private Set JavaDoc mos;
56   private ObjectInstanceMonitor imo;
57   private static int dbHomeCounter = 0;
58   private static File JavaDoc tempDirectory;
59
60   public void setUp() throws Exception JavaDoc {
61     // XXX: This static temp directory is here to solve file problems
62
// on Windows. Each test spawns a new instance of the test class
63
// which causes a cleaning of the old temp directory which fails
64
// because the je lock file can't be removed because it's still open
65
// by sleepycat (sleepycat has a static cache).
66
// The static temp directory is here to stop subsequent test instances
67
// from trying to clean the temp directory and failing.
68
if (tempDirectory == null) tempDirectory = getTempDirectory();
69     logger = TCLogging.getLogger(getClass());
70     rootIDs = new HashSet JavaDoc();
71     rootNames = new HashSet JavaDoc();
72     mos = new HashSet JavaDoc();
73     imo = new ObjectInstanceMonitorImpl();
74
75   }
76
77   public void tearDown() throws Exception JavaDoc {
78     persistor = null;
79     ptp = null;
80     env = null;
81     mop = null;
82     logger = null;
83     stringIndex = null;
84     rootIDs = null;
85     rootNames = null;
86     mos = null;
87   }
88
89   public void testSleepycatSerializer() throws Exception JavaDoc {
90     SerializationAdapterFactory saf = new SleepycatSerializationAdapterFactory();
91     doTest(saf);
92   }
93
94   public void testCustomSerializer() throws Exception JavaDoc {
95     SerializationAdapterFactory saf = new CustomSerializationAdapterFactory();
96     doTest(saf);
97   }
98
99   public void doTest(SerializationAdapterFactory saf) throws Exception JavaDoc {
100     File JavaDoc dbHome = newDBHome();
101     initDB(dbHome, saf);
102
103     PersistenceTransaction ptx = ptp.newTransaction();
104
105     String JavaDoc rootOne = "rootName";
106     ObjectID rootID = newObjectID();
107
108     // add some roots and objects
109
addRoot(ptx, rootOne, rootID);
110
111     for (int i = 0; i < 100; i++) {
112       ObjectID oid = newObjectID();
113       addRoot(ptx, "foo" + i, oid);
114       ManagedObject mo = newManagedObject(oid, i);
115       assertTrue(mo.isDirty());
116       mos.add(mo);
117     }
118     mop.saveAllObjects(ptx, mos);
119
120     ManagedObject mo = newPhysicalObject(newObjectID());
121     mop.saveObject(ptx, mo);
122     mos.add(mo);
123
124     mo = newLogicalDateObject(newObjectID());
125     mop.saveObject(ptx, mo);
126     mos.add(mo);
127
128     ptx.commit();
129
130     System.err.println("String index before: " + stringIndex);
131     initDB(dbHome, saf);
132
133     System.err.println("String index after: " + stringIndex);
134
135     assertEquals(rootID, mop.loadRootID(rootOne));
136     assertNotSame(rootID, mop.loadRootID(rootOne));
137     assertEquals(rootIDs, mop.loadRoots());
138     assertEquals(rootNames, mop.loadRootNames());
139
140     for (Iterator JavaDoc i = mos.iterator(); i.hasNext();) {
141       ManagedObjectImpl test = (ManagedObjectImpl) i.next();
142       ManagedObjectImpl loaded = (ManagedObjectImpl) mop.loadObjectByID(test.getID());
143       assertFalse(test.isDirty());
144       assertFalse(loaded.isDirty());
145       assertFalse(test.isNew());
146       assertFalse(loaded.isNew());
147       assertTrue(test.isEqual(loaded));
148       assertNotSame(test, loaded);
149       assertNotSame(mop.loadObjectByID(test.getID()), mop.loadObjectByID(test.getID()));
150
151       byte type = loaded.getManagedObjectState().getType();
152       switch (type) {
153         case ManagedObjectState.PHYSICAL_TYPE:
154           loaded.apply(newPhysicalDNA(), new TransactionID(++transactionSequence), new BackReferences(), imo);
155           break;
156         case ManagedObjectState.MAP_TYPE:
157         case ManagedObjectState.PARTIAL_MAP_TYPE:
158           loaded.apply(newLogicalMapDNA(), new TransactionID(++transactionSequence), new BackReferences(), imo);
159           break;
160         case ManagedObjectState.LIST_TYPE:
161           loaded.apply(newLogicalListDNA(), new TransactionID(++transactionSequence), new BackReferences(), imo);
162           break;
163         case ManagedObjectState.SET_TYPE:
164           loaded.apply(newLogicalSetDNA(), new TransactionID(++transactionSequence), new BackReferences(), imo);
165           break;
166         case ManagedObjectState.ARRAY_TYPE:
167           loaded.apply(newLogicalArrayDNA(), new TransactionID(++transactionSequence), new BackReferences(), imo);
168           break;
169         case ManagedObjectState.LINKED_HASHMAP_TYPE:
170           loaded.apply(newLogicalLinkedHashMapDNA(), new TransactionID(++transactionSequence), new BackReferences(),
171                        imo);
172           break;
173         case ManagedObjectState.DATE_TYPE:
174           loaded.apply(newLogicalDateDNA(), new TransactionID(++transactionSequence), new BackReferences(), imo);
175           break;
176         case ManagedObjectState.LITERAL_TYPE:
177           loaded.apply(newLiteralDNA(), new TransactionID(++transactionSequence), new BackReferences(), imo);
178           break;
179         case ManagedObjectState.TREE_MAP_TYPE:
180           loaded.apply(newLogicalTreeMapDNA(), new TransactionID(++transactionSequence), new BackReferences(), imo);
181           break;
182         case ManagedObjectState.TREE_SET_TYPE:
183           loaded.apply(newLogicalTreeSetDNA(), new TransactionID(++transactionSequence), new BackReferences(), imo);
184           break;
185       }
186
187     }
188   }
189
190   private ManagedObject newManagedObject(ObjectID oid, int i) {
191     switch (i % 10) {
192       case 0:
193         return newPhysicalObject(oid);
194       case 1:
195         return newLogicalMapObject(oid);
196       case 2:
197         return newLogicalArrayObject(oid);
198       case 3:
199         return newLogicalLiteralObject(oid);
200       case 4:
201         return newLogicalLinkedHashMapObject(oid);
202       case 5:
203         return newLogicalListObject(oid);
204       case 6:
205         return newLogicalSetObject(oid);
206       case 7:
207         return newLogicalTreeSetObject(oid);
208       case 8:
209         return newLogicalTreeMapObject(oid);
210       default:
211         return newLogicalDateObject(oid);
212     }
213   }
214
215   private ManagedObject newLogicalTreeMapObject(ObjectID oid) {
216     return newLogicalObject(oid, newLogicalTreeMapDNA());
217   }
218
219   private TestDNA newLogicalTreeMapDNA() {
220     TestDNACursor cursor = new TestDNACursor();
221     cursor.addLogicalAction(SerializationUtil.PUT, new Object JavaDoc[] { new Short JavaDoc((short) 10), "good bad and ugly" });
222     cursor.addLogicalAction(SerializationUtil.PUT, new Object JavaDoc[] { new Boolean JavaDoc(true), "mapped" });
223     cursor.addLogicalAction(SerializationUtil.PUT, new Object JavaDoc[] { new Boolean JavaDoc(true), "Remapped" });
224     TestDNA dna = new TestDNA(cursor, TreeMap JavaDoc.class.getName());
225     return dna;
226   }
227
228   private ManagedObject newLogicalTreeSetObject(ObjectID oid) {
229     return newLogicalObject(oid, newLogicalTreeSetDNA());
230   }
231
232   private TestDNA newLogicalTreeSetDNA() {
233     TestDNACursor cursor = new TestDNACursor();
234     cursor.addLogicalAction(SerializationUtil.ADD, new Object JavaDoc[] { new Integer JavaDoc(10343) });
235     cursor.addLogicalAction(SerializationUtil.ADD, new Object JavaDoc[] { "Hello" });
236     cursor.addLogicalAction(SerializationUtil.ADD, new Object JavaDoc[] { new ObjectID(25) });
237     cursor.addLogicalAction(SerializationUtil.ADD, new Object JavaDoc[] { newLong() });
238     TestDNA dna = new TestDNA(cursor, TreeSet JavaDoc.class.getName());
239     return dna;
240   }
241
242   private ManagedObject newLogicalSetObject(ObjectID oid) {
243     return newLogicalObject(oid, newLogicalSetDNA());
244   }
245
246   private TestDNA newLogicalSetDNA() {
247     TestDNACursor cursor = new TestDNACursor();
248     cursor.addLogicalAction(SerializationUtil.ADD, new Object JavaDoc[] { new Integer JavaDoc(10343) });
249     cursor.addLogicalAction(SerializationUtil.ADD, new Object JavaDoc[] { "Hello" });
250     cursor.addLogicalAction(SerializationUtil.ADD, new Object JavaDoc[] { new ObjectID(25) });
251     cursor.addLogicalAction(SerializationUtil.ADD, new Object JavaDoc[] { newLong() });
252     TestDNA dna = new TestDNA(cursor, HashSet JavaDoc.class.getName());
253     return dna;
254   }
255
256   private ManagedObject newLogicalListObject(ObjectID oid) {
257     return newLogicalObject(oid, newLogicalListDNA());
258   }
259
260   private TestDNA newLogicalListDNA() {
261     TestDNACursor cursor = new TestDNACursor();
262     cursor.addLogicalAction(SerializationUtil.ADD, new Object JavaDoc[] { new Integer JavaDoc(10343) });
263     cursor.addLogicalAction(SerializationUtil.ADD, new Object JavaDoc[] { "Hello" });
264     cursor.addLogicalAction(SerializationUtil.ADD, new Object JavaDoc[] { new ObjectID(25) });
265     cursor.addLogicalAction(SerializationUtil.ADD, new Object JavaDoc[] { newLong() });
266     TestDNA dna = new TestDNA(cursor, ArrayList JavaDoc.class.getName());
267     return dna;
268   }
269
270   private ManagedObject newLogicalLinkedHashMapObject(ObjectID oid) {
271     return newLogicalObject(oid, newLogicalLinkedHashMapDNA());
272   }
273
274   private TestDNA newLogicalLinkedHashMapDNA() {
275     TestDNACursor cursor = new TestDNACursor();
276     cursor.addPhysicalAction("java.util.LinkedHashMap.accessOrder", Boolean.TRUE);
277     cursor.addLogicalAction(SerializationUtil.PUT, new Object JavaDoc[] { new Integer JavaDoc(10), "King Kong" });
278     cursor.addLogicalAction(SerializationUtil.PUT, new Object JavaDoc[] { new Integer JavaDoc(20), "Mad Max" });
279     cursor.addLogicalAction(SerializationUtil.PUT, new Object JavaDoc[] { new Integer JavaDoc(25), "Mummy Returns" });
280     cursor.addLogicalAction(SerializationUtil.GET, new Object JavaDoc[] { new Integer JavaDoc(20) });
281     TestDNA dna = new TestDNA(cursor, LinkedHashMap JavaDoc.class.getName());
282     return dna;
283   }
284
285   private ManagedObject newLogicalLiteralObject(ObjectID oid) {
286     return newLogicalObject(oid, newLiteralDNA());
287   }
288
289   private TestDNA newLiteralDNA() {
290     TestDNACursor cursor = new TestDNACursor();
291     Short JavaDoc s = new Short JavaDoc((short) 0x0045);
292     cursor.addLiteralAction("literal", s);
293     TestDNA dna = new TestDNA(cursor, Short JavaDoc.class.getName());
294     return dna;
295   }
296
297   private ManagedObject newLogicalArrayObject(ObjectID oid) {
298     return newLogicalObject(oid, newLogicalArrayDNA());
299   }
300
301   private TestDNA newLogicalArrayDNA() {
302     TestDNACursor cursor = new TestDNACursor();
303     Object JavaDoc[] array = new Object JavaDoc[] { newLong(), newLong(), newLong() };
304     cursor.addArrayAction(array);
305     TestDNA dna = new TestDNA(cursor, array.getClass().getName());
306     return dna;
307   }
308
309   Random JavaDoc r = new Random JavaDoc();
310
311   private Long JavaDoc newLong() {
312     return new Long JavaDoc(r.nextLong());
313   }
314
315   private ManagedObject newLogicalDateObject(ObjectID objectID) {
316     return newLogicalObject(objectID, newLogicalDateDNA());
317   }
318
319   private ManagedObject newLogicalObject(ObjectID objectID, TestDNA dna) {
320     ManagedObjectImpl rv = new ManagedObjectImpl(objectID);
321     assertTrue(rv.isNew());
322     rv.apply(dna, new TransactionID(++transactionSequence), new BackReferences(), imo);
323     assertFalse(rv.isNew());
324     return rv;
325   }
326
327   private ManagedObject newLogicalMapObject(ObjectID oid) {
328     return newLogicalObject(oid, newLogicalMapDNA());
329   }
330
331   private void addRoot(PersistenceTransaction ptx, String JavaDoc rootName, ObjectID objectID) {
332     mop.addRoot(ptx, rootName, objectID);
333     rootIDs.add(objectID);
334     rootNames.add(rootName);
335   }
336
337   private ObjectID newObjectID() {
338     return new ObjectID(++objectIDSequence);
339   }
340
341   private ManagedObject newPhysicalObject(ObjectID objectID) {
342     ManagedObjectImpl rv = new ManagedObjectImpl(objectID);
343     TestDNA dna = newPhysicalDNA();
344     assertTrue(rv.isNew());
345     rv.apply(dna, new TransactionID(++transactionSequence), new BackReferences(), imo);
346     assertFalse(rv.isNew());
347     return rv;
348   }
349
350   private TestDNA newPhysicalDNA() {
351     TestDNACursor cursor = new TestDNACursor();
352     cursor.addPhysicalAction("stringField", "Foo");
353     cursor.addPhysicalAction("referenceField", newObjectID());
354     TestDNA dna = new TestDNA(cursor);
355     return dna;
356   }
357
358   private TestDNA newLogicalMapDNA() {
359     TestDNACursor cursor = new TestDNACursor();
360     cursor.addLogicalAction(SerializationUtil.PUT, new Object JavaDoc[] { new Integer JavaDoc(10), "King Kong" });
361     cursor.addLogicalAction(SerializationUtil.PUT, new Object JavaDoc[] { new Integer JavaDoc(20), "Mad Max" });
362     cursor.addLogicalAction(SerializationUtil.PUT, new Object JavaDoc[] { new Integer JavaDoc(25), "Mummy Returns" });
363     TestDNA dna = new TestDNA(cursor, HashMap JavaDoc.class.getName());
364     return dna;
365   }
366
367   private TestDNA newLogicalDateDNA() {
368     TestDNACursor cursor = new TestDNACursor();
369     cursor.addLogicalAction(SerializationUtil.SET_TIME, new Object JavaDoc[] { new Long JavaDoc(100233434L) });
370     TestDNA dna = new TestDNA(cursor, Date JavaDoc.class.getName());
371     return dna;
372   }
373
374   private File JavaDoc newDBHome() {
375     File JavaDoc file;
376     // XXX: UGH... this extra increment is here because of a weird interaction with the static cache of the sleepycat
377
// database and JUnit's test running junk, and something else I don't quite understand about the File.exists(). I
378
// had to add this to ensure that the db counter was actually incremented and that a new directory was actually
379
// used.
380
++dbHomeCounter;
381     for (file = new File JavaDoc(tempDirectory, "db" + dbHomeCounter); file.exists(); ++dbHomeCounter) {
382       //
383
}
384     assertFalse(file.exists());
385     return file;
386   }
387
388   private void initDB(File JavaDoc dbHome, SerializationAdapterFactory saf) throws IOException JavaDoc, DatabaseException {
389     if (env != null) env.close();
390     env = new DBEnvironment(true, dbHome);
391     persistor = new SleepycatPersistor(logger, env, saf);
392     ptp = persistor.getPersistenceTransactionProvider();
393     mop = persistor.getManagedObjectPersistor();
394     stringIndex = persistor.getStringIndex();
395
396     ManagedObjectStateFactory.disableSingleton(true);
397     ManagedObjectStateFactory.createInstance(new NullManagedObjectChangeListenerProvider(), persistor);
398
399   }
400 }
401
Popular Tags