Source Code Cross Referenced for ODMGRollbackTest.java in  » Database-ORM » db-ojb » org » apache » ojb » odmg » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database ORM » db ojb » org.apache.ojb.odmg 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.apache.ojb.odmg;
002:
003:        import java.util.ArrayList;
004:        import java.util.Collection;
005:        import java.util.Iterator;
006:        import java.util.List;
007:
008:        import org.apache.ojb.broker.PersistenceBroker;
009:        import org.apache.ojb.broker.PersistenceBrokerFactory;
010:        import org.apache.ojb.broker.query.Criteria;
011:        import org.apache.ojb.broker.query.Query;
012:        import org.apache.ojb.broker.query.QueryByCriteria;
013:        import org.apache.ojb.broker.query.QueryByIdentity;
014:        import org.apache.ojb.broker.query.QueryFactory;
015:        import org.apache.ojb.junit.ODMGTestCase;
016:        import org.apache.ojb.odmg.shared.ODMGGourmet;
017:        import org.apache.ojb.odmg.shared.ODMGZoo;
018:        import org.odmg.Database;
019:        import org.odmg.Implementation;
020:        import org.odmg.LockNotGrantedException;
021:        import org.odmg.OQLQuery;
022:        import org.odmg.Transaction;
023:
024:        /**
025:         * Do some rollback tests and check behavior within transactions.
026:         * CAUTION: This tests works only against the default repository.
027:         */
028:        public class ODMGRollbackTest extends ODMGTestCase {
029:            public static void main(String[] args) {
030:                String[] arr = { ODMGRollbackTest.class.getName() };
031:                junit.textui.TestRunner.main(arr);
032:            }
033:
034:            public ODMGRollbackTest(String s) {
035:                super (s);
036:            }
037:
038:            public void testDatabaseClose() throws Exception {
039:                TransactionExt tx = (TransactionExt) odmg.newTransaction();
040:                try {
041:                    tx.begin();
042:                    database.close();
043:                    fail("We should not able to close database instance while running tx");
044:                } catch (Exception e) {
045:                } finally {
046:                    tx.abort();
047:                }
048:            }
049:
050:            public void testDeleteAll() throws Exception {
051:                deleteAll(odmg, ODMGZoo.class);
052:                deleteAll(odmg, ODMGGourmet.class);
053:            }
054:
055:            public void testTransactionFlush() throws Exception {
056:                String name = "testTransactionFlush_"
057:                        + System.currentTimeMillis();
058:                TransactionExt tx = (TransactionExt) odmg.newTransaction();
059:
060:                tx.begin();
061:                PersistenceBroker broker = tx.getBroker();
062:                ODMGZoo obj = new ODMGZoo();
063:                tx.lock(obj, Transaction.WRITE);
064:                obj.setName(name);
065:
066:                tx.flush();
067:
068:                Criteria crit = new Criteria();
069:                crit.addEqualTo("name", obj.getName());
070:                QueryByCriteria query = QueryFactory.newQuery(ODMGZoo.class,
071:                        crit);
072:                // we flushed all objects, thus we should find object in DB/cache
073:                Iterator it = broker.getIteratorByQuery(query);
074:                assertTrue(it.hasNext());
075:                ODMGZoo other = (ODMGZoo) it.next();
076:                assertNotNull(other);
077:                assertEquals(obj.getZooId(), other.getZooId());
078:                assertEquals(obj.getName(), other.getName());
079:                assertFalse(it.hasNext());
080:                // now we abort tx, so all flushed objects shouldn't be found
081:                // in further queries
082:                tx.abort();
083:
084:                tx = (TransactionExt) odmg.newTransaction();
085:                tx.begin();
086:                broker = tx.getBroker();
087:                QueryByIdentity query2 = new QueryByIdentity(obj);
088:                Object result = broker.getObjectByQuery(query2);
089:                tx.commit();
090:
091:                assertNull("We should not find objects from aborted tx", result);
092:            }
093:
094:            public void testTransactionFlush_2() throws Exception {
095:                String name = "testTransactionFlush_2_"
096:                        + System.currentTimeMillis();
097:                TransactionExt tx = (TransactionExt) odmg.newTransaction();
098:
099:                tx.begin();
100:                PersistenceBroker broker = tx.getBroker();
101:
102:                ODMGZoo obj = new ODMGZoo();
103:                obj.setName(name);
104:                tx.lock(obj, Transaction.WRITE);
105:                tx.flush();
106:                // System.err.println("First flush call, insert new object");
107:
108:                // PB to query
109:                Criteria crit = new Criteria();
110:                crit.addEqualTo("name", obj.getName());
111:                QueryByCriteria query = QueryFactory.newQuery(ODMGZoo.class,
112:                        crit);
113:                // we flushed all objects, thus we should found object in DB/cache
114:                Iterator it = broker.getIteratorByQuery(query);
115:                assertTrue(it.hasNext());
116:                ODMGZoo other = (ODMGZoo) it.next();
117:                assertNotNull(other);
118:                assertEquals(obj.getZooId(), other.getZooId());
119:                assertEquals(obj.getName(), other.getName());
120:                assertFalse(it.hasNext());
121:
122:                /*** Charles : Start ***/
123:                // Let's flush, change the name and flush again
124:                tx.flush();
125:                // System.err.println("Second flush call, nothing to do");
126:                obj.setName("updated_" + name);
127:                tx.flush();
128:                // System.err.println("Third flush call, update");
129:                OQLQuery q = odmg.newOQLQuery();
130:                q.create("select zoos from " + ODMGZoo.class.getName()
131:                        + " where name like $1");
132:                q.bind("updated_" + name);
133:
134:                //Redo the query - we should find the object again
135:                it = ((Collection) q.execute()).iterator();
136:                assertTrue(it.hasNext());
137:                other = (ODMGZoo) it.next();
138:                assertNotNull(other);
139:                assertEquals(obj.getZooId(), other.getZooId());
140:                assertEquals(obj.getName(), other.getName());
141:                assertFalse(it.hasNext());
142:                /*** Charles : End ***/
143:
144:                // now we abort tx, so all flushed objects shouldn't be found
145:                // in further queries
146:                tx.abort();
147:
148:                tx = (TransactionExt) odmg.newTransaction();
149:                tx.begin();
150:                broker = tx.getBroker();
151:                QueryByIdentity query2 = new QueryByIdentity(obj);
152:                Object result = broker.getObjectByQuery(query2);
153:                tx.commit();
154:
155:                assertNull("We should not find objects from aborted tx", result);
156:            }
157:
158:            /**
159:             * Tests behavior within transactions. If i store 5 odmgZoos within a transaction
160:             * and after that within the same transaction i do query 'select all odmgZoos'
161:             * the number of odmgZoos returned should be oldNumber+5 when using checkpoint.
162:             * thma:
163:             * this testcase seems to fail for some strange problems with the testbed data
164:             * the thrown error is unrelated to the things covered in the testcase.
165:             * arminw: should be fixed
166:             */
167:            public void testResultsWhileTransactionWithCheckpoint()
168:                    throws Exception {
169:                // if(ojbSkipKnownIssueProblem()) return;
170:
171:                int odmgZoosBefore = getDBObjectCountWithNewPB(ODMGZoo.class);
172:                int projectsBefore = getDBObjectCountWithNewPB(ODMGGourmet.class);
173:
174:                TransactionExt tx = (TransactionExt) odmg.newTransaction();
175:                tx.begin();
176:                List zoos = new ArrayList(getNewODMGZoos(
177:                        "testResultsWhileTransactionWithCheckpoint", 5));
178:                List projects = new ArrayList(getNewProjects(
179:                        "testResultsWhileTransactionWithCheckpoint", 3));
180:                //store some objects
181:                storeObjects(tx, zoos);
182:                storeObjects(tx, projects);
183:                // checkpoint, should bring objects to DB but shouldn't commit
184:                tx.checkpoint();
185:
186:                //Do a queries within a transaction
187:                int odmgZoosWhile = getDBObjectCountViaOqlQuery(odmg,
188:                        ODMGZoo.class);
189:                int projectsWhile = getDBObjectCountViaOqlQuery(odmg,
190:                        ODMGGourmet.class);
191:                int odmgZoosWhilePB = 0;
192:                int projectsWhilePB = 0;
193:
194:                odmgZoosWhilePB = getDBObjectCount(tx.getBroker(),
195:                        ODMGZoo.class);
196:                projectsWhilePB = getDBObjectCount(tx.getBroker(),
197:                        ODMGGourmet.class);
198:
199:                //store more
200:                List zoos2 = new ArrayList(getNewODMGZoos(
201:                        "testResultsWhileTransactionWithCheckpoint", 5));
202:                List projects2 = new ArrayList(getNewProjects(
203:                        "testResultsWhileTransactionWithCheckpoint", 2));
204:                storeObjects(tx, zoos2);
205:                storeObjects(tx, projects2);
206:
207:                zoos.addAll(zoos2);
208:                projects.addAll(projects2);
209:                // checkpoint, should bring objects to DB but shouldn't commit
210:                tx.checkpoint();
211:
212:                // after checkpoint another tx should NOT be able to lock these objects
213:                TransactionImpl tx2 = (TransactionImpl) odmg.newTransaction();
214:                tx2.begin();
215:                try {
216:                    Iterator it = zoos.iterator();
217:                    while (it.hasNext()) {
218:                        Object o = it.next();
219:                        tx2.lock(o, Transaction.WRITE);
220:                    }
221:
222:                    it = projects.iterator();
223:                    while (it.hasNext()) {
224:                        Object o = it.next();
225:                        tx2.lock(o, Transaction.WRITE);
226:                    }
227:                    fail("After checkpoint all locks should be still exist for objects");
228:                } catch (LockNotGrantedException e) {
229:                    // expected
230:                    assertTrue(true);
231:                } finally {
232:                    tx2.abort();
233:                }
234:                // reassign tx
235:                tx.join();
236:
237:                //more queries
238:                int odmgZoosWhile2 = getDBObjectCountViaOqlQuery(odmg,
239:                        ODMGZoo.class);
240:                int projectsWhile2 = getDBObjectCountViaOqlQuery(odmg,
241:                        ODMGGourmet.class);
242:                int odmgZoosWhilePB2 = 0;
243:                int projectsWhilePB2 = 0;
244:
245:                odmgZoosWhilePB2 = getDBObjectCount(tx.getBroker(),
246:                        ODMGZoo.class);
247:                projectsWhilePB2 = getDBObjectCount(tx.getBroker(),
248:                        ODMGGourmet.class);
249:
250:                tx.commit();
251:                int odmgZoosAfter = getDBObjectCountWithNewPB(ODMGZoo.class);
252:                int projectsAfter = getDBObjectCountWithNewPB(ODMGGourmet.class);
253:                int odmgZoosAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(
254:                        odmg, ODMGZoo.class);
255:                int projectsAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(
256:                        odmg, ODMGGourmet.class);
257:
258:                assertEquals("Wrong number of odmgZoos found after commit",
259:                        (odmgZoosBefore + 10), odmgZoosAfter);
260:                assertEquals("Wrong number of projects found after commit",
261:                        (projectsBefore + 5), projectsAfter);
262:                assertEquals("Wrong number of odmgZoos found after commit",
263:                        (odmgZoosBefore + 10), odmgZoosAfterOQL);
264:                assertEquals("Wrong number of projects found after commit",
265:                        (projectsBefore + 5), projectsAfterOQL);
266:
267:                /*
268:                Here we test if we can see our changes while the transaction runs. IMO it must be
269:                possible to see all changes made in a transaction.
270:                 */
271:
272:                assertEquals(
273:                        "Wrong number of odmgZoos found while transaction",
274:                        (odmgZoosBefore + 5), odmgZoosWhilePB);
275:                assertEquals(
276:                        "Wrong number of projects found while transaction",
277:                        (projectsBefore + 3), projectsWhilePB);
278:                assertEquals(
279:                        "Wrong number of odmgZoos found while transaction",
280:                        (odmgZoosBefore + 10), odmgZoosWhilePB2);
281:                assertEquals(
282:                        "Wrong number of projects found while transaction",
283:                        (projectsBefore + 5), projectsWhilePB2);
284:                assertEquals(
285:                        "Wrong number of odmgZoos found while transaction",
286:                        (odmgZoosBefore + 5), odmgZoosWhile);
287:                assertEquals(
288:                        "Wrong number of projects found while transaction",
289:                        (projectsBefore + 3), projectsWhile);
290:                assertEquals(
291:                        "Wrong number of odmgZoos found while transaction",
292:                        (odmgZoosBefore + 10), odmgZoosWhile2);
293:                assertEquals(
294:                        "Wrong number of projects found while transaction",
295:                        (projectsBefore + 5), projectsWhile2);
296:
297:                // after tx another tx should be able to lock these objects
298:                tx2 = (TransactionImpl) odmg.newTransaction();
299:                tx2.begin();
300:                try {
301:                    Iterator it = zoos.iterator();
302:                    while (it.hasNext()) {
303:                        Object o = it.next();
304:                        tx2.lock(o, Transaction.WRITE);
305:                    }
306:
307:                    it = projects.iterator();
308:                    while (it.hasNext()) {
309:                        Object o = it.next();
310:                        tx2.lock(o, Transaction.WRITE);
311:                    }
312:                } finally {
313:                    tx2.abort();
314:                }
315:            }
316:
317:            /**
318:             * Tests object count after a commited transaction
319:             * thma:
320:             * this testcase seems to fail for some strange problems with the testbed data
321:             * the thrown error is unrelated to the things covered in the testcase.
322:             * arminw: should be fixed
323:             */
324:            public void testResultsAfterTransaction() throws Exception {
325:                // if(ojbSkipKnownIssueProblem()) return;
326:
327:                int odmgZoosBefore = getDBObjectCountWithNewPB(ODMGZoo.class);
328:                int projectsBefore = getDBObjectCountWithNewPB(ODMGGourmet.class);
329:
330:                Transaction tx = odmg.newTransaction();
331:                tx.begin();
332:                //store
333:                persistentStoreObjects(database, getNewODMGZoos(
334:                        "testResultsAfterTransaction", 5));
335:                persistentStoreObjects(database, getNewProjects(
336:                        "testResultsAfterTransaction", 3));
337:                //store more
338:                storeObjects(tx, getNewODMGZoos("testResultsAfterTransaction",
339:                        5));
340:                storeObjects(tx, getNewProjects("testResultsAfterTransaction",
341:                        2));
342:                tx.commit();
343:
344:                int odmgZoosAfter = getDBObjectCountWithNewPB(ODMGZoo.class);
345:                int projectsAfter = getDBObjectCountWithNewPB(ODMGGourmet.class);
346:                int odmgZoosAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(
347:                        odmg, ODMGZoo.class);
348:                int projectsAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(
349:                        odmg, ODMGGourmet.class);
350:
351:                assertEquals("Wrong number of odmgZoos found",
352:                        (odmgZoosBefore + 10), odmgZoosAfter);
353:                assertEquals("Wrong number of projects found",
354:                        (projectsBefore + 5), projectsAfter);
355:                assertEquals("Wrong number of odmgZoos found",
356:                        (odmgZoosBefore + 10), odmgZoosAfterOQL);
357:                assertEquals("Wrong number of projects found",
358:                        (projectsBefore + 5), projectsAfterOQL);
359:
360:                //we do twice
361:                odmgZoosBefore = getDBObjectCountWithNewPB(ODMGZoo.class);
362:                projectsBefore = getDBObjectCountWithNewPB(ODMGGourmet.class);
363:
364:                //tx should be reusable
365:                tx.begin();
366:                //store
367:                persistentStoreObjects(database, getNewODMGZoos(
368:                        "testResultsAfterTransaction", 5));
369:                persistentStoreObjects(database, getNewProjects(
370:                        "testResultsAfterTransaction", 3));
371:                //store more
372:                storeObjects(tx, getNewODMGZoos("testResultsAfterTransaction",
373:                        5));
374:                storeObjects(tx, getNewProjects("testResultsAfterTransaction",
375:                        2));
376:                tx.commit();
377:
378:                odmgZoosAfter = getDBObjectCountWithNewPB(ODMGZoo.class);
379:                projectsAfter = getDBObjectCountWithNewPB(ODMGGourmet.class);
380:                odmgZoosAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(
381:                        odmg, ODMGZoo.class);
382:                projectsAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(
383:                        odmg, ODMGGourmet.class);
384:
385:                assertEquals("Wrong number of odmgZoos found",
386:                        (odmgZoosBefore + 10), odmgZoosAfter);
387:                assertEquals("Wrong number of projects found",
388:                        (projectsBefore + 5), projectsAfter);
389:                assertEquals("Wrong number of odmgZoos found",
390:                        (odmgZoosBefore + 10), odmgZoosAfterOQL);
391:                assertEquals("Wrong number of projects found",
392:                        (projectsBefore + 5), projectsAfterOQL);
393:
394:            }
395:
396:            /**
397:             * Tests object count after a commited transaction
398:             */
399:            public void testResultsAfterTransactionWithClearedCache()
400:                    throws Exception {
401:                int odmgZoosBefore = getDBObjectCountWithNewPB(ODMGZoo.class);
402:                int projectsBefore = getDBObjectCountWithNewPB(ODMGGourmet.class);
403:
404:                Transaction tx = odmg.newTransaction();
405:                tx.begin();
406:                //store
407:                persistentStoreObjects(database, getNewODMGZoos(
408:                        "testResultsAfterTransactionWithClearedCache", 5));
409:                persistentStoreObjects(database, getNewProjects(
410:                        "testResultsAfterTransactionWithClearedCache", 3));
411:                //store more
412:                storeObjects(tx, getNewODMGZoos(
413:                        "testResultsAfterTransactionWithClearedCache", 5));
414:                storeObjects(tx, getNewProjects(
415:                        "testResultsAfterTransactionWithClearedCache", 2));
416:                tx.commit();
417:
418:                //###### hack we clear cache of PB ########
419:                PersistenceBroker tmp = PersistenceBrokerFactory
420:                        .defaultPersistenceBroker();
421:                tmp.clearCache();
422:                tmp.close();
423:
424:                int odmgZoosAfter = getDBObjectCountWithNewPB(ODMGZoo.class);
425:                int projectsAfter = getDBObjectCountWithNewPB(ODMGGourmet.class);
426:                int odmgZoosAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(
427:                        odmg, ODMGZoo.class);
428:                int projectsAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(
429:                        odmg, ODMGGourmet.class);
430:
431:                assertEquals("Wrong number of odmgZoos found",
432:                        (odmgZoosBefore + 10), odmgZoosAfter);
433:                assertEquals("Wrong number of projects found",
434:                        (projectsBefore + 5), projectsAfter);
435:                assertEquals("Wrong number of odmgZoos found",
436:                        (odmgZoosBefore + 10), odmgZoosAfterOQL);
437:                assertEquals("Wrong number of projects found",
438:                        (projectsBefore + 5), projectsAfterOQL);
439:
440:                //we do twice
441:                odmgZoosBefore = getDBObjectCountWithNewPB(ODMGZoo.class);
442:                projectsBefore = getDBObjectCountWithNewPB(ODMGGourmet.class);
443:
444:                //tx should be reusable
445:                tx.begin();
446:                //store
447:                persistentStoreObjects(database, getNewODMGZoos(
448:                        "testResultsAfterTransactionWithClearedCache", 5));
449:                persistentStoreObjects(database, getNewProjects(
450:                        "testResultsAfterTransactionWithClearedCache", 3));
451:                //store more
452:                storeObjects(tx, getNewODMGZoos(
453:                        "testResultsAfterTransactionWithClearedCache", 5));
454:                storeObjects(tx, getNewProjects(
455:                        "testResultsAfterTransactionWithClearedCache", 2));
456:                tx.commit();
457:
458:                //###### hack we clear cache of PB ########
459:                tmp = PersistenceBrokerFactory.defaultPersistenceBroker();
460:                tmp.clearCache();
461:                tmp.close();
462:
463:                odmgZoosAfter = getDBObjectCountWithNewPB(ODMGZoo.class);
464:                projectsAfter = getDBObjectCountWithNewPB(ODMGGourmet.class);
465:                odmgZoosAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(
466:                        odmg, ODMGZoo.class);
467:                projectsAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(
468:                        odmg, ODMGGourmet.class);
469:
470:                assertEquals("Wrong number of odmgZoos found",
471:                        (odmgZoosBefore + 10), odmgZoosAfter);
472:                assertEquals("Wrong number of projects found",
473:                        (projectsBefore + 5), projectsAfter);
474:                assertEquals("Wrong number of odmgZoos found",
475:                        (odmgZoosBefore + 10), odmgZoosAfterOQL);
476:                assertEquals("Wrong number of projects found",
477:                        (projectsBefore + 5), projectsAfterOQL);
478:            }
479:
480:            /**
481:             * Test the rollback behaviour. If i store 10 odmgZoos within a transaction and after
482:             * that store the transaction was aborted, the number of odmgZoos in the DB should be unchanged.
483:             */
484:            public void testUserRollback() throws Exception {
485:                int odmgZoosBefore = getDBObjectCountWithNewPB(ODMGZoo.class);
486:                int projectsBefore = getDBObjectCountWithNewPB(ODMGGourmet.class);
487:
488:                Transaction tx = odmg.newTransaction();
489:                tx.begin();
490:                storeObjects(tx, getNewODMGZoos("testUserRollback", 10));
491:                storeObjects(tx, getNewProjects("testUserRollback", 10));
492:                //we abort tx
493:                tx.abort();
494:
495:                int odmgZoosAfter = getDBObjectCountWithNewPB(ODMGZoo.class);
496:                int projectsAfter = getDBObjectCountWithNewPB(ODMGGourmet.class);
497:                int odmgZoosAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(
498:                        odmg, ODMGZoo.class);
499:                int projectsAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(
500:                        odmg, ODMGGourmet.class);
501:
502:                assertEquals("Wrong number of odmgZoos found", odmgZoosBefore,
503:                        odmgZoosAfter);
504:                assertEquals("Wrong number of projects found", projectsBefore,
505:                        projectsAfter);
506:                assertEquals("Wrong number of odmgZoos found",
507:                        (odmgZoosBefore), odmgZoosAfterOQL);
508:                assertEquals("Wrong number of projects found",
509:                        (projectsBefore), projectsAfterOQL);
510:
511:                //We do this twice
512:                odmgZoosBefore = getDBObjectCountWithNewPB(ODMGZoo.class);
513:                projectsBefore = getDBObjectCountWithNewPB(ODMGGourmet.class);
514:
515:                tx.begin();
516:                storeObjects(tx, getNewODMGZoos("testUserRollback", 10));
517:                storeObjects(tx, getNewProjects("testUserRollback", 10));
518:                //we abort tx
519:                tx.abort();
520:
521:                odmgZoosAfter = getDBObjectCountWithNewPB(ODMGZoo.class);
522:                projectsAfter = getDBObjectCountWithNewPB(ODMGGourmet.class);
523:                odmgZoosAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(
524:                        odmg, ODMGZoo.class);
525:                projectsAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(
526:                        odmg, ODMGGourmet.class);
527:
528:                assertEquals("Wrong number of odmgZoos found", odmgZoosBefore,
529:                        odmgZoosAfter);
530:                assertEquals("Wrong number of projects found", projectsBefore,
531:                        projectsAfter);
532:                assertEquals("Wrong number of odmgZoos found",
533:                        (odmgZoosBefore), odmgZoosAfterOQL);
534:                assertEquals("Wrong number of projects found",
535:                        (projectsBefore), projectsAfterOQL);
536:            }
537:
538:            /**
539:             * Test the rollback behaviour. If i store 10 odmgZoos within a transaction and do a checkpoint call.
540:             * After that the transaction was aborted, the number of odmgZoos in the DB should be unchanged.
541:             */
542:            public void testUserRollbackWithCheckpoint() throws Exception {
543:                int odmgZoosBefore = getDBObjectCountWithNewPB(ODMGZoo.class);
544:                int projectsBefore = getDBObjectCountWithNewPB(ODMGGourmet.class);
545:
546:                Transaction tx = odmg.newTransaction();
547:                tx.begin();
548:                storeObjects(tx, getNewODMGZoos(
549:                        "testUserRollbackWithCheckpoint", 10));
550:                // now we store objects to DB
551:                tx.checkpoint();
552:                storeObjects(tx, getNewProjects(
553:                        "testUserRollbackWithCheckpoint", 10));
554:                //we abort tx, all actions after the last checkpoint call should be rollback
555:                tx.abort();
556:
557:                int odmgZoosAfter = getDBObjectCountWithNewPB(ODMGZoo.class);
558:                int projectsAfter = getDBObjectCountWithNewPB(ODMGGourmet.class);
559:                int odmgZoosAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(
560:                        odmg, ODMGZoo.class);
561:                int projectsAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(
562:                        odmg, ODMGGourmet.class);
563:
564:                assertEquals("Wrong number of odmgZoos found",
565:                        odmgZoosBefore + 10, odmgZoosAfter);
566:                assertEquals("Wrong number of projects found", projectsBefore,
567:                        projectsAfter);
568:                assertEquals("Wrong number of odmgZoos found",
569:                        odmgZoosBefore + 10, odmgZoosAfterOQL);
570:                assertEquals("Wrong number of projects found", projectsBefore,
571:                        projectsAfterOQL);
572:
573:                //***********************
574:                // do the procedure again
575:                odmgZoosBefore = getDBObjectCountWithNewPB(ODMGZoo.class);
576:                projectsBefore = getDBObjectCountWithNewPB(ODMGGourmet.class);
577:                // we reuse current tx
578:                tx.begin();
579:                storeObjects(tx, getNewODMGZoos(
580:                        "testUserRollbackWithCheckpoint", 10));
581:                // now we store objects to DB
582:                tx.checkpoint();
583:                storeObjects(tx, getNewProjects(
584:                        "testUserRollbackWithCheckpoint", 10));
585:                //we abort tx, all actions after the last checkpoint call should be rollback
586:                tx.abort();
587:
588:                odmgZoosAfter = getDBObjectCountWithNewPB(ODMGZoo.class);
589:                projectsAfter = getDBObjectCountWithNewPB(ODMGGourmet.class);
590:                odmgZoosAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(
591:                        odmg, ODMGZoo.class);
592:                projectsAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(
593:                        odmg, ODMGGourmet.class);
594:
595:                assertEquals("Wrong number of odmgZoos found",
596:                        odmgZoosBefore + 10, odmgZoosAfter);
597:                assertEquals("Wrong number of projects found", projectsBefore,
598:                        projectsAfter);
599:                assertEquals("Wrong number of odmgZoos found",
600:                        odmgZoosBefore + 10, odmgZoosAfterOQL);
601:                assertEquals("Wrong number of projects found", projectsBefore,
602:                        projectsAfterOQL);
603:            }
604:
605:            private void storeObjects(Transaction tx, Collection objects) {
606:                for (Iterator iterator = objects.iterator(); iterator.hasNext();) {
607:                    tx.lock(iterator.next(), Transaction.WRITE);
608:                }
609:            }
610:
611:            private void persistentStoreObjects(Database database,
612:                    Collection objects) {
613:                for (Iterator iterator = objects.iterator(); iterator.hasNext();) {
614:                    database.makePersistent(iterator.next());
615:                }
616:            }
617:
618:            private static int counter;
619:
620:            protected Collection getNewProjects(String name, int count) {
621:                ArrayList list = new ArrayList();
622:                for (int i = 0; i < count; i++) {
623:                    list.add(newProject(name));
624:                }
625:                return list;
626:            }
627:
628:            protected ODMGGourmet newProject(String name) {
629:                ODMGGourmet p = new ODMGGourmet();
630:                ++counter;
631:                if (name == null) {
632:                    p.setName("Test " + counter);
633:                } else {
634:                    p.setName(name + "_" + counter);
635:                }
636:                return p;
637:            }
638:
639:            protected Collection getNewODMGZoos(String name, int count) {
640:                ArrayList list = new ArrayList();
641:                for (int i = 0; i < count; i++) {
642:                    list.add(newODMGZoo(name));
643:                }
644:                return list;
645:            }
646:
647:            protected ODMGZoo newODMGZoo(String name) {
648:                ODMGZoo odmgZoo = new ODMGZoo();
649:                ++counter;
650:                if (name == null) {
651:                    odmgZoo.setName("animal_" + counter);
652:                } else {
653:                    odmgZoo.setName(name + "_" + counter);
654:                }
655:                return odmgZoo;
656:            }
657:
658:            protected int getDBObjectCountWithNewPB(Class target)
659:                    throws Exception {
660:                PersistenceBroker broker = PersistenceBrokerFactory
661:                        .defaultPersistenceBroker();
662:                Criteria c = new Criteria();
663:                Query q = new QueryByCriteria(target, c);
664:                int count = broker.getCount(q);
665:                broker.close();
666:                return count;
667:            }
668:
669:            protected int getDBObjectCount(PersistenceBroker broker,
670:                    Class target) throws Exception {
671:                Criteria c = new Criteria();
672:                Query q = new QueryByCriteria(target, c);
673:                int count = broker.getCount(q);
674:                return count;
675:            }
676:
677:            protected int getDBObjectCountViaOqlQueryUseNewTransaction(
678:                    Implementation odmg, Class target) throws Exception {
679:                Transaction tx = odmg.newTransaction();
680:                tx.begin();
681:                OQLQuery query = odmg.newOQLQuery();
682:                query.create("select allODMGGourmets from " + target.getName());
683:                List list = (List) query.execute();
684:                tx.commit();
685:                return list.size();
686:            }
687:
688:            protected int getDBObjectCountViaOqlQuery(Implementation odmg,
689:                    Class target) throws Exception {
690:                OQLQuery query = odmg.newOQLQuery();
691:                query.create("select allObjects from " + target.getName());
692:                List list = (List) query.execute();
693:                return list.size();
694:            }
695:
696:            protected void deleteAll(Implementation odmg, Class target)
697:                    throws Exception {
698:                TransactionExt tx = (TransactionExt) odmg.newTransaction();
699:                tx.begin();
700:                tx.setCascadingDelete(target, true);
701:                OQLQuery query = odmg.newOQLQuery();
702:                query.create("select allObjects from " + target.getName());
703:                List list = (List) query.execute();
704:                for (int i = 0; i < list.size(); i++) {
705:                    Object obj = list.get(i);
706:                    database.deletePersistent(obj);
707:                }
708:                tx.commit();
709:            }
710:
711:            // user described test case
712:            public void testDuplicateLocking() throws Exception {
713:                RollbackObjectOne ro = null;
714:
715:                Transaction tx = odmg.newTransaction();
716:                tx.begin();
717:                ro = new RollbackObjectOne();
718:                ro.setName("test_step_1");
719:                tx.lock(ro, Transaction.WRITE);
720:                tx.lock(ro, Transaction.WRITE);
721:                tx.commit();
722:
723:                tx.begin();
724:                tx.lock(ro, Transaction.WRITE);
725:                tx.lock(ro, Transaction.WRITE);
726:                ro.setName("test_step_2");
727:                tx.commit();
728:
729:                tx.begin();
730:                tx.lock(ro, Transaction.WRITE);
731:                ro.setName("test_step_3");
732:                tx.lock(ro, Transaction.WRITE);
733:                tx.commit();
734:            }
735:
736:            public void testCheckCacheAfterRollback() throws Exception {
737:                RollbackObjectOne ro = null;
738:                RollbackObjectOne ro_2 = null;
739:                String name = "testCheckCacheAfterRollback_"
740:                        + System.currentTimeMillis();
741:
742:                Transaction tx = odmg.newTransaction();
743:                tx.begin();
744:                ro = new RollbackObjectOne();
745:                ro.setName(name);
746:                tx.lock(ro, Transaction.WRITE);
747:
748:                ro_2 = new RollbackObjectOne();
749:                ro_2.setName(name);
750:                tx.lock(ro_2, Transaction.WRITE);
751:                tx.commit();
752:
753:                tx = odmg.newTransaction();
754:                tx.begin();
755:                OQLQuery query = odmg.newOQLQuery();
756:                query.create("select all from "
757:                        + RollbackObjectOne.class.getName()
758:                        + " where name like $1");
759:                query.bind(name);
760:                List list = (List) query.execute();
761:                tx.commit();
762:                assertEquals(2, list.size());
763:
764:                tx = odmg.newTransaction();
765:                tx.begin();
766:                tx.lock(ro, Transaction.WRITE);
767:                ro = new RollbackObjectOne();
768:                ro.setDescription(name);
769:
770:                tx.lock(ro_2, Transaction.WRITE);
771:                ro_2 = new RollbackObjectOne();
772:                ro_2.setDescription(name);
773:
774:                tx.abort();
775:
776:                tx = odmg.newTransaction();
777:                tx.begin();
778:                query = odmg.newOQLQuery();
779:                query.create("select all from "
780:                        + RollbackObjectOne.class.getName()
781:                        + " where name like $1");
782:                query.bind(name);
783:                list = (List) query.execute();
784:                tx.commit();
785:                assertEquals(2, list.size());
786:                assertNull(((RollbackObjectOne) list.get(0)).getDescription());
787:                assertNull(((RollbackObjectOne) list.get(1)).getDescription());
788:
789:                // after tx another tx should be able to lock these objects
790:                TransactionImpl tx2 = (TransactionImpl) odmg.newTransaction();
791:                tx2.begin();
792:                try {
793:                    Iterator it = list.iterator();
794:                    while (it.hasNext()) {
795:                        Object o = it.next();
796:                        tx2.lock(o, Transaction.WRITE);
797:                    }
798:                } finally {
799:                    tx2.abort();
800:                }
801:            }
802:
803:            /**
804:             * test empty usage of methods
805:             */
806:            public void testEmpty() throws Exception {
807:                // get new tx instance each time
808:                Transaction tx = odmg.newTransaction();
809:                tx.begin();
810:                tx.abort();
811:
812:                tx = odmg.newTransaction();
813:                tx.begin();
814:                tx.checkpoint();
815:                tx.checkpoint();
816:                tx.abort();
817:
818:                tx = odmg.newTransaction();
819:                tx.begin();
820:                tx.checkpoint();
821:                tx.checkpoint();
822:                tx.commit();
823:
824:                tx = odmg.newTransaction();
825:                tx.begin();
826:                tx.commit();
827:
828:                // with same tx instance
829:                tx = odmg.newTransaction();
830:                tx.begin();
831:                tx.abort();
832:
833:                tx.begin();
834:                tx.checkpoint();
835:                tx.checkpoint();
836:                tx.abort();
837:
838:                tx.begin();
839:                tx.checkpoint();
840:                tx.checkpoint();
841:                tx.commit();
842:
843:                tx.begin();
844:                tx.commit();
845:            }
846:
847:            public void testDoubleAbortTxCall() throws Exception {
848:                try {
849:                    Transaction tx = odmg.newTransaction();
850:                    tx.begin();
851:                    tx.abort();
852:                    tx.abort();
853:                } catch (Exception e) {
854:                    e.printStackTrace();
855:                    fail("We allow to do multiple tx.abort calls, but this test fails with: "
856:                            + e.getMessage());
857:                }
858:            }
859:
860:            public void testInternalCausedRollback() throws Exception {
861:                Transaction tx = odmg.newTransaction();
862:                String name = "testCheckCacheAfterRollback_"
863:                        + System.currentTimeMillis();
864:                try {
865:                    tx.begin();
866:
867:                    RollbackObjectOne ro = new RollbackObjectOne();
868:                    ro.setName(name);
869:                    tx.lock(ro, Transaction.WRITE);
870:                    // this should fail
871:                    tx.lock(new Exception(), Transaction.WRITE);
872:
873:                    tx.commit();
874:                    fail("A exception was expected");
875:                } catch (Exception e) {
876:                    if (tx != null && tx.isOpen())
877:                        tx.abort();
878:                }
879:            }
880:
881:            //**************************************************************
882:            // test classes
883:            //**************************************************************
884:            public static class RollbackObjectOne {
885:                Integer objId;
886:                String name;
887:                String description;
888:
889:                public Integer getObjId() {
890:                    return objId;
891:                }
892:
893:                public void setObjId(Integer objId) {
894:                    this .objId = objId;
895:                }
896:
897:                public String getName() {
898:                    return name;
899:                }
900:
901:                public void setName(String name) {
902:                    this .name = name;
903:                }
904:
905:                public String getDescription() {
906:                    return description;
907:                }
908:
909:                public void setDescription(String description) {
910:                    this .description = description;
911:                }
912:            }
913:
914:            public static class RollbackObjectTwo {
915:                Integer objId;
916:                String name;
917:                String description;
918:
919:                public Integer getObjId() {
920:                    return objId;
921:                }
922:
923:                public void setObjId(Integer objId) {
924:                    this .objId = objId;
925:                }
926:
927:                public String getName() {
928:                    return name;
929:                }
930:
931:                public void setName(String name) {
932:                    this .name = name;
933:                }
934:
935:                public String getDescription() {
936:                    return description;
937:                }
938:
939:                public void setDescription(String description) {
940:                    this.description = description;
941:                }
942:            }
943:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.