KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > odmg > M2NTest


1 package org.apache.ojb.odmg;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Collection JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.LinkedList JavaDoc;
7 import java.util.List JavaDoc;
8
9 import org.apache.commons.lang.StringUtils;
10 import org.apache.commons.lang.builder.EqualsBuilder;
11 import org.apache.commons.lang.builder.ToStringBuilder;
12 import org.apache.ojb.broker.ManageableCollection;
13 import org.apache.ojb.broker.PersistenceBroker;
14 import org.apache.ojb.broker.PersistenceBrokerException;
15 import org.apache.ojb.broker.OJBRuntimeException;
16 import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
17 import org.apache.ojb.broker.query.Criteria;
18 import org.apache.ojb.broker.query.Query;
19 import org.apache.ojb.broker.query.QueryFactory;
20 import org.apache.ojb.junit.ODMGTestCase;
21 import org.odmg.OQLQuery;
22 import org.odmg.QueryException;
23 import org.odmg.Transaction;
24
25 /**
26  * Test m:n relation handling with the odmg-api. The mandatory auto-update/auto-delete
27  * setting are 'false' in that case equals to 'LINK'.
28  * <p/>
29  * TODO: we need more tests doing delete/update operations on M:N relations
30  *
31  * @author <a HREF="mailto:arminw@apache.org">Armin Waibel</a>
32  * @version $Id: M2NTest.java,v 1.3.2.6 2005/06/04 17:35:19 arminw Exp $
33  */

34 public class M2NTest extends ODMGTestCase
35 {
36     static final int NONE = ObjectReferenceDescriptor.CASCADE_NONE;
37     static final int LINK = ObjectReferenceDescriptor.CASCADE_LINK;
38     static final int OBJECT = ObjectReferenceDescriptor.CASCADE_OBJECT;
39     boolean oldImpliciteWriteLock;
40
41     public static void main(String JavaDoc[] args)
42     {
43         junit.textui.TestRunner.main(new String JavaDoc[]{M2NTest.class.getName()});
44     }
45
46     protected void setUp() throws Exception JavaDoc
47     {
48         super.setUp();
49         oldImpliciteWriteLock = odmg.isImpliciteWriteLocks();
50     }
51
52     protected void tearDown() throws Exception JavaDoc
53     {
54         odmg.setImpliciteWriteLocks(oldImpliciteWriteLock);
55         super.tearDown();
56     }
57
58     public void testOnetoNViaIndirectionTable() throws Exception JavaDoc
59     {
60         String JavaDoc name = "testOnetoNViaIndirectionTable_" + System.currentTimeMillis();
61         int opId = (int) (Math.random() * Integer.MAX_VALUE);
62         String JavaDoc cId = "m2n_" + opId;
63
64         // explicit set false
65
odmg.setImpliciteWriteLocks(false);
66
67         // Make a profile and County
68
TransactionExt tx = (TransactionExt) odmg.newTransaction();
69         tx.begin();
70         OfficeProfile profile = new OfficeProfileImpl();
71         profile.setId(opId);
72         County c = new County();
73         c.setName(name);
74         c.setId(cId);
75         odmg.getDatabase(null).makePersistent(profile);
76         odmg.getDatabase(null).makePersistent(c);
77         tx.commit();
78
79         tx.begin();
80         PersistenceBroker pb = tx.getBroker();
81         pb.clearCache();
82         // use PB-api to lookup object
83
Criteria crit = new Criteria();
84         crit.addLike("name", name);
85         Query q = QueryFactory.newQuery(County.class, crit);
86         County county = (County) pb.getObjectByQuery(q);
87         tx.commit();
88
89         // Add a county to it
90
tx = (TransactionExt) odmg.newTransaction();
91         tx.begin();
92         tx.lock(profile, Transaction.WRITE);
93         tx.lock(county, Transaction.READ);
94         // add already persisted County object
95
profile.addCounty(county);
96         // add a new County object
97
County c2 = new County();
98         c2.setId(cId + "_new");
99         c2.setName(name + "_new");
100         profile.addCounty(c2);
101         tx.commit();
102
103         tx.begin();
104         OQLQuery query = odmg.newOQLQuery();
105         query.create("select all from " + OfficeProfile.class.getName() + " where id=$1");
106         query.bind(new Integer JavaDoc(opId));
107         List JavaDoc result = (List JavaDoc) query.execute();
108         assertEquals(1, result.size());
109         tx.commit();
110         OfficeProfile newProfile = (OfficeProfile) result.get(0);
111         assertTrue(profile.equals(newProfile));
112
113         tx.begin();
114         tx.lock(newProfile, Transaction.WRITE);
115         List JavaDoc counties = newProfile.getCounties();
116         for(int i = 0; i < counties.size(); i++)
117         {
118             County tmp = (County) counties.get(i);
119             tmp.setName(tmp.getName() + "_updated");
120         }
121         tx.commit();
122
123         tx.begin();
124         query = odmg.newOQLQuery();
125         query.create("select all from " + County.class.getName() + " where name like $1");
126         query.bind(name + "%");
127         result = (List JavaDoc) query.execute();
128         assertEquals(2, result.size());
129         tx.commit();
130         for(int i = 0; i < counties.size(); i++)
131         {
132             County tmp = (County) counties.get(i);
133             assertTrue(StringUtils.contains(tmp.getName(), "_updated"));
134         }
135
136         tx.begin();
137         tx.lock(newProfile, Transaction.WRITE);
138         counties = newProfile.getCounties();
139         // this only remove the indirection table entry
140
County removed = (County) counties.remove(0);
141         // update the "unlinked" object
142
removed.setName(removed.getName() + "_unlinked");
143         tx.commit();
144
145         tx.begin();
146         query = odmg.newOQLQuery();
147         query.create("select all from " + County.class.getName() + " where name=$1");
148         query.bind(removed.getName());
149         result = (List JavaDoc) query.execute();
150         assertEquals(1, result.size());
151         tx.commit();
152
153         tx.begin();
154         tx.setCascadingDelete(OfficeProfileImpl.class, true);
155         database.deletePersistent(newProfile);
156         tx.commit();
157
158         tx.begin();
159         query = odmg.newOQLQuery();
160         query.create("select all from " + County.class.getName() + " where name like $1");
161         query.bind(name + "%");
162         result = (List JavaDoc) query.execute();
163         // expect to find the unlinked County object
164
assertEquals(1, result.size());
165         tx.commit();
166     }
167
168     public void testTwoMtoNRelationsInOneClass() throws Exception JavaDoc
169     {
170         String JavaDoc postfixId = "testTwoMtoNRelationsInOneClass_" + System.currentTimeMillis();
171         Movie m = new MovieImpl(postfixId, "Movie_" + postfixId, "none");
172
173         Actor a1 = new Actor("Actor_1_" + postfixId);
174         m.addActors(a1);
175
176         Actor a2a = new Actor("Actor_2a_" + postfixId);
177         m.addActors2(a2a);
178         Actor a2b = new Actor("Actor_2b_" + postfixId);
179         m.addActors2(a2b);
180
181         TransactionExt tx = (TransactionExt) odmg.newTransaction();
182         tx.begin();
183         database.makePersistent(m);
184         tx.commit();
185
186         tx.begin();
187         tx.getBroker().clearCache();
188
189         OQLQuery queryMovie = movieQuery(postfixId);
190         Collection JavaDoc resultMovie = (Collection JavaDoc) queryMovie.execute();
191         assertEquals(1, resultMovie.size());
192         Movie newMovie = (Movie) resultMovie.iterator().next();
193         assertNotNull(newMovie.getActors());
194         assertEquals(1, newMovie.getActors().size());
195         assertEquals(2, newMovie.getActors2().size());
196
197         OQLQuery queryActor = actorQuery(postfixId);
198         Collection JavaDoc resultActor = (Collection JavaDoc) queryActor.execute();
199         assertEquals(1 + 2, resultActor.size());
200
201         OQLQuery queryRole = roleQuery(null, m);
202         Collection JavaDoc resultRole = (Collection JavaDoc) queryRole.execute();
203         assertEquals(1, resultRole.size());
204
205         // remove both Actors from relation and delete one Actor
206
// instance completely, the other Actor should still in DB
207
Object JavaDoc removed1 = newMovie.getActors2().remove(0);
208         Actor removed2 = (Actor) newMovie.getActors2().remove(0);
209         database.deletePersistent(removed1);
210         // update the Actor unlinked from relation
211
tx.lock(removed2, Transaction.WRITE);
212         String JavaDoc newName = removed2.getName() + "_updated";
213         removed2.setName(newName);
214         tx.commit();
215
216         queryMovie = movieQuery(postfixId);
217         resultMovie = (Collection JavaDoc) queryMovie.execute();
218         assertEquals(1, resultMovie.size());
219         newMovie = (Movie) resultMovie.iterator().next();
220         assertNotNull(newMovie.getActors());
221         assertEquals(1, newMovie.getActors().size());
222         assertEquals(0, newMovie.getActors2().size());
223
224         queryActor = actorQuery(postfixId);
225         resultActor = (Collection JavaDoc) queryActor.execute();
226         assertEquals(1 + 1, resultActor.size());
227
228         queryActor = actorQuery(newName);
229         resultActor = (Collection JavaDoc) queryActor.execute();
230         assertEquals(1, resultActor.size());
231
232         queryRole = roleQuery(null, m);
233         resultRole = (Collection JavaDoc) queryRole.execute();
234         assertEquals(1, resultRole.size());
235    }
236
237     public void testStore() throws Exception JavaDoc
238     {
239         // arminw: fixed
240
// TODO: Seems that the order of objects is not valid to insert M:N
241
//if(ojbSkipKnownIssueProblem()) return;
242
changeMovieCollectionDescriptorTo(true, NONE, NONE, false);
243         changeActorCollectionDescriptorTo(true, NONE, NONE, false);
244         doTestStore();
245     }
246
247     public void testStoreWithProxy() throws Exception JavaDoc
248     {
249         // arminw: fixed
250
// TODO: Seems that the order of objects is not valid to insert M:N
251
// if(ojbSkipKnownIssueProblem()) return;
252
changeMovieCollectionDescriptorTo(true, NONE, NONE, true);
253         changeActorCollectionDescriptorTo(true, NONE, NONE, false);
254         doTestStore();
255     }
256
257     public void doTestStore() throws Exception JavaDoc
258     {
259         String JavaDoc postfix = "doTestStore_" + System.currentTimeMillis();
260         Movie movie = buildMovieWithActors(postfix);
261
262         Transaction tx = odmg.newTransaction();
263         tx.begin();
264         database.makePersistent(movie);
265         tx.commit();
266
267         OQLQuery queryMovie = movieQuery(postfix);
268         Collection JavaDoc resultMovie = (Collection JavaDoc) queryMovie.execute();
269         assertEquals(1, resultMovie.size());
270         Movie newMovie = (Movie) resultMovie.iterator().next();
271         assertNotNull(newMovie.getActors());
272         assertEquals(3, newMovie.getActors().size());
273         assertEquals(2, newMovie.getActors2().size());
274
275         OQLQuery queryActor = actorQuery(postfix);
276         Collection JavaDoc resultActor = (Collection JavaDoc) queryActor.execute();
277         assertEquals(3 + 2, resultActor.size());
278
279         OQLQuery queryRole = roleQuery(null, movie);
280         Collection JavaDoc resultRole = (Collection JavaDoc) queryRole.execute();
281         assertEquals(3, resultRole.size());
282    }
283
284     public void testStore_2() throws Exception JavaDoc
285     {
286         changeMovieCollectionDescriptorTo(true, NONE, NONE, false);
287         changeActorCollectionDescriptorTo(true, NONE, NONE, false);
288         doTestStore_2();
289     }
290
291 // arminw: this test will not work, because we use a user defined ManageableCollection
292
// and the collection proxy is not compatible. Maybe this test will work in future versions
293
// when it's possible set a CollectionProxy impl in metadate.
294
// public void testStore_2WithProxy() throws Exception
295
// {
296
// prepareAutoUpdateDeleteSettings(true);
297
// doTestStore_2();
298
// }
299

300     public void doTestStore_2() throws Exception JavaDoc
301     {
302         String JavaDoc postfix = "doTestStore_2_" + System.currentTimeMillis();
303         Movie movie = buildMovieWithActors(postfix);
304
305         Transaction tx = odmg.newTransaction();
306         tx.begin();
307         Iterator JavaDoc it = movie.getActors().iterator();
308         while(it.hasNext())
309         {
310             database.makePersistent(it.next());
311         }
312         database.makePersistent(movie);
313         tx.commit();
314
315         OQLQuery queryMovie = movieQuery(postfix);
316         Collection JavaDoc resultMovie = (Collection JavaDoc) queryMovie.execute();
317         assertEquals(1, resultMovie.size());
318         Movie newMovie = (Movie) resultMovie.iterator().next();
319         assertNotNull(newMovie.getActors());
320         assertEquals(3, newMovie.getActors().size());
321
322         OQLQuery queryActor = actorQuery(postfix);
323         Collection JavaDoc resultActor = (Collection JavaDoc) queryActor.execute();
324         assertEquals(5, resultActor.size());
325
326         OQLQuery queryRole = roleQuery(null, movie);
327         Collection JavaDoc resultRole = (Collection JavaDoc) queryRole.execute();
328         assertEquals(3, resultRole.size());
329     }
330
331     public void testStoreComplex() throws Exception JavaDoc
332     {
333         // arminw: fixed
334
// TODO: Seems that the order of objects is not valid to insert M:N
335
// if(ojbSkipKnownIssueProblem()) return;
336
changeMovieCollectionDescriptorTo(true, NONE, NONE, false);
337         changeActorCollectionDescriptorTo(true, NONE, NONE, false);
338         doTestStoreComplex();
339     }
340
341     public void testStoreComplexWithProxy() throws Exception JavaDoc
342     {
343         // arminw: fixed
344
// TODO: Seems that the order of objects is not valid to insert M:N
345
// if(ojbSkipKnownIssueProblem()) return;
346
changeMovieCollectionDescriptorTo(true, NONE, NONE, true);
347         changeActorCollectionDescriptorTo(true, NONE, NONE, false);
348         doTestStoreComplex();
349     }
350
351
352     public void doTestStoreComplex() throws Exception JavaDoc
353     {
354         String JavaDoc postfix = "doTestStoreComplex_" + System.currentTimeMillis();
355         Movie movie = buildMovieWithActorsAndBackReferences(postfix);
356
357         Transaction tx = odmg.newTransaction();
358         tx.begin();
359         database.makePersistent(movie);
360         tx.commit();
361
362         OQLQuery queryMovie = movieQuery(postfix);
363         Collection JavaDoc resultMovie = (Collection JavaDoc) queryMovie.execute();
364         assertEquals(3, resultMovie.size());
365         Iterator JavaDoc it = resultMovie.iterator();
366         boolean matchActors = false;
367         while(it.hasNext())
368         {
369             Movie m = (Movie) it.next();
370             if(m.getActors() != null)
371             {
372                 matchActors = m.getActors().size() == 3;
373                 if(matchActors) break;
374             }
375         }
376         assertTrue(matchActors);
377
378         OQLQuery queryActor = actorQuery(postfix);
379         Collection JavaDoc resultActor = (Collection JavaDoc) queryActor.execute();
380         assertEquals(3, resultActor.size());
381         it = resultActor.iterator();
382         boolean matchMovies = false;
383         while(it.hasNext())
384         {
385             Actor a = (Actor) it.next();
386             if(a.getMovies() != null)
387             {
388                 matchMovies = a.getMovies().size() == 3;
389                 if(matchMovies) break;
390             }
391         }
392         assertTrue(matchMovies);
393
394         OQLQuery queryRole = odmg.newOQLQuery();
395         queryRole.create("select obj from " + Role.class.getName() + " where movieStrId=$3");
396         queryRole.bind(postfix);
397         Collection JavaDoc resultRole = (Collection JavaDoc) queryRole.execute();
398         assertEquals(5, resultRole.size());
399     }
400
401     public void testStoreComplex_2() throws Exception JavaDoc
402     {
403         // arminw: fixed
404
// TODO: Seems that the order of objects is not valid to insert M:N
405
// if(ojbSkipKnownIssueProblem()) return;
406
changeMovieCollectionDescriptorTo(true, NONE, NONE, false);
407         changeActorCollectionDescriptorTo(true, NONE, NONE, false);
408         doTestStoreComplex_2();
409     }
410
411     public void testStoreComplex_2_WithProxy() throws Exception JavaDoc
412     {
413         // arminw: fixed
414
// TODO: Seems that the order of objects is not valid to insert M:N
415
// if(ojbSkipKnownIssueProblem()) return;
416
changeMovieCollectionDescriptorTo(true, NONE, NONE, true);
417         changeActorCollectionDescriptorTo(true, NONE, NONE, false);
418         doTestStoreComplex_2();
419     }
420
421     public void doTestStoreComplex_2() throws Exception JavaDoc
422     {
423         String JavaDoc postfix = "doTestStoreComplex_2_" + System.currentTimeMillis();
424         Movie movie = buildMovieWithActorsAndBackReferences(postfix);
425
426         Transaction tx = odmg.newTransaction();
427         tx.begin();
428         Iterator JavaDoc it = movie.getActors().iterator();
429         while(it.hasNext())
430         {
431             database.makePersistent(it.next());
432         }
433         database.makePersistent(movie);
434         tx.commit();
435
436         OQLQuery queryMovie = movieQuery(postfix);
437         Collection JavaDoc resultMovie = (Collection JavaDoc) queryMovie.execute();
438         assertEquals(3, resultMovie.size());
439         it = resultMovie.iterator();
440         boolean matchActors = false;
441         while(it.hasNext())
442         {
443             Movie m = (Movie) it.next();
444             if(m.getActors() != null)
445             {
446                 matchActors = m.getActors().size() == 3;
447                 if(matchActors) break;
448             }
449         }
450
451         OQLQuery queryActor = actorQuery(postfix);
452         Collection JavaDoc resultActor = (Collection JavaDoc) queryActor.execute();
453         assertEquals(3, resultActor.size());
454         it = resultActor.iterator();
455         boolean matchMovies = false;
456         while(it.hasNext())
457         {
458             Actor a = (Actor) it.next();
459             if(a.getMovies() != null)
460             {
461                 matchMovies = a.getMovies().size() == 3;
462                 if(matchMovies) break;
463             }
464         }
465         assertTrue(matchMovies);
466
467         OQLQuery queryRole = odmg.newOQLQuery();
468         queryRole.create("select obj from " + Role.class.getName() + " where movieStrId=$3");
469         queryRole.bind(postfix);
470         Collection JavaDoc resultRole = (Collection JavaDoc) queryRole.execute();
471         assertEquals(5, resultRole.size());
472     }
473
474
475     public void testStoreDelete() throws Exception JavaDoc
476     {
477         changeMovieCollectionDescriptorTo(true, NONE, NONE, false);
478         changeActorCollectionDescriptorTo(true, NONE, NONE, false);
479         doTestStoreDelete();
480     }
481
482     public void testStoreDeleteWithProxy() throws Exception JavaDoc
483     {
484         changeMovieCollectionDescriptorTo(true, NONE, NONE, true);
485         changeActorCollectionDescriptorTo(true, NONE, NONE, false);
486         doTestStoreDelete();
487     }
488
489     public void doTestStoreDelete() throws Exception JavaDoc
490     {
491         String JavaDoc postfix = "doTestStoreDelete_" + System.currentTimeMillis();
492         Movie movie = buildMovieWithActors(postfix);
493
494         TransactionExt tx = (TransactionExt) odmg.newTransaction();
495         tx.begin();
496         Iterator JavaDoc it = movie.getActors().iterator();
497         while(it.hasNext())
498         {
499             database.makePersistent(it.next());
500         }
501         database.makePersistent(movie);
502         tx.commit();
503
504         OQLQuery queryMovie = movieQuery(postfix);
505         Collection JavaDoc resultMovie = (Collection JavaDoc) queryMovie.execute();
506         assertEquals(1, resultMovie.size());
507         Movie newMovie = (Movie) resultMovie.iterator().next();
508         assertNotNull(newMovie.getActors());
509         assertEquals(3, newMovie.getActors().size());
510
511         OQLQuery queryActor = actorQuery(postfix);
512         Collection JavaDoc resultActor = (Collection JavaDoc) queryActor.execute();
513         assertEquals(5, resultActor.size());
514
515         OQLQuery queryRole = roleQuery(null, movie);
516         Collection JavaDoc resultRole = (Collection JavaDoc) queryRole.execute();
517         assertEquals(3, resultRole.size());
518
519         tx.begin();
520         tx.getBroker().clearCache();
521         queryMovie = movieQuery(postfix);
522         resultMovie = (Collection JavaDoc) queryMovie.execute();
523         Movie m = (Movie) resultMovie.iterator().next();
524         assertNotNull(m);
525         //**********************************************
526
tx.lock(m, Transaction.WRITE);
527         it = m.getActors().iterator();
528         Actor actor = (Actor) it.next();
529         /*
530         we expect that the entry in indirection table was removed
531         AND the Actor object itself will be deleted
532         */

533         database.deletePersistent(actor);
534         //*********************************************
535
tx.commit();
536
537         tx.begin();
538         tx.getBroker().clearCache();
539         //tx.commit();
540

541         queryMovie = movieQuery(postfix);
542         resultMovie = (Collection JavaDoc) queryMovie.execute();
543         assertEquals(1, resultMovie.size());
544         newMovie = (Movie) resultMovie.iterator().next();
545         assertNotNull(newMovie.getActors());
546         assertEquals(2, newMovie.getActors().size());
547
548         queryActor = actorQuery(postfix);
549         resultActor = (Collection JavaDoc) queryActor.execute();
550         assertEquals(4, resultActor.size());
551
552         queryRole = roleQuery(null, movie);
553         resultRole = (Collection JavaDoc) queryRole.execute();
554         assertEquals(2, resultRole.size());
555
556         tx.commit();
557     }
558
559     /**
560      * Use auto-delete="object" to enable cascading delete.
561      */

562     public void testStoreDeleteCascade() throws Exception JavaDoc
563     {
564         String JavaDoc postfix = "doTestStoreDeleteCascade_" + System.currentTimeMillis();
565
566         changeMovieCollectionDescriptorTo(true, NONE, OBJECT, true);
567         changeActorCollectionDescriptorTo(true, NONE, OBJECT, false);
568
569         Movie movie = buildMovieWithActors(postfix);
570
571         TransactionExt tx = (TransactionExt) odmg.newTransaction();
572         tx.begin();
573         database.makePersistent(movie);
574         tx.commit();
575
576         OQLQuery queryMovie = movieQuery(postfix);
577         Collection JavaDoc resultMovie = (Collection JavaDoc) queryMovie.execute();
578         assertEquals(1, resultMovie.size());
579         Movie newMovie = (Movie) resultMovie.iterator().next();
580         assertNotNull(newMovie.getActors());
581         assertEquals(3, newMovie.getActors().size());
582
583         OQLQuery queryActor = actorQuery(postfix);
584         Collection JavaDoc resultActor = (Collection JavaDoc) queryActor.execute();
585         assertEquals(5, resultActor.size());
586
587         OQLQuery queryRole = roleQuery(null, movie);
588         Collection JavaDoc resultRole = (Collection JavaDoc) queryRole.execute();
589         assertEquals(3, resultRole.size());
590
591         tx.begin();
592         tx.getBroker().clearCache();
593         queryMovie = movieQuery(postfix);
594         resultMovie = (Collection JavaDoc) queryMovie.execute();
595         Movie m = (Movie) resultMovie.iterator().next();
596         //**********************************************
597
database.deletePersistent(m);
598         //*********************************************
599
tx.commit();
600
601         tx.begin();
602         tx.getBroker().clearCache();
603
604         queryMovie = movieQuery(postfix);
605         resultMovie = (Collection JavaDoc) queryMovie.execute();
606         assertEquals(0, resultMovie.size());
607
608         queryActor = actorQuery(postfix);
609         resultActor = (Collection JavaDoc) queryActor.execute();
610         assertEquals(0, resultActor.size());
611
612         queryRole = roleQuery(null, movie);
613         resultRole = (Collection JavaDoc) queryRole.execute();
614         assertEquals(0, resultRole.size());
615
616         tx.commit();
617     }
618
619     public void testRemoveAssociation() throws Exception JavaDoc
620     {
621         changeMovieCollectionDescriptorTo(true, NONE, NONE, false);
622         changeActorCollectionDescriptorTo(true, NONE, NONE, false);
623         doTestRemoveAssociation();
624     }
625
626     public void testRemoveAssociationWithProxy() throws Exception JavaDoc
627     {
628         changeMovieCollectionDescriptorTo(true, NONE, NONE, true);
629         changeActorCollectionDescriptorTo(true, NONE, NONE, false);
630         doTestRemoveAssociation();
631     }
632
633     public void doTestRemoveAssociation() throws Exception JavaDoc
634     {
635         String JavaDoc postfix = "doTestRemoveAssociation_" + System.currentTimeMillis();
636         Movie movie = buildMovieWithActors(postfix);
637
638         TransactionExt tx = (TransactionExt) odmg.newTransaction();
639         tx.begin();
640         Iterator JavaDoc it = movie.getActors().iterator();
641         while(it.hasNext())
642         {
643             database.makePersistent(it.next());
644         }
645         database.makePersistent(movie);
646         tx.commit();
647
648         OQLQuery queryMovie = movieQuery(postfix);
649         Collection JavaDoc resultMovie = (Collection JavaDoc) queryMovie.execute();
650         assertEquals(1, resultMovie.size());
651         Movie newMovie = (Movie) resultMovie.iterator().next();
652         assertNotNull(newMovie.getActors());
653         assertEquals(3, newMovie.getActors().size());
654
655         OQLQuery queryActor = actorQuery(postfix);
656         Collection JavaDoc resultActor = (Collection JavaDoc) queryActor.execute();
657         assertEquals(5, resultActor.size());
658
659         OQLQuery queryRole = roleQuery(null, movie);
660         Collection JavaDoc resultRole = (Collection JavaDoc) queryRole.execute();
661         assertEquals(3, resultRole.size());
662
663         tx.begin();
664         tx.getBroker().clearCache();
665
666         queryMovie = movieQuery(postfix);
667         resultMovie = (Collection JavaDoc) queryMovie.execute();
668         assertEquals(1, resultMovie.size());
669         Movie m = (Movie) resultMovie.iterator().next();
670         assertNotNull(m);
671         Collection JavaDoc actors = m.getActors();
672         assertEquals(3, actors.size());
673         //***************************************
674
tx.lock(m, Transaction.WRITE);
675         /*
676         now remove an association between Movie ans Actor
677         we expect that the entry in indirection class will be removed,
678         but the Actor entry should be still there
679         */

680         Iterator JavaDoc iter = actors.iterator();
681         iter.next();
682         iter.remove();
683         //***************************************
684
assertEquals(2, m.getActors().size());
685         tx.commit();
686
687         tx.begin();
688         tx.getBroker().clearCache();
689         //tx.commit();
690

691         queryMovie = movieQuery(postfix);
692         resultMovie = (Collection JavaDoc) queryMovie.execute();
693         assertEquals(1, resultMovie.size());
694         newMovie = (Movie) resultMovie.iterator().next();
695         assertNotNull(newMovie.getActors());
696         assertEquals(2, newMovie.getActors().size());
697
698         queryRole = roleQuery(null, movie);
699         resultRole = (Collection JavaDoc) queryRole.execute();
700         assertEquals(2, resultRole.size());
701
702         // we only remove the association
703
queryActor = actorQuery(postfix);
704         resultActor = (Collection JavaDoc) queryActor.execute();
705         assertEquals(5, resultActor.size());
706
707         tx.commit();
708     }
709
710     //=======================================================================
711
// helper methods
712
//=======================================================================
713

714     OQLQuery movieQuery(String JavaDoc postfix) throws QueryException
715     {
716         OQLQuery query = odmg.newOQLQuery();
717         query.create("select obj from " + Movie.class.getName() + " where idStr like $1");
718         query.bind("%" + postfix + "%");
719         return query;
720     }
721
722     OQLQuery actorQuery(String JavaDoc postfix) throws QueryException
723     {
724         OQLQuery query = odmg.newOQLQuery();
725         query.create("select obj from " + Actor.class.getName() + " where name like $1");
726         query.bind("%" + postfix + "%");
727         return query;
728     }
729
730     OQLQuery roleQuery(Actor actor, Movie movie) throws QueryException
731     {
732         OQLQuery query = odmg.newOQLQuery();
733         String JavaDoc select = "select obj from " + Role.class.getName() + " where";
734         boolean needsAnd = false;
735         if(actor != null)
736         {
737             select = select + " actorId=$1 and actorId2=$2";
738             needsAnd = true;
739         }
740         if(movie != null)
741         {
742             if(needsAnd) select = select + " and";
743             select = select + " movieIntId=$3 and movieIntId2=$4 and movieStrId=$5";
744         }
745         query.create(select);
746         if(actor != null)
747         {
748             query.bind(actor.getId());
749             query.bind(actor.getId2());
750
751         }
752         if(movie != null)
753         {
754             query.bind(movie.getIdInt());
755             query.bind(movie.getIdInt2());
756             query.bind(movie.getIdStr());
757         }
758         return query;
759     }
760
761     /**
762      * Returns 1 movie object with 3 actor objects in actors-collection
763      * and 2 actor objects in actors2-collection
764      */

765     Movie buildMovieWithActors(String JavaDoc postfixId)
766     {
767         Movie m = new MovieImpl(postfixId,
768                 "Dr. Strangelove or: How I Learned to Stop Worrying and Love the Bomb " + postfixId,
769                 "An insane general starts a process to nuclear holocaust that a war" +
770                 " room of politicians and generals frantically try to stop. " + postfixId);
771         
772         Actor a1 = new Actor("Peter Sellers " + postfixId);
773         Actor a2 = new Actor("George C. Scott " + postfixId);
774         Actor a3 = new Actor("Sterling Hayden " + postfixId);
775         ArrayList JavaDoc list = new ArrayList JavaDoc();
776         list.add(a1);
777         list.add(a2);
778         list.add(a3);
779         m.setActors(list);
780
781         Actor a4 = new Actor("Actor 2 A " + postfixId);
782         Actor a5 = new Actor("Actor 2 B " + postfixId);
783         ArrayList JavaDoc list2 = new ArrayList JavaDoc();
784         list2.add(a4);
785         list2.add(a5);
786         m.setActors2(list2);
787
788         return m;
789     }
790
791     /**
792      * Returns 1 movie object m1 with 3 actor objects and one actor object with
793      * back-reference to movie object m1 + 2 new movies
794      */

795     Movie buildMovieWithActorsAndBackReferences(String JavaDoc postfixId)
796     {
797         Movie m = new MovieImpl(postfixId,
798                 "Dr. Strangelove or: How I Learned to Stop Worrying and Love the Bomb " + postfixId,
799                 "An insane general starts a process to nuclear holocaust that a war" +
800                 " room of politicians and generals frantically try to stop. " + postfixId);
801         Actor a1 = new Actor("Peter Sellers " + postfixId);
802         Actor a2 = new Actor("George C. Scott " + postfixId);
803         Actor a3 = new Actor("Sterling Hayden " + postfixId);
804         ArrayList JavaDoc list = new ArrayList JavaDoc();
805         list.add(a1);
806         list.add(a2);
807         list.add(a3);
808         m.setActors(list);
809
810         Movie m1 = new MovieImpl(postfixId + "", "A Shot in the Dark",
811                 "As murder follows murder, beautiful Maria is the obvious suspect...");
812         Movie m2 = new MovieImpl(postfixId + "", "The Pink Panther",
813                 " In the first movie starring Peter Sellers as the bumbling Inspector Clouseau...");
814
815         MovieManageableCollection mlist1 = new MovieManageableCollectionImpl();
816         mlist1.add(m);
817         mlist1.add(m1);
818         mlist1.add(m2);
819         MovieManageableCollection mlist2 = new MovieManageableCollectionImpl();
820         MovieManageableCollection mlist3 = new MovieManageableCollectionImpl();
821         a1.setMovies(mlist1);
822         a2.setMovies(mlist2);
823         a3.setMovies(mlist3);
824
825         return m;
826     }
827
828     Actor buildActorWithMovies(String JavaDoc postfixId)
829     {
830         Actor a = new Actor("John Cusack" + postfixId);
831         MovieManageableCollection list = new MovieManageableCollectionImpl();
832         list.add(new MovieImpl("a_" + postfixId, "High Fidelity", "A comedy about fear of commitment, hating your job..." + postfixId));
833         list.add(new MovieImpl("b_" + postfixId, "Identity", "When a nasty storm hits a hotel, ten strangers are stranded within ..." + postfixId));
834         list.add(new MovieImpl("c_" + postfixId, "Grosse Pointe Blank", "Martin Blank is a professional assassin. He is sent on a mission to a small Detroit ..." + postfixId));
835         a.setMovies(list);
836         return a;
837     }
838
839     void changeActorCollectionDescriptorTo(boolean autoRetrieve, int autoUpdate, int autoDelete, boolean proxy)
840     {
841         ojbChangeReferenceSetting(Actor.class, "movies", autoRetrieve, autoUpdate, autoDelete, proxy);
842     }
843
844     void changeMovieCollectionDescriptorTo(boolean autoRetrieve, int autoUpdate, int autoDelete, boolean proxy)
845     {
846         ojbChangeReferenceSetting(MovieImpl.class, "actors", autoRetrieve, autoUpdate, autoDelete, proxy);
847         ojbChangeReferenceSetting(MovieImpl.class, "actors2", autoRetrieve, autoUpdate, autoDelete, proxy);
848     }
849
850
851     //=======================================================================
852
// Inner classes, persistence capable test classes
853
//=======================================================================
854

855     public static interface MovieManageableCollection extends ManageableCollection
856     {
857         public Iterator JavaDoc iterator();
858
859         public int size();
860
861         public boolean isEmpty();
862
863         public void clear();
864
865         public boolean add(Movie movie);
866
867         public boolean remove(Movie movie);
868
869         public boolean contains(Movie movie);
870
871         public Movie get(int index);
872     }
873
874     public static class MovieManageableCollectionImpl implements MovieManageableCollection
875     {
876         private ArrayList JavaDoc list = new ArrayList JavaDoc();
877
878         public void ojbAdd(Object JavaDoc anObject)
879         {
880             list.add(anObject);
881         }
882
883         public void ojbAddAll(ManageableCollection otherCollection)
884         {
885             Iterator JavaDoc it = otherCollection.ojbIterator();
886             while(it.hasNext())
887             {
888                 list.add(it.next());
889             }
890         }
891
892         public Iterator JavaDoc ojbIterator()
893         {
894             return list.iterator();
895         }
896
897         public void afterStore(PersistenceBroker broker) throws PersistenceBrokerException
898         {
899         }
900
901         public Iterator JavaDoc iterator()
902         {
903             return list.iterator();
904         }
905
906         public int size()
907         {
908             return list.size();
909         }
910
911         public boolean isEmpty()
912         {
913             return list.isEmpty();
914         }
915
916         public void clear()
917         {
918             list.clear();
919         }
920
921         public boolean add(Movie movie)
922         {
923             return list.add(movie);
924         }
925
926         public boolean remove(Movie movie)
927         {
928             return list.remove(movie);
929         }
930
931         public boolean contains(Movie movie)
932         {
933             return list.contains(movie);
934         }
935
936         public Movie get(int index)
937         {
938             return (Movie) list.get(index);
939         }
940     }
941
942     public static class Actor
943     {
944         private Integer JavaDoc id;
945         private Integer JavaDoc id2;
946         private String JavaDoc name;
947         private MovieManageableCollection movies;
948
949         public Actor()
950         {
951         }
952
953         public Actor(String JavaDoc name)
954         {
955             this.name = name;
956         }
957
958         public MovieManageableCollection getMovies()
959         {
960             return movies;
961         }
962
963         public void setMovies(MovieManageableCollection movies)
964         {
965             this.movies = movies;
966         }
967
968         public Integer JavaDoc getId()
969         {
970             return id;
971         }
972
973         public void setId(Integer JavaDoc id)
974         {
975             this.id = id;
976         }
977
978         public Integer JavaDoc getId2()
979         {
980             return id2;
981         }
982
983         public void setId2(Integer JavaDoc id2)
984         {
985             this.id2 = id2;
986         }
987
988         public String JavaDoc getName()
989         {
990             return name;
991         }
992
993         public void setName(String JavaDoc name)
994         {
995             this.name = name;
996         }
997
998         public String JavaDoc toString()
999         {
1000            return ToStringBuilder.reflectionToString(this).toString();
1001        }
1002    }
1003
1004    public static interface Movie
1005    {
1006        public void addActors(Actor actor);
1007        public void addActors2(Actor actor);
1008
1009        public Collection JavaDoc getActors();
1010        public void setActors(Collection JavaDoc actors);
1011
1012        public List JavaDoc getActors2();
1013        public void setActors2(List JavaDoc actors);
1014
1015        public Integer JavaDoc getIdInt2();
1016        public Integer JavaDoc getIdInt();
1017
1018        public void setIdInt2(Integer JavaDoc id2Int);
1019        public void setIdInt(Integer JavaDoc idInt);
1020
1021        public String JavaDoc getIdStr();
1022        public void setIdStr(String JavaDoc idStr);
1023
1024        public String JavaDoc getTitle();
1025        public void setTitle(String JavaDoc title);
1026
1027        public String JavaDoc getDescription();
1028        public void setDescription(String JavaDoc description);
1029    }
1030
1031    public static class MovieImpl implements Movie
1032    {
1033        private Integer JavaDoc idInt;
1034        private Integer JavaDoc idInt2;
1035        private String JavaDoc idStr;
1036        private String JavaDoc title;
1037        private String JavaDoc description;
1038        private Collection JavaDoc actors;
1039        private List JavaDoc actors2;
1040
1041        public MovieImpl()
1042        {
1043        }
1044
1045        public MovieImpl(String JavaDoc idStr, String JavaDoc title, String JavaDoc description)
1046        {
1047            this.idStr = idStr;
1048            this.title = title;
1049            this.description = description;
1050        }
1051
1052        public void addActors(Actor actor)
1053        {
1054            if(actors == null)
1055            {
1056                actors = new ArrayList JavaDoc();
1057            }
1058            if(!actors.contains(actor)) actors.add(actor);
1059            else throw new OJBRuntimeException("Can't add same object twice");
1060        }
1061
1062        public void addActors2(Actor actor)
1063        {
1064            if(actors2 == null)
1065            {
1066                actors2 = new ArrayList JavaDoc();
1067            }
1068            if(!actors2.contains(actor)) actors2.add(actor);
1069            else throw new OJBRuntimeException("Can't add same object twice");
1070        }
1071
1072        public Collection JavaDoc getActors()
1073        {
1074            return actors;
1075        }
1076
1077        public void setActors(Collection JavaDoc actors)
1078        {
1079            this.actors = actors;
1080        }
1081
1082       public List JavaDoc getActors2()
1083        {
1084            return actors2;
1085        }
1086
1087        public void setActors2(List JavaDoc actors)
1088        {
1089            this.actors2 = actors;
1090        }
1091        
1092        public Integer JavaDoc getIdInt()
1093        {
1094            return idInt;
1095        }
1096
1097        public void setIdInt(Integer JavaDoc idInt)
1098        {
1099            this.idInt = idInt;
1100        }
1101
1102        public Integer JavaDoc getIdInt2()
1103        {
1104            return idInt2;
1105        }
1106
1107        public void setIdInt2(Integer JavaDoc idInt2)
1108        {
1109            this.idInt2 = idInt2;
1110        }
1111
1112        public String JavaDoc getIdStr()
1113        {
1114            return idStr;
1115        }
1116
1117        public void setIdStr(String JavaDoc idStr)
1118        {
1119            this.idStr = idStr;
1120        }
1121
1122        public String JavaDoc getTitle()
1123        {
1124            return title;
1125        }
1126
1127        public void setTitle(String JavaDoc title)
1128        {
1129            this.title = title;
1130        }
1131
1132        public String JavaDoc getDescription()
1133        {
1134            return description;
1135        }
1136
1137        public void setDescription(String JavaDoc description)
1138        {
1139            this.description = description;
1140        }
1141
1142        public String JavaDoc toString()
1143        {
1144            return ToStringBuilder.reflectionToString(this).toString();
1145        }
1146    }
1147
1148    public static class Role
1149    {
1150        private Integer JavaDoc actorId;
1151        private Integer JavaDoc actorId2;
1152        private Integer JavaDoc movieIntId;
1153        private Integer JavaDoc movieIntId2;
1154        private String JavaDoc movieStrId;
1155
1156        public Role()
1157        {
1158        }
1159
1160        public Integer JavaDoc getActorId()
1161        {
1162            return actorId;
1163        }
1164
1165        public void setActorId(Integer JavaDoc actorId)
1166        {
1167            this.actorId = actorId;
1168        }
1169
1170        public Integer JavaDoc getMovieIntId()
1171        {
1172            return movieIntId;
1173        }
1174
1175        public Integer JavaDoc getMovieIntId2()
1176        {
1177            return movieIntId2;
1178        }
1179
1180        public void setMovieIntId2(Integer JavaDoc movieIntId2)
1181        {
1182            this.movieIntId2 = movieIntId2;
1183        }
1184
1185        public Integer JavaDoc getActorId2()
1186        {
1187            return actorId2;
1188        }
1189
1190        public void setActorId2(Integer JavaDoc actorId2)
1191        {
1192            this.actorId2 = actorId2;
1193        }
1194
1195        public void setMovieIntId(Integer JavaDoc movieIntId)
1196        {
1197            this.movieIntId = movieIntId;
1198        }
1199
1200        public String JavaDoc getMovieStrId()
1201        {
1202            return movieStrId;
1203        }
1204
1205        public void setMovieStrId(String JavaDoc movieStrId)
1206        {
1207            this.movieStrId = movieStrId;
1208        }
1209
1210        public String JavaDoc toString()
1211        {
1212            return ToStringBuilder.reflectionToString(this).toString();
1213        }
1214    }
1215
1216
1217
1218    public static class County
1219    {
1220        private String JavaDoc id;
1221        private String JavaDoc name;
1222
1223        public County()
1224        {
1225        }
1226
1227        /**
1228         * Compares the given objects on the basis of their
1229         * toString() methods. Returns <code>false</code>
1230         * if the given object is not of type County.
1231         */

1232        public boolean equals(Object JavaDoc obj)
1233        {
1234            if(!(obj instanceof County))
1235            {
1236                return false;
1237            }
1238            County other = (County) obj;
1239            return new EqualsBuilder().append(getId(), other.getId())
1240                    .append(getName(), other.getName())
1241                    .isEquals();
1242        }
1243
1244        public String JavaDoc getId()
1245        {
1246            return id;
1247        }
1248
1249        public void setId(String JavaDoc id)
1250        {
1251            this.id = id;
1252        }
1253
1254        public String JavaDoc getName()
1255        {
1256            return name;
1257        }
1258
1259        public void setName(String JavaDoc name)
1260        {
1261            this.name = name;
1262        }
1263    }
1264
1265    public static interface OfficeProfile
1266    {
1267        public int getId();
1268        public void setId(int officeId);
1269        public void setName(String JavaDoc name);
1270        public String JavaDoc getName();
1271        public List JavaDoc getCounties();
1272        public void setCounties(List JavaDoc list);
1273        public void clearCounties();
1274        public void addCounty(County county);
1275        public void removeCounty(County county);
1276    }
1277
1278    public static class OfficeProfileImpl implements OfficeProfile
1279    {
1280        private int id;
1281        private String JavaDoc name;
1282        private List JavaDoc counties;
1283
1284        public OfficeProfileImpl()
1285        {
1286        }
1287
1288        public boolean equals(Object JavaDoc obj)
1289        {
1290            if(!(obj instanceof OfficeProfile))
1291            {
1292                return false;
1293            }
1294            OfficeProfile other = (OfficeProfile) obj;
1295            return new EqualsBuilder().append(getId(), other.getId())
1296                    .append(getName(), other.getName())
1297                    .append(getCounties(), other.getCounties())
1298                    .isEquals();
1299        }
1300
1301        public int getId()
1302        {
1303            return id;
1304        }
1305
1306        public void setId(int officeId)
1307        {
1308            id = officeId;
1309        }
1310
1311        public String JavaDoc getName()
1312        {
1313            return name;
1314        }
1315
1316        public void setName(String JavaDoc name)
1317        {
1318            this.name = name;
1319        }
1320
1321        public List JavaDoc getCounties()
1322        {
1323            return counties;
1324        }
1325
1326        public void setCounties(List JavaDoc list)
1327        {
1328            counties = list;
1329        }
1330
1331        public void clearCounties()
1332        {
1333            if(counties != null)
1334            {
1335                counties.clear();
1336            }
1337        }
1338
1339        public void addCounty(County county)
1340        {
1341            if(counties == null)
1342            {
1343                counties = new LinkedList JavaDoc();
1344            }
1345            counties.add(county);
1346        }
1347
1348        public void removeCounty(County county)
1349        {
1350            if(counties != null)
1351            {
1352                counties.remove(county);
1353            }
1354        }
1355    }
1356}
1357
Popular Tags