KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.commons.lang.ArrayUtils;
7
8 import com.sleepycat.je.Database;
9 import com.sleepycat.je.DatabaseConfig;
10 import com.sleepycat.je.DatabaseEntry;
11 import com.sleepycat.je.DatabaseException;
12 import com.sleepycat.je.EnvironmentConfig;
13 import com.sleepycat.je.LockMode;
14 import com.sleepycat.je.OperationStatus;
15 import com.tc.objectserver.persistence.sleepycat.DBEnvironment.ClassCatalogWrapper;
16 import com.tc.test.TCTestCase;
17
18 import java.io.File JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Arrays JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.LinkedList JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27
28 /**
29  * IMPORTANT: Sleepycat uses a static cache. If you open an environment, but
30  * don't close it, even if you delete the data files underneath it, when you
31  * create another instance of the environment on the same directory, the data
32  * may still be there in cache. This makes it difficult to test in a JUnit
33  * scenario.
34  */

35 public class DBEnvironmentTest extends TCTestCase {
36   private File JavaDoc envHome;
37   private EnvironmentConfig ecfg;
38   private DatabaseConfig dbcfg;
39   private static int count = 0;
40
41   protected void setUp() throws Exception JavaDoc {
42     super.setUp();
43     ecfg = new EnvironmentConfig();
44     ecfg.setAllowCreate(true);
45     ecfg.setReadOnly(false);
46     ecfg.setTransactional(true);
47
48     dbcfg = new DatabaseConfig();
49     dbcfg.setAllowCreate(true);
50     dbcfg.setReadOnly(false);
51     dbcfg.setTransactional(true);
52     while ((envHome = new File JavaDoc(this.getTempDirectory(), ++count + "")).exists()) {
53       //
54
}
55     System.out.println("DB home: " + envHome);
56
57   }
58
59   private DBEnvironment newEnv(boolean paranoid) throws IOException JavaDoc {
60     return newEnv(new HashMap JavaDoc(), new ArrayList JavaDoc(), paranoid);
61   }
62
63   private DBEnvironment newEnv(Map JavaDoc map, List JavaDoc list, boolean paranoid) throws IOException JavaDoc {
64     return new DBEnvironment(map, list, paranoid, envHome, ecfg, dbcfg);
65   }
66
67   public void testCrashParanoidReopenParanoid() throws Exception JavaDoc {
68     DBEnvironment env = newEnv(true);
69     assertTrue(env.open().isClean());
70     env.forceClose();
71     env = newEnv(true);
72     // this should succeed, since a paranoid database should always be clean.
73
assertTrue(env.open().isClean());
74     env.close();
75   }
76
77   public void testCrashNotParanoidReopenNotParanoid() throws Exception JavaDoc {
78     DBEnvironment env = newEnv(false);
79     assertCleanDir();
80     assertTrue(env.open().isClean());
81     env.forceClose();
82     env = newEnv(false);
83     // this shouldn't open clean, since the database wasn't opened in paranoid
84
// mode before it crashed
85
assertFalse(env.open().isClean());
86     env.forceClose();
87   }
88
89   public void testCrashParanoidReopenNotParanoid() throws Exception JavaDoc {
90     DBEnvironment env = newEnv(true);
91     assertTrue(env.open().isClean());
92     env.forceClose();
93     env = newEnv(false);
94     // this should succeed, since a paranoid database should always be clean
95
assertTrue(env.open().isClean());
96     env.forceClose();
97   }
98
99   private void assertCleanDir() throws Exception JavaDoc {
100     assertEquals(Arrays.asList(new Object JavaDoc[] {}), Arrays.asList(this.envHome.listFiles()));
101   }
102
103   public void testCrashNotParanoidReopenParanoid() throws Exception JavaDoc {
104     DBEnvironment env = newEnv(false);
105     assertTrue(env.open().isClean());
106     env.forceClose();
107     env = newEnv(true);
108     // this shouldn't open clean, since the database wasn't opened in paranoid
109
// mode
110
// before it crashed.
111
assertFalse(env.open().isClean());
112   }
113
114   public void testLifecycleParanoid() throws Exception JavaDoc {
115     testLifecycle(true);
116   }
117
118   public void testLifecycleNotParanoid() throws Exception JavaDoc {
119     testLifecycle(false);
120   }
121
122   private void testLifecycle(boolean paranoid) throws Exception JavaDoc {
123     List JavaDoc databases = new LinkedList JavaDoc();
124     Map JavaDoc databasesByName = new HashMap JavaDoc();
125     assertFalse(this.envHome.exists());
126     DBEnvironment env = newEnv(databasesByName, databases, paranoid);
127
128     try {
129       env.getEnvironment();
130       fail("Should have thrown an exception trying to get the environment before open()");
131     } catch (DatabaseNotOpenException e) {
132       // ok.
133
}
134
135     try {
136       env.getObjectDatabase();
137       fail("Should have thrown an exception trying to get the database before open()");
138     } catch (DatabaseNotOpenException e) {
139       // ok.
140
}
141
142     try {
143       env.close();
144       fail("Should have thrown an exception trying to close the database before open()");
145     } catch (DatabaseNotOpenException e) {
146       // ok.
147
}
148
149     try {
150       env.getClassCatalogWrapper();
151       fail("Should have thrown an exception trying to get the class catalog before open()");
152     } catch (DatabaseNotOpenException e) {
153       // ok.
154
}
155     assertEquals(0, databases.size());
156     assertEquals(databasesByName.size(), databases.size());
157     DatabaseOpenResult result = env.open();
158
159     // the first time the database is opened, it should be brand new and clean.
160
assertTrue(result.isClean());
161
162     assertEquals(databasesByName.size(), databases.size());
163     assertDatabasesOpen(databases);
164
165     try {
166       env.open();
167       fail("Should have thrown an exception trying to open the environment twice.");
168     } catch (DatabaseOpenException e) {
169       // ok.
170
}
171
172     ClassCatalogWrapper cc = env.getClassCatalogWrapper();
173     env.close();
174     assertDatabasesClosed(databases);
175     assertClassCatalogClosed(cc);
176
177     try {
178       env.close();
179       fail("Should have thrown an exception trying to open the environment after close()");
180     } catch (DatabaseNotOpenException e) {
181       // ok.
182
}
183
184     databases.clear();
185     databasesByName.clear();
186
187     env = newEnv(databasesByName, databases, paranoid);
188     result = env.open();
189     assertTrue(result.isClean());
190
191     Database db = env.getObjectDatabase();
192
193     DatabaseEntry key = new DatabaseEntry(new byte[] { 1 });
194     DatabaseEntry one = new DatabaseEntry(new byte[] { 1 });
195     DatabaseEntry two = new DatabaseEntry(new byte[] { 2 });
196
197     DatabaseEntry value = new DatabaseEntry();
198
199     OperationStatus status = db.get(null, key, value, LockMode.DEFAULT);
200     assertEquals(OperationStatus.NOTFOUND, status);
201
202     status = db.put(null, key, one);
203     assertEquals(OperationStatus.SUCCESS, status);
204
205     status = db.get(null, key, value, LockMode.DEFAULT);
206     assertEquals(OperationStatus.SUCCESS, status);
207     assertTrue(ArrayUtils.isEquals(one.getData(), value.getData()));
208
209     status = db.put(null, key, two);
210     assertEquals(OperationStatus.SUCCESS, status);
211
212     status = db.get(null, key, value, LockMode.DEFAULT);
213     assertEquals(OperationStatus.SUCCESS, status);
214     assertTrue(ArrayUtils.isEquals(two.getData(), value.getData()));
215
216     env.close();
217
218     databases.clear();
219     databasesByName.clear();
220     env = newEnv(databasesByName, databases, paranoid);
221     env.open();
222     db = env.getObjectDatabase();
223     status = db.get(null, key, value, LockMode.DEFAULT);
224     assertTrue(ArrayUtils.isEquals(two.getData(), value.getData()));
225
226     // test closing then opening again.
227
env.close();
228
229     databases.clear();
230     databasesByName.clear();
231     env = newEnv(databasesByName, databases, paranoid);
232     env.open();
233     db = env.getObjectDatabase();
234
235     status = db.get(null, key, value, LockMode.DEFAULT);
236     assertEquals(OperationStatus.SUCCESS, status);
237     assertTrue(ArrayUtils.isEquals(two.getData(), value.getData()));
238
239     env.close();
240   }
241
242   private void assertClassCatalogClosed(ClassCatalogWrapper cc) throws DatabaseException {
243     try {
244       cc.close();
245       fail("Should have thrown an exception.");
246     } catch (IllegalStateException JavaDoc e) {
247       // ok
248
}
249   }
250
251   private void assertDatabasesOpen(List JavaDoc databases) throws Exception JavaDoc {
252     for (Iterator JavaDoc i = databases.iterator(); i.hasNext();) {
253       assertTrue(isDatabaseOpen((Database) i.next()));
254     }
255   }
256
257   private void assertDatabasesClosed(List JavaDoc databases) throws Exception JavaDoc {
258     for (Iterator JavaDoc i = databases.iterator(); i.hasNext();) {
259       assertFalse(isDatabaseOpen((Database) i.next()));
260     }
261   }
262
263   private boolean isDatabaseOpen(Database db) throws Exception JavaDoc {
264     DatabaseEntry key = new DatabaseEntry();
265     key.setData(new byte[] { 1 });
266     try {
267       db.get(null, key, new DatabaseEntry(), LockMode.DEFAULT);
268       return true;
269     } catch (DatabaseException e) {
270       // XXX: This may not be a reliable test, but there doesn't seem to be
271
// another way to tell.
272
return false;
273     }
274
275   }
276 }
Popular Tags