KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > runtime > relations > TestRelations


1 /**
2  * Speedo: an implementation of JDO compliant personality on top of JORM generic
3  * I/O sub-system.
4  * Copyright (C) 2001-2004 France Telecom R&D
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  *
21  *
22  * Contact: speedo@objectweb.org
23  *
24  * Authors: S.Chassande-Barrioz.
25  *
26  */

27
28 package org.objectweb.speedo.runtime.relations;
29
30 import org.objectweb.speedo.SpeedoTestHelper;
31 import org.objectweb.speedo.pobjects.relations.A;
32 import org.objectweb.speedo.pobjects.relations.B;
33
34 import javax.jdo.PersistenceManager;
35 import java.util.ArrayList JavaDoc;
36 import java.util.Collection JavaDoc;
37 import java.util.Arrays JavaDoc;
38
39 public class TestRelations extends SpeedoTestHelper {
40
41     public TestRelations(String JavaDoc s) {
42         super(s);
43     }
44
45     protected String JavaDoc getLoggerName() {
46         return LOG_NAME + "rt.relations.Relations";
47     }
48
49     public void testOneOne() {
50         A a1 = new A("testOneOne_a1");
51         A a2 = new A("testOneOne_a2");
52         B b1 = new B("testOneOne_b1");
53         B b2 = new B("testOneOne_b2");
54
55         a1.setB(b1);
56         assertTrue(b1.getA() == a1);
57
58         a2.setB(b2);
59         assertTrue(b2.getA() == a2);
60
61         a1.setB(b2);
62         assertTrue(b1.getA() == null);
63         assertTrue(b2.getA() == a1);
64         assertTrue(a2.getB() == null);
65
66         b1.setA(a2);
67         assertTrue(a2.getB() == b1);
68
69         b1.setA(a1);
70         assertTrue(a1.getB() == b1);
71         assertTrue(b2.getA() == null);
72         assertTrue(a2.getB() == null);
73     }
74
75     public void testOneMany() { // tests on active persistent objects
76
A a1 = new A("testOneMany_a1");
77         A a2 = new A("testOneMany_a2");
78
79         B b1 = new B("testOneMany_b1");
80         B b2 = new B("testOneMany_b2");
81         B b3 = new B("testOneMany_b3");
82         B b4 = new B("testOneMany_b4");
83
84         PersistenceManager pm = pmf.getPersistenceManager();
85         pm.makePersistent(a1);
86         pm.makePersistent(a2);
87         pm.makePersistent(b1);
88         pm.makePersistent(b2);
89         pm.makePersistent(b3);
90         pm.makePersistent(b4);
91         pm.close();
92
93         try {
94             pm = pmf.getPersistenceManager();
95
96             a1.getNbs().add(b1);
97             assertTrue(b1.getNa() == a1);
98             a1.getNbs().add(b2);
99             assertTrue(b2.getNa() == a1);
100
101             a2.getNbs().add(b3);
102             assertTrue(b3.getNa() == a2);
103             a2.getNbs().add(b4);
104             assertTrue(b4.getNa() == a2);
105
106             a1.getNbs().add(b3);
107             assertTrue(b3.getNa() == a1);
108             assertTrue(!a2.getNbs().contains(b3));
109
110             a1.getNbs().remove(b1);
111             assertTrue(b1.getNa() == null);
112
113             b3.setNa(a2);
114             assertTrue(!a1.getNbs().contains(b3));
115             assertTrue(a2.getNbs().contains(b3));
116
117             a1.setNbs(a2.getNbs());
118             // reload objects before testing effects of the above assignement
119
pm.close();
120             pm = pmf.getPersistenceManager();
121
122             assertTrue(!a1.getNbs().contains(b1));
123             assertTrue(!a1.getNbs().contains(b2));
124             assertTrue(a1.getNbs().contains(b3));
125             assertTrue(a1.getNbs().contains(b4));
126             assertTrue(a1.getNbs().size() == 2);
127
128             assertTrue(!a2.getNbs().contains(b1));
129             assertTrue(!a2.getNbs().contains(b2));
130             assertTrue(!a2.getNbs().contains(b3));
131             assertTrue(!a2.getNbs().contains(b4));
132
133             assertTrue(b1.getNa() == null);
134             assertTrue(b2.getNa() == null);
135             assertTrue(b3.getNa() == a1);
136             assertTrue(b4.getNa() == a1);
137
138             pm.close();
139         } finally {
140             pm = pmf.getPersistenceManager();
141             pm.currentTransaction().begin();
142             pm.deletePersistent(a1);
143             pm.deletePersistent(a2);
144             pm.deletePersistent(b1);
145             pm.deletePersistent(b2);
146             pm.deletePersistent(b3);
147             pm.deletePersistent(b4);
148             pm.currentTransaction().commit();
149             pm.close();
150         }
151     }
152
153     public void testOneMany2() { // tests on non persistent objects
154
A a1 = new A("testOneMany2_a1");
155         A a2 = new A("testOneMany2_a2");
156
157         B b1 = new B("testOneMany2_b1");
158         B b2 = new B("testOneMany2_b2");
159         B b3 = new B("testOneMany2_b3");
160         B b4 = new B("testOneMany2_b4");
161
162         PersistenceManager pm = pmf.getPersistenceManager();
163
164         b1.setNa(a1);
165         // tests that a1.nbs, initially null, is created and contains b1
166
assertTrue(a1.getNbs() != null);
167         assertTrue(a1.getNbs().contains(b1));
168
169         b2.setNa(a1);
170         assertTrue(a1.getNbs().contains(b2));
171
172         b3.setNa(a2);
173         assertTrue(a2.getNbs() != null);
174         assertTrue(a2.getNbs().contains(b3));
175
176         b4.setNa(a2);
177         assertTrue(a2.getNbs().contains(b4));
178
179         pm.makePersistent(a1);
180         pm.makePersistent(a2);
181
182         pm.close();
183
184         try {
185             pm = pmf.getPersistenceManager();
186             Collection JavaDoc c = new ArrayList JavaDoc();
187             c.addAll(a2.getNbs());
188             a1.setNbs(c);
189
190             pm.close();
191
192             pm = pmf.getPersistenceManager();
193             assertTrue(!a1.getNbs().contains(b1));
194             assertTrue(!a1.getNbs().contains(b2));
195             assertTrue(a1.getNbs().contains(b3));
196             assertTrue(a1.getNbs().contains(b4));
197
198             assertTrue(a2.getNbs().size() == 0);
199
200             assertTrue(b1.getNa() == null);
201             assertTrue(b2.getNa() == null);
202             assertTrue(b3.getNa() == a1);
203             assertTrue(b4.getNa() == a1);
204
205             pm.close();
206         } finally {
207             pm = pmf.getPersistenceManager();
208             pm.currentTransaction().begin();
209             pm.deletePersistent(a1);
210             pm.deletePersistent(a2);
211             pm.deletePersistent(b1);
212             pm.deletePersistent(b2);
213             pm.deletePersistent(b3);
214             pm.deletePersistent(b4);
215             pm.currentTransaction().commit();
216             pm.close();
217         }
218     }
219
220     public void testOneMany3() { // tests with manually managed consistency
221
A a1 = new A("testOneMany3_a1");
222
223         B b1 = new B("testOneMany3_b1");
224         B b2 = new B("testOneMany3_b2");
225
226         PersistenceManager pm = pmf.getPersistenceManager();
227
228         ArrayList JavaDoc l = new ArrayList JavaDoc();
229         l.add(b1);
230         l.add(b2);
231         a1.setNbs(l);
232
233         b1.setNa(a1);
234         b2.setNa(a1);
235
236         assertTrue(a1.getNbs().size() == 2);
237
238         pm.makePersistent(a1);
239
240         pm.close();
241
242         try {
243             pm = pmf.getPersistenceManager();
244             assertTrue(a1.getNbs().size() == 2);
245             pm.close();
246         } finally {
247             pm = pmf.getPersistenceManager();
248             pm.currentTransaction().begin();
249             pm.deletePersistent(a1);
250             pm.deletePersistent(b1);
251             pm.deletePersistent(b1);
252             pm.currentTransaction().commit();
253             pm.close();
254         }
255     }
256
257     public void testOneManyDelete() {
258         A a1 = new A("testOneManyDelete_a1");
259         A a2 = new A("testOneManyDelete_a2");
260
261         B b1 = new B("testOneManyDelete_b1");
262         B b2 = new B("testOneManyDelete_b2");
263         B b3 = new B("testOneManyDelete_b3");
264         B b4 = new B("testOneManyDelete_b4");
265
266         PersistenceManager pm = pmf.getPersistenceManager();
267         pm.makePersistent(a1);
268         pm.makePersistent(a2);
269         pm.makePersistent(b1);
270         pm.makePersistent(b2);
271         pm.makePersistent(b3);
272         pm.makePersistent(b4);
273         pm.close();
274
275         try {
276             pm = pmf.getPersistenceManager();
277
278             a1.getNbs().add(b1);
279             assertTrue(b1.getNa() == a1);
280             a1.getNbs().add(b2);
281             assertTrue(b2.getNa() == a1);
282
283             a2.getNbs().add(b3);
284             assertTrue(b3.getNa() == a2);
285             a2.getNbs().add(b4);
286             assertTrue(b4.getNa() == a2);
287
288             pm.close();
289
290             pm = pmf.getPersistenceManager();
291             pm.currentTransaction().begin();
292             pm.deletePersistent(a1);
293             pm.currentTransaction().commit();
294             assertNull("reverse reference of a1.nbs (b1.na) must be null", b1.getNa());
295             assertNull("reverse reference of a1.nbs (b2.na) must be null", b2.getNa());
296             pm.currentTransaction().begin();
297             pm.deletePersistent(b3);
298             pm.currentTransaction().commit();
299             assertTrue("reverse reference of b3.na (a2.nbs) must not contain b3",
300                     !a2.getNbs().contains(b3));
301             pm.close();
302
303             pm = pmf.getPersistenceManager();
304             assertTrue("reverse reference of b3.na (a2.nbs) must not contain b3",
305                     !a2.getNbs().contains(b3));
306
307             assertEquals("Bad a2.nbs size", 1, a2.getNbs().size());
308             assertTrue("reverse reference of b3.na (a2.nbs) must not contain b3",
309                     !a2.getNbs().contains(b3));
310             assertTrue(a2.getNbs().contains(b4));
311
312             assertNull("reverse reference of a1.nbs (b1.na) must be null", b1.getNa());
313             assertNull("reverse reference of a1.nbs (b2.na) must be null", b2.getNa());
314             assertTrue(b4.getNa() == a2);
315
316             pm.close();
317         } finally {
318             pm = pmf.getPersistenceManager();
319             pm.currentTransaction().begin();
320             pm.deletePersistent(a2);
321             pm.deletePersistent(b1);
322             pm.deletePersistent(b2);
323             pm.deletePersistent(b4);
324             pm.currentTransaction().commit();
325             pm.close();
326         }
327     }
328
329     public void testManyMany() { // tests on active persistent objects
330
A a1 = new A("testManyMany_a1");
331         A a2 = new A("testManyMany_a2");
332         A a3 = new A("testManyMany_a3");
333
334         B b1 = new B("testManyMany_b1");
335         B b2 = new B("testManyMany_b2");
336         B b3 = new B("testManyMany_b3");
337
338         PersistenceManager pm = pmf.getPersistenceManager();
339         pm.makePersistent(a1);
340         pm.makePersistent(a2);
341         pm.makePersistent(a3);
342         pm.makePersistent(b1);
343         pm.makePersistent(b2);
344         pm.makePersistent(b3);
345         pm.close();
346
347         try {
348             pm = pmf.getPersistenceManager();
349
350             a1.getBs().add(b1);
351             assertTrue(b1.getAs().contains(a1));
352             a1.getBs().add(b2);
353             assertTrue(b2.getAs().contains(a1));
354
355             a2.getBs().add(b1);
356             assertTrue(b1.getAs().contains(a2));
357             a2.getBs().add(b2);
358             assertTrue(b2.getAs().contains(a2));
359             a2.getBs().add(b3);
360             assertTrue(b3.getAs().contains(a2));
361
362             a3.getBs().add(b2);
363             assertTrue(b2.getAs().contains(a3));
364             a3.getBs().add(b3);
365             assertTrue(b3.getAs().contains(a3));
366
367             a1.getBs().add(b3);
368             assertTrue(b3.getAs().contains(a1));
369
370             a1.getBs().remove(b1);
371             assertTrue(!b1.getAs().contains(a1));
372
373             b2.getAs().remove(a2);
374             assertTrue(!a2.getBs().contains(b2));
375
376             a1.setBs(a2.getBs());
377             // reload objects before testing effects of the above assignement
378
pm.close();
379             pm = pmf.getPersistenceManager();
380
381             assertTrue(a1.getBs().contains(b1));
382             assertTrue(!a1.getBs().contains(b2));
383             assertTrue(a1.getBs().contains(b3));
384
385             assertTrue(a2.getBs().contains(b1));
386             assertTrue(!a2.getBs().contains(b2));
387             assertTrue(a2.getBs().contains(b3));
388
389             assertTrue(!a3.getBs().contains(b1));
390             assertTrue(a3.getBs().contains(b2));
391             assertTrue(a3.getBs().contains(b3));
392
393             assertTrue(b1.getAs().contains(a1));
394             assertTrue(b1.getAs().contains(a2));
395             assertTrue(!b1.getAs().contains(a3));
396
397             assertTrue(!b2.getAs().contains(a1));
398             assertTrue(!b2.getAs().contains(a2));
399             assertTrue(b2.getAs().contains(a3));
400
401             assertTrue(b3.getAs().contains(a1));
402             assertTrue(b3.getAs().contains(a2));
403             assertTrue(b3.getAs().contains(a3));
404
405             pm.close();
406         } finally {
407             pm = pmf.getPersistenceManager();
408             pm.currentTransaction().begin();
409             pm.deletePersistent(a1);
410             pm.deletePersistent(a2);
411             pm.deletePersistent(a3);
412             pm.deletePersistent(b1);
413             pm.deletePersistent(b2);
414             pm.deletePersistent(b3);
415             pm.currentTransaction().commit();
416             pm.close();
417         }
418     }
419
420     public void testManyMany2() { // tests on non persistent objects
421
A a1 = new A("testManyMany2_a1");
422         A a2 = new A("testManyMany2_a2");
423         A a3 = new A("testManyMany2_a3");
424
425         B b1 = new B();
426         B b2 = new B();
427         B b3 = new B();
428
429         PersistenceManager pm = pmf.getPersistenceManager();
430
431         a1.getBs().add(b1);
432         a1.getBs().add(b2);
433
434         a2.getBs().add(b1);
435         a2.getBs().add(b2);
436         a2.getBs().add(b3);
437
438         a3.getBs().add(b2);
439         a3.getBs().add(b3);
440
441         pm.makePersistent(a1);
442         pm.makePersistent(a2);
443         pm.makePersistent(a3);
444
445         pm.close();
446
447         try {
448             pm = pmf.getPersistenceManager();
449
450             assertTrue(b1.getAs().contains(a1));
451             assertTrue(b2.getAs().contains(a1));
452
453             assertTrue(b1.getAs().contains(a2));
454             assertTrue(b2.getAs().contains(a2));
455             assertTrue(b3.getAs().contains(a2));
456
457             assertTrue(b2.getAs().contains(a3));
458             assertTrue(b3.getAs().contains(a3));
459
460             Collection JavaDoc c = new ArrayList JavaDoc();
461             c.addAll(a2.getBs());
462             a1.setBs(c);
463
464             pm.close();
465
466             pm = pmf.getPersistenceManager();
467
468             assertTrue(a1.getBs().contains(b1));
469             assertTrue(a1.getBs().contains(b2));
470             assertTrue(a1.getBs().contains(b3));
471
472             assertTrue(a2.getBs().contains(b1));
473             assertTrue(a2.getBs().contains(b2));
474             assertTrue(a2.getBs().contains(b3));
475
476             assertTrue(!a3.getBs().contains(b1));
477             assertTrue(a3.getBs().contains(b2));
478             assertTrue(a3.getBs().contains(b3));
479
480             assertTrue(b1.getAs().contains(a1));
481             assertTrue(b1.getAs().contains(a2));
482             assertTrue(!b1.getAs().contains(a3));
483
484             assertTrue(b2.getAs().contains(a1));
485             assertTrue(b2.getAs().contains(a2));
486             assertTrue(b2.getAs().contains(a3));
487
488             assertTrue(b3.getAs().contains(a1));
489             assertTrue(b3.getAs().contains(a2));
490             assertTrue(b3.getAs().contains(a3));
491
492             pm.close();
493         } finally {
494             pm = pmf.getPersistenceManager();
495             pm.currentTransaction().begin();
496             pm.deletePersistent(a1);
497             pm.deletePersistent(a2);
498             pm.deletePersistent(a3);
499             pm.deletePersistent(b1);
500             pm.deletePersistent(b2);
501             pm.deletePersistent(b3);
502             pm.currentTransaction().commit();
503             pm.close();
504         }
505     }
506
507     public void testManyMany3() { // tests with manually managed consistency
508
A a1 = new A("testManyMany3_a1");
509         A a2 = new A("testManyMany3_a2");
510         A a3 = new A("testManyMany3_a3");
511
512         B b1 = new B("testManyMany3_b1");
513         B b2 = new B("testManyMany3_b2");
514         B b3 = new B("testManyMany3_b3");
515
516         PersistenceManager pm = pmf.getPersistenceManager();
517
518         a1.setBs(Arrays.asList(new Object JavaDoc[] { b1, b2 }));
519         a2.setBs(Arrays.asList(new Object JavaDoc[] { b1, b2, b3 }));
520         a3.setBs(Arrays.asList(new Object JavaDoc[] { b2, b3 }));
521
522         b1.setAs(Arrays.asList(new Object JavaDoc[] { a1, a2 }));
523         b2.setAs(Arrays.asList(new Object JavaDoc[] { a1, a2, a3 }));
524         b3.setAs(Arrays.asList(new Object JavaDoc[] { a2, a3 }));
525
526         pm.makePersistent(a1);
527         pm.makePersistent(a2);
528         pm.makePersistent(a3);
529
530         pm.close();
531
532         try {
533             pm = pmf.getPersistenceManager();
534
535             assertTrue(a1.getBs().size() == 2);
536             assertTrue(a1.getBs().contains(b1));
537             assertTrue(a1.getBs().contains(b2));
538
539             assertTrue(a2.getBs().size() == 3);
540             assertTrue(a2.getBs().contains(b1));
541             assertTrue(a2.getBs().contains(b2));
542             assertTrue(a2.getBs().contains(b3));
543
544             assertTrue(a3.getBs().size() == 2);
545             assertTrue(a3.getBs().contains(b2));
546             assertTrue(a3.getBs().contains(b3));
547
548             assertTrue(b1.getAs().size() == 2);
549             assertTrue(b1.getAs().contains(a1));
550             assertTrue(b1.getAs().contains(a2));
551
552             assertTrue(b2.getAs().size() == 3);
553             assertTrue(b2.getAs().contains(a1));
554             assertTrue(b2.getAs().contains(a2));
555             assertTrue(b2.getAs().contains(a3));
556
557             assertTrue(b3.getAs().size() == 2);
558             assertTrue(b3.getAs().contains(a2));
559             assertTrue(b3.getAs().contains(a3));
560
561             pm.close();
562         } finally {
563             pm = pmf.getPersistenceManager();
564             pm.currentTransaction().begin();
565             pm.deletePersistent(a1);
566             pm.deletePersistent(a2);
567             pm.deletePersistent(a3);
568             pm.deletePersistent(b1);
569             pm.deletePersistent(b2);
570             pm.deletePersistent(b3);
571             pm.currentTransaction().commit();
572             pm.close();
573         }
574     }
575
576     public void testManyManyDelete() {
577         A a1 = new A("testManyManyDelete_a1");
578         A a2 = new A("testManyManyDelete_a2");
579         A a3 = new A("testManyManyDelete_a3");
580
581         B b1 = new B("testManyManyDelete_b1");
582         B b2 = new B("testManyManyDelete_b2");
583         B b3 = new B("testManyManyDelete_b3");
584
585         PersistenceManager pm = pmf.getPersistenceManager();
586         pm.makePersistent(a1);
587         pm.makePersistent(a2);
588         pm.makePersistent(a3);
589         pm.makePersistent(b1);
590         pm.makePersistent(b2);
591         pm.makePersistent(b3);
592         pm.close();
593
594         try {
595             pm = pmf.getPersistenceManager();
596
597             a1.getBs().add(b1);
598             assertTrue(b1.getAs().contains(a1));
599             a1.getBs().add(b2);
600             assertTrue(b2.getAs().contains(a1));
601
602             a2.getBs().add(b1);
603             assertTrue(b1.getAs().contains(a2));
604             a2.getBs().add(b2);
605             assertTrue(b2.getAs().contains(a2));
606             a2.getBs().add(b3);
607             assertTrue(b3.getAs().contains(a2));
608
609             a3.getBs().add(b2);
610             assertTrue(b2.getAs().contains(a3));
611             a3.getBs().add(b3);
612             assertTrue(b3.getAs().contains(a3));
613
614             pm.close();
615
616             pm = pmf.getPersistenceManager();
617             pm.currentTransaction().begin();
618             pm.deletePersistent(b2);
619             pm.currentTransaction().commit();
620             pm.close();
621
622             pm = pmf.getPersistenceManager();
623
624             assertTrue(a1.getBs().size() == 1);
625             assertTrue(a1.getBs().contains(b1));
626
627             assertTrue(a2.getBs().size() == 2);
628             assertTrue(a2.getBs().contains(b1));
629             assertTrue(a2.getBs().contains(b3));
630
631             assertTrue(a3.getBs().size() == 1);
632             assertTrue(a3.getBs().contains(b3));
633
634             assertTrue(b1.getAs().contains(a1));
635             assertTrue(b1.getAs().contains(a2));
636             assertTrue(!b1.getAs().contains(a3));
637
638             assertTrue(!b3.getAs().contains(a1));
639             assertTrue(b3.getAs().contains(a2));
640             assertTrue(b3.getAs().contains(a3));
641
642             pm.close();
643         } finally {
644             pm = pmf.getPersistenceManager();
645             pm.currentTransaction().begin();
646             pm.deletePersistent(a1);
647             pm.deletePersistent(a2);
648             pm.deletePersistent(a3);
649             pm.deletePersistent(b1);
650             pm.deletePersistent(b3);
651             pm.currentTransaction().commit();
652             pm.close();
653         }
654     }
655 }
656
Popular Tags