001: /**
002: * Speedo: an implementation of JDO compliant personality on top of JORM generic
003: * I/O sub-system.
004: * Copyright (C) 2001-2004 France Telecom R&D
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: *
021: *
022: * Contact: speedo@objectweb.org
023: *
024: * Authors: S.Chassande-Barrioz.
025: *
026: */package org.objectweb.speedo.runtime.relations;
027:
028: import org.objectweb.speedo.SpeedoTestHelper;
029: import org.objectweb.speedo.pobjects.relations.A;
030: import org.objectweb.speedo.pobjects.relations.B;
031: import org.objectweb.speedo.pobjects.relations.ni_ejboo.Article;
032: import org.objectweb.speedo.pobjects.relations.ni_ejboo.Catalogue;
033: import org.objectweb.speedo.pobjects.relations.ni_ejboo.Marche;
034: import org.objectweb.util.monolog.api.BasicLevel;
035:
036: import javax.jdo.Extent;
037: import javax.jdo.PersistenceManager;
038: import javax.jdo.Query;
039:
040: import java.util.ArrayList;
041: import java.util.Collection;
042: import java.util.Arrays;
043: import java.util.Collections;
044: import java.util.Iterator;
045:
046: public class TestRelations extends SpeedoTestHelper {
047:
048: public TestRelations(String s) {
049: super (s);
050: }
051:
052: protected String getLoggerName() {
053: return LOG_NAME + "rt.relations.Relations";
054: }
055:
056: public void testOneOne() {
057: A a1 = new A("testOneOne_a1");
058: A a2 = new A("testOneOne_a2");
059: B b1 = new B("testOneOne_b1");
060: B b2 = new B("testOneOne_b2");
061:
062: a1.setB(b1);
063: assertTrue(b1.getA() == a1);
064:
065: a2.setB(b2);
066: assertTrue(b2.getA() == a2);
067:
068: a1.setB(b2);
069: assertTrue(b1.getA() == null);
070: assertTrue(b2.getA() == a1);
071: assertTrue(a2.getB() == null);
072:
073: b1.setA(a2);
074: assertTrue(a2.getB() == b1);
075:
076: b1.setA(a1);
077: assertTrue(a1.getB() == b1);
078: assertTrue(b2.getA() == null);
079: assertTrue(a2.getB() == null);
080: }
081:
082: public void testOneMany() { // tests on active persistent objects
083: A a1 = new A("testOneMany_a1");
084: A a2 = new A("testOneMany_a2");
085:
086: B b1 = new B("testOneMany_b1");
087: B b2 = new B("testOneMany_b2");
088: B b3 = new B("testOneMany_b3");
089: B b4 = new B("testOneMany_b4");
090:
091: PersistenceManager pm = pmf.getPersistenceManager();
092: pm.makePersistent(a1);
093: pm.makePersistent(a2);
094: pm.makePersistent(b1);
095: pm.makePersistent(b2);
096: pm.makePersistent(b3);
097: pm.makePersistent(b4);
098: pm.close();
099:
100: try {
101: pm = pmf.getPersistenceManager();
102:
103: a1.getNbs().add(b1);
104: assertTrue(b1.getNa() == a1);
105: a1.getNbs().add(b2);
106: assertTrue(b2.getNa() == a1);
107:
108: a2.getNbs().add(b3);
109: assertTrue(b3.getNa() == a2);
110: a2.getNbs().add(b4);
111: assertTrue(b4.getNa() == a2);
112:
113: a1.getNbs().add(b3);
114: assertTrue(b3.getNa() == a1);
115: assertTrue(!a2.getNbs().contains(b3));
116:
117: a1.getNbs().remove(b1);
118: assertTrue(b1.getNa() == null);
119:
120: b3.setNa(a2);
121: assertTrue(!a1.getNbs().contains(b3));
122: assertTrue(a2.getNbs().contains(b3));
123:
124: a1.setNbs(a2.getNbs());
125: // reload objects before testing effects of the above assignement
126: pm.close();
127: pm = pmf.getPersistenceManager();
128:
129: assertTrue(!a1.getNbs().contains(b1));
130: assertTrue(!a1.getNbs().contains(b2));
131: assertTrue(a1.getNbs().contains(b3));
132: assertTrue(a1.getNbs().contains(b4));
133: assertTrue(a1.getNbs().size() == 2);
134:
135: assertTrue(!a2.getNbs().contains(b1));
136: assertTrue(!a2.getNbs().contains(b2));
137: assertTrue(!a2.getNbs().contains(b3));
138: assertTrue(!a2.getNbs().contains(b4));
139:
140: assertTrue(b1.getNa() == null);
141: assertTrue(b2.getNa() == null);
142: assertTrue(b3.getNa() == a1);
143: assertTrue(b4.getNa() == a1);
144:
145: pm.close();
146: } finally {
147: pm = pmf.getPersistenceManager();
148: pm.currentTransaction().begin();
149: pm.deletePersistent(a1);
150: pm.deletePersistent(a2);
151: pm.deletePersistent(b1);
152: pm.deletePersistent(b2);
153: pm.deletePersistent(b3);
154: pm.deletePersistent(b4);
155: pm.currentTransaction().commit();
156: pm.close();
157: }
158: }
159:
160: public void testOneMany2() { // tests on non persistent objects
161: A a1 = new A("testOneMany2_a1");
162: A a2 = new A("testOneMany2_a2");
163:
164: B b1 = new B("testOneMany2_b1");
165: B b2 = new B("testOneMany2_b2");
166: B b3 = new B("testOneMany2_b3");
167: B b4 = new B("testOneMany2_b4");
168:
169: PersistenceManager pm = pmf.getPersistenceManager();
170:
171: b1.setNa(a1);
172: // tests that a1.nbs, initially null, is created and contains b1
173: assertTrue(a1.getNbs() != null);
174: assertTrue(a1.getNbs().contains(b1));
175:
176: b2.setNa(a1);
177: assertTrue(a1.getNbs().contains(b2));
178:
179: b3.setNa(a2);
180: assertTrue(a2.getNbs() != null);
181: assertTrue(a2.getNbs().contains(b3));
182:
183: b4.setNa(a2);
184: assertTrue(a2.getNbs().contains(b4));
185:
186: pm.makePersistent(a1);
187: pm.makePersistent(a2);
188:
189: pm.close();
190:
191: try {
192: pm = pmf.getPersistenceManager();
193: Collection c = new ArrayList();
194: c.addAll(a2.getNbs());
195: a1.setNbs(c);
196:
197: pm.close();
198:
199: pm = pmf.getPersistenceManager();
200: assertTrue(!a1.getNbs().contains(b1));
201: assertTrue(!a1.getNbs().contains(b2));
202: assertTrue(a1.getNbs().contains(b3));
203: assertTrue(a1.getNbs().contains(b4));
204:
205: assertTrue(a2.getNbs().size() == 0);
206:
207: assertTrue(b1.getNa() == null);
208: assertTrue(b2.getNa() == null);
209: assertTrue(b3.getNa() == a1);
210: assertTrue(b4.getNa() == a1);
211:
212: pm.close();
213: } finally {
214: pm = pmf.getPersistenceManager();
215: pm.currentTransaction().begin();
216: pm.deletePersistent(a1);
217: pm.deletePersistent(a2);
218: pm.deletePersistent(b1);
219: pm.deletePersistent(b2);
220: pm.deletePersistent(b3);
221: pm.deletePersistent(b4);
222: pm.currentTransaction().commit();
223: pm.close();
224: }
225: }
226:
227: public void testOneMany3() { // tests with manually managed consistency
228: A a1 = new A("testOneMany3_a1");
229:
230: B b1 = new B("testOneMany3_b1");
231: B b2 = new B("testOneMany3_b2");
232:
233: PersistenceManager pm = pmf.getPersistenceManager();
234:
235: ArrayList l = new ArrayList();
236: l.add(b1);
237: l.add(b2);
238: a1.setNbs(l);
239:
240: b1.setNa(a1);
241: b2.setNa(a1);
242:
243: assertTrue(a1.getNbs().size() == 2);
244:
245: pm.makePersistent(a1);
246:
247: pm.close();
248:
249: try {
250: pm = pmf.getPersistenceManager();
251: assertTrue(a1.getNbs().size() == 2);
252: pm.close();
253: } finally {
254: pm = pmf.getPersistenceManager();
255: pm.currentTransaction().begin();
256: pm.deletePersistent(a1);
257: pm.deletePersistent(b1);
258: pm.deletePersistent(b1);
259: pm.currentTransaction().commit();
260: pm.close();
261: }
262: }
263:
264: public void testOneManyDelete() {
265: A a1 = new A("testOneManyDelete_a1");
266: A a2 = new A("testOneManyDelete_a2");
267:
268: B b1 = new B("testOneManyDelete_b1");
269: B b2 = new B("testOneManyDelete_b2");
270: B b3 = new B("testOneManyDelete_b3");
271: B b4 = new B("testOneManyDelete_b4");
272:
273: PersistenceManager pm = pmf.getPersistenceManager();
274: pm.makePersistent(a1);
275: pm.makePersistent(a2);
276: pm.makePersistent(b1);
277: pm.makePersistent(b2);
278: pm.makePersistent(b3);
279: pm.makePersistent(b4);
280: pm.close();
281:
282: try {
283: pm = pmf.getPersistenceManager();
284:
285: a1.getNbs().add(b1);
286: assertTrue(b1.getNa() == a1);
287: a1.getNbs().add(b2);
288: assertTrue(b2.getNa() == a1);
289:
290: a2.getNbs().add(b3);
291: assertTrue(b3.getNa() == a2);
292: a2.getNbs().add(b4);
293: assertTrue(b4.getNa() == a2);
294:
295: pm.close();
296:
297: pm = pmf.getPersistenceManager();
298: pm.currentTransaction().begin();
299: pm.deletePersistent(a1);
300: pm.currentTransaction().commit();
301: assertNull(
302: "reverse reference of a1.nbs (b1.na) must be null",
303: b1.getNa());
304: assertNull(
305: "reverse reference of a1.nbs (b2.na) must be null",
306: b2.getNa());
307: pm.currentTransaction().begin();
308: pm.deletePersistent(b3);
309: pm.currentTransaction().commit();
310: assertTrue(
311: "reverse reference of b3.na (a2.nbs) must not contain b3",
312: !a2.getNbs().contains(b3));
313: pm.close();
314:
315: pm = pmf.getPersistenceManager();
316: assertTrue(
317: "reverse reference of b3.na (a2.nbs) must not contain b3",
318: !a2.getNbs().contains(b3));
319:
320: assertEquals("Bad a2.nbs size", 1, a2.getNbs().size());
321: assertTrue(
322: "reverse reference of b3.na (a2.nbs) must not contain b3",
323: !a2.getNbs().contains(b3));
324: assertTrue(a2.getNbs().contains(b4));
325:
326: assertNull(
327: "reverse reference of a1.nbs (b1.na) must be null",
328: b1.getNa());
329: assertNull(
330: "reverse reference of a1.nbs (b2.na) must be null",
331: b2.getNa());
332: assertTrue(b4.getNa() == a2);
333:
334: pm.close();
335: } finally {
336: pm = pmf.getPersistenceManager();
337: pm.currentTransaction().begin();
338: pm.deletePersistent(a2);
339: pm.deletePersistent(b1);
340: pm.deletePersistent(b2);
341: pm.deletePersistent(b4);
342: pm.currentTransaction().commit();
343: pm.close();
344: }
345: }
346:
347: public void testManyMany() { // tests on active persistent objects
348: A a1 = new A("testManyMany_a1");
349: A a2 = new A("testManyMany_a2");
350: A a3 = new A("testManyMany_a3");
351:
352: B b1 = new B("testManyMany_b1");
353: B b2 = new B("testManyMany_b2");
354: B b3 = new B("testManyMany_b3");
355:
356: PersistenceManager pm = pmf.getPersistenceManager();
357: pm.makePersistent(a1);
358: pm.makePersistent(a2);
359: pm.makePersistent(a3);
360: pm.makePersistent(b1);
361: pm.makePersistent(b2);
362: pm.makePersistent(b3);
363: pm.close();
364:
365: try {
366: pm = pmf.getPersistenceManager();
367:
368: a1.getBs().add(b1);
369: assertTrue(b1.getAs().contains(a1));
370: a1.getBs().add(b2);
371: assertTrue(b2.getAs().contains(a1));
372:
373: a2.getBs().add(b1);
374: assertTrue(b1.getAs().contains(a2));
375: a2.getBs().add(b2);
376: assertTrue(b2.getAs().contains(a2));
377: a2.getBs().add(b3);
378: assertTrue(b3.getAs().contains(a2));
379:
380: a3.getBs().add(b2);
381: assertTrue(b2.getAs().contains(a3));
382: a3.getBs().add(b3);
383: assertTrue(b3.getAs().contains(a3));
384:
385: a1.getBs().add(b3);
386: assertTrue(b3.getAs().contains(a1));
387:
388: a1.getBs().remove(b1);
389: assertTrue(!b1.getAs().contains(a1));
390:
391: b2.getAs().remove(a2);
392: assertTrue(!a2.getBs().contains(b2));
393:
394: a1.setBs(a2.getBs());
395: // reload objects before testing effects of the above assignement
396: pm.close();
397: pm = pmf.getPersistenceManager();
398:
399: assertTrue(a1.getBs().contains(b1));
400: assertTrue(!a1.getBs().contains(b2));
401: assertTrue(a1.getBs().contains(b3));
402:
403: assertTrue(a2.getBs().contains(b1));
404: assertTrue(!a2.getBs().contains(b2));
405: assertTrue(a2.getBs().contains(b3));
406:
407: assertTrue(!a3.getBs().contains(b1));
408: assertTrue(a3.getBs().contains(b2));
409: assertTrue(a3.getBs().contains(b3));
410:
411: assertTrue(b1.getAs().contains(a1));
412: assertTrue(b1.getAs().contains(a2));
413: assertTrue(!b1.getAs().contains(a3));
414:
415: assertTrue(!b2.getAs().contains(a1));
416: assertTrue(!b2.getAs().contains(a2));
417: assertTrue(b2.getAs().contains(a3));
418:
419: assertTrue(b3.getAs().contains(a1));
420: assertTrue(b3.getAs().contains(a2));
421: assertTrue(b3.getAs().contains(a3));
422:
423: pm.close();
424: } finally {
425: pm = pmf.getPersistenceManager();
426: pm.currentTransaction().begin();
427: pm.deletePersistent(a1);
428: pm.deletePersistent(a2);
429: pm.deletePersistent(a3);
430: pm.deletePersistent(b1);
431: pm.deletePersistent(b2);
432: pm.deletePersistent(b3);
433: pm.currentTransaction().commit();
434: pm.close();
435: }
436: }
437:
438: public void testManyMany2() { // tests on non persistent objects
439: A a1 = new A("testManyMany2_a1");
440: A a2 = new A("testManyMany2_a2");
441: A a3 = new A("testManyMany2_a3");
442:
443: B b1 = new B("testManyMany2_b1");
444: B b2 = new B("testManyMany2_b2");
445: B b3 = new B("testManyMany2_b3");
446:
447: PersistenceManager pm = pmf.getPersistenceManager();
448:
449: a1.getBs().add(b1);
450: a1.getBs().add(b2);
451:
452: a2.getBs().add(b1);
453: a2.getBs().add(b2);
454: a2.getBs().add(b3);
455:
456: a3.getBs().add(b2);
457: a3.getBs().add(b3);
458:
459: pm.makePersistent(a1);
460: pm.makePersistent(a2);
461: pm.makePersistent(a3);
462:
463: pm.close();
464:
465: try {
466: pm = pmf.getPersistenceManager();
467:
468: assertTrue(b1.getAs().contains(a1));
469: assertTrue(b2.getAs().contains(a1));
470:
471: assertTrue(b1.getAs().contains(a2));
472: assertTrue(b2.getAs().contains(a2));
473: assertTrue(b3.getAs().contains(a2));
474:
475: assertTrue(b2.getAs().contains(a3));
476: assertTrue(b3.getAs().contains(a3));
477:
478: Collection c = new ArrayList();
479: c.addAll(a2.getBs());
480: a1.setBs(c);
481:
482: pm.close();
483:
484: pm = pmf.getPersistenceManager();
485:
486: assertTrue(a1.getBs().contains(b1));
487: assertTrue(a1.getBs().contains(b2));
488: assertTrue(a1.getBs().contains(b3));
489:
490: assertTrue(a2.getBs().contains(b1));
491: assertTrue(a2.getBs().contains(b2));
492: assertTrue(a2.getBs().contains(b3));
493:
494: assertTrue(!a3.getBs().contains(b1));
495: assertTrue(a3.getBs().contains(b2));
496: assertTrue(a3.getBs().contains(b3));
497:
498: assertTrue(b1.getAs().contains(a1));
499: assertTrue(b1.getAs().contains(a2));
500: assertTrue(!b1.getAs().contains(a3));
501:
502: assertTrue(b2.getAs().contains(a1));
503: assertTrue(b2.getAs().contains(a2));
504: assertTrue(b2.getAs().contains(a3));
505:
506: assertTrue(b3.getAs().contains(a1));
507: assertTrue(b3.getAs().contains(a2));
508: assertTrue(b3.getAs().contains(a3));
509:
510: pm.close();
511: } finally {
512: pm = pmf.getPersistenceManager();
513: pm.currentTransaction().begin();
514: pm.deletePersistent(a1);
515: pm.deletePersistent(a2);
516: pm.deletePersistent(a3);
517: pm.deletePersistent(b1);
518: pm.deletePersistent(b2);
519: pm.deletePersistent(b3);
520: pm.currentTransaction().commit();
521: pm.close();
522: }
523: }
524:
525: public void testManyMany3() { // tests with manually managed consistency
526: A a1 = new A("testManyMany3_a1");
527: A a2 = new A("testManyMany3_a2");
528: A a3 = new A("testManyMany3_a3");
529:
530: B b1 = new B("testManyMany3_b1");
531: B b2 = new B("testManyMany3_b2");
532: B b3 = new B("testManyMany3_b3");
533:
534: PersistenceManager pm = pmf.getPersistenceManager();
535:
536: a1.setBs(Arrays.asList(new Object[] { b1, b2 }));
537: a2.setBs(Arrays.asList(new Object[] { b1, b2, b3 }));
538: a3.setBs(Arrays.asList(new Object[] { b2, b3 }));
539:
540: b1.setAs(Arrays.asList(new Object[] { a1, a2 }));
541: b2.setAs(Arrays.asList(new Object[] { a1, a2, a3 }));
542: b3.setAs(Arrays.asList(new Object[] { a2, a3 }));
543:
544: pm.makePersistent(a1);
545: pm.makePersistent(a2);
546: pm.makePersistent(a3);
547:
548: pm.close();
549:
550: try {
551: pm = pmf.getPersistenceManager();
552:
553: assertTrue(a1.getBs().size() == 2);
554: assertTrue(a1.getBs().contains(b1));
555: assertTrue(a1.getBs().contains(b2));
556:
557: assertTrue(a2.getBs().size() == 3);
558: assertTrue(a2.getBs().contains(b1));
559: assertTrue(a2.getBs().contains(b2));
560: assertTrue(a2.getBs().contains(b3));
561:
562: assertTrue(a3.getBs().size() == 2);
563: assertTrue(a3.getBs().contains(b2));
564: assertTrue(a3.getBs().contains(b3));
565:
566: assertTrue(b1.getAs().size() == 2);
567: assertTrue(b1.getAs().contains(a1));
568: assertTrue(b1.getAs().contains(a2));
569:
570: assertTrue(b2.getAs().size() == 3);
571: assertTrue(b2.getAs().contains(a1));
572: assertTrue(b2.getAs().contains(a2));
573: assertTrue(b2.getAs().contains(a3));
574:
575: assertTrue(b3.getAs().size() == 2);
576: assertTrue(b3.getAs().contains(a2));
577: assertTrue(b3.getAs().contains(a3));
578:
579: pm.close();
580: } finally {
581: pm = pmf.getPersistenceManager();
582: pm.currentTransaction().begin();
583: pm.deletePersistent(a1);
584: pm.deletePersistent(a2);
585: pm.deletePersistent(a3);
586: pm.deletePersistent(b1);
587: pm.deletePersistent(b2);
588: pm.deletePersistent(b3);
589: pm.currentTransaction().commit();
590: pm.close();
591: }
592: }
593:
594: public void testManyManyDelete() {
595: A a1 = new A("testManyManyDelete_a1");
596: A a2 = new A("testManyManyDelete_a2");
597: A a3 = new A("testManyManyDelete_a3");
598:
599: B b1 = new B("testManyManyDelete_b1");
600: B b2 = new B("testManyManyDelete_b2");
601: B b3 = new B("testManyManyDelete_b3");
602:
603: PersistenceManager pm = pmf.getPersistenceManager();
604: pm.makePersistent(a1);
605: pm.makePersistent(a2);
606: pm.makePersistent(a3);
607: pm.makePersistent(b1);
608: pm.makePersistent(b2);
609: pm.makePersistent(b3);
610: pm.close();
611:
612: try {
613: pm = pmf.getPersistenceManager();
614:
615: a1.getBs().add(b1);
616: assertTrue(b1.getAs().contains(a1));
617: a1.getBs().add(b2);
618: assertTrue(b2.getAs().contains(a1));
619:
620: a2.getBs().add(b1);
621: assertTrue(b1.getAs().contains(a2));
622: a2.getBs().add(b2);
623: assertTrue(b2.getAs().contains(a2));
624: a2.getBs().add(b3);
625: assertTrue(b3.getAs().contains(a2));
626:
627: a3.getBs().add(b2);
628: assertTrue(b2.getAs().contains(a3));
629: a3.getBs().add(b3);
630: assertTrue(b3.getAs().contains(a3));
631:
632: pm.close();
633:
634: pm = pmf.getPersistenceManager();
635: pm.currentTransaction().begin();
636: pm.deletePersistent(b2);
637: pm.currentTransaction().commit();
638: pm.close();
639:
640: pm = pmf.getPersistenceManager();
641:
642: assertTrue(a1.getBs().size() == 1);
643: assertTrue(a1.getBs().contains(b1));
644:
645: assertTrue(a2.getBs().size() == 2);
646: assertTrue(a2.getBs().contains(b1));
647: assertTrue(a2.getBs().contains(b3));
648:
649: assertTrue(a3.getBs().size() == 1);
650: assertTrue(a3.getBs().contains(b3));
651:
652: assertTrue(b1.getAs().contains(a1));
653: assertTrue(b1.getAs().contains(a2));
654: assertTrue(!b1.getAs().contains(a3));
655:
656: assertTrue(!b3.getAs().contains(a1));
657: assertTrue(b3.getAs().contains(a2));
658: assertTrue(b3.getAs().contains(a3));
659:
660: pm.close();
661: } finally {
662: pm = pmf.getPersistenceManager();
663: pm.currentTransaction().begin();
664: pm.deletePersistent(a1);
665: pm.deletePersistent(a2);
666: pm.deletePersistent(a3);
667: pm.deletePersistent(b1);
668: pm.deletePersistent(b3);
669: pm.currentTransaction().commit();
670: pm.close();
671: }
672: }
673:
674: public void testNIEjboo() {
675: final int NB_ARTICLE = 10;
676: final int CATLAOGUE_SIZE = 2;
677: final int MARCHE_SIZE = 2;
678: PersistenceManager pm = pmf.getPersistenceManager();
679: pm.currentTransaction().begin();
680: Catalogue cat = null;
681: int nbCat = 0;
682: Marche mar = null;
683: int nbMar = 0;
684: Article a;
685: for (int idArt = 0; idArt < NB_ARTICLE; idArt++) {
686: if ((idArt / CATLAOGUE_SIZE) == nbCat) {
687: cat = new Catalogue();
688: pm.makePersistent(cat);
689: nbCat++;
690: }
691: if ((idArt / MARCHE_SIZE) == nbMar) {
692: mar = new Marche();
693: pm.makePersistent(mar);
694: nbMar++;
695: }
696: a = new Article(idArt);
697: pm.makePersistent(a);
698: a.setCatalogue(cat);
699: mar.getArticles().add(a);
700: }
701: pm.currentTransaction().commit();
702:
703: a = null;
704: cat = null;
705: mar = null;
706: pm.evictAll();
707:
708: Extent extent = pm.getExtent(Catalogue.class, true);
709: Iterator it = extent.iterator();
710: while (it.hasNext()) {
711: cat = (Catalogue) it.next();
712: logger.log(BasicLevel.DEBUG, "Catalogue " + cat.getId());
713: Collection arts = cat.getArticles();
714: Iterator articles = arts.iterator();
715: while (articles.hasNext()) {
716: a = (Article) articles.next();
717: logger.log(BasicLevel.DEBUG, "\tArticle " + a.getId());
718: Collection mars = a.getMarches();
719: Iterator marches = mars.iterator();
720: while (marches.hasNext()) {
721: mar = (Marche) marches.next();
722: logger.log(BasicLevel.DEBUG, "\t\tMarche "
723: + mar.getId());
724: Collection m2as = mar.getArticles();
725: assertTrue("The article '" + a.getId()
726: + "' is not in the collection marche("
727: + mar.getId() + ").articles", m2as
728: .contains(a));
729: }
730: }
731: }
732: extent.closeAll();
733:
734: pm.currentTransaction().begin();
735: Query q = pm.newQuery(Catalogue.class);
736: q.setResult("distinct this");
737: q
738: .setFilter("articles.contains(a) && a.marches.contains(m) && m.id==MID");
739: q.declareParameters("long MID");
740: q.declareVariables("Marche m;Article a");
741: Collection c = (Collection) q.execute(new Long(mar.getId()));
742: Collection expectedResults = Collections.singletonList(cat);
743: assertSameCollection(
744: "Collection of results is not the one expected",
745: expectedResults, c);
746: q.closeAll();
747: pm.currentTransaction().commit();
748:
749: a = null;
750: cat = null;
751: mar = null;
752: pm.currentTransaction().begin();
753: extent = pm.getExtent(Article.class, true);
754: it = extent.iterator();
755: while (it.hasNext()) {
756: a = (Article) it.next();
757: cat = a.getCatalogue();
758: if (cat != null) {
759: pm.deletePersistent(cat);
760: }
761: pm.deletePersistentAll(a.getMarches());
762: pm.deletePersistent(a);
763: }
764: pm.currentTransaction().commit();
765: pm.close();
766: }
767: }
|