KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > test > TxnTestCase


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: TxnTestCase.java,v 1.17 2006/10/30 21:14:50 bostic Exp $
7  */

8
9 package com.sleepycat.je.test;
10
11 import java.io.File JavaDoc;
12 import java.util.Enumeration JavaDoc;
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 /**
25  * Permuates test cases over three transaction types: null (non-transactional),
26  * auto-commit, and user (explicit).
27  *
28  * <p>Overrides runTest, setUp and tearDown to open/close the environment and to
29  * set up protected members for use by test cases.</p>
30  *
31  * <p>If a subclass needs to override setUp or tearDown, the overridden method
32  * should call super.setUp or super.tearDown.</p>
33  *
34  * <p>When writing a test case based on this class, write it as if a user txn
35  * were always used: call txnBegin, txnCommit and txnAbort for all write
36  * operations. Use the isTransactional protected field for setup of a database
37  * config.</p>
38  */

39 public abstract class TxnTestCase extends TestCase {
40
41     public static final String JavaDoc TXN_NULL = "txn-null";
42     public static final String JavaDoc TXN_AUTO = "txn-auto";
43     public static final String JavaDoc TXN_USER = "txn-user";
44
45     protected File JavaDoc envHome;
46     protected Environment env;
47     protected EnvironmentConfig envConfig;
48     protected String JavaDoc txnType;
49     protected boolean isTransactional;
50
51     /**
52      * Returns a txn test suite. If txnTypes is null, all three types are run.
53      */

54     public static TestSuite txnTestSuite(Class JavaDoc testCaseClass,
55                                          EnvironmentConfig envConfig,
56                                          String JavaDoc[] txnTypes) {
57         if (txnTypes == null) {
58             txnTypes = new String JavaDoc[] { 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 JavaDoc 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 JavaDoc txnType) {
81         
82         this.envConfig = envConfig;
83         this.txnType = txnType;
84         isTransactional = (txnType != TXN_NULL);
85     }
86
87     public void setUp()
88         throws Exception JavaDoc {
89
90         envHome = new File JavaDoc(System.getProperty(TestUtils.DEST_DIR));
91         TestUtils.removeLogFiles("Setup", envHome, false);
92     }
93
94     public void runTest()
95         throws Throwable JavaDoc {
96
97         openEnv();
98         super.runTest();
99         closeEnv();
100     }
101     
102     public void tearDown()
103         throws Exception JavaDoc {
104         
105         /* Set test name for reporting; cannot be done in the ctor or setUp. */
106         setName(txnType + ':' + getName());
107
108         if (env != null) {
109             try {
110                 env.close();
111             } catch (Throwable JavaDoc e) {
112                 System.out.println("tearDown: " + e);
113             }
114             env = null;
115         }
116
117         try {
118             TestUtils.removeLogFiles("TearDown", envHome, false);
119         } catch (Throwable JavaDoc e) {
120             System.out.println("tearDown: " + e);
121         }
122     }
123
124     /**
125      * Closes the environment and sets the env field to null.
126      * Used for closing and reopening the environment.
127      */

128     public void closeEnv()
129         throws DatabaseException {
130
131         if (env != null) {
132             env.close();
133             env = null;
134         }
135     }
136
137     /**
138      * Opens the environment based on the txnType for this test case.
139      * Used for closing and reopening the environment.
140      */

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     /**
159      * Begin a txn if in TXN_USER mode; otherwise return null;
160      */

161     protected Transaction txnBegin()
162         throws DatabaseException {
163
164         return txnBegin(null, null);
165     }
166
167     /**
168      * Begin a txn if in TXN_USER mode; otherwise return null;
169      */

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     /**
182      * Begin a txn if in TXN_USER or TXN_AUTO mode; otherwise return null;
183      */

184     protected Transaction txnBeginCursor()
185         throws DatabaseException {
186
187         return txnBeginCursor(null, null);
188     }
189
190     /**
191      * Begin a txn if in TXN_USER or TXN_AUTO mode; otherwise return null;
192      */

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     /**
205      * Commit a txn if non-null.
206      */

207     protected void txnCommit(Transaction txn)
208         throws DatabaseException {
209         
210         if (txn != null) {
211             txn.commit();
212         }
213     }
214
215     /**
216      * Commit a txn if non-null.
217      */

218     protected void txnAbort(Transaction txn)
219         throws DatabaseException {
220         
221         if (txn != null) {
222             txn.abort();
223         }
224     }
225 }
226
Popular Tags