Source Code Cross Referenced for CollectionsTest.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) 


0001:        package org.apache.ojb.odmg;
0002:
0003:        import java.io.Serializable;
0004:        import java.util.ArrayList;
0005:        import java.util.Arrays;
0006:        import java.util.Collection;
0007:        import java.util.Iterator;
0008:        import java.util.List;
0009:        import java.util.Vector;
0010:
0011:        import org.apache.ojb.broker.PersistenceBroker;
0012:        import org.apache.ojb.broker.PersistenceBrokerFactory;
0013:        import org.apache.ojb.broker.metadata.CollectionDescriptor;
0014:        import org.apache.ojb.broker.query.Criteria;
0015:        import org.apache.ojb.broker.query.Query;
0016:        import org.apache.ojb.broker.query.QueryFactory;
0017:        import org.apache.ojb.junit.ODMGTestCase;
0018:        import org.apache.commons.lang.builder.ToStringBuilder;
0019:        import org.apache.commons.lang.builder.ToStringStyle;
0020:        import org.odmg.OQLQuery;
0021:        import org.odmg.Transaction;
0022:
0023:        /**
0024:         * Test case handles with collections.
0025:         *
0026:         * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
0027:         * @version $Id: CollectionsTest.java,v 1.13.2.12 2005/11/25 14:35:30 arminw Exp $
0028:         */
0029:        public class CollectionsTest extends ODMGTestCase {
0030:            public CollectionsTest(String s) {
0031:                super (s);
0032:            }
0033:
0034:            public static void main(String[] args) {
0035:                String[] arr = { CollectionsTest.class.getName() };
0036:                junit.textui.TestRunner.main(arr);
0037:            }
0038:
0039:            public void setUp() throws Exception {
0040:                super .setUp();
0041:            }
0042:
0043:            protected void tearDown() throws Exception {
0044:                super .tearDown();
0045:            }
0046:
0047:            public void testStoreDeleteCascadeDelete() throws Exception {
0048:                String prefix = "testStoreDeleteCascadeDelete_"
0049:                        + System.currentTimeMillis();
0050:                String queryStr = "select gatherer from "
0051:                        + Gatherer.class.getName() + " where gatId=$1";
0052:
0053:                ojbChangeReferenceSetting(Gatherer.class, "collectiblesA",
0054:                        true, CollectionDescriptor.CASCADE_NONE,
0055:                        CollectionDescriptor.CASCADE_OBJECT, false);
0056:
0057:                // prepare test case
0058:                Gatherer gat = new Gatherer(null, prefix + "_Gatherer");
0059:                CollectibleA[] cols = prepareCollectibleA(gat, prefix);
0060:                List colList = Arrays.asList(cols);
0061:                // set List of CollectiblesA objects
0062:                gat.setCollectiblesA(colList);
0063:                TransactionExt tx = (TransactionExt) odmg.newTransaction();
0064:                tx.begin();
0065:                database.makePersistent(gat);
0066:                tx.commit();
0067:
0068:                // check if gatherer was stored
0069:                tx.begin();
0070:                tx.getBroker().clearCache();
0071:                OQLQuery query = odmg.newOQLQuery();
0072:                query.create(queryStr);
0073:                Integer gatId = gat.getGatId();
0074:                assertNotNull(gatId);
0075:                query.bind(gatId);
0076:                Collection result = (Collection) query.execute();
0077:                tx.commit();
0078:                assertEquals("Wrong number of objects found", 1, result.size());
0079:                Gatherer fetchedGat = (Gatherer) result.iterator().next();
0080:
0081:                List colsA = fetchedGat.getCollectiblesA();
0082:                assertEquals("Wrong number of CollectiblesA", 3, colsA.size());
0083:                // check if gatherer contains list of CollectibleBase
0084:                tx.begin();
0085:                //*************************************
0086:                database.deletePersistent(fetchedGat);
0087:                //*************************************
0088:                tx.commit();
0089:
0090:                // check if the CollectibleBase was really deleted from DB
0091:                tx.begin();
0092:                query = odmg.newOQLQuery();
0093:                query.create("select allCollectibleA from "
0094:                        + CollectibleA.class.getName() + " where name like $1");
0095:                query.bind(prefix + "%");
0096:                result = (Collection) query.execute();
0097:                assertEquals("Wrong number of objects found", 0, result.size());
0098:                tx.commit();
0099:
0100:                // check if the gatherer now contains a CollectibleBase list
0101:                // reduced by the deleted
0102:                tx.begin();
0103:                query = odmg.newOQLQuery();
0104:                query.create(queryStr);
0105:                query.bind(gatId);
0106:                result = (Collection) query.execute();
0107:                assertEquals("Wrong number of objects found", 0, result.size());
0108:                tx.commit();
0109:            }
0110:
0111:            public void testStoreCollectionElementWithoutBackReference()
0112:                    throws Exception {
0113:                // String queryColl = "select colls from " + CollectibleC.class.getName() + " where name=$1";
0114:                String queryGat = "select gatherer from "
0115:                        + Gatherer.class.getName() + " where gatId=$1";
0116:                String prefix = "testStoreCollectionElementWithoutBackReference_"
0117:                        + System.currentTimeMillis();
0118:
0119:                // prepare test case
0120:                Gatherer gat = new Gatherer(null, prefix + "_Gatherer");
0121:                TransactionExt tx = (TransactionExt) odmg.newTransaction();
0122:                tx.begin();
0123:                database.makePersistent(gat);
0124:                tx.commit();
0125:                // check if gatherer was stored
0126:                tx.begin();
0127:                tx.getBroker().clearCache();
0128:                assertNotNull(gat.getGatId());
0129:                OQLQuery query = odmg.newOQLQuery();
0130:                query.create(queryGat);
0131:                query.bind(gat.getGatId());
0132:                Collection result = (Collection) query.execute();
0133:                tx.commit();
0134:                assertEquals("Wrong number of objects found", 1, result.size());
0135:                gat = (Gatherer) result.iterator().next();
0136:                assertNotNull(gat);
0137:                //**********************************************
0138:                CollectibleC child = new CollectibleC(prefix, null,
0139:                        "a new CollectibleC");
0140:                tx.begin();
0141:                tx.lock(gat, Transaction.WRITE);
0142:                tx.lock(child, Transaction.WRITE);
0143:                List childs = new ArrayList();
0144:                childs.add(child);
0145:                gat.setCollectiblesC(childs);
0146:                tx.commit();
0147:                //**********************************************
0148:                // check if gatherer was stored
0149:                tx.begin();
0150:                tx.getBroker().clearCache();
0151:                assertNotNull(gat.getGatId());
0152:                query = odmg.newOQLQuery();
0153:                query.create(queryGat);
0154:                query.bind(gat.getGatId());
0155:                result = (Collection) query.execute();
0156:                tx.commit();
0157:                assertEquals("Wrong number of objects found", 1, result.size());
0158:                gat = (Gatherer) result.iterator().next();
0159:                assertNotNull(gat);
0160:                assertNotNull(gat.getCollectiblesC());
0161:                assertEquals(1, gat.getCollectiblesC().size());
0162:            }
0163:
0164:            /**
0165:             * Create an main object Gatherer with a collection of objects CollectiblesC.
0166:             * CollectiblesC hasn't a reference back to the main object. After creation we
0167:             * remove an collection element.
0168:             */
0169:            public void testRemoveCollectionElementWithoutBackReference()
0170:                    throws Exception {
0171:                // String queryColl = "select colls from " + CollectibleC.class.getName() + " where name=$1";
0172:                String queryGat = "select gatherer from "
0173:                        + Gatherer.class.getName() + " where gatId=$1";
0174:                String prefix = "testDeleteCollectionElementWithoutBackReference_"
0175:                        + System.currentTimeMillis();
0176:
0177:                // prepare test case
0178:                Gatherer gat = new Gatherer(null, prefix + "_Gatherer");
0179:                // we don't set the gatId in CollectiblesC, because we don't have one
0180:                // Set List of CollectiblesC objects
0181:                gat.setCollectiblesC(Arrays.asList(prepareCollectibleC(null,
0182:                        prefix)));
0183:                TransactionExt tx = (TransactionExt) odmg.newTransaction();
0184:                tx.begin();
0185:                database.makePersistent(gat);
0186:                tx.commit();
0187:
0188:                // check if gatherer was stored
0189:                tx.begin();
0190:                tx.getBroker().clearCache();
0191:                assertNotNull(gat.getGatId());
0192:
0193:                OQLQuery query = odmg.newOQLQuery();
0194:                query.create(queryGat);
0195:                query.bind(gat.getGatId());
0196:
0197:                Collection result = (Collection) query.execute();
0198:                tx.commit();
0199:                assertEquals("Wrong number of objects found", 1, result.size());
0200:                Gatherer fetchedGat = (Gatherer) result.iterator().next();
0201:                assertNotNull(fetchedGat);
0202:
0203:                // check if gatherer contains list of CollectibleBase
0204:                List colC = fetchedGat.getCollectiblesC();
0205:                assertEquals("Wrong number of CollectiblesC", 3, colC.size());
0206:
0207:                tx.begin();
0208:                //*************************************
0209:                tx.lock(fetchedGat, Transaction.WRITE);
0210:                // Remove collection object
0211:                Object toDelete = fetchedGat.getCollectiblesC().remove(0);
0212:                // explicit persistent delete call needed
0213:                database.deletePersistent(toDelete);
0214:                // alternative use TransactionExt#autoDeleteRemovedCollectionReferences
0215:                //*************************************
0216:                tx.commit();
0217:
0218:                // check if the Collectibles were really deleted from DB
0219:                tx.begin();
0220:                tx.getBroker().clearCache();
0221:
0222:                query = odmg.newOQLQuery();
0223:                query.create("select colls from "
0224:                        + CollectibleC.class.getName() + " where name like $1");
0225:                query.bind(prefix + "%");
0226:                result = (Collection) query.execute();
0227:
0228:                assertEquals("Wrong number of objects found", 2, result.size());
0229:                tx.commit();
0230:
0231:                // check if the gatherer now contains a CollectibleBase list
0232:                // increased by the added
0233:                tx.begin();
0234:                tx.getBroker().clearCache();
0235:                query = odmg.newOQLQuery();
0236:                query.create(queryGat);
0237:                query.bind(gat.getGatId());
0238:                result = (Collection) query.execute();
0239:                assertEquals("Wrong number of objects found", 1, result.size());
0240:                fetchedGat = (Gatherer) result.iterator().next();
0241:                colC = fetchedGat.getCollectiblesC();
0242:                assertEquals("Wrong number of CollectiblesA found in Gatherer",
0243:                        2, colC.size());
0244:                tx.commit();
0245:                assertNotNull(colC.get(0));
0246:            }
0247:
0248:            public void testStoreFetchDeleteCollectionWithBackReference()
0249:                    throws Exception {
0250:                String prefix = "testStoreFetchDeleteCollectionWithBackReference_"
0251:                        + System.currentTimeMillis();
0252:                String queryStr = "select gatherer from "
0253:                        + Gatherer.class.getName() + " where gatId=$1";
0254:
0255:                // prepare test case
0256:                Gatherer gat = new Gatherer(null, prefix + "_Gatherer");
0257:                CollectibleA[] cols = prepareCollectibleA(gat, prefix);
0258:                List colList = Arrays.asList(cols);
0259:                // set List of CollectiblesA objects
0260:                gat.setCollectiblesA(colList);
0261:                TransactionExt tx = (TransactionExt) odmg.newTransaction();
0262:                tx.begin();
0263:                database.makePersistent(gat);
0264:                tx.commit();
0265:
0266:                // check if gatherer was stored
0267:                tx.begin();
0268:                tx.getBroker().clearCache();
0269:                OQLQuery query = odmg.newOQLQuery();
0270:                query.create(queryStr);
0271:                Integer gatId = gat.getGatId();
0272:                assertNotNull(gatId);
0273:                query.bind(gatId);
0274:                Collection result = (Collection) query.execute();
0275:                tx.commit();
0276:                assertEquals("Wrong number of objects found", 1, result.size());
0277:                Gatherer fetchedGat = (Gatherer) result.iterator().next();
0278:
0279:                List colsA = fetchedGat.getCollectiblesA();
0280:                assertEquals("Wrong number of CollectiblesA", 3, colsA.size());
0281:                // check if gatherer contains list of CollectibleBase
0282:                tx.begin();
0283:                //*************************************
0284:                // delete one of the CollectibleBase
0285:                // we have to set the new reduced list in the
0286:                // gatherer object
0287:                List newCols = new ArrayList();
0288:                newCols.add(colsA.get(1));
0289:                newCols.add(colsA.get(2));
0290:                fetchedGat.setCollectiblesA(newCols);
0291:                tx.lock(fetchedGat, Transaction.WRITE);
0292:                // todo: do we need to delete removed reference explicit?
0293:                database.deletePersistent(colsA.get(0));
0294:                //*************************************
0295:                tx.commit();
0296:
0297:                // check if the CollectibleBase was really deleted from DB
0298:                tx.begin();
0299:                query = odmg.newOQLQuery();
0300:                query.create("select allCollectibleA from "
0301:                        + CollectibleA.class.getName() + " where name like $1");
0302:                query.bind(prefix + "%");
0303:                result = (Collection) query.execute();
0304:                assertEquals("Wrong number of objects found", 2, result.size());
0305:                tx.commit();
0306:
0307:                // check if the gatherer now contains a CollectibleBase list
0308:                // reduced by the deleted
0309:                tx.begin();
0310:                query = odmg.newOQLQuery();
0311:                query.create(queryStr);
0312:                query.bind(gatId);
0313:                result = (Collection) query.execute();
0314:                assertEquals("Wrong number of objects found", 1, result.size());
0315:                fetchedGat = (Gatherer) result.iterator().next();
0316:                colsA = fetchedGat.getCollectiblesA();
0317:                assertEquals("Wrong number of CollectiblesA found in Gatherer",
0318:                        2, colsA.size());
0319:                tx.commit();
0320:
0321:                colsA.get(0);
0322:            }
0323:
0324:            public void testWithBackReference_1() throws Exception {
0325:                String prefix = "testWithBackReference_1_"
0326:                        + System.currentTimeMillis();
0327:                String queryStr = "select gatherer from "
0328:                        + Gatherer.class.getName() + " where gatId=$1";
0329:
0330:                TransactionExt tx = (TransactionExt) odmg.newTransaction();
0331:                tx.begin();
0332:                // prepare test case
0333:                Gatherer gat = new Gatherer(null, prefix + "_Gatherer");
0334:                CollectibleA[] colsA = prepareCollectibleA(gat, prefix);
0335:                CollectibleB[] colsB = prepareCollectibleB(gat, prefix);
0336:                List colListA = Arrays.asList(colsA);
0337:                List colListB = Arrays.asList(colsB);
0338:                // set List of CollectibleBase objects
0339:                gat.setCollectiblesA(colListA);
0340:                gat.setCollectiblesB(colListB);
0341:
0342:                database.makePersistent(gat);
0343:                tx.commit();
0344:
0345:                // check if gatherer was stored
0346:                tx.begin();
0347:                tx.getBroker().clearCache();
0348:                OQLQuery query = odmg.newOQLQuery();
0349:                query.create(queryStr);
0350:                Integer gatId = gat.getGatId();
0351:                assertNotNull(gatId);
0352:                query.bind(gatId);
0353:                Collection result = (Collection) query.execute();
0354:
0355:                assertEquals("Wrong number of objects found", 1, result.size());
0356:                Gatherer fetchedGat = (Gatherer) result.iterator().next();
0357:                //*************************************
0358:                tx.lock(fetchedGat, Transaction.WRITE);
0359:                assertNotNull(fetchedGat.getCollectiblesA());
0360:                assertNotNull(fetchedGat.getCollectiblesB());
0361:                assertEquals(3, fetchedGat.getCollectiblesA().size());
0362:                assertEquals(3, fetchedGat.getCollectiblesB().size());
0363:                assertEquals(0, fetchedGat.getCollectiblesC().size());
0364:                assertNotNull(fetchedGat.getCollectiblesA().get(0));
0365:                assertNotNull(fetchedGat.getCollectiblesB().get(0));
0366:
0367:                fetchedGat.getCollectiblesA().remove(0);
0368:                fetchedGat.getCollectiblesB().remove(0);
0369:                //*************************************
0370:                //System.out.println("===> commit");
0371:                tx.commit();
0372:
0373:                tx.begin();
0374:                tx.getBroker().clearCache();
0375:                query = odmg.newOQLQuery();
0376:                query.create(queryStr);
0377:                gatId = gat.getGatId();
0378:                assertNotNull(gatId);
0379:                query.bind(gatId);
0380:                result = (Collection) query.execute();
0381:                tx.commit();
0382:                assertEquals("Wrong number of objects found", 1, result.size());
0383:                fetchedGat = (Gatherer) result.iterator().next();
0384:
0385:                assertNotNull(fetchedGat.getCollectiblesA());
0386:                assertNotNull(fetchedGat.getCollectiblesB());
0387:                assertEquals(2, fetchedGat.getCollectiblesA().size());
0388:                assertEquals(2, fetchedGat.getCollectiblesB().size());
0389:                assertNotNull(fetchedGat.getCollectiblesA().get(0));
0390:                assertNotNull(fetchedGat.getCollectiblesB().get(0));
0391:            }
0392:
0393:            /**
0394:             * This test shows an issue with circular references in conjunction with
0395:             * lazy loading and a non-global-shared cache.
0396:             */
0397:            public void testWithBackReference_2() throws Exception {
0398:                if (ojbSkipKnownIssueProblem("Issue using proxies with circular references and a non-global-shared cache"))
0399:                    return;
0400:
0401:                /*
0402:                Say we have an object with circular reference (A has a 1:1 reference to B
0403:                and B has a 1:n collection reference to A)
0404:                A1 -1:1-> B1 -1:n-> [A1,A4]
0405:                and the 1:n is a collection proxy.
0406:
0407:                Now user lookup A1 and get A1@11->B1@12-->[proxy@]. He wants to
0408:                remove the A4 object in the 1:n reference in B@12. Because B has an
0409:                proxy collection, the proxy materialize on
0410:                B.getA's().remove(1) ==> remove A4
0411:                call.
0412:                While materialization of the collection proxy OJB lookup again an A1 instance.
0413:                When the previous materialzed A1@11 instance isn't in the session
0414:                cache (e.g. A1 was used in a previous session), OJB lookup the real cache.
0415:                If the real cache is "empty" or works with copies of persistent objects (TLCache)
0416:                new instance for A1 ==> A1@22 and a new B1@44 will be materialized.
0417:
0418:                Thus we have after the remove of A4 with materialized proxy:
0419:                A1@11 --> B1@12 -->proxy@[A1@22[-->B1@44]-->A1@22]] !!!!
0420:                Needless to say this will cause problems on update.
0421:
0422:                The workaround for the odmg-api is shown in test #testWithBackReference_2,
0423:                if the materialization of the proxy object is done within a running tx and
0424:                the changed object is locked again after materialization of the proxy (to replace
0425:                the new created object instance with the old one).
0426:                 */
0427:
0428:                String prefix = "testWithBackReference_2_"
0429:                        + System.currentTimeMillis();
0430:                String queryStr = "select gatherer from "
0431:                        + Gatherer.class.getName() + " where gatId=$1";
0432:
0433:                TransactionExt tx = (TransactionExt) odmg.newTransaction();
0434:                tx.begin();
0435:                // prepare test case
0436:                Gatherer gat = new Gatherer(null, prefix + "_Gatherer");
0437:                CollectibleA[] colsA = prepareCollectibleA(gat, prefix);
0438:                CollectibleB[] colsB = prepareCollectibleB(gat, prefix);
0439:                List colListA = Arrays.asList(colsA);
0440:                List colListB = Arrays.asList(colsB);
0441:                // set List of CollectibleBase objects
0442:                gat.setCollectiblesA(colListA);
0443:                gat.setCollectiblesB(colListB);
0444:
0445:                database.makePersistent(gat);
0446:                tx.commit();
0447:                //System.out.println("===> commit");
0448:                //System.out.println();
0449:                //System.out.println();
0450:
0451:                // check if gatherer was stored
0452:                tx.begin();
0453:                tx.getBroker().clearCache();
0454:                OQLQuery query = odmg.newOQLQuery();
0455:                query.create(queryStr);
0456:                Integer gatId = gat.getGatId();
0457:                assertNotNull(gatId);
0458:                query.bind(gatId);
0459:                Collection result = (Collection) query.execute();
0460:                tx.commit();
0461:
0462:                assertEquals("Wrong number of objects found", 1, result.size());
0463:                Gatherer fetchedGat = (Gatherer) result.iterator().next();
0464:                assertNotNull(fetchedGat.getCollectiblesA());
0465:                assertNotNull(fetchedGat.getCollectiblesB());
0466:                assertEquals(3, fetchedGat.getCollectiblesA().size());
0467:                assertEquals(3, fetchedGat.getCollectiblesB().size());
0468:                assertEquals(0, fetchedGat.getCollectiblesC().size());
0469:                assertNotNull(fetchedGat.getCollectiblesA().get(0));
0470:                assertNotNull(fetchedGat.getCollectiblesB().get(0));
0471:                //System.out.println("A: " + fetchedGat.getCollectiblesA());
0472:                //System.out.println("B: " + fetchedGat.getCollectiblesB());
0473:                //        for(int i = 0; i < fetchedGat.getCollectiblesB().size(); i++)
0474:                //        {
0475:                //            System.out.println("  b="+fetchedGat.getCollectiblesB().get(i));
0476:                //        }
0477:                //System.out.println();
0478:                //System.out.println();
0479:                //System.out.println("## New tx begin");
0480:                tx.begin();
0481:                tx.getBroker().clearCache();
0482:                //*************************************
0483:                tx.lock(fetchedGat, Transaction.WRITE);
0484:                // we want automatic delete of removed collection objects
0485:                //tx.autoDeleteRemovedCollectionReferences(true);
0486:                // alternative do explicit call Database#deletePersistent for removed objects
0487:                fetchedGat.getCollectiblesA().remove(0);
0488:                fetchedGat.getCollectiblesB().remove(0);
0489:                tx.getBroker().serviceObjectCache().cache(
0490:                        tx.getBroker().serviceIdentity().buildIdentity(
0491:                                fetchedGat), fetchedGat);
0492:                //System.out.println("remove: " + tx.getBroker().serviceIdentity().buildIdentity(fetchedGat.getCollectiblesA().remove(0)));
0493:                //System.out.println("remove: " + tx.getBroker().serviceIdentity().buildIdentity(fetchedGat.getCollectiblesB().remove(0)));
0494:                //System.out.println("A: " + fetchedGat.getCollectiblesA());
0495:                //System.out.println("B: " + fetchedGat.getCollectiblesB());
0496:                //System.out.println("===> commit after remove");
0497:                //System.out.println("===> commit after remove");
0498:                //*************************************
0499:                tx.commit();
0500:                //System.out.println("after commit <==");
0501:                //System.out.println("after commit <==");
0502:                //System.out.println("");System.out.println();
0503:
0504:                tx.begin();
0505:                tx.getBroker().clearCache();
0506:                query = odmg.newOQLQuery();
0507:                query.create(queryStr);
0508:                gatId = gat.getGatId();
0509:                assertNotNull(gatId);
0510:                query.bind(gatId);
0511:                result = (Collection) query.execute();
0512:                tx.commit();
0513:                assertEquals("Wrong number of objects found", 1, result.size());
0514:                fetchedGat = (Gatherer) result.iterator().next();
0515:
0516:                assertNotNull(fetchedGat.getCollectiblesA());
0517:                assertNotNull(fetchedGat.getCollectiblesB());
0518:                assertEquals(2, fetchedGat.getCollectiblesA().size());
0519:                assertEquals(2, fetchedGat.getCollectiblesB().size());
0520:                assertNotNull(fetchedGat.getCollectiblesA().get(0));
0521:                assertNotNull(fetchedGat.getCollectiblesB().get(0));
0522:            }
0523:
0524:            public void testUpdateCollectionWithBackReference()
0525:                    throws Exception {
0526:                String name = "testUpdateCollectionWithBackReference"
0527:                        + System.currentTimeMillis();
0528:                String queryStr = "select colls from "
0529:                        + CollectibleA.class.getName() + " where name=$1";
0530:
0531:                // prepare test case
0532:                Gatherer gat_1 = new Gatherer(null, "Gatherer_" + name);
0533:                CollectibleA coll_1 = new CollectibleA(name);
0534:                Gatherer gat_2 = new Gatherer(null, "Gatherer_" + name);
0535:                CollectibleA coll_2 = new CollectibleA(name);
0536:
0537:                coll_1.setGatherer(gat_1);
0538:                ArrayList alist = new ArrayList();
0539:                alist.add(coll_1);
0540:                gat_1.setCollectiblesA(alist);
0541:
0542:                coll_2.setGatherer(gat_2);
0543:                ArrayList blist = new ArrayList();
0544:                blist.add(coll_2);
0545:                gat_2.setCollectiblesA(blist);
0546:
0547:                TransactionExt tx = (TransactionExt) odmg.newTransaction();
0548:                tx.begin();
0549:                database.makePersistent(coll_1);
0550:                database.makePersistent(coll_2);
0551:                tx.commit();
0552:
0553:                tx.begin();
0554:                tx.getBroker().clearCache();
0555:                OQLQuery query = odmg.newOQLQuery();
0556:                query.create(queryStr);
0557:                query.bind(name);
0558:                Collection result = (Collection) query.execute();
0559:                assertNotNull(result);
0560:                assertEquals(2, result.size());
0561:
0562:                for (Iterator iterator = result.iterator(); iterator.hasNext();) {
0563:                    CollectibleA collectible = (CollectibleA) iterator.next();
0564:                    Gatherer gat = collectible.getGatherer();
0565:                    assertNotNull(gat);
0566:                    assertEquals("Gatherer_" + name, gat.getName());
0567:                    tx.lock(collectible, Transaction.WRITE);
0568:                    collectible.getGatherer().setName("New_" + name);
0569:                }
0570:                tx.commit();
0571:
0572:                tx.begin();
0573:                tx.getBroker().clearCache();
0574:                query = odmg.newOQLQuery();
0575:                query.create(queryStr);
0576:                query.bind(name);
0577:                result = (Collection) query.execute();
0578:                assertNotNull(result);
0579:                assertEquals(2, result.size());
0580:
0581:                // we don't want that Gatherer does some cascade delete
0582:                tx.setCascadingDelete(Gatherer.class, false);
0583:                for (Iterator iterator = result.iterator(); iterator.hasNext();) {
0584:                    CollectibleA collectible = (CollectibleA) iterator.next();
0585:                    Gatherer gat = collectible.getGatherer();
0586:                    assertNotNull(gat);
0587:                    assertEquals("New_" + name, gat.getName());
0588:                    tx.lock(collectible, Transaction.WRITE);
0589:                    collectible.setGatherer(null);
0590:                    gat.getCollectiblesA().remove(collectible);
0591:                }
0592:                tx.commit();
0593:
0594:                tx.begin();
0595:                tx.getBroker().clearCache();
0596:                query = odmg.newOQLQuery();
0597:                query.create(queryStr);
0598:                query.bind(name);
0599:                result = (Collection) query.execute();
0600:                assertNotNull(result);
0601:                assertEquals(2, result.size());
0602:
0603:                for (Iterator iterator = result.iterator(); iterator.hasNext();) {
0604:                    CollectibleA collectible = (CollectibleA) iterator.next();
0605:                    Gatherer gat = collectible.getGatherer();
0606:                    assertNull(gat);
0607:                }
0608:                tx.commit();
0609:            }
0610:
0611:            /**
0612:             * Create object with 3 objects in associated collection.
0613:             * We change one object of the collection
0614:             */
0615:            public void testUpdateCollection() throws Exception {
0616:                String prefix = "testUpdateCollection"
0617:                        + System.currentTimeMillis();
0618:                String queryStr = "select gatherer from "
0619:                        + Gatherer.class.getName() + " where gatId=$1";
0620:                String modifiedName = "modified_name_"
0621:                        + System.currentTimeMillis();
0622:                String queryMod = "select coll from "
0623:                        + CollectibleA.class.getName() + " where name like $1";
0624:
0625:                // prepare test case
0626:                Gatherer gat = new Gatherer(null, prefix + "_Gatherer");
0627:                // set List of CollectiblesA objects
0628:                gat.setCollectiblesA(Arrays.asList(prepareCollectibleA(gat,
0629:                        prefix + "_1_")));
0630:                TransactionExt tx = (TransactionExt) odmg.newTransaction();
0631:                tx.begin();
0632:                database.makePersistent(gat);
0633:                tx.commit();
0634:
0635:                tx.begin();
0636:                tx.getBroker().clearCache();
0637:                // check if gatherer was stored
0638:                OQLQuery query = odmg.newOQLQuery();
0639:                query.create(queryStr);
0640:                assertNotNull(gat.getGatId());
0641:                query.bind(gat.getGatId());
0642:                Collection result = (Collection) query.execute();
0643:                tx.commit();
0644:                assertEquals("Wrong number of objects found", 1, result.size());
0645:                Gatherer fetchedGat = (Gatherer) result.iterator().next();
0646:
0647:                tx.begin();
0648:                tx.lock(fetchedGat, Transaction.WRITE);
0649:                List collsA = fetchedGat.getCollectiblesA();
0650:                assertEquals(3, collsA.size());
0651:                // now we change an object of the collection
0652:                ((CollectibleA) collsA.get(0)).setName(modifiedName);
0653:                tx.commit();
0654:
0655:                tx.begin();
0656:                tx.getBroker().clearCache();
0657:                // now check if the modification was stored
0658:                query = odmg.newOQLQuery();
0659:                query.create(queryMod);
0660:                query.bind(modifiedName);
0661:                result = (Collection) query.execute();
0662:                tx.commit();
0663:                assertEquals("Wrong number of objects found", 1, result.size());
0664:                CollectibleA collA = (CollectibleA) result.iterator().next();
0665:                assertNotNull(collA);
0666:                assertEquals(modifiedName, collA.getName());
0667:            }
0668:
0669:            /**
0670:             * we create two objects with one object in collection:
0671:             * gat1{collC1} and gat2{collC2}
0672:             * then we exchange the collections
0673:             * gat1{collC2} and gat2{collC1}
0674:             * and commit. So the size of the collection
0675:             * hold by the main object doesn't change
0676:             */
0677:            public void testUpdateWhenExchangeObjectsInCollection()
0678:                    throws Exception {
0679:                final String prefix = "testUpdateWhenExchangeObjectsInCollection"
0680:                        + System.currentTimeMillis();
0681:                final String queryStr = "select gatherer from "
0682:                        + Gatherer.class.getName()
0683:                        + " where gatId=$1 or gatId=$2 order by gatId asc";
0684:
0685:                // prepare test case
0686:                final String gat1Name = prefix + "_Gatherer";
0687:                final String gat2Name = prefix + "_Gatherer2";
0688:                Gatherer gat = new Gatherer(null, gat1Name);
0689:                Gatherer gat2 = new Gatherer(null, gat2Name);
0690:                // set List of CollectiblesC objects
0691:                CollectibleC collC_1 = new CollectibleC(prefix + "NO_1", null,
0692:                        "nothing1");
0693:                CollectibleC collC_2 = new CollectibleC(prefix + "NO_2", null,
0694:                        "nothing2");
0695:                gat.setCollectiblesC(Arrays
0696:                        .asList(new CollectibleC[] { collC_1 }));
0697:                gat2.setCollectiblesC(Arrays
0698:                        .asList(new CollectibleC[] { collC_2 }));
0699:                TransactionExt tx = (TransactionExt) odmg.newTransaction();
0700:                tx.begin();
0701:                database.makePersistent(gat);
0702:                database.makePersistent(gat2);
0703:                tx.commit();
0704:
0705:                // query and check the result
0706:                tx.begin();
0707:                tx.getBroker().clearCache();
0708:                OQLQuery query = odmg.newOQLQuery();
0709:                query.create(queryStr);
0710:                assertNotNull(gat.getGatId());
0711:                query.bind(gat.getGatId());
0712:                query.bind(gat2.getGatId());
0713:                Collection result = (Collection) query.execute();
0714:                tx.commit();
0715:                assertEquals("Wrong number of objects found", 2, result.size());
0716:                Iterator it = result.iterator();
0717:                Gatherer fetchedGat = (Gatherer) it.next();
0718:                Gatherer fetchedGat2 = (Gatherer) it.next();
0719:                assertNotNull(fetchedGat);
0720:                assertNotNull(fetchedGat2);
0721:                assertEquals(
0722:                        "Wrong gatherer returned: fetchedGat should be first Gatherer",
0723:                        gat1Name, fetchedGat.getName());
0724:                assertEquals(
0725:                        "Wrong gatherer returned: fetchedGat2 should be second Gatherer",
0726:                        gat2Name, fetchedGat2.getName());
0727:                assertNotNull(fetchedGat.collectiblesC);
0728:                assertNotNull(fetchedGat2.collectiblesC);
0729:                assertEquals(1, fetchedGat.getCollectiblesC().size());
0730:                assertEquals(1, fetchedGat2.getCollectiblesC().size());
0731:
0732:                collC_1 = (CollectibleC) fetchedGat.getCollectiblesC().get(0);
0733:                collC_2 = (CollectibleC) fetchedGat2.getCollectiblesC().get(0);
0734:                assertEquals(prefix + "NO_1", collC_1.getName());
0735:                assertEquals(prefix + "NO_2", collC_2.getName());
0736:
0737:                //*****************************************************
0738:                List list1 = fetchedGat.getCollectiblesC();
0739:                List list2 = fetchedGat2.getCollectiblesC();
0740:                // now exchange the lists
0741:                tx.begin();
0742:                tx.lock(fetchedGat, Transaction.WRITE);
0743:                tx.lock(fetchedGat2, Transaction.WRITE);
0744:                fetchedGat.setCollectiblesC(list2);
0745:                fetchedGat2.setCollectiblesC(list1);
0746:                // System.out.println("#####===> start commit");
0747:                tx.commit();
0748:                //*****************************************************
0749:                // System.out.println("#####===> end commit");
0750:
0751:                // now we do same procedure to query and check
0752:                tx.begin();
0753:                tx.getBroker().clearCache();
0754:                query = odmg.newOQLQuery();
0755:                query.create(queryStr);
0756:                assertNotNull(gat.getGatId());
0757:                query.bind(gat.getGatId());
0758:                query.bind(gat2.getGatId());
0759:                result = (Collection) query.execute();
0760:                tx.commit();
0761:                assertEquals("Wrong number of objects found", 2, result.size());
0762:                it = result.iterator();
0763:                fetchedGat = (Gatherer) it.next();
0764:                fetchedGat2 = (Gatherer) it.next();
0765:                assertNotNull(fetchedGat);
0766:                assertNotNull(fetchedGat2);
0767:                assertNotNull(fetchedGat.getCollectiblesC());
0768:                assertNotNull(fetchedGat2.getCollectiblesC());
0769:                assertEquals(1, fetchedGat.getCollectiblesC().size());
0770:                assertEquals(1, fetchedGat2.getCollectiblesC().size());
0771:
0772:                collC_1 = (CollectibleC) fetchedGat.getCollectiblesC().get(0);
0773:                collC_2 = (CollectibleC) fetchedGat2.getCollectiblesC().get(0);
0774:                // we exchange the lists, thus we expect exchanged names
0775:                assertEquals(prefix + "NO_2", collC_1.getName());
0776:                assertEquals(prefix + "NO_1", collC_2.getName());
0777:            }
0778:
0779:            /**
0780:             * Create an main object Gatherer with a collection of objects CollectiblesC.
0781:             * CollectiblesC hasn't a reference back to the main object, thus we don't have to set
0782:             * the main object in the collection objects. Further we can't set the object id of the
0783:             * main object, because we don't know it at creation time.
0784:             * Then we remove one object of the collection
0785:             */
0786:            public void testRemoveCollectionElementWithoutBackReference_2()
0787:                    throws Exception {
0788:                // String queryColl = "select colls from " + CollectibleC.class.getName() + " where name=$1";
0789:                String queryGat = "select gatherer from "
0790:                        + Gatherer.class.getName() + " where gatId=$1";
0791:                String prefix = "testRemoveCollectionObjectWithoutBackReference_2_"
0792:                        + System.currentTimeMillis();
0793:
0794:                // prepare test case
0795:                Gatherer gat = new Gatherer(null, prefix + "_Gatherer");
0796:                // we don't set the gatId in CollectiblesC, because we don't have one
0797:                // Set List of CollectiblesC objects
0798:                gat.setCollectiblesC(Arrays.asList(prepareCollectibleC(null,
0799:                        prefix)));
0800:                TransactionExt tx = (TransactionExt) odmg.newTransaction();
0801:                tx.begin();
0802:                database.makePersistent(gat);
0803:                tx.commit();
0804:
0805:                // check if gatherer was stored
0806:                tx.begin();
0807:                tx.getBroker().clearCache();
0808:                assertNotNull(gat.getGatId());
0809:
0810:                OQLQuery query = odmg.newOQLQuery();
0811:                query.create(queryGat);
0812:                query.bind(gat.getGatId());
0813:
0814:                Collection result = (Collection) query.execute();
0815:                tx.commit();
0816:                assertEquals("Wrong number of objects found", 1, result.size());
0817:                Gatherer fetchedGat = (Gatherer) result.iterator().next();
0818:                assertNotNull(fetchedGat);
0819:
0820:                List colC = fetchedGat.getCollectiblesC();
0821:                assertEquals("Wrong number of CollectiblesC", 3, colC.size());
0822:                // check if gatherer contains list of CollectibleBase
0823:                tx.begin();
0824:                //**********************************************************
0825:                // we replace the collection of main object with a new collection
0826:                // reduced by one element
0827:                List newCols = new ArrayList(colC);
0828:                Object toDelete = newCols.remove(2);
0829:                // lock object before do changes
0830:                tx.lock(fetchedGat, Transaction.WRITE);
0831:                fetchedGat.setCollectiblesC(newCols);
0832:                // todo: we need to delete removed object explicit?
0833:                database.deletePersistent(toDelete);
0834:                //**********************************************************
0835:                tx.commit();
0836:
0837:                // check if the Collectibles were really deleted from DB
0838:                tx.begin();
0839:                tx.getBroker().clearCache();
0840:
0841:                query = odmg.newOQLQuery();
0842:                query.create("select colls from "
0843:                        + CollectibleC.class.getName() + " where name like $1");
0844:                query.bind(prefix + "%");
0845:                result = (Collection) query.execute();
0846:                assertEquals("Wrong number of objects found", 2, result.size());
0847:                tx.commit();
0848:
0849:                // check if the gatherer now contains a CollectibleBase list
0850:                // reduced by the deleted
0851:                tx.begin();
0852:                query = odmg.newOQLQuery();
0853:                query.create(queryGat);
0854:                query.bind(gat.getGatId());
0855:                result = (Collection) query.execute();
0856:                assertEquals("Wrong number of objects found", 1, result.size());
0857:                fetchedGat = (Gatherer) result.iterator().next();
0858:                colC = fetchedGat.getCollectiblesC();
0859:                assertEquals("Wrong number of CollectiblesA found in Gatherer",
0860:                        2, colC.size());
0861:                tx.commit();
0862:
0863:                colC.get(0);
0864:            }
0865:
0866:            /**
0867:             * Create an main object Gatherer with a collection of objects CollectiblesC.
0868:             * CollectiblesC hasn't a reference back to the main object, thus we don't have to set
0869:             * the main object in the collection objects. Further we can't set the object id of the
0870:             * main object, because we don't know it at creation time.
0871:             * Then we ADD a new object to the collection
0872:             */
0873:            public void testAddCollectionElementWithoutBackReference()
0874:                    throws Exception {
0875:                // String queryColl = "select colls from " + CollectibleC.class.getName() + " where name=$1";
0876:                String queryGat = "select gatherer from "
0877:                        + Gatherer.class.getName() + " where gatId=$1";
0878:                String prefix = "testAddCollectionElementWithoutBackReference_"
0879:                        + System.currentTimeMillis();
0880:
0881:                // prepare test case
0882:                Gatherer gat = new Gatherer(null, prefix + "_Gatherer");
0883:                // we don't set the gatId in CollectiblesC, because we don't have one
0884:                // Set List of CollectiblesC objects
0885:                gat.setCollectiblesC(Arrays.asList(prepareCollectibleC(null,
0886:                        prefix)));
0887:                TransactionExt tx = (TransactionExt) odmg.newTransaction();
0888:                tx.begin();
0889:                database.makePersistent(gat);
0890:                tx.commit();
0891:
0892:                // check if gatherer was stored
0893:                tx.begin();
0894:                tx.getBroker().clearCache();
0895:                assertNotNull(gat.getGatId());
0896:
0897:                OQLQuery query = odmg.newOQLQuery();
0898:                query.create(queryGat);
0899:                query.bind(gat.getGatId());
0900:
0901:                Collection result = (Collection) query.execute();
0902:                tx.commit();
0903:                assertEquals("Wrong number of objects found", 1, result.size());
0904:                Gatherer fetchedGat = (Gatherer) result.iterator().next();
0905:                assertNotNull(fetchedGat);
0906:
0907:                // check if gatherer contains list of CollectibleBase
0908:                List colC = fetchedGat.getCollectiblesC();
0909:                assertEquals("Wrong number of CollectiblesC", 3, colC.size());
0910:
0911:                tx.begin();
0912:                //*************************************
0913:                tx.lock(fetchedGat, Transaction.WRITE);
0914:                // Now add a new collection object
0915:                CollectibleC newC = new CollectibleC(prefix, null,
0916:                        "### new added ###");
0917:                fetchedGat.getCollectiblesC().add(newC);
0918:                newC.setGathererId(fetchedGat.getGatId());
0919:                tx.lock(newC, Transaction.WRITE);
0920:                //*************************************
0921:                tx.commit();
0922:
0923:                // check if the Collectibles were really deleted from DB
0924:                tx.begin();
0925:                tx.getBroker().clearCache();
0926:
0927:                query = odmg.newOQLQuery();
0928:                query.create("select colls from "
0929:                        + CollectibleC.class.getName() + " where name like $1");
0930:                query.bind(prefix + "%");
0931:                result = (Collection) query.execute();
0932:                assertEquals("Wrong number of objects found", 4, result.size());
0933:                tx.commit();
0934:
0935:                // check if the gatherer now contains a CollectibleBase list
0936:                // increased by the added
0937:                tx.begin();
0938:                tx.getBroker().clearCache();
0939:                query = odmg.newOQLQuery();
0940:                query.create(queryGat);
0941:                query.bind(gat.getGatId());
0942:                result = (Collection) query.execute();
0943:                assertEquals("Wrong number of objects found", 1, result.size());
0944:                fetchedGat = (Gatherer) result.iterator().next();
0945:                colC = fetchedGat.getCollectiblesC();
0946:                assertEquals("Wrong number of CollectiblesA found in Gatherer",
0947:                        4, colC.size());
0948:                tx.commit();
0949:
0950:                colC.get(0);
0951:            }
0952:
0953:            /**
0954:             * Create an main object Gatherer with a collection of objects CollectiblesC
0955:             * (CollectiblesC has a reference back to the main object).
0956:             * Then we ADD a new object to the collection
0957:             */
0958:            public void testAddCollectionElementWithBackReference()
0959:                    throws Exception {
0960:                String queryGat = "select gatherer from "
0961:                        + Gatherer.class.getName() + " where gatId=$1";
0962:                String prefix = "testAddCollectionElementWithBackReference_"
0963:                        + System.currentTimeMillis();
0964:
0965:                /*
0966:                prepare test case
0967:                If the back reference was not set, the test doesn't pass
0968:                 */
0969:                Gatherer gat = new Gatherer(null, prefix + "_Gatherer");
0970:                // Set List of CollectiblesB objects
0971:                gat.setCollectiblesB(Arrays.asList(prepareCollectibleB(gat,
0972:                        prefix)));
0973:
0974:                TransactionExt tx = (TransactionExt) odmg.newTransaction();
0975:                tx.begin();
0976:                database.makePersistent(gat);
0977:                tx.commit();
0978:
0979:                // check if gatherer was stored
0980:                tx.begin();
0981:                tx.getBroker().clearCache();
0982:                assertNotNull(gat.getGatId());
0983:
0984:                OQLQuery query = odmg.newOQLQuery();
0985:                query.create(queryGat);
0986:                query.bind(gat.getGatId());
0987:
0988:                Collection result = (Collection) query.execute();
0989:                tx.commit();
0990:                assertEquals("Wrong number of objects found", 1, result.size());
0991:                Gatherer fetchedGat = (Gatherer) result.iterator().next();
0992:                assertNotNull(fetchedGat);
0993:
0994:                tx.begin();
0995:                // check if gatherer contains list of CollectibleBase
0996:                List colB = fetchedGat.getCollectiblesB();
0997:                assertEquals("Wrong number of CollectiblesB", 3, colB.size());
0998:
0999:                //*************************************
1000:                tx.lock(fetchedGat, Transaction.WRITE);
1001:                // Now add a new collection object
1002:                CollectibleB newB = new CollectibleB(prefix);
1003:                newB.setGatherer(fetchedGat);
1004:                fetchedGat.getCollectiblesB().add(newB);
1005:                // lock the new object
1006:                tx.lock(newB, Transaction.WRITE);
1007:                //*************************************
1008:
1009:                tx.commit();
1010:
1011:                // check
1012:                tx.begin();
1013:                tx.getBroker().clearCache();
1014:
1015:                query = odmg.newOQLQuery();
1016:                query.create("select colls from "
1017:                        + CollectibleB.class.getName() + " where name like $1");
1018:                query.bind(prefix + "%");
1019:                result = (Collection) query.execute();
1020:                assertEquals("Wrong number of objects found", 4, result.size());
1021:                tx.commit();
1022:
1023:                // check if the gatherer now contains a CollectibleBase list
1024:                // increased by the added
1025:                tx.begin();
1026:                tx.getBroker().clearCache();
1027:                query = odmg.newOQLQuery();
1028:                query.create(queryGat);
1029:                query.bind(gat.getGatId());
1030:                result = (Collection) query.execute();
1031:                assertEquals("Wrong number of objects found", 1, result.size());
1032:                fetchedGat = (Gatherer) result.iterator().next();
1033:                colB = fetchedGat.getCollectiblesB();
1034:                assertEquals("Wrong number of CollectiblesA found in Gatherer",
1035:                        4, colB.size());
1036:                tx.commit();
1037:
1038:                colB.get(0);
1039:            }
1040:
1041:            /*
1042:             User test case from user-list:
1043:             > A contains a collection of B.  B has a reference back to its parent A.
1044:             > Calling A.addB(B) sets B's reference to A.
1045:             >
1046:             > Create new A.
1047:             > Create new B.
1048:             > Add B to A.
1049:             > Make A persistent.
1050:             > A and B successfully persisted to the database.
1051:             > Clear the OJB cache
1052:             > Retrieve A (using PB)
1053:             > Start Tx (ODMG)
1054:             > Lock A for writing (ODMG)
1055:             > Create a new B2.
1056:             > Add B2 to A.
1057:             > Commit Tx.
1058:             > Clear Cache.
1059:             > Retrieve A (using PB)
1060:             > Assert(A count Bs == 2) FAIL.  B2 was never persisted.
1061:             >
1062:             > ODMG's Persistence by reachability should have persisted B2, should it not?
1063:             > I thought that using DList might fix this.
1064:             */
1065:            public void testAddCollectionElementCrossAPI() throws Exception {
1066:                String name = "testAddCollectionElementCrossAPI_"
1067:                        + System.currentTimeMillis();
1068:                // prepare test case
1069:                Gatherer gat = new Gatherer(null, name);
1070:                CollectibleB B = new CollectibleB(name);
1071:                B.setGatherer(gat);
1072:                ArrayList cols = new ArrayList();
1073:                cols.add(B);
1074:                gat.setCollectiblesB(cols);
1075:
1076:                TransactionExt tx = (TransactionExt) odmg.newTransaction();
1077:                tx.begin();
1078:                database.makePersistent(gat);
1079:                tx.commit();
1080:
1081:                // now cross ODMG with PB api
1082:                Gatherer fetchedGatherer = null;
1083:                PersistenceBroker pb = null;
1084:                try {
1085:                    pb = PersistenceBrokerFactory.defaultPersistenceBroker();
1086:                    pb.clearCache();
1087:                    Criteria crit = new Criteria();
1088:                    crit.addLike("name", name);
1089:                    Query q = QueryFactory.newQuery(Gatherer.class, crit);
1090:                    fetchedGatherer = (Gatherer) pb.getObjectByQuery(q);
1091:                } finally {
1092:                    if (pb != null)
1093:                        pb.close();
1094:                }
1095:
1096:                // check queried result
1097:                assertNotNull(fetchedGatherer);
1098:                assertEquals(gat.getGatId(), fetchedGatherer.getGatId());
1099:                assertNotNull(fetchedGatherer.getCollectiblesB());
1100:                assertEquals(1, fetchedGatherer.getCollectiblesB().size());
1101:                CollectibleB fetched_B = (CollectibleB) fetchedGatherer
1102:                        .getCollectiblesB().iterator().next();
1103:                assertNotNull(fetched_B);
1104:
1105:                // Now work with queried result
1106:                tx.begin();
1107:                tx.getBroker().clearCache();
1108:                //*************************************
1109:                tx.lock(fetchedGatherer, Transaction.WRITE);
1110:                CollectibleB newB = new CollectibleB(name);
1111:                newB.setGatherer(fetchedGatherer);
1112:                fetchedGatherer.getCollectiblesB().add(newB);
1113:                tx.lock(newB, Transaction.WRITE);
1114:                //*************************************
1115:                assertEquals(2, fetchedGatherer.getCollectiblesB().size());
1116:                tx.commit();
1117:
1118:                // now cross again ODMG with PB api
1119:                fetchedGatherer = null;
1120:                pb = null;
1121:                try {
1122:                    pb = PersistenceBrokerFactory.defaultPersistenceBroker();
1123:                    pb.clearCache();
1124:                    Criteria crit = new Criteria();
1125:                    crit.addLike("name", name);
1126:                    Query q = QueryFactory.newQuery(Gatherer.class, crit);
1127:                    fetchedGatherer = (Gatherer) pb.getObjectByQuery(q);
1128:                } finally {
1129:                    if (pb != null)
1130:                        pb.close();
1131:                }
1132:
1133:                // check queried result
1134:                assertNotNull(fetchedGatherer);
1135:                assertEquals(gat.getGatId(), fetchedGatherer.getGatId());
1136:                assertNotNull(fetchedGatherer.getCollectiblesB());
1137:                assertEquals(2, fetchedGatherer.getCollectiblesB().size());
1138:                CollectibleB fetched_B_1 = (CollectibleB) fetchedGatherer
1139:                        .getCollectiblesB().iterator().next();
1140:                CollectibleB fetched_B_2 = (CollectibleB) fetchedGatherer
1141:                        .getCollectiblesB().iterator().next();
1142:                assertNotNull(fetched_B_1);
1143:                assertNotNull(fetched_B_2);
1144:            }
1145:
1146:            //**********************************************************
1147:            // helper methods
1148:            //**********************************************************
1149:
1150:            private CollectibleA[] prepareCollectibleA(Gatherer gat,
1151:                    String namePrefix) {
1152:                CollectibleA[] colA = new CollectibleA[] {
1153:                        new CollectibleA(namePrefix + " colA_1"),
1154:                        new CollectibleA(namePrefix + " colA_2"),
1155:                        new CollectibleA(namePrefix + " colA_3") };
1156:                for (int i = 0; i < colA.length; i++) {
1157:                    CollectibleA collectibleA = colA[i];
1158:                    collectibleA.setGatherer(gat);
1159:                }
1160:                return colA;
1161:            }
1162:
1163:            private CollectibleB[] prepareCollectibleB(Gatherer gat,
1164:                    String namePrefix) {
1165:                CollectibleB[] colB = new CollectibleB[] {
1166:                        new CollectibleB(namePrefix + " colB_1"),
1167:                        new CollectibleB(namePrefix + " colB_2"),
1168:                        new CollectibleB(namePrefix + " colB_3") };
1169:                for (int i = 0; i < colB.length; i++) {
1170:                    CollectibleB collectibleB = colB[i];
1171:                    collectibleB.setGatherer(gat);
1172:                }
1173:                return colB;
1174:            }
1175:
1176:            private CollectibleC[] prepareCollectibleC(Gatherer gat,
1177:                    String namePrefix) {
1178:                CollectibleC[] colC = new CollectibleC[] {
1179:                        new CollectibleC(namePrefix + " colC_1", null, "ext1"),
1180:                        new CollectibleC(namePrefix + " colC_2", null, "ext2"),
1181:                        new CollectibleC(namePrefix + " colC_3", null, "ext3") };
1182:                for (int i = 0; i < colC.length; i++) {
1183:                    CollectibleC collectibleC = colC[i];
1184:                    collectibleC.setGathererId(gat != null ? gat.gatId : null);
1185:                }
1186:                return colC;
1187:            }
1188:
1189:            //****************************************************************************
1190:            // inner classes
1191:            //****************************************************************************
1192:            public static class Gatherer implements  Serializable {
1193:                private Integer gatId;
1194:                private String name;
1195:                private List collectiblesA = new Vector();
1196:                private List collectiblesB = new Vector();
1197:                private List collectiblesC = new Vector();
1198:
1199:                public Gatherer() {
1200:                }
1201:
1202:                public String toString() {
1203:                    return new ToStringBuilder(this ,
1204:                            ToStringStyle.MULTI_LINE_STYLE).append("gatId",
1205:                            gatId).append("name", name).append("colA",
1206:                            collectiblesA).append("colB", collectiblesB)
1207:                            .append("colc", collectiblesC).toString();
1208:                }
1209:
1210:                public void addCollectibleA(CollectibleA colA) {
1211:                    if (collectiblesA == null)
1212:                        collectiblesA = new Vector();
1213:                    collectiblesA.add(colA);
1214:                }
1215:
1216:                public Gatherer(Integer gatId, String name) {
1217:                    this .gatId = gatId;
1218:                    this .name = name;
1219:                }
1220:
1221:                public Integer getGatId() {
1222:                    return gatId;
1223:                }
1224:
1225:                public void setGatId(Integer gatId) {
1226:                    this .gatId = gatId;
1227:                }
1228:
1229:                public String getName() {
1230:                    return name;
1231:                }
1232:
1233:                public void setName(String name) {
1234:                    this .name = name;
1235:                }
1236:
1237:                public List getCollectiblesA() {
1238:                    return collectiblesA;
1239:                }
1240:
1241:                public void setCollectiblesA(List collectiblesA) {
1242:                    this .collectiblesA = collectiblesA;
1243:                }
1244:
1245:                public List getCollectiblesB() {
1246:                    return collectiblesB;
1247:                }
1248:
1249:                public void setCollectiblesB(List collectiblesB) {
1250:                    this .collectiblesB = collectiblesB;
1251:                }
1252:
1253:                public List getCollectiblesC() {
1254:                    return collectiblesC;
1255:                }
1256:
1257:                public void setCollectiblesC(List collectiblesC) {
1258:                    this .collectiblesC = collectiblesC;
1259:                }
1260:            }
1261:
1262:            public static interface CollectibleBIF extends Serializable {
1263:                Integer getColId();
1264:
1265:                void setColId(Integer colId);
1266:
1267:                String getName();
1268:
1269:                void setName(String name);
1270:
1271:                Integer getGathererId();
1272:
1273:                void setGathererId(Integer colId);
1274:
1275:                Gatherer getGatherer();
1276:
1277:                void setGatherer(Gatherer gatherer);
1278:            }
1279:
1280:            public static class CollectibleB implements  CollectibleBIF {
1281:                private Integer colId;
1282:                private String name;
1283:                private Integer gathererId;
1284:                private Gatherer gatherer;
1285:
1286:                public CollectibleB() {
1287:                }
1288:
1289:                public CollectibleB(String name) {
1290:                    this .name = name;
1291:                }
1292:
1293:                public CollectibleB(String name, Integer gathererId) {
1294:                    this .name = name;
1295:                    this .gathererId = gathererId;
1296:                }
1297:
1298:                public CollectibleB(Integer colId, String name,
1299:                        Integer gathererId) {
1300:                    this .colId = colId;
1301:                    this .name = name;
1302:                    this .gathererId = gathererId;
1303:                }
1304:
1305:                public Gatherer getGatherer() {
1306:                    return gatherer;
1307:                }
1308:
1309:                public void setGatherer(Gatherer gatherer) {
1310:                    this .gatherer = gatherer;
1311:                }
1312:
1313:                public Integer getGathererId() {
1314:                    return gathererId;
1315:                }
1316:
1317:                public void setGathererId(Integer gathererId) {
1318:                    this .gathererId = gathererId;
1319:                }
1320:
1321:                public Integer getColId() {
1322:                    return colId;
1323:                }
1324:
1325:                public void setColId(Integer colId) {
1326:                    this .colId = colId;
1327:                }
1328:
1329:                public String getName() {
1330:                    return name;
1331:                }
1332:
1333:                public void setName(String name) {
1334:                    this .name = name;
1335:                }
1336:            }
1337:
1338:            public static interface CollectibleAIF extends Serializable {
1339:                Integer getColId();
1340:
1341:                void setColId(Integer colId);
1342:
1343:                String getName();
1344:
1345:                void setName(String name);
1346:
1347:                Integer getGathererId();
1348:
1349:                void setGathererId(Integer colId);
1350:
1351:                Gatherer getGatherer();
1352:
1353:                void setGatherer(Gatherer gatherer);
1354:            }
1355:
1356:            public static class CollectibleA implements  CollectibleAIF {
1357:                private Integer colId;
1358:                private String name;
1359:                private Integer gathererId;
1360:                private Gatherer gatherer;
1361:
1362:                public CollectibleA() {
1363:                }
1364:
1365:                public CollectibleA(Integer colId, String name,
1366:                        Integer gathererId) {
1367:                    this .colId = colId;
1368:                    this .name = name;
1369:                    this .gathererId = gathererId;
1370:                }
1371:
1372:                public CollectibleA(String name, Integer gathererId) {
1373:                    this .name = name;
1374:                    this .gathererId = gathererId;
1375:                }
1376:
1377:                public CollectibleA(String name) {
1378:                    this .name = name;
1379:                }
1380:
1381:                public Gatherer getGatherer() {
1382:                    return gatherer;
1383:                }
1384:
1385:                public void setGatherer(Gatherer gatherer) {
1386:                    this .gatherer = gatherer;
1387:                }
1388:
1389:                public Integer getGathererId() {
1390:                    return gathererId;
1391:                }
1392:
1393:                public void setGathererId(Integer gathererId) {
1394:                    this .gathererId = gathererId;
1395:                }
1396:
1397:                public Integer getColId() {
1398:                    return colId;
1399:                }
1400:
1401:                public void setColId(Integer colId) {
1402:                    this .colId = colId;
1403:                }
1404:
1405:                public String getName() {
1406:                    return name;
1407:                }
1408:
1409:                public void setName(String name) {
1410:                    this .name = name;
1411:                }
1412:            }
1413:
1414:            public static interface CollectibleCIF extends Serializable {
1415:                Integer getColId();
1416:
1417:                void setColId(Integer colId);
1418:
1419:                String getName();
1420:
1421:                void setName(String name);
1422:
1423:                Integer getGathererId();
1424:
1425:                void setGathererId(Integer colId);
1426:
1427:                Gatherer getGatherer();
1428:
1429:                void setGatherer(Gatherer gatherer);
1430:
1431:                String getExtentName();
1432:
1433:                void setExtentName(String extentName);
1434:            }
1435:
1436:            public static class CollectibleC {
1437:                private Integer colId;
1438:                private String name;
1439:                private Integer gathererId;
1440:                private String extentName;
1441:
1442:                public CollectibleC() {
1443:                }
1444:
1445:                public CollectibleC(String name, Integer gathererId,
1446:                        String extentName) {
1447:                    this .name = name;
1448:                    this .gathererId = gathererId;
1449:                    this .extentName = extentName;
1450:                }
1451:
1452:                public String getExtentName() {
1453:                    return extentName;
1454:                }
1455:
1456:                public void setExtentName(String extentName) {
1457:                    this .extentName = extentName;
1458:                }
1459:
1460:                public Integer getColId() {
1461:                    return colId;
1462:                }
1463:
1464:                public void setColId(Integer colId) {
1465:                    this .colId = colId;
1466:                }
1467:
1468:                public String getName() {
1469:                    return name;
1470:                }
1471:
1472:                public void setName(String name) {
1473:                    this .name = name;
1474:                }
1475:
1476:                public Integer getGathererId() {
1477:                    return gathererId;
1478:                }
1479:
1480:                public void setGathererId(Integer gathererId) {
1481:                    this.gathererId = gathererId;
1482:                }
1483:            }
1484:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.