1 21 package db4ounit.extensions; 22 23 import com.db4o.*; 24 import com.db4o.config.Configuration; 25 import com.db4o.ext.ExtObjectContainer; 26 import com.db4o.foundation.*; 27 import com.db4o.query.Query; 28 import com.db4o.reflect.*; 29 30 import db4ounit.*; 31 import db4ounit.extensions.fixtures.*; 32 33 public class AbstractDb4oTestCase implements Db4oTestCase { 34 35 private transient Db4oFixture _fixture; 36 37 40 public void fixture(Db4oFixture fixture) { 41 _fixture = fixture; 42 } 43 44 47 public Db4oFixture fixture() { 48 return _fixture; 49 } 50 51 public boolean isClientServer() { 52 return fixture() instanceof AbstractClientServerDb4oFixture; 53 } 54 55 protected void reopen() throws Exception { 56 _fixture.reopen(); 57 } 58 59 public final void setUp() throws Exception { 60 _fixture.clean(); 61 configure(_fixture.config()); 62 _fixture.open(); 63 db4oSetupBeforeStore(); 64 store(); 65 _fixture.db().commit(); 66 _fixture.close(); 67 _fixture.open(); 68 db4oSetupAfterStore(); 69 } 70 71 public final void tearDown() throws Exception { 72 db4oCustomTearDown(); 73 _fixture.close(); 74 _fixture.clean(); 75 } 76 77 protected void db4oSetupBeforeStore() throws Exception {} 78 protected void db4oSetupAfterStore() throws Exception {} 79 protected void db4oCustomTearDown() throws Exception {} 80 81 protected void configure(Configuration config) {} 82 83 protected void store() throws Exception {} 84 85 88 public ExtObjectContainer db() { 89 return fixture().db(); 90 } 91 92 protected Class [] testCases() { 93 return new Class [] { getClass() }; 94 } 95 96 public int runSoloAndClientServer() { 97 return runSoloAndClientServer(true); 98 } 99 100 private int runSoloAndClientServer(final boolean independentConfig) { 101 return new TestRunner(new TestSuite(new Test[] { 102 soloSuite(independentConfig).build(), 103 clientServerSuite(independentConfig).build(), 104 })).run(); 105 } 106 107 public int runSolo() { 108 return runSolo(true); 109 } 110 111 public int runSolo(boolean independentConfig) { 112 return new TestRunner( 113 soloSuite(independentConfig)).run(); 114 } 115 116 public int runClientServer() { 117 return runClientServer(true); 118 } 119 120 public int runClientServer(boolean independentConfig) { 121 return new TestRunner( 122 clientServerSuite(independentConfig)).run(); 123 } 124 125 private Db4oTestSuiteBuilder soloSuite(boolean independentConfig) { 126 return new Db4oTestSuiteBuilder( 127 new Db4oSolo(configSource(independentConfig)), testCases()); 128 } 129 130 private Db4oTestSuiteBuilder clientServerSuite(boolean independentConfig) { 131 return new Db4oTestSuiteBuilder( 132 new Db4oSingleClient(configSource(independentConfig)), 133 testCases()); 134 } 135 136 private ConfigurationSource configSource(boolean independentConfig) { 137 return (independentConfig ? (ConfigurationSource)new IndependentConfigurationSource() : new GlobalConfigurationSource()); 138 } 139 140 protected YapStream stream() { 141 return (YapStream) db(); 142 } 143 144 protected YapFile fileSession() { 145 return fixture().fileSession(); 146 } 147 148 protected Transaction trans() { 149 return stream().getTransaction(); 150 } 151 152 protected Transaction systemTrans() { 153 return stream().getSystemTransaction(); 154 } 155 156 protected Query newQuery(Transaction transaction, Class clazz) { 157 final Query query = newQuery(transaction); 158 query.constrain(clazz); 159 return query; 160 } 161 162 protected Query newQuery(Transaction transaction) { 163 return stream().query(transaction); 164 } 165 166 protected Query newQuery(){ 167 return db().query(); 168 } 169 170 protected Reflector reflector(){ 171 return stream().reflector(); 172 } 173 174 protected void indexField(Configuration config,Class clazz, String fieldName) { 175 config.objectClass(clazz).objectField(fieldName).indexed(true); 176 } 177 178 protected Transaction newTransaction() { 179 return stream().newTransaction(); 180 } 181 182 protected Query newQuery(Class clazz) { 183 final Query query = newQuery(); 184 query.constrain(clazz); 185 return query; 186 } 187 188 protected Object retrieveOnlyInstance(Class clazz) { 189 ObjectSet result=newQuery(clazz).execute(); 190 Assert.areEqual(1,result.size()); 191 return result.next(); 192 } 193 194 protected int countOccurences(Class clazz) { 195 ObjectSet result = newQuery(clazz).execute(); 196 return result.size(); 197 } 198 199 protected void foreach(Class clazz, Visitor4 visitor) { 200 ExtObjectContainer oc = db(); 201 oc.deactivate(clazz, Integer.MAX_VALUE); 202 ObjectSet set = newQuery(clazz).execute(); 203 while (set.hasNext()) { 204 visitor.visit(set.next()); 205 } 206 } 207 208 protected void deleteAll(Class clazz) { 209 foreach(clazz, new Visitor4() { 210 public void visit(Object obj) { 211 db().delete(obj); 212 } 213 }); 214 } 215 216 protected final void store(Object obj) { 217 db().set(obj); 218 } 219 220 protected ReflectClass reflectClass(Class clazz) { 221 return reflector().forClass(clazz); 222 } 223 } 224 | Popular Tags |