KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > sequence > NativeIdentifierTest


1 package org.apache.ojb.broker.sequence;
2
3 import java.io.Serializable JavaDoc;
4 import java.sql.Connection JavaDoc;
5 import java.sql.SQLException JavaDoc;
6 import java.sql.Statement JavaDoc;
7 import java.util.ArrayList JavaDoc;
8 import java.util.Collection JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.List JavaDoc;
11
12 import org.apache.commons.lang.builder.ToStringBuilder;
13 import org.apache.ojb.broker.Identity;
14 import org.apache.ojb.broker.PersistenceBroker;
15 import org.apache.ojb.broker.PersistenceBrokerFactory;
16 import org.apache.ojb.broker.TestHelper;
17 import org.apache.ojb.broker.metadata.MetadataManager;
18 import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
19 import org.apache.ojb.broker.metadata.SequenceDescriptor;
20 import org.apache.ojb.broker.platforms.Platform;
21 import org.apache.ojb.broker.platforms.PlatformHsqldbImpl;
22 import org.apache.ojb.broker.platforms.PlatformMySQLImpl;
23 import org.apache.ojb.broker.query.Criteria;
24 import org.apache.ojb.broker.query.QueryByCriteria;
25 import org.apache.ojb.broker.query.QueryFactory;
26 import org.apache.ojb.broker.util.sequence.SequenceManagerNativeImpl;
27 import org.apache.ojb.junit.PBTestCase;
28 import org.apache.ojb.odmg.OJB;
29 import org.apache.ojb.odmg.TransactionExt;
30 import org.odmg.Database;
31 import org.odmg.Implementation;
32 import org.odmg.Transaction;
33
34 /**
35  * Test case for {@link SequenceManagerNativeImpl}. These test check
36  * support for native identity columns. Test case only works for
37  * Hsql and Mysql.
38  *
39  * @author <a HREF="mailto:armin@codeAuLait.de">Armin Waibel</a>
40  * @version $Id: NativeIdentifierTest.java,v 1.10.2.8 2005/10/06 21:01:44 arminw Exp $
41  */

42 public class NativeIdentifierTest extends PBTestCase
43 {
44     // Statements for MainObject table
45
private static final String JavaDoc DROP = "DROP TABLE NATIVE_MAIN_OBJECT";
46     private static final String JavaDoc CREATE_MYSQL =
47             "CREATE TABLE NATIVE_MAIN_OBJECT(NATIVE_ID int(11) NOT NULL PRIMARY KEY" +
48             " auto_increment,REF_ID int(11),NAME VARCHAR(250))";
49     private static final String JavaDoc CREATE_HSQL =
50             "CREATE TABLE NATIVE_MAIN_OBJECT(NATIVE_ID IDENTITY NOT NULL PRIMARY KEY," +
51             " REF_ID int,NAME VARCHAR(250))";
52
53     private static final String JavaDoc INSERT_DUMMY = "INSERT INTO NATIVE_MAIN_OBJECT (NAME) VALUES ('Dummy_1')";
54
55     private static final String JavaDoc ADD_CONSTRAINT =
56         "ALTER TABLE NATIVE_MAIN_OBJECT" +
57         " ADD CONSTRAINT MAIN_REF_FK" +
58         " FOREIGN KEY (REF_ID) REFERENCES NATIVE_REFERENCE_OBJECT (NATIVE_ID)";
59     private static final String JavaDoc DROP_CONSTRAINT_HSQL =
60         "ALTER TABLE NATIVE_MAIN_OBJECT" +
61         " DROP CONSTRAINT MAIN_REF_FK";
62     private static final String JavaDoc DROP_CONSTRAINT_MYSQL =
63         "ALTER TABLE NATIVE_MAIN_OBJECT" +
64         " DROP FOREIGN KEY MAIN_REF_FK";
65
66
67     // Statements for NATIVE_REF_TEST table
68
private static final String JavaDoc CREATE_REF_MYSQL =
69             "CREATE TABLE NATIVE_REFERENCE_OBJECT (NATIVE_ID int(11) NOT NULL PRIMARY KEY auto_increment," +
70             " NAME VARCHAR(250), OJB_CONCRETE_CLASS VARCHAR(250), FK_ID int, REF_ID int(11), SINGLE_REF_FK BIGINT" +
71             " , FOREIGN KEY (FK_ID) REFERENCES NATIVE_MAIN_OBJECT (NATIVE_ID) )";
72     private static final String JavaDoc CREATE_REF_HSQL =
73             "CREATE TABLE NATIVE_REFERENCE_OBJECT (NATIVE_ID IDENTITY NOT NULL PRIMARY KEY," +
74             " NAME VARCHAR(250), OJB_CONCRETE_CLASS VARCHAR(250), FK_ID int, REF_ID int, SINGLE_REF_FK BIGINT" +
75             " , FOREIGN KEY (FK_ID) REFERENCES NATIVE_MAIN_OBJECT (NATIVE_ID) )";
76     private static final String JavaDoc DROP_REF = "DROP TABLE NATIVE_REFERENCE_OBJECT";
77     private static final String JavaDoc INSERT_DUMMY_REF = "INSERT INTO NATIVE_REFERENCE_OBJECT (NAME) VALUES ('Dummy_2')";
78
79     private Platform platform;
80     private Class JavaDoc oldSequenceManager;
81
82     public NativeIdentifierTest(String JavaDoc s)
83     {
84         super(s);
85     }
86
87     public static void main(String JavaDoc[] args)
88     {
89         String JavaDoc[] arr = {NativeIdentifierTest.class.getName()};
90         junit.textui.TestRunner.main(arr);
91     }
92
93     private boolean skipTest() throws Exception JavaDoc
94     {
95         return !((platform instanceof PlatformMySQLImpl) || (platform instanceof PlatformHsqldbImpl));
96     }
97
98     public void setUp() throws Exception JavaDoc
99     {
100         super.setUp();
101
102         platform = broker.serviceConnectionManager().getSupportedPlatform();
103         if (skipTest()) return;
104
105         Connection JavaDoc con;
106         Statement JavaDoc stmt;
107
108         PersistenceBroker pb = PersistenceBrokerFactory.defaultPersistenceBroker();
109         try
110         {
111             con = pb.serviceConnectionManager().getConnection();
112             stmt = con.createStatement();
113             try
114             {
115                 if(platform instanceof PlatformMySQLImpl)
116                 {
117                     stmt.execute(DROP_CONSTRAINT_MYSQL);
118                 }
119                 else if(platform instanceof PlatformHsqldbImpl)
120                 {
121                     stmt.execute(DROP_CONSTRAINT_HSQL);
122                 }
123             }
124             catch (SQLException JavaDoc e)
125             {
126             }
127             stmt.close();
128
129             stmt = con.createStatement();
130             try
131             {
132                 stmt.execute(DROP_REF);
133             }
134             catch (SQLException JavaDoc e)
135             {
136             }
137             stmt.close();
138
139             stmt = con.createStatement();
140             try
141             {
142                 stmt.execute(DROP);
143             }
144             catch (SQLException JavaDoc e)
145             {
146             }
147             stmt.close();
148         }
149         finally
150         {
151             if (pb != null) pb.close();
152         }
153
154
155         try
156         {
157             con = broker.serviceConnectionManager().getConnection();
158             if(platform instanceof PlatformMySQLImpl)
159             {
160                 stmt = con.createStatement();
161                 stmt.execute(CREATE_MYSQL);
162                 stmt.close();
163             }
164             if(platform instanceof PlatformHsqldbImpl)
165             {
166                 stmt = con.createStatement();
167                 stmt.execute(CREATE_HSQL);
168                 stmt.close();
169             }
170
171             stmt = con.createStatement();
172             stmt.execute(INSERT_DUMMY);
173             stmt.close();
174
175             if(platform instanceof PlatformMySQLImpl)
176             {
177                 stmt = con.createStatement();
178                 stmt.execute(CREATE_REF_MYSQL);
179                 stmt.close();
180             }
181             if(platform instanceof PlatformHsqldbImpl)
182             {
183                 stmt = con.createStatement();
184                 stmt.execute(CREATE_REF_HSQL);
185                 stmt.close();
186             }
187
188             stmt = con.createStatement();
189             stmt.execute(ADD_CONSTRAINT);
190             stmt.close();
191
192             stmt = con.createStatement();
193             stmt.execute(INSERT_DUMMY_REF);
194             stmt.close();
195
196             SequenceDescriptor sd = MetadataManager.getInstance().connectionRepository().
197                     getDescriptor(broker.getPBKey()).getSequenceDescriptor();
198             oldSequenceManager = sd.getSequenceManagerClass();
199             sd.setSequenceManagerClass(SequenceManagerNativeImpl.class);
200         }
201         catch (SQLException JavaDoc ex)
202         {
203             ex.printStackTrace();
204         }
205         finally
206         {
207             if (broker != null) broker.close();
208         }
209
210         PersistenceBrokerFactory.releaseAllInstances();
211         broker = PersistenceBrokerFactory.defaultPersistenceBroker();
212         SequenceDescriptor sd = MetadataManager.getInstance().connectionRepository().
213                 getDescriptor(broker.getPBKey()).getSequenceDescriptor();
214         assertEquals(SequenceManagerNativeImpl.class, sd.getSequenceManagerClass());
215     }
216
217     public void tearDown() throws Exception JavaDoc
218     {
219         super.tearDown();
220         if (skipTest()) return;
221
222         Connection JavaDoc con;
223         Statement JavaDoc stmt;
224         PersistenceBroker pb = PersistenceBrokerFactory.defaultPersistenceBroker();
225         try
226         {
227             con = pb.serviceConnectionManager().getConnection();
228
229             stmt = con.createStatement();
230             if(platform instanceof PlatformMySQLImpl)
231             {
232                 stmt.execute(DROP_CONSTRAINT_MYSQL);
233             }
234             else if(platform instanceof PlatformHsqldbImpl)
235             {
236                 stmt.execute(DROP_CONSTRAINT_HSQL);
237             }
238             stmt.close();
239
240             stmt = con.createStatement();
241             stmt.execute(DROP_REF);
242             stmt.close();
243
244             stmt = con.createStatement();
245             stmt.execute(DROP);
246             stmt.close();
247
248             SequenceDescriptor sd = MetadataManager.getInstance().connectionRepository().
249                     getDescriptor(pb.getPBKey()).getSequenceDescriptor();
250             sd.setSequenceManagerClass(oldSequenceManager);
251         }
252         catch (SQLException JavaDoc ex)
253         {
254             ex.printStackTrace();
255         }
256         finally
257         {
258             if (pb != null)
259             {
260                 pb.clearCache();
261                 pb.close();
262             }
263         }
264
265         PersistenceBrokerFactory.releaseAllInstances();
266         broker = PersistenceBrokerFactory.defaultPersistenceBroker();
267         SequenceDescriptor sd = MetadataManager.getInstance().connectionRepository().
268                 getDescriptor(broker.getPBKey()).getSequenceDescriptor();
269         assertEquals(oldSequenceManager, sd.getSequenceManagerClass());
270         broker.close();
271     }
272
273     public void testSimpleInsert_1() throws Exception JavaDoc
274     {
275         // prepare for PB-api test
276
ojbChangeReferenceSetting(MainObject.class, "singleReference", true, true, true, false);
277         ojbChangeReferenceSetting(MainObject.class, "allReferences", true, true, true, false);
278         ojbChangeReferenceSetting(CollectionReference.class, "singleReference", true, true, true, false);
279         ojbChangeReferenceSetting(SingleReference.class, "mainObject", true, true, true, false);
280         doTtestSimpleInsert();
281     }
282
283     public void testSimpleInsert_2() throws Exception JavaDoc
284     {
285         // prepare for PB-api test
286
ojbChangeReferenceSetting(MainObject.class, "singleReference", true, true, true, true);
287         ojbChangeReferenceSetting(MainObject.class, "allReferences", true, true, true, true);
288         ojbChangeReferenceSetting(CollectionReference.class, "singleReference", true, true, true, true);
289         ojbChangeReferenceSetting(SingleReference.class, "mainObject", true, true, true, true);
290         doTtestSimpleInsert();
291     }
292
293     public void doTtestSimpleInsert() throws Exception JavaDoc
294     {
295         if (skipTest()) return;
296
297         long timestamp = System.currentTimeMillis();
298         String JavaDoc name = "testSimpleInsert_" + timestamp;
299
300         MainObject obj_1 = new MainObject(null, name);
301         MainObject obj_2 = new MainObject(null, name);
302         MainObject obj_3 = new MainObject(null, name);
303
304         broker.beginTransaction();
305         broker.store(obj_1);
306 // System.out.println("obj_1: "+obj_1);
307
broker.store(obj_2);
308 // System.out.println("obj_2: "+obj_2);
309
broker.store(obj_3);
310 // System.out.println("obj_3: "+obj_3);
311
broker.commitTransaction();
312
313         Criteria crit = new Criteria();
314         crit.addEqualTo("name", name);
315         QueryByCriteria query = QueryFactory.newQuery(MainObject.class, crit);
316         int result = broker.getCount(query);
317         assertEquals("Not all objects created", 3, result);
318         assertNotNull(obj_1.getIdentifier());
319         assertTrue(obj_1.getIdentifier().longValue() > 0);
320         assertTrue(obj_3.getIdentifier().longValue() > 0);
321     }
322
323     public void testSimpleInsertODMG_1() throws Exception JavaDoc
324     {
325         int none = ObjectReferenceDescriptor.CASCADE_NONE;
326         ojbChangeReferenceSetting(MainObject.class, "singleReference", true, none, none, false);
327         ojbChangeReferenceSetting(MainObject.class, "allReferences", true, none, none, false);
328         ojbChangeReferenceSetting(CollectionReference.class, "singleReference", true, none, none, false);
329         ojbChangeReferenceSetting(SingleReference.class, "mainObject", true, none, none, false);
330     }
331
332     public void testSimpleInsertODMG_2() throws Exception JavaDoc
333     {
334         int none = ObjectReferenceDescriptor.CASCADE_NONE;
335         ojbChangeReferenceSetting(MainObject.class, "singleReference", true, none, none, true);
336         ojbChangeReferenceSetting(MainObject.class, "allReferences", true, none, none, true);
337         ojbChangeReferenceSetting(CollectionReference.class, "singleReference", true, none, none, true);
338         ojbChangeReferenceSetting(SingleReference.class, "mainObject", true, none, none, true);
339     }
340     public void doTestSimpleInsertODMG() throws Exception JavaDoc
341     {
342         if (skipTest()) return;
343
344         long timestamp = System.currentTimeMillis();
345         String JavaDoc name = "testSimpleInsert_" + timestamp;
346
347         MainObject obj_1 = new MainObject(null, name);
348         MainObject obj_2 = new MainObject(null, name);
349         MainObject obj_3 = new MainObject(null, name);
350
351         Implementation odmg = OJB.getInstance();
352         Database db = odmg.newDatabase();
353         db.open(TestHelper.DEF_DATABASE_NAME, Database.OPEN_READ_WRITE);
354
355         Transaction tx = odmg.newTransaction();
356         tx.begin();
357         tx.lock(obj_1, Transaction.WRITE);
358         tx.lock(obj_2, Transaction.WRITE);
359         tx.lock(obj_3, Transaction.WRITE);
360         tx.commit();
361
362         Criteria crit = new Criteria();
363         crit.addEqualTo("name", name);
364         QueryByCriteria query = QueryFactory.newQuery(MainObject.class, crit);
365         int result = broker.getCount(query);
366         assertEquals("Not all objects created", 3, result);
367         assertNotNull(obj_1.getIdentifier());
368         assertTrue(obj_1.getIdentifier().longValue() > 0);
369         assertTrue(obj_3.getIdentifier().longValue() > 0);
370     }
371
372     public void testReferenceInsertUpdate_1() throws Exception JavaDoc
373     {
374         // prepare for PB-api test
375
ojbChangeReferenceSetting(MainObject.class, "singleReference", true, true, true, false);
376         ojbChangeReferenceSetting(MainObject.class, "allReferences", true, true, true, false);
377         ojbChangeReferenceSetting(CollectionReference.class, "singleReference", true, true, true, false);
378         ojbChangeReferenceSetting(SingleReference.class, "mainObject", true, true, true, false);
379         doTestReferenceInsertUpdate();
380     }
381
382     public void testReferenceInsertUpdate_2() throws Exception JavaDoc
383     {
384         // prepare for PB-api test
385
ojbChangeReferenceSetting(MainObject.class, "singleReference", true, true, true, true);
386         ojbChangeReferenceSetting(MainObject.class, "allReferences", true, true, true, true);
387         ojbChangeReferenceSetting(CollectionReference.class, "singleReference", true, true, true, true);
388         ojbChangeReferenceSetting(SingleReference.class, "mainObject", true, true, true, true);
389         doTestReferenceInsertUpdate();
390     }
391
392     public void doTestReferenceInsertUpdate() throws Exception JavaDoc
393     {
394         if (skipTest()) return;
395         long timestamp = System.currentTimeMillis();
396         String JavaDoc name = "testReferenceInsert_main_" + timestamp;
397         String JavaDoc nameRef = "testReferenceInsert_reference_" + timestamp;
398         String JavaDoc nameSingleRef = "testReferenceInsert_single_reference_" + timestamp;
399
400         MainObject obj_1 = new MainObject(null, name);
401         MainObject obj_2 = new MainObject(null, name);
402
403         SingleReference s_ref_1 = new SingleReference(nameSingleRef);
404         SingleReference s_ref_2 = new SingleReference(nameSingleRef);
405
406         CollectionReference ref_1 = new CollectionReference(null, nameRef);
407         CollectionReference ref_2 = new CollectionReference(null, nameRef);
408         CollectionReference ref_3 = new CollectionReference(null, nameRef);
409         CollectionReference ref_4 = new CollectionReference(null, nameRef);
410         ref_1.setSingleReference(s_ref_1);
411         ref_4.setSingleReference(s_ref_2);
412
413         SingleReference s_ref_3 = new SingleReference(nameSingleRef);
414         SingleReference s_ref_4 = new SingleReference(nameSingleRef);
415
416         obj_1.addReference(ref_1);
417         obj_1.addReference(ref_2);
418         obj_1.addReference(ref_3);
419         obj_1.addReference(ref_4);
420
421         obj_1.setSingleReference(s_ref_3);
422         s_ref_3.setMainObject(obj_1);
423         obj_2.setSingleReference(s_ref_4);
424         s_ref_3.setMainObject(obj_1);
425
426         broker.beginTransaction();
427         // first store a reference
428
broker.store(ref_1);
429 // System.out.println("ref_1: "+ref_1);
430
// then store main object with other references
431
broker.store(obj_1);
432 // System.out.println("obj_1: "+obj_1);
433
// store second object without references
434
broker.store(obj_2);
435 // System.out.println("obj_2: "+obj_2);
436
broker.commitTransaction();
437
438         // try to find both objects
439
Criteria crit = new Criteria();
440         crit.addEqualTo("name", name);
441         QueryByCriteria query = QueryFactory.newQuery(MainObject.class, crit);
442         int result = broker.getCount(query);
443         assertEquals("Wrong object count", 2, result);
444
445         // pk have to set and have to be different
446
assertNotNull(obj_1.getIdentifier());
447         assertNotNull(obj_2.getIdentifier());
448         assertNotSame(obj_1.getIdentifier(), obj_2.getIdentifier());
449         assertTrue(obj_1.getIdentifier().longValue() > 0);
450         assertTrue(obj_2.getIdentifier().longValue() > 0);
451         assertTrue(s_ref_3.getId().longValue() > 0);
452         assertTrue(ref_3.getRefIdentifier().longValue() > 0);
453
454         // get Identity objects
455
Identity oid_1 = new Identity(obj_1, broker);
456         Identity oid_2 = new Identity(obj_2, broker);
457         // get identifier (PK) values
458
Long JavaDoc id_1 = obj_1.getIdentifier();
459         Long JavaDoc id_2 = obj_2.getIdentifier();
460
461         broker.clearCache();
462
463         // get object with references
464
obj_1 = (MainObject) broker.getObjectByIdentity(oid_1);
465         assertNotNull(obj_1);
466         List JavaDoc references = obj_1.getAllReferences();
467         assertNotNull(references);
468         assertEquals("4 references expected for object: "+obj_1, 4, references.size());
469         Iterator JavaDoc it = references.iterator();
470         while (it.hasNext())
471         {
472             CollectionReference ref = (CollectionReference) it.next();
473             assertEquals("Main object fk expected", obj_1.getIdentifier(), ref.fkIdentifier);
474             assertTrue("We expect a positive value, identity columns have to start > 0",
475                     (ref.getRefIdentifier().longValue() > 0));
476         }
477         assertNotNull(obj_1.getSingleReference());
478         obj_2 = (MainObject) broker.getObjectByIdentity(oid_2);
479         assertTrue(obj_1.getIdentifier().longValue() > 0);
480         assertTrue(obj_2.getIdentifier().longValue() > 0);
481         assertNotNull(obj_2.getSingleReference());
482         assertTrue(obj_2.getSingleReference().getId().longValue() > 0);
483         assertTrue(obj_1.getSingleReference().getId().longValue() > 0);
484         assertNotSame(obj_1.getSingleReference(), obj_2.getSingleReference());
485         broker.clearCache();
486
487         // get references only
488
Criteria crit_2 = new Criteria();
489         crit_2.addEqualTo("refName", nameRef);
490         QueryByCriteria query_2 = QueryFactory.newQuery(CollectionReference.class, crit_2);
491         int result_2 = broker.getCount(query_2);
492         assertEquals("Not all objects created", 4, result_2);
493         assertNotNull(ref_3.getRefIdentifier());
494
495         broker.clearCache();
496
497         // get second object
498
MainObject retObj = (MainObject) broker.getObjectByIdentity(oid_2);
499         List JavaDoc refList = retObj.getAllReferences();
500         assertNotNull(refList);
501         assertEquals("object do not have references", 0, refList.size());
502
503         // add new reference to object
504
CollectionReference ref_5 = new CollectionReference(null, nameRef);
505         CollectionReference ref_6 = new CollectionReference(null, nameRef);
506         obj_1.addReference(ref_5);
507         obj_2.addReference(ref_6);
508         broker.beginTransaction();
509         broker.store(obj_1);
510         broker.store(obj_2);
511         broker.commitTransaction();
512         assertNotNull(ref_5.getRefIdentifier());
513         assertNotNull(ref_6.getRefIdentifier());
514         assertEquals(id_1, obj_1.getIdentifier());
515         assertEquals(id_2, obj_2.getIdentifier());
516
517         obj_1 = (MainObject) broker.getObjectByIdentity(oid_1);
518         assertNotNull(obj_1);
519         references = obj_1.getAllReferences();
520         assertNotNull(references);
521         assertEquals("5 references expected for object: "+obj_1, 5, references.size());
522
523         obj_2 = (MainObject) broker.getObjectByIdentity(oid_2);
524         assertNotNull(obj_2);
525         references = obj_2.getAllReferences();
526         assertNotNull(references);
527         assertEquals("1 references expected for object: "+obj_2, 1, references.size());
528
529         assertEquals(id_1, obj_1.getIdentifier());
530         assertEquals(id_2, obj_2.getIdentifier());
531
532         // now update main objects
533
obj_1.setName(name+"_update");
534         obj_2.setName(name+"_update");
535         broker.beginTransaction();
536         broker.store(obj_1);
537         broker.store(obj_2);
538         broker.commitTransaction();
539
540         obj_1 = (MainObject) broker.getObjectByIdentity(oid_1);
541         obj_2 = (MainObject) broker.getObjectByIdentity(oid_2);
542
543         assertNotNull(obj_1);
544         assertNotNull(obj_2);
545         assertEquals(obj_1.getName(), name+"_update");
546         assertEquals(obj_2.getName(), name+"_update");
547         assertEquals(id_1, obj_1.getIdentifier());
548         assertEquals(id_2, obj_2.getIdentifier());
549
550         // now update reference
551
obj_2 = (MainObject) broker.getObjectByIdentity(oid_2);
552         assertNotNull(obj_2);
553         references = obj_2.getAllReferences();
554         CollectionReference ref = (CollectionReference) references.get(0);
555         ref.setRefName(nameRef+"_update");
556         broker.beginTransaction();
557         broker.store(obj_2);
558         broker.commitTransaction();
559         obj_2 = (MainObject) broker.getObjectByIdentity(oid_2);
560         assertNotNull(obj_2);
561         references = obj_2.getAllReferences();
562         ref = (CollectionReference) references.get(0);
563         assertEquals(nameRef+"_update", ref.getRefName());
564         assertEquals(id_1, obj_1.getIdentifier());
565         assertEquals(id_2, obj_2.getIdentifier());
566     }
567
568     /**
569      * critical test case, because single broker instance (PB-api) is concurrent used
570      * with the ODMG-api, take care of caches
571      */

572     public void testReferenceInsertUpdateODMG_1() throws Exception JavaDoc
573     {
574         if (skipTest()) return;
575
576         // prepare metadata for odmg-api
577
int none = ObjectReferenceDescriptor.CASCADE_NONE;
578         ojbChangeReferenceSetting(MainObject.class, "singleReference", true, none, none, false);
579         ojbChangeReferenceSetting(MainObject.class, "allReferences", true, none, none, false);
580         ojbChangeReferenceSetting(CollectionReference.class, "singleReference", true, none, none, false);
581         ojbChangeReferenceSetting(SingleReference.class, "mainObject", true, none, none, false);
582
583         long timestamp = System.currentTimeMillis();
584         String JavaDoc name = "testReferenceInsert_main_" + timestamp;
585         String JavaDoc nameRef = "testReferenceInsert_reference_" + timestamp;
586         String JavaDoc nameSingleRef = "testReferenceInsert_single_reference_" + timestamp;
587
588         MainObject obj_2 = new MainObject(null, name);
589         SingleReference s_ref_4 = new SingleReference(nameSingleRef);
590         obj_2.setSingleReference(s_ref_4);
591
592         Implementation odmg = OJB.getInstance();
593         Database db = odmg.newDatabase();
594         db.open(TestHelper.DEF_DATABASE_NAME, Database.OPEN_READ_WRITE);
595
596         TransactionExt tx = (TransactionExt) odmg.newTransaction();
597         tx.begin();
598         db.makePersistent(s_ref_4);
599         db.makePersistent(obj_2);
600         tx.commit();
601
602         tx.begin();
603
604         // try to find object
605
Criteria crit = new Criteria();
606         crit.addEqualTo("name", name);
607         QueryByCriteria query = QueryFactory.newQuery(MainObject.class, crit);
608
609         int result = tx.getBroker().getCount(query);
610         assertEquals("Wrong object count", 1, result);
611         // pk have to set and have to be different
612
assertNotNull(obj_2.getIdentifier());
613         assertTrue(obj_2.getIdentifier().longValue() > 0);
614         // no collection reference set
615
List JavaDoc references = obj_2.getAllReferences();
616         assertTrue(references == null || references.size() == 0);
617         // get Identity objects
618
Identity oid_2 = tx.getBroker().serviceIdentity().buildIdentity(obj_2);
619         // get identifier (PK) values
620
Long JavaDoc id_2 = obj_2.getIdentifier();
621
622         tx.getBroker().clearCache();
623         obj_2 = (MainObject) tx.getBroker().getObjectByIdentity(oid_2);
624
625         assertTrue(obj_2.getIdentifier().longValue() > 0);
626         assertNotNull(obj_2.getSingleReference());
627         assertTrue(obj_2.getSingleReference().getId().longValue() > 0);
628         // no collection reference set
629
references = obj_2.getAllReferences();
630         assertTrue(references == null || references.size() == 0);
631
632         tx.getBroker().clearCache();
633         // get references only
634
Criteria crit_2 = new Criteria();
635         crit_2.addEqualTo("refName", nameRef);
636         QueryByCriteria query_2 = QueryFactory.newQuery(CollectionReference.class, crit_2);
637         int result_2 = tx.getBroker().getCount(query_2);
638
639         assertEquals(0, result_2);
640
641         tx.getBroker().clearCache();
642         // get object
643
MainObject retObj = (MainObject) tx.getBroker().getObjectByIdentity(oid_2);
644
645         List JavaDoc refList = retObj.getAllReferences();
646         assertNotNull(refList);
647         assertEquals("object do not have references", 0, refList.size());
648         tx.commit();
649
650         // add new reference to object
651
CollectionReference ref_6 = new CollectionReference(null, "###_new_" + nameRef);
652         tx.begin();
653         tx.lock(obj_2, Transaction.WRITE);
654         obj_2.addReference(ref_6);
655         tx.commit();
656
657         references = obj_2.getAllReferences();
658         assertNotNull(references);
659         assertEquals("1 references expected for object: "+obj_2, 1, references.size());
660
661
662         assertNotNull(ref_6.getRefIdentifier());
663         // check FK setting
664
Long JavaDoc fk = ref_6.getFkIdentifier();
665         assertNotNull(fk);
666         assertEquals(obj_2.getIdentifier(), fk);
667         assertEquals(id_2, obj_2.getIdentifier());
668         references = obj_2.getAllReferences();
669         assertNotNull(references);
670         assertEquals("1 references expected for object: "+obj_2, 1, references.size());
671         assertNotNull(references);
672
673         tx.begin();
674         obj_2 = (MainObject) tx.getBroker().getObjectByIdentity(oid_2);
675         // we don't change the main object, only add an reference, so the
676
// cached version of the object isn't up to date
677
tx.getBroker().retrieveAllReferences(obj_2);
678         tx.commit();
679
680         assertNotNull(obj_2);
681         references = obj_2.getAllReferences();
682         assertNotNull(references);
683         assertEquals("Reference expected for object", 1, references.size());
684
685         assertEquals(id_2, obj_2.getIdentifier());
686
687         // now update main objects
688
tx.begin();
689         tx.lock(obj_2, Transaction.WRITE);
690         obj_2.setName(name+"_update");
691         tx.commit();
692
693         broker = PersistenceBrokerFactory.defaultPersistenceBroker();
694         obj_2 = (MainObject) broker.getObjectByIdentity(oid_2);
695         broker.close();
696
697         assertNotNull(obj_2);
698         assertEquals(obj_2.getName(), name+"_update");
699         assertEquals(id_2, obj_2.getIdentifier());
700
701         broker = PersistenceBrokerFactory.defaultPersistenceBroker();
702         obj_2 = (MainObject) broker.getObjectByIdentity(oid_2);
703         broker.close();
704
705         // now update reference
706
assertNotNull(obj_2);
707         tx.begin();
708         tx.lock(obj_2, Transaction.WRITE);
709         references = obj_2.getAllReferences();
710         CollectionReference ref = (CollectionReference) references.get(0);
711         tx.lock(ref, Transaction.WRITE);
712         ref.setRefName(nameRef+"_update");
713         tx.commit();
714
715         broker = PersistenceBrokerFactory.defaultPersistenceBroker();
716         obj_2 = (MainObject) broker.getObjectByIdentity(oid_2);
717         assertNotNull(obj_2);
718         references = obj_2.getAllReferences();
719         ref = (CollectionReference) references.get(0);
720         assertEquals(nameRef+"_update", ref.getRefName());
721         assertEquals(id_2, obj_2.getIdentifier());
722     }
723
724     /**
725      * critical test case, because single broker instance (PB-api) is concurrent used
726      * with the ODMG-api, take care of caches
727      */

728     public void testReferenceInsertUpdateODMG_2() throws Exception JavaDoc
729     {
730         if (skipTest()) return;
731
732         // prepare metadata for odmg-api
733
int none = ObjectReferenceDescriptor.CASCADE_NONE;
734         ojbChangeReferenceSetting(MainObject.class, "singleReference", true, none, none, false);
735         ojbChangeReferenceSetting(MainObject.class, "allReferences", true, none, none, false);
736         ojbChangeReferenceSetting(CollectionReference.class, "singleReference", true, none, none, false);
737         ojbChangeReferenceSetting(SingleReference.class, "mainObject", true, none, none, false);
738
739         long timestamp = System.currentTimeMillis();
740         String JavaDoc name = "testReferenceInsert_main_" + timestamp;
741         String JavaDoc nameRef = "testReferenceInsert_reference_" + timestamp;
742         String JavaDoc nameSingleRef = "testReferenceInsert_single_reference_" + timestamp;
743
744         MainObject obj_1 = new MainObject(null, name);
745         MainObject obj_2 = new MainObject(null, name);
746
747         SingleReference s_ref_1 = new SingleReference(nameSingleRef);
748         SingleReference s_ref_2 = new SingleReference(nameSingleRef);
749
750         CollectionReference ref_1 = new CollectionReference(null, nameRef);
751         CollectionReference ref_2 = new CollectionReference(null, nameRef);
752         CollectionReference ref_3 = new CollectionReference(null, nameRef);
753         CollectionReference ref_4 = new CollectionReference(null, nameRef);
754         ref_1.setSingleReference(s_ref_1);
755         ref_4.setSingleReference(s_ref_2);
756
757         SingleReference s_ref_3 = new SingleReference(nameSingleRef);
758         SingleReference s_ref_4 = new SingleReference(nameSingleRef);
759
760         obj_1.addReference(ref_1);
761         obj_1.addReference(ref_2);
762         obj_1.addReference(ref_3);
763         obj_1.addReference(ref_4);
764
765         obj_1.setSingleReference(s_ref_3);
766         obj_2.setSingleReference(s_ref_4);
767
768         Implementation odmg = OJB.getInstance();
769         Database db = odmg.newDatabase();
770         db.open(TestHelper.DEF_DATABASE_NAME, Database.OPEN_READ_WRITE);
771
772         Transaction tx = odmg.newTransaction();
773         tx.begin();
774         db.makePersistent(s_ref_1);
775         db.makePersistent(s_ref_2);
776         db.makePersistent(s_ref_3);
777         db.makePersistent(s_ref_4);
778
779         db.makePersistent(obj_1);
780         db.makePersistent(obj_2);
781         tx.commit();
782
783         // try to find both objects
784
Criteria crit = new Criteria();
785         crit.addEqualTo("name", name);
786         QueryByCriteria query = QueryFactory.newQuery(MainObject.class, crit);
787         int result = broker.getCount(query);
788         assertEquals("Wrong object count", 2, result);
789
790         // pk have to set and have to be different
791
assertNotNull(obj_1.getIdentifier());
792         assertNotNull(obj_2.getIdentifier());
793         assertNotSame(obj_1.getIdentifier(), obj_2.getIdentifier());
794         assertTrue(obj_1.getIdentifier().longValue() > 0);
795         assertTrue(obj_2.getIdentifier().longValue() > 0);
796         assertTrue(s_ref_3.getId().longValue() > 0);
797         assertTrue(ref_3.getRefIdentifier().longValue() > 0);
798
799         // no collection reference set
800
List JavaDoc references = obj_2.getAllReferences();
801         assertTrue(references == null || references.size() == 0);
802         // check anonymous FK setting
803
Long JavaDoc fk = (Long JavaDoc) broker.getClassDescriptor(MainObject.class)
804                 .getFieldDescriptorByName("refFK")
805                 .getPersistentField().get(obj_1);
806         assertTrue("The assigned FK should be > 0 after store of main object, but was " + fk.longValue(), fk.longValue() > 0);
807
808         // get Identity objects
809
Identity oid_1 = new Identity(obj_1, broker);
810         Identity oid_2 = new Identity(obj_2, broker);
811         // get identifier (PK) values
812
Long JavaDoc id_1 = obj_1.getIdentifier();
813         Long JavaDoc id_2 = obj_2.getIdentifier();
814
815         broker.clearCache();
816
817         // get object with references
818
obj_1 = (MainObject) broker.getObjectByIdentity(oid_1);
819         assertNotNull(obj_1);
820         references = obj_1.getAllReferences();
821         assertNotNull(references);
822         assertEquals("4 references expected for object: "+obj_1, 4, references.size());
823         Iterator JavaDoc it = references.iterator();
824         while (it.hasNext())
825         {
826             CollectionReference ref = (CollectionReference) it.next();
827             assertEquals("Main object fk expected", obj_1.getIdentifier(), ref.fkIdentifier);
828             assertTrue("We expect a positive value, identity columns have to start > 0",
829                     (ref.getRefIdentifier().longValue() > 0));
830         }
831         assertNotNull(obj_1.getSingleReference());
832         obj_2 = (MainObject) broker.getObjectByIdentity(oid_2);
833         assertTrue(obj_1.getIdentifier().longValue() > 0);
834         assertTrue(obj_2.getIdentifier().longValue() > 0);
835         assertNotNull(obj_2.getSingleReference());
836         assertTrue(obj_2.getSingleReference().getId().longValue() > 0);
837         assertTrue(obj_1.getSingleReference().getId().longValue() > 0);
838         assertNotSame(obj_1.getSingleReference(), obj_2.getSingleReference());
839         // no collection reference set
840
references = obj_2.getAllReferences();
841         assertTrue(references == null || references.size() == 0);
842         broker.clearCache();
843
844         // get references only
845
Criteria crit_2 = new Criteria();
846         crit_2.addEqualTo("refName", nameRef);
847         QueryByCriteria query_2 = QueryFactory.newQuery(CollectionReference.class, crit_2);
848         int result_2 = broker.getCount(query_2);
849         assertEquals("Not all objects created", 4, result_2);
850         assertNotNull(ref_3.getRefIdentifier());
851
852         broker.clearCache();
853
854         // get second object
855
MainObject retObj = (MainObject) broker.getObjectByIdentity(oid_2);
856         List JavaDoc refList = retObj.getAllReferences();
857         assertNotNull(refList);
858         assertEquals("object do not have references", 0, refList.size());
859
860         // add new reference to object
861
CollectionReference ref_5 = new CollectionReference(null, "##new ref 1_" + nameRef);
862         CollectionReference ref_6 = new CollectionReference(null, "##new ref 2_" + nameRef);
863         tx.begin();
864         tx.lock(obj_1, Transaction.WRITE);
865         tx.lock(obj_2, Transaction.WRITE);
866         obj_1.addReference(ref_5);
867         obj_2.addReference(ref_6);
868         references = obj_2.getAllReferences();
869         assertNotNull(references);
870         assertEquals("1 references expected for object: "+obj_2, 1, references.size());
871         tx.commit();
872
873         assertNotNull(ref_5.getRefIdentifier());
874         assertNotNull(ref_6.getRefIdentifier());
875         // check FK setting
876
fk = ref_5.getFkIdentifier();
877         assertNotNull(fk);
878         assertEquals(obj_1.getIdentifier(), fk);
879         fk = ref_6.getFkIdentifier();
880         assertNotNull(fk);
881         assertEquals(obj_2.getIdentifier(), fk);
882         assertEquals(id_1, obj_1.getIdentifier());
883         assertEquals(id_2, obj_2.getIdentifier());
884         references = obj_2.getAllReferences();
885         assertNotNull(references);
886         assertEquals("1 references expected for object: "+obj_2, 1, references.size());
887
888         // refresh used broker instance to avoid problems with session cache (when used)
889
broker.close();
890         broker = PersistenceBrokerFactory.defaultPersistenceBroker();
891
892         obj_1 = (MainObject) broker.getObjectByIdentity(oid_1);
893         assertNotNull(obj_1);
894         references = obj_1.getAllReferences();
895         assertNotNull(references);
896         assertEquals("5 references expected for object: "+obj_1, 5, references.size());
897
898         // we don't change the main object, only add an reference, so the
899
// cached version of the object isn't up to date. So we have to retrieve
900
// all referenced objects to make it work with all cache implementations
901
// or evict the whole cache instead
902
// broker.clearCache();
903
obj_2 = (MainObject) broker.getObjectByIdentity(oid_2);
904         broker.retrieveAllReferences(obj_2);
905         assertNotNull(obj_2);
906         references = obj_2.getAllReferences();
907         assertNotNull(references);
908         assertEquals("1 references expected for object: "+obj_2, 1, references.size());
909
910         assertEquals(id_1, obj_1.getIdentifier());
911         assertEquals(id_2, obj_2.getIdentifier());
912
913         // now update main objects
914
tx.begin();
915         tx.lock(obj_1, Transaction.WRITE);
916         tx.lock(obj_2, Transaction.WRITE);
917         obj_1.setName(name+"_update");
918         obj_2.setName(name+"_update");
919         tx.commit();
920
921         obj_1 = (MainObject) broker.getObjectByIdentity(oid_1);
922         obj_2 = (MainObject) broker.getObjectByIdentity(oid_2);
923
924         assertNotNull(obj_1);
925         assertNotNull(obj_2);
926         assertEquals(obj_1.getName(), name+"_update");
927         assertEquals(obj_2.getName(), name+"_update");
928         assertEquals(id_1, obj_1.getIdentifier());
929         assertEquals(id_2, obj_2.getIdentifier());
930
931         // now update reference
932
obj_2 = (MainObject) broker.getObjectByIdentity(oid_2);
933         assertNotNull(obj_2);
934         tx.begin();
935         tx.lock(obj_2, Transaction.WRITE);
936         references = obj_2.getAllReferences();
937         CollectionReference ref = (CollectionReference) references.get(0);
938         tx.lock(ref, Transaction.WRITE);
939         ref.setRefName(nameRef+"_update");
940         tx.commit();
941
942         obj_2 = (MainObject) broker.getObjectByIdentity(oid_2);
943         assertNotNull(obj_2);
944         references = obj_2.getAllReferences();
945         ref = (CollectionReference) references.get(0);
946         assertEquals(nameRef+"_update", ref.getRefName());
947         assertEquals(id_1, obj_1.getIdentifier());
948         assertEquals(id_2, obj_2.getIdentifier());
949     }
950
951     public void testDelete_1() throws Exception JavaDoc
952     {
953         // prepare for PB-api test
954
ojbChangeReferenceSetting(MainObject.class, "singleReference", true, true, true, false);
955         ojbChangeReferenceSetting(MainObject.class, "allReferences", true, true, true, false);
956         ojbChangeReferenceSetting(CollectionReference.class, "singleReference", true, true, true, false);
957         ojbChangeReferenceSetting(SingleReference.class, "mainObject", true, true, true, false);
958         doTestDelete();
959     }
960
961     public void testDelete_2() throws Exception JavaDoc
962     {
963         // prepare for PB-api test
964
ojbChangeReferenceSetting(MainObject.class, "singleReference", true, true, true, true);
965         ojbChangeReferenceSetting(MainObject.class, "allReferences", true, true, true, true);
966         ojbChangeReferenceSetting(CollectionReference.class, "singleReference", true, true, true, true);
967         ojbChangeReferenceSetting(SingleReference.class, "mainObject", true, true, true, true);
968         doTestDelete();
969     }
970
971     public void doTestDelete() throws Exception JavaDoc
972     {
973         if (skipTest()) return;
974
975         long timestamp = System.currentTimeMillis();
976         String JavaDoc name = "testDelete_main_" + timestamp;
977         String JavaDoc nameRef = "testDelete_reference_" + timestamp;
978
979         MainObject obj_1 = new MainObject(null, name);
980
981         CollectionReference ref_1 = new CollectionReference(null, nameRef);
982         CollectionReference ref_2 = new CollectionReference(null, nameRef);
983
984         obj_1.addReference(ref_1);
985         obj_1.addReference(ref_2);
986         broker.beginTransaction();
987         broker.store(obj_1);
988         broker.commitTransaction();
989         Identity oid_1 = new Identity(obj_1, broker);
990
991         MainObject result = (MainObject) broker.getObjectByIdentity(oid_1);
992         assertNotNull(result);
993         assertNotNull(result.getAllReferences());
994         assertEquals(2, result.getAllReferences().size());
995         Long JavaDoc fk = ((CollectionReference) result.getAllReferences().get(0)).getFkIdentifier();
996         assertNotNull(result.getIdentifier());
997         assertEquals(result.getIdentifier(), fk);
998
999         broker.beginTransaction();
1000        broker.delete(obj_1);
1001        broker.commitTransaction();
1002
1003        result = (MainObject) broker.getObjectByIdentity(oid_1);
1004        assertNull(result);
1005        Criteria crit_2 = new Criteria();
1006        crit_2.addEqualTo("refName", nameRef);
1007        QueryByCriteria query_2 = QueryFactory.newQuery(CollectionReference.class, crit_2);
1008        int result_2 = broker.getCount(query_2);
1009        assertEquals(0, result_2);
1010    }
1011
1012    public void testDeleteTwo_1() throws Exception JavaDoc
1013    {
1014        // prepare for PB-api test
1015
ojbChangeReferenceSetting(MainObject.class, "singleReference", true, true, true, false);
1016        ojbChangeReferenceSetting(MainObject.class, "allReferences", true, true, true, false);
1017        ojbChangeReferenceSetting(CollectionReference.class, "singleReference", true, true, true, false);
1018        ojbChangeReferenceSetting(SingleReference.class, "mainObject", true, true, true, false);
1019        doTestDeleteTwo();
1020    }
1021
1022    public void testDeleteTwo_2() throws Exception JavaDoc
1023    {
1024        // prepare for PB-api test
1025
ojbChangeReferenceSetting(MainObject.class, "singleReference", true, true, true, true);
1026        ojbChangeReferenceSetting(MainObject.class, "allReferences", true, true, true, true);
1027        ojbChangeReferenceSetting(CollectionReference.class, "singleReference", true, true, true, true);
1028        ojbChangeReferenceSetting(SingleReference.class, "mainObject", true, true, true, true);
1029        doTestDeleteTwo();
1030    }
1031
1032    public void doTestDeleteTwo() throws Exception JavaDoc
1033    {
1034        if (skipTest()) return;
1035        long timestamp = System.currentTimeMillis();
1036        String JavaDoc name = "testDeleteTwo_main_" + timestamp;
1037        String JavaDoc nameRef = "testDeleteTwo_reference_" + timestamp;
1038
1039        MainObject obj_1 = new MainObject(null, name);
1040
1041        CollectionReference ref_1 = new CollectionReference(null, nameRef);
1042        CollectionReference ref_2 = new CollectionReference(null, nameRef);
1043
1044        obj_1.addReference(ref_1);
1045        obj_1.addReference(ref_2);
1046
1047        // chaotic operations
1048
broker.beginTransaction();
1049        // System.out.println("1. "+obj_1);
1050
broker.store(obj_1);
1051        // System.out.println("2. "+obj_1);
1052
broker.delete(obj_1);
1053        // System.out.println("3. "+obj_1);
1054
broker.store(obj_1);
1055        // System.out.println("4. "+obj_1);
1056
broker.delete(obj_1);
1057        // System.out.println("5. "+obj_1);
1058
broker.store(obj_1);
1059        // System.out.println("6. "+obj_1);
1060
broker.delete(obj_1);
1061        // System.out.println("7. "+obj_1);
1062
broker.store(obj_1);
1063        // System.out.println("8. "+obj_1);
1064
broker.commitTransaction();
1065        Identity oid_1 = new Identity(obj_1, broker);
1066
1067        MainObject result = (MainObject) broker.getObjectByIdentity(oid_1);
1068        assertNotNull(result);
1069        assertNotNull(result.getAllReferences());
1070        assertEquals(2, result.getAllReferences().size());
1071        Long JavaDoc fk = ((CollectionReference) result.getAllReferences().get(0)).getFkIdentifier();
1072        assertNotNull(result.getIdentifier());
1073        assertEquals(result.getIdentifier(), fk);
1074
1075        // we should find exactly one object
1076
Criteria c = new Criteria();
1077        c.addEqualTo("name", name);
1078        QueryByCriteria q = QueryFactory.newQuery(MainObject.class, c);
1079        Collection JavaDoc col = broker.getCollectionByQuery(q);
1080        assertNotNull(col);
1081        assertEquals(1, col.size());
1082
1083        broker.beginTransaction();
1084        broker.delete(obj_1);
1085        broker.commitTransaction();
1086
1087        result = (MainObject) broker.getObjectByIdentity(oid_1);
1088        assertNull(result);
1089        Criteria crit_2 = new Criteria();
1090        crit_2.addEqualTo("refName", nameRef);
1091        QueryByCriteria query_2 = QueryFactory.newQuery(CollectionReference.class, crit_2);
1092        int result_2 = broker.getCount(query_2);
1093        assertEquals(0, result_2);
1094    }
1095
1096// public void testAllInOne() throws Exception
1097
// {
1098
// // sleep thread to make timestamp based tests work
1099
// testSimpleInsert();
1100
// ojbSleep();
1101
// ojbSleep();
1102
// testSimpleInsert();
1103
// ojbSleep();
1104
// ojbSleep();
1105
// testReferenceInsertUpdate();
1106
// ojbSleep();
1107
// ojbSleep();
1108
// // testReferenceInsertUpdate_2();
1109
// ojbSleep();
1110
// ojbSleep();
1111
// testReferenceInsertUpdate();
1112
// }
1113

1114// void ojbChangeReferenceSetting(Class clazz, String referenceField, boolean autoRetrieve, int autoUpdate, int autoDelete, boolean useProxy)
1115
// {
1116
// ClassDescriptor cld = broker.getClassDescriptor(clazz);
1117
// ObjectReferenceDescriptor ref = cld.getCollectionDescriptorByName(referenceField);
1118
// if(ref == null) ref = cld.getObjectReferenceDescriptorByName(referenceField);
1119
// ref.setLazy(useProxy);
1120
// ref.setCascadeRetrieve(autoRetrieve);
1121
// ref.setCascadingStore(autoUpdate);
1122
// ref.setCascadingDelete(autoDelete);
1123
// }
1124
//
1125
// void ojbChangeReferenceSetting(Class clazz, String referenceField, boolean autoRetrieve, boolean autoUpdate, boolean autoDelete, boolean useProxy)
1126
// {
1127
// ClassDescriptor cld = broker.getClassDescriptor(clazz);
1128
// ObjectReferenceDescriptor ref = cld.getCollectionDescriptorByName(referenceField);
1129
// if(ref == null) ref = cld.getObjectReferenceDescriptorByName(referenceField);
1130
// ref.setLazy(useProxy);
1131
// ref.setCascadeRetrieve(autoRetrieve);
1132
// ref.setCascadeStore(autoUpdate);
1133
// ref.setCascadeDelete(autoDelete);
1134
// }
1135

1136
1137    //========================================================================
1138
// inner classes, used for test
1139
//========================================================================
1140

1141    public static interface MainObjectIF extends Serializable JavaDoc
1142    {
1143        public SingleReferenceIF getSingleReference();
1144        public void setSingleReference(SingleReferenceIF singleReference);
1145        public List JavaDoc getAllReferences();
1146        public void addReference(CollectionReference reference);
1147        public void setAllReferences(List JavaDoc allReferences);
1148        public Long JavaDoc getIdentifier();
1149        public void setIdentifier(Long JavaDoc identifier);
1150        public String JavaDoc getName();
1151        public void setName(String JavaDoc name);
1152    }
1153
1154    public static class MainObject implements MainObjectIF
1155    {
1156        private Long JavaDoc identifier;
1157        private String JavaDoc name;
1158        private List JavaDoc allReferences;
1159        // we use anonymous field for FK
1160
private SingleReferenceIF singleReference;
1161
1162
1163        public MainObject()
1164        {
1165        }
1166
1167        public MainObject(Long JavaDoc identifier, String JavaDoc name)
1168        {
1169            this.identifier = identifier;
1170            this.name = name;
1171        }
1172
1173        public SingleReferenceIF getSingleReference()
1174        {
1175            return singleReference;
1176        }
1177
1178        public void setSingleReference(SingleReferenceIF singleReference)
1179        {
1180            this.singleReference = singleReference;
1181        }
1182
1183        public List JavaDoc getAllReferences()
1184        {
1185            return allReferences;
1186        }
1187
1188        public void addReference(CollectionReference reference)
1189        {
1190            if (allReferences == null)
1191            {
1192                allReferences = new ArrayList JavaDoc();
1193            }
1194            allReferences.add(reference);
1195        }
1196
1197        public void setAllReferences(List JavaDoc allReferences)
1198        {
1199            this.allReferences = allReferences;
1200        }
1201
1202        public Long JavaDoc getIdentifier()
1203        {
1204            return identifier;
1205        }
1206
1207        public void setIdentifier(Long JavaDoc identifier)
1208        {
1209            this.identifier = identifier;
1210        }
1211
1212        public String JavaDoc getName()
1213        {
1214            return name;
1215        }
1216
1217        public void setName(String JavaDoc name)
1218        {
1219            this.name = name;
1220        }
1221
1222        public String JavaDoc toString()
1223        {
1224            return new ToStringBuilder(this).append("identifier", identifier).append("name", name)
1225                    .append("allReferences", allReferences != null ? allReferences.toString() : "null")
1226                    .append("singleReference", singleReference.getClass().toString()).toString();
1227        }
1228    }
1229
1230    public static interface SingleReferenceIF extends Serializable JavaDoc
1231    {
1232        public MainObjectIF getMainObject();
1233        public void setMainObject(MainObjectIF mainObject);
1234        public Long JavaDoc getId();
1235        public void setId(Long JavaDoc id);
1236        public String JavaDoc getName();
1237        public void setName(String JavaDoc name);
1238    }
1239
1240    public static class SingleReference implements SingleReferenceIF
1241    {
1242        Long JavaDoc id;
1243        String JavaDoc name;
1244        String JavaDoc ojbConcreteClass;
1245        MainObjectIF mainObject;
1246
1247        public SingleReference()
1248        {
1249            this(null);
1250        }
1251
1252        public SingleReference(String JavaDoc name)
1253        {
1254            this.name = name;
1255            ojbConcreteClass = SingleReference.class.getName();
1256            // id = new Long((long)(Math.random() * Integer.MAX_VALUE));
1257
}
1258
1259        public MainObjectIF getMainObject()
1260        {
1261            return mainObject;
1262        }
1263
1264        public void setMainObject(MainObjectIF mainObject)
1265        {
1266            this.mainObject = mainObject;
1267        }
1268
1269        public Long JavaDoc getId()
1270        {
1271            return id;
1272        }
1273
1274        public void setId(Long JavaDoc id)
1275        {
1276            this.id = id;
1277        }
1278
1279        public String JavaDoc getName()
1280        {
1281            return name;
1282        }
1283
1284        public void setName(String JavaDoc name)
1285        {
1286            this.name = name;
1287        }
1288
1289        public String JavaDoc toString()
1290        {
1291            return new ToStringBuilder(this).append("id", id).append("name", name)
1292                    .append("mainObject", mainObject != null ? mainObject.getClass().toString() : "null").toString();
1293        }
1294    }
1295
1296    public static interface CollectionReferenceIF extends Serializable JavaDoc
1297    {
1298        public Long JavaDoc getRefIdentifier();
1299        public void setRefIdentifier(Long JavaDoc refIdentifier);
1300        public SingleReferenceIF getSingleReference();
1301        public void setSingleReference(SingleReferenceIF singleReference);
1302        public Long JavaDoc getFkIdentifier();
1303        public void setFkIdentifier(Long JavaDoc fkIdentifier);
1304        public String JavaDoc getRefName();
1305        public void setRefName(String JavaDoc refName);
1306    }
1307
1308    public static class CollectionReference implements CollectionReferenceIF
1309    {
1310        private Long JavaDoc refIdentifier;
1311        private String JavaDoc refName;
1312        private Long JavaDoc fkIdentifier;
1313        String JavaDoc ojbConcreteClass;
1314        private SingleReferenceIF singleReference;
1315
1316        public CollectionReference()
1317        {
1318            ojbConcreteClass = CollectionReference.class.getName();
1319        }
1320
1321        public CollectionReference(Long JavaDoc refIdentifier, String JavaDoc refName)
1322        {
1323            this();
1324            this.refIdentifier = refIdentifier;
1325            this.refName = refName;
1326        }
1327
1328        public Long JavaDoc getRefIdentifier()
1329        {
1330            return refIdentifier;
1331        }
1332
1333        public void setRefIdentifier(Long JavaDoc refIdentifier)
1334        {
1335            this.refIdentifier = refIdentifier;
1336        }
1337
1338        public SingleReferenceIF getSingleReference()
1339        {
1340            return singleReference;
1341        }
1342
1343        public void setSingleReference(SingleReferenceIF singleReference)
1344        {
1345            this.singleReference = singleReference;
1346        }
1347
1348        public Long JavaDoc getFkIdentifier()
1349        {
1350            return fkIdentifier;
1351        }
1352
1353        public void setFkIdentifier(Long JavaDoc fkIdentifier)
1354        {
1355            this.fkIdentifier = fkIdentifier;
1356        }
1357
1358        public String JavaDoc getRefName()
1359        {
1360            return refName;
1361        }
1362
1363        public void setRefName(String JavaDoc refName)
1364        {
1365            this.refName = refName;
1366        }
1367
1368        public String JavaDoc toString()
1369        {
1370            return new ToStringBuilder(this).append("id", refIdentifier).append("name", refName)
1371                    .append("fkIdentifier", fkIdentifier)
1372                    .append("singleReference", singleReference != null ? singleReference.toString() : "null")
1373                    .toString();
1374        }
1375    }
1376}
1377
Popular Tags