KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.ojb.broker;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Collection JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7
8 import org.apache.commons.lang.ClassUtils;
9 import org.apache.commons.lang.builder.EqualsBuilder;
10 import org.apache.commons.lang.builder.HashCodeBuilder;
11 import org.apache.commons.lang.builder.ToStringBuilder;
12 import org.apache.commons.lang.time.StopWatch;
13 import org.apache.ojb.broker.metadata.CollectionDescriptor;
14 import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
15 import org.apache.ojb.broker.query.Criteria;
16 import org.apache.ojb.broker.query.Query;
17 import org.apache.ojb.broker.query.QueryFactory;
18 import org.apache.ojb.broker.util.ObjectModification;
19 import org.apache.ojb.junit.PBTestCase;
20
21 /**
22  * Test (non-decomposed) M:N relations.
23  *
24  * IMPORTANT NOTE: The global runtime metadata changes made by this test case
25  * are NOT recommended in multithreaded environments, because they are global
26  * and each thread will be affected.
27  *
28  * @author <a HREF="mailto:arminw@apache.org">Armin Waibel</a>
29  * @version $Id: M2NTest.java,v 1.7.2.11 2005/11/26 02:37:03 arminw Exp $
30  */

31 public class M2NTest extends PBTestCase
32 {
33     static final int NONE = ObjectReferenceDescriptor.CASCADE_NONE;
34     static final int LINK = ObjectReferenceDescriptor.CASCADE_LINK;
35     static final int OBJECT = ObjectReferenceDescriptor.CASCADE_OBJECT;
36
37     int actorCount = 200;
38     int movieCount = 100;
39
40     public static void main(String JavaDoc[] args)
41     {
42         junit.textui.TestRunner.main(new String JavaDoc[]{M2NTest.class.getName()});
43     }
44
45     public void tearDown() throws Exception JavaDoc
46     {
47         super.tearDown();
48     }
49
50     /**
51      * Test for OJB-76
52      */

53     public void testStoreWithSharedIndirectionTableColumn()
54     {
55         ojbChangeReferenceSetting(MovieImpl.class, "producers", true, OBJECT, OBJECT, false);
56         ojbChangeReferenceSetting(Producer.class, "movies", true, OBJECT, OBJECT, false);
57         String JavaDoc timestamp = "" + System.currentTimeMillis();
58         String JavaDoc postfix = "testStoreWithSharedIndirectionTableColumn_" + timestamp;
59
60         Movie m_1 = new MovieImpl(postfix, postfix + "_1", null);
61         Movie m_2 = new MovieImpl(postfix, postfix + "_2", null);
62         Producer p_1 = new Producer(postfix, "producer_" + timestamp);
63         m_1.addProducer(p_1);
64         p_1.addMovie(m_1);
65         broker.beginTransaction();
66         broker.store(p_1, ObjectModification.INSERT);
67         broker.commitTransaction();
68
69         broker.clearCache();
70         Criteria crit = new Criteria();
71         crit.addLike("title", postfix + "%");
72         Query q = QueryFactory.newQuery(Movie.class, crit);
73         Movie new_m_1 = (Movie) broker.getObjectByQuery(q);
74         assertNotNull(new_m_1);
75         assertNotNull(new_m_1.getProducers());
76         assertEquals(1, new_m_1.getProducers().size());
77
78         broker.beginTransaction();
79         p_1.addMovie(m_2);
80         m_2.addProducer(p_1);
81         broker.store(p_1, ObjectModification.UPDATE);
82         // or (but this will cause more DB traffic)
83
// broker.store(m_2, ObjectModification.INSERT);
84
broker.commitTransaction();
85
86         broker.clearCache();
87         new_m_1 = (Movie) broker.getObjectByQuery(q);
88         assertNotNull(new_m_1);
89         assertNotNull(new_m_1.getProducers());
90         assertEquals(1, new_m_1.getProducers().size());
91         Producer new_p_1 = (Producer) new_m_1.getProducers().get(0);
92         assertNotNull(new_p_1);
93         assertNotNull(new_p_1.getMovies());
94         assertEquals(2, new_p_1.getMovies().size());
95
96
97         broker.beginTransaction();
98         broker.delete(p_1);
99         broker.commitTransaction();
100
101         new_m_1 = (Movie) broker.getObjectByQuery(q);
102         assertNull(new_m_1);
103
104         crit = new Criteria();
105         crit.addEqualTo("name", "producer_" + timestamp);
106         q = QueryFactory.newQuery(Producer.class, crit);
107         new_p_1 = (Producer) broker.getObjectByQuery(q);
108         assertNull(new_p_1);
109     }
110
111     public void testSimpleStore_1()
112     {
113         ojbChangeReferenceSetting(MovieImpl.class, "actors", true, OBJECT, OBJECT, false);
114         ojbChangeReferenceSetting(MovieImpl.class, "actors2", true, OBJECT, OBJECT, false);
115         ojbChangeReferenceSetting(Actor.class, "movies", true, OBJECT, OBJECT, false);
116         String JavaDoc postfix = "testSimpleStore_1_" + System.currentTimeMillis();
117         Movie m = new MovieImpl(postfix, postfix, null);
118         Actor a = new Actor(postfix);
119
120         broker.beginTransaction();
121         broker.store(m);
122         broker.store(a);
123
124         m.addActor(a);
125         broker.store(m);
126         broker.commitTransaction();
127
128         broker.retrieveAllReferences(a);
129
130         assertNotNull(a.getMovies());
131         assertEquals(1, a.getMovies().size());
132     }
133
134     public void testSimpleStore_2()
135     {
136         ojbChangeReferenceSetting(MovieImpl.class, "actors", false, OBJECT, OBJECT, false);
137         ojbChangeReferenceSetting(MovieImpl.class, "actors2", false, OBJECT, OBJECT, false);
138         ojbChangeReferenceSetting(Actor.class, "movies", false, OBJECT, OBJECT, false);
139         String JavaDoc postfix = "testSimpleStore_2_" + System.currentTimeMillis();
140         Movie m = new MovieImpl(postfix, postfix, null);
141         Actor a = new Actor(postfix);
142
143         broker.beginTransaction();
144         broker.store(m);
145         broker.store(a);
146
147         m.addActor(a);
148         broker.store(m);
149         broker.commitTransaction();
150
151         // needed, because autoretrieve is set false
152
broker.retrieveAllReferences(a);
153
154         assertNotNull(a.getMovies());
155         assertEquals(1, a.getMovies().size());
156     }
157
158     /**
159      * Test deprecated auto Settings
160      */

161     public void testAutoUpdateDeleteSettings()
162     {
163         ojbChangeReferenceSetting(Actor.class, "movies", false, false, false, false);
164         CollectionDescriptor ord = broker.getClassDescriptor(Actor.class)
165                 .getCollectionDescriptorByName("movies");
166         assertEquals(LINK, ord.getCascadingStore());
167         assertEquals(LINK, ord.getCascadingDelete());
168         assertEquals(false, ord.getCascadeStore());
169         assertEquals(false, ord.getCascadeDelete());
170
171         ojbChangeReferenceSetting(Actor.class, "movies", false, true, true, false);
172         ord = broker.getClassDescriptor(Actor.class)
173                 .getCollectionDescriptorByName("movies");
174         assertEquals(OBJECT, ord.getCascadingStore());
175         assertEquals(OBJECT, ord.getCascadingDelete());
176         assertEquals(true, ord.getCascadeStore());
177         assertEquals(true, ord.getCascadeDelete());
178     }
179
180     public void testMassStoreUpdateAutomatic()
181     {
182
183         long testPeriod = 0;
184
185         String JavaDoc postfix = "testMassStoreUpdateAutomatic_" + System.currentTimeMillis();
186         ojbChangeReferenceSetting(MovieImpl.class, "actors", true, OBJECT, OBJECT, false);
187         ojbChangeReferenceSetting(MovieImpl.class, "actors2", true, OBJECT, OBJECT, false);
188         ojbChangeReferenceSetting(Actor.class, "movies", true, OBJECT, OBJECT, false);
189
190         Movie movie = buildMovieWithActors(postfix, actorCount);
191         Actor actor = buildActorWithMovies(postfix, movieCount);
192
193         List JavaDoc actors = new ArrayList JavaDoc(movie.getActors());
194         actors.add(actor);
195         movie.setActors(actors);
196
197         MovieManageableCollection movies = actor.getMovies();
198         movies.add(movie);
199
200         StopWatch watch = new StopWatch();
201         watch.start();
202         broker.beginTransaction();
203         broker.store(movie);
204         broker.commitTransaction();
205         watch.stop();
206         System.out.println("[" + ClassUtils.getShortClassName(this.getClass())
207                 + "#testMassStoreUpdateAutomatic] Time to store "+(actorCount + movieCount)+" m:n objects=" + watch.getTime());
208         testPeriod += watch.getTime();
209
210         watch.reset();
211         watch.start();
212         Query queryMovie = queryMovie(postfix);
213         Collection JavaDoc resultMovie = broker.getCollectionByQuery(queryMovie);
214         assertEquals(movieCount + 1, resultMovie.size());
215         watch.stop();
216         System.out.println("[" + ClassUtils.getShortClassName(this.getClass())
217                 + "#testMassStoreUpdateAutomatic] Time to query "+movieCount+" m:n objects=" + watch.getTime());
218         testPeriod += watch.getTime();
219
220         watch.reset();
221         watch.start();
222         Query queryActor = queryActor(postfix);
223         Collection JavaDoc resultActor = broker.getCollectionByQuery(queryActor);
224         assertEquals(actorCount + 1, resultActor.size());
225         watch.stop();
226         System.out.println("[" + ClassUtils.getShortClassName(this.getClass())
227                 + "#testMassStoreUpdateAutomatic] Time to query "+actorCount+" m:n objects=" + watch.getTime());
228         testPeriod += watch.getTime();
229
230         Query queryRole = roleQueryActorOrMovieMatch(actor, movie);
231         Collection JavaDoc resultRole = broker.getCollectionByQuery(queryRole);
232         assertEquals(actorCount + movieCount + 1, resultRole.size());
233
234         //*****************************
235
// update movie
236
movie.setActors(new ArrayList JavaDoc());
237         watch.reset();
238         watch.start();
239         broker.beginTransaction();
240         broker.store(movie);
241         broker.commitTransaction();
242         watch.stop();
243         System.out.println("[" + ClassUtils.getShortClassName(this.getClass())
244                 + "#testMassStoreUpdateAutomatic] Time to update object with "+actorCount + " m:n objects=" + watch.getTime());
245         testPeriod += watch.getTime();
246         //*****************************
247

248         broker.clearCache();
249         Identity oid = broker.serviceIdentity().buildIdentity(movie);
250         movie = (Movie) broker.getObjectByIdentity(oid);
251
252         resultMovie = broker.getCollectionByQuery(queryMovie);
253         assertEquals(movieCount + 1, resultMovie.size());
254
255         resultActor = broker.getCollectionByQuery(queryActor);
256         assertEquals(actorCount + 1, resultActor.size());
257
258         resultRole = broker.getCollectionByQuery(queryRole);
259         assertEquals(movieCount, resultRole.size());
260
261         assertNotNull(movie);
262         assertEquals(0, movie.getActors().size());
263
264         //*****************************
265
// remove actor
266
movie.setActors(new ArrayList JavaDoc());
267         watch.reset();
268         watch.start();
269         broker.beginTransaction();
270         broker.delete(actor);
271         broker.commitTransaction();
272         watch.stop();
273         System.out.println("[" + ClassUtils.getShortClassName(this.getClass())
274                 + "#testMassStoreUpdateAutomatic] Time to remove object with "+ movieCount + " m:n objects=" + watch.getTime());
275         testPeriod += watch.getTime();
276         //*****************************
277

278         broker.clearCache();
279         oid = broker.serviceIdentity().buildIdentity(actor);
280         actor = (Actor) broker.getObjectByIdentity(oid);
281
282         resultMovie = broker.getCollectionByQuery(queryMovie);
283         assertEquals(0, resultMovie.size());
284
285         // we never delete these actor objects
286
resultActor = broker.getCollectionByQuery(queryActor);
287         assertEquals(actorCount, resultActor.size());
288
289         resultRole = broker.getCollectionByQuery(queryRole);
290         assertEquals(0, resultRole.size());
291
292         assertNull(actor);
293
294         broker.beginTransaction();
295         broker.delete(movie);
296         broker.commitTransaction();
297
298         resultMovie = broker.getCollectionByQuery(queryMovie);
299         assertEquals(0, resultMovie.size());
300         System.out.println("[" + ClassUtils.getShortClassName(this.getClass())
301                 + "#testMassStoreUpdateAutomatic] Total test time is "+ testPeriod+" ms");
302         System.out.println("");
303     }
304
305
306     public void testMassStoreUpdateLinking()
307     {
308         long testPeriod = 0;
309
310         String JavaDoc postfix = "testMassStoreUpdateLinking" + System.currentTimeMillis();
311         ojbChangeReferenceSetting(MovieImpl.class, "actors", true, NONE, OBJECT, false);
312         ojbChangeReferenceSetting(MovieImpl.class, "actors2", true, NONE, OBJECT, false);
313         ojbChangeReferenceSetting(Actor.class, "movies", true, NONE, OBJECT, false);
314
315         Movie movie = buildMovieWithActors(postfix, actorCount);
316         Actor actor = buildActorWithMovies(postfix, movieCount);
317
318         List JavaDoc actors = new ArrayList JavaDoc(movie.getActors());
319         actors.add(actor);
320         movie.setActors(actors);
321
322         MovieManageableCollection movies = actor.getMovies();
323         movies.add(movie);
324
325         StopWatch watch = new StopWatch();
326         watch.start();
327         broker.beginTransaction();
328         broker.store(movie);
329         for(int i = 0; i < actors.size(); i++)
330         {
331             broker.store(actors.get(i));
332         }
333         MovieManageableCollection actorMovies = actor.getMovies();
334         for(int i = 0; i < actorMovies.size(); i++)
335         {
336              broker.store(actorMovies.get(i));
337         }
338         broker.serviceBrokerHelper().link(movie, true);
339         broker.serviceBrokerHelper().link(actor, true);
340         broker.commitTransaction();
341         watch.stop();
342         System.out.println("[" + ClassUtils.getShortClassName(this.getClass())
343                 + "#testMassStoreUpdateLinking] Time to store "+(actorCount + movieCount)+" m:n objects=" + watch.getTime());
344         testPeriod += watch.getTime();
345
346         watch.reset();
347         watch.start();
348         Query queryMovie = queryMovie(postfix);
349         Collection JavaDoc resultMovie = broker.getCollectionByQuery(queryMovie);
350         assertEquals(movieCount + 1, resultMovie.size());
351         watch.stop();
352         System.out.println("[" + ClassUtils.getShortClassName(this.getClass())
353                 + "#testMassStoreUpdateLinking] Time to query "+movieCount+" m:n objects=" + watch.getTime());
354         testPeriod += watch.getTime();
355
356         watch.reset();
357         watch.start();
358         Query queryActor = queryActor(postfix);
359         Collection JavaDoc resultActor = broker.getCollectionByQuery(queryActor);
360         assertEquals(actorCount + 1, resultActor.size());
361         watch.stop();
362         System.out.println("[" + ClassUtils.getShortClassName(this.getClass())
363                 + "#testMassStoreUpdateLinking] Time to query "+actorCount+" m:n objects=" + watch.getTime());
364         testPeriod += watch.getTime();
365
366         Query queryRole = roleQueryActorOrMovieMatch(actor, movie);
367         Collection JavaDoc resultRole = broker.getCollectionByQuery(queryRole);
368         assertEquals(actorCount + movieCount + 1, resultRole.size());
369
370         //*****************************
371
// update movie
372
movie.setActors(new ArrayList JavaDoc());
373         watch.reset();
374         watch.start();
375         broker.beginTransaction();
376         broker.serviceBrokerHelper().unlink(movie);
377         broker.store(movie);
378         broker.serviceBrokerHelper().link(movie, false);
379         broker.commitTransaction();
380         watch.stop();
381         System.out.println("[" + ClassUtils.getShortClassName(this.getClass())
382                 + "#testMassStoreUpdateLinking] Time to update object with "+actorCount + " m:n objects=" + watch.getTime());
383         testPeriod += watch.getTime();
384         //*****************************
385

386         broker.clearCache();
387         Identity oid = broker.serviceIdentity().buildIdentity(movie);
388         movie = (Movie) broker.getObjectByIdentity(oid);
389
390         resultMovie = broker.getCollectionByQuery(queryMovie);
391         assertEquals(movieCount + 1, resultMovie.size());
392
393         resultActor = broker.getCollectionByQuery(queryActor);
394         assertEquals(actorCount + 1, resultActor.size());
395
396         resultRole = broker.getCollectionByQuery(queryRole);
397         assertEquals(movieCount, resultRole.size());
398
399         assertNotNull(movie);
400         assertEquals(0, movie.getActors().size());
401
402         //*****************************
403
// remove actor
404
movie.setActors(new ArrayList JavaDoc());
405         watch.reset();
406         watch.start();
407         broker.beginTransaction();
408         broker.delete(actor);
409         broker.commitTransaction();
410         watch.stop();
411         System.out.println("[" + ClassUtils.getShortClassName(this.getClass())
412                 + "#testMassStoreUpdateLinking] Time to remove object with "+ movieCount + " m:n objects=" + watch.getTime());
413         testPeriod += watch.getTime();
414         //*****************************
415

416         broker.clearCache();
417         oid = broker.serviceIdentity().buildIdentity(actor);
418         actor = (Actor) broker.getObjectByIdentity(oid);
419
420         resultMovie = broker.getCollectionByQuery(queryMovie);
421         assertEquals(0, resultMovie.size());
422
423         // we never delete these actor objects
424
resultActor = broker.getCollectionByQuery(queryActor);
425         assertEquals(actorCount, resultActor.size());
426
427         resultRole = broker.getCollectionByQuery(queryRole);
428         assertEquals(0, resultRole.size());
429
430         assertNull(actor);
431
432         broker.beginTransaction();
433         broker.delete(movie);
434         broker.commitTransaction();
435
436         resultMovie = broker.getCollectionByQuery(queryMovie);
437         assertEquals(0, resultMovie.size());
438         System.out.println("[" + ClassUtils.getShortClassName(this.getClass())
439                 + "#testMassStoreUpdateLinking] Total test time is "+ testPeriod+" ms");
440         System.out.println("");
441     }
442
443     //============================================================
444
// auto-retrieve true / auto-update/auto-delete LINK
445
//============================================================
446
// test needs refactoring, LINK settings doesn't allow to store
447
// an object graph
448
public void YYYtestStoreAddUpdateDeleteTLLF()
449     {
450         ojbChangeReferenceSetting(MovieImpl.class, "actors", true, LINK, LINK, false);
451         ojbChangeReferenceSetting(MovieImpl.class, "actors2", true, LINK, LINK, false);
452         ojbChangeReferenceSetting(Actor.class, "movies", true, LINK, LINK, false);
453         String JavaDoc postfix = "_testStoreTLLF_" + System.currentTimeMillis();
454         Movie movie = buildMovieWithActorsAndBackReferences(postfix);
455         doTestStoreAddUpdateDeleteTLLX(movie, postfix);
456     }
457
458     public void YYYtestStoreAddUpdateDeleteTLLT()
459     {
460         ojbChangeReferenceSetting(MovieImpl.class, "actors", true, LINK, LINK, true);
461         ojbChangeReferenceSetting(MovieImpl.class, "actors2", true, LINK, LINK, true);
462         ojbChangeReferenceSetting(Actor.class, "movies", true, LINK, LINK, false);
463         String JavaDoc postfix = "_testStoreTLLF_" + System.currentTimeMillis();
464         Movie movie = buildMovieWithActorsAndBackReferences(postfix);
465         doTestStoreAddUpdateDeleteTLLX(movie, postfix);
466     }
467
468
469     public void doTestStoreAddUpdateDeleteTLLX(Movie movie, String JavaDoc postfix)
470     {
471         broker.beginTransaction();
472         broker.store(movie);
473         broker.commitTransaction();
474         Identity movieOID = broker.serviceIdentity().buildIdentity(movie);
475
476         /*
477         auto-update setting is LINK, but it behaves same way as OBJECT
478         */

479         broker.clearCache();
480         Query queryMovie = queryMovie(postfix);
481         Collection JavaDoc resultMovie = broker.getCollectionByQuery(queryMovie);
482         assertEquals(3, resultMovie.size());
483
484         Query queryActor = queryActor(postfix);
485         Collection JavaDoc resultActor = broker.getCollectionByQuery(queryActor);
486         assertEquals(3, resultActor.size());
487
488         // first created actor
489
Actor actor = (Actor) movie.getActors().iterator().next();
490         Query queryRole = queryRole(actor, movie);
491         Collection JavaDoc resultRole = broker.getCollectionByQuery(queryRole);
492         assertEquals(5, resultRole.size());
493
494         broker.clearCache();
495         broker.beginTransaction();
496         Movie newMovie = (Movie) broker.getObjectByIdentity(movieOID);
497         newMovie.setTitle("updated_title_" + postfix);
498         Iterator JavaDoc it = newMovie.getActors().iterator();
499         while(it.hasNext())
500         {
501             Actor a = (Actor) it.next();
502             a.setName("updated_name_" + postfix);
503         }
504         ArrayList JavaDoc list = new ArrayList JavaDoc(newMovie.getActors());
505         list.add(new Actor("updated_name_" + postfix));
506         newMovie.setActors(list);
507         broker.store(newMovie);
508         broker.commitTransaction();
509
510         broker.clearCache();
511         queryMovie = queryMovie(postfix);
512         resultMovie = broker.getCollectionByQuery(queryMovie);
513         assertEquals(3, resultMovie.size());
514
515         queryActor = queryActor(postfix);
516         resultActor = broker.getCollectionByQuery(queryActor);
517         assertEquals(4, resultActor.size());
518
519         // first created actor
520
actor = (Actor) movie.getActors().iterator().next();
521         queryRole = queryRole(actor, movie);
522         resultRole = broker.getCollectionByQuery(queryRole);
523         assertEquals(6, resultRole.size());
524
525         newMovie = (Movie) broker.getObjectByIdentity(movieOID);
526         assertEquals("updated_title_"+postfix, newMovie.getTitle());
527         it = newMovie.getActors().iterator();
528         while(it.hasNext())
529         {
530             Actor a = (Actor) it.next();
531             assertEquals("updated_name_" + postfix, a.getName());
532         }
533
534         // now we delete the movie object
535
broker.beginTransaction();
536         broker.delete(newMovie);
537         broker.commitTransaction();
538
539         broker.clearCache();
540         resultMovie = broker.getCollectionByQuery(queryMovie);
541         assertEquals(2, resultMovie.size());
542
543         resultActor = broker.getCollectionByQuery(queryActor);
544         assertEquals(4, resultActor.size());
545
546         resultRole = broker.getCollectionByQuery(queryRole);
547         assertEquals(2, resultRole.size());
548     }
549
550     //============================================================
551
// auto-update / auto-retrieve false
552
//============================================================
553

554     /**
555      * All auto-xxx settings are false, thus expect that all thing must be
556      * done by hand
557      */

558     public void testStoreFFFF()
559     {
560         ojbChangeReferenceSetting(MovieImpl.class, "actors", false, NONE, NONE, false);
561         ojbChangeReferenceSetting(MovieImpl.class, "actors2", false, NONE, NONE, false);
562         ojbChangeReferenceSetting(Actor.class, "movies", false, NONE, NONE, false);
563         String JavaDoc postfix = "" + System.currentTimeMillis();
564         Movie movie = buildMovieWithActors(postfix);
565         doTestStoreFFFX(movie, postfix);
566     }
567
568     /**
569      * All auto-xxx settings are false, thus expect that all thing must be
570      * done by hand, proxy true should not affect anything
571      */

572     public void testStoreFFFT()
573     {
574         ojbChangeReferenceSetting(MovieImpl.class, "actors", false, NONE, NONE, true);
575         ojbChangeReferenceSetting(MovieImpl.class, "actors2", false, NONE, NONE, true);
576         ojbChangeReferenceSetting(Actor.class, "movies", false, NONE, NONE, false);
577         String JavaDoc postfix = "" + System.currentTimeMillis();
578         Movie movie = buildMovieWithActors(postfix);
579         doTestStoreFFFX(movie, postfix);
580     }
581
582     /**
583      * All auto-xxx settings are false, thus expect that all thing must be
584      * done by hand. Actors has a collection of movies too - should be ignored
585      * in this case.
586      */

587     public void testStoreFFFF_2()
588     {
589         ojbChangeReferenceSetting(MovieImpl.class, "actors", false, NONE, NONE, false);
590         ojbChangeReferenceSetting(MovieImpl.class, "actors2", false, NONE, NONE, false);
591         ojbChangeReferenceSetting(Actor.class, "movies", false, NONE, NONE, false);
592         String JavaDoc postfix = "" + System.currentTimeMillis();
593         Movie movie = buildMovieWithActorsAndBackReferences(postfix);
594         doTestStoreFFFX(movie, postfix);
595     }
596
597     /**
598      * All auto-xxx settings are false, thus expect that all thing must be
599      * done by hand, proxy true should not affect anything. Actors has a
600      * collection of movies too - should be ignored in this case.
601      */

602     public void testStoreFFFT_2()
603     {
604         ojbChangeReferenceSetting(MovieImpl.class, "actors", false, NONE, NONE, true);
605         ojbChangeReferenceSetting(MovieImpl.class, "actors2", false, NONE, NONE, true);
606         ojbChangeReferenceSetting(Actor.class, "movies", false, NONE, NONE, false);
607         String JavaDoc postfix = "" + System.currentTimeMillis();
608         Movie movie = buildMovieWithActorsAndBackReferences(postfix);
609         doTestStoreFFFX(movie, postfix);
610     }
611
612     public void doTestStoreFFFX(Movie movie, String JavaDoc postfix)
613     {
614         broker.beginTransaction();
615         broker.store(movie);
616         broker.commitTransaction();
617
618         /*
619         all auto-xxx settings are false, so only the movie object should be
620         stored - no other inserts!
621         */

622         Query queryMovie = queryMovie(postfix);
623         Collection JavaDoc resultMovie = broker.getCollectionByQuery(queryMovie);
624         assertEquals(1, resultMovie.size());
625
626         Query queryActor = queryActor(postfix);
627         Collection JavaDoc resultActor = broker.getCollectionByQuery(queryActor);
628         // auto-update is false, thus we don't expect an Actor object
629
assertEquals(0, resultActor.size());
630
631         Query queryRole = queryRole(null, movie);
632         Collection JavaDoc resultRole = broker.getCollectionByQuery(queryRole);
633         // auto-update is false, thus we don't expect Role objects
634
assertEquals(0, resultRole.size());
635
636         broker.beginTransaction();
637         /*
638         now we store the right-side objects and the intermediary entries
639         */

640         Iterator JavaDoc it = movie.getActors().iterator();
641         while(it.hasNext())
642         {
643             Object JavaDoc actor = it.next();
644             broker.store(actor);
645         }
646         // now both side exist and we can link the references
647
broker.serviceBrokerHelper().link(movie, "actors", true);
648         /*
649         alternative call
650         broker.serviceBrokerHelper().link(movie, true);
651         */

652         broker.commitTransaction();
653
654         /*
655         now we expect all stored objects
656         */

657         resultMovie = broker.getCollectionByQuery(queryMovie);
658         assertEquals(1, resultMovie.size());
659
660         resultActor = broker.getCollectionByQuery(queryActor);
661         assertEquals(3, resultActor.size());
662
663         resultRole = broker.getCollectionByQuery(queryRole);
664         assertEquals(3, resultRole.size());
665
666         broker.clearCache();
667         Identity oid = broker.serviceIdentity().buildIdentity(movie);
668         Movie readMovie = (Movie) broker.getObjectByIdentity(oid);
669         assertNotNull(readMovie);
670         // auto-retrieve false
671
assertTrue(readMovie.getActors() == null || readMovie.getActors().size() == 0);
672
673         broker.retrieveAllReferences(readMovie);
674         assertEquals(3, readMovie.getActors().size());
675
676         resultActor = broker.getCollectionByQuery(queryActor);
677         assertEquals(3, resultActor.size());
678         Actor readActor = (Actor) resultActor.iterator().next();
679         // auto-retrieve false
680
assertTrue(readActor.getMovies() == null || readActor.getMovies().size() == 0);
681         broker.retrieveAllReferences(readActor);
682         assertEquals(1, readActor.getMovies().size());
683
684         // We try to delete all objects
685
// first do unlink the m:n references
686
broker.beginTransaction();
687         broker.serviceBrokerHelper().unlink(readMovie, "actors");
688         /*
689         alternative call
690         broker.serviceBrokerHelper().unlink(readMovie);
691         */

692         broker.commitTransaction();
693
694         broker.clearCache();
695         resultMovie = broker.getCollectionByQuery(queryMovie);
696         assertEquals(1, resultMovie.size());
697
698         resultActor = broker.getCollectionByQuery(queryActor);
699         assertEquals(3, resultActor.size());
700
701         resultRole = broker.getCollectionByQuery(queryRole);
702         assertEquals(0, resultRole.size());
703
704         // now we delete the n- and m-side objects
705
broker.beginTransaction();
706         Iterator JavaDoc iter = movie.getActors().iterator();
707         while(iter.hasNext())
708         {
709             broker.delete(iter.next());
710         }
711         broker.delete(movie);
712         broker.commitTransaction();
713
714         broker.clearCache();
715         resultMovie = broker.getCollectionByQuery(queryMovie);
716         assertEquals(0, resultMovie.size());
717
718         resultActor = broker.getCollectionByQuery(queryActor);
719         assertEquals(0, resultActor.size());
720
721         resultRole = broker.getCollectionByQuery(queryRole);
722         assertEquals(0, resultRole.size());
723     }
724
725     //============================================================
726
// auto-retrieve true / auto-update/auto-delete false
727
//============================================================
728
/**
729      * auto-retrieve is true, other false. proxy true/false should not affect
730      * anything.
731      */

732     public void testStoreTFFF()
733     {
734         ojbChangeReferenceSetting(MovieImpl.class, "actors", true, NONE, NONE, false);
735         ojbChangeReferenceSetting(MovieImpl.class, "actors2", true, NONE, NONE, false);
736         ojbChangeReferenceSetting(Actor.class, "movies", true, NONE, NONE, false);
737         doTestStoreTFFX();
738     }
739
740     /**
741      * auto-retrieve is true, other false. proxy true/false should not affect
742      * anything.
743      */

744     public void testStoreTFFT()
745     {
746         ojbChangeReferenceSetting(MovieImpl.class, "actors", true, NONE, NONE, true);
747         ojbChangeReferenceSetting(MovieImpl.class, "actors2", true, NONE, NONE, true);
748         ojbChangeReferenceSetting(Actor.class, "movies", true, NONE, NONE, false);
749         doTestStoreTFFX();
750     }
751
752     public void doTestStoreTFFX()
753     {
754         String JavaDoc postfix = "" + System.currentTimeMillis();
755         Movie movie = buildMovieWithActorsAndBackReferences(postfix);
756
757         broker.beginTransaction();
758         broker.store(movie);
759         broker.commitTransaction();
760         broker.clearCache();
761
762         /*
763         auto-update settings is false, so only the movie object should be
764         stored - no other inserts!
765         */

766         Query queryMovie = queryMovie(postfix);
767         Collection JavaDoc collMovie = broker.getCollectionByQuery(queryMovie);
768         assertEquals(1, collMovie.size());
769
770         Query queryActor = queryActor(postfix);
771         Collection JavaDoc resultActor = broker.getCollectionByQuery(queryActor);
772         // auto-update is false, thus we don't expect an Actor object
773
assertEquals(0, resultActor.size());
774
775         Query queryRole = queryRole(null, movie);
776         Collection JavaDoc resultRole = broker.getCollectionByQuery(queryRole);
777         // auto-update is false, thus we don't expect Role objects
778
assertEquals(0, resultRole.size());
779
780         broker.beginTransaction();
781         /*
782         now we store the right-side objects and the intermediary entries
783         */

784         Iterator JavaDoc it = movie.getActors().iterator();
785         while(it.hasNext())
786         {
787             Object JavaDoc actor = it.next();
788             broker.store(actor);
789         }
790         // now both side exist and we can link the references
791
broker.serviceBrokerHelper().link(movie, "actors", true);
792         /*
793         alternative call
794         broker.serviceBrokerHelper().link(movie, true);
795         */

796         broker.commitTransaction();
797
798         /*
799         now we expect all stored objects
800         */

801         collMovie = broker.getCollectionByQuery(queryMovie);
802         assertEquals(1, collMovie.size());
803
804         resultActor = broker.getCollectionByQuery(queryActor);
805         assertEquals(3, resultActor.size());
806
807         resultRole = broker.getCollectionByQuery(queryRole);
808         assertEquals(3, resultRole.size());
809
810         broker.clearCache();
811         Identity oid = broker.serviceIdentity().buildIdentity(movie);
812         Movie readMovie = (Movie) broker.getObjectByIdentity(oid);
813         assertNotNull(readMovie);
814         // auto-retrieve true
815
assertTrue(readMovie.getActors() != null);
816         assertEquals(3, readMovie.getActors().size());
817
818         // Now we want to add new objects
819
Actor a1 = new Actor(postfix);
820         Actor a2 = new Actor(postfix);
821         readMovie.addActor(a1);
822         readMovie.addActor(a2);
823         broker.beginTransaction();
824         broker.store(a1);
825         broker.store(a2);
826         broker.serviceBrokerHelper().unlink(readMovie, "actors");
827         broker.serviceBrokerHelper().link(readMovie, "actors", true);
828         broker.commitTransaction();
829
830         collMovie = broker.getCollectionByQuery(queryMovie);
831         assertEquals(1, collMovie.size());
832         resultActor = broker.getCollectionByQuery(queryActor);
833         assertEquals(5, resultActor.size());
834         resultRole = broker.getCollectionByQuery(queryRole);
835         assertEquals(5, resultRole.size());
836         broker.clearCache();
837         oid = broker.serviceIdentity().buildIdentity(movie);
838         readMovie = (Movie) broker.getObjectByIdentity(oid);
839         assertNotNull(readMovie);
840         // auto-retrieve true
841
assertTrue(readMovie.getActors() != null);
842         assertEquals(5, readMovie.getActors().size());
843
844         // We try to delete all objects
845
// first do unlink the m:n references
846
broker.beginTransaction();
847         broker.serviceBrokerHelper().unlink(readMovie, "actors");
848         /*
849         alternative call
850         broker.serviceBrokerHelper().unlink(readMovie);
851         */

852         broker.commitTransaction();
853
854         broker.clearCache();
855         // TODO: replace this with query below (when prefetching bug was solved)
856
//Movie movieLookup = (Movie) broker.getObjectByIdentity(broker.serviceIdentity().buildIdentity(movie));
857
//assertNotNull(movieLookup);
858
collMovie = broker.getCollectionByQuery(queryMovie);
859         assertEquals(1, collMovie.size());
860         readMovie = (Movie) collMovie.iterator().next();
861         assertEquals(0, readMovie.getActors().size());
862
863         resultActor = broker.getCollectionByQuery(queryActor);
864         assertEquals(5, resultActor.size());
865
866         resultRole = broker.getCollectionByQuery(queryRole);
867         assertEquals(0, resultRole.size());
868
869         // now we delete the n- and m-side objects
870
broker.beginTransaction();
871         Iterator JavaDoc iter = resultActor.iterator();
872         while(iter.hasNext())
873         {
874             broker.delete(iter.next());
875         }
876         broker.delete(readMovie);
877         broker.commitTransaction();
878
879         // broker.clearCache();
880
collMovie = broker.getCollectionByQuery(queryMovie);
881         assertEquals(0, collMovie.size());
882
883         resultActor = broker.getCollectionByQuery(queryActor);
884         assertEquals(0, resultActor.size());
885
886         resultRole = broker.getCollectionByQuery(queryRole);
887         assertEquals(0, resultRole.size());
888     }
889
890
891
892
893     //============================================================
894
// auto-update / auto-retrieve true
895
//============================================================
896

897     public void testStoreTTFF()
898     {
899         ojbChangeReferenceSetting(MovieImpl.class, "actors", true, OBJECT, NONE, false);
900         ojbChangeReferenceSetting(MovieImpl.class, "actors2", true, OBJECT, NONE, false);
901         ojbChangeReferenceSetting(Actor.class, "movies", true, OBJECT, NONE, false);
902         doTestStoreTTXX();
903     }
904
905     public void testStoreTTFT()
906     {
907         ojbChangeReferenceSetting(MovieImpl.class, "actors", true, OBJECT, NONE, true);
908         ojbChangeReferenceSetting(MovieImpl.class, "actors2", true, OBJECT, NONE, true);
909         ojbChangeReferenceSetting(Actor.class, "movies", true, OBJECT, NONE, false);
910         doTestStoreTTXX();
911     }
912
913     public void doTestStoreTTXX()
914     {
915         String JavaDoc postfix = "" + System.currentTimeMillis();
916         Movie movie = buildMovieWithActors(postfix);
917
918         broker.beginTransaction();
919         broker.store(movie);
920         broker.commitTransaction();
921         broker.clearCache();
922
923         Query queryMovie = queryMovie(postfix);
924         Collection JavaDoc resultMovie = broker.getCollectionByQuery(queryMovie);
925         assertEquals(1, resultMovie.size());
926
927         Query queryActor = queryActor(postfix);
928         Collection JavaDoc resultActor = broker.getCollectionByQuery(queryActor);
929         assertEquals(3 + 2, resultActor.size());
930
931         Query queryRole = queryRole(null, movie);
932         Collection JavaDoc resultRole = broker.getCollectionByQuery(queryRole);
933         assertEquals(3, resultRole.size());
934
935         broker.clearCache();
936         Identity oid = broker.serviceIdentity().buildIdentity(movie);
937         Movie readMovie = (Movie) broker.getObjectByIdentity(oid);
938         assertNotNull(readMovie);
939         assertEquals(3, readMovie.getActors().size());
940         assertEquals(2, readMovie.getActors2().size());
941     }
942
943     /**
944      * movies with back-references
945      * auto-update = OBJECT
946      */

947     public void testStoreTTFT_2()
948     {
949         ojbChangeReferenceSetting(MovieImpl.class, "actors", true, OBJECT, NONE, true);
950         ojbChangeReferenceSetting(MovieImpl.class, "actors2", true, OBJECT, NONE, true);
951         ojbChangeReferenceSetting(Actor.class, "movies", true, OBJECT, NONE, false);
952         doTestStoreTTXX_2();
953     }
954
955     public void doTestStoreTTXX_2()
956     {
957         String JavaDoc postfix = "" + System.currentTimeMillis();
958         Movie movie = buildMovieWithActorsAndBackReferences(postfix);
959
960         broker.beginTransaction();
961         broker.store(movie);
962         broker.commitTransaction();
963         broker.clearCache();
964
965         Query queryMovie = queryMovie(postfix);
966         Collection JavaDoc resultMovie = broker.getCollectionByQuery(queryMovie);
967         assertEquals(3, resultMovie.size());
968
969         Query queryActor = queryActor(postfix);
970         Collection JavaDoc resultActor = broker.getCollectionByQuery(queryActor);
971         assertEquals(3, resultActor.size());
972
973         Query queryRole = queryRole(null, movie);
974         Collection JavaDoc resultRole = broker.getCollectionByQuery(queryRole);
975         assertEquals(3, resultRole.size());
976
977         broker.clearCache();
978         Identity oid = broker.serviceIdentity().buildIdentity(movie);
979         Movie readMovie = (Movie) broker.getObjectByIdentity(oid);
980         assertNotNull(readMovie);
981         assertEquals(3, readMovie.getActors().size());
982     }
983
984     public void testStoreUpdateTTFF()
985     {
986         ojbChangeReferenceSetting(MovieImpl.class, "actors", true, OBJECT, NONE, false);
987         ojbChangeReferenceSetting(MovieImpl.class, "actors2", true, OBJECT, NONE, false);
988         ojbChangeReferenceSetting(Actor.class, "movies", true, OBJECT, NONE, false);
989         doTestStoreUpdateTTXX();
990     }
991
992     public void testStoreUpdateTTFF_2()
993     {
994         ojbChangeReferenceSetting(MovieImpl.class, "actors", true, OBJECT, NONE, true);
995         ojbChangeReferenceSetting(MovieImpl.class, "actors2", true, OBJECT, NONE, true);
996         ojbChangeReferenceSetting(Actor.class, "movies", true, OBJECT, NONE, false);
997         doTestStoreUpdateTTXX();
998     }
999
1000    public void doTestStoreUpdateTTXX()
1001    {
1002        String JavaDoc postfix = "doTestStoreUpdateTTXX_" + System.currentTimeMillis();
1003        Movie movie = buildMovieWithActors(postfix);
1004
1005        broker.beginTransaction();
1006        broker.store(movie);
1007        broker.commitTransaction();
1008        broker.clearCache();
1009
1010        Query queryMovie = queryMovie(postfix);
1011        Collection JavaDoc resultMovie = broker.getCollectionByQuery(queryMovie);
1012        assertEquals(1, resultMovie.size());
1013
1014        Query queryActor = queryActor(postfix);
1015        Collection JavaDoc resultActor = broker.getCollectionByQuery(queryActor);
1016        assertEquals(3 + 2, resultActor.size());
1017
1018        Query queryRole = queryRole(null, movie);
1019        Collection JavaDoc resultRole = broker.getCollectionByQuery(queryRole);
1020        assertEquals(3, resultRole.size());
1021
1022        //*****************************
1023
// remove all actors
1024
movie.setActors(new ArrayList JavaDoc());
1025        broker.beginTransaction();
1026        broker.store(movie);
1027        broker.commitTransaction();
1028        //*****************************
1029

1030        broker.clearCache();
1031        Identity oid = broker.serviceIdentity().buildIdentity(movie);
1032        movie = (Movie) broker.getObjectByIdentity(oid);
1033
1034        resultMovie = broker.getCollectionByQuery(queryMovie);
1035        assertEquals(1, resultMovie.size());
1036
1037        resultActor = broker.getCollectionByQuery(queryActor);
1038        assertEquals(3 + 2, resultActor.size());
1039
1040        resultRole = broker.getCollectionByQuery(queryRole);
1041        assertEquals(0, resultRole.size());
1042
1043        assertNotNull(movie);
1044        assertEquals(0, movie.getActors().size());
1045    }
1046
1047    public void testStoreUpdateActorTTFF()
1048    {
1049        ojbChangeReferenceSetting(MovieImpl.class, "actors", true, OBJECT, NONE, false);
1050        ojbChangeReferenceSetting(MovieImpl.class, "actors2", true, OBJECT, NONE, false);
1051        ojbChangeReferenceSetting(Actor.class, "movies", true, OBJECT, NONE, false);
1052
1053        String JavaDoc postfix = "" + System.currentTimeMillis();
1054        Actor actor = buildActorWithMovies(postfix);
1055
1056        broker.beginTransaction();
1057        broker.store(actor);
1058        broker.commitTransaction();
1059
1060        broker.clearCache();
1061        Query queryMovie = queryMovie(postfix);
1062        Collection JavaDoc resultMovie = broker.getCollectionByQuery(queryMovie);
1063        assertEquals(3, resultMovie.size());
1064
1065        Query queryActor = queryActor(postfix);
1066        Collection JavaDoc resultActor = broker.getCollectionByQuery(queryActor);
1067        assertEquals(1, resultActor.size());
1068
1069        Query queryRole = queryRole(actor, null);
1070        Collection JavaDoc resultRole = broker.getCollectionByQuery(queryRole);
1071        assertEquals(3, resultRole.size());
1072
1073        broker.clearCache();
1074        Identity oid = broker.serviceIdentity().buildIdentity(actor);
1075        Actor loadedActor = (Actor) broker.getObjectByIdentity(oid);
1076        assertNotNull(loadedActor);
1077        MovieManageableCollection col = loadedActor.getMovies();
1078        assertNotNull(col);
1079        col.get(0);
1080    }
1081
1082    public void testAddNewEntriesTTTF()
1083    {
1084        ojbChangeReferenceSetting(MovieImpl.class, "actors", true, OBJECT, OBJECT, false);
1085        ojbChangeReferenceSetting(MovieImpl.class, "actors2", true, OBJECT, OBJECT, false);
1086        ojbChangeReferenceSetting(Actor.class, "movies", true, OBJECT, OBJECT, false);
1087        doTestAddNewEntries();
1088    }
1089
1090    public void testAddNewEntriesTTTT()
1091    {
1092        ojbChangeReferenceSetting(MovieImpl.class, "actors", true, OBJECT, OBJECT, true);
1093        ojbChangeReferenceSetting(MovieImpl.class, "actors2", true, OBJECT, OBJECT, true);
1094        // default proxy does not work for user defined collection
1095
ojbChangeReferenceSetting(Actor.class, "movies", true, OBJECT, OBJECT, false);
1096        doTestAddNewEntries();
1097    }
1098
1099    public void doTestAddNewEntries()
1100    {
1101        String JavaDoc postfix = "doTestAddNewEntries_" + System.currentTimeMillis();
1102
1103        /*
1104        Returns 1 movie object with 3 actor objects and one actor object with
1105        back-reference to movie object + 2 new movies
1106        */

1107        Movie movie = buildMovieWithActorsAndBackReferences(postfix);
1108        Actor a_1 = new Actor("testAddNewEntries_"+postfix);
1109        Actor a_2 = new Actor("testAddNewEntries_"+postfix);
1110        Actor a_3 = new Actor("testAddNewEntries_"+postfix);
1111        Actor a_4 = new Actor("testAddNewEntries_"+postfix);
1112        /*
1113        all in all we expect 3 movie, 6 actor, 3 role entries after first
1114        store.
1115        */

1116
1117        broker.beginTransaction();
1118        broker.store(movie);
1119        broker.store(a_1);
1120        broker.store(a_2);
1121        broker.store(a_3);
1122        broker.commitTransaction();
1123
1124        broker.clearCache();
1125
1126        Query queryMovie = queryMovie(postfix);
1127        Collection JavaDoc resultMovie = broker.getCollectionByQuery(queryMovie);
1128        assertEquals(3, resultMovie.size());
1129
1130        Query queryActor = queryActor(postfix);
1131        Collection JavaDoc resultActor = broker.getCollectionByQuery(queryActor);
1132        assertEquals(6, resultActor.size());
1133
1134        Query queryRole = queryRole(null, movie);
1135        Collection JavaDoc resultRole = broker.getCollectionByQuery(queryRole);
1136        assertEquals(3, resultRole.size());
1137
1138        broker.clearCache();
1139        Identity oid = broker.serviceIdentity().buildIdentity(movie);
1140        Movie readMovie = (Movie) broker.getObjectByIdentity(oid);
1141        assertNotNull(readMovie);
1142        assertEquals(3, readMovie.getActors().size());
1143        assertEquals(0, readMovie.getActors2().size());
1144
1145        /*
1146        we add 2 existing actor an movie object, thus we expect
1147        3 movie, 6 actor, 5 role entries after store.
1148        And next lookup of movie we expect 5 dependend actor objects
1149        */

1150        movie.getActors().add(a_1);
1151        movie.getActors().add(a_2);
1152        // add new actor object
1153
movie.getActors().add(a_4);
1154        broker.beginTransaction();
1155        broker.store(movie);
1156        broker.commitTransaction();
1157
1158        broker.clearCache();
1159
1160        queryMovie = queryMovie(postfix);
1161        resultMovie = broker.getCollectionByQuery(queryMovie);
1162        assertEquals(3, resultMovie.size());
1163
1164        queryActor = queryActor(postfix);
1165        resultActor = broker.getCollectionByQuery(queryActor);
1166        assertEquals(7, resultActor.size());
1167
1168        queryRole = queryRole(null, movie);
1169        resultRole = broker.getCollectionByQuery(queryRole);
1170        assertEquals(6, resultRole.size());
1171
1172        broker.clearCache();
1173        oid = broker.serviceIdentity().buildIdentity(movie);
1174        readMovie = (Movie) broker.getObjectByIdentity(oid);
1175        assertNotNull(readMovie);
1176        assertEquals(6, readMovie.getActors().size());
1177
1178        /*
1179        on delete we expect that all entries are deleted except the single
1180        actor which have no references to any movie object
1181        */

1182        broker.beginTransaction();
1183        broker.delete(movie);
1184        broker.commitTransaction();
1185
1186        broker.clearCache();
1187        resultMovie = broker.getCollectionByQuery(queryMovie);
1188        assertEquals(0, resultMovie.size());
1189
1190        resultActor = broker.getCollectionByQuery(queryActor);
1191        assertEquals(1, resultActor.size());
1192
1193        resultRole = broker.getCollectionByQuery(queryRole);
1194        assertEquals(0, resultRole.size());
1195    }
1196
1197    //=======================================================================
1198
// helper methods
1199
//=======================================================================
1200
Query queryMovie(String JavaDoc postfix)
1201    {
1202        Criteria c = new Criteria();
1203        c.addLike("idStr", "%" + postfix + "%");
1204        return QueryFactory.newQuery(Movie.class, c);
1205    }
1206
1207    Query queryActor(String JavaDoc postfix)
1208    {
1209        Criteria c = new Criteria();
1210        c.addLike("name", "%" + postfix + "%");
1211        return QueryFactory.newQuery(Actor.class, c);
1212    }
1213
1214    Query queryRole(Actor actor, Movie movie)
1215    {
1216        Criteria c = new Criteria();
1217        if(actor != null) c.addEqualTo("actorId", actor.getId());
1218        if(movie != null && actor != null)
1219        {
1220            Criteria c2 = new Criteria();
1221            c2.addEqualTo("movieIntId", movie.getIdInt());
1222            c2.addEqualTo("movieStrId", movie.getIdStr());
1223            c.addOrCriteria(c2);
1224        }
1225        else if(movie != null)
1226        {
1227            c.addEqualTo("movieIntId", movie.getIdInt());
1228            c.addEqualTo("movieStrId", movie.getIdStr());
1229        }
1230        return QueryFactory.newQuery(Role.class, c);
1231    }
1232
1233    Query roleQueryActorOrMovieMatch(Actor actor, Movie movie)
1234    {
1235        Criteria c_1 = new Criteria();
1236        Criteria c_2 = new Criteria();
1237        if(actor != null) c_1.addEqualTo("actorId", actor.getId());
1238        if(movie != null)
1239        {
1240            c_2.addEqualTo("movieIntId", movie.getIdInt());
1241            c_2.addEqualTo("movieStrId", movie.getIdStr());
1242        }
1243        if(actor != null)
1244        {
1245            c_2.addOrCriteria(c_1);
1246        }
1247        else
1248        {
1249            c_2 = c_1;
1250        }
1251        return QueryFactory.newQuery(Role.class, c_2);
1252    }
1253
1254    /**
1255     * Returns 1 movie object with 3 actor objects in actors-collection
1256     * and 2 actor objects in actors2-collection
1257     */

1258    Movie buildMovieWithActors(String JavaDoc postfixId)
1259    {
1260        Movie m = new MovieImpl(postfixId,
1261                "Dr. Strangelove or: How I Learned to Stop Worrying and Love the Bomb " + postfixId,
1262                "An insane general starts a process to nuclear holocaust that a war" +
1263                " room of politicians and generals frantically try to stop. " + postfixId);
1264
1265        Actor a1 = new Actor("Peter Sellers " + postfixId);
1266        Actor a2 = new Actor("George C. Scott " + postfixId);
1267        Actor a3 = new Actor("Sterling Hayden " + postfixId);
1268        ArrayList JavaDoc list = new ArrayList JavaDoc();
1269        list.add(a1);
1270        list.add(a2);
1271        list.add(a3);
1272        m.setActors(list);
1273
1274        Actor a4 = new Actor("Actor 2 A " + postfixId);
1275        Actor a5 = new Actor("Actor 2 B " + postfixId);
1276        ArrayList JavaDoc list2 = new ArrayList JavaDoc();
1277        list2.add(a4);
1278        list2.add(a5);
1279        m.setActors2(list2);
1280
1281        return m;
1282    }
1283
1284    /**
1285     * Returns 1 movie object with 3 actor objects
1286     */

1287    Movie buildMovieWithActors(String JavaDoc postfixId, int actorCount)
1288    {
1289        Movie m = new MovieImpl(postfixId, "Movie with "+ actorCount+" actors_" + postfixId, "none");
1290
1291        ArrayList JavaDoc list = new ArrayList JavaDoc();
1292        for(int i = 0; i < actorCount; i++)
1293        {
1294            Actor a = new Actor("A bad actor_" + postfixId);
1295            list.add(a);
1296        }
1297        m.setActors(list);
1298        return m;
1299    }
1300
1301    /**
1302     * Returns 1 movie object with 3 actor objects
1303     */

1304    Actor buildActorWithMovies(String JavaDoc postfixId, int movieCount)
1305    {
1306        Actor a = new Actor(postfixId+"_Actor play in "+ movieCount+" movies");
1307
1308        MovieManageableCollection list = new MovieManageableCollection();
1309        for(int i = 0; i < movieCount; i++)
1310        {
1311            Movie m = new MovieImpl(postfixId, "A bad movie_" + postfixId, "none");
1312            list.add(m);
1313        }
1314        a.setMovies(list);
1315        return a;
1316    }
1317
1318    /**
1319     * Returns 1 movie object with 3 actor objects and one actor object with
1320     * back-reference to movie object + 2 new movies
1321     */

1322    Movie buildMovieWithActorsAndBackReferences(String JavaDoc postfixId)
1323    {
1324        Movie m = new MovieImpl(postfixId,
1325                "Dr. Strangelove or: How I Learned to Stop Worrying and Love the Bomb " + postfixId,
1326                "An insane general starts a process to nuclear holocaust that a war" +
1327                " room of politicians and generals frantically try to stop. " + postfixId);
1328        Actor a1 = new Actor("Peter Sellers " + postfixId);
1329        Actor a2 = new Actor("George C. Scott " + postfixId);
1330        Actor a3 = new Actor("Sterling Hayden " + postfixId);
1331        ArrayList JavaDoc list = new ArrayList JavaDoc();
1332        list.add(a1);
1333        list.add(a2);
1334        list.add(a3);
1335        m.setActors(list);
1336
1337        Movie m1 = new MovieImpl(postfixId + "", "A Shot in the Dark",
1338                "As murder follows murder, beautiful Maria is the obvious suspect...");
1339        Movie m2 = new MovieImpl(postfixId + "", "The Pink Panther",
1340                " In the first movie starring Peter Sellers as the bumbling Inspector Clouseau...");
1341
1342        MovieManageableCollection mlist1 = new MovieManageableCollection();
1343        mlist1.add(m);
1344        mlist1.add(m1);
1345        mlist1.add(m2);
1346        MovieManageableCollection mlist2 = new MovieManageableCollection();
1347        MovieManageableCollection mlist3 = new MovieManageableCollection();
1348        a1.setMovies(mlist1);
1349        a2.setMovies(mlist2);
1350        a3.setMovies(mlist3);
1351
1352        return m;
1353    }
1354
1355    Actor buildActorWithMovies(String JavaDoc postfixId)
1356    {
1357        Actor a = new Actor("John Cusack" + postfixId);
1358        MovieManageableCollection list = new MovieManageableCollection();
1359        list.add(new MovieImpl("a_" + postfixId, "High Fidelity", "A comedy about fear of commitment, hating your job..." + postfixId));
1360        list.add(new MovieImpl("b_" + postfixId, "Identity", "When a nasty storm hits a hotel, ten strangers are stranded within ..." + postfixId));
1361        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));
1362        a.setMovies(list);
1363        return a;
1364    }
1365
1366
1367
1368    //=======================================================================
1369
// Inner classes, persistence capable test classes
1370
//=======================================================================
1371
public static class MovieManageableCollection implements ManageableCollection
1372    {
1373        private ArrayList JavaDoc list = new ArrayList JavaDoc();
1374
1375        public void ojbAdd(Object JavaDoc anObject)
1376        {
1377            list.add(anObject);
1378        }
1379
1380        public void ojbAddAll(ManageableCollection otherCollection)
1381        {
1382            Iterator JavaDoc it = otherCollection.ojbIterator();
1383            while(it.hasNext())
1384            {
1385                list.add(it.next());
1386            }
1387        }
1388
1389        public Iterator JavaDoc ojbIterator()
1390        {
1391            return list.iterator();
1392        }
1393
1394        public void afterStore(PersistenceBroker broker) throws PersistenceBrokerException
1395        {
1396        }
1397
1398        public Iterator JavaDoc iterator()
1399        {
1400            return list.iterator();
1401        }
1402
1403        public int size()
1404        {
1405            return list.size();
1406        }
1407
1408        public boolean isEmpty()
1409        {
1410            return list.isEmpty();
1411        }
1412
1413        public void clear()
1414        {
1415            list.clear();
1416        }
1417
1418        public boolean add(Movie movie)
1419        {
1420            return list.add(movie);
1421        }
1422
1423        public boolean remove(Movie movie)
1424        {
1425            return list.remove(movie);
1426        }
1427
1428        public boolean contains(Movie movie)
1429        {
1430            return list.contains(movie);
1431        }
1432
1433        public Movie get(int index)
1434        {
1435            return (Movie) list.get(index);
1436        }
1437    }
1438
1439    //===================================================================
1440
// inner class
1441
//===================================================================
1442
public static class Actor
1443    {
1444        private Integer JavaDoc id;
1445        private Integer JavaDoc id2;
1446        private String JavaDoc name;
1447        private MovieManageableCollection movies;
1448
1449        public Actor()
1450        {
1451        }
1452
1453        public Actor(String JavaDoc name)
1454        {
1455            this.name = name;
1456        }
1457
1458        public MovieManageableCollection getMovies()
1459        {
1460            return movies;
1461        }
1462
1463        public void setMovies(MovieManageableCollection movies)
1464        {
1465            this.movies = movies;
1466        }
1467
1468        public void addMovie(Movie m)
1469        {
1470            if(movies == null)
1471            {
1472                movies = new MovieManageableCollection();
1473            }
1474            movies.add(m);
1475        }
1476
1477        public Integer JavaDoc getId()
1478        {
1479            return id;
1480        }
1481
1482        public void setId(Integer JavaDoc id)
1483        {
1484            this.id = id;
1485        }
1486
1487        public Integer JavaDoc getId2()
1488        {
1489            return id2;
1490        }
1491
1492        public void setId2(Integer JavaDoc id2)
1493        {
1494            this.id2 = id2;
1495        }
1496
1497        public String JavaDoc getName()
1498        {
1499            return name;
1500        }
1501
1502        public void setName(String JavaDoc name)
1503        {
1504            this.name = name;
1505        }
1506
1507        public String JavaDoc toString()
1508        {
1509            return ToStringBuilder.reflectionToString(this);
1510        }
1511    }
1512
1513    //===================================================================
1514
// inner class
1515
//===================================================================
1516
public static interface Movie
1517    {
1518        public Collection JavaDoc getActors();
1519        public void setActors(Collection JavaDoc actors);
1520        public void addActor(Actor a);
1521
1522        public Collection JavaDoc getActors2();
1523        public void setActors2(Collection JavaDoc actors);
1524
1525        public List JavaDoc getProducers();
1526        public void setProducers(List JavaDoc producers);
1527        public void addProducer(Producer p);
1528
1529        public Integer JavaDoc getIdInt2();
1530        public Integer JavaDoc getIdInt();
1531
1532        public void setIdInt2(Integer JavaDoc id2Int);
1533        public void setIdInt(Integer JavaDoc idInt);
1534
1535        public String JavaDoc getIdStr();
1536        public void setIdStr(String JavaDoc idStr);
1537
1538        public String JavaDoc getTitle();
1539        public void setTitle(String JavaDoc title);
1540
1541        public String JavaDoc getDescription();
1542        public void setDescription(String JavaDoc description);
1543    }
1544
1545    //===================================================================
1546
// inner class
1547
//===================================================================
1548
public static class MovieImpl implements Movie
1549    {
1550        private Integer JavaDoc idInt;
1551        private Integer JavaDoc idInt2;
1552        private String JavaDoc idStr;
1553
1554        private String JavaDoc title;
1555        private String JavaDoc description;
1556        private Collection JavaDoc actors;
1557        private Collection JavaDoc actors2;
1558        private List JavaDoc producers;
1559
1560        public MovieImpl()
1561        {
1562        }
1563
1564        public MovieImpl(String JavaDoc idStr, String JavaDoc title, String JavaDoc description)
1565        {
1566            this.idStr = idStr;
1567            this.title = title;
1568            this.description = description;
1569        }
1570
1571        public List JavaDoc getProducers()
1572        {
1573            return producers;
1574        }
1575
1576        public void setProducers(List JavaDoc producers)
1577        {
1578            this.producers = producers;
1579        }
1580
1581        public void addProducer(Producer p)
1582        {
1583            if(producers == null)
1584            {
1585                producers = new ArrayList JavaDoc();
1586            }
1587            producers.add(p);
1588            if(p.getMovies() == null || !p.getMovies().contains(this))
1589            {
1590                p.addMovie(this);
1591            }
1592        }
1593
1594        public Collection JavaDoc getActors()
1595        {
1596            return actors;
1597        }
1598
1599        public void setActors(Collection JavaDoc actors)
1600        {
1601            this.actors = actors;
1602        }
1603
1604        public void addActor(Actor a)
1605        {
1606            if(actors == null)
1607            {
1608                actors = new ArrayList JavaDoc();
1609            }
1610            actors.add(a);
1611        }
1612
1613        public Collection JavaDoc getActors2()
1614        {
1615            return actors2;
1616        }
1617
1618        public void setActors2(Collection JavaDoc actors)
1619        {
1620            this.actors2 = actors;
1621        }
1622
1623        public Integer JavaDoc getIdInt()
1624        {
1625            return idInt;
1626        }
1627
1628        public void setIdInt(Integer JavaDoc idInt)
1629        {
1630            this.idInt = idInt;
1631        }
1632
1633        public Integer JavaDoc getIdInt2()
1634        {
1635            return idInt2;
1636        }
1637
1638        public void setIdInt2(Integer JavaDoc idInt2)
1639        {
1640            this.idInt2 = idInt2;
1641        }
1642
1643        public String JavaDoc getIdStr()
1644        {
1645            return idStr;
1646        }
1647
1648        public void setIdStr(String JavaDoc idStr)
1649        {
1650            this.idStr = idStr;
1651        }
1652
1653        public String JavaDoc getTitle()
1654        {
1655            return title;
1656        }
1657
1658        public void setTitle(String JavaDoc title)
1659        {
1660            this.title = title;
1661        }
1662
1663        public String JavaDoc getDescription()
1664        {
1665            return description;
1666        }
1667
1668        public void setDescription(String JavaDoc description)
1669        {
1670            this.description = description;
1671        }
1672
1673        public int hashCode()
1674        {
1675            return new HashCodeBuilder().append(idInt).append(idInt2).append(idStr).hashCode();
1676        }
1677
1678        public boolean equals(Object JavaDoc obj)
1679        {
1680            boolean result = false;
1681            if(obj instanceof MovieImpl)
1682            {
1683                MovieImpl other = (MovieImpl) obj;
1684                result = new EqualsBuilder().append(idInt, other.idInt).append(idInt2, other.idInt2).append(idStr, other.idStr).isEquals();
1685            }
1686            return result;
1687        }
1688
1689        public String JavaDoc toString()
1690        {
1691            return ToStringBuilder.reflectionToString(this);
1692        }
1693    }
1694
1695    //===================================================================
1696
// inner class
1697
//===================================================================
1698
public static class Role
1699    {
1700        private Integer JavaDoc actorId;
1701        private Integer JavaDoc actorId2;
1702        private Integer JavaDoc movieIntId;
1703        private Integer JavaDoc movieIntId2;
1704        private String JavaDoc movieStrId;
1705
1706        public Role()
1707        {
1708        }
1709
1710        public Integer JavaDoc getActorId()
1711        {
1712            return actorId;
1713        }
1714
1715        public void setActorId(Integer JavaDoc actorId)
1716        {
1717            this.actorId = actorId;
1718        }
1719
1720        public Integer JavaDoc getMovieIntId()
1721        {
1722            return movieIntId;
1723        }
1724
1725        public Integer JavaDoc getMovieIntId2()
1726        {
1727            return movieIntId2;
1728        }
1729
1730        public void setMovieIntId2(Integer JavaDoc movieIntId2)
1731        {
1732            this.movieIntId2 = movieIntId2;
1733        }
1734
1735        public Integer JavaDoc getActorId2()
1736        {
1737            return actorId2;
1738        }
1739
1740        public void setActorId2(Integer JavaDoc actorId2)
1741        {
1742            this.actorId2 = actorId2;
1743        }
1744
1745        public void setMovieIntId(Integer JavaDoc movieIntId)
1746        {
1747            this.movieIntId = movieIntId;
1748        }
1749
1750        public String JavaDoc getMovieStrId()
1751        {
1752            return movieStrId;
1753        }
1754
1755        public void setMovieStrId(String JavaDoc movieStrId)
1756        {
1757            this.movieStrId = movieStrId;
1758        }
1759
1760        public String JavaDoc toString()
1761        {
1762            return ToStringBuilder.reflectionToString(this);
1763        }
1764    }
1765
1766    //===================================================================
1767
// inner class
1768
//===================================================================
1769
/**
1770     * This class has a m:n relation with Movie and also use a composite
1771     * key.
1772     */

1773    public static class Producer
1774    {
1775        private Integer JavaDoc id;
1776        private String JavaDoc idStr;
1777        private String JavaDoc name;
1778        private List JavaDoc movies;
1779
1780        public Producer()
1781        {
1782        }
1783
1784        public Producer(String JavaDoc idStr, String JavaDoc name)
1785        {
1786            this.idStr = idStr;
1787            this.name = name;
1788        }
1789
1790        public List JavaDoc getMovies()
1791        {
1792            return movies;
1793        }
1794
1795        public void setMovies(List JavaDoc movies)
1796        {
1797            this.movies = movies;
1798        }
1799
1800        public void addMovie(Movie movie)
1801        {
1802            if(movies == null)
1803            {
1804                movies = new ArrayList JavaDoc();
1805            }
1806            movies.add(movie);
1807            if(movie.getProducers() == null || !movie.getProducers().contains(this))
1808            {
1809                movie.addProducer(this);
1810            }
1811        }
1812
1813        public Integer JavaDoc getId()
1814        {
1815            return id;
1816        }
1817
1818        public void setId(Integer JavaDoc id)
1819        {
1820            this.id = id;
1821        }
1822
1823        public String JavaDoc getIdStr()
1824        {
1825            return idStr;
1826        }
1827
1828        public void setIdStr(String JavaDoc idStr)
1829        {
1830            this.idStr = idStr;
1831        }
1832
1833        public String JavaDoc getName()
1834        {
1835            return name;
1836        }
1837
1838        public void setName(String JavaDoc name)
1839        {
1840            this.name = name;
1841        }
1842
1843        public int hashCode()
1844        {
1845            return new HashCodeBuilder().append(id).append(idStr).hashCode();
1846        }
1847
1848        public boolean equals(Object JavaDoc obj)
1849        {
1850            boolean result = false;
1851            if(obj instanceof Producer)
1852            {
1853                Producer other = (Producer) obj;
1854                result = new EqualsBuilder().append(id, other.id).append(idStr, other.idStr).isEquals();
1855            }
1856            return result;
1857        }
1858
1859        public String JavaDoc toString()
1860        {
1861            return ToStringBuilder.reflectionToString(this);
1862        }
1863    }
1864}
Popular Tags