KickJava   Java API By Example, From Geeks To Geeks.

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


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

28 public class ODMGRollbackTest extends ODMGTestCase
29 {
30     public static void main(String JavaDoc[] args)
31     {
32         String JavaDoc[] arr = {ODMGRollbackTest.class.getName()};
33         junit.textui.TestRunner.main(arr);
34     }
35
36     public ODMGRollbackTest(String JavaDoc s)
37     {
38         super(s);
39     }
40
41     public void testDatabaseClose() throws Exception JavaDoc
42     {
43         TransactionExt tx = (TransactionExt) odmg.newTransaction();
44         try
45         {
46             tx.begin();
47             database.close();
48             fail("We should not able to close database instance while running tx");
49         }
50         catch (Exception JavaDoc e)
51         {
52         }
53         finally
54         {
55             tx.abort();
56         }
57     }
58
59     public void testDeleteAll() throws Exception JavaDoc
60     {
61         deleteAll(odmg, ODMGZoo.class);
62         deleteAll(odmg, ODMGGourmet.class);
63     }
64
65     public void testTransactionFlush() throws Exception JavaDoc
66     {
67         String JavaDoc name = "testTransactionFlush_" + System.currentTimeMillis();
68         TransactionExt tx = (TransactionExt) odmg.newTransaction();
69
70         tx.begin();
71         PersistenceBroker broker = tx.getBroker();
72         ODMGZoo obj = new ODMGZoo();
73         tx.lock(obj, Transaction.WRITE);
74         obj.setName(name);
75
76         tx.flush();
77
78         Criteria crit = new Criteria();
79         crit.addEqualTo("name", obj.getName());
80         QueryByCriteria query = QueryFactory.newQuery(ODMGZoo.class, crit);
81         // we flushed all objects, thus we should find object in DB/cache
82
Iterator JavaDoc it = broker.getIteratorByQuery(query);
83         assertTrue(it.hasNext());
84         ODMGZoo other = (ODMGZoo) it.next();
85         assertNotNull(other);
86         assertEquals(obj.getZooId(), other.getZooId());
87         assertEquals(obj.getName(), other.getName());
88         assertFalse(it.hasNext());
89         // now we abort tx, so all flushed objects shouldn't be found
90
// in further queries
91
tx.abort();
92
93         tx = (TransactionExt) odmg.newTransaction();
94         tx.begin();
95         broker = tx.getBroker();
96         QueryByIdentity query2 = new QueryByIdentity(obj);
97         Object JavaDoc result = broker.getObjectByQuery(query2);
98         tx.commit();
99
100         assertNull("We should not find objects from aborted tx", result);
101     }
102
103     public void testTransactionFlush_2() throws Exception JavaDoc
104     {
105         String JavaDoc name = "testTransactionFlush_2_" + System.currentTimeMillis();
106         TransactionExt tx = (TransactionExt) odmg.newTransaction();
107
108         tx.begin();
109         PersistenceBroker broker = tx.getBroker();
110
111         ODMGZoo obj = new ODMGZoo();
112         obj.setName(name);
113         tx.lock(obj, Transaction.WRITE);
114         tx.flush();
115         // System.err.println("First flush call, insert new object");
116

117         // PB to query
118
Criteria crit = new Criteria();
119         crit.addEqualTo("name", obj.getName());
120         QueryByCriteria query = QueryFactory.newQuery(ODMGZoo.class, crit);
121         // we flushed all objects, thus we should found object in DB/cache
122
Iterator JavaDoc it = broker.getIteratorByQuery(query);
123         assertTrue(it.hasNext());
124         ODMGZoo other = (ODMGZoo) it.next();
125         assertNotNull(other);
126         assertEquals(obj.getZooId(), other.getZooId());
127         assertEquals(obj.getName(), other.getName());
128         assertFalse(it.hasNext());
129
130         /*** Charles : Start ***/
131         // Let's flush, change the name and flush again
132
tx.flush();
133         // System.err.println("Second flush call, nothing to do");
134
obj.setName("updated_" + name);
135         tx.flush();
136         // System.err.println("Third flush call, update");
137
OQLQuery q = odmg.newOQLQuery();
138         q.create("select zoos from " + ODMGZoo.class.getName() + " where name like $1");
139         q.bind("updated_" + name);
140
141         //Redo the query - we should find the object again
142
it = ((Collection JavaDoc) q.execute()).iterator();
143         assertTrue(it.hasNext());
144         other = (ODMGZoo) it.next();
145         assertNotNull(other);
146         assertEquals(obj.getZooId(), other.getZooId());
147         assertEquals(obj.getName(), other.getName());
148         assertFalse(it.hasNext());
149         /*** Charles : End ***/
150
151         // now we abort tx, so all flushed objects shouldn't be found
152
// in further queries
153
tx.abort();
154
155         tx = (TransactionExt) odmg.newTransaction();
156         tx.begin();
157         broker = tx.getBroker();
158         QueryByIdentity query2 = new QueryByIdentity(obj);
159         Object JavaDoc result = broker.getObjectByQuery(query2);
160         tx.commit();
161
162         assertNull("We should not find objects from aborted tx", result);
163     }
164
165     /**
166      * Tests behavior within transactions. If i store 5 odmgZoos within a transaction
167      * and after that within the same transaction i do query 'select all odmgZoos'
168      * the number of odmgZoos returned should be oldNumber+5 when using checkpoint.
169      * thma:
170      * this testcase seems to fail for some strange problems with the testbed data
171      * the thrown error is unrelated to the things covered in the testcase.
172      * arminw: should be fixed
173      */

174     public void testResultsWhileTransactionWithCheckpoint() throws Exception JavaDoc
175     {
176         // if(ojbSkipKnownIssueProblem()) return;
177

178         int odmgZoosBefore = getDBObjectCountWithNewPB(ODMGZoo.class);
179         int projectsBefore = getDBObjectCountWithNewPB(ODMGGourmet.class);
180
181         TransactionExt tx = (TransactionExt) odmg.newTransaction();
182         tx.begin();
183         List JavaDoc zoos = new ArrayList JavaDoc(getNewODMGZoos("testResultsWhileTransactionWithCheckpoint", 5));
184         List JavaDoc projects = new ArrayList JavaDoc(getNewProjects("testResultsWhileTransactionWithCheckpoint", 3));
185         //store some objects
186
storeObjects(tx, zoos);
187         storeObjects(tx, projects);
188         // checkpoint, should bring objects to DB but shouldn't commit
189
tx.checkpoint();
190
191         //Do a queries within a transaction
192
int odmgZoosWhile = getDBObjectCountViaOqlQuery(odmg, ODMGZoo.class);
193         int projectsWhile = getDBObjectCountViaOqlQuery(odmg, ODMGGourmet.class);
194         int odmgZoosWhilePB = 0;
195         int projectsWhilePB = 0;
196
197         odmgZoosWhilePB = getDBObjectCount(tx.getBroker(), ODMGZoo.class);
198         projectsWhilePB = getDBObjectCount(tx.getBroker(), ODMGGourmet.class);
199
200         //store more
201
List JavaDoc zoos2 = new ArrayList JavaDoc(getNewODMGZoos("testResultsWhileTransactionWithCheckpoint", 5));
202         List JavaDoc projects2 = new ArrayList JavaDoc(getNewProjects("testResultsWhileTransactionWithCheckpoint", 2));
203         storeObjects(tx, zoos2);
204         storeObjects(tx, projects2);
205
206         zoos.addAll(zoos2);
207         projects.addAll(projects2);
208         // checkpoint, should bring objects to DB but shouldn't commit
209
tx.checkpoint();
210
211         // after checkpoint another tx should NOT be able to lock these objects
212
TransactionImpl tx2 = (TransactionImpl) odmg.newTransaction();
213         tx2.begin();
214         try
215         {
216             Iterator JavaDoc it = zoos.iterator();
217             while (it.hasNext())
218             {
219                 Object JavaDoc o = it.next();
220                 tx2.lock(o, Transaction.WRITE);
221             }
222
223             it = projects.iterator();
224             while (it.hasNext())
225             {
226                 Object JavaDoc o = it.next();
227                 tx2.lock(o, Transaction.WRITE);
228             }
229             fail("After checkpoint all locks should be still exist for objects");
230         }
231         catch(LockNotGrantedException e)
232         {
233             // expected
234
assertTrue(true);
235         }
236         finally
237         {
238             tx2.abort();
239         }
240         // reassign tx
241
tx.join();
242
243         //more queries
244
int odmgZoosWhile2 = getDBObjectCountViaOqlQuery(odmg, ODMGZoo.class);
245         int projectsWhile2 = getDBObjectCountViaOqlQuery(odmg, ODMGGourmet.class);
246         int odmgZoosWhilePB2 = 0;
247         int projectsWhilePB2 = 0;
248
249         odmgZoosWhilePB2 = getDBObjectCount(tx.getBroker(), ODMGZoo.class);
250         projectsWhilePB2 = getDBObjectCount(tx.getBroker(), ODMGGourmet.class);
251
252         tx.commit();
253         int odmgZoosAfter = getDBObjectCountWithNewPB(ODMGZoo.class);
254         int projectsAfter = getDBObjectCountWithNewPB(ODMGGourmet.class);
255         int odmgZoosAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg, ODMGZoo.class);
256         int projectsAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg, ODMGGourmet.class);
257
258         assertEquals("Wrong number of odmgZoos found after commit", (odmgZoosBefore + 10), odmgZoosAfter);
259         assertEquals("Wrong number of projects found after commit", (projectsBefore + 5), projectsAfter);
260         assertEquals("Wrong number of odmgZoos found after commit", (odmgZoosBefore + 10), odmgZoosAfterOQL);
261         assertEquals("Wrong number of projects found after commit", (projectsBefore + 5), projectsAfterOQL);
262
263         /*
264         Here we test if we can see our changes while the transaction runs. IMO it must be
265         possible to see all changes made in a transaction.
266         */

267
268         assertEquals("Wrong number of odmgZoos found while transaction", (odmgZoosBefore + 5), odmgZoosWhilePB);
269         assertEquals("Wrong number of projects found while transaction", (projectsBefore + 3), projectsWhilePB);
270         assertEquals("Wrong number of odmgZoos found while transaction", (odmgZoosBefore + 10), odmgZoosWhilePB2);
271         assertEquals("Wrong number of projects found while transaction", (projectsBefore + 5), projectsWhilePB2);
272         assertEquals("Wrong number of odmgZoos found while transaction", (odmgZoosBefore + 5), odmgZoosWhile);
273         assertEquals("Wrong number of projects found while transaction", (projectsBefore + 3), projectsWhile);
274         assertEquals("Wrong number of odmgZoos found while transaction", (odmgZoosBefore + 10), odmgZoosWhile2);
275         assertEquals("Wrong number of projects found while transaction", (projectsBefore + 5), projectsWhile2);
276
277         // after tx another tx should be able to lock these objects
278
tx2 = (TransactionImpl) odmg.newTransaction();
279         tx2.begin();
280         try
281         {
282             Iterator JavaDoc it = zoos.iterator();
283             while (it.hasNext())
284             {
285                 Object JavaDoc o = it.next();
286                 tx2.lock(o, Transaction.WRITE);
287             }
288
289             it = projects.iterator();
290             while (it.hasNext())
291             {
292                 Object JavaDoc o = it.next();
293                 tx2.lock(o, Transaction.WRITE);
294             }
295         }
296         finally
297         {
298             tx2.abort();
299         }
300     }
301
302     /**
303      * Tests object count after a commited transaction
304      * thma:
305      * this testcase seems to fail for some strange problems with the testbed data
306      * the thrown error is unrelated to the things covered in the testcase.
307      * arminw: should be fixed
308      */

309     public void testResultsAfterTransaction() throws Exception JavaDoc
310     {
311         // if(ojbSkipKnownIssueProblem()) return;
312

313         int odmgZoosBefore = getDBObjectCountWithNewPB(ODMGZoo.class);
314         int projectsBefore = getDBObjectCountWithNewPB(ODMGGourmet.class);
315
316         Transaction tx = odmg.newTransaction();
317         tx.begin();
318         //store
319
persistentStoreObjects(database, getNewODMGZoos("testResultsAfterTransaction", 5));
320         persistentStoreObjects(database, getNewProjects("testResultsAfterTransaction", 3));
321         //store more
322
storeObjects(tx, getNewODMGZoos("testResultsAfterTransaction", 5));
323         storeObjects(tx, getNewProjects("testResultsAfterTransaction", 2));
324         tx.commit();
325
326         int odmgZoosAfter = getDBObjectCountWithNewPB(ODMGZoo.class);
327         int projectsAfter = getDBObjectCountWithNewPB(ODMGGourmet.class);
328         int odmgZoosAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg, ODMGZoo.class);
329         int projectsAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg, ODMGGourmet.class);
330
331         assertEquals("Wrong number of odmgZoos found", (odmgZoosBefore + 10), odmgZoosAfter);
332         assertEquals("Wrong number of projects found", (projectsBefore + 5), projectsAfter);
333         assertEquals("Wrong number of odmgZoos found", (odmgZoosBefore + 10), odmgZoosAfterOQL);
334         assertEquals("Wrong number of projects found", (projectsBefore + 5), projectsAfterOQL);
335
336
337         //we do twice
338
odmgZoosBefore = getDBObjectCountWithNewPB(ODMGZoo.class);
339         projectsBefore = getDBObjectCountWithNewPB(ODMGGourmet.class);
340
341         //tx should be reusable
342
tx.begin();
343         //store
344
persistentStoreObjects(database, getNewODMGZoos("testResultsAfterTransaction", 5));
345         persistentStoreObjects(database, getNewProjects("testResultsAfterTransaction", 3));
346         //store more
347
storeObjects(tx, getNewODMGZoos("testResultsAfterTransaction", 5));
348         storeObjects(tx, getNewProjects("testResultsAfterTransaction", 2));
349         tx.commit();
350
351         odmgZoosAfter = getDBObjectCountWithNewPB(ODMGZoo.class);
352         projectsAfter = getDBObjectCountWithNewPB(ODMGGourmet.class);
353         odmgZoosAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg, ODMGZoo.class);
354         projectsAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg, ODMGGourmet.class);
355
356         assertEquals("Wrong number of odmgZoos found", (odmgZoosBefore + 10), odmgZoosAfter);
357         assertEquals("Wrong number of projects found", (projectsBefore + 5), projectsAfter);
358         assertEquals("Wrong number of odmgZoos found", (odmgZoosBefore + 10), odmgZoosAfterOQL);
359         assertEquals("Wrong number of projects found", (projectsBefore + 5), projectsAfterOQL);
360
361     }
362
363     /**
364      * Tests object count after a commited transaction
365      */

366     public void testResultsAfterTransactionWithClearedCache() throws Exception JavaDoc
367     {
368         int odmgZoosBefore = getDBObjectCountWithNewPB(ODMGZoo.class);
369         int projectsBefore = getDBObjectCountWithNewPB(ODMGGourmet.class);
370
371         Transaction tx = odmg.newTransaction();
372         tx.begin();
373         //store
374
persistentStoreObjects(database, getNewODMGZoos("testResultsAfterTransactionWithClearedCache", 5));
375         persistentStoreObjects(database, getNewProjects("testResultsAfterTransactionWithClearedCache", 3));
376         //store more
377
storeObjects(tx, getNewODMGZoos("testResultsAfterTransactionWithClearedCache", 5));
378         storeObjects(tx, getNewProjects("testResultsAfterTransactionWithClearedCache", 2));
379         tx.commit();
380
381         //###### hack we clear cache of PB ########
382
PersistenceBroker tmp = PersistenceBrokerFactory.defaultPersistenceBroker();
383         tmp.clearCache();
384         tmp.close();
385
386         int odmgZoosAfter = getDBObjectCountWithNewPB(ODMGZoo.class);
387         int projectsAfter = getDBObjectCountWithNewPB(ODMGGourmet.class);
388         int odmgZoosAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg, ODMGZoo.class);
389         int projectsAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg, ODMGGourmet.class);
390
391         assertEquals("Wrong number of odmgZoos found", (odmgZoosBefore + 10), odmgZoosAfter);
392         assertEquals("Wrong number of projects found", (projectsBefore + 5), projectsAfter);
393         assertEquals("Wrong number of odmgZoos found", (odmgZoosBefore + 10), odmgZoosAfterOQL);
394         assertEquals("Wrong number of projects found", (projectsBefore + 5), projectsAfterOQL);
395
396
397         //we do twice
398
odmgZoosBefore = getDBObjectCountWithNewPB(ODMGZoo.class);
399         projectsBefore = getDBObjectCountWithNewPB(ODMGGourmet.class);
400
401         //tx should be reusable
402
tx.begin();
403         //store
404
persistentStoreObjects(database, getNewODMGZoos("testResultsAfterTransactionWithClearedCache", 5));
405         persistentStoreObjects(database, getNewProjects("testResultsAfterTransactionWithClearedCache", 3));
406         //store more
407
storeObjects(tx, getNewODMGZoos("testResultsAfterTransactionWithClearedCache", 5));
408         storeObjects(tx, getNewProjects("testResultsAfterTransactionWithClearedCache", 2));
409         tx.commit();
410
411         //###### hack we clear cache of PB ########
412
tmp = PersistenceBrokerFactory.defaultPersistenceBroker();
413         tmp.clearCache();
414         tmp.close();
415
416         odmgZoosAfter = getDBObjectCountWithNewPB(ODMGZoo.class);
417         projectsAfter = getDBObjectCountWithNewPB(ODMGGourmet.class);
418         odmgZoosAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg, ODMGZoo.class);
419         projectsAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg, ODMGGourmet.class);
420
421         assertEquals("Wrong number of odmgZoos found", (odmgZoosBefore + 10), odmgZoosAfter);
422         assertEquals("Wrong number of projects found", (projectsBefore + 5), projectsAfter);
423         assertEquals("Wrong number of odmgZoos found", (odmgZoosBefore + 10), odmgZoosAfterOQL);
424         assertEquals("Wrong number of projects found", (projectsBefore + 5), projectsAfterOQL);
425     }
426
427     /**
428      * Test the rollback behaviour. If i store 10 odmgZoos within a transaction and after
429      * that store the transaction was aborted, the number of odmgZoos in the DB should be unchanged.
430      */

431     public void testUserRollback() throws Exception JavaDoc
432     {
433         int odmgZoosBefore = getDBObjectCountWithNewPB(ODMGZoo.class);
434         int projectsBefore = getDBObjectCountWithNewPB(ODMGGourmet.class);
435
436         Transaction tx = odmg.newTransaction();
437         tx.begin();
438         storeObjects(tx, getNewODMGZoos("testUserRollback", 10));
439         storeObjects(tx, getNewProjects("testUserRollback", 10));
440         //we abort tx
441
tx.abort();
442
443         int odmgZoosAfter = getDBObjectCountWithNewPB(ODMGZoo.class);
444         int projectsAfter = getDBObjectCountWithNewPB(ODMGGourmet.class);
445         int odmgZoosAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg, ODMGZoo.class);
446         int projectsAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg, ODMGGourmet.class);
447
448         assertEquals("Wrong number of odmgZoos found", odmgZoosBefore, odmgZoosAfter);
449         assertEquals("Wrong number of projects found", projectsBefore, projectsAfter);
450         assertEquals("Wrong number of odmgZoos found", (odmgZoosBefore), odmgZoosAfterOQL);
451         assertEquals("Wrong number of projects found", (projectsBefore), projectsAfterOQL);
452
453         //We do this twice
454
odmgZoosBefore = getDBObjectCountWithNewPB(ODMGZoo.class);
455         projectsBefore = getDBObjectCountWithNewPB(ODMGGourmet.class);
456
457         tx.begin();
458         storeObjects(tx, getNewODMGZoos("testUserRollback", 10));
459         storeObjects(tx, getNewProjects("testUserRollback", 10));
460         //we abort tx
461
tx.abort();
462
463         odmgZoosAfter = getDBObjectCountWithNewPB(ODMGZoo.class);
464         projectsAfter = getDBObjectCountWithNewPB(ODMGGourmet.class);
465         odmgZoosAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg, ODMGZoo.class);
466         projectsAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg, ODMGGourmet.class);
467
468         assertEquals("Wrong number of odmgZoos found", odmgZoosBefore, odmgZoosAfter);
469         assertEquals("Wrong number of projects found", projectsBefore, projectsAfter);
470         assertEquals("Wrong number of odmgZoos found", (odmgZoosBefore), odmgZoosAfterOQL);
471         assertEquals("Wrong number of projects found", (projectsBefore), projectsAfterOQL);
472     }
473
474     /**
475      * Test the rollback behaviour. If i store 10 odmgZoos within a transaction and do a checkpoint call.
476      * After that the transaction was aborted, the number of odmgZoos in the DB should be unchanged.
477      */

478     public void testUserRollbackWithCheckpoint() throws Exception JavaDoc
479     {
480         int odmgZoosBefore = getDBObjectCountWithNewPB(ODMGZoo.class);
481         int projectsBefore = getDBObjectCountWithNewPB(ODMGGourmet.class);
482
483         Transaction tx = odmg.newTransaction();
484         tx.begin();
485         storeObjects(tx, getNewODMGZoos("testUserRollbackWithCheckpoint", 10));
486         // now we store objects to DB
487
tx.checkpoint();
488         storeObjects(tx, getNewProjects("testUserRollbackWithCheckpoint", 10));
489         //we abort tx, all actions after the last checkpoint call should be rollback
490
tx.abort();
491
492         int odmgZoosAfter = getDBObjectCountWithNewPB(ODMGZoo.class);
493         int projectsAfter = getDBObjectCountWithNewPB(ODMGGourmet.class);
494         int odmgZoosAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg, ODMGZoo.class);
495         int projectsAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg, ODMGGourmet.class);
496
497         assertEquals("Wrong number of odmgZoos found", odmgZoosBefore + 10, odmgZoosAfter);
498         assertEquals("Wrong number of projects found", projectsBefore, projectsAfter);
499         assertEquals("Wrong number of odmgZoos found", odmgZoosBefore + 10, odmgZoosAfterOQL);
500         assertEquals("Wrong number of projects found", projectsBefore, projectsAfterOQL);
501
502         //***********************
503
// do the procedure again
504
odmgZoosBefore = getDBObjectCountWithNewPB(ODMGZoo.class);
505         projectsBefore = getDBObjectCountWithNewPB(ODMGGourmet.class);
506         // we reuse current tx
507
tx.begin();
508         storeObjects(tx, getNewODMGZoos("testUserRollbackWithCheckpoint", 10));
509         // now we store objects to DB
510
tx.checkpoint();
511         storeObjects(tx, getNewProjects("testUserRollbackWithCheckpoint", 10));
512         //we abort tx, all actions after the last checkpoint call should be rollback
513
tx.abort();
514
515         odmgZoosAfter = getDBObjectCountWithNewPB(ODMGZoo.class);
516         projectsAfter = getDBObjectCountWithNewPB(ODMGGourmet.class);
517         odmgZoosAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg, ODMGZoo.class);
518         projectsAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg, ODMGGourmet.class);
519
520         assertEquals("Wrong number of odmgZoos found", odmgZoosBefore + 10, odmgZoosAfter);
521         assertEquals("Wrong number of projects found", projectsBefore, projectsAfter);
522         assertEquals("Wrong number of odmgZoos found", odmgZoosBefore + 10, odmgZoosAfterOQL);
523         assertEquals("Wrong number of projects found", projectsBefore, projectsAfterOQL);
524     }
525
526
527
528
529     private void storeObjects(Transaction tx, Collection JavaDoc objects)
530     {
531         for (Iterator JavaDoc iterator = objects.iterator(); iterator.hasNext();)
532         {
533             tx.lock(iterator.next(), Transaction.WRITE);
534         }
535     }
536
537     private void persistentStoreObjects(Database database, Collection JavaDoc objects)
538     {
539         for (Iterator JavaDoc iterator = objects.iterator(); iterator.hasNext();)
540         {
541             database.makePersistent(iterator.next());
542         }
543     }
544
545     private static int counter;
546
547     protected Collection JavaDoc getNewProjects(String JavaDoc name, int count)
548     {
549         ArrayList JavaDoc list = new ArrayList JavaDoc();
550         for (int i = 0; i < count; i++)
551         {
552             list.add(newProject(name));
553         }
554         return list;
555     }
556
557     protected ODMGGourmet newProject(String JavaDoc name)
558     {
559         ODMGGourmet p = new ODMGGourmet();
560         ++counter;
561         if(name == null)
562         {
563             p.setName("Test " + counter);
564         }
565         else
566         {
567             p.setName(name + "_" + counter);
568         }
569         return p;
570     }
571
572     protected Collection JavaDoc getNewODMGZoos(String JavaDoc name, int count)
573     {
574         ArrayList JavaDoc list = new ArrayList JavaDoc();
575         for (int i = 0; i < count; i++)
576         {
577             list.add(newODMGZoo(name));
578         }
579         return list;
580     }
581
582     protected ODMGZoo newODMGZoo(String JavaDoc name)
583     {
584         ODMGZoo odmgZoo = new ODMGZoo();
585         ++counter;
586         if(name == null)
587         {
588             odmgZoo.setName("animal_" + counter);
589         }
590         else
591         {
592             odmgZoo.setName(name + "_" + counter);
593         }
594         return odmgZoo;
595     }
596
597     protected int getDBObjectCountWithNewPB(Class JavaDoc target) throws Exception JavaDoc
598     {
599         PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker();
600         Criteria c = new Criteria();
601         Query q = new QueryByCriteria(target, c);
602         int count = broker.getCount(q);
603         broker.close();
604         return count;
605     }
606
607     protected int getDBObjectCount(PersistenceBroker broker, Class JavaDoc target) throws Exception JavaDoc
608     {
609         Criteria c = new Criteria();
610         Query q = new QueryByCriteria(target, c);
611         int count = broker.getCount(q);
612         return count;
613     }
614
615     protected int getDBObjectCountViaOqlQueryUseNewTransaction(Implementation odmg, Class JavaDoc target) throws Exception JavaDoc
616     {
617         Transaction tx = odmg.newTransaction();
618         tx.begin();
619         OQLQuery query = odmg.newOQLQuery();
620         query.create("select allODMGGourmets from " + target.getName());
621         List JavaDoc list = (List JavaDoc) query.execute();
622         tx.commit();
623         return list.size();
624     }
625
626     protected int getDBObjectCountViaOqlQuery(Implementation odmg, Class JavaDoc target) throws Exception JavaDoc
627     {
628         OQLQuery query = odmg.newOQLQuery();
629         query.create("select allObjects from " + target.getName());
630         List JavaDoc list = (List JavaDoc) query.execute();
631         return list.size();
632     }
633
634     protected void deleteAll(Implementation odmg, Class JavaDoc target) throws Exception JavaDoc
635     {
636         TransactionExt tx = (TransactionExt) odmg.newTransaction();
637         tx.begin();
638         tx.setCascadingDelete(target, true);
639         OQLQuery query = odmg.newOQLQuery();
640         query.create("select allObjects from " + target.getName());
641         List JavaDoc list = (List JavaDoc) query.execute();
642         for (int i = 0; i < list.size(); i++)
643         {
644             Object JavaDoc obj = list.get(i);
645             database.deletePersistent(obj);
646         }
647         tx.commit();
648     }
649
650     // user described test case
651
public void testDuplicateLocking() throws Exception JavaDoc
652     {
653         RollbackObjectOne ro = null;
654
655         Transaction tx = odmg.newTransaction();
656         tx.begin();
657         ro = new RollbackObjectOne();
658         ro.setName("test_step_1");
659         tx.lock(ro, Transaction.WRITE);
660         tx.lock(ro, Transaction.WRITE);
661         tx.commit();
662
663         tx.begin();
664         tx.lock(ro, Transaction.WRITE);
665         tx.lock(ro, Transaction.WRITE);
666         ro.setName("test_step_2");
667         tx.commit();
668
669         tx.begin();
670         tx.lock(ro, Transaction.WRITE);
671         ro.setName("test_step_3");
672         tx.lock(ro, Transaction.WRITE);
673         tx.commit();
674     }
675
676     public void testCheckCacheAfterRollback() throws Exception JavaDoc
677     {
678         RollbackObjectOne ro = null;
679         RollbackObjectOne ro_2 = null;
680         String JavaDoc name = "testCheckCacheAfterRollback_"+System.currentTimeMillis();
681
682         Transaction tx = odmg.newTransaction();
683         tx.begin();
684         ro = new RollbackObjectOne();
685         ro.setName(name);
686         tx.lock(ro, Transaction.WRITE);
687
688         ro_2 = new RollbackObjectOne();
689         ro_2.setName(name);
690         tx.lock(ro_2, Transaction.WRITE);
691         tx.commit();
692
693         tx = odmg.newTransaction();
694         tx.begin();
695         OQLQuery query = odmg.newOQLQuery();
696         query.create("select all from " + RollbackObjectOne.class.getName() + " where name like $1");
697         query.bind(name);
698         List JavaDoc list = (List JavaDoc) query.execute();
699         tx.commit();
700         assertEquals(2,list.size());
701
702         tx = odmg.newTransaction();
703         tx.begin();
704         tx.lock(ro, Transaction.WRITE);
705         ro = new RollbackObjectOne();
706         ro.setDescription(name);
707
708         tx.lock(ro_2, Transaction.WRITE);
709         ro_2 = new RollbackObjectOne();
710         ro_2.setDescription(name);
711
712         tx.abort();
713
714         tx = odmg.newTransaction();
715         tx.begin();
716         query = odmg.newOQLQuery();
717         query.create("select all from " + RollbackObjectOne.class.getName() + " where name like $1");
718         query.bind(name);
719         list = (List JavaDoc) query.execute();
720         tx.commit();
721         assertEquals(2,list.size());
722         assertNull(((RollbackObjectOne)list.get(0)).getDescription());
723         assertNull(((RollbackObjectOne)list.get(1)).getDescription());
724
725         // after tx another tx should be able to lock these objects
726
TransactionImpl tx2 = (TransactionImpl) odmg.newTransaction();
727         tx2.begin();
728         try
729         {
730             Iterator JavaDoc it = list.iterator();
731             while (it.hasNext())
732             {
733                 Object JavaDoc o = it.next();
734                 tx2.lock(o, Transaction.WRITE);
735             }
736         }
737         finally
738         {
739             tx2.abort();
740         }
741     }
742
743     /**
744      * test empty usage of methods
745      */

746     public void testEmpty() throws Exception JavaDoc
747     {
748         // get new tx instance each time
749
Transaction tx = odmg.newTransaction();
750         tx.begin();
751         tx.abort();
752
753         tx = odmg.newTransaction();
754         tx.begin();
755         tx.checkpoint();
756         tx.checkpoint();
757         tx.abort();
758
759         tx = odmg.newTransaction();
760         tx.begin();
761         tx.checkpoint();
762         tx.checkpoint();
763         tx.commit();
764
765         tx = odmg.newTransaction();
766         tx.begin();
767         tx.commit();
768
769         // with same tx instance
770
tx = odmg.newTransaction();
771         tx.begin();
772         tx.abort();
773
774         tx.begin();
775         tx.checkpoint();
776         tx.checkpoint();
777         tx.abort();
778
779         tx.begin();
780         tx.checkpoint();
781         tx.checkpoint();
782         tx.commit();
783
784         tx.begin();
785         tx.commit();
786     }
787
788     public void testDoubleAbortTxCall() throws Exception JavaDoc
789     {
790         try
791         {
792             Transaction tx = odmg.newTransaction();
793             tx.begin();
794             tx.abort();
795             tx.abort();
796         }
797         catch(Exception JavaDoc e)
798         {
799             e.printStackTrace();
800             fail("We allow to do multiple tx.abort calls, but this test fails with: " + e.getMessage());
801         }
802     }
803
804     public void testInternalCausedRollback() throws Exception JavaDoc
805     {
806         Transaction tx = odmg.newTransaction();
807         String JavaDoc name = "testCheckCacheAfterRollback_"+System.currentTimeMillis();
808         try
809         {
810             tx.begin();
811
812             RollbackObjectOne ro = new RollbackObjectOne();
813             ro.setName(name);
814             tx.lock(ro, Transaction.WRITE);
815             // this should fail
816
tx.lock(new Exception JavaDoc(), Transaction.WRITE);
817
818             tx.commit();
819             fail("A exception was expected");
820         }
821         catch(Exception JavaDoc e)
822         {
823             if(tx != null && tx.isOpen()) tx.abort();
824         }
825     }
826
827
828     //**************************************************************
829
// test classes
830
//**************************************************************
831
public static class RollbackObjectOne
832     {
833         Integer JavaDoc objId;
834         String JavaDoc name;
835         String JavaDoc description;
836
837         public Integer JavaDoc getObjId()
838         {
839             return objId;
840         }
841
842         public void setObjId(Integer JavaDoc objId)
843         {
844             this.objId = objId;
845         }
846
847         public String JavaDoc getName()
848         {
849             return name;
850         }
851
852         public void setName(String JavaDoc name)
853         {
854             this.name = name;
855         }
856
857         public String JavaDoc getDescription()
858         {
859             return description;
860         }
861
862         public void setDescription(String JavaDoc description)
863         {
864             this.description = description;
865         }
866     }
867
868     public static class RollbackObjectTwo
869     {
870         Integer JavaDoc objId;
871         String JavaDoc name;
872         String JavaDoc description;
873
874         public Integer JavaDoc getObjId()
875         {
876             return objId;
877         }
878
879         public void setObjId(Integer JavaDoc objId)
880         {
881             this.objId = objId;
882         }
883
884         public String JavaDoc getName()
885         {
886             return name;
887         }
888
889         public void setName(String JavaDoc name)
890         {
891             this.name = name;
892         }
893
894         public String JavaDoc getDescription()
895         {
896             return description;
897         }
898
899         public void setDescription(String JavaDoc description)
900         {
901             this.description = description;
902         }
903     }
904 }
905
Popular Tags