1 8 package com.sleepycat.persist.test; 9 10 import java.io.File ; 11 import java.util.Enumeration ; 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 32 public class EvolveTestBase extends TestCase { 33 34 File envHome; 35 Environment env; 36 EntityStore store; 37 RawStore rawStore; 38 EntityStore newStore; 39 int caseIndex; 40 Class <? extends EvolveCase> caseCls; 41 EvolveCase caseObj; 42 43 static Test getSuite(Class testClass) 44 throws Exception { 45 46 TestSuite suite = new TestSuite(); 47 for (int caseIndex = 0; 48 caseIndex < EvolveClasses.ALL.size(); 49 caseIndex += 1) { 50 Class <? extends EvolveCase> caseCls = 51 EvolveClasses.ALL.get(caseIndex); 52 TestSuite baseSuite = new TestSuite(testClass); 53 Enumeration 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 <? extends EvolveCase> caseCls) 64 throws Exception { 65 66 this.caseIndex = caseIndex; 67 this.caseCls = caseCls; 68 caseObj = caseCls.newInstance(); 69 } 70 71 File getTestInitHome() { 72 return new File 73 (System.getProperty(TestUtils.DEST_DIR), 74 "../testevolve/C" + caseIndex); 75 } 76 77 @Override 78 public void tearDown() { 79 80 81 String 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 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 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 117 private boolean openStore(StoreConfig config) 118 throws Exception { 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 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 e) { 134 if (expectException != null) { 135 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 { 147 148 StoreConfig config = new StoreConfig(); 149 config.setReadOnly(true); 150 return openStore(config); 151 } 152 153 boolean openStoreReadWrite() 154 throws Exception { 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 { 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 |