001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: TxnTestCase.java,v 1.17.2.3 2008/01/07 15:14:33 cwl Exp $
007: */
008:
009: package com.sleepycat.je.test;
010:
011: import java.io.File;
012: import java.util.Enumeration;
013:
014: import junit.framework.TestCase;
015: import junit.framework.TestSuite;
016:
017: import com.sleepycat.je.DatabaseException;
018: import com.sleepycat.je.Environment;
019: import com.sleepycat.je.EnvironmentConfig;
020: import com.sleepycat.je.Transaction;
021: import com.sleepycat.je.TransactionConfig;
022: import com.sleepycat.je.util.TestUtils;
023:
024: /**
025: * Permuates test cases over three transaction types: null (non-transactional),
026: * auto-commit, and user (explicit).
027: *
028: * <p>Overrides runTest, setUp and tearDown to open/close the environment and to
029: * set up protected members for use by test cases.</p>
030: *
031: * <p>If a subclass needs to override setUp or tearDown, the overridden method
032: * should call super.setUp or super.tearDown.</p>
033: *
034: * <p>When writing a test case based on this class, write it as if a user txn
035: * were always used: call txnBegin, txnCommit and txnAbort for all write
036: * operations. Use the isTransactional protected field for setup of a database
037: * config.</p>
038: */
039: public abstract class TxnTestCase extends TestCase {
040:
041: public static final String TXN_NULL = "txn-null";
042: public static final String TXN_AUTO = "txn-auto";
043: public static final String TXN_USER = "txn-user";
044:
045: protected File envHome;
046: protected Environment env;
047: protected EnvironmentConfig envConfig;
048: protected String txnType;
049: protected boolean isTransactional;
050:
051: /**
052: * Returns a txn test suite. If txnTypes is null, all three types are run.
053: */
054: public static TestSuite txnTestSuite(Class testCaseClass,
055: EnvironmentConfig envConfig, String[] txnTypes) {
056: if (txnTypes == null) {
057: txnTypes = new String[] { TxnTestCase.TXN_NULL,
058: TxnTestCase.TXN_USER, TxnTestCase.TXN_AUTO };
059: }
060: if (envConfig == null) {
061: envConfig = TestUtils.initEnvConfig();
062: envConfig.setAllowCreate(true);
063: envConfig.setTxnNoSync(Boolean
064: .getBoolean(TestUtils.NO_SYNC));
065: }
066: TestSuite suite = new TestSuite();
067: for (int i = 0; i < txnTypes.length; i += 1) {
068: TestSuite baseSuite = new TestSuite(testCaseClass);
069: Enumeration e = baseSuite.tests();
070: while (e.hasMoreElements()) {
071: TxnTestCase test = (TxnTestCase) e.nextElement();
072: test.txnInit(envConfig, txnTypes[i]);
073: suite.addTest(test);
074: }
075: }
076: return suite;
077: }
078:
079: private void txnInit(EnvironmentConfig envConfig, String txnType) {
080:
081: this .envConfig = envConfig;
082: this .txnType = txnType;
083: isTransactional = (txnType != TXN_NULL);
084: }
085:
086: public void setUp() throws Exception {
087:
088: envHome = new File(System.getProperty(TestUtils.DEST_DIR));
089: TestUtils.removeLogFiles("Setup", envHome, false);
090: }
091:
092: public void runTest() throws Throwable {
093:
094: openEnv();
095: super .runTest();
096: closeEnv();
097: }
098:
099: public void tearDown() throws Exception {
100:
101: /* Set test name for reporting; cannot be done in the ctor or setUp. */
102: setName(txnType + ':' + getName());
103:
104: if (env != null) {
105: try {
106: env.close();
107: } catch (Throwable e) {
108: System.out.println("tearDown: " + e);
109: }
110: env = null;
111: }
112:
113: try {
114: TestUtils.removeLogFiles("TearDown", envHome, false);
115: } catch (Throwable e) {
116: System.out.println("tearDown: " + e);
117: }
118: }
119:
120: /**
121: * Closes the environment and sets the env field to null.
122: * Used for closing and reopening the environment.
123: */
124: public void closeEnv() throws DatabaseException {
125:
126: if (env != null) {
127: env.close();
128: env = null;
129: }
130: }
131:
132: /**
133: * Opens the environment based on the txnType for this test case.
134: * Used for closing and reopening the environment.
135: */
136: public void openEnv() throws DatabaseException {
137:
138: if (txnType == TXN_NULL) {
139: envConfig.setTransactional(false);
140: env = new Environment(envHome, envConfig);
141: } else if (txnType == TXN_AUTO) {
142: envConfig.setTransactional(true);
143: env = new Environment(envHome, envConfig);
144: } else if (txnType == TXN_USER) {
145: envConfig.setTransactional(true);
146: env = new Environment(envHome, envConfig);
147: } else {
148: assert false;
149: }
150: }
151:
152: /**
153: * Begin a txn if in TXN_USER mode; otherwise return null;
154: */
155: protected Transaction txnBegin() throws DatabaseException {
156:
157: return txnBegin(null, null);
158: }
159:
160: /**
161: * Begin a txn if in TXN_USER mode; otherwise return null;
162: */
163: protected Transaction txnBegin(Transaction parentTxn,
164: TransactionConfig config) throws DatabaseException {
165:
166: if (txnType == TXN_USER) {
167: return env.beginTransaction(parentTxn, config);
168: } else {
169: return null;
170: }
171: }
172:
173: /**
174: * Begin a txn if in TXN_USER or TXN_AUTO mode; otherwise return null;
175: */
176: protected Transaction txnBeginCursor() throws DatabaseException {
177:
178: return txnBeginCursor(null, null);
179: }
180:
181: /**
182: * Begin a txn if in TXN_USER or TXN_AUTO mode; otherwise return null;
183: */
184: protected Transaction txnBeginCursor(Transaction parentTxn,
185: TransactionConfig config) throws DatabaseException {
186:
187: if (txnType == TXN_USER || txnType == TXN_AUTO) {
188: return env.beginTransaction(parentTxn, config);
189: } else {
190: return null;
191: }
192: }
193:
194: /**
195: * Commit a txn if non-null.
196: */
197: protected void txnCommit(Transaction txn) throws DatabaseException {
198:
199: if (txn != null) {
200: txn.commit();
201: }
202: }
203:
204: /**
205: * Commit a txn if non-null.
206: */
207: protected void txnAbort(Transaction txn) throws DatabaseException {
208:
209: if (txn != null) {
210: txn.abort();
211: }
212: }
213: }
|