KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > odmg > MultiDBUsageTest


1 package org.apache.ojb.odmg;
2
3 import org.apache.ojb.broker.FarAwayClass;
4 import org.apache.ojb.broker.PBKey;
5 import org.apache.ojb.broker.PersistenceBroker;
6 import org.apache.ojb.broker.PersistenceBrokerFactory;
7 import org.apache.ojb.broker.TestHelper;
8 import org.apache.ojb.broker.metadata.MetadataManager;
9 import org.apache.ojb.broker.metadata.JdbcConnectionDescriptor;
10 import org.apache.ojb.broker.metadata.ConnectionRepository;
11 import org.apache.ojb.broker.query.Criteria;
12 import org.apache.ojb.broker.query.Query;
13 import org.apache.ojb.broker.query.QueryByCriteria;
14 import org.apache.ojb.odmg.shared.Project;
15 import org.apache.ojb.odmg.shared.ODMGZoo;
16 import org.apache.ojb.junit.OJBTestCase;
17 import org.odmg.Database;
18 import org.odmg.Implementation;
19 import org.odmg.OQLQuery;
20 import org.odmg.Transaction;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26
27 /**
28  * Do some rollback tests and check behavior within transactions.
29  * CAUTION: This tests works only against the default repository.
30  */

31 public class MultiDBUsageTest extends OJBTestCase
32 {
33     private Implementation odmg_1;
34     private Database db_1;
35     private Implementation odmg_2;
36     private Database db_2;
37
38     public static void main(String JavaDoc[] args)
39     {
40         String JavaDoc[] arr = {MultiDBUsageTest.class.getName()};
41         junit.textui.TestRunner.main(arr);
42     }
43
44     public MultiDBUsageTest(String JavaDoc s)
45     {
46         super(s);
47     }
48
49     public void setUp() throws Exception JavaDoc
50     {
51         super.setUp();
52
53         MetadataManager mm = MetadataManager.getInstance();
54         JdbcConnectionDescriptor jcd = mm.connectionRepository().getDescriptor(TestHelper.FAR_AWAY_KEY);
55         if(jcd == null)
56         {
57             ConnectionRepository cr = mm.readConnectionRepository(TestHelper.FAR_AWAY_CONNECTION_REPOSITORY);
58             mm.connectionRepository().addDescriptor(cr.getDescriptor(TestHelper.FAR_AWAY_KEY));
59         }
60
61         odmg_1 = OJB.getInstance();
62         db_1 = odmg_1.newDatabase();
63         db_1.open(TestHelper.DEF_DATABASE_NAME, Database.OPEN_READ_WRITE);
64
65         odmg_2 = OJB.getInstance();
66         db_2 = odmg_2.newDatabase();
67         db_2.open(TestHelper.FAR_AWAY_DATABASE_NAME, Database.OPEN_READ_WRITE);
68     }
69
70     protected void tearDown() throws Exception JavaDoc
71     {
72         MetadataManager mm = MetadataManager.getInstance();
73         JdbcConnectionDescriptor jcd = mm.connectionRepository().getDescriptor(TestHelper.FAR_AWAY_KEY);
74         mm.connectionRepository().removeDescriptor(jcd);
75         try
76         {
77             if(odmg_1.currentTransaction() != null)
78             {
79                 odmg_1.currentTransaction().abort();
80             }
81             db_1.close();
82             odmg_1 = null;
83         }
84         catch (Exception JavaDoc e)
85         {
86             // ignore
87
}
88
89         try
90         {
91             if(odmg_2.currentTransaction() != null)
92             {
93                 odmg_2.currentTransaction().abort();
94             }
95             db_2.close();
96             odmg_2 = null;
97         }
98         catch (Exception JavaDoc e)
99         {
100             // ignore
101
}
102         super.tearDown();
103     }
104
105     /**
106      * Test store / delete objects to different db
107      */

108     public void testStore() throws Exception JavaDoc
109     {
110         String JavaDoc name = "testStoreDelete_" + System.currentTimeMillis();
111
112         // little hack for the test. use PB and ODMG api to verify results
113
int odmgZoosBefore = getDBObjectCountWithNewPB(((DatabaseImpl) db_1).getPBKey(), ODMGZoo.class);
114         int projectsBefore = getDBObjectCountWithNewPB(((DatabaseImpl) db_1).getPBKey(), Project.class);
115         int farAwaysBefore = getDBObjectCountWithNewPB(((DatabaseImpl) db_2).getPBKey(), FarAwayClass.class);
116
117         Transaction tx_1 = odmg_1.newTransaction();
118         tx_1.begin();
119         //store
120
storeObjects(tx_1, getNewODMGZoos(name, 5));
121         storeObjects(tx_1, getNewProjects(name, 3));
122         //store more
123
storeObjects(tx_1, getNewODMGZoos(name, 5));
124         storeObjects(tx_1, getNewProjects(name, 2));
125         tx_1.commit();
126
127         Transaction tx_2 = odmg_2.newTransaction();
128         tx_2.begin();
129         //store
130
storeObjects(tx_2, getNewFarAways(name, 9));
131         //store more
132
storeObjects(tx_2, getNewFarAways(name, 11));
133         tx_2.commit();
134
135         int odmgZoosAfter = getDBObjectCountWithNewPB(((DatabaseImpl) db_1).getPBKey(), ODMGZoo.class);
136         int projectsAfter = getDBObjectCountWithNewPB(((DatabaseImpl) db_1).getPBKey(), Project.class);
137         int farAwaysAfter = getDBObjectCountWithNewPB(((DatabaseImpl) db_2).getPBKey(), FarAwayClass.class);
138         int odmgZoosAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg_1, ODMGZoo.class);
139         int projectsAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg_1, Project.class);
140         int farAwaysAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg_2, FarAwayClass.class);
141
142         assertEquals("Wrong number of odmgZoos found", (odmgZoosBefore + 10), odmgZoosAfter);
143         assertEquals("Wrong number of projects found", (projectsBefore + 5), projectsAfter);
144         assertEquals("Wrong number of odmgZoos found", (odmgZoosBefore + 10), odmgZoosAfterOQL);
145         assertEquals("Wrong number of projects found", (projectsBefore + 5), projectsAfterOQL);
146         assertEquals("Wrong number of farAways found", (farAwaysBefore + 20), farAwaysAfter);
147         assertEquals("Wrong number of farAways found", (farAwaysBefore + 20), farAwaysAfterOQL);
148
149         //************
150
// we do twice
151
//************
152
// little hack for the test
153
odmgZoosBefore = getDBObjectCountWithNewPB(((DatabaseImpl) db_1).getPBKey(), ODMGZoo.class);
154         projectsBefore = getDBObjectCountWithNewPB(((DatabaseImpl) db_1).getPBKey(), Project.class);
155         farAwaysBefore = getDBObjectCountWithNewPB(((DatabaseImpl) db_2).getPBKey(), FarAwayClass.class);
156
157         tx_1.begin();
158         //store
159
storeObjects(tx_1, getNewODMGZoos(name, 5));
160         storeObjects(tx_1, getNewProjects(name, 3));
161         //store more
162
storeObjects(tx_1, getNewODMGZoos(name, 5));
163         storeObjects(tx_1, getNewProjects(name, 2));
164         tx_1.commit();
165
166         tx_2.begin();
167         //store
168
storeObjects(tx_2, getNewFarAways(name, 9));
169         //store more
170
storeObjects(tx_2, getNewFarAways(name, 11));
171         tx_2.commit();
172
173         odmgZoosAfter = getDBObjectCountWithNewPB(((DatabaseImpl) db_1).getPBKey(), ODMGZoo.class);
174         projectsAfter = getDBObjectCountWithNewPB(((DatabaseImpl) db_1).getPBKey(), Project.class);
175         farAwaysAfter = getDBObjectCountWithNewPB(((DatabaseImpl) db_2).getPBKey(), FarAwayClass.class);
176         odmgZoosAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg_1, ODMGZoo.class);
177         projectsAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg_1, Project.class);
178         farAwaysAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg_2, FarAwayClass.class);
179
180         assertEquals("Wrong number of odmgZoos found", (odmgZoosBefore + 10), odmgZoosAfter);
181         assertEquals("Wrong number of projects found", (projectsBefore + 5), projectsAfter);
182         assertEquals("Wrong number of odmgZoos found", (odmgZoosBefore + 10), odmgZoosAfterOQL);
183         assertEquals("Wrong number of projects found", (projectsBefore + 5), projectsAfterOQL);
184         assertEquals("Wrong number of farAways found", (farAwaysBefore + 20), farAwaysAfter);
185         assertEquals("Wrong number of farAways found", (farAwaysBefore + 20), farAwaysAfterOQL);
186
187         List JavaDoc result;
188         tx_1.begin();
189         OQLQuery query = odmg_1.newOQLQuery();
190         query.create("select projects from " + Project.class.getName() + " where title like $1");
191         query.bind(name);
192         result = (List JavaDoc) query.execute();
193         deleteObjects(db_1, result);
194         tx_1.commit();
195
196         tx_1.begin();
197         query = odmg_1.newOQLQuery();
198         query.create("select projects from " + ODMGZoo.class.getName() + " where name like $1");
199         query.bind(name);
200         result = (List JavaDoc) query.execute();
201         deleteObjects(db_1, result);
202         tx_1.commit();
203
204         tx_2.begin();
205         query = odmg_2.newOQLQuery();
206         query.create("select projects from " + FarAwayClass.class.getName() + " where name like $1");
207         query.bind(name);
208         result = (List JavaDoc) query.execute();
209         deleteObjects(db_2, result);
210         tx_2.commit();
211
212
213         tx_1.begin();
214         query = odmg_1.newOQLQuery();
215         query.create("select projects from " + Project.class.getName() + " where title like $1");
216         query.bind(name);
217         result = (List JavaDoc) query.execute();
218         tx_1.commit();
219         assertEquals(0, result.size());
220
221         tx_1.begin();
222         query = odmg_1.newOQLQuery();
223         query.create("select projects from " + ODMGZoo.class.getName() + " where name like $1");
224         query.bind(name);
225         result = (List JavaDoc) query.execute();
226         tx_1.commit();
227         assertEquals(0, result.size());
228
229         tx_2.begin();
230         query = odmg_2.newOQLQuery();
231         query.create("select projects from " + FarAwayClass.class.getName() + " where name like $1");
232         query.bind(name);
233         result = (List JavaDoc) query.execute();
234         tx_2.commit();
235         assertEquals(0, result.size());
236     }
237
238
239     private void storeObjects(Transaction tx, Collection JavaDoc objects)
240     {
241         for (Iterator JavaDoc iterator = objects.iterator(); iterator.hasNext();)
242         {
243             tx.lock(iterator.next(), Transaction.WRITE);
244         }
245     }
246
247     private void deleteObjects(Database db, Collection JavaDoc objects)
248     {
249         for (Iterator JavaDoc iterator = objects.iterator(); iterator.hasNext();)
250         {
251             db.deletePersistent(iterator.next());
252         }
253     }
254
255     private static int counter;
256
257     protected Collection JavaDoc getNewProjects(String JavaDoc name, int count)
258     {
259         ArrayList JavaDoc list = new ArrayList JavaDoc();
260         for (int i = 0; i < count; i++)
261         {
262             list.add(newProject(name));
263         }
264         return list;
265     }
266
267     protected Project newProject(String JavaDoc name)
268     {
269         Project p = new Project();
270         ++counter;
271         p.setDescription("Test project " + counter);
272         p.setTitle(name);
273         return p;
274     }
275
276     protected Collection JavaDoc getNewODMGZoos(String JavaDoc name, int count)
277     {
278         ArrayList JavaDoc list = new ArrayList JavaDoc();
279         for (int i = 0; i < count; i++)
280         {
281             list.add(newODMGZoo(name));
282         }
283         return list;
284     }
285
286     protected Collection JavaDoc getNewFarAways(String JavaDoc name, int count)
287     {
288         ArrayList JavaDoc list = new ArrayList JavaDoc();
289         for (int i = 0; i < count; i++)
290         {
291             list.add(newFarAway(name));
292         }
293         return list;
294     }
295
296     protected ODMGZoo newODMGZoo(String JavaDoc name)
297     {
298         ODMGZoo odmgZoo = new ODMGZoo();
299         ++counter;
300         odmgZoo.setName(name);
301         return odmgZoo;
302     }
303
304     private FarAwayClass newFarAway(String JavaDoc name)
305     {
306         FarAwayClass fa = new FarAwayClass();
307         counter++;
308         fa.setName(name);
309         fa.setDescription("so far away from " + counter);
310         return fa;
311     }
312
313     protected int getDBObjectCountWithNewPB(PBKey pbKey, Class JavaDoc target) throws Exception JavaDoc
314     {
315         PersistenceBroker broker = PersistenceBrokerFactory.createPersistenceBroker(pbKey);
316         Criteria c = new Criteria();
317         Query q = new QueryByCriteria(target, c);
318         int count = broker.getCount(q);
319         broker.close();
320         return count;
321     }
322
323     protected int getDBObjectCount(PersistenceBroker broker, Class JavaDoc target) throws Exception JavaDoc
324     {
325         Criteria c = new Criteria();
326         Query q = new QueryByCriteria(target, c);
327         int count = broker.getCount(q);
328         return count;
329     }
330
331     protected int getDBObjectCountViaOqlQueryUseNewTransaction(Implementation ojb, Class JavaDoc target) throws Exception JavaDoc
332     {
333         Transaction tx = ojb.newTransaction();
334         tx.begin();
335         OQLQuery query = ojb.newOQLQuery();
336         query.create("select allProjects from " + target.getName());
337         List JavaDoc list = (List JavaDoc) query.execute();
338         tx.commit();
339         return list.size();
340     }
341
342     protected int getDBObjectCountViaOqlQuery(Implementation ojb, Class JavaDoc target) throws Exception JavaDoc
343     {
344         OQLQuery query = ojb.newOQLQuery();
345         query.create("select allObjects from " + target.getName());
346         List JavaDoc list = (List JavaDoc) query.execute();
347         return list.size();
348     }
349
350     protected List JavaDoc getAllObjects(Implementation ojb, Class JavaDoc target) throws Exception JavaDoc
351     {
352         OQLQuery query = ojb.newOQLQuery();
353         query.create("select allObjects from " + target.getName());
354         List JavaDoc list = (List JavaDoc) query.execute();
355         return list;
356     }
357 }
358
Popular Tags