1 8 9 package com.sleepycat.collections.test; 10 11 import java.io.File ; 12 import java.io.IOException ; 13 14 import com.sleepycat.compat.DbCompat; 15 import com.sleepycat.je.DatabaseException; 16 import com.sleepycat.je.Environment; 17 import com.sleepycat.je.EnvironmentConfig; 18 19 22 public class TestEnv { 23 24 public static final TestEnv BDB; 25 public static final TestEnv CDB; 26 public static final TestEnv TXN; 27 static { 28 EnvironmentConfig config; 29 30 config = newEnvConfig(); 31 BDB = new TestEnv("bdb", config); 32 33 if (DbCompat.CDB) { 34 config = newEnvConfig(); 35 DbCompat.setInitializeCDB(config, true); 36 CDB = new TestEnv("cdb", config); 37 } else { 38 CDB = null; 39 } 40 41 config = newEnvConfig(); 42 config.setTransactional(true); 43 DbCompat.setInitializeLocking(config, true); 44 TXN = new TestEnv("txn", config); 45 } 46 47 private static EnvironmentConfig newEnvConfig() { 48 49 EnvironmentConfig config = new EnvironmentConfig(); 50 if (DbCompat.MEMORY_SUBSYSTEM) { 51 DbCompat.setInitializeCache(config, true); 52 } 53 return config; 54 } 55 56 public static final TestEnv[] ALL; 57 static { 58 if (DbCompat.CDB) { 59 ALL = new TestEnv[] { BDB, CDB, TXN }; 60 } else { 61 ALL = new TestEnv[] { BDB, TXN }; 62 } 63 } 64 65 private String name; 66 private EnvironmentConfig config; 67 68 TestEnv(String name, EnvironmentConfig config) { 69 70 this.name = name; 71 this.config = config; 72 } 73 74 public String getName() { 75 76 return name; 77 } 78 79 public boolean isTxnMode() { 80 81 return config.getTransactional(); 82 } 83 84 public boolean isCdbMode() { 85 86 return DbCompat.getInitializeCDB(config); 87 } 88 89 public Environment open(String testName) 90 throws IOException , DatabaseException { 91 92 return open(testName, true); 93 } 94 95 public Environment open(String testName, boolean create) 96 throws IOException , DatabaseException { 97 98 config.setAllowCreate(create); 99 100 DbCompat.setLockDetectModeOldest(config); 101 File dir = getDirectory(testName, create); 102 return newEnvironment(dir, config); 103 } 104 105 108 protected Environment newEnvironment(File dir, EnvironmentConfig config) 109 throws DatabaseException, IOException { 110 111 return new Environment(dir, config); 112 } 113 114 public File getDirectory(String testName) 115 throws IOException { 116 117 return getDirectory(testName, true); 118 } 119 120 public File getDirectory(String testName, boolean create) 121 throws IOException { 122 123 if (create) { 124 return DbTestUtil.getNewDir("db-test/" + testName); 125 } else { 126 return DbTestUtil.getExistingDir("db-test/" + testName); 127 } 128 } 129 } 130 | Popular Tags |