1 8 9 package com.sleepycat.je.test; 10 11 import java.io.File ; 12 import java.util.Enumeration ; 13 14 import junit.framework.TestCase; 15 import junit.framework.TestSuite; 16 17 import com.sleepycat.je.DatabaseException; 18 import com.sleepycat.je.Environment; 19 import com.sleepycat.je.EnvironmentConfig; 20 import com.sleepycat.je.Transaction; 21 import com.sleepycat.je.TransactionConfig; 22 import com.sleepycat.je.util.TestUtils; 23 24 39 public abstract class TxnTestCase extends TestCase { 40 41 public static final String TXN_NULL = "txn-null"; 42 public static final String TXN_AUTO = "txn-auto"; 43 public static final String TXN_USER = "txn-user"; 44 45 protected File envHome; 46 protected Environment env; 47 protected EnvironmentConfig envConfig; 48 protected String txnType; 49 protected boolean isTransactional; 50 51 54 public static TestSuite txnTestSuite(Class testCaseClass, 55 EnvironmentConfig envConfig, 56 String [] txnTypes) { 57 if (txnTypes == null) { 58 txnTypes = new String [] { TxnTestCase.TXN_NULL, 59 TxnTestCase.TXN_USER, 60 TxnTestCase.TXN_AUTO }; 61 } 62 if (envConfig == null) { 63 envConfig = TestUtils.initEnvConfig(); 64 envConfig.setAllowCreate(true); 65 envConfig.setTxnNoSync(Boolean.getBoolean(TestUtils.NO_SYNC)); 66 } 67 TestSuite suite = new TestSuite(); 68 for (int i = 0; i < txnTypes.length; i += 1) { 69 TestSuite baseSuite = new TestSuite(testCaseClass); 70 Enumeration e = baseSuite.tests(); 71 while (e.hasMoreElements()) { 72 TxnTestCase test = (TxnTestCase) e.nextElement(); 73 test.txnInit(envConfig, txnTypes[i]); 74 suite.addTest(test); 75 } 76 } 77 return suite; 78 } 79 80 private void txnInit(EnvironmentConfig envConfig, String txnType) { 81 82 this.envConfig = envConfig; 83 this.txnType = txnType; 84 isTransactional = (txnType != TXN_NULL); 85 } 86 87 public void setUp() 88 throws Exception { 89 90 envHome = new File (System.getProperty(TestUtils.DEST_DIR)); 91 TestUtils.removeLogFiles("Setup", envHome, false); 92 } 93 94 public void runTest() 95 throws Throwable { 96 97 openEnv(); 98 super.runTest(); 99 closeEnv(); 100 } 101 102 public void tearDown() 103 throws Exception { 104 105 106 setName(txnType + ':' + getName()); 107 108 if (env != null) { 109 try { 110 env.close(); 111 } catch (Throwable e) { 112 System.out.println("tearDown: " + e); 113 } 114 env = null; 115 } 116 117 try { 118 TestUtils.removeLogFiles("TearDown", envHome, false); 119 } catch (Throwable e) { 120 System.out.println("tearDown: " + e); 121 } 122 } 123 124 128 public void closeEnv() 129 throws DatabaseException { 130 131 if (env != null) { 132 env.close(); 133 env = null; 134 } 135 } 136 137 141 public void openEnv() 142 throws DatabaseException { 143 144 if (txnType == TXN_NULL) { 145 envConfig.setTransactional(false); 146 env = new Environment(envHome, envConfig); 147 } else if (txnType == TXN_AUTO) { 148 envConfig.setTransactional(true); 149 env = new Environment(envHome, envConfig); 150 } else if (txnType == TXN_USER) { 151 envConfig.setTransactional(true); 152 env = new Environment(envHome, envConfig); 153 } else { 154 assert false; 155 } 156 } 157 158 161 protected Transaction txnBegin() 162 throws DatabaseException { 163 164 return txnBegin(null, null); 165 } 166 167 170 protected Transaction txnBegin(Transaction parentTxn, 171 TransactionConfig config) 172 throws DatabaseException { 173 174 if (txnType == TXN_USER) { 175 return env.beginTransaction(parentTxn, config); 176 } else { 177 return null; 178 } 179 } 180 181 184 protected Transaction txnBeginCursor() 185 throws DatabaseException { 186 187 return txnBeginCursor(null, null); 188 } 189 190 193 protected Transaction txnBeginCursor(Transaction parentTxn, 194 TransactionConfig config) 195 throws DatabaseException { 196 197 if (txnType == TXN_USER || txnType == TXN_AUTO) { 198 return env.beginTransaction(parentTxn, config); 199 } else { 200 return null; 201 } 202 } 203 204 207 protected void txnCommit(Transaction txn) 208 throws DatabaseException { 209 210 if (txn != null) { 211 txn.commit(); 212 } 213 } 214 215 218 protected void txnAbort(Transaction txn) 219 throws DatabaseException { 220 221 if (txn != null) { 222 txn.abort(); 223 } 224 } 225 } 226 | Popular Tags |