KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.ojb.broker;
2
3 import java.io.Serializable JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.Collection JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8
9 import org.apache.commons.lang.SerializationUtils;
10 import org.apache.ojb.broker.metadata.ClassDescriptor;
11 import org.apache.ojb.broker.metadata.CollectionDescriptor;
12 import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
13 import org.apache.ojb.broker.query.Criteria;
14 import org.apache.ojb.broker.query.Query;
15 import org.apache.ojb.broker.query.QueryFactory;
16 import org.apache.ojb.junit.PBTestCase;
17
18 /**
19  * Test case for collection handling.
20  *
21  * IMPORTANT NOTE: The global runtime metadata changes made by this test case
22  * are NOT recommended in multithreaded environments, because they are global
23  * and each thread will be affected.
24  *
25  * @author <a HREF="mailto:armin@codeAuLait.de">Armin Waibel</a>
26  * @version $Id: CollectionTest2.java,v 1.5.2.1 2004/07/27 00:31:50 arminw Exp $
27  */

28 public class CollectionTest2 extends PBTestCase
29 {
30     public static void main(String JavaDoc[] args)
31     {
32         String JavaDoc[] arr = {CollectionTest2.class.getName()};
33         junit.textui.TestRunner.main(arr);
34     }
35
36     public void tearDown() throws Exception JavaDoc
37     {
38         if(broker != null)
39         {
40             changeAutoSetting(Project.class, "subProjects", false, false, false, false);
41             changeAutoSetting(Project.class, "developers", false, false, false, false);
42             changeAutoSetting(SubProject.class, "project", false, false, false, false);
43             broker.close();
44         }
45         super.tearDown();
46     }
47
48     public void testAutoUpdateDeleteSettings()
49     {
50         changeAutoSetting(Project.class, "subProjects", false, false, false, false);
51         CollectionDescriptor ord = broker.getClassDescriptor(Project.class)
52                 .getCollectionDescriptorByName("subProjects");
53         assertEquals(CollectionDescriptor.CASCADE_LINK, ord.getCascadingStore());
54         assertEquals(CollectionDescriptor.CASCADE_NONE, ord.getCascadingDelete());
55         assertEquals(false, ord.getCascadeStore());
56         assertEquals(false, ord.getCascadeDelete());
57
58         changeAutoSetting(Project.class, "subProjects", false, true, true, false);
59         ord = broker.getClassDescriptor(Project.class)
60                 .getCollectionDescriptorByName("subProjects");
61         assertEquals(ObjectReferenceDescriptor.CASCADE_OBJECT, ord.getCascadingStore());
62         assertEquals(ObjectReferenceDescriptor.CASCADE_OBJECT, ord.getCascadingDelete());
63         assertEquals(true, ord.getCascadeStore());
64         assertEquals(true, ord.getCascadeDelete());
65     }
66
67     public void testStoreUpdateDelete_1a()
68     {
69         changeAutoSetting(Project.class, "subProjects", false, false, false, false);
70         changeAutoSetting(Project.class, "developers", false, false, false, false);
71         changeAutoSetting(SubProject.class, "project", false, false, false, false);
72         doTestStoreUpdateDelete_1();
73     }
74
75     public void testStoreUpdateDelete_1b()
76     {
77         changeAutoSetting(Project.class, "subProjects", false, false, false, false);
78         changeAutoSetting(Project.class, "developers", false, false, false, false);
79         changeAutoSetting(SubProject.class, "project", false, false, false, false);
80         doTestStoreUpdateDelete_1();
81     }
82
83     public void doTestStoreUpdateDelete_1()
84     {
85         String JavaDoc name = "testStoreUpdateDelete_1" + System.currentTimeMillis();
86         changeAutoSetting(Project.class, "subProjects", false, false, false, false);
87         changeAutoSetting(Project.class, "developers", false, false, false, false);
88         changeAutoSetting(SubProject.class, "project", false, false, false, false);
89
90         Developer dev1 = new Developer(name);
91         Developer dev2 = new Developer(name);
92         ArrayList JavaDoc devList = new ArrayList JavaDoc();
93         devList.add(dev1);
94         devList.add(dev2);
95
96         SubProject sub1 = new SubProject(name, null);
97         SubProject sub2 = new SubProject(name, null);
98         ArrayList JavaDoc subList = new ArrayList JavaDoc();
99         subList.add(sub1);
100         subList.add(sub2);
101
102         Project pro = new Project(name, subList, devList);
103         sub1.setProject(pro);
104         sub2.setProject(pro);
105         pro.setDevelopers(devList);
106         pro.setSubProjects(subList);
107         sub1.setProject(pro);
108         sub2.setProject(pro);
109
110         Query queryProject = createQueryFor(Project.class, "name", name);
111         Query querySubProject = createQueryFor(SubProject.class, "name", name);
112         Query queryDeveloper = createQueryFor(Developer.class, "name", name);
113
114         //*****************************************
115
// insert
116
//*****************************************
117
broker.beginTransaction();
118         broker.store(pro);
119         broker.commitTransaction();
120
121         broker.clearCache();
122         Collection JavaDoc result = broker.getCollectionByQuery(queryProject);
123         assertEquals(1, result.size());
124         assertNotNull(result.iterator().next());
125         result = broker.getCollectionByQuery(querySubProject);
126         assertEquals(0, result.size());
127         result = broker.getCollectionByQuery(queryDeveloper);
128         assertEquals(0, result.size());
129
130         broker.beginTransaction();
131         broker.store(sub1);
132         broker.store(sub2);
133         broker.store(dev1);
134         broker.store(dev2);
135         broker.commitTransaction();
136
137         broker.clearCache();
138         result = broker.getCollectionByQuery(queryProject);
139         assertEquals(1, result.size());
140         assertNotNull(result.iterator().next());
141         result = broker.getCollectionByQuery(querySubProject);
142         assertEquals(2, result.size());
143         result = broker.getCollectionByQuery(queryDeveloper);
144         assertEquals(2, result.size());
145
146         Identity proOid = new Identity(pro, broker);
147         Project newPro = (Project) broker.getObjectByIdentity(proOid);
148         assertNull(newPro.getDevelopers());
149         assertNull(newPro.getSubProjects());
150         //assertEquals(0, newPro.getDevelopers().size());
151
//assertEquals(0, newPro.getSubProjects().size());
152
broker.retrieveAllReferences(newPro);
153         assertNotNull(newPro.getDevelopers());
154         assertNotNull(newPro.getSubProjects());
155         assertEquals(2, newPro.getDevelopers().size());
156         assertEquals(2, newPro.getSubProjects().size());
157         SubProject sub = (SubProject) newPro.getSubProjects().iterator().next();
158         assertNotNull(sub);
159         assertNull(sub.getProject());
160         broker.retrieveAllReferences(sub);
161         assertNotNull(sub.getProject());
162         assertEquals(sub.getProject().getId(), newPro.getId());
163
164         //*****************************************
165
// update
166
//*****************************************
167
broker.clearCache();
168         newPro = (Project) broker.getObjectByIdentity(proOid);
169         newPro.setName("updated_" + name);
170         broker.retrieveAllReferences(newPro);
171
172         broker.beginTransaction();
173         broker.store(newPro);
174         Iterator JavaDoc it = newPro.getSubProjects().iterator();
175         while(it.hasNext())
176         {
177             SubProject subProject = (SubProject) it.next();
178             broker.retrieveAllReferences(subProject);
179             subProject.setName("updated_" + name);
180             broker.store(subProject);
181         }
182         broker.commitTransaction();
183
184         broker.clearCache();
185         newPro = (Project) broker.getObjectByIdentity(proOid);
186         assertEquals("updated_" + name, newPro.getName());
187         assertNull(newPro.getDevelopers());
188         assertNull(newPro.getSubProjects());
189         broker.retrieveAllReferences(newPro);
190         assertNotNull(newPro.getDevelopers());
191         assertNotNull(newPro.getSubProjects());
192         assertEquals(2, newPro.getDevelopers().size());
193         assertEquals(2, newPro.getSubProjects().size());
194         it = newPro.getSubProjects().iterator();
195         while(it.hasNext())
196         {
197             SubProject subProject = (SubProject) it.next();
198             assertEquals("updated_" + name, subProject.getName());
199         }
200
201
202         //*****************************************
203
// delete
204
//*****************************************
205
broker.clearCache();
206         broker.beginTransaction();
207         broker.delete(sub1);
208         broker.delete(sub2);
209         broker.delete(dev1);
210         broker.delete(dev2);
211         broker.delete(pro);
212         broker.commitTransaction();
213
214         result = broker.getCollectionByQuery(queryProject);
215         assertEquals(0, result.size());
216         result = broker.getCollectionByQuery(querySubProject);
217         assertEquals(0, result.size());
218         result = broker.getCollectionByQuery(queryDeveloper);
219         assertEquals(0, result.size());
220     }
221
222     public void testStoreUpdateDelete_2a()
223     {
224         changeAutoSetting(Project.class, "subProjects", true, true, true, false);
225         changeAutoSetting(Project.class, "developers", true, true, true, false);
226         changeAutoSetting(SubProject.class, "project", true, true, true, false);
227         doTestStoreUpdateDelete_2();
228     }
229
230     public void testStoreUpdateDelete_2b()
231     {
232         changeAutoSetting(Project.class, "subProjects", true, true, true, true);
233         changeAutoSetting(Project.class, "developers", true, true, true, true);
234         changeAutoSetting(SubProject.class, "project", true, true, true, true);
235         doTestStoreUpdateDelete_2();
236     }
237
238     public void doTestStoreUpdateDelete_2()
239     {
240         String JavaDoc name = "testStoreUpdateDelete_2" + System.currentTimeMillis();
241
242         Developer dev1 = new Developer(name);
243         Developer dev2 = new Developer(name);
244         ArrayList JavaDoc devList = new ArrayList JavaDoc();
245         devList.add(dev1);
246         devList.add(dev2);
247
248         SubProject sub1 = new SubProject(name, null);
249         SubProject sub2 = new SubProject(name, null);
250         ArrayList JavaDoc subList = new ArrayList JavaDoc();
251         subList.add(sub1);
252         subList.add(sub2);
253
254         Project pro = new Project(name, subList, devList);
255         sub1.setProject(pro);
256         sub2.setProject(pro);
257
258         sub1.setProject(pro);
259         sub2.setProject(pro);
260         pro.setSubProjects(subList);
261         pro.setDevelopers(devList);
262
263         Query queryProject = createQueryFor(Project.class, "name", name);
264         Query querySubProject = createQueryFor(SubProject.class, "name", name);
265         Query queryDeveloper = createQueryFor(Developer.class, "name", name);
266
267         //*****************************************
268
// insert
269
//*****************************************
270
broker.beginTransaction();
271         broker.store(pro);
272         broker.commitTransaction();
273
274         broker.clearCache();
275         Collection JavaDoc result = broker.getCollectionByQuery(queryProject);
276         assertEquals(1, result.size());
277         assertNotNull(result.iterator().next());
278         result = broker.getCollectionByQuery(querySubProject);
279         assertEquals(2, result.size());
280         result = broker.getCollectionByQuery(queryDeveloper);
281         assertEquals(2, result.size());
282
283         Identity proOid = new Identity(pro, broker);
284         Project newPro = (Project) broker.getObjectByIdentity(proOid);
285         assertNotNull(newPro.getDevelopers());
286         assertNotNull(newPro.getSubProjects());
287         assertEquals(2, newPro.getDevelopers().size());
288         assertEquals(2, newPro.getSubProjects().size());
289         SubProject sub = (SubProject) newPro.getSubProjects().iterator().next();
290         assertNotNull(sub);
291         assertNotNull(sub.getProject());
292         assertEquals(sub.getProject().getId(), newPro.getId());
293
294         //*****************************************
295
// update
296
//*****************************************
297
broker.clearCache();
298         newPro = (Project) broker.getObjectByIdentity(proOid);
299         newPro.setName("updated_" + name);
300         broker.beginTransaction();
301         Iterator JavaDoc it = newPro.getSubProjects().iterator();
302         while(it.hasNext())
303         {
304             SubProject subProject = (SubProject) it.next();
305             subProject.setName("updated_" + name);
306         }
307         broker.store(newPro);
308         broker.commitTransaction();
309
310         broker.clearCache();
311         newPro = (Project) broker.getObjectByIdentity(proOid);
312         assertEquals("updated_" + name, newPro.getName());
313         assertNotNull(newPro.getDevelopers());
314         assertNotNull(newPro.getSubProjects());
315         assertEquals(2, newPro.getDevelopers().size());
316         assertEquals(2, newPro.getSubProjects().size());
317         it = newPro.getSubProjects().iterator();
318         while(it.hasNext())
319         {
320             SubProject subProject = (SubProject) it.next();
321             assertEquals("updated_" + name, subProject.getName());
322         }
323
324         //*****************************************
325
// delete
326
//*****************************************
327
broker.clearCache();
328         broker.beginTransaction();
329         broker.delete(pro);
330         broker.commitTransaction();
331
332         result = broker.getCollectionByQuery(queryProject);
333         assertEquals(0, result.size());
334         result = broker.getCollectionByQuery(querySubProject);
335         assertEquals(0, result.size());
336         result = broker.getCollectionByQuery(queryDeveloper);
337         assertEquals(0, result.size());
338     }
339
340     public void testStoreUpdateDeleteSerialize_1a()
341     {
342         changeAutoSetting(Project.class, "subProjects", true, true, true, false);
343         changeAutoSetting(Project.class, "developers", true, true, true, false);
344         changeAutoSetting(SubProject.class, "project", true, true, true, false);
345         doTestStoreUpdateDeleteSerialize();
346     }
347
348     public void testStoreUpdateDeleteSerialize_1b()
349     {
350         changeAutoSetting(Project.class, "subProjects", true, true, true, true);
351         changeAutoSetting(Project.class, "developers", true, true, true, true);
352         changeAutoSetting(SubProject.class, "project", true, true, true, true);
353         doTestStoreUpdateDeleteSerialize();
354     }
355
356     public void doTestStoreUpdateDeleteSerialize()
357     {
358         String JavaDoc name = "testStoreUpdateDelete_2" + System.currentTimeMillis();
359
360         Developer dev1 = new Developer(name);
361         Developer dev2 = new Developer(name);
362         ArrayList JavaDoc devList = new ArrayList JavaDoc();
363         devList.add(dev1);
364         devList.add(dev2);
365
366         SubProject sub1 = new SubProject(name, null);
367         SubProject sub2 = new SubProject(name, null);
368         ArrayList JavaDoc subList = new ArrayList JavaDoc();
369         subList.add(sub1);
370         subList.add(sub2);
371
372         Project pro = new Project(name, subList, devList);
373         sub1.setProject(pro);
374         sub2.setProject(pro);
375
376         sub1.setProject(pro);
377         sub2.setProject(pro);
378         pro.setSubProjects(subList);
379         pro.setDevelopers(devList);
380
381         Query queryProject = createQueryFor(Project.class, "name", name);
382         Query querySubProject = createQueryFor(SubProject.class, "name", name);
383         Query queryDeveloper = createQueryFor(Developer.class, "name", name);
384
385         //*****************************************
386
// insert
387
//*****************************************
388
broker.beginTransaction();
389         broker.store(pro);
390         broker.commitTransaction();
391
392         broker.clearCache();
393         Collection JavaDoc result = broker.getCollectionByQuery(queryProject);
394         assertEquals(1, result.size());
395         assertNotNull(result.iterator().next());
396         result = broker.getCollectionByQuery(querySubProject);
397         assertEquals(2, result.size());
398         result = broker.getCollectionByQuery(queryDeveloper);
399         assertEquals(2, result.size());
400
401         Identity proOid = new Identity(pro, broker);
402         Project newPro = (Project) broker.getObjectByIdentity(proOid);
403         assertNotNull(newPro.getDevelopers());
404         assertNotNull(newPro.getSubProjects());
405         assertEquals(2, newPro.getDevelopers().size());
406         assertEquals(2, newPro.getSubProjects().size());
407         SubProject sub = (SubProject) newPro.getSubProjects().iterator().next();
408         assertNotNull(sub);
409         assertNotNull(sub.getProject());
410         assertEquals(sub.getProject().getId(), newPro.getId());
411
412         //*****************************************
413
// update
414
//*****************************************
415
broker.clearCache();
416         newPro = (Project) broker.getObjectByIdentity(proOid);
417         newPro.setName("updated_" + name);
418
419         //*****************************************
420
// de-/serialize object
421
//*****************************************
422
newPro = (Project) SerializationUtils.deserialize(SerializationUtils.serialize(newPro));
423
424
425         broker.beginTransaction();
426         newPro.getSubProjects().get(0);
427         Iterator JavaDoc it = newPro.getSubProjects().iterator();
428         while(it.hasNext())
429         {
430             SubProject subProject = (SubProject) it.next();
431             subProject.setName("updated_" + name);
432         }
433         broker.store(newPro);
434         broker.commitTransaction();
435
436         broker.clearCache();
437         newPro = (Project) broker.getObjectByIdentity(proOid);
438         assertEquals("updated_" + name, newPro.getName());
439         assertNotNull(newPro.getDevelopers());
440         assertNotNull(newPro.getSubProjects());
441         assertEquals(2, newPro.getDevelopers().size());
442         assertEquals(2, newPro.getSubProjects().size());
443         it = newPro.getSubProjects().iterator();
444         while(it.hasNext())
445         {
446             SubProject subProject = (SubProject) it.next();
447             assertEquals("updated_" + name, subProject.getName());
448         }
449
450         //*****************************************
451
// de-/serialize object
452
//*****************************************
453
newPro = (Project) SerializationUtils.deserialize(SerializationUtils.serialize(newPro));
454
455         //*****************************************
456
// delete
457
//*****************************************
458
broker.clearCache();
459         broker.beginTransaction();
460         broker.delete(pro);
461         broker.commitTransaction();
462
463         result = broker.getCollectionByQuery(queryProject);
464         assertEquals(0, result.size());
465         result = broker.getCollectionByQuery(querySubProject);
466         assertEquals(0, result.size());
467         result = broker.getCollectionByQuery(queryDeveloper);
468         assertEquals(0, result.size());
469     }
470
471     public void testStoreUpdateDelete_3a()
472     {
473         changeAutoSetting(Project.class, "subProjects", true,
474                 CollectionDescriptor.CASCADE_OBJECT, CollectionDescriptor.CASCADE_LINK, false);
475         changeAutoSetting(Project.class, "developers", true,
476                 CollectionDescriptor.CASCADE_LINK, CollectionDescriptor.CASCADE_OBJECT, false);
477         changeAutoSetting(SubProject.class, "project", true,
478                 CollectionDescriptor.CASCADE_OBJECT, CollectionDescriptor.CASCADE_NONE, false);
479         doTestStoreUpdateDelete_3();
480     }
481
482     public void testStoreUpdateDelete_3b()
483     {
484         changeAutoSetting(Project.class, "subProjects", true,
485                 CollectionDescriptor.CASCADE_OBJECT, CollectionDescriptor.CASCADE_LINK, true);
486         changeAutoSetting(Project.class, "developers", true,
487                 CollectionDescriptor.CASCADE_LINK, CollectionDescriptor.CASCADE_OBJECT, true);
488         changeAutoSetting(SubProject.class, "project", true,
489                 CollectionDescriptor.CASCADE_OBJECT, CollectionDescriptor.CASCADE_NONE, true);
490         doTestStoreUpdateDelete_3();
491     }
492
493     public void doTestStoreUpdateDelete_3()
494     {
495         String JavaDoc name = "testStoreUpdateDelete_2" + System.currentTimeMillis();
496
497         Developer dev1 = new Developer(name);
498         Developer dev2 = new Developer(name);
499         ArrayList JavaDoc devList = new ArrayList JavaDoc();
500         devList.add(dev1);
501         devList.add(dev2);
502
503         SubProject sub1 = new SubProject(name, null);
504         SubProject sub2 = new SubProject(name, null);
505         ArrayList JavaDoc subList = new ArrayList JavaDoc();
506         subList.add(sub1);
507         subList.add(sub2);
508
509         Project pro = new Project(name, subList, devList);
510         sub1.setProject(pro);
511         sub2.setProject(pro);
512
513         sub1.setProject(pro);
514         sub2.setProject(pro);
515         pro.setSubProjects(subList);
516         pro.setDevelopers(devList);
517
518         Query queryProject = createQueryFor(Project.class, "name", name);
519         Query querySubProject = createQueryFor(SubProject.class, "name", name);
520         Query queryDeveloper = createQueryFor(Developer.class, "name", name);
521
522         //*****************************************
523
// insert
524
//*****************************************
525
broker.beginTransaction();
526         broker.store(pro);
527         broker.commitTransaction();
528
529         broker.clearCache();
530         Collection JavaDoc result = broker.getCollectionByQuery(queryProject);
531         assertEquals(1, result.size());
532         assertNotNull(result.iterator().next());
533         result = broker.getCollectionByQuery(querySubProject);
534         assertEquals(2, result.size());
535         result = broker.getCollectionByQuery(queryDeveloper);
536         assertEquals(0, result.size());
537
538         broker.beginTransaction();
539         dev1.setProjectId(pro.getId());
540         dev2.setProjectId(pro.getId());
541         broker.store(dev1);
542         broker.store(dev2);
543         broker.commitTransaction();
544
545         broker.clearCache();
546         Identity proOid = new Identity(pro, broker);
547         Project newPro = (Project) broker.getObjectByIdentity(proOid);
548         assertNotNull(newPro.getDevelopers());
549         assertNotNull(newPro.getSubProjects());
550         assertEquals(2, newPro.getDevelopers().size());
551         assertEquals(2, newPro.getSubProjects().size());
552         SubProject sub = (SubProject) newPro.getSubProjects().iterator().next();
553         assertNotNull(sub);
554         assertNotNull(sub.getProject());
555         assertEquals(sub.getProject().getId(), newPro.getId());
556
557         //*****************************************
558
// update
559
//*****************************************
560
broker.clearCache();
561         newPro = (Project) broker.getObjectByIdentity(proOid);
562         newPro.setName("updated_" + name);
563         broker.beginTransaction();
564         Iterator JavaDoc it = newPro.getSubProjects().iterator();
565         while(it.hasNext())
566         {
567             SubProject subProject = (SubProject) it.next();
568             subProject.setName("updated_" + name);
569         }
570         broker.store(newPro);
571         broker.commitTransaction();
572
573         broker.clearCache();
574         newPro = (Project) broker.getObjectByIdentity(proOid);
575         assertEquals("updated_" + name, newPro.getName());
576         assertNotNull(newPro.getDevelopers());
577         assertNotNull(newPro.getSubProjects());
578         assertEquals(2, newPro.getDevelopers().size());
579         assertEquals(2, newPro.getSubProjects().size());
580         it = newPro.getSubProjects().iterator();
581         while(it.hasNext())
582         {
583             SubProject subProject = (SubProject) it.next();
584             assertEquals("updated_" + name, subProject.getName());
585         }
586
587         //*****************************************
588
// delete
589
//*****************************************
590
broker.clearCache();
591         broker.beginTransaction();
592         broker.serviceBrokerHelper().unlink(sub1, "project");
593         broker.serviceBrokerHelper().unlink(sub2, "project");
594         broker.store(sub1);
595         broker.store(sub2);
596         broker.delete(pro);
597         broker.commitTransaction();
598
599         result = broker.getCollectionByQuery(queryProject);
600         assertEquals(0, result.size());
601         result = broker.getCollectionByQuery(querySubProject);
602         assertEquals(2, result.size());
603         result = broker.getCollectionByQuery(queryDeveloper);
604         assertEquals(0, result.size());
605     }
606
607     public void testUpdate_1()
608     {
609         changeAutoSetting(Project.class, "subProjects", true,
610                 CollectionDescriptor.CASCADE_OBJECT, CollectionDescriptor.CASCADE_NONE, false);
611         changeAutoSetting(Project.class, "developers", true,
612                 CollectionDescriptor.CASCADE_LINK, CollectionDescriptor.CASCADE_LINK, false);
613         changeAutoSetting(SubProject.class, "project", true,
614                 CollectionDescriptor.CASCADE_OBJECT, CollectionDescriptor.CASCADE_NONE, false);
615     }
616
617     public void testUpdate_2()
618     {
619         changeAutoSetting(Project.class, "subProjects", true,
620                 CollectionDescriptor.CASCADE_OBJECT, CollectionDescriptor.CASCADE_NONE, true);
621         changeAutoSetting(Project.class, "developers", true,
622                 CollectionDescriptor.CASCADE_LINK, CollectionDescriptor.CASCADE_LINK, true);
623         changeAutoSetting(SubProject.class, "project", true,
624                 CollectionDescriptor.CASCADE_OBJECT, CollectionDescriptor.CASCADE_NONE, true);
625     }
626
627     public void doTestUpdate()
628     {
629         String JavaDoc name = "testStoreUpdateDelete_2" + System.currentTimeMillis();
630
631         changeAutoSetting(Project.class, "subProjects", true,
632                 CollectionDescriptor.CASCADE_OBJECT, CollectionDescriptor.CASCADE_NONE, false);
633         changeAutoSetting(Project.class, "developers", true,
634                 CollectionDescriptor.CASCADE_LINK, CollectionDescriptor.CASCADE_LINK, false);
635         changeAutoSetting(SubProject.class, "project", true,
636                 CollectionDescriptor.CASCADE_OBJECT, CollectionDescriptor.CASCADE_NONE, false);
637
638         Developer dev1 = new Developer(name);
639         Developer dev2 = new Developer(name);
640         ArrayList JavaDoc devList = new ArrayList JavaDoc();
641         devList.add(dev1);
642         devList.add(dev2);
643
644         SubProject sub1 = new SubProject(name, null);
645         SubProject sub2 = new SubProject(name, null);
646         ArrayList JavaDoc subList = new ArrayList JavaDoc();
647         subList.add(sub1);
648         subList.add(sub2);
649
650         Project pro = new Project(name, subList, devList);
651         sub1.setProject(pro);
652         sub2.setProject(pro);
653
654         sub1.setProject(pro);
655         sub2.setProject(pro);
656         pro.setSubProjects(subList);
657         pro.setDevelopers(devList);
658
659         Query queryProject = createQueryFor(Project.class, "name", name);
660         Query querySubProject = createQueryFor(SubProject.class, "name", name);
661         Query queryDeveloper = createQueryFor(Developer.class, "name", name);
662
663         //*****************************************
664
// insert
665
//*****************************************
666
broker.beginTransaction();
667         broker.store(pro);
668         broker.commitTransaction();
669
670         broker.clearCache();
671         Collection JavaDoc result = broker.getCollectionByQuery(queryProject);
672         assertEquals(1, result.size());
673         assertNotNull(result.iterator().next());
674         result = broker.getCollectionByQuery(querySubProject);
675         assertEquals(2, result.size());
676         result = broker.getCollectionByQuery(queryDeveloper);
677         assertEquals(0, result.size());
678     }
679
680     //============================================================================
681
// helper methods
682
//============================================================================
683

684     void changeAutoSetting(Class JavaDoc clazz, String JavaDoc referenceField, boolean autoRetrieve, boolean autoUpdate, boolean autoDelete, boolean useProxy)
685     {
686         ClassDescriptor cld = broker.getClassDescriptor(clazz);
687         ObjectReferenceDescriptor ref = cld.getCollectionDescriptorByName(referenceField);
688         if(ref == null) ref = cld.getObjectReferenceDescriptorByName(referenceField);
689         ref.setLazy(useProxy);
690         ref.setCascadeRetrieve(autoRetrieve);
691         ref.setCascadeStore(autoUpdate);
692         ref.setCascadeDelete(autoDelete);
693     }
694
695     void changeAutoSetting(Class JavaDoc clazz, String JavaDoc referenceField, boolean autoRetrieve, int autoUpdate, int autoDelete, boolean useProxy)
696     {
697         ClassDescriptor cld = broker.getClassDescriptor(clazz);
698         ObjectReferenceDescriptor ref = cld.getCollectionDescriptorByName(referenceField);
699         if(ref == null) ref = cld.getObjectReferenceDescriptorByName(referenceField);
700         ref.setLazy(useProxy);
701         ref.setCascadeRetrieve(autoRetrieve);
702         ref.setCascadingStore(autoUpdate);
703         ref.setCascadingDelete(autoDelete);
704     }
705
706     Query createQueryFor(Class JavaDoc clazz, String JavaDoc attribute, String JavaDoc value)
707     {
708         Criteria crit = new Criteria();
709         crit.addLike(attribute, "%" + value + "%");
710         return QueryFactory.newQuery(clazz, crit);
711     }
712
713
714     //============================================================================
715
// inner classes, used test classes
716
//============================================================================
717

718     public static class Project implements ProjectIF
719     {
720         private Integer JavaDoc id;
721         private String JavaDoc name;
722         private List JavaDoc subProjects;
723         private Collection JavaDoc developers;
724
725         public Project()
726         {
727         }
728
729         public Project(String JavaDoc name, List JavaDoc subProjects, Collection JavaDoc developers)
730         {
731             this.name = name;
732             this.subProjects = subProjects;
733             this.developers = developers;
734         }
735
736         public Integer JavaDoc getId()
737         {
738             return id;
739         }
740
741         public void setId(Integer JavaDoc id)
742         {
743             this.id = id;
744         }
745
746         public String JavaDoc getName()
747         {
748             return name;
749         }
750
751         public void setName(String JavaDoc name)
752         {
753             this.name = name;
754         }
755
756         public List JavaDoc getSubProjects()
757         {
758             return subProjects;
759         }
760
761         public void setSubProjects(List JavaDoc subProjects)
762         {
763             this.subProjects = subProjects;
764         }
765
766         public Collection JavaDoc getDevelopers()
767         {
768             return developers;
769         }
770
771         public void setDevelopers(Collection JavaDoc developers)
772         {
773             this.developers = developers;
774         }
775     }
776
777     public static interface ProjectIF extends Serializable JavaDoc
778     {
779         public Integer JavaDoc getId();
780         public void setId(Integer JavaDoc id);
781         public String JavaDoc getName();
782         public void setName(String JavaDoc name);
783         public List JavaDoc getSubProjects();
784         public void setSubProjects(List JavaDoc subProjects);
785         public Collection JavaDoc getDevelopers();
786         public void setDevelopers(Collection JavaDoc developers);
787     }
788
789     public static class SubProject implements SubProjectIF
790     {
791         private Integer JavaDoc id;
792         private String JavaDoc name;
793         private ProjectIF project;
794
795         public SubProject()
796         {
797         }
798
799         public SubProject(String JavaDoc name, Project project)
800         {
801             this.name = name;
802             this.project = project;
803         }
804
805         public Integer JavaDoc getId()
806         {
807             return id;
808         }
809
810         public void setId(Integer JavaDoc id)
811         {
812             this.id = id;
813         }
814
815         public String JavaDoc getName()
816         {
817             return name;
818         }
819
820         public void setName(String JavaDoc name)
821         {
822             this.name = name;
823         }
824
825         public ProjectIF getProject()
826         {
827             return project;
828         }
829
830         public void setProject(ProjectIF project)
831         {
832             this.project = project;
833         }
834     }
835
836     public static interface SubProjectIF extends Serializable JavaDoc
837     {
838         public Integer JavaDoc getId();
839         public void setId(Integer JavaDoc id);
840         public String JavaDoc getName();
841         public void setName(String JavaDoc name);
842         public ProjectIF getProject();
843         public void setProject(ProjectIF project);
844     }
845
846     public static class Developer implements Serializable JavaDoc
847     {
848         private Integer JavaDoc id;
849         private String JavaDoc name;
850         private Integer JavaDoc projectId;
851
852         public Developer()
853         {
854         }
855
856         public Developer(String JavaDoc name)
857         {
858             this.name = name;
859         }
860
861         public Integer JavaDoc getId()
862         {
863             return id;
864         }
865
866         public void setId(Integer JavaDoc id)
867         {
868             this.id = id;
869         }
870
871         public String JavaDoc getName()
872         {
873             return name;
874         }
875
876         public void setName(String JavaDoc name)
877         {
878             this.name = name;
879         }
880
881         public Integer JavaDoc getProjectId()
882         {
883             return projectId;
884         }
885
886         public void setProjectId(Integer JavaDoc projectId)
887         {
888             this.projectId = projectId;
889         }
890     }
891 }
892
Popular Tags