1 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 ; 19 import java.io.IOException ; 20 import java.util.ArrayList ; 21 import java.util.Arrays ; 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.LinkedList ; 25 import java.util.List ; 26 import java.util.Map ; 27 28 35 public class DBEnvironmentTest extends TCTestCase { 36 private File envHome; 37 private EnvironmentConfig ecfg; 38 private DatabaseConfig dbcfg; 39 private static int count = 0; 40 41 protected void setUp() throws Exception { 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 (this.getTempDirectory(), ++count + "")).exists()) { 53 } 55 System.out.println("DB home: " + envHome); 56 57 } 58 59 private DBEnvironment newEnv(boolean paranoid) throws IOException { 60 return newEnv(new HashMap (), new ArrayList (), paranoid); 61 } 62 63 private DBEnvironment newEnv(Map map, List list, boolean paranoid) throws IOException { 64 return new DBEnvironment(map, list, paranoid, envHome, ecfg, dbcfg); 65 } 66 67 public void testCrashParanoidReopenParanoid() throws Exception { 68 DBEnvironment env = newEnv(true); 69 assertTrue(env.open().isClean()); 70 env.forceClose(); 71 env = newEnv(true); 72 assertTrue(env.open().isClean()); 74 env.close(); 75 } 76 77 public void testCrashNotParanoidReopenNotParanoid() throws Exception { 78 DBEnvironment env = newEnv(false); 79 assertCleanDir(); 80 assertTrue(env.open().isClean()); 81 env.forceClose(); 82 env = newEnv(false); 83 assertFalse(env.open().isClean()); 86 env.forceClose(); 87 } 88 89 public void testCrashParanoidReopenNotParanoid() throws Exception { 90 DBEnvironment env = newEnv(true); 91 assertTrue(env.open().isClean()); 92 env.forceClose(); 93 env = newEnv(false); 94 assertTrue(env.open().isClean()); 96 env.forceClose(); 97 } 98 99 private void assertCleanDir() throws Exception { 100 assertEquals(Arrays.asList(new Object [] {}), Arrays.asList(this.envHome.listFiles())); 101 } 102 103 public void testCrashNotParanoidReopenParanoid() throws Exception { 104 DBEnvironment env = newEnv(false); 105 assertTrue(env.open().isClean()); 106 env.forceClose(); 107 env = newEnv(true); 108 assertFalse(env.open().isClean()); 112 } 113 114 public void testLifecycleParanoid() throws Exception { 115 testLifecycle(true); 116 } 117 118 public void testLifecycleNotParanoid() throws Exception { 119 testLifecycle(false); 120 } 121 122 private void testLifecycle(boolean paranoid) throws Exception { 123 List databases = new LinkedList (); 124 Map databasesByName = new HashMap (); 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 } 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 } 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 } 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 } 155 assertEquals(0, databases.size()); 156 assertEquals(databasesByName.size(), databases.size()); 157 DatabaseOpenResult result = env.open(); 158 159 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 } 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 } 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 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 e) { 247 } 249 } 250 251 private void assertDatabasesOpen(List databases) throws Exception { 252 for (Iterator i = databases.iterator(); i.hasNext();) { 253 assertTrue(isDatabaseOpen((Database) i.next())); 254 } 255 } 256 257 private void assertDatabasesClosed(List databases) throws Exception { 258 for (Iterator i = databases.iterator(); i.hasNext();) { 259 assertFalse(isDatabaseOpen((Database) i.next())); 260 } 261 } 262 263 private boolean isDatabaseOpen(Database db) throws Exception { 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 return false; 273 } 274 275 } 276 } | Popular Tags |