Source Code Cross Referenced for CollectionTest.java in  » Database-ORM » db-ojb » org » apache » ojb » broker » 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.broker 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        package org.apache.ojb.broker;
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:
0010:        import org.apache.commons.lang.builder.ToStringBuilder;
0011:        import org.apache.commons.lang.builder.ToStringStyle;
0012:        import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
0013:        import org.apache.ojb.broker.query.Criteria;
0014:        import org.apache.ojb.broker.query.Query;
0015:        import org.apache.ojb.broker.query.QueryByCriteria;
0016:        import org.apache.ojb.broker.query.QueryFactory;
0017:        import org.apache.ojb.broker.util.ObjectModification;
0018:        import org.apache.ojb.broker.util.collections.RemovalAwareCollection;
0019:        import org.apache.ojb.junit.PBTestCase;
0020:
0021:        /**
0022:         * Test case for collection handling.
0023:         *
0024:         * Main class Gatherer have five collections of type CollectibleBase,
0025:         * CollectibleB, CollectibleC, CollectibleD, CollectibleDD
0026:         *
0027:         * Class hierarchy:
0028:         * [CollectibleBaseIF <--] CollectibleBase <-- CollectibleB <-- CollectibleC <-- CollectibleCC
0029:         *                              |                                 |
0030:         *                         CollectibleD <-- CollectibleDD       CollectibleCCC
0031:         *
0032:         * in repository interface CollectibleBaseIF was declared with five
0033:         * extents (CollectibleBase, CollectibleB, CollectibleC, CollectibleD, CollectibleDD)
0034:         *
0035:         * CollectibleBase
0036:         * auto-retrieve, auto-update, auto-delete all true
0037:         * proxy false
0038:         *
0039:         * CollectibleB
0040:         * auto-retrieve, auto-update set true, auto-delete false
0041:         * proxy true
0042:         *
0043:         * CollectibleC
0044:         * auto-retrieve, auto-update, auto-delete set true,
0045:         * proxy true
0046:         * CollectibleC has a reference back to the Gatherer object
0047:         * (auto-retrieve, auto-update, auto-delete set false to avoid circular
0048:         * object creation)
0049:         *
0050:         * CollectibleCC
0051:         * auto-retrieve true, auto-update true, auto-delete set true,
0052:         * proxy false
0053:         *
0054:         * CollectibleCCC
0055:         * auto-retrieve false, auto-update true, auto-delete set true,
0056:         * proxy false
0057:         *
0058:         * CollectibleD
0059:         * auto-retrieve, auto-update, auto-delete all true
0060:         * proxy false
0061:         *
0062:         * CollectibleDD
0063:         * auto-retrieve, auto-update, auto-delete all true
0064:         * proxy true
0065:         *
0066:         * TODO: Need some refactoring and more structured tests of different auto_xyz/proxy settings
0067:         *
0068:         * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
0069:         * @version $Id: CollectionTest.java,v 1.11.2.7 2005/12/29 22:18:21 arminw Exp $
0070:         */
0071:        public class CollectionTest extends PBTestCase {
0072:            static final int NONE = ObjectReferenceDescriptor.CASCADE_NONE;
0073:            static final int LINK = ObjectReferenceDescriptor.CASCADE_LINK;
0074:            static final int OBJECT = ObjectReferenceDescriptor.CASCADE_OBJECT;
0075:
0076:            public static void main(String[] args) {
0077:                String[] arr = { CollectionTest.class.getName() };
0078:                junit.textui.TestRunner.main(arr);
0079:            }
0080:
0081:            /**
0082:             * test for OJB-82
0083:             */
0084:            public void testUsingExtentWhichIsNotInheritedFromBaseClass()
0085:                    throws Exception {
0086:                // TODO: fix this bug
0087:                if (ojbSkipKnownIssueProblem("Test for OJB-82 will be fixed in next version"))
0088:                    return;
0089:
0090:                String prefix = "testUsingExtentWhichIsNotInheritedFromBaseClass_"
0091:                        + System.currentTimeMillis();
0092:
0093:                ojbChangeReferenceSetting(BookShelf.class, "items", true,
0094:                        OBJECT, OBJECT, false);
0095:                ojbChangeReferenceSetting(DVD.class, "shelf", true, NONE, NONE,
0096:                        false);
0097:                ojbChangeReferenceSetting(Book.class, "shelf", true, NONE,
0098:                        NONE, false);
0099:                ojbChangeReferenceSetting(Candie.class, "shelf", true, NONE,
0100:                        NONE, false);
0101:
0102:                BookShelf bookShelf = new BookShelf(prefix);
0103:                BookShelfItem dvd = new DVD(prefix + "_dvd", bookShelf);
0104:                BookShelfItem book = new Book(prefix + "_book", bookShelf);
0105:                Candie candie = new Candie(prefix + "_book", bookShelf);
0106:                List items = new ArrayList();
0107:                items.add(dvd);
0108:                items.add(book);
0109:                items.add(candie);
0110:                bookShelf.setItems(items);
0111:
0112:                broker.beginTransaction();
0113:                broker.store(bookShelf);
0114:                broker.commitTransaction();
0115:
0116:                broker.clearCache();
0117:                BookShelf loadedCopy = (BookShelf) broker
0118:                        .getObjectByIdentity(broker.serviceIdentity()
0119:                                .buildIdentity(BookShelf.class,
0120:                                        bookShelf.getPk()));
0121:                assertNotNull(loadedCopy);
0122:                assertNotNull(loadedCopy.getItems());
0123:                assertEquals(3, loadedCopy.getItems().size());
0124:
0125:                broker.beginTransaction();
0126:                broker.clearCache();
0127:                Criteria criteria = new Criteria();
0128:                criteria.addLike("name", prefix + "%");
0129:                Query q = new QueryByCriteria(BookShelfItem.class, criteria);
0130:                Collection result = broker.getCollectionByQuery(q);
0131:                assertNotNull(result);
0132:                assertEquals(3, result.size());
0133:            }
0134:
0135:            public void testMoveProxyCollectionFromOneToAnother()
0136:                    throws Exception {
0137:                String prefix = "testMoveProxyCollectionFromOneToAnother_"
0138:                        + System.currentTimeMillis();
0139:
0140:                ojbChangeReferenceSetting(BookShelf.class, "items", true,
0141:                        OBJECT, OBJECT, true);
0142:                ojbChangeReferenceSetting(DVD.class, "shelf", true, NONE, NONE,
0143:                        true);
0144:                ojbChangeReferenceSetting(Book.class, "shelf", true, NONE,
0145:                        NONE, true);
0146:
0147:                BookShelf bookShelf = new BookShelf(prefix);
0148:                BookShelf bookShelfSecond = new BookShelf(prefix + "_second");
0149:                BookShelfItem ev1 = new DVD(prefix + "_dvd", bookShelf);
0150:                BookShelfItem ev2 = new Book(prefix + "_book", bookShelf);
0151:                bookShelf.addItem(ev1);
0152:                bookShelf.addItem(ev2);
0153:
0154:                broker.beginTransaction();
0155:                broker.store(bookShelfSecond);
0156:                broker.store(bookShelf);
0157:                broker.commitTransaction();
0158:
0159:                broker.clearCache();
0160:                BookShelf loadedCopy = (BookShelf) broker
0161:                        .getObjectByIdentity(broker.serviceIdentity()
0162:                                .buildIdentity(BookShelf.class,
0163:                                        bookShelf.getPk()));
0164:                assertNotNull(loadedCopy);
0165:                assertNotNull(loadedCopy.getItems());
0166:                assertEquals(2, loadedCopy.getItems().size());
0167:
0168:                broker.beginTransaction();
0169:                /*
0170:                now we move the unmaterialzed proxy collection from one to another object,
0171:                it's important to first store the bookshelf object with the nullified items
0172:                and then the bookshelf with moved item collection proxy - otherwise the PB-api
0173:                doesn't recognize the changes
0174:                 */
0175:                bookShelfSecond.setItems(bookShelf.getItems());
0176:                bookShelf.setItems(null);
0177:                broker.store(bookShelf, ObjectModification.UPDATE);
0178:                broker.store(bookShelfSecond, ObjectModification.UPDATE);
0179:                broker.commitTransaction();
0180:                broker.clearCache();
0181:
0182:                loadedCopy = (BookShelf) broker.getObjectByIdentity(broker
0183:                        .serviceIdentity().buildIdentity(BookShelf.class,
0184:                                bookShelf.getPk()));
0185:                assertNotNull(loadedCopy);
0186:                assertNotNull(loadedCopy.getItems());
0187:                assertEquals(0, loadedCopy.getItems().size());
0188:
0189:                BookShelf loadedCopySecond = (BookShelf) broker
0190:                        .getObjectByIdentity(broker.serviceIdentity()
0191:                                .buildIdentity(BookShelf.class,
0192:                                        bookShelfSecond.getPk()));
0193:                assertNotNull(loadedCopySecond);
0194:                assertNotNull(loadedCopySecond.getItems());
0195:                assertEquals(2, loadedCopySecond.getItems().size());
0196:
0197:                broker.clearCache();
0198:                Criteria criteria = new Criteria();
0199:                criteria.addLike("name", prefix + "%");
0200:                Query q = new QueryByCriteria(BookShelfItem.class, criteria);
0201:                Collection items = broker.getCollectionByQuery(q);
0202:                assertNotNull(items);
0203:                assertEquals(2, items.size());
0204:                // we are using collection proxies, so we have to use the interface
0205:                BookShelfItem item = (BookShelfItem) items.iterator().next();
0206:                assertNotNull(item.getShelf());
0207:                BookShelfIF bs = item.getShelf();
0208:                assertEquals(bookShelfSecond.getPk(), bs.getPk());
0209:            }
0210:
0211:            public void testReadProxyCollection() throws Exception {
0212:                String name = "testReadProxyCollection_"
0213:                        + System.currentTimeMillis();
0214:                Gatherer gat = new Gatherer(null, name);
0215:                CollectibleB[] cols = prepareCollectibleB(name);
0216:
0217:                gat.setCollectiblesB(Arrays.asList(cols));
0218:                broker.beginTransaction();
0219:                broker.store(gat);
0220:                broker.commitTransaction();
0221:
0222:                broker.clearCache();
0223:                Criteria crit = new Criteria();
0224:                crit.addLike("name", name);
0225:                Query q = QueryFactory.newQuery(Gatherer.class, crit);
0226:                Gatherer newGat = (Gatherer) broker.getObjectByQuery(q);
0227:
0228:                Iterator it = newGat.getCollectiblesB().iterator();
0229:                int i = 0;
0230:                while (it.hasNext()) {
0231:                    CollectibleB colB = (CollectibleB) it.next();
0232:                    assertTrue(colB.getName().indexOf(name) > -1);
0233:                    i++;
0234:                }
0235:                assertEquals(4, i);
0236:            }
0237:
0238:            public void testStoreReadOfUserDefinedCollectionClass() {
0239:                String name = "testStoreReadOfUserDefinedCollectionClass_"
0240:                        + System.currentTimeMillis();
0241:                Gatherer gat = new Gatherer(null, name);
0242:
0243:                CollectibleBase[] collBase = prepareCollectibleBase(name);
0244:                CollectionClassDummy dummyList = new CollectionClassDummy();
0245:                for (int i = 0; i < collBase.length; i++) {
0246:                    CollectibleBase collectibleBase = collBase[i];
0247:                    dummyList.ojbAdd(collectibleBase);
0248:                }
0249:                gat.setCollectionDummy(dummyList);
0250:
0251:                broker.beginTransaction();
0252:                broker.store(gat);
0253:                broker.commitTransaction();
0254:
0255:                Identity oid = broker.serviceIdentity().buildIdentity(gat);
0256:                broker.clearCache();
0257:                Gatherer new_gat = (Gatherer) broker.getObjectByIdentity(oid);
0258:                assertNotNull(new_gat);
0259:                assertNotNull(new_gat.getCollectionDummy());
0260:                assertEquals(collBase.length, new_gat.getCollectionDummy()
0261:                        .size());
0262:
0263:            }
0264:
0265:            public void testStoreReadOfUserDefinedCollectionClass_2() {
0266:                String name = "testStoreReadOfUserDefinedCollectionClass_2_"
0267:                        + System.currentTimeMillis();
0268:                Gatherer gat = new Gatherer(null, name);
0269:
0270:                CollectibleBase[] collBase = prepareCollectibleBase(name);
0271:                CollectionClassDummy dummyList = new CollectionClassDummy();
0272:                for (int i = 0; i < collBase.length; i++) {
0273:                    CollectibleBase collectibleBase = collBase[i];
0274:                    dummyList.ojbAdd(collectibleBase);
0275:                }
0276:                gat.setCollectionDummy(dummyList);
0277:
0278:                broker.beginTransaction();
0279:                broker.store(gat);
0280:                broker.commitTransaction();
0281:
0282:                broker.clearCache();
0283:                Criteria crit = new Criteria();
0284:                crit.addEqualTo("name", name);
0285:                Query q = QueryFactory.newQuery(Gatherer.class, crit);
0286:                Collection result = broker.getCollectionByQuery(q);
0287:                assertNotNull(result);
0288:                assertEquals(1, result.size());
0289:                Gatherer new_gat = (Gatherer) result.iterator().next();
0290:                assertNotNull(new_gat);
0291:                assertNotNull(new_gat.getCollectionDummy());
0292:                assertEquals(collBase.length, new_gat.getCollectionDummy()
0293:                        .size());
0294:
0295:            }
0296:
0297:            /**
0298:             * generate main object with collections and store
0299:             * main object to make all persistent
0300:             */
0301:            public void testStoreDeleteSimpleCollections() {
0302:                long timestamp = System.currentTimeMillis();
0303:                String colPrefix = "col_" + timestamp;
0304:                String name = timestamp + "_testStoreDeleteSimpleCollections";
0305:
0306:                // create gatherer with collections
0307:                Gatherer gatherer = new Gatherer(null, name);
0308:                gatherer.setCollectiblesBase(Arrays
0309:                        .asList(prepareCollectibleBase(colPrefix)));
0310:                gatherer.setCollectiblesB(Arrays
0311:                        .asList(prepareCollectibleB(colPrefix)));
0312:
0313:                broker.beginTransaction();
0314:                broker.store(gatherer);
0315:                broker.commitTransaction();
0316:                assertEquals("CollectibleBase objects", 3, gatherer
0317:                        .getCollectiblesBase().size());
0318:                assertEquals(gatherer.getGatId(), ((CollectibleBaseIF) gatherer
0319:                        .getCollectiblesBase().get(0)).getGathererId());
0320:                assertEquals("CollectibleB objects", 4, gatherer
0321:                        .getCollectiblesB().size());
0322:                assertEquals(gatherer.getGatId(), ((CollectibleBIF) gatherer
0323:                        .getCollectiblesB().get(0)).getGathererId());
0324:
0325:                Identity oid = broker.serviceIdentity().buildIdentity(gatherer);
0326:                broker.clearCache();
0327:                Gatherer new_gatherer = (Gatherer) broker
0328:                        .getObjectByIdentity(oid);
0329:
0330:                assertNotNull(new_gatherer);
0331:                assertNotNull(new_gatherer.getCollectiblesBase());
0332:                assertNotNull(new_gatherer.getCollectiblesB());
0333:                assertEquals("CollectibleBase objects", 3, new_gatherer
0334:                        .getCollectiblesBase().size());
0335:                assertEquals("CollectibleB objects", 4, new_gatherer
0336:                        .getCollectiblesB().size());
0337:                assertEquals(new_gatherer.getGatId(),
0338:                        ((CollectibleBaseIF) new_gatherer.getCollectiblesBase()
0339:                                .get(0)).getGathererId());
0340:                assertEquals(new_gatherer.getGatId(),
0341:                        ((CollectibleBIF) new_gatherer.getCollectiblesB()
0342:                                .get(0)).getGathererId());
0343:
0344:                broker.clearCache();
0345:
0346:                Criteria criteria = new Criteria();
0347:                criteria.addLike("name", colPrefix + "_colBase*");
0348:                Query q = new QueryByCriteria(CollectibleBase.class, criteria);
0349:                Collection result = broker.getCollectionByQuery(q);
0350:                assertNotNull(result);
0351:                assertEquals("Wrong number of queried objects", 3, result
0352:                        .size());
0353:
0354:                criteria = new Criteria();
0355:                criteria.addLike("name", colPrefix + "_colB*");
0356:                q = new QueryByCriteria(CollectibleB.class, criteria);
0357:                result = broker.getCollectionByQuery(q);
0358:                assertNotNull(result);
0359:                assertEquals("Wrong number of queried objects", 4, result
0360:                        .size());
0361:
0362:                criteria = new Criteria();
0363:                criteria.addLike("name", colPrefix + "*");
0364:                q = new QueryByCriteria(CollectibleBaseIF.class, criteria);
0365:                result = broker.getCollectionByQuery(q);
0366:                assertNotNull(result);
0367:                assertEquals("Wrong number of queried objects", 7, result
0368:                        .size());
0369:
0370:                // now we delete the main object
0371:                // and see what's going on with the dependend objects
0372:                broker.beginTransaction();
0373:                // auto-delete false set for CollectiblesB
0374:                List manuallyList = new_gatherer.getCollectiblesB();
0375:                for (Iterator iterator = manuallyList.iterator(); iterator
0376:                        .hasNext();) {
0377:                    broker.delete(iterator.next());
0378:                }
0379:                broker.delete(new_gatherer);
0380:                broker.commitTransaction();
0381:
0382:                broker.clearCache();
0383:                new_gatherer = (Gatherer) broker.getObjectByIdentity(oid);
0384:
0385:                assertNull(new_gatherer);
0386:
0387:                criteria = new Criteria();
0388:                criteria.addLike("name", colPrefix + "_colBase*");
0389:                q = new QueryByCriteria(CollectibleBase.class, criteria);
0390:                result = broker.getCollectionByQuery(q);
0391:                assertNotNull(result);
0392:                // auto-delete is set true
0393:                assertEquals("Wrong number of queried objects", 0, result
0394:                        .size());
0395:
0396:                criteria = new Criteria();
0397:                criteria.addLike("name", colPrefix + "_colB*");
0398:                q = new QueryByCriteria(CollectibleB.class, criteria);
0399:                result = broker.getCollectionByQuery(q);
0400:                assertNotNull(result);
0401:                // auto-delete is set false, but we removed manually
0402:                assertEquals("Wrong number of queried objects", 0, result
0403:                        .size());
0404:
0405:                criteria = new Criteria();
0406:                criteria.addLike("name", colPrefix + "*");
0407:                q = new QueryByCriteria(CollectibleBaseIF.class, criteria);
0408:                result = broker.getCollectionByQuery(q);
0409:                assertNotNull(result);
0410:                // since we delete all childs
0411:                assertEquals("Wrong number of queried objects", 0, result
0412:                        .size());
0413:            }
0414:
0415:            /**
0416:             * generate main object with collections and store
0417:             * main object to make all persistent
0418:             */
0419:            public void testDeleteCollection() {
0420:                long timestamp = System.currentTimeMillis();
0421:                String colPrefix = "col_" + timestamp;
0422:                String name = timestamp
0423:                        + "_testDeleteCollectionDoRemoveCollectionObjectBeforeDelete";
0424:
0425:                // create gatherer with collections
0426:                Gatherer gatherer = new Gatherer(null, name);
0427:                gatherer.setCollectiblesBase(Arrays
0428:                        .asList(prepareCollectibleBase(colPrefix)));
0429:                gatherer.setCollectiblesB(Arrays
0430:                        .asList(prepareCollectibleB(colPrefix)));
0431:                gatherer.setCollectiblesCC(Arrays
0432:                        .asList(prepareCollectibleCC(colPrefix)));
0433:
0434:                broker.beginTransaction();
0435:                broker.store(gatherer);
0436:                broker.commitTransaction();
0437:                assertEquals("CollectibleBase objects", 3, gatherer
0438:                        .getCollectiblesBase().size());
0439:                assertEquals(gatherer.getGatId(), ((CollectibleBaseIF) gatherer
0440:                        .getCollectiblesBase().get(0)).getGathererId());
0441:                assertEquals("CollectibleB objects", 4, gatherer
0442:                        .getCollectiblesB().size());
0443:                assertEquals(gatherer.getGatId(), ((CollectibleBIF) gatherer
0444:                        .getCollectiblesB().get(0)).getGathererId());
0445:
0446:                Identity oid = broker.serviceIdentity().buildIdentity(gatherer);
0447:                broker.clearCache();
0448:                Gatherer new_gatherer = (Gatherer) broker
0449:                        .getObjectByIdentity(oid);
0450:
0451:                assertNotNull(new_gatherer);
0452:                assertNotNull(new_gatherer.getCollectiblesBase());
0453:                assertNotNull(new_gatherer.getCollectiblesB());
0454:                assertEquals("CollectibleBase objects", 3, new_gatherer
0455:                        .getCollectiblesBase().size());
0456:                assertEquals("CollectibleB objects", 4, new_gatherer
0457:                        .getCollectiblesB().size());
0458:                assertEquals(new_gatherer.getGatId(),
0459:                        ((CollectibleBaseIF) new_gatherer.getCollectiblesBase()
0460:                                .get(0)).getGathererId());
0461:                assertEquals(new_gatherer.getGatId(),
0462:                        ((CollectibleBIF) new_gatherer.getCollectiblesB()
0463:                                .get(0)).getGathererId());
0464:
0465:                broker.clearCache();
0466:
0467:                Criteria criteria = new Criteria();
0468:                criteria.addLike("name", colPrefix + "_colBase*");
0469:                Query q = new QueryByCriteria(CollectibleBase.class, criteria);
0470:                Collection result = broker.getCollectionByQuery(q);
0471:                assertNotNull(result);
0472:                assertEquals("Wrong number of queried objects", 3, result
0473:                        .size());
0474:
0475:                criteria = new Criteria();
0476:                criteria.addLike("name", colPrefix + "_colB*");
0477:                q = new QueryByCriteria(CollectibleB.class, criteria);
0478:                result = broker.getCollectionByQuery(q);
0479:                assertNotNull(result);
0480:                assertEquals("Wrong number of queried objects", 4, result
0481:                        .size());
0482:
0483:                criteria = new Criteria();
0484:                criteria.addLike("name", colPrefix + "*");
0485:                q = new QueryByCriteria(CollectibleBaseIF.class, criteria);
0486:                result = broker.getCollectionByQuery(q);
0487:                assertNotNull(result);
0488:                assertEquals("Wrong number of queried objects", 7, result
0489:                        .size());
0490:
0491:                broker.beginTransaction();
0492:                // now get all CollectibleB
0493:                List colBList = new_gatherer.getCollectiblesB();
0494:                for (Iterator iterator = colBList.iterator(); iterator
0495:                        .hasNext();) {
0496:                    broker.delete(iterator.next());
0497:                }
0498:                broker.delete(new_gatherer);
0499:                broker.commitTransaction();
0500:
0501:                broker.clearCache();
0502:                new_gatherer = (Gatherer) broker.getObjectByIdentity(oid);
0503:
0504:                assertNull(new_gatherer);
0505:
0506:                criteria = new Criteria();
0507:                criteria.addLike("name", colPrefix + "_colBase*");
0508:                q = new QueryByCriteria(CollectibleBase.class, criteria);
0509:                result = broker.getCollectionByQuery(q);
0510:                assertNotNull(result);
0511:                // auto-delete is set true
0512:                assertEquals("Wrong number of queried objects", 0, result
0513:                        .size());
0514:
0515:                criteria = new Criteria();
0516:                criteria.addLike("name", colPrefix + "_colB*");
0517:                q = new QueryByCriteria(CollectibleB.class, criteria);
0518:                result = broker.getCollectionByQuery(q);
0519:                assertNotNull(result);
0520:                // auto-delete is set false
0521:                assertEquals("Wrong number of queried objects", 0, result
0522:                        .size());
0523:
0524:                criteria = new Criteria();
0525:                criteria.addLike("name", colPrefix + "*");
0526:                q = new QueryByCriteria(CollectibleBaseIF.class, criteria);
0527:                result = broker.getCollectionByQuery(q);
0528:                assertNotNull(result);
0529:                assertEquals("Wrong number of queried objects", 0, result
0530:                        .size());
0531:            }
0532:
0533:            /**
0534:             * generate main object with collections and store
0535:             * main object to make all persistent
0536:             */
0537:            public void testDeleteMainObjectWithOneToNRelation() {
0538:                long timestamp = System.currentTimeMillis();
0539:                String colPrefix = "col_" + timestamp;
0540:                String name = timestamp
0541:                        + "_testDeleteMainObjectWithOneToNRelation";
0542:
0543:                // create gatherer with collections
0544:                Gatherer gatherer = new Gatherer(null, name);
0545:                List colsList = Arrays.asList(prepareCollectibleC2(colPrefix));
0546:                gatherer.setCollectiblesC2(colsList);
0547:                for (Iterator iterator = colsList.iterator(); iterator
0548:                        .hasNext();) {
0549:                    ((CollectibleC2) iterator.next()).setGatherer(gatherer);
0550:                }
0551:
0552:                broker.beginTransaction();
0553:                broker.store(gatherer);
0554:                broker.commitTransaction();
0555:
0556:                assertNotNull(gatherer.getCollectiblesC2());
0557:                assertEquals(5, gatherer.getCollectiblesC2().size());
0558:                assertEquals(gatherer.getGatId(), ((CollectibleC2) gatherer
0559:                        .getCollectiblesC2().get(0)).getGathererId());
0560:
0561:                broker.clearCache();
0562:                Identity oid = broker.serviceIdentity().buildIdentity(gatherer);
0563:                gatherer = (Gatherer) broker.getObjectByIdentity(oid);
0564:                assertNotNull(gatherer);
0565:                // auto-retierve is set false
0566:                assertNull(gatherer.getCollectiblesC2());
0567:
0568:                Criteria criteria = new Criteria();
0569:                criteria.addLike("name", colPrefix + "*");
0570:                Query q = new QueryByCriteria(CollectibleC2.class, criteria);
0571:                Collection result = broker.getCollectionByQuery(q);
0572:                assertNotNull(result);
0573:                assertEquals("Wrong number of queried objects", 5, result
0574:                        .size());
0575:
0576:                broker.beginTransaction();
0577:                // auto-retieve is false, so get references manually
0578:                broker.retrieveAllReferences(gatherer);
0579:                assertNotNull(gatherer.getCollectiblesC2());
0580:                List colList = gatherer.getCollectiblesC2();
0581:                for (Iterator iterator = colList.iterator(); iterator.hasNext();) {
0582:                    // delete all references first
0583:                    broker.delete(iterator.next());
0584:                }
0585:                broker.delete(gatherer);
0586:                broker.commitTransaction();
0587:
0588:                criteria = new Criteria();
0589:                criteria.addLike("name", colPrefix + "*");
0590:                q = new QueryByCriteria(CollectibleC2.class, criteria);
0591:                result = broker.getCollectionByQuery(q);
0592:                assertNotNull(result);
0593:                assertEquals("Wrong number of queried objects", 0, result
0594:                        .size());
0595:            }
0596:
0597:            /**
0598:             * generate main object with collections and store
0599:             * main object to make all persistent
0600:             * using ojbConcreteClass feature to map different
0601:             * objects to same table
0602:             */
0603:            public void testStoreDeleteSimpleCollections_2() {
0604:                long timestamp = System.currentTimeMillis();
0605:                String colPrefix = "col_" + timestamp;
0606:                String name = timestamp + "_testStoreDeleteSimpleCollections";
0607:
0608:                // create gatherer with collections
0609:                Gatherer gatherer = new Gatherer(null, name);
0610:                gatherer.setCollectiblesD(Arrays
0611:                        .asList(prepareCollectibleD(colPrefix)));
0612:                gatherer.setCollectiblesDD(Arrays
0613:                        .asList(prepareCollectibleDD(colPrefix)));
0614:
0615:                broker.beginTransaction();
0616:                broker.store(gatherer);
0617:                broker.commitTransaction();
0618:                assertEquals("CollectibleD objects", 2, gatherer
0619:                        .getCollectiblesD().size());
0620:                assertEquals(gatherer.getGatId(), ((CollectibleDIF) gatherer
0621:                        .getCollectiblesD().get(0)).getGathererId());
0622:                assertEquals("CollectibleDD objects", 3, gatherer
0623:                        .getCollectiblesDD().size());
0624:                assertEquals(gatherer.getGatId(), ((CollectibleDDIF) gatherer
0625:                        .getCollectiblesDD().get(0)).getGathererId());
0626:
0627:                Identity oid = broker.serviceIdentity().buildIdentity(gatherer);
0628:                broker.clearCache();
0629:                Gatherer new_gatherer = (Gatherer) broker
0630:                        .getObjectByIdentity(oid);
0631:
0632:                assertNotNull(new_gatherer);
0633:                assertNotNull(new_gatherer.getCollectiblesD());
0634:                assertNotNull(new_gatherer.getCollectiblesDD());
0635:                assertEquals("CollectibleD objects", 2, new_gatherer
0636:                        .getCollectiblesD().size());
0637:                assertEquals("CollectibleDD objects", 3, new_gatherer
0638:                        .getCollectiblesDD().size());
0639:                assertEquals(new_gatherer.getGatId(),
0640:                        ((CollectibleDIF) new_gatherer.getCollectiblesD()
0641:                                .get(0)).getGathererId());
0642:                assertEquals(new_gatherer.getGatId(),
0643:                        ((CollectibleDDIF) new_gatherer.getCollectiblesDD()
0644:                                .get(0)).getGathererId());
0645:
0646:                broker.clearCache();
0647:
0648:                Criteria criteria = new Criteria();
0649:                criteria.addLike("name", colPrefix + "_colD*");
0650:                criteria.addLike("name", colPrefix + "*");
0651:                Query q = new QueryByCriteria(CollectibleD.class, criteria);
0652:                Collection result = broker.getCollectionByQuery(q);
0653:                assertNotNull(result);
0654:                assertEquals("Wrong number of queried objects", 2, result
0655:                        .size());
0656:
0657:                criteria = new Criteria();
0658:                criteria.addLike("name", colPrefix + "_colDD*");
0659:                q = new QueryByCriteria(CollectibleDD.class, criteria);
0660:                result = broker.getCollectionByQuery(q);
0661:                assertNotNull(result);
0662:                assertEquals("Wrong number of queried objects", 3, result
0663:                        .size());
0664:
0665:                // now test objConcreteClass feature
0666:                // should only return CollectibleD class instances
0667:                criteria = new Criteria();
0668:                criteria.addLike("name", colPrefix + "*");
0669:                q = new QueryByCriteria(CollectibleD.class, criteria);
0670:                result = broker.getCollectionByQuery(q);
0671:                assertNotNull(result);
0672:                assertEquals("Wrong number of queried objects", 2, result
0673:                        .size());
0674:                // now test objConcreteClass feature
0675:                criteria = new Criteria();
0676:                criteria.addLike("name", colPrefix + "*");
0677:                q = new QueryByCriteria(CollectibleDD.class, criteria);
0678:                result = broker.getCollectionByQuery(q);
0679:                assertNotNull(result);
0680:                assertEquals("Wrong number of queried objects", 3, result
0681:                        .size());
0682:
0683:                criteria = new Criteria();
0684:                criteria.addLike("name", colPrefix + "*");
0685:                q = new QueryByCriteria(CollectibleBaseIF.class, criteria);
0686:                result = broker.getCollectionByQuery(q);
0687:                assertNotNull(result);
0688:                assertEquals("Wrong number of queried objects", 5, result
0689:                        .size());
0690:
0691:                // now we delete the main object
0692:                // and see what's going on with the dependend objects
0693:                broker.beginTransaction();
0694:                broker.delete(new_gatherer);
0695:                broker.commitTransaction();
0696:
0697:                broker.clearCache();
0698:                new_gatherer = (Gatherer) broker.getObjectByIdentity(oid);
0699:
0700:                assertNull(new_gatherer);
0701:
0702:                criteria = new Criteria();
0703:                criteria.addLike("name", colPrefix + "_colD*");
0704:                q = new QueryByCriteria(CollectibleD.class, criteria);
0705:                result = broker.getCollectionByQuery(q);
0706:                assertNotNull(result);
0707:                // auto-delete is set true
0708:                assertEquals("Wrong number of queried objects", 0, result
0709:                        .size());
0710:
0711:                criteria = new Criteria();
0712:                criteria.addLike("name", colPrefix + "_colDD*");
0713:                q = new QueryByCriteria(CollectibleDD.class, criteria);
0714:                result = broker.getCollectionByQuery(q);
0715:                assertNotNull(result);
0716:                // auto-delete is set true
0717:                assertEquals("Wrong number of queried objects", 0, result
0718:                        .size());
0719:
0720:                criteria = new Criteria();
0721:                criteria.addLike("name", colPrefix + "*");
0722:                q = new QueryByCriteria(CollectibleBaseIF.class, criteria);
0723:                result = broker.getCollectionByQuery(q);
0724:                assertNotNull(result);
0725:                assertEquals("Wrong number of queried objects", 0, result
0726:                        .size());
0727:            }
0728:
0729:            public void testStoreSimpleCollections() {
0730:                long timestamp = System.currentTimeMillis();
0731:                String colPrefix = "col_" + timestamp;
0732:                String name = timestamp + "_testStoreSimpleCollections";
0733:
0734:                // create gatherer with collections
0735:                Gatherer gatherer = new Gatherer(null, name);
0736:
0737:                broker.beginTransaction();
0738:                broker.store(gatherer);
0739:                gatherer.setCollectiblesBase(Arrays
0740:                        .asList(prepareCollectibleBase(colPrefix)));
0741:                gatherer.setCollectiblesB(Arrays
0742:                        .asList(prepareCollectibleB(colPrefix)));
0743:                broker.store(gatherer);
0744:                broker.commitTransaction();
0745:
0746:                Identity oid = broker.serviceIdentity().buildIdentity(gatherer);
0747:                broker.clearCache();
0748:                Gatherer new_gatherer = (Gatherer) broker
0749:                        .getObjectByIdentity(oid);
0750:
0751:                assertNotNull(new_gatherer);
0752:                assertNotNull(new_gatherer.getCollectiblesBase());
0753:                assertNotNull(new_gatherer.getCollectiblesB());
0754:                assertEquals("CollectibleBase objects", 3, new_gatherer
0755:                        .getCollectiblesBase().size());
0756:                assertEquals("CollectibleB objects", 4, new_gatherer
0757:                        .getCollectiblesB().size());
0758:                Integer gatId = ((CollectibleBaseIF) new_gatherer
0759:                        .getCollectiblesBase().get(0)).getGathererId();
0760:                assertNotNull(gatId);
0761:                assertEquals(new_gatherer.gatId, gatId);
0762:                broker.clearCache();
0763:
0764:                Criteria criteria = new Criteria();
0765:                criteria.addLike("name", colPrefix + "_colBase*");
0766:                Query q = new QueryByCriteria(CollectibleBase.class, criteria);
0767:                Collection result = broker.getCollectionByQuery(q);
0768:                assertNotNull(result);
0769:                assertEquals("Wrong number of queried objects", 3, result
0770:                        .size());
0771:
0772:                criteria = new Criteria();
0773:                criteria.addLike("name", colPrefix + "_colB*");
0774:                q = new QueryByCriteria(CollectibleB.class, criteria);
0775:                result = broker.getCollectionByQuery(q);
0776:                assertNotNull(result);
0777:                assertEquals("Wrong number of queried objects", 4, result
0778:                        .size());
0779:
0780:                criteria = new Criteria();
0781:                criteria.addLike("name", colPrefix + "*");
0782:                q = new QueryByCriteria(CollectibleBaseIF.class, criteria);
0783:                result = broker.getCollectionByQuery(q);
0784:                assertNotNull(result);
0785:                assertEquals("Wrong number of queried objects", 7, result
0786:                        .size());
0787:            }
0788:
0789:            /**
0790:             * Add new reference objects to an existing collection reference (1:n)
0791:             * of a main object.
0792:             */
0793:            public void testAddNewObjectsToExistingCollection() {
0794:                long timestamp = System.currentTimeMillis();
0795:                String colPrefix = "testAddNewObjectsToExistingCollection_"
0796:                        + timestamp;
0797:                String name = "testAddNewObjectsToExistingCollection_"
0798:                        + timestamp;
0799:
0800:                // create gatherer with collections
0801:                Gatherer gatherer = new Gatherer(null, name);
0802:
0803:                broker.beginTransaction();
0804:                gatherer.setCollectiblesBase(Arrays
0805:                        .asList(prepareCollectibleBase(colPrefix)));
0806:                broker.store(gatherer);
0807:                broker.commitTransaction();
0808:
0809:                Identity oid = broker.serviceIdentity().buildIdentity(gatherer);
0810:                broker.clearCache();
0811:                Gatherer new_gatherer = (Gatherer) broker
0812:                        .getObjectByIdentity(oid);
0813:
0814:                assertNotNull(new_gatherer);
0815:                assertNotNull(new_gatherer.getCollectiblesBase());
0816:                assertEquals("CollectibleBase objects", 3, new_gatherer
0817:                        .getCollectiblesBase().size());
0818:                Integer gatId = ((CollectibleBaseIF) new_gatherer
0819:                        .getCollectiblesBase().get(0)).getGathererId();
0820:                assertNotNull(gatId);
0821:                assertEquals(new_gatherer.gatId, gatId);
0822:                broker.clearCache();
0823:
0824:                // additional check, read reference objects by query
0825:                Criteria criteria = new Criteria();
0826:                criteria.addLike("name", colPrefix + "*");
0827:                Query q = new QueryByCriteria(CollectibleBase.class, criteria);
0828:                Collection result = broker.getCollectionByQuery(q);
0829:                assertNotNull(result);
0830:                assertEquals("Wrong number of queried objects", 3, result
0831:                        .size());
0832:
0833:                List newEntries = Arrays
0834:                        .asList(prepareCollectibleBase(colPrefix));
0835:                new_gatherer.getCollectiblesBase().addAll(newEntries);
0836:                broker.beginTransaction();
0837:                broker.store(new_gatherer);
0838:                broker.commitTransaction();
0839:                broker.clearCache();
0840:
0841:                new_gatherer = (Gatherer) broker.getObjectByIdentity(oid);
0842:                assertNotNull(new_gatherer);
0843:                assertNotNull(new_gatherer.getCollectiblesBase());
0844:                assertEquals("CollectibleBase objects", 6, new_gatherer
0845:                        .getCollectiblesBase().size());
0846:                gatId = ((CollectibleBaseIF) new_gatherer.getCollectiblesBase()
0847:                        .get(5)).getGathererId();
0848:                assertNotNull(gatId);
0849:                assertEquals(new_gatherer.gatId, gatId);
0850:            }
0851:
0852:            /**
0853:             * generate main object with collections and store
0854:             * main object to make all persistent.
0855:             * same like {@link #testStoreSimpleCollections} but now the
0856:             * collection objects have a reference back to main object.
0857:             *
0858:             * Curious but this test does not pass
0859:             */
0860:            public void testStoreCollectionObjectsWithBackReference() {
0861:                long timestamp = System.currentTimeMillis();
0862:                String colPrefix = "col_" + timestamp;
0863:                String name = timestamp
0864:                        + "_testStoreCollectionObjectsWithBackReference";
0865:
0866:                // create gatherer with collections
0867:                Gatherer gatherer = new Gatherer(null, name);
0868:                List collsCList = Arrays.asList(prepareCollectibleC(colPrefix));
0869:                gatherer.setCollectiblesC(collsCList);
0870:                for (Iterator iterator = collsCList.iterator(); iterator
0871:                        .hasNext();) {
0872:                    ((CollectibleC) iterator.next()).setGatherer(gatherer);
0873:                }
0874:
0875:                broker.beginTransaction();
0876:                broker.store(gatherer);
0877:                broker.commitTransaction();
0878:                assertEquals("CollectibleC objects", 5, gatherer
0879:                        .getCollectiblesC().size());
0880:                assertNotNull(gatherer.getCollectiblesC());
0881:                assertTrue(gatherer.getCollectiblesC().size() > 0);
0882:                assertEquals(gatherer.getGatId(), ((CollectibleCIF) gatherer
0883:                        .getCollectiblesC().get(0)).getGathererId());
0884:
0885:                Identity oid = broker.serviceIdentity().buildIdentity(gatherer);
0886:                broker.clearCache();
0887:                Gatherer new_gatherer = (Gatherer) broker
0888:                        .getObjectByIdentity(oid);
0889:
0890:                assertNotNull(new_gatherer);
0891:                assertNotNull(new_gatherer.getCollectiblesC());
0892:                assertEquals("CollectibleC objects", 5, gatherer
0893:                        .getCollectiblesC().size());
0894:                assertEquals(gatherer.getGatId(), ((CollectibleCIF) gatherer
0895:                        .getCollectiblesC().get(0)).getGathererId());
0896:
0897:                broker.clearCache();
0898:
0899:                Criteria criteria = new Criteria();
0900:                criteria.addLike("name", colPrefix + "_colC*");
0901:                Query q = new QueryByCriteria(CollectibleC.class, criteria);
0902:                Collection result = broker.getCollectionByQuery(q);
0903:                assertNotNull(result);
0904:                assertEquals("Wrong number of queried objects", 5, result
0905:                        .size());
0906:
0907:                criteria = new Criteria();
0908:                criteria.addLike("name", colPrefix + "*");
0909:                q = new QueryByCriteria(CollectibleBaseIF.class, criteria);
0910:                result = broker.getCollectionByQuery(q);
0911:                assertNotNull(result);
0912:                assertEquals("Wrong number of queried objects", 5, result
0913:                        .size());
0914:            }
0915:
0916:            public void testOneBookShelfQueryByCollection() throws Exception {
0917:                String prefix = "testOneBookShelfQueryByCollection_"
0918:                        + System.currentTimeMillis();
0919:
0920:                BookShelf bookShelf = new BookShelf(prefix);
0921:                BookShelfItem ev1 = new DVD(bookShelf);
0922:                bookShelf.addItem(ev1);
0923:                BookShelfItem ev2 = new Book(bookShelf);
0924:                bookShelf.addItem(ev2);
0925:
0926:                broker.beginTransaction();
0927:                broker.store(bookShelf);
0928:                broker.store(ev1);
0929:                broker.store(ev2);
0930:                broker.commitTransaction();
0931:                assertTrue(bookShelf.getPk() != null);
0932:
0933:                broker.clearCache();
0934:                BookShelf loadedCopy = (BookShelf) broker
0935:                        .getObjectByIdentity(broker.serviceIdentity()
0936:                                .buildIdentity(BookShelf.class,
0937:                                        bookShelf.getPk()));
0938:                assertNotNull(loadedCopy);
0939:                assertNotNull(loadedCopy.getItems());
0940:                assertEquals(2, loadedCopy.getItems().size());
0941:
0942:                broker.clearCache();
0943:                Criteria criteria = new Criteria();
0944:                criteria.addLike("name", prefix);
0945:                Query q = new QueryByCriteria(BookShelf.class, criteria);
0946:                Collection books = broker.getCollectionByQuery(q);
0947:                assertNotNull(books);
0948:                assertEquals(1, books.size());
0949:                // we are using collection proxies, so we have to use the interface
0950:                BookShelfIF bookShelfIF = (BookShelfIF) books.iterator().next();
0951:                assertNotNull(bookShelfIF.getItems());
0952:                assertEquals("wrong number of items found", 2, bookShelfIF
0953:                        .getItems().size());
0954:            }
0955:
0956:            public void testOneBookShelfQueryByIterator() throws Exception {
0957:                String prefix = "testOneBookShelfQueryByIterator_"
0958:                        + System.currentTimeMillis();
0959:
0960:                BookShelf bookShelf = new BookShelf(prefix);
0961:                BookShelfItem ev1 = new DVD(bookShelf);
0962:                bookShelf.addItem(ev1);
0963:                BookShelfItem ev2 = new Book(bookShelf);
0964:                bookShelf.addItem(ev2);
0965:
0966:                broker.beginTransaction();
0967:                broker.store(bookShelf);
0968:                broker.store(ev1);
0969:                broker.store(ev2);
0970:                broker.commitTransaction();
0971:
0972:                assertTrue(bookShelf.getPk() != null);
0973:
0974:                broker.clearCache();
0975:                BookShelf loadedCopy = (BookShelf) broker
0976:                        .getObjectByIdentity(broker.serviceIdentity()
0977:                                .buildIdentity(BookShelf.class,
0978:                                        bookShelf.getPk()));
0979:                assertNotNull(loadedCopy);
0980:                assertNotNull(loadedCopy.getItems());
0981:                assertEquals(2, loadedCopy.getItems().size());
0982:
0983:                broker.clearCache();
0984:                Criteria criteria = new Criteria();
0985:                criteria.addLike("name", prefix);
0986:                Query q = new QueryByCriteria(BookShelf.class, criteria);
0987:                Iterator books = broker.getIteratorByQuery(q);
0988:                assertTrue(books.hasNext());
0989:                loadedCopy = (BookShelf) books.next();
0990:                assertNotNull(loadedCopy.getItems());
0991:                assertEquals("wrong number of items found", 2, loadedCopy
0992:                        .getItems().size());
0993:            }
0994:
0995:            /**
0996:             * Test RemovalAwareCollection remove() and clear()
0997:             */
0998:            public void testRemovalAwareCollection() {
0999:                String prefix = "testRemovalAwareCollection_"
1000:                        + System.currentTimeMillis();
1001:
1002:                Identity gathererId;
1003:                Gatherer loadedCopy;
1004:                Gatherer gatherer = new Gatherer(null, "Gatherer_" + prefix);
1005:                List coll = new ArrayList();
1006:                coll.add(new CollectibleBase("Base_1_" + prefix));
1007:                coll.add(new CollectibleBase("Base_2_" + prefix));
1008:                coll.add(new CollectibleBase("Base_3_" + prefix));
1009:                gatherer.setCollectiblesBase(coll);
1010:
1011:                broker.beginTransaction();
1012:                broker.store(gatherer);
1013:                broker.commitTransaction();
1014:                assertTrue(gatherer.getGatId() != null);
1015:                gathererId = broker.serviceIdentity().buildIdentity(gatherer);
1016:
1017:                broker.clearCache();
1018:                loadedCopy = (Gatherer) broker.getObjectByIdentity(gathererId);
1019:                assertNotNull(loadedCopy);
1020:                assertNotNull(loadedCopy.getCollectiblesBase());
1021:                assertTrue(loadedCopy.getCollectiblesBase() instanceof  RemovalAwareCollection);
1022:                assertEquals(3, loadedCopy.getCollectiblesBase().size());
1023:
1024:                //
1025:                // Remove a single element
1026:                //
1027:                broker.beginTransaction();
1028:                loadedCopy.getCollectiblesBase().remove(2);
1029:                broker.store(loadedCopy);
1030:                broker.commitTransaction();
1031:
1032:                broker.clearCache();
1033:                loadedCopy = (Gatherer) broker.getObjectByIdentity(gathererId);
1034:                assertNotNull(loadedCopy);
1035:                assertNotNull(loadedCopy.getCollectiblesBase());
1036:                assertTrue(loadedCopy.getCollectiblesBase() instanceof  RemovalAwareCollection);
1037:                assertEquals(2, loadedCopy.getCollectiblesBase().size());
1038:
1039:                //
1040:                // Remove all elements
1041:                //
1042:                broker.beginTransaction();
1043:                loadedCopy.getCollectiblesBase().clear();
1044:                broker.store(loadedCopy);
1045:                broker.commitTransaction();
1046:
1047:                broker.clearCache();
1048:                loadedCopy = (Gatherer) broker.getObjectByIdentity(gathererId);
1049:                assertNotNull(loadedCopy);
1050:                assertNotNull(loadedCopy.getCollectiblesBase());
1051:                assertTrue(loadedCopy.getCollectiblesBase() instanceof  RemovalAwareCollection);
1052:                assertEquals(0, loadedCopy.getCollectiblesBase().size());
1053:            }
1054:
1055:            /**
1056:             * Test RemovalAwareCollection remove() of non persistent obj
1057:             */
1058:            public void testRemovalAwareCollection2() {
1059:                String prefix = "testRemovalAwareCollection2_"
1060:                        + System.currentTimeMillis();
1061:
1062:                Identity gathererId;
1063:                Gatherer loadedCopy;
1064:                Gatherer gatherer = new Gatherer(null, "Gatherer_" + prefix);
1065:                List coll = new ArrayList();
1066:                coll.add(new CollectibleBase("Base_1_" + prefix));
1067:                coll.add(new CollectibleBase("Base_2_" + prefix));
1068:                gatherer.setCollectiblesBase(coll);
1069:
1070:                broker.beginTransaction();
1071:                broker.store(gatherer);
1072:                broker.commitTransaction();
1073:                assertTrue(gatherer.getGatId() != null);
1074:                gathererId = broker.serviceIdentity().buildIdentity(gatherer);
1075:
1076:                broker.clearCache();
1077:                loadedCopy = (Gatherer) broker.getObjectByIdentity(gathererId);
1078:                assertNotNull(loadedCopy);
1079:                assertNotNull(loadedCopy.getCollectiblesBase());
1080:                assertTrue(loadedCopy.getCollectiblesBase() instanceof  RemovalAwareCollection);
1081:                assertEquals(2, loadedCopy.getCollectiblesBase().size());
1082:
1083:                // add and remove non persistent obj
1084:                loadedCopy.getCollectiblesBase().add(
1085:                        new CollectibleBase("Base_3_" + prefix));
1086:                assertEquals(3, loadedCopy.getCollectiblesBase().size());
1087:                loadedCopy.getCollectiblesBase().remove(2);
1088:
1089:                broker.beginTransaction();
1090:                broker.store(loadedCopy);
1091:                broker.commitTransaction();
1092:            }
1093:
1094:            /**
1095:             * Test RemovalAwareCollection remove() and clear()
1096:             */
1097:            public void testRemovalAwareCollectionProxy() {
1098:                String prefix = "testRemovalAwareCollectionProxy_"
1099:                        + System.currentTimeMillis();
1100:
1101:                Identity pgId;
1102:                ProductGroup loadedCopy;
1103:                InterfaceArticle article;
1104:                ProductGroup pg = new ProductGroup(null, "PG_" + prefix, null);
1105:                article = new Article();
1106:                article.setArticleName("Art_1_" + prefix);
1107:                pg.add(article);
1108:                article = new Article();
1109:                article.setArticleName("Art_2_" + prefix);
1110:                pg.add(article);
1111:                article = new Article();
1112:                article.setArticleName("Art_3_" + prefix);
1113:                pg.add(article);
1114:
1115:                broker.beginTransaction();
1116:                broker.store(pg);
1117:                broker.commitTransaction();
1118:                assertTrue(pg.getGroupId() != null);
1119:                pgId = broker.serviceIdentity().buildIdentity(pg);
1120:
1121:                broker.clearCache();
1122:                loadedCopy = (ProductGroup) broker.getObjectByIdentity(pgId);
1123:                assertNotNull(loadedCopy);
1124:                assertNotNull(loadedCopy.getAllArticlesInGroup());
1125:                assertTrue(loadedCopy.getAllArticlesInGroup() instanceof  RemovalAwareCollection);
1126:                assertEquals(3, loadedCopy.getAllArticlesInGroup().size());
1127:
1128:                //
1129:                // Remove a single element
1130:                //
1131:                broker.beginTransaction();
1132:                loadedCopy.getAllArticlesInGroup().remove(2);
1133:                broker.store(loadedCopy);
1134:                broker.commitTransaction();
1135:
1136:                broker.clearCache();
1137:                loadedCopy = (ProductGroup) broker.getObjectByIdentity(pgId);
1138:                assertNotNull(loadedCopy);
1139:                assertNotNull(loadedCopy.getAllArticlesInGroup());
1140:                assertTrue(loadedCopy.getAllArticlesInGroup() instanceof  RemovalAwareCollection);
1141:                assertEquals(2, loadedCopy.getAllArticlesInGroup().size());
1142:
1143:                //
1144:                // Remove all elements
1145:                //
1146:                broker.beginTransaction();
1147:                loadedCopy.getAllArticlesInGroup().clear();
1148:                broker.store(loadedCopy);
1149:                broker.commitTransaction();
1150:
1151:                broker.clearCache();
1152:                loadedCopy = (ProductGroup) broker.getObjectByIdentity(pgId);
1153:                assertNotNull(loadedCopy);
1154:                assertNotNull(loadedCopy.getAllArticlesInGroup());
1155:                assertTrue(loadedCopy.getAllArticlesInGroup() instanceof  RemovalAwareCollection);
1156:                assertEquals(0, loadedCopy.getAllArticlesInGroup().size());
1157:            }
1158:
1159:            //************************************************************
1160:            // helper methods
1161:            //************************************************************
1162:
1163:            private CollectibleBase[] prepareCollectibleBase(String namePrefix) {
1164:                return new CollectibleBase[] {
1165:                        new CollectibleBase(namePrefix + "_colBase_1"),
1166:                        new CollectibleBase(namePrefix + "_colBase_2"),
1167:                        new CollectibleBase(namePrefix + "_colBase_3") };
1168:            }
1169:
1170:            private CollectibleB[] prepareCollectibleB(String namePrefix) {
1171:                return new CollectibleB[] {
1172:                        new CollectibleB(namePrefix + "_colB_1"),
1173:                        new CollectibleB(namePrefix + "_colB_2"),
1174:                        new CollectibleB(namePrefix + "_colB_3"),
1175:                        new CollectibleB(namePrefix + "_colB_4") };
1176:            }
1177:
1178:            private CollectibleC[] prepareCollectibleC(String namePrefix) {
1179:                return new CollectibleC[] {
1180:                        new CollectibleC(namePrefix + "_colC_1", "ext1"),
1181:                        new CollectibleC(namePrefix + "_colC_2", "ext2"),
1182:                        new CollectibleC(namePrefix + "_colC_3", "ext3"),
1183:                        new CollectibleC(namePrefix + "_colC_4", "ext4"),
1184:                        new CollectibleC(namePrefix + "_colC_5", "ext5") };
1185:            }
1186:
1187:            private CollectibleCC[] prepareCollectibleCC(String namePrefix) {
1188:                return new CollectibleCC[] {
1189:                        new CollectibleCC(namePrefix + "_colCC_1", "ext1"),
1190:                        new CollectibleCC(namePrefix + "_colCC_2", "ext2"),
1191:                        new CollectibleCC(namePrefix + "_colCC_3", "ext3"),
1192:                        new CollectibleCC(namePrefix + "_colCC_4", "ext4"),
1193:                        new CollectibleCC(namePrefix + "_colCC_5", "ext5") };
1194:            }
1195:
1196:            private CollectibleC2[] prepareCollectibleC2(String namePrefix) {
1197:                return new CollectibleC2[] {
1198:                        new CollectibleC2(namePrefix + "_colC2_1", "ext1"),
1199:                        new CollectibleC2(namePrefix + "_colC2_2", "ext2"),
1200:                        new CollectibleC2(namePrefix + "_colC2_3", "ext3"),
1201:                        new CollectibleC2(namePrefix + "_colC2_4", "ext4"),
1202:                        new CollectibleC2(namePrefix + "_colC2_5", "ext5") };
1203:            }
1204:
1205:            private CollectibleD[] prepareCollectibleD(String namePrefix) {
1206:                return new CollectibleD[] {
1207:                        new CollectibleD(namePrefix + "_colD_1"),
1208:                        new CollectibleD(namePrefix + "_colD_2"), };
1209:            }
1210:
1211:            private CollectibleDD[] prepareCollectibleDD(String namePrefix) {
1212:                return new CollectibleDD[] {
1213:                        new CollectibleDD(namePrefix + "_colDD_1"),
1214:                        new CollectibleDD(namePrefix + "_colDD_2"),
1215:                        new CollectibleDD(namePrefix + "_colDD_3") };
1216:            }
1217:
1218:            //*********************************************************************
1219:            // inner classes - persistent object
1220:            //*********************************************************************
1221:
1222:            public static class CollectionClassDummy implements 
1223:                    ManageableCollection {
1224:                ArrayList list = new ArrayList();
1225:
1226:                public void ojbAdd(Object anObject) {
1227:                    list.add(anObject);
1228:                }
1229:
1230:                public void ojbAddAll(ManageableCollection otherCollection) {
1231:                    Iterator it = otherCollection.ojbIterator();
1232:                    while (it.hasNext()) {
1233:                        list.add(it.next());
1234:                    }
1235:                }
1236:
1237:                public Iterator ojbIterator() {
1238:                    return list.iterator();
1239:                }
1240:
1241:                public void afterStore(PersistenceBroker broker)
1242:                        throws PersistenceBrokerException {
1243:                    //noop
1244:                }
1245:
1246:                public int size() {
1247:                    return list.size();
1248:                }
1249:            }
1250:
1251:            public static class Gatherer implements  Serializable {
1252:                private Integer gatId;
1253:                private String name;
1254:                private List collectiblesBase;
1255:                private List collectiblesB;
1256:                private List collectiblesC;
1257:                private List collectiblesCC;
1258:                private List collectiblesC2;
1259:                private List collectiblesD;
1260:                private List collectiblesDD;
1261:                private CollectionClassDummy collectionDummy;
1262:
1263:                public Gatherer() {
1264:                }
1265:
1266:                public Gatherer(Integer gatId, String name) {
1267:                    this .gatId = gatId;
1268:                    this .name = name;
1269:                }
1270:
1271:                public Integer getGatId() {
1272:                    return gatId;
1273:                }
1274:
1275:                public void setGatId(Integer gatId) {
1276:                    this .gatId = gatId;
1277:                }
1278:
1279:                public String getName() {
1280:                    return name;
1281:                }
1282:
1283:                public void setName(String name) {
1284:                    this .name = name;
1285:                }
1286:
1287:                public CollectionClassDummy getCollectionDummy() {
1288:                    return collectionDummy;
1289:                }
1290:
1291:                public void setCollectionDummy(
1292:                        CollectionClassDummy collectionDummy) {
1293:                    this .collectionDummy = collectionDummy;
1294:                }
1295:
1296:                public List getCollectiblesBase() {
1297:                    return collectiblesBase;
1298:                }
1299:
1300:                public void setCollectiblesBase(List collectiblesBase) {
1301:                    this .collectiblesBase = collectiblesBase;
1302:                }
1303:
1304:                public List getCollectiblesB() {
1305:                    return collectiblesB;
1306:                }
1307:
1308:                public void setCollectiblesB(List collectiblesB) {
1309:                    this .collectiblesB = collectiblesB;
1310:                }
1311:
1312:                public List getCollectiblesC() {
1313:                    return collectiblesC;
1314:                }
1315:
1316:                public void setCollectiblesC(List collectiblesC) {
1317:                    this .collectiblesC = collectiblesC;
1318:                }
1319:
1320:                public List getCollectiblesCC() {
1321:                    return collectiblesCC;
1322:                }
1323:
1324:                public void setCollectiblesCC(List collectiblesCC) {
1325:                    this .collectiblesCC = collectiblesCC;
1326:                }
1327:
1328:                public List getCollectiblesC2() {
1329:                    return collectiblesC2;
1330:                }
1331:
1332:                public void setCollectiblesC2(List collectiblesC2) {
1333:                    this .collectiblesC2 = collectiblesC2;
1334:                }
1335:
1336:                public List getCollectiblesD() {
1337:                    return collectiblesD;
1338:                }
1339:
1340:                public void setCollectiblesD(List collectiblesD) {
1341:                    this .collectiblesD = collectiblesD;
1342:                }
1343:
1344:                public List getCollectiblesDD() {
1345:                    return collectiblesDD;
1346:                }
1347:
1348:                public void setCollectiblesDD(List collectiblesDD) {
1349:                    this .collectiblesDD = collectiblesDD;
1350:                }
1351:
1352:                public String toString() {
1353:                    ToStringBuilder buf = new ToStringBuilder(this ,
1354:                            ToStringStyle.MULTI_LINE_STYLE);
1355:                    buf.append("gatId", gatId);
1356:                    buf.append("name", name);
1357:                    buf.append("collectiblesBase", collectiblesBase);
1358:                    buf.append("collectiblesB", collectiblesB);
1359:                    buf.append("collectiblesC", collectiblesC);
1360:                    buf.append("collectiblesD", collectiblesD);
1361:                    buf.append("collectiblesDD", collectiblesDD);
1362:                    return buf.toString();
1363:                }
1364:            }
1365:
1366:            public static interface CollectibleBaseIF extends Serializable {
1367:                Integer getColId();
1368:
1369:                void setColId(Integer colId);
1370:
1371:                String getName();
1372:
1373:                void setName(String name);
1374:
1375:                Integer getGathererId();
1376:
1377:                void setGathererId(Integer colId);
1378:
1379:                Gatherer getGatherer();
1380:
1381:                void setGatherer(Gatherer gatherer);
1382:            }
1383:
1384:            public static class CollectibleBase implements  CollectibleBaseIF {
1385:                private Integer colId;
1386:                private String name;
1387:                private Integer gathererId;
1388:                private Gatherer gatherer;
1389:
1390:                // protected String ojbConcreteClass;
1391:
1392:                public CollectibleBase() {
1393:                    // ojbConcreteClass = CollectibleBase.class.getName();
1394:                }
1395:
1396:                public CollectibleBase(String name) {
1397:                    // ojbConcreteClass = CollectibleBase.class.getName();
1398:                    this .name = name;
1399:                }
1400:
1401:                public String toString() {
1402:                    ToStringBuilder buf = new ToStringBuilder(this );
1403:                    buf.append("colId", colId);
1404:                    buf.append("name", name);
1405:                    buf.append("gathererId", gathererId);
1406:                    //            buf.append("ojbConcreteClass", ojbConcreteClass);
1407:                    return buf.toString();
1408:                }
1409:
1410:                public Gatherer getGatherer() {
1411:                    return gatherer;
1412:                }
1413:
1414:                public void setGatherer(Gatherer gatherer) {
1415:                    this .gatherer = gatherer;
1416:                }
1417:
1418:                public Integer getGathererId() {
1419:                    return gathererId;
1420:                }
1421:
1422:                public void setGathererId(Integer gathererId) {
1423:                    this .gathererId = gathererId;
1424:                }
1425:
1426:                public Integer getColId() {
1427:                    return colId;
1428:                }
1429:
1430:                public void setColId(Integer colId) {
1431:                    this .colId = colId;
1432:                }
1433:
1434:                public String getName() {
1435:                    return name;
1436:                }
1437:
1438:                public void setName(String name) {
1439:                    this .name = name;
1440:                }
1441:            }
1442:
1443:            public static interface CollectibleBIF extends CollectibleBaseIF {
1444:
1445:            }
1446:
1447:            public static class CollectibleB extends CollectibleBase implements 
1448:                    CollectibleBIF {
1449:                public CollectibleB() {
1450:                    //            ojbConcreteClass = CollectibleB.class.getName();
1451:                }
1452:
1453:                public CollectibleB(String name) {
1454:                    super (name);
1455:                    //            ojbConcreteClass = CollectibleB.class.getName();
1456:                }
1457:            }
1458:
1459:            public static interface CollectibleCIF extends CollectibleBIF {
1460:                String getExtentName();
1461:
1462:                void setExtentName(String extentName);
1463:            }
1464:
1465:            public static class CollectibleC extends CollectibleB implements 
1466:                    CollectibleCIF {
1467:                private String extentName;
1468:
1469:                public CollectibleC() {
1470:                    //            ojbConcreteClass = CollectibleC.class.getName();
1471:                }
1472:
1473:                public CollectibleC(String name) {
1474:                    super (name);
1475:                    //            ojbConcreteClass = CollectibleC.class.getName();
1476:                }
1477:
1478:                public CollectibleC(String name, String extentName) {
1479:                    super (name);
1480:                    //            ojbConcreteClass = CollectibleC.class.getName();
1481:                    this .extentName = extentName;
1482:                }
1483:
1484:                public String getExtentName() {
1485:                    return extentName;
1486:                }
1487:
1488:                public void setExtentName(String extentName) {
1489:                    this .extentName = extentName;
1490:                }
1491:            }
1492:
1493:            public static class CollectibleCC extends CollectibleC {
1494:                public CollectibleCC() {
1495:                }
1496:
1497:                public CollectibleCC(String name) {
1498:                    super (name);
1499:                }
1500:
1501:                public CollectibleCC(String name, String extentName) {
1502:                    super (name, extentName);
1503:                }
1504:            }
1505:
1506:            public static class CollectibleC2 extends CollectibleC {
1507:                public CollectibleC2() {
1508:                }
1509:
1510:                public CollectibleC2(String name) {
1511:                    super (name);
1512:                }
1513:
1514:                public CollectibleC2(String name, String extentName) {
1515:                    super (name, extentName);
1516:                }
1517:            }
1518:
1519:            public static interface CollectibleDIF extends CollectibleBaseIF {
1520:
1521:            }
1522:
1523:            public static class CollectibleD extends CollectibleBase implements 
1524:                    CollectibleDIF {
1525:                protected String ojbConcreteClass;
1526:
1527:                public CollectibleD() {
1528:                    ojbConcreteClass = CollectibleD.class.getName();
1529:                }
1530:
1531:                public CollectibleD(String name) {
1532:                    super (name);
1533:                    ojbConcreteClass = CollectibleD.class.getName();
1534:                }
1535:
1536:                public String getOjbConcreteClass() {
1537:                    return ojbConcreteClass;
1538:                }
1539:
1540:                public void setOjbConcreteClass(String ojbConcreteClass) {
1541:                    this .ojbConcreteClass = ojbConcreteClass;
1542:                }
1543:            }
1544:
1545:            public static interface CollectibleDDIF extends CollectibleBaseIF {
1546:
1547:            }
1548:
1549:            public static class CollectibleDD extends CollectibleBase implements 
1550:                    CollectibleDDIF {
1551:                protected String ojbConcreteClass;
1552:
1553:                public CollectibleDD() {
1554:                    ojbConcreteClass = CollectibleDD.class.getName();
1555:                }
1556:
1557:                public CollectibleDD(String name) {
1558:                    super (name);
1559:                    ojbConcreteClass = CollectibleDD.class.getName();
1560:                }
1561:
1562:                public String getOjbConcreteClass() {
1563:                    return ojbConcreteClass;
1564:                }
1565:
1566:                public void setOjbConcreteClass(String ojbConcreteClass) {
1567:                    this .ojbConcreteClass = ojbConcreteClass;
1568:                }
1569:            }
1570:
1571:            public static interface BookShelfIF {
1572:                public void addItem(BookShelfItem event);
1573:
1574:                public List getItems();
1575:
1576:                public Integer getPk();
1577:
1578:                public void setPk(Integer pk);
1579:
1580:                public String getName();
1581:
1582:                public void setName(String name);
1583:            }
1584:
1585:            public static class BookShelf implements  BookShelfIF {
1586:                private Integer pk;
1587:                private String name;
1588:                private List items;
1589:
1590:                public BookShelf() {
1591:                }
1592:
1593:                public BookShelf(String name) {
1594:                    this .name = name;
1595:                }
1596:
1597:                public void addItem(BookShelfItem event) {
1598:                    if (items == null)
1599:                        items = new ArrayList();
1600:
1601:                    items.add(event);
1602:                }
1603:
1604:                public void setItems(List items) {
1605:                    this .items = items;
1606:                }
1607:
1608:                public List getItems() {
1609:                    return items;
1610:                }
1611:
1612:                public Integer getPk() {
1613:                    return pk;
1614:                }
1615:
1616:                public void setPk(Integer pk) {
1617:                    this .pk = pk;
1618:                }
1619:
1620:                public String getName() {
1621:                    return name;
1622:                }
1623:
1624:                public void setName(String name) {
1625:                    this .name = name;
1626:                }
1627:            }
1628:
1629:            public static abstract class BookShelfItem {
1630:                private Integer pk;
1631:                private String name;
1632:                private BookShelfIF shelf;
1633:
1634:                public BookShelfItem() {
1635:                }
1636:
1637:                public BookShelfItem(BookShelfIF shelf) {
1638:                    this .shelf = shelf;
1639:                }
1640:
1641:                protected BookShelfItem(String name, BookShelfIF shelf) {
1642:                    this .name = name;
1643:                    this .shelf = shelf;
1644:                }
1645:
1646:                public Integer getPk() {
1647:                    return pk;
1648:                }
1649:
1650:                public void setPk(Integer pk) {
1651:                    this .pk = pk;
1652:                }
1653:
1654:                public String getName() {
1655:                    return name;
1656:                }
1657:
1658:                public void setName(String name) {
1659:                    this .name = name;
1660:                }
1661:
1662:                public BookShelfIF getShelf() {
1663:                    return shelf;
1664:                }
1665:
1666:                public void setShelf(BookShelf shelf) {
1667:                    this .shelf = shelf;
1668:                }
1669:            }
1670:
1671:            public static class DVD extends BookShelfItem {
1672:                public DVD() {
1673:                }
1674:
1675:                public DVD(BookShelf shelf) {
1676:                    super (shelf);
1677:                }
1678:
1679:                public DVD(String name, BookShelfIF shelf) {
1680:                    super (name, shelf);
1681:                }
1682:            }
1683:
1684:            public static class Book extends BookShelfItem {
1685:                public Book() {
1686:                }
1687:
1688:                public Book(BookShelfIF shelf) {
1689:                    super (shelf);
1690:                }
1691:
1692:                public Book(String name, BookShelfIF shelf) {
1693:                    super (name, shelf);
1694:                }
1695:            }
1696:
1697:            public static class Candie {
1698:                private Integer pk;
1699:                private String name;
1700:                private String ingredients;
1701:                private BookShelfIF shelf;
1702:
1703:                public Candie() {
1704:                }
1705:
1706:                public Candie(BookShelfIF shelf) {
1707:                    this .shelf = shelf;
1708:                }
1709:
1710:                protected Candie(String name, BookShelfIF shelf) {
1711:                    this .name = name;
1712:                    this .shelf = shelf;
1713:                }
1714:
1715:                public Integer getPk() {
1716:                    return pk;
1717:                }
1718:
1719:                public void setPk(Integer pk) {
1720:                    this .pk = pk;
1721:                }
1722:
1723:                public String getName() {
1724:                    return name;
1725:                }
1726:
1727:                public void setName(String name) {
1728:                    this .name = name;
1729:                }
1730:
1731:                public String getIngredients() {
1732:                    return ingredients;
1733:                }
1734:
1735:                public void setIngredients(String ingredients) {
1736:                    this .ingredients = ingredients;
1737:                }
1738:
1739:                public BookShelfIF getShelf() {
1740:                    return shelf;
1741:                }
1742:
1743:                public void setShelf(BookShelf shelf) {
1744:                    this.shelf = shelf;
1745:                }
1746:            }
1747:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.