KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > persist > test > EvolveTestBase


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

8 package com.sleepycat.persist.test;
9
10 import java.io.File JavaDoc;
11 import java.util.Enumeration JavaDoc;
12
13 import junit.framework.Test;
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.util.TestUtils;
21 import com.sleepycat.persist.EntityStore;
22 import com.sleepycat.persist.StoreConfig;
23 import com.sleepycat.persist.model.AnnotationModel;
24 import com.sleepycat.persist.model.EntityModel;
25 import com.sleepycat.persist.raw.RawStore;
26
27 /**
28  * Base class for EvolveTest and EvolveTestInit.
29  *
30  * @author Mark Hayes
31  */

32 public class EvolveTestBase extends TestCase {
33
34     File JavaDoc envHome;
35     Environment env;
36     EntityStore store;
37     RawStore rawStore;
38     EntityStore newStore;
39     int caseIndex;
40     Class JavaDoc<? extends EvolveCase> caseCls;
41     EvolveCase caseObj;
42
43     static Test getSuite(Class JavaDoc testClass)
44         throws Exception JavaDoc {
45
46         TestSuite suite = new TestSuite();
47         for (int caseIndex = 0;
48              caseIndex < EvolveClasses.ALL.size();
49              caseIndex += 1) {
50             Class JavaDoc<? extends EvolveCase> caseCls =
51                 EvolveClasses.ALL.get(caseIndex);
52             TestSuite baseSuite = new TestSuite(testClass);
53             Enumeration JavaDoc e = baseSuite.tests();
54             while (e.hasMoreElements()) {
55                 EvolveTestBase test = (EvolveTestBase) e.nextElement();
56                 test.init(caseIndex, caseCls);
57                 suite.addTest(test);
58             }
59         }
60         return suite;
61     }
62
63     private void init(int caseIndex, Class JavaDoc<? extends EvolveCase> caseCls)
64         throws Exception JavaDoc {
65
66         this.caseIndex = caseIndex;
67         this.caseCls = caseCls;
68         caseObj = caseCls.newInstance();
69     }
70
71     File JavaDoc getTestInitHome() {
72         return new File JavaDoc
73             (System.getProperty(TestUtils.DEST_DIR),
74              "../testevolve/C" + caseIndex);
75     }
76
77     @Override JavaDoc
78     public void tearDown() {
79
80         /* Set test name for reporting; cannot be done in the ctor or setUp. */
81         String JavaDoc caseClsName = caseCls.getName();
82         caseClsName = caseClsName.substring(caseClsName.lastIndexOf('$') + 1);
83         setName(String.valueOf(caseIndex) + ':' +
84                 caseClsName + '-' +
85                 getName());
86
87         if (env != null) {
88             try {
89                 closeAll();
90             } catch (Throwable JavaDoc e) {
91                 System.out.println("During tearDown: " + e);
92             }
93         }
94         envHome = null;
95         env = null;
96         store = null;
97         caseCls = null;
98         caseObj = null;
99
100         /* Do not delete log files so they can be used by 2nd phase of test. */
101     }
102
103     void openEnv()
104         throws DatabaseException {
105
106         EnvironmentConfig config = new EnvironmentConfig();
107         config.setAllowCreate(true);
108         config.setTransactional(true);
109         env = new Environment(envHome, config);
110     }
111
112     /**
113      * Returns true if the store was opened successfully. Returns false if the
114      * store could not be opened because an exception was expected -- this is
115      * not a test failure but no further tests for an EntityStore may be run.
116      */

117     private boolean openStore(StoreConfig config)
118         throws Exception JavaDoc {
119
120         config.setTransactional(true);
121         config.setMutations(caseObj.getMutations());
122
123         EntityModel model = new AnnotationModel();
124         config.setModel(model);
125         caseObj.configure(model, config);
126
127         String JavaDoc expectException = caseObj.getStoreOpenException();
128         try {
129             store = new EntityStore(env, EvolveCase.STORE_NAME, config);
130             if (expectException != null) {
131                 fail("Expected: " + expectException);
132             }
133         } catch (Exception JavaDoc e) {
134             if (expectException != null) {
135                 //e.printStackTrace();
136
EvolveCase.checkEquals(expectException, e.toString());
137                 return false;
138             } else {
139                 throw e;
140             }
141         }
142         return true;
143     }
144
145     boolean openStoreReadOnly()
146         throws Exception JavaDoc {
147
148         StoreConfig config = new StoreConfig();
149         config.setReadOnly(true);
150         return openStore(config);
151     }
152
153     boolean openStoreReadWrite()
154         throws Exception JavaDoc {
155
156         StoreConfig config = new StoreConfig();
157         config.setAllowCreate(true);
158         return openStore(config);
159     }
160
161     void openRawStore()
162         throws DatabaseException {
163
164         rawStore = new RawStore(env, EvolveCase.STORE_NAME, null);
165     }
166
167     void closeStore()
168         throws DatabaseException {
169
170         if (store != null) {
171             store.close();
172             store = null;
173         }
174     }
175
176     void openNewStore()
177         throws Exception JavaDoc {
178
179         StoreConfig config = new StoreConfig();
180         config.setAllowCreate(true);
181         config.setTransactional(true);
182
183         EntityModel model = new AnnotationModel();
184         config.setModel(model);
185         caseObj.configure(model, config);
186
187         newStore = new EntityStore(env, "new", config);
188     }
189
190     void closeNewStore()
191         throws DatabaseException {
192
193         if (newStore != null) {
194             newStore.close();
195             newStore = null;
196         }
197     }
198
199     void closeRawStore()
200         throws DatabaseException {
201
202         if (rawStore != null) {
203             rawStore.close();
204             rawStore = null;
205         }
206     }
207
208     void closeEnv()
209         throws DatabaseException {
210
211         if (env != null) {
212             env.close();
213             env = null;
214         }
215     }
216
217     void closeAll()
218         throws DatabaseException {
219
220         closeStore();
221         closeRawStore();
222         closeNewStore();
223         closeEnv();
224     }
225 }
226
Popular Tags