001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package db4ounit.extensions;
022:
023: import com.db4o.*;
024: import com.db4o.config.*;
025: import com.db4o.events.*;
026: import com.db4o.ext.*;
027: import com.db4o.foundation.*;
028: import com.db4o.internal.*;
029: import com.db4o.query.*;
030: import com.db4o.reflect.*;
031:
032: import db4ounit.*;
033: import db4ounit.extensions.concurrency.*;
034: import db4ounit.extensions.fixtures.*;
035:
036: public class AbstractDb4oTestCase implements Db4oTestCase {
037:
038: private transient Db4oFixture _fixture;
039:
040: private static final int DEFAULT_CONCURRENCY_THREAD_COUNT = 10;
041:
042: private transient int _threadCount = DEFAULT_CONCURRENCY_THREAD_COUNT;
043:
044: /* (non-Javadoc)
045: * @see db4ounit.extensions.Db4oTestCase#fixture(db4ounit.extensions.Db4oFixture)
046: */
047: public void fixture(Db4oFixture fixture) {
048: _fixture = fixture;
049: }
050:
051: /* (non-Javadoc)
052: * @see db4ounit.extensions.Db4oTestCase#fixture()
053: */
054: public Db4oFixture fixture() {
055: return _fixture;
056: }
057:
058: public boolean isClientServer() {
059: return fixture() instanceof Db4oClientServerFixture;
060: }
061:
062: protected boolean isEmbeddedClientServer() {
063: return isClientServer()
064: && ((Db4oClientServerFixture) fixture())
065: .embeddedClients();
066: }
067:
068: // TODO: The following code is only a temporary addition until MTOC
069: // is part of the core. When it is, all occurences of this
070: // method should be replaced with isEmbeddedClientServer()
071: protected boolean isMTOC() {
072: return fixture().db() instanceof EmbeddedClientObjectContainer;
073: }
074:
075: protected void reopen() throws Exception {
076: _fixture.reopen();
077: }
078:
079: public final void setUp() throws Exception {
080: _fixture.clean();
081: configure(_fixture.config());
082: _fixture.open();
083: db4oSetupBeforeStore();
084: store();
085: _fixture.db().commit();
086: _fixture.close();
087: _fixture.open();
088: db4oSetupAfterStore();
089: }
090:
091: public final void tearDown() throws Exception {
092: try {
093: db4oTearDownBeforeClean();
094: } finally {
095: _fixture.close();
096: _fixture.clean();
097: }
098: db4oTearDownAfterClean();
099: }
100:
101: protected void db4oSetupBeforeStore() throws Exception {
102: }
103:
104: protected void db4oSetupAfterStore() throws Exception {
105: }
106:
107: protected void db4oTearDownBeforeClean() throws Exception {
108: }
109:
110: protected void db4oTearDownAfterClean() throws Exception {
111: }
112:
113: protected void configure(Configuration config) throws Exception {
114: }
115:
116: protected void store() throws Exception {
117: }
118:
119: /* (non-Javadoc)
120: * @see db4ounit.extensions.Db4oTestCase#db()
121: */
122: public ExtObjectContainer db() {
123: return fixture().db();
124: }
125:
126: protected Class[] testCases() {
127: return new Class[] { getClass() };
128: }
129:
130: public int runAll() {
131: return runAll(true);
132: }
133:
134: private int runAll(final boolean independentConfig) {
135: return new TestRunner(
136: new TestSuite(new Test[] {
137: soloSuite(independentConfig).build(),
138: clientServerSuite(independentConfig).build(),
139: embeddedClientServerSuite(independentConfig)
140: .build(), })).run();
141: }
142:
143: public int runSoloAndClientServer() {
144: return runSoloAndClientServer(true);
145: }
146:
147: private int runSoloAndClientServer(final boolean independentConfig) {
148: return new TestRunner(new TestSuite(new Test[] {
149: soloSuite(independentConfig).build(),
150: clientServerSuite(independentConfig).build(), })).run();
151: }
152:
153: public int runSolo() {
154: return runSolo(true);
155: }
156:
157: public int runSolo(boolean independentConfig) {
158: return new TestRunner(soloSuite(independentConfig)).run();
159: }
160:
161: public int runClientServer() {
162: return runClientServer(true);
163: }
164:
165: public int runEmbeddedClientServer() {
166: return runEmbeddedClientServer(true);
167: }
168:
169: public int runConcurrency() {
170: return runConcurrency(true);
171: }
172:
173: public int runEmbeddedConcurrency() {
174: return runEmbeddedConcurrency(true);
175: }
176:
177: public int runConcurrencyAll() {
178: return runConcurrencyAll(true);
179: }
180:
181: private int runEmbeddedClientServer(boolean independentConfig) {
182: return new TestRunner(
183: embeddedClientServerSuite(independentConfig)).run();
184: }
185:
186: public int runClientServer(boolean independentConfig) {
187: return new TestRunner(clientServerSuite(independentConfig))
188: .run();
189: }
190:
191: private int runConcurrency(boolean independentConfig) {
192: return new TestRunner(concurrenyClientServerSuite(
193: independentConfig, false, "CONC")).run();
194: }
195:
196: private int runEmbeddedConcurrency(boolean independentConfig) {
197: return new TestRunner(concurrenyClientServerSuite(
198: independentConfig, true, "CONC EMBEDDED")).run();
199: }
200:
201: private int runConcurrencyAll(final boolean independentConfig) {
202: return new TestRunner(new TestSuite(new Test[] {
203: concurrenyClientServerSuite(independentConfig, false,
204: "CONC").build(),
205: concurrenyClientServerSuite(independentConfig, true,
206: "CONC EMBEDDED").build(), })).run();
207: }
208:
209: private Db4oTestSuiteBuilder soloSuite(boolean independentConfig) {
210: return new Db4oTestSuiteBuilder(new Db4oSolo(
211: configSource(independentConfig)), testCases());
212: }
213:
214: private Db4oTestSuiteBuilder clientServerSuite(
215: boolean independentConfig) {
216: return new Db4oTestSuiteBuilder(new Db4oClientServer(
217: configSource(independentConfig), false, "C/S"),
218: testCases());
219: }
220:
221: private Db4oTestSuiteBuilder embeddedClientServerSuite(
222: boolean independentConfig) {
223: return new Db4oTestSuiteBuilder(new Db4oClientServer(
224: configSource(independentConfig), true, "C/S EMBEDDED"),
225: testCases());
226: }
227:
228: private Db4oTestSuiteBuilder concurrenyClientServerSuite(
229: boolean independentConfig, boolean embedded, String label) {
230: return new Db4oConcurrencyTestSuiteBuilder(
231: new Db4oClientServer(configSource(independentConfig),
232: embedded, label), testCases());
233: }
234:
235: private ConfigurationSource configSource(boolean independentConfig) {
236: return (independentConfig ? (ConfigurationSource) new IndependentConfigurationSource()
237: : new GlobalConfigurationSource());
238: }
239:
240: protected ObjectContainerBase stream() {
241: return ((InternalObjectContainer) db()).container();
242: }
243:
244: public LocalObjectContainer fileSession() {
245: return fixture().fileSession();
246: }
247:
248: public Transaction trans() {
249: return ((InternalObjectContainer) db()).transaction();
250: }
251:
252: protected Transaction systemTrans() {
253: return trans().systemTransaction();
254: }
255:
256: protected Query newQuery(Transaction transaction, Class clazz) {
257: final Query query = newQuery(transaction);
258: query.constrain(clazz);
259: return query;
260: }
261:
262: protected Query newQuery(Transaction transaction) {
263: return stream().query(transaction);
264: }
265:
266: protected Query newQuery() {
267: return newQuery(db());
268: }
269:
270: protected static Query newQuery(ExtObjectContainer oc) {
271: return oc.query();
272: }
273:
274: protected Query newQuery(Class clazz) {
275: return newQuery(db(), clazz);
276: }
277:
278: protected static Query newQuery(ExtObjectContainer oc, Class clazz) {
279: final Query query = newQuery(oc);
280: query.constrain(clazz);
281: return query;
282: }
283:
284: protected Reflector reflector() {
285: return stream().reflector();
286: }
287:
288: protected void indexField(Configuration config, Class clazz,
289: String fieldName) {
290: config.objectClass(clazz).objectField(fieldName).indexed(true);
291: }
292:
293: protected Transaction newTransaction() {
294: return stream().newUserTransaction();
295: }
296:
297: public Object retrieveOnlyInstance(Class clazz) {
298: return retrieveOnlyInstance(db(), clazz);
299: }
300:
301: public static Object retrieveOnlyInstance(ExtObjectContainer oc,
302: Class clazz) {
303: ObjectSet result = newQuery(oc, clazz).execute();
304: Assert.areEqual(1, result.size());
305: return result.next();
306: }
307:
308: protected int countOccurences(Class clazz) {
309: ObjectSet result = newQuery(clazz).execute();
310: return result.size();
311: }
312:
313: protected int countOccurences(ExtObjectContainer oc, Class clazz) {
314: ObjectSet result = newQuery(oc, clazz).execute();
315: return result.size();
316: }
317:
318: protected void assertOccurrences(ExtObjectContainer oc,
319: Class clazz, int expected) {
320: Assert.areEqual(expected, countOccurences(oc, clazz));
321: }
322:
323: protected void foreach(Class clazz, Visitor4 visitor) {
324: ExtObjectContainer oc = db();
325: oc.deactivate(clazz, Integer.MAX_VALUE);
326: ObjectSet set = newQuery(clazz).execute();
327: while (set.hasNext()) {
328: visitor.visit(set.next());
329: }
330: }
331:
332: protected final void deleteAll(Class clazz) {
333: deleteAll(db(), clazz);
334: }
335:
336: protected final void deleteAll(final ExtObjectContainer oc,
337: Class clazz) {
338: foreach(clazz, new Visitor4() {
339: public void visit(Object obj) {
340: oc.delete(obj);
341: }
342: });
343: }
344:
345: protected final void deleteObjectSet(ObjectSet os) {
346: deleteObjectSet(db(), os);
347: }
348:
349: protected final void deleteObjectSet(ObjectContainer oc,
350: ObjectSet os) {
351: while (os.hasNext()) {
352: oc.delete(os.next());
353: }
354: }
355:
356: public final void store(Object obj) {
357: db().set(obj);
358: }
359:
360: protected ReflectClass reflectClass(Class clazz) {
361: return reflector().forClass(clazz);
362: }
363:
364: protected void defragment() throws Exception {
365: fixture().close();
366: fixture().defragment();
367: fixture().open();
368: }
369:
370: public final int threadCount() {
371: return _threadCount;
372: }
373:
374: public final void configureThreadCount(int count) {
375: _threadCount = count;
376: }
377:
378: protected EventRegistry eventRegistry() {
379: return EventRegistryFactory.forObjectContainer(db());
380: }
381:
382: protected EventRegistry serverEventRegistry() {
383: return EventRegistryFactory.forObjectContainer(fileSession());
384: }
385: }
|