KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > db4ounit > extensions > AbstractDb4oTestCase


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

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     /* (non-Javadoc)
38      * @see db4ounit.extensions.Db4oTestCase#fixture(db4ounit.extensions.Db4oFixture)
39      */

40     public void fixture(Db4oFixture fixture) {
41         _fixture = fixture;
42     }
43
44     /* (non-Javadoc)
45      * @see db4ounit.extensions.Db4oTestCase#fixture()
46      */

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 JavaDoc{
56         _fixture.reopen();
57     }
58     
59     public final void setUp() throws Exception JavaDoc {
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 JavaDoc {
72         db4oCustomTearDown();
73         _fixture.close();
74         _fixture.clean();
75     }
76     
77     protected void db4oSetupBeforeStore() throws Exception JavaDoc {}
78     protected void db4oSetupAfterStore() throws Exception JavaDoc {}
79     protected void db4oCustomTearDown() throws Exception JavaDoc {}
80
81     protected void configure(Configuration config) {}
82     
83     protected void store() throws Exception JavaDoc {}
84
85     /* (non-Javadoc)
86      * @see db4ounit.extensions.Db4oTestCase#db()
87      */

88     public ExtObjectContainer db() {
89         return fixture().db();
90     }
91     
92     protected Class JavaDoc[] testCases() {
93         return new Class JavaDoc[] { 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 JavaDoc 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 JavaDoc clazz, String JavaDoc 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 JavaDoc clazz) {
183         final Query query = newQuery();
184         query.constrain(clazz);
185         return query;
186     }
187     
188     protected Object JavaDoc retrieveOnlyInstance(Class JavaDoc clazz) {
189         ObjectSet result=newQuery(clazz).execute();
190         Assert.areEqual(1,result.size());
191         return result.next();
192     }
193     
194     protected int countOccurences(Class JavaDoc clazz) {
195         ObjectSet result = newQuery(clazz).execute();
196         return result.size();
197     }
198     
199     protected void foreach(Class JavaDoc 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 JavaDoc clazz) {
209         foreach(clazz, new Visitor4() {
210             public void visit(Object JavaDoc obj) {
211                 db().delete(obj);
212             }
213         });
214     }
215     
216     protected final void store(Object JavaDoc obj) {
217         db().set(obj);
218     }
219
220     protected ReflectClass reflectClass(Class JavaDoc clazz) {
221         return reflector().forClass(clazz);
222     }
223 }
224
Popular Tags