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


0001:        package org.apache.ojb.broker.sequence;
0002:
0003:        import java.io.Serializable;
0004:        import java.sql.Connection;
0005:        import java.sql.SQLException;
0006:        import java.sql.Statement;
0007:        import java.util.ArrayList;
0008:        import java.util.Collection;
0009:        import java.util.Iterator;
0010:        import java.util.List;
0011:
0012:        import org.apache.commons.lang.builder.ToStringBuilder;
0013:        import org.apache.ojb.broker.Identity;
0014:        import org.apache.ojb.broker.PersistenceBroker;
0015:        import org.apache.ojb.broker.PersistenceBrokerFactory;
0016:        import org.apache.ojb.broker.TestHelper;
0017:        import org.apache.ojb.broker.metadata.MetadataManager;
0018:        import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
0019:        import org.apache.ojb.broker.metadata.SequenceDescriptor;
0020:        import org.apache.ojb.broker.platforms.Platform;
0021:        import org.apache.ojb.broker.platforms.PlatformHsqldbImpl;
0022:        import org.apache.ojb.broker.platforms.PlatformMySQLImpl;
0023:        import org.apache.ojb.broker.query.Criteria;
0024:        import org.apache.ojb.broker.query.QueryByCriteria;
0025:        import org.apache.ojb.broker.query.QueryFactory;
0026:        import org.apache.ojb.broker.util.sequence.SequenceManagerNativeImpl;
0027:        import org.apache.ojb.junit.PBTestCase;
0028:        import org.apache.ojb.odmg.OJB;
0029:        import org.apache.ojb.odmg.TransactionExt;
0030:        import org.odmg.Database;
0031:        import org.odmg.Implementation;
0032:        import org.odmg.Transaction;
0033:
0034:        /**
0035:         * Test case for {@link SequenceManagerNativeImpl}. These test check
0036:         * support for native identity columns. Test case only works for
0037:         * Hsql and Mysql.
0038:         *
0039:         * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
0040:         * @version $Id: NativeIdentifierTest.java,v 1.10.2.8 2005/10/06 21:01:44 arminw Exp $
0041:         */
0042:        public class NativeIdentifierTest extends PBTestCase {
0043:            // Statements for MainObject table
0044:            private static final String DROP = "DROP TABLE NATIVE_MAIN_OBJECT";
0045:            private static final String CREATE_MYSQL = "CREATE TABLE NATIVE_MAIN_OBJECT(NATIVE_ID int(11) NOT NULL PRIMARY KEY"
0046:                    + " auto_increment,REF_ID int(11),NAME VARCHAR(250))";
0047:            private static final String CREATE_HSQL = "CREATE TABLE NATIVE_MAIN_OBJECT(NATIVE_ID IDENTITY NOT NULL PRIMARY KEY,"
0048:                    + " REF_ID int,NAME VARCHAR(250))";
0049:
0050:            private static final String INSERT_DUMMY = "INSERT INTO NATIVE_MAIN_OBJECT (NAME) VALUES ('Dummy_1')";
0051:
0052:            private static final String ADD_CONSTRAINT = "ALTER TABLE NATIVE_MAIN_OBJECT"
0053:                    + " ADD CONSTRAINT MAIN_REF_FK"
0054:                    + " FOREIGN KEY (REF_ID) REFERENCES NATIVE_REFERENCE_OBJECT (NATIVE_ID)";
0055:            private static final String DROP_CONSTRAINT_HSQL = "ALTER TABLE NATIVE_MAIN_OBJECT"
0056:                    + " DROP CONSTRAINT MAIN_REF_FK";
0057:            private static final String DROP_CONSTRAINT_MYSQL = "ALTER TABLE NATIVE_MAIN_OBJECT"
0058:                    + " DROP FOREIGN KEY MAIN_REF_FK";
0059:
0060:            // Statements for NATIVE_REF_TEST table
0061:            private static final String CREATE_REF_MYSQL = "CREATE TABLE NATIVE_REFERENCE_OBJECT (NATIVE_ID int(11) NOT NULL PRIMARY KEY auto_increment,"
0062:                    + " NAME VARCHAR(250), OJB_CONCRETE_CLASS VARCHAR(250), FK_ID int, REF_ID int(11), SINGLE_REF_FK BIGINT"
0063:                    + " , FOREIGN KEY (FK_ID) REFERENCES NATIVE_MAIN_OBJECT (NATIVE_ID) )";
0064:            private static final String CREATE_REF_HSQL = "CREATE TABLE NATIVE_REFERENCE_OBJECT (NATIVE_ID IDENTITY NOT NULL PRIMARY KEY,"
0065:                    + " NAME VARCHAR(250), OJB_CONCRETE_CLASS VARCHAR(250), FK_ID int, REF_ID int, SINGLE_REF_FK BIGINT"
0066:                    + " , FOREIGN KEY (FK_ID) REFERENCES NATIVE_MAIN_OBJECT (NATIVE_ID) )";
0067:            private static final String DROP_REF = "DROP TABLE NATIVE_REFERENCE_OBJECT";
0068:            private static final String INSERT_DUMMY_REF = "INSERT INTO NATIVE_REFERENCE_OBJECT (NAME) VALUES ('Dummy_2')";
0069:
0070:            private Platform platform;
0071:            private Class oldSequenceManager;
0072:
0073:            public NativeIdentifierTest(String s) {
0074:                super (s);
0075:            }
0076:
0077:            public static void main(String[] args) {
0078:                String[] arr = { NativeIdentifierTest.class.getName() };
0079:                junit.textui.TestRunner.main(arr);
0080:            }
0081:
0082:            private boolean skipTest() throws Exception {
0083:                return !((platform instanceof  PlatformMySQLImpl) || (platform instanceof  PlatformHsqldbImpl));
0084:            }
0085:
0086:            public void setUp() throws Exception {
0087:                super .setUp();
0088:
0089:                platform = broker.serviceConnectionManager()
0090:                        .getSupportedPlatform();
0091:                if (skipTest())
0092:                    return;
0093:
0094:                Connection con;
0095:                Statement stmt;
0096:
0097:                PersistenceBroker pb = PersistenceBrokerFactory
0098:                        .defaultPersistenceBroker();
0099:                try {
0100:                    con = pb.serviceConnectionManager().getConnection();
0101:                    stmt = con.createStatement();
0102:                    try {
0103:                        if (platform instanceof  PlatformMySQLImpl) {
0104:                            stmt.execute(DROP_CONSTRAINT_MYSQL);
0105:                        } else if (platform instanceof  PlatformHsqldbImpl) {
0106:                            stmt.execute(DROP_CONSTRAINT_HSQL);
0107:                        }
0108:                    } catch (SQLException e) {
0109:                    }
0110:                    stmt.close();
0111:
0112:                    stmt = con.createStatement();
0113:                    try {
0114:                        stmt.execute(DROP_REF);
0115:                    } catch (SQLException e) {
0116:                    }
0117:                    stmt.close();
0118:
0119:                    stmt = con.createStatement();
0120:                    try {
0121:                        stmt.execute(DROP);
0122:                    } catch (SQLException e) {
0123:                    }
0124:                    stmt.close();
0125:                } finally {
0126:                    if (pb != null)
0127:                        pb.close();
0128:                }
0129:
0130:                try {
0131:                    con = broker.serviceConnectionManager().getConnection();
0132:                    if (platform instanceof  PlatformMySQLImpl) {
0133:                        stmt = con.createStatement();
0134:                        stmt.execute(CREATE_MYSQL);
0135:                        stmt.close();
0136:                    }
0137:                    if (platform instanceof  PlatformHsqldbImpl) {
0138:                        stmt = con.createStatement();
0139:                        stmt.execute(CREATE_HSQL);
0140:                        stmt.close();
0141:                    }
0142:
0143:                    stmt = con.createStatement();
0144:                    stmt.execute(INSERT_DUMMY);
0145:                    stmt.close();
0146:
0147:                    if (platform instanceof  PlatformMySQLImpl) {
0148:                        stmt = con.createStatement();
0149:                        stmt.execute(CREATE_REF_MYSQL);
0150:                        stmt.close();
0151:                    }
0152:                    if (platform instanceof  PlatformHsqldbImpl) {
0153:                        stmt = con.createStatement();
0154:                        stmt.execute(CREATE_REF_HSQL);
0155:                        stmt.close();
0156:                    }
0157:
0158:                    stmt = con.createStatement();
0159:                    stmt.execute(ADD_CONSTRAINT);
0160:                    stmt.close();
0161:
0162:                    stmt = con.createStatement();
0163:                    stmt.execute(INSERT_DUMMY_REF);
0164:                    stmt.close();
0165:
0166:                    SequenceDescriptor sd = MetadataManager.getInstance()
0167:                            .connectionRepository().getDescriptor(
0168:                                    broker.getPBKey()).getSequenceDescriptor();
0169:                    oldSequenceManager = sd.getSequenceManagerClass();
0170:                    sd.setSequenceManagerClass(SequenceManagerNativeImpl.class);
0171:                } catch (SQLException ex) {
0172:                    ex.printStackTrace();
0173:                } finally {
0174:                    if (broker != null)
0175:                        broker.close();
0176:                }
0177:
0178:                PersistenceBrokerFactory.releaseAllInstances();
0179:                broker = PersistenceBrokerFactory.defaultPersistenceBroker();
0180:                SequenceDescriptor sd = MetadataManager.getInstance()
0181:                        .connectionRepository()
0182:                        .getDescriptor(broker.getPBKey())
0183:                        .getSequenceDescriptor();
0184:                assertEquals(SequenceManagerNativeImpl.class, sd
0185:                        .getSequenceManagerClass());
0186:            }
0187:
0188:            public void tearDown() throws Exception {
0189:                super .tearDown();
0190:                if (skipTest())
0191:                    return;
0192:
0193:                Connection con;
0194:                Statement stmt;
0195:                PersistenceBroker pb = PersistenceBrokerFactory
0196:                        .defaultPersistenceBroker();
0197:                try {
0198:                    con = pb.serviceConnectionManager().getConnection();
0199:
0200:                    stmt = con.createStatement();
0201:                    if (platform instanceof  PlatformMySQLImpl) {
0202:                        stmt.execute(DROP_CONSTRAINT_MYSQL);
0203:                    } else if (platform instanceof  PlatformHsqldbImpl) {
0204:                        stmt.execute(DROP_CONSTRAINT_HSQL);
0205:                    }
0206:                    stmt.close();
0207:
0208:                    stmt = con.createStatement();
0209:                    stmt.execute(DROP_REF);
0210:                    stmt.close();
0211:
0212:                    stmt = con.createStatement();
0213:                    stmt.execute(DROP);
0214:                    stmt.close();
0215:
0216:                    SequenceDescriptor sd = MetadataManager.getInstance()
0217:                            .connectionRepository()
0218:                            .getDescriptor(pb.getPBKey())
0219:                            .getSequenceDescriptor();
0220:                    sd.setSequenceManagerClass(oldSequenceManager);
0221:                } catch (SQLException ex) {
0222:                    ex.printStackTrace();
0223:                } finally {
0224:                    if (pb != null) {
0225:                        pb.clearCache();
0226:                        pb.close();
0227:                    }
0228:                }
0229:
0230:                PersistenceBrokerFactory.releaseAllInstances();
0231:                broker = PersistenceBrokerFactory.defaultPersistenceBroker();
0232:                SequenceDescriptor sd = MetadataManager.getInstance()
0233:                        .connectionRepository()
0234:                        .getDescriptor(broker.getPBKey())
0235:                        .getSequenceDescriptor();
0236:                assertEquals(oldSequenceManager, sd.getSequenceManagerClass());
0237:                broker.close();
0238:            }
0239:
0240:            public void testSimpleInsert_1() throws Exception {
0241:                // prepare for PB-api test
0242:                ojbChangeReferenceSetting(MainObject.class, "singleReference",
0243:                        true, true, true, false);
0244:                ojbChangeReferenceSetting(MainObject.class, "allReferences",
0245:                        true, true, true, false);
0246:                ojbChangeReferenceSetting(CollectionReference.class,
0247:                        "singleReference", true, true, true, false);
0248:                ojbChangeReferenceSetting(SingleReference.class, "mainObject",
0249:                        true, true, true, false);
0250:                doTtestSimpleInsert();
0251:            }
0252:
0253:            public void testSimpleInsert_2() throws Exception {
0254:                // prepare for PB-api test
0255:                ojbChangeReferenceSetting(MainObject.class, "singleReference",
0256:                        true, true, true, true);
0257:                ojbChangeReferenceSetting(MainObject.class, "allReferences",
0258:                        true, true, true, true);
0259:                ojbChangeReferenceSetting(CollectionReference.class,
0260:                        "singleReference", true, true, true, true);
0261:                ojbChangeReferenceSetting(SingleReference.class, "mainObject",
0262:                        true, true, true, true);
0263:                doTtestSimpleInsert();
0264:            }
0265:
0266:            public void doTtestSimpleInsert() throws Exception {
0267:                if (skipTest())
0268:                    return;
0269:
0270:                long timestamp = System.currentTimeMillis();
0271:                String name = "testSimpleInsert_" + timestamp;
0272:
0273:                MainObject obj_1 = new MainObject(null, name);
0274:                MainObject obj_2 = new MainObject(null, name);
0275:                MainObject obj_3 = new MainObject(null, name);
0276:
0277:                broker.beginTransaction();
0278:                broker.store(obj_1);
0279:                //        System.out.println("obj_1: "+obj_1);
0280:                broker.store(obj_2);
0281:                //        System.out.println("obj_2: "+obj_2);
0282:                broker.store(obj_3);
0283:                //        System.out.println("obj_3: "+obj_3);
0284:                broker.commitTransaction();
0285:
0286:                Criteria crit = new Criteria();
0287:                crit.addEqualTo("name", name);
0288:                QueryByCriteria query = QueryFactory.newQuery(MainObject.class,
0289:                        crit);
0290:                int result = broker.getCount(query);
0291:                assertEquals("Not all objects created", 3, result);
0292:                assertNotNull(obj_1.getIdentifier());
0293:                assertTrue(obj_1.getIdentifier().longValue() > 0);
0294:                assertTrue(obj_3.getIdentifier().longValue() > 0);
0295:            }
0296:
0297:            public void testSimpleInsertODMG_1() throws Exception {
0298:                int none = ObjectReferenceDescriptor.CASCADE_NONE;
0299:                ojbChangeReferenceSetting(MainObject.class, "singleReference",
0300:                        true, none, none, false);
0301:                ojbChangeReferenceSetting(MainObject.class, "allReferences",
0302:                        true, none, none, false);
0303:                ojbChangeReferenceSetting(CollectionReference.class,
0304:                        "singleReference", true, none, none, false);
0305:                ojbChangeReferenceSetting(SingleReference.class, "mainObject",
0306:                        true, none, none, false);
0307:            }
0308:
0309:            public void testSimpleInsertODMG_2() throws Exception {
0310:                int none = ObjectReferenceDescriptor.CASCADE_NONE;
0311:                ojbChangeReferenceSetting(MainObject.class, "singleReference",
0312:                        true, none, none, true);
0313:                ojbChangeReferenceSetting(MainObject.class, "allReferences",
0314:                        true, none, none, true);
0315:                ojbChangeReferenceSetting(CollectionReference.class,
0316:                        "singleReference", true, none, none, true);
0317:                ojbChangeReferenceSetting(SingleReference.class, "mainObject",
0318:                        true, none, none, true);
0319:            }
0320:
0321:            public void doTestSimpleInsertODMG() throws Exception {
0322:                if (skipTest())
0323:                    return;
0324:
0325:                long timestamp = System.currentTimeMillis();
0326:                String name = "testSimpleInsert_" + timestamp;
0327:
0328:                MainObject obj_1 = new MainObject(null, name);
0329:                MainObject obj_2 = new MainObject(null, name);
0330:                MainObject obj_3 = new MainObject(null, name);
0331:
0332:                Implementation odmg = OJB.getInstance();
0333:                Database db = odmg.newDatabase();
0334:                db.open(TestHelper.DEF_DATABASE_NAME, Database.OPEN_READ_WRITE);
0335:
0336:                Transaction tx = odmg.newTransaction();
0337:                tx.begin();
0338:                tx.lock(obj_1, Transaction.WRITE);
0339:                tx.lock(obj_2, Transaction.WRITE);
0340:                tx.lock(obj_3, Transaction.WRITE);
0341:                tx.commit();
0342:
0343:                Criteria crit = new Criteria();
0344:                crit.addEqualTo("name", name);
0345:                QueryByCriteria query = QueryFactory.newQuery(MainObject.class,
0346:                        crit);
0347:                int result = broker.getCount(query);
0348:                assertEquals("Not all objects created", 3, result);
0349:                assertNotNull(obj_1.getIdentifier());
0350:                assertTrue(obj_1.getIdentifier().longValue() > 0);
0351:                assertTrue(obj_3.getIdentifier().longValue() > 0);
0352:            }
0353:
0354:            public void testReferenceInsertUpdate_1() throws Exception {
0355:                // prepare for PB-api test
0356:                ojbChangeReferenceSetting(MainObject.class, "singleReference",
0357:                        true, true, true, false);
0358:                ojbChangeReferenceSetting(MainObject.class, "allReferences",
0359:                        true, true, true, false);
0360:                ojbChangeReferenceSetting(CollectionReference.class,
0361:                        "singleReference", true, true, true, false);
0362:                ojbChangeReferenceSetting(SingleReference.class, "mainObject",
0363:                        true, true, true, false);
0364:                doTestReferenceInsertUpdate();
0365:            }
0366:
0367:            public void testReferenceInsertUpdate_2() throws Exception {
0368:                // prepare for PB-api test
0369:                ojbChangeReferenceSetting(MainObject.class, "singleReference",
0370:                        true, true, true, true);
0371:                ojbChangeReferenceSetting(MainObject.class, "allReferences",
0372:                        true, true, true, true);
0373:                ojbChangeReferenceSetting(CollectionReference.class,
0374:                        "singleReference", true, true, true, true);
0375:                ojbChangeReferenceSetting(SingleReference.class, "mainObject",
0376:                        true, true, true, true);
0377:                doTestReferenceInsertUpdate();
0378:            }
0379:
0380:            public void doTestReferenceInsertUpdate() throws Exception {
0381:                if (skipTest())
0382:                    return;
0383:                long timestamp = System.currentTimeMillis();
0384:                String name = "testReferenceInsert_main_" + timestamp;
0385:                String nameRef = "testReferenceInsert_reference_" + timestamp;
0386:                String nameSingleRef = "testReferenceInsert_single_reference_"
0387:                        + timestamp;
0388:
0389:                MainObject obj_1 = new MainObject(null, name);
0390:                MainObject obj_2 = new MainObject(null, name);
0391:
0392:                SingleReference s_ref_1 = new SingleReference(nameSingleRef);
0393:                SingleReference s_ref_2 = new SingleReference(nameSingleRef);
0394:
0395:                CollectionReference ref_1 = new CollectionReference(null,
0396:                        nameRef);
0397:                CollectionReference ref_2 = new CollectionReference(null,
0398:                        nameRef);
0399:                CollectionReference ref_3 = new CollectionReference(null,
0400:                        nameRef);
0401:                CollectionReference ref_4 = new CollectionReference(null,
0402:                        nameRef);
0403:                ref_1.setSingleReference(s_ref_1);
0404:                ref_4.setSingleReference(s_ref_2);
0405:
0406:                SingleReference s_ref_3 = new SingleReference(nameSingleRef);
0407:                SingleReference s_ref_4 = new SingleReference(nameSingleRef);
0408:
0409:                obj_1.addReference(ref_1);
0410:                obj_1.addReference(ref_2);
0411:                obj_1.addReference(ref_3);
0412:                obj_1.addReference(ref_4);
0413:
0414:                obj_1.setSingleReference(s_ref_3);
0415:                s_ref_3.setMainObject(obj_1);
0416:                obj_2.setSingleReference(s_ref_4);
0417:                s_ref_3.setMainObject(obj_1);
0418:
0419:                broker.beginTransaction();
0420:                // first store a reference
0421:                broker.store(ref_1);
0422:                //        System.out.println("ref_1: "+ref_1);
0423:                // then store main object with other references
0424:                broker.store(obj_1);
0425:                //        System.out.println("obj_1: "+obj_1);
0426:                // store second object without references
0427:                broker.store(obj_2);
0428:                //        System.out.println("obj_2: "+obj_2);
0429:                broker.commitTransaction();
0430:
0431:                // try to find both objects
0432:                Criteria crit = new Criteria();
0433:                crit.addEqualTo("name", name);
0434:                QueryByCriteria query = QueryFactory.newQuery(MainObject.class,
0435:                        crit);
0436:                int result = broker.getCount(query);
0437:                assertEquals("Wrong object count", 2, result);
0438:
0439:                // pk have to set and have to be different
0440:                assertNotNull(obj_1.getIdentifier());
0441:                assertNotNull(obj_2.getIdentifier());
0442:                assertNotSame(obj_1.getIdentifier(), obj_2.getIdentifier());
0443:                assertTrue(obj_1.getIdentifier().longValue() > 0);
0444:                assertTrue(obj_2.getIdentifier().longValue() > 0);
0445:                assertTrue(s_ref_3.getId().longValue() > 0);
0446:                assertTrue(ref_3.getRefIdentifier().longValue() > 0);
0447:
0448:                // get Identity objects
0449:                Identity oid_1 = new Identity(obj_1, broker);
0450:                Identity oid_2 = new Identity(obj_2, broker);
0451:                // get identifier (PK) values
0452:                Long id_1 = obj_1.getIdentifier();
0453:                Long id_2 = obj_2.getIdentifier();
0454:
0455:                broker.clearCache();
0456:
0457:                // get object with references
0458:                obj_1 = (MainObject) broker.getObjectByIdentity(oid_1);
0459:                assertNotNull(obj_1);
0460:                List references = obj_1.getAllReferences();
0461:                assertNotNull(references);
0462:                assertEquals("4 references expected for object: " + obj_1, 4,
0463:                        references.size());
0464:                Iterator it = references.iterator();
0465:                while (it.hasNext()) {
0466:                    CollectionReference ref = (CollectionReference) it.next();
0467:                    assertEquals("Main object fk expected", obj_1
0468:                            .getIdentifier(), ref.fkIdentifier);
0469:                    assertTrue(
0470:                            "We expect a positive value, identity columns have to start > 0",
0471:                            (ref.getRefIdentifier().longValue() > 0));
0472:                }
0473:                assertNotNull(obj_1.getSingleReference());
0474:                obj_2 = (MainObject) broker.getObjectByIdentity(oid_2);
0475:                assertTrue(obj_1.getIdentifier().longValue() > 0);
0476:                assertTrue(obj_2.getIdentifier().longValue() > 0);
0477:                assertNotNull(obj_2.getSingleReference());
0478:                assertTrue(obj_2.getSingleReference().getId().longValue() > 0);
0479:                assertTrue(obj_1.getSingleReference().getId().longValue() > 0);
0480:                assertNotSame(obj_1.getSingleReference(), obj_2
0481:                        .getSingleReference());
0482:                broker.clearCache();
0483:
0484:                // get references only
0485:                Criteria crit_2 = new Criteria();
0486:                crit_2.addEqualTo("refName", nameRef);
0487:                QueryByCriteria query_2 = QueryFactory.newQuery(
0488:                        CollectionReference.class, crit_2);
0489:                int result_2 = broker.getCount(query_2);
0490:                assertEquals("Not all objects created", 4, result_2);
0491:                assertNotNull(ref_3.getRefIdentifier());
0492:
0493:                broker.clearCache();
0494:
0495:                // get second object
0496:                MainObject retObj = (MainObject) broker
0497:                        .getObjectByIdentity(oid_2);
0498:                List refList = retObj.getAllReferences();
0499:                assertNotNull(refList);
0500:                assertEquals("object do not have references", 0, refList.size());
0501:
0502:                // add new reference to object
0503:                CollectionReference ref_5 = new CollectionReference(null,
0504:                        nameRef);
0505:                CollectionReference ref_6 = new CollectionReference(null,
0506:                        nameRef);
0507:                obj_1.addReference(ref_5);
0508:                obj_2.addReference(ref_6);
0509:                broker.beginTransaction();
0510:                broker.store(obj_1);
0511:                broker.store(obj_2);
0512:                broker.commitTransaction();
0513:                assertNotNull(ref_5.getRefIdentifier());
0514:                assertNotNull(ref_6.getRefIdentifier());
0515:                assertEquals(id_1, obj_1.getIdentifier());
0516:                assertEquals(id_2, obj_2.getIdentifier());
0517:
0518:                obj_1 = (MainObject) broker.getObjectByIdentity(oid_1);
0519:                assertNotNull(obj_1);
0520:                references = obj_1.getAllReferences();
0521:                assertNotNull(references);
0522:                assertEquals("5 references expected for object: " + obj_1, 5,
0523:                        references.size());
0524:
0525:                obj_2 = (MainObject) broker.getObjectByIdentity(oid_2);
0526:                assertNotNull(obj_2);
0527:                references = obj_2.getAllReferences();
0528:                assertNotNull(references);
0529:                assertEquals("1 references expected for object: " + obj_2, 1,
0530:                        references.size());
0531:
0532:                assertEquals(id_1, obj_1.getIdentifier());
0533:                assertEquals(id_2, obj_2.getIdentifier());
0534:
0535:                // now update main objects
0536:                obj_1.setName(name + "_update");
0537:                obj_2.setName(name + "_update");
0538:                broker.beginTransaction();
0539:                broker.store(obj_1);
0540:                broker.store(obj_2);
0541:                broker.commitTransaction();
0542:
0543:                obj_1 = (MainObject) broker.getObjectByIdentity(oid_1);
0544:                obj_2 = (MainObject) broker.getObjectByIdentity(oid_2);
0545:
0546:                assertNotNull(obj_1);
0547:                assertNotNull(obj_2);
0548:                assertEquals(obj_1.getName(), name + "_update");
0549:                assertEquals(obj_2.getName(), name + "_update");
0550:                assertEquals(id_1, obj_1.getIdentifier());
0551:                assertEquals(id_2, obj_2.getIdentifier());
0552:
0553:                // now update reference
0554:                obj_2 = (MainObject) broker.getObjectByIdentity(oid_2);
0555:                assertNotNull(obj_2);
0556:                references = obj_2.getAllReferences();
0557:                CollectionReference ref = (CollectionReference) references
0558:                        .get(0);
0559:                ref.setRefName(nameRef + "_update");
0560:                broker.beginTransaction();
0561:                broker.store(obj_2);
0562:                broker.commitTransaction();
0563:                obj_2 = (MainObject) broker.getObjectByIdentity(oid_2);
0564:                assertNotNull(obj_2);
0565:                references = obj_2.getAllReferences();
0566:                ref = (CollectionReference) references.get(0);
0567:                assertEquals(nameRef + "_update", ref.getRefName());
0568:                assertEquals(id_1, obj_1.getIdentifier());
0569:                assertEquals(id_2, obj_2.getIdentifier());
0570:            }
0571:
0572:            /**
0573:             * critical test case, because single broker instance (PB-api) is concurrent used
0574:             * with the ODMG-api, take care of caches
0575:             */
0576:            public void testReferenceInsertUpdateODMG_1() throws Exception {
0577:                if (skipTest())
0578:                    return;
0579:
0580:                // prepare metadata for odmg-api
0581:                int none = ObjectReferenceDescriptor.CASCADE_NONE;
0582:                ojbChangeReferenceSetting(MainObject.class, "singleReference",
0583:                        true, none, none, false);
0584:                ojbChangeReferenceSetting(MainObject.class, "allReferences",
0585:                        true, none, none, false);
0586:                ojbChangeReferenceSetting(CollectionReference.class,
0587:                        "singleReference", true, none, none, false);
0588:                ojbChangeReferenceSetting(SingleReference.class, "mainObject",
0589:                        true, none, none, false);
0590:
0591:                long timestamp = System.currentTimeMillis();
0592:                String name = "testReferenceInsert_main_" + timestamp;
0593:                String nameRef = "testReferenceInsert_reference_" + timestamp;
0594:                String nameSingleRef = "testReferenceInsert_single_reference_"
0595:                        + timestamp;
0596:
0597:                MainObject obj_2 = new MainObject(null, name);
0598:                SingleReference s_ref_4 = new SingleReference(nameSingleRef);
0599:                obj_2.setSingleReference(s_ref_4);
0600:
0601:                Implementation odmg = OJB.getInstance();
0602:                Database db = odmg.newDatabase();
0603:                db.open(TestHelper.DEF_DATABASE_NAME, Database.OPEN_READ_WRITE);
0604:
0605:                TransactionExt tx = (TransactionExt) odmg.newTransaction();
0606:                tx.begin();
0607:                db.makePersistent(s_ref_4);
0608:                db.makePersistent(obj_2);
0609:                tx.commit();
0610:
0611:                tx.begin();
0612:
0613:                // try to find object
0614:                Criteria crit = new Criteria();
0615:                crit.addEqualTo("name", name);
0616:                QueryByCriteria query = QueryFactory.newQuery(MainObject.class,
0617:                        crit);
0618:
0619:                int result = tx.getBroker().getCount(query);
0620:                assertEquals("Wrong object count", 1, result);
0621:                // pk have to set and have to be different
0622:                assertNotNull(obj_2.getIdentifier());
0623:                assertTrue(obj_2.getIdentifier().longValue() > 0);
0624:                // no collection reference set
0625:                List references = obj_2.getAllReferences();
0626:                assertTrue(references == null || references.size() == 0);
0627:                // get Identity objects
0628:                Identity oid_2 = tx.getBroker().serviceIdentity()
0629:                        .buildIdentity(obj_2);
0630:                // get identifier (PK) values
0631:                Long id_2 = obj_2.getIdentifier();
0632:
0633:                tx.getBroker().clearCache();
0634:                obj_2 = (MainObject) tx.getBroker().getObjectByIdentity(oid_2);
0635:
0636:                assertTrue(obj_2.getIdentifier().longValue() > 0);
0637:                assertNotNull(obj_2.getSingleReference());
0638:                assertTrue(obj_2.getSingleReference().getId().longValue() > 0);
0639:                // no collection reference set
0640:                references = obj_2.getAllReferences();
0641:                assertTrue(references == null || references.size() == 0);
0642:
0643:                tx.getBroker().clearCache();
0644:                // get references only
0645:                Criteria crit_2 = new Criteria();
0646:                crit_2.addEqualTo("refName", nameRef);
0647:                QueryByCriteria query_2 = QueryFactory.newQuery(
0648:                        CollectionReference.class, crit_2);
0649:                int result_2 = tx.getBroker().getCount(query_2);
0650:
0651:                assertEquals(0, result_2);
0652:
0653:                tx.getBroker().clearCache();
0654:                // get object
0655:                MainObject retObj = (MainObject) tx.getBroker()
0656:                        .getObjectByIdentity(oid_2);
0657:
0658:                List refList = retObj.getAllReferences();
0659:                assertNotNull(refList);
0660:                assertEquals("object do not have references", 0, refList.size());
0661:                tx.commit();
0662:
0663:                // add new reference to object
0664:                CollectionReference ref_6 = new CollectionReference(null,
0665:                        "###_new_" + nameRef);
0666:                tx.begin();
0667:                tx.lock(obj_2, Transaction.WRITE);
0668:                obj_2.addReference(ref_6);
0669:                tx.commit();
0670:
0671:                references = obj_2.getAllReferences();
0672:                assertNotNull(references);
0673:                assertEquals("1 references expected for object: " + obj_2, 1,
0674:                        references.size());
0675:
0676:                assertNotNull(ref_6.getRefIdentifier());
0677:                // check FK setting
0678:                Long fk = ref_6.getFkIdentifier();
0679:                assertNotNull(fk);
0680:                assertEquals(obj_2.getIdentifier(), fk);
0681:                assertEquals(id_2, obj_2.getIdentifier());
0682:                references = obj_2.getAllReferences();
0683:                assertNotNull(references);
0684:                assertEquals("1 references expected for object: " + obj_2, 1,
0685:                        references.size());
0686:                assertNotNull(references);
0687:
0688:                tx.begin();
0689:                obj_2 = (MainObject) tx.getBroker().getObjectByIdentity(oid_2);
0690:                // we don't change the main object, only add an reference, so the
0691:                // cached version of the object isn't up to date
0692:                tx.getBroker().retrieveAllReferences(obj_2);
0693:                tx.commit();
0694:
0695:                assertNotNull(obj_2);
0696:                references = obj_2.getAllReferences();
0697:                assertNotNull(references);
0698:                assertEquals("Reference expected for object", 1, references
0699:                        .size());
0700:
0701:                assertEquals(id_2, obj_2.getIdentifier());
0702:
0703:                // now update main objects
0704:                tx.begin();
0705:                tx.lock(obj_2, Transaction.WRITE);
0706:                obj_2.setName(name + "_update");
0707:                tx.commit();
0708:
0709:                broker = PersistenceBrokerFactory.defaultPersistenceBroker();
0710:                obj_2 = (MainObject) broker.getObjectByIdentity(oid_2);
0711:                broker.close();
0712:
0713:                assertNotNull(obj_2);
0714:                assertEquals(obj_2.getName(), name + "_update");
0715:                assertEquals(id_2, obj_2.getIdentifier());
0716:
0717:                broker = PersistenceBrokerFactory.defaultPersistenceBroker();
0718:                obj_2 = (MainObject) broker.getObjectByIdentity(oid_2);
0719:                broker.close();
0720:
0721:                // now update reference
0722:                assertNotNull(obj_2);
0723:                tx.begin();
0724:                tx.lock(obj_2, Transaction.WRITE);
0725:                references = obj_2.getAllReferences();
0726:                CollectionReference ref = (CollectionReference) references
0727:                        .get(0);
0728:                tx.lock(ref, Transaction.WRITE);
0729:                ref.setRefName(nameRef + "_update");
0730:                tx.commit();
0731:
0732:                broker = PersistenceBrokerFactory.defaultPersistenceBroker();
0733:                obj_2 = (MainObject) broker.getObjectByIdentity(oid_2);
0734:                assertNotNull(obj_2);
0735:                references = obj_2.getAllReferences();
0736:                ref = (CollectionReference) references.get(0);
0737:                assertEquals(nameRef + "_update", ref.getRefName());
0738:                assertEquals(id_2, obj_2.getIdentifier());
0739:            }
0740:
0741:            /**
0742:             * critical test case, because single broker instance (PB-api) is concurrent used
0743:             * with the ODMG-api, take care of caches
0744:             */
0745:            public void testReferenceInsertUpdateODMG_2() throws Exception {
0746:                if (skipTest())
0747:                    return;
0748:
0749:                // prepare metadata for odmg-api
0750:                int none = ObjectReferenceDescriptor.CASCADE_NONE;
0751:                ojbChangeReferenceSetting(MainObject.class, "singleReference",
0752:                        true, none, none, false);
0753:                ojbChangeReferenceSetting(MainObject.class, "allReferences",
0754:                        true, none, none, false);
0755:                ojbChangeReferenceSetting(CollectionReference.class,
0756:                        "singleReference", true, none, none, false);
0757:                ojbChangeReferenceSetting(SingleReference.class, "mainObject",
0758:                        true, none, none, false);
0759:
0760:                long timestamp = System.currentTimeMillis();
0761:                String name = "testReferenceInsert_main_" + timestamp;
0762:                String nameRef = "testReferenceInsert_reference_" + timestamp;
0763:                String nameSingleRef = "testReferenceInsert_single_reference_"
0764:                        + timestamp;
0765:
0766:                MainObject obj_1 = new MainObject(null, name);
0767:                MainObject obj_2 = new MainObject(null, name);
0768:
0769:                SingleReference s_ref_1 = new SingleReference(nameSingleRef);
0770:                SingleReference s_ref_2 = new SingleReference(nameSingleRef);
0771:
0772:                CollectionReference ref_1 = new CollectionReference(null,
0773:                        nameRef);
0774:                CollectionReference ref_2 = new CollectionReference(null,
0775:                        nameRef);
0776:                CollectionReference ref_3 = new CollectionReference(null,
0777:                        nameRef);
0778:                CollectionReference ref_4 = new CollectionReference(null,
0779:                        nameRef);
0780:                ref_1.setSingleReference(s_ref_1);
0781:                ref_4.setSingleReference(s_ref_2);
0782:
0783:                SingleReference s_ref_3 = new SingleReference(nameSingleRef);
0784:                SingleReference s_ref_4 = new SingleReference(nameSingleRef);
0785:
0786:                obj_1.addReference(ref_1);
0787:                obj_1.addReference(ref_2);
0788:                obj_1.addReference(ref_3);
0789:                obj_1.addReference(ref_4);
0790:
0791:                obj_1.setSingleReference(s_ref_3);
0792:                obj_2.setSingleReference(s_ref_4);
0793:
0794:                Implementation odmg = OJB.getInstance();
0795:                Database db = odmg.newDatabase();
0796:                db.open(TestHelper.DEF_DATABASE_NAME, Database.OPEN_READ_WRITE);
0797:
0798:                Transaction tx = odmg.newTransaction();
0799:                tx.begin();
0800:                db.makePersistent(s_ref_1);
0801:                db.makePersistent(s_ref_2);
0802:                db.makePersistent(s_ref_3);
0803:                db.makePersistent(s_ref_4);
0804:
0805:                db.makePersistent(obj_1);
0806:                db.makePersistent(obj_2);
0807:                tx.commit();
0808:
0809:                // try to find both objects
0810:                Criteria crit = new Criteria();
0811:                crit.addEqualTo("name", name);
0812:                QueryByCriteria query = QueryFactory.newQuery(MainObject.class,
0813:                        crit);
0814:                int result = broker.getCount(query);
0815:                assertEquals("Wrong object count", 2, result);
0816:
0817:                // pk have to set and have to be different
0818:                assertNotNull(obj_1.getIdentifier());
0819:                assertNotNull(obj_2.getIdentifier());
0820:                assertNotSame(obj_1.getIdentifier(), obj_2.getIdentifier());
0821:                assertTrue(obj_1.getIdentifier().longValue() > 0);
0822:                assertTrue(obj_2.getIdentifier().longValue() > 0);
0823:                assertTrue(s_ref_3.getId().longValue() > 0);
0824:                assertTrue(ref_3.getRefIdentifier().longValue() > 0);
0825:
0826:                // no collection reference set
0827:                List references = obj_2.getAllReferences();
0828:                assertTrue(references == null || references.size() == 0);
0829:                // check anonymous FK setting
0830:                Long fk = (Long) broker.getClassDescriptor(MainObject.class)
0831:                        .getFieldDescriptorByName("refFK").getPersistentField()
0832:                        .get(obj_1);
0833:                assertTrue(
0834:                        "The assigned FK should be > 0 after store of main object, but was "
0835:                                + fk.longValue(), fk.longValue() > 0);
0836:
0837:                // get Identity objects
0838:                Identity oid_1 = new Identity(obj_1, broker);
0839:                Identity oid_2 = new Identity(obj_2, broker);
0840:                // get identifier (PK) values
0841:                Long id_1 = obj_1.getIdentifier();
0842:                Long id_2 = obj_2.getIdentifier();
0843:
0844:                broker.clearCache();
0845:
0846:                // get object with references
0847:                obj_1 = (MainObject) broker.getObjectByIdentity(oid_1);
0848:                assertNotNull(obj_1);
0849:                references = obj_1.getAllReferences();
0850:                assertNotNull(references);
0851:                assertEquals("4 references expected for object: " + obj_1, 4,
0852:                        references.size());
0853:                Iterator it = references.iterator();
0854:                while (it.hasNext()) {
0855:                    CollectionReference ref = (CollectionReference) it.next();
0856:                    assertEquals("Main object fk expected", obj_1
0857:                            .getIdentifier(), ref.fkIdentifier);
0858:                    assertTrue(
0859:                            "We expect a positive value, identity columns have to start > 0",
0860:                            (ref.getRefIdentifier().longValue() > 0));
0861:                }
0862:                assertNotNull(obj_1.getSingleReference());
0863:                obj_2 = (MainObject) broker.getObjectByIdentity(oid_2);
0864:                assertTrue(obj_1.getIdentifier().longValue() > 0);
0865:                assertTrue(obj_2.getIdentifier().longValue() > 0);
0866:                assertNotNull(obj_2.getSingleReference());
0867:                assertTrue(obj_2.getSingleReference().getId().longValue() > 0);
0868:                assertTrue(obj_1.getSingleReference().getId().longValue() > 0);
0869:                assertNotSame(obj_1.getSingleReference(), obj_2
0870:                        .getSingleReference());
0871:                // no collection reference set
0872:                references = obj_2.getAllReferences();
0873:                assertTrue(references == null || references.size() == 0);
0874:                broker.clearCache();
0875:
0876:                // get references only
0877:                Criteria crit_2 = new Criteria();
0878:                crit_2.addEqualTo("refName", nameRef);
0879:                QueryByCriteria query_2 = QueryFactory.newQuery(
0880:                        CollectionReference.class, crit_2);
0881:                int result_2 = broker.getCount(query_2);
0882:                assertEquals("Not all objects created", 4, result_2);
0883:                assertNotNull(ref_3.getRefIdentifier());
0884:
0885:                broker.clearCache();
0886:
0887:                // get second object
0888:                MainObject retObj = (MainObject) broker
0889:                        .getObjectByIdentity(oid_2);
0890:                List refList = retObj.getAllReferences();
0891:                assertNotNull(refList);
0892:                assertEquals("object do not have references", 0, refList.size());
0893:
0894:                // add new reference to object
0895:                CollectionReference ref_5 = new CollectionReference(null,
0896:                        "##new ref 1_" + nameRef);
0897:                CollectionReference ref_6 = new CollectionReference(null,
0898:                        "##new ref 2_" + nameRef);
0899:                tx.begin();
0900:                tx.lock(obj_1, Transaction.WRITE);
0901:                tx.lock(obj_2, Transaction.WRITE);
0902:                obj_1.addReference(ref_5);
0903:                obj_2.addReference(ref_6);
0904:                references = obj_2.getAllReferences();
0905:                assertNotNull(references);
0906:                assertEquals("1 references expected for object: " + obj_2, 1,
0907:                        references.size());
0908:                tx.commit();
0909:
0910:                assertNotNull(ref_5.getRefIdentifier());
0911:                assertNotNull(ref_6.getRefIdentifier());
0912:                // check FK setting
0913:                fk = ref_5.getFkIdentifier();
0914:                assertNotNull(fk);
0915:                assertEquals(obj_1.getIdentifier(), fk);
0916:                fk = ref_6.getFkIdentifier();
0917:                assertNotNull(fk);
0918:                assertEquals(obj_2.getIdentifier(), fk);
0919:                assertEquals(id_1, obj_1.getIdentifier());
0920:                assertEquals(id_2, obj_2.getIdentifier());
0921:                references = obj_2.getAllReferences();
0922:                assertNotNull(references);
0923:                assertEquals("1 references expected for object: " + obj_2, 1,
0924:                        references.size());
0925:
0926:                // refresh used broker instance to avoid problems with session cache (when used)
0927:                broker.close();
0928:                broker = PersistenceBrokerFactory.defaultPersistenceBroker();
0929:
0930:                obj_1 = (MainObject) broker.getObjectByIdentity(oid_1);
0931:                assertNotNull(obj_1);
0932:                references = obj_1.getAllReferences();
0933:                assertNotNull(references);
0934:                assertEquals("5 references expected for object: " + obj_1, 5,
0935:                        references.size());
0936:
0937:                // we don't change the main object, only add an reference, so the
0938:                // cached version of the object isn't up to date. So we have to retrieve
0939:                // all referenced objects to make it work with all cache implementations
0940:                // or evict the whole cache instead
0941:                // broker.clearCache();
0942:                obj_2 = (MainObject) broker.getObjectByIdentity(oid_2);
0943:                broker.retrieveAllReferences(obj_2);
0944:                assertNotNull(obj_2);
0945:                references = obj_2.getAllReferences();
0946:                assertNotNull(references);
0947:                assertEquals("1 references expected for object: " + obj_2, 1,
0948:                        references.size());
0949:
0950:                assertEquals(id_1, obj_1.getIdentifier());
0951:                assertEquals(id_2, obj_2.getIdentifier());
0952:
0953:                // now update main objects
0954:                tx.begin();
0955:                tx.lock(obj_1, Transaction.WRITE);
0956:                tx.lock(obj_2, Transaction.WRITE);
0957:                obj_1.setName(name + "_update");
0958:                obj_2.setName(name + "_update");
0959:                tx.commit();
0960:
0961:                obj_1 = (MainObject) broker.getObjectByIdentity(oid_1);
0962:                obj_2 = (MainObject) broker.getObjectByIdentity(oid_2);
0963:
0964:                assertNotNull(obj_1);
0965:                assertNotNull(obj_2);
0966:                assertEquals(obj_1.getName(), name + "_update");
0967:                assertEquals(obj_2.getName(), name + "_update");
0968:                assertEquals(id_1, obj_1.getIdentifier());
0969:                assertEquals(id_2, obj_2.getIdentifier());
0970:
0971:                // now update reference
0972:                obj_2 = (MainObject) broker.getObjectByIdentity(oid_2);
0973:                assertNotNull(obj_2);
0974:                tx.begin();
0975:                tx.lock(obj_2, Transaction.WRITE);
0976:                references = obj_2.getAllReferences();
0977:                CollectionReference ref = (CollectionReference) references
0978:                        .get(0);
0979:                tx.lock(ref, Transaction.WRITE);
0980:                ref.setRefName(nameRef + "_update");
0981:                tx.commit();
0982:
0983:                obj_2 = (MainObject) broker.getObjectByIdentity(oid_2);
0984:                assertNotNull(obj_2);
0985:                references = obj_2.getAllReferences();
0986:                ref = (CollectionReference) references.get(0);
0987:                assertEquals(nameRef + "_update", ref.getRefName());
0988:                assertEquals(id_1, obj_1.getIdentifier());
0989:                assertEquals(id_2, obj_2.getIdentifier());
0990:            }
0991:
0992:            public void testDelete_1() throws Exception {
0993:                // prepare for PB-api test
0994:                ojbChangeReferenceSetting(MainObject.class, "singleReference",
0995:                        true, true, true, false);
0996:                ojbChangeReferenceSetting(MainObject.class, "allReferences",
0997:                        true, true, true, false);
0998:                ojbChangeReferenceSetting(CollectionReference.class,
0999:                        "singleReference", true, true, true, false);
1000:                ojbChangeReferenceSetting(SingleReference.class, "mainObject",
1001:                        true, true, true, false);
1002:                doTestDelete();
1003:            }
1004:
1005:            public void testDelete_2() throws Exception {
1006:                // prepare for PB-api test
1007:                ojbChangeReferenceSetting(MainObject.class, "singleReference",
1008:                        true, true, true, true);
1009:                ojbChangeReferenceSetting(MainObject.class, "allReferences",
1010:                        true, true, true, true);
1011:                ojbChangeReferenceSetting(CollectionReference.class,
1012:                        "singleReference", true, true, true, true);
1013:                ojbChangeReferenceSetting(SingleReference.class, "mainObject",
1014:                        true, true, true, true);
1015:                doTestDelete();
1016:            }
1017:
1018:            public void doTestDelete() throws Exception {
1019:                if (skipTest())
1020:                    return;
1021:
1022:                long timestamp = System.currentTimeMillis();
1023:                String name = "testDelete_main_" + timestamp;
1024:                String nameRef = "testDelete_reference_" + timestamp;
1025:
1026:                MainObject obj_1 = new MainObject(null, name);
1027:
1028:                CollectionReference ref_1 = new CollectionReference(null,
1029:                        nameRef);
1030:                CollectionReference ref_2 = new CollectionReference(null,
1031:                        nameRef);
1032:
1033:                obj_1.addReference(ref_1);
1034:                obj_1.addReference(ref_2);
1035:                broker.beginTransaction();
1036:                broker.store(obj_1);
1037:                broker.commitTransaction();
1038:                Identity oid_1 = new Identity(obj_1, broker);
1039:
1040:                MainObject result = (MainObject) broker
1041:                        .getObjectByIdentity(oid_1);
1042:                assertNotNull(result);
1043:                assertNotNull(result.getAllReferences());
1044:                assertEquals(2, result.getAllReferences().size());
1045:                Long fk = ((CollectionReference) result.getAllReferences().get(
1046:                        0)).getFkIdentifier();
1047:                assertNotNull(result.getIdentifier());
1048:                assertEquals(result.getIdentifier(), fk);
1049:
1050:                broker.beginTransaction();
1051:                broker.delete(obj_1);
1052:                broker.commitTransaction();
1053:
1054:                result = (MainObject) broker.getObjectByIdentity(oid_1);
1055:                assertNull(result);
1056:                Criteria crit_2 = new Criteria();
1057:                crit_2.addEqualTo("refName", nameRef);
1058:                QueryByCriteria query_2 = QueryFactory.newQuery(
1059:                        CollectionReference.class, crit_2);
1060:                int result_2 = broker.getCount(query_2);
1061:                assertEquals(0, result_2);
1062:            }
1063:
1064:            public void testDeleteTwo_1() throws Exception {
1065:                // prepare for PB-api test
1066:                ojbChangeReferenceSetting(MainObject.class, "singleReference",
1067:                        true, true, true, false);
1068:                ojbChangeReferenceSetting(MainObject.class, "allReferences",
1069:                        true, true, true, false);
1070:                ojbChangeReferenceSetting(CollectionReference.class,
1071:                        "singleReference", true, true, true, false);
1072:                ojbChangeReferenceSetting(SingleReference.class, "mainObject",
1073:                        true, true, true, false);
1074:                doTestDeleteTwo();
1075:            }
1076:
1077:            public void testDeleteTwo_2() throws Exception {
1078:                // prepare for PB-api test
1079:                ojbChangeReferenceSetting(MainObject.class, "singleReference",
1080:                        true, true, true, true);
1081:                ojbChangeReferenceSetting(MainObject.class, "allReferences",
1082:                        true, true, true, true);
1083:                ojbChangeReferenceSetting(CollectionReference.class,
1084:                        "singleReference", true, true, true, true);
1085:                ojbChangeReferenceSetting(SingleReference.class, "mainObject",
1086:                        true, true, true, true);
1087:                doTestDeleteTwo();
1088:            }
1089:
1090:            public void doTestDeleteTwo() throws Exception {
1091:                if (skipTest())
1092:                    return;
1093:                long timestamp = System.currentTimeMillis();
1094:                String name = "testDeleteTwo_main_" + timestamp;
1095:                String nameRef = "testDeleteTwo_reference_" + timestamp;
1096:
1097:                MainObject obj_1 = new MainObject(null, name);
1098:
1099:                CollectionReference ref_1 = new CollectionReference(null,
1100:                        nameRef);
1101:                CollectionReference ref_2 = new CollectionReference(null,
1102:                        nameRef);
1103:
1104:                obj_1.addReference(ref_1);
1105:                obj_1.addReference(ref_2);
1106:
1107:                // chaotic operations
1108:                broker.beginTransaction();
1109:                // System.out.println("1. "+obj_1);
1110:                broker.store(obj_1);
1111:                // System.out.println("2. "+obj_1);
1112:                broker.delete(obj_1);
1113:                // System.out.println("3. "+obj_1);
1114:                broker.store(obj_1);
1115:                // System.out.println("4. "+obj_1);
1116:                broker.delete(obj_1);
1117:                // System.out.println("5. "+obj_1);
1118:                broker.store(obj_1);
1119:                // System.out.println("6. "+obj_1);
1120:                broker.delete(obj_1);
1121:                // System.out.println("7. "+obj_1);
1122:                broker.store(obj_1);
1123:                // System.out.println("8. "+obj_1);
1124:                broker.commitTransaction();
1125:                Identity oid_1 = new Identity(obj_1, broker);
1126:
1127:                MainObject result = (MainObject) broker
1128:                        .getObjectByIdentity(oid_1);
1129:                assertNotNull(result);
1130:                assertNotNull(result.getAllReferences());
1131:                assertEquals(2, result.getAllReferences().size());
1132:                Long fk = ((CollectionReference) result.getAllReferences().get(
1133:                        0)).getFkIdentifier();
1134:                assertNotNull(result.getIdentifier());
1135:                assertEquals(result.getIdentifier(), fk);
1136:
1137:                // we should find exactly one object
1138:                Criteria c = new Criteria();
1139:                c.addEqualTo("name", name);
1140:                QueryByCriteria q = QueryFactory.newQuery(MainObject.class, c);
1141:                Collection col = broker.getCollectionByQuery(q);
1142:                assertNotNull(col);
1143:                assertEquals(1, col.size());
1144:
1145:                broker.beginTransaction();
1146:                broker.delete(obj_1);
1147:                broker.commitTransaction();
1148:
1149:                result = (MainObject) broker.getObjectByIdentity(oid_1);
1150:                assertNull(result);
1151:                Criteria crit_2 = new Criteria();
1152:                crit_2.addEqualTo("refName", nameRef);
1153:                QueryByCriteria query_2 = QueryFactory.newQuery(
1154:                        CollectionReference.class, crit_2);
1155:                int result_2 = broker.getCount(query_2);
1156:                assertEquals(0, result_2);
1157:            }
1158:
1159:            //    public void testAllInOne() throws Exception
1160:            //    {
1161:            //        // sleep thread to make timestamp based tests work
1162:            //        testSimpleInsert();
1163:            //        ojbSleep();
1164:            //        ojbSleep();
1165:            //        testSimpleInsert();
1166:            //        ojbSleep();
1167:            //        ojbSleep();
1168:            //        testReferenceInsertUpdate();
1169:            //        ojbSleep();
1170:            //        ojbSleep();
1171:            //        // testReferenceInsertUpdate_2();
1172:            //        ojbSleep();
1173:            //        ojbSleep();
1174:            //        testReferenceInsertUpdate();
1175:            //    }
1176:
1177:            //    void ojbChangeReferenceSetting(Class clazz, String referenceField, boolean autoRetrieve, int autoUpdate, int autoDelete, boolean useProxy)
1178:            //    {
1179:            //        ClassDescriptor cld = broker.getClassDescriptor(clazz);
1180:            //        ObjectReferenceDescriptor ref = cld.getCollectionDescriptorByName(referenceField);
1181:            //        if(ref == null) ref = cld.getObjectReferenceDescriptorByName(referenceField);
1182:            //        ref.setLazy(useProxy);
1183:            //        ref.setCascadeRetrieve(autoRetrieve);
1184:            //        ref.setCascadingStore(autoUpdate);
1185:            //        ref.setCascadingDelete(autoDelete);
1186:            //    }
1187:            //
1188:            //    void ojbChangeReferenceSetting(Class clazz, String referenceField, boolean autoRetrieve, boolean autoUpdate, boolean autoDelete, boolean useProxy)
1189:            //    {
1190:            //        ClassDescriptor cld = broker.getClassDescriptor(clazz);
1191:            //        ObjectReferenceDescriptor ref = cld.getCollectionDescriptorByName(referenceField);
1192:            //        if(ref == null) ref = cld.getObjectReferenceDescriptorByName(referenceField);
1193:            //        ref.setLazy(useProxy);
1194:            //        ref.setCascadeRetrieve(autoRetrieve);
1195:            //        ref.setCascadeStore(autoUpdate);
1196:            //        ref.setCascadeDelete(autoDelete);
1197:            //    }
1198:
1199:            //========================================================================
1200:            // inner classes, used for test
1201:            //========================================================================
1202:
1203:            public static interface MainObjectIF extends Serializable {
1204:                public SingleReferenceIF getSingleReference();
1205:
1206:                public void setSingleReference(SingleReferenceIF singleReference);
1207:
1208:                public List getAllReferences();
1209:
1210:                public void addReference(CollectionReference reference);
1211:
1212:                public void setAllReferences(List allReferences);
1213:
1214:                public Long getIdentifier();
1215:
1216:                public void setIdentifier(Long identifier);
1217:
1218:                public String getName();
1219:
1220:                public void setName(String name);
1221:            }
1222:
1223:            public static class MainObject implements  MainObjectIF {
1224:                private Long identifier;
1225:                private String name;
1226:                private List allReferences;
1227:                // we use anonymous field for FK
1228:                private SingleReferenceIF singleReference;
1229:
1230:                public MainObject() {
1231:                }
1232:
1233:                public MainObject(Long identifier, String name) {
1234:                    this .identifier = identifier;
1235:                    this .name = name;
1236:                }
1237:
1238:                public SingleReferenceIF getSingleReference() {
1239:                    return singleReference;
1240:                }
1241:
1242:                public void setSingleReference(SingleReferenceIF singleReference) {
1243:                    this .singleReference = singleReference;
1244:                }
1245:
1246:                public List getAllReferences() {
1247:                    return allReferences;
1248:                }
1249:
1250:                public void addReference(CollectionReference reference) {
1251:                    if (allReferences == null) {
1252:                        allReferences = new ArrayList();
1253:                    }
1254:                    allReferences.add(reference);
1255:                }
1256:
1257:                public void setAllReferences(List allReferences) {
1258:                    this .allReferences = allReferences;
1259:                }
1260:
1261:                public Long getIdentifier() {
1262:                    return identifier;
1263:                }
1264:
1265:                public void setIdentifier(Long identifier) {
1266:                    this .identifier = identifier;
1267:                }
1268:
1269:                public String getName() {
1270:                    return name;
1271:                }
1272:
1273:                public void setName(String name) {
1274:                    this .name = name;
1275:                }
1276:
1277:                public String toString() {
1278:                    return new ToStringBuilder(this ).append("identifier",
1279:                            identifier).append("name", name).append(
1280:                            "allReferences",
1281:                            allReferences != null ? allReferences.toString()
1282:                                    : "null").append("singleReference",
1283:                            singleReference.getClass().toString()).toString();
1284:                }
1285:            }
1286:
1287:            public static interface SingleReferenceIF extends Serializable {
1288:                public MainObjectIF getMainObject();
1289:
1290:                public void setMainObject(MainObjectIF mainObject);
1291:
1292:                public Long getId();
1293:
1294:                public void setId(Long id);
1295:
1296:                public String getName();
1297:
1298:                public void setName(String name);
1299:            }
1300:
1301:            public static class SingleReference implements  SingleReferenceIF {
1302:                Long id;
1303:                String name;
1304:                String ojbConcreteClass;
1305:                MainObjectIF mainObject;
1306:
1307:                public SingleReference() {
1308:                    this (null);
1309:                }
1310:
1311:                public SingleReference(String name) {
1312:                    this .name = name;
1313:                    ojbConcreteClass = SingleReference.class.getName();
1314:                    // id = new Long((long)(Math.random() * Integer.MAX_VALUE));
1315:                }
1316:
1317:                public MainObjectIF getMainObject() {
1318:                    return mainObject;
1319:                }
1320:
1321:                public void setMainObject(MainObjectIF mainObject) {
1322:                    this .mainObject = mainObject;
1323:                }
1324:
1325:                public Long getId() {
1326:                    return id;
1327:                }
1328:
1329:                public void setId(Long id) {
1330:                    this .id = id;
1331:                }
1332:
1333:                public String getName() {
1334:                    return name;
1335:                }
1336:
1337:                public void setName(String name) {
1338:                    this .name = name;
1339:                }
1340:
1341:                public String toString() {
1342:                    return new ToStringBuilder(this ).append("id", id).append(
1343:                            "name", name).append(
1344:                            "mainObject",
1345:                            mainObject != null ? mainObject.getClass()
1346:                                    .toString() : "null").toString();
1347:                }
1348:            }
1349:
1350:            public static interface CollectionReferenceIF extends Serializable {
1351:                public Long getRefIdentifier();
1352:
1353:                public void setRefIdentifier(Long refIdentifier);
1354:
1355:                public SingleReferenceIF getSingleReference();
1356:
1357:                public void setSingleReference(SingleReferenceIF singleReference);
1358:
1359:                public Long getFkIdentifier();
1360:
1361:                public void setFkIdentifier(Long fkIdentifier);
1362:
1363:                public String getRefName();
1364:
1365:                public void setRefName(String refName);
1366:            }
1367:
1368:            public static class CollectionReference implements 
1369:                    CollectionReferenceIF {
1370:                private Long refIdentifier;
1371:                private String refName;
1372:                private Long fkIdentifier;
1373:                String ojbConcreteClass;
1374:                private SingleReferenceIF singleReference;
1375:
1376:                public CollectionReference() {
1377:                    ojbConcreteClass = CollectionReference.class.getName();
1378:                }
1379:
1380:                public CollectionReference(Long refIdentifier, String refName) {
1381:                    this ();
1382:                    this .refIdentifier = refIdentifier;
1383:                    this .refName = refName;
1384:                }
1385:
1386:                public Long getRefIdentifier() {
1387:                    return refIdentifier;
1388:                }
1389:
1390:                public void setRefIdentifier(Long refIdentifier) {
1391:                    this .refIdentifier = refIdentifier;
1392:                }
1393:
1394:                public SingleReferenceIF getSingleReference() {
1395:                    return singleReference;
1396:                }
1397:
1398:                public void setSingleReference(SingleReferenceIF singleReference) {
1399:                    this .singleReference = singleReference;
1400:                }
1401:
1402:                public Long getFkIdentifier() {
1403:                    return fkIdentifier;
1404:                }
1405:
1406:                public void setFkIdentifier(Long fkIdentifier) {
1407:                    this .fkIdentifier = fkIdentifier;
1408:                }
1409:
1410:                public String getRefName() {
1411:                    return refName;
1412:                }
1413:
1414:                public void setRefName(String refName) {
1415:                    this .refName = refName;
1416:                }
1417:
1418:                public String toString() {
1419:                    return new ToStringBuilder(this )
1420:                            .append("id", refIdentifier)
1421:                            .append("name", refName).append("fkIdentifier",
1422:                                    fkIdentifier).append(
1423:                                    "singleReference",
1424:                                    singleReference != null ? singleReference
1425:                                            .toString() : "null").toString();
1426:                }
1427:            }
1428:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.