1/*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 2000,2008 Oracle. All rights reserved. 5 * 6 * $Id: EvolveTestBase.java,v 1.1 2008/02/07 17:12:32 mark Exp $ 7 */ 8package com.sleepycat.persist.test; 9 10import java.io.File; 11import java.io.IOException; 12import java.util.Enumeration; 13 14import junit.framework.TestCase; 15import junit.framework.TestSuite; 16 17import com.sleepycat.db.DatabaseException; 18import com.sleepycat.db.Environment; 19import com.sleepycat.db.EnvironmentConfig; 20import com.sleepycat.persist.EntityStore; 21import com.sleepycat.persist.StoreConfig; 22import com.sleepycat.persist.model.AnnotationModel; 23import com.sleepycat.persist.model.EntityModel; 24import com.sleepycat.persist.raw.RawStore; 25import com.sleepycat.util.test.TestEnv; 26 27/** 28 * Base class for EvolveTest and EvolveTestInit. 29 * 30 * @author Mark Hayes 31 */ 32public abstract class EvolveTestBase extends TestCase { 33 34 /* 35 * When adding a evolve test class, three places need to be changed: 36 * 1) Add the unmodified class to EvolveClass.java.original. 37 * 2) Add the modified class to EvolveClass.java. 38 * 3) Add the class name to the ALL list below as a pair of strings. The 39 * first string in each pair is the name of the original class, and the 40 * second string is the name of the evolved class or null if the evolved 41 * name is the same as the original. The index in the list identifies a 42 * test case, and the class at that position identifies the old and new 43 * class to use for the test. 44 */ 45 private static final String[] ALL = { 46 "DeletedEntity1_ClassRemoved", 47 "DeletedEntity1_ClassRemoved_NoMutation", 48 "DeletedEntity2_ClassRemoved", 49 "DeletedEntity2_ClassRemoved_WithDeleter", 50 "DeletedEntity3_AnnotRemoved_NoMutation", 51 null, 52 "DeletedEntity4_AnnotRemoved_WithDeleter", 53 null, 54 "DeletedEntity5_EntityToPersist_NoMutation", 55 null, 56 "DeletedEntity6_EntityToPersist_WithDeleter", 57 null, 58 "DeletedPersist1_ClassRemoved_NoMutation", 59 null, 60 "DeletedPersist2_ClassRemoved_WithDeleter", 61 null, 62 "DeletedPersist3_AnnotRemoved_NoMutation", 63 null, 64 "DeletedPersist4_AnnotRemoved_WithDeleter", 65 null, 66 "DeletedPersist5_PersistToEntity_NoMutation", 67 null, 68 "DeletedPersist6_PersistToEntity_WithDeleter", 69 null, 70 "RenamedEntity1_NewEntityName", 71 "RenamedEntity1_NewEntityName_NoMutation", 72 "RenamedEntity2_NewEntityName", 73 "RenamedEntity2_NewEntityName_WithRenamer", 74 "DeleteSuperclass1_NoMutation", 75 null, 76 "DeleteSuperclass2_WithConverter", 77 null, 78 "DeleteSuperclass3_WithDeleter", 79 null, 80 "DeleteSuperclass4_NoFields", 81 null, 82 "DeleteSuperclass5_Top", 83 null, 84 "InsertSuperclass1_Between", 85 null, 86 "InsertSuperclass2_Top", 87 null, 88 "DisallowNonKeyField_PrimitiveToObject", 89 null, 90 "DisallowNonKeyField_ObjectToPrimitive", 91 null, 92 "DisallowNonKeyField_ObjectToSubtype", 93 null, 94 "DisallowNonKeyField_ObjectToUnrelatedSimple", 95 null, 96 "DisallowNonKeyField_ObjectToUnrelatedOther", 97 null, 98 "DisallowNonKeyField_byte2boolean", 99 null, 100 "DisallowNonKeyField_short2byte", 101 null, 102 "DisallowNonKeyField_int2short", 103 null, 104 "DisallowNonKeyField_long2int", 105 null, 106 "DisallowNonKeyField_float2long", 107 null, 108 "DisallowNonKeyField_double2float", 109 null, 110 "DisallowNonKeyField_Byte2byte", 111 null, 112 "DisallowNonKeyField_Character2char", 113 null, 114 "DisallowNonKeyField_Short2short", 115 null, 116 "DisallowNonKeyField_Integer2int", 117 null, 118 "DisallowNonKeyField_Long2long", 119 null, 120 "DisallowNonKeyField_Float2float", 121 null, 122 "DisallowNonKeyField_Double2double", 123 null, 124 "DisallowNonKeyField_float2BigInt", 125 null, 126 "DisallowNonKeyField_BigInt2long", 127 null, 128 "DisallowSecKeyField_byte2short", 129 null, 130 "DisallowSecKeyField_char2int", 131 null, 132 "DisallowSecKeyField_short2int", 133 null, 134 "DisallowSecKeyField_int2long", 135 null, 136 "DisallowSecKeyField_long2float", 137 null, 138 "DisallowSecKeyField_float2double", 139 null, 140 "DisallowSecKeyField_Byte2short2", 141 null, 142 "DisallowSecKeyField_Character2int", 143 null, 144 "DisallowSecKeyField_Short2int2", 145 null, 146 "DisallowSecKeyField_Integer2long", 147 null, 148 "DisallowSecKeyField_Long2float2", 149 null, 150 "DisallowSecKeyField_Float2double2", 151 null, 152 "DisallowSecKeyField_int2BigInt", 153 null, 154 "DisallowPriKeyField_byte2short", 155 null, 156 "DisallowPriKeyField_char2int", 157 null, 158 "DisallowPriKeyField_short2int", 159 null, 160 "DisallowPriKeyField_int2long", 161 null, 162 "DisallowPriKeyField_long2float", 163 null, 164 "DisallowPriKeyField_float2double", 165 null, 166 "DisallowPriKeyField_Byte2short2", 167 null, 168 "DisallowPriKeyField_Character2int", 169 null, 170 "DisallowPriKeyField_Short2int2", 171 null, 172 "DisallowPriKeyField_Integer2long", 173 null, 174 "DisallowPriKeyField_Long2float2", 175 null, 176 "DisallowPriKeyField_Float2double2", 177 null, 178 "DisallowPriKeyField_Long2BigInt", 179 null, 180 "DisallowCompositeKeyField_byte2short", 181 null, 182 "AllowPriKeyField_Byte2byte2", 183 null, 184 "AllowPriKeyField_byte2Byte", 185 null, 186 "AllowFieldTypeChanges", 187 null, 188 "ConvertExample1_Entity", 189 null, 190 "ConvertExample2_Person", 191 null, 192 "ConvertExample3_Person", 193 null, 194 "ConvertExample4_Entity", 195 null, 196 "ConvertExample5_Entity", 197 null, 198 "AllowFieldAddDelete", 199 null, 200 "ProxiedClass_Entity", 201 null, 202 "DisallowChangeProxyFor", 203 null, 204 "DisallowDeleteProxyFor", 205 null, 206 "ArrayNameChange_Entity", 207 null, 208 "AddEnumConstant_Entity", 209 null, 210 "DeleteEnumConstant_NoMutation", 211 null, 212 "DisallowChangeKeyRelate", 213 null, 214 "AllowChangeKeyMetadata", 215 null, 216 "AllowAddSecondary", 217 null, 218 "FieldAddAndConvert", 219 null, 220 }; 221 222 File envHome; 223 Environment env; 224 EntityStore store; 225 RawStore rawStore; 226 EntityStore newStore; 227 String caseClsName; 228 Class caseCls; 229 EvolveCase caseObj; 230 String caseLabel; 231 232 static TestSuite getSuite(Class testClass) 233 throws Exception { 234 235 TestSuite suite = new TestSuite(); 236 for (int i = 0; i < ALL.length; i += 2) { 237 String originalClsName = ALL[i]; 238 String evolvedClsName = ALL[i + 1]; 239 if (evolvedClsName == null) { 240 evolvedClsName = originalClsName; 241 } 242 TestSuite baseSuite = new TestSuite(testClass); 243 Enumeration e = baseSuite.tests(); 244 while (e.hasMoreElements()) { 245 EvolveTestBase test = (EvolveTestBase) e.nextElement(); 246 test.init(originalClsName, evolvedClsName); 247 suite.addTest(test); 248 } 249 } 250 return suite; 251 } 252 253 private void init(String originalClsName, 254 String evolvedClsName) 255 throws Exception { 256 257 String caseClsName = useEvolvedClass() ? 258 evolvedClsName : originalClsName; 259 caseClsName = "com.sleepycat.persist.test.EvolveClasses$" + 260 caseClsName; 261 262 this.caseClsName = caseClsName; 263 this.caseCls = Class.forName(caseClsName); 264 this.caseObj = (EvolveCase) caseCls.newInstance(); 265 this.caseLabel = evolvedClsName; 266 } 267 268 abstract boolean useEvolvedClass(); 269 270 File getTestInitHome(boolean evolved) { 271 return new File 272 (System.getProperty("testevolvedir"), 273 (evolved ? "evolved" : "original") + '/' + caseLabel); 274 } 275 276 @Override 277 public void tearDown() { 278 279 /* Set test name for reporting; cannot be done in the ctor or setUp. */ 280 setName(caseLabel + '-' + getName()); 281 282 if (env != null) { 283 try { 284 closeAll(); 285 } catch (Throwable e) { 286 System.out.println("During tearDown: " + e); 287 } 288 } 289 envHome = null; 290 env = null; 291 store = null; 292 caseCls = null; 293 caseObj = null; 294 caseLabel = null; 295 296 /* Do not delete log files so they can be used by 2nd phase of test. */ 297 } 298 299 void openEnv() 300 throws IOException, DatabaseException { 301 302 EnvironmentConfig config = TestEnv.TXN.getConfig(); 303 config.setAllowCreate(true); 304 env = new Environment(envHome, config); 305 } 306 307 /** 308 * Returns true if the store was opened successfully. Returns false if the 309 * store could not be opened because an exception was expected -- this is 310 * not a test failure but no further tests for an EntityStore may be run. 311 */ 312 private boolean openStore(StoreConfig config) 313 throws Exception { 314 315 config.setTransactional(true); 316 config.setMutations(caseObj.getMutations()); 317 318 EntityModel model = new AnnotationModel(); 319 config.setModel(model); 320 caseObj.configure(model, config); 321 322 String expectException = caseObj.getStoreOpenException(); 323 try { 324 store = new EntityStore(env, EvolveCase.STORE_NAME, config); 325 if (expectException != null) { 326 fail("Expected: " + expectException); 327 } 328 } catch (Exception e) { 329 if (expectException != null) { 330 //e.printStackTrace(); 331 EvolveCase.checkEquals(expectException, e.toString()); 332 return false; 333 } else { 334 throw e; 335 } 336 } 337 return true; 338 } 339 340 boolean openStoreReadOnly() 341 throws Exception { 342 343 StoreConfig config = new StoreConfig(); 344 config.setReadOnly(true); 345 return openStore(config); 346 } 347 348 boolean openStoreReadWrite() 349 throws Exception { 350 351 StoreConfig config = new StoreConfig(); 352 config.setAllowCreate(true); 353 return openStore(config); 354 } 355 356 void openRawStore() 357 throws DatabaseException { 358 359 rawStore = new RawStore(env, EvolveCase.STORE_NAME, null); 360 } 361 362 void closeStore() 363 throws DatabaseException { 364 365 if (store != null) { 366 store.close(); 367 store = null; 368 } 369 } 370 371 void openNewStore() 372 throws Exception { 373 374 StoreConfig config = new StoreConfig(); 375 config.setAllowCreate(true); 376 config.setTransactional(true); 377 378 EntityModel model = new AnnotationModel(); 379 config.setModel(model); 380 caseObj.configure(model, config); 381 382 newStore = new EntityStore(env, "new", config); 383 } 384 385 void closeNewStore() 386 throws DatabaseException { 387 388 if (newStore != null) { 389 newStore.close(); 390 newStore = null; 391 } 392 } 393 394 void closeRawStore() 395 throws DatabaseException { 396 397 if (rawStore != null) { 398 rawStore.close(); 399 rawStore = null; 400 } 401 } 402 403 void closeEnv() 404 throws DatabaseException { 405 406 if (env != null) { 407 env.close(); 408 env = null; 409 } 410 } 411 412 void closeAll() 413 throws DatabaseException { 414 415 closeStore(); 416 closeRawStore(); 417 closeNewStore(); 418 closeEnv(); 419 } 420} 421