1 8 package com.sleepycat.persist.test; 9 10 import java.util.Iterator ; 11 import java.util.List ; 12 13 import junit.framework.TestCase; 14 15 import com.sleepycat.je.DatabaseException; 16 import com.sleepycat.je.Environment; 17 import com.sleepycat.persist.EntityStore; 18 import com.sleepycat.persist.StoreConfig; 19 import com.sleepycat.persist.evolve.Mutations; 20 import com.sleepycat.persist.model.ClassMetadata; 21 import com.sleepycat.persist.model.EntityModel; 22 import com.sleepycat.persist.model.Persistent; 23 import com.sleepycat.persist.raw.RawStore; 24 import com.sleepycat.persist.raw.RawType; 25 26 @Persistent 27 abstract class EvolveCase { 28 29 static final String STORE_NAME = "foo"; 30 31 Mutations getMutations() { 32 return null; 33 } 34 35 void configure(EntityModel model, StoreConfig config) { 36 } 37 38 String getStoreOpenException() { 39 return null; 40 } 41 42 int getNRecordsExpected() { 43 return 1; 44 } 45 46 void checkUnevolvedModel(EntityModel model, Environment env) { 47 } 48 49 void checkEvolvedModel(EntityModel model, 50 Environment env, 51 boolean oldTypesExist) { 52 } 53 54 void writeObjects(EntityStore store) 55 throws DatabaseException { 56 } 57 58 void readObjects(EntityStore store, boolean doUpdate) 59 throws DatabaseException { 60 } 61 62 void readRawObjects(RawStore store, 63 boolean expectEvolved, 64 boolean expectUpdated) 65 throws DatabaseException { 66 } 67 68 void copyRawObjects(RawStore rawStore, EntityStore newStore) 69 throws DatabaseException { 70 } 71 72 76 static void checkEquals(Object expected, Object got) { 77 if ((expected != null) ? (!expected.equals(got)) : (got != null)) { 78 TestCase.fail("Expected:\n" + expected + "\nBut got:\n" + got); 79 } 80 } 81 82 85 static void assertDbExists(boolean expectExists, 86 Environment env, 87 String entityClassName) { 88 assertDbExists(expectExists, env, entityClassName, null); 89 } 90 91 94 static void checkEntity(boolean exists, 95 EntityModel model, 96 Environment env, 97 String className, 98 int version, 99 String secKeyName) { 100 if (exists) { 101 TestCase.assertNotNull(model.getEntityMetadata(className)); 102 ClassMetadata meta = model.getClassMetadata(className); 103 TestCase.assertNotNull(meta); 104 TestCase.assertEquals(version, meta.getVersion()); 105 TestCase.assertTrue(meta.isEntityClass()); 106 107 RawType raw = model.getRawType(className); 108 TestCase.assertNotNull(raw); 109 TestCase.assertEquals(version, raw.getVersion()); 110 } else { 111 TestCase.assertNull(model.getEntityMetadata(className)); 112 TestCase.assertNull(model.getClassMetadata(className)); 113 TestCase.assertNull(model.getRawType(className)); 114 } 115 116 assertDbExists(exists, env, className); 117 if (secKeyName != null) { 118 assertDbExists(exists, env, className, secKeyName); 119 } 120 } 121 122 125 static void checkNonEntity(boolean exists, 126 EntityModel model, 127 Environment env, 128 String className, 129 int version) { 130 if (exists) { 131 ClassMetadata meta = model.getClassMetadata(className); 132 TestCase.assertNotNull(meta); 133 TestCase.assertEquals(version, meta.getVersion()); 134 TestCase.assertTrue(!meta.isEntityClass()); 135 136 RawType raw = model.getRawType(className); 137 TestCase.assertNotNull(raw); 138 TestCase.assertEquals(version, raw.getVersion()); 139 } else { 140 TestCase.assertNull(model.getClassMetadata(className)); 141 TestCase.assertNull(model.getRawType(className)); 142 } 143 144 TestCase.assertNull(model.getEntityMetadata(className)); 145 assertDbExists(false, env, className); 146 } 147 148 153 static void assertDbExists(boolean expectExists, 154 Environment env, 155 String entityClassName, 156 String keyName) { 157 PersistTestUtils.assertDbExists 158 (expectExists, env, STORE_NAME, entityClassName, keyName); 159 } 160 161 static void checkVersions(EntityModel model, String name, int version) { 162 checkVersions(model, new String [] {name}, new int[] {version}); 163 } 164 165 static void checkVersions(EntityModel model, 166 String name1, 167 int version1, 168 String name2, 169 int version2) { 170 checkVersions 171 (model, new String [] {name1, name2}, 172 new int[] {version1, version2}); 173 } 174 175 private static void checkVersions(EntityModel model, 176 String [] names, 177 int[] versions) { 178 List <RawType> all = model.getAllRawTypeVersions(names[0]); 179 TestCase.assertNotNull(all); 180 181 assert names.length == versions.length; 182 TestCase.assertEquals(names.length, all.size()); 183 184 Iterator <RawType> iter = all.iterator(); 185 for (int i = 0; i < names.length; i += 1) { 186 RawType type = iter.next(); 187 TestCase.assertEquals(versions[i], type.getVersion()); 188 TestCase.assertEquals(names[i], type.getClassName()); 189 } 190 } 191 } 192 | Popular Tags |