KickJava   Java API By Example, From Geeks To Geeks.

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


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.logging.TCLogger;
8 import com.tc.logging.TCLogging;
9 import com.tc.object.ObjectID;
10 import com.tc.objectserver.persistence.api.PersistenceTransaction;
11 import com.tc.objectserver.persistence.api.PersistenceTransactionProvider;
12 import com.tc.objectserver.persistence.api.PersistentCollectionFactory;
13 import com.tc.test.TCTestCase;
14 import com.tc.util.Assert;
15
16 import java.io.File JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.util.Arrays JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Set JavaDoc;
23
24 public class SleepycatCollectionsTest extends TCTestCase {
25
26   private SleepycatPersistor persistor;
27   private PersistenceTransactionProvider ptp;
28   private DBEnvironment env;
29   private PersistentCollectionFactory collectionsFactory;
30   private SleepycatCollectionsPersistor collectionsPersistor;
31   private static int dbHomeCounter = 0;
32   private static File JavaDoc tempDirectory;
33
34   public void setUp() throws Exception JavaDoc {
35
36     if (env != null) env.close();
37     File JavaDoc dbHome = newDBHome();
38     TCLogger logger = TCLogging.getLogger(getClass());
39     CustomSerializationAdapterFactory saf = new CustomSerializationAdapterFactory();
40     env = new DBEnvironment(true, dbHome);
41     persistor = new SleepycatPersistor(logger, env, saf);
42     ptp = persistor.getPersistenceTransactionProvider();
43     collectionsFactory = persistor.getPersistentCollectionFactory();
44     collectionsPersistor = persistor.getCollectionsPersistor();
45   }
46
47   // XXX:: Check SleepycatSerializationTest if you want know why its done like this or ask Orion.
48
private File JavaDoc newDBHome() throws IOException JavaDoc {
49     File JavaDoc file;
50     if (tempDirectory == null) tempDirectory = getTempDirectory();
51     ++dbHomeCounter;
52     for (file = new File JavaDoc(tempDirectory, "db" + dbHomeCounter); file.exists(); ++dbHomeCounter) {
53       //
54
}
55     assertFalse(file.exists());
56     System.err.println("DB Home = " + file);
57     return file;
58   }
59
60   public void tearDown() throws Exception JavaDoc {
61     persistor = null;
62     ptp = null;
63     env = null;
64   }
65
66   public void testSleepycatPersistableMap() throws Exception JavaDoc {
67     ObjectID id = new ObjectID(7);
68     SleepycatPersistableMap sMap = (SleepycatPersistableMap) collectionsFactory.createPersistentMap(id);
69     addToMap(sMap);
70     Map JavaDoc localMap = new HashMap JavaDoc();
71     addToMap(localMap);
72     equals(localMap, sMap);
73
74     PersistenceTransaction tx = ptp.newTransaction();
75     collectionsPersistor.saveMap(tx, sMap);
76     tx.commit();
77     equals(localMap, sMap);
78
79     tx = ptp.newTransaction();
80     SleepycatPersistableMap sMap2 = collectionsPersistor.loadMap(tx, new ObjectID(1000));
81     tx.commit();
82     equals(new HashMap JavaDoc(), sMap2);
83
84     tx = ptp.newTransaction();
85     sMap2 = collectionsPersistor.loadMap(tx, id);
86     tx.commit();
87     equals(localMap, sMap2);
88
89     System.err.println(" Adding more maps ....");
90     addMoreMaps();
91     
92     System.err.println(" Loading map again ....");
93     tx = ptp.newTransaction();
94     sMap2 = collectionsPersistor.loadMap(tx, id);
95     tx.commit();
96     equals(localMap, sMap2);
97
98     System.err.println(" Loading different map ....");
99     tx = ptp.newTransaction();
100     SleepycatPersistableMap sMap3 = collectionsPersistor.loadMap(tx, new ObjectID(25));
101     tx.commit();
102     equals(localMap, sMap3);
103     
104     addToMap(sMap, 2);
105     addToMap(localMap, 2);
106     equals(localMap, sMap);
107
108     tx = ptp.newTransaction();
109     collectionsPersistor.saveMap(tx, sMap);
110     tx.commit();
111     equals(localMap, sMap);
112
113     tx = ptp.newTransaction();
114     sMap2 = collectionsPersistor.loadMap(tx, id);
115     tx.commit();
116     equals(localMap, sMap2);
117     
118     addAndRemoveFromMap(sMap);
119     addAndRemoveFromMap(localMap);
120     Assert.assertEquals(localMap, sMap);
121
122     tx = ptp.newTransaction();
123     collectionsPersistor.saveMap(tx, sMap);
124     tx.commit();
125     equals(localMap, sMap);
126
127     tx = ptp.newTransaction();
128     sMap2 = collectionsPersistor.loadMap(tx, id);
129     tx.commit();
130     equals(localMap, sMap2);
131     
132     addRemoveClearFromMap(sMap);
133     addRemoveClearFromMap(localMap);
134     equals(localMap, sMap);
135
136     tx = ptp.newTransaction();
137     collectionsPersistor.saveMap(tx, sMap);
138     tx.commit();
139     equals(localMap, sMap);
140
141     tx = ptp.newTransaction();
142     sMap2 = collectionsPersistor.loadMap(tx, id);
143     tx.commit();
144     equals(localMap, sMap2);
145     
146     tx = ptp.newTransaction();
147     Assert.assertTrue(collectionsPersistor.deleteCollection(tx, id));
148     tx.commit();
149     
150     tx = ptp.newTransaction();
151     sMap2 = collectionsPersistor.loadMap(tx, id);
152     tx.commit();
153     equals(new HashMap JavaDoc(), sMap2);
154     
155     tx = ptp.newTransaction();
156     Assert.assertFalse(collectionsPersistor.deleteCollection(tx, id));
157     tx.commit();
158     
159   }
160
161   private void equals(Map JavaDoc m1, Map JavaDoc m2) {
162     Assert.assertEquals(m1.size(), m2.size());
163     Assert.assertEquals(m1, m2);
164     equals(m1.keySet(), m2.keySet());
165     equals(m1.values(), m2.values());
166     Assert.assertEquals(m1.entrySet(), m2.entrySet());
167   }
168
169   // This implementation does not care about the order
170
private void equals(Collection JavaDoc c1, Collection JavaDoc c2) {
171     Assert.assertEquals(c1.size(), c2.size());
172     Assert.assertTrue(c1.containsAll(c2));
173     Assert.assertTrue(c2.containsAll(c1));
174   }
175   
176   private void equals(Set JavaDoc s1, Set JavaDoc s2) {
177     Assert.assertEquals(s1, s2);
178     equals(Arrays.asList(s1.toArray()), Arrays.asList(s2.toArray()));
179   }
180
181   private void addMoreMaps() throws IOException JavaDoc, DatabaseException {
182     for (int j = 20; j < 40; j++) {
183       ObjectID id = new ObjectID(j);
184       SleepycatPersistableMap sMap = (SleepycatPersistableMap) collectionsFactory.createPersistentMap(id);
185       addToMap(sMap);
186       PersistenceTransaction tx = ptp.newTransaction();
187       collectionsPersistor.saveMap(tx, sMap);
188       tx.commit();
189     }
190   }
191
192   private void addToMap(Map JavaDoc map) {
193     addToMap(map, 1);
194   }
195
196   private void addToMap(Map JavaDoc map, int increCount) {
197     int j = 0;
198     for (int i = 0; i < 50; i++, j += increCount) {
199       map.put(new ObjectID(j), new ObjectID(100 + j));
200       map.put(new Integer JavaDoc(j), new Long JavaDoc(j));
201       map.put(new String JavaDoc("" + j), new String JavaDoc("" + j));
202       map.put(new Double JavaDoc(j + 0.005), new Float JavaDoc(j - 0.004));
203     }
204   }
205
206   private void addAndRemoveFromMap(Map JavaDoc map) {
207     int j = 50;
208     for (int i = 0; i < 50; i++, j ++) {
209       map.put(new ObjectID(j), new ObjectID(100 + j));
210       map.put(new Integer JavaDoc(j), new Long JavaDoc(j));
211       map.put(new String JavaDoc("" + j), new String JavaDoc("" + j));
212       map.put(new Double JavaDoc(j + 0.005), new Float JavaDoc(j - 0.004));
213       map.remove(new ObjectID(j-25));
214       map.remove(new Integer JavaDoc(j-25));
215       map.remove(new String JavaDoc(""+(j-25)));
216       map.remove(new Double JavaDoc((j-25) + 0.005));
217     }
218   }
219   
220   private void addRemoveClearFromMap(Map JavaDoc map) {
221     int j = 100;
222     for (int i = 0; i < 50; i++, j ++) {
223       map.put(new ObjectID(j), new ObjectID(100 + j));
224       map.put(new Integer JavaDoc(j), new Long JavaDoc(j));
225       map.put(new String JavaDoc("" + j), new String JavaDoc("" + j));
226       map.put(new Double JavaDoc(j + 0.005), new Float JavaDoc(j - 0.004));
227       map.remove(new ObjectID(j-25));
228       map.remove(new Integer JavaDoc(j-25));
229       map.remove(new String JavaDoc(""+(j-25)));
230       map.remove(new Double JavaDoc((j-25) + 0.005));
231       if(i % 20 == 19) {
232         map.clear();
233       }
234     }
235   }
236
237 }
238
Popular Tags