KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > clients > entity > F_Relation_omuEC2


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
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.1 of the License, or 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
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: F_Relation_omuEC2.java,v 1.31 2005/06/06 07:58:37 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.clients.entity;
27
28 import junit.framework.Test;
29 import junit.framework.TestSuite;
30 import javax.naming.NamingException JavaDoc;
31 import javax.rmi.PortableRemoteObject JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Collection JavaDoc;
34 import java.util.Enumeration JavaDoc;
35 import java.util.Hashtable JavaDoc;
36 import java.util.Iterator JavaDoc;
37
38 import org.objectweb.jonas.jtests.beans.relation.omu.AHomeRemote;
39 import org.objectweb.jonas.jtests.beans.relation.omu.ARemote;
40 import org.objectweb.jonas.jtests.beans.relation.omu.BHomeRemote;
41 import org.objectweb.jonas.jtests.beans.relation.omu.BRemote;
42
43
44 /**
45  * For testing one-to-many unidirectional relationships
46  * @author S.Chassande-Barrioz, Helene Joanin
47  */

48
49 public class F_Relation_omuEC2 extends A_Cmp2Util {
50
51
52     private static String JavaDoc BEAN_HOME_A = "relation_omu_AHome";
53     protected static AHomeRemote ahome = null;
54     private static String JavaDoc BEAN_HOME_B = "relation_omu_BHome";
55     protected static BHomeRemote bhome = null;
56
57     static Hashtable JavaDoc tbRelationA2B = new Hashtable JavaDoc();
58
59     static {
60         tbRelationA2B.put("as0", new String JavaDoc[]{});
61         tbRelationA2B.put("as1", new String JavaDoc[]{"bs1_1"});
62         tbRelationA2B.put("as2", new String JavaDoc[]{"bs2_1", "bs2_2", "bs2_3"});
63         tbRelationA2B.put("a0", new String JavaDoc[]{});
64         tbRelationA2B.put("a1", new String JavaDoc[]{"b1_1", "b1_2", "b1_3"});
65         tbRelationA2B.put("a2", new String JavaDoc[]{"b2_1", "b2_2", "b2_3"});
66         tbRelationA2B.put("a3", new String JavaDoc[]{"b3_1"});
67         // Translate the String[] to a Collection of String
68
for (Enumeration JavaDoc ea = tbRelationA2B.keys(); ea.hasMoreElements();) {
69             String JavaDoc aname = (String JavaDoc) (ea.nextElement());
70             String JavaDoc[] tb = (String JavaDoc[]) tbRelationA2B.get(aname);
71             ArrayList JavaDoc col = new ArrayList JavaDoc(tb.length);
72             for (int i = 0; i < tb.length; i++) {
73                 col.add(tb[i]);
74             }
75             tbRelationA2B.put(aname, col);
76         }
77     }
78
79     public F_Relation_omuEC2(String JavaDoc name) {
80         super(name);
81         debug("F_Relation_omuEC2("+name+")");
82     }
83
84     protected static boolean isInit = false;
85
86     protected void setUp() {
87         super.setUp();
88         boolean ok = false;
89         int nbtry = 0;
90         while (!ok && nbtry < 3) {
91             if (!isInit) {
92                 // load bean if not loaded yet
93
useBeans("omu", true);
94                 try {
95                     ahome = (AHomeRemote) PortableRemoteObject.narrow(ictx.lookup(BEAN_HOME_A),
96                                                                       AHomeRemote.class);
97                     bhome = (BHomeRemote) PortableRemoteObject.narrow(ictx.lookup(BEAN_HOME_B),
98                                                                       BHomeRemote.class);
99                 } catch (NamingException JavaDoc e) {
100                     fail("Cannot get bean home: " + e.getMessage());
101                 }
102                 // check if tables have been initialized
103
try {
104                     ahome.findByPrimaryKey("a0");
105                 } catch (Exception JavaDoc e) {
106                     try {
107                         utx.begin();
108                         bhome.create("bs0");
109                         bhome.create("bs1_1");
110                         bhome.create("bs2_1");
111                         bhome.create("bs2_2");
112                         bhome.create("bs2_3");
113                         bhome.create("bs3");
114                         bhome.create("b0");
115                         bhome.create("b1_1");
116                         bhome.create("b1_2");
117                         bhome.create("b1_3");
118                         bhome.create("b2_1");
119                         bhome.create("b2_2");
120                         bhome.create("b2_3");
121                         bhome.create("b3_1");
122                         ARemote as0 = ahome.create("as0");
123                         ARemote as1 = ahome.create("as1");
124                         ARemote as2 = ahome.create("as2");
125                         ARemote a0 = ahome.create("a0");
126                         ARemote a1 = ahome.create("a1");
127                         ARemote a2 = ahome.create("a2");
128                         ARemote a3 = ahome.create("a3");
129                         as1.assignB((Collection JavaDoc)tbRelationA2B.get("as1"));
130                         as2.assignB((Collection JavaDoc)tbRelationA2B.get("as2"));
131                         a1.assignB((Collection JavaDoc)tbRelationA2B.get("a1"));
132                         a2.assignB((Collection JavaDoc)tbRelationA2B.get("a2"));
133                         a3.assignB((Collection JavaDoc)tbRelationA2B.get("a3"));
134                     } catch (Exception JavaDoc i) {
135                         fail("InitialState creation problem:: "+i);
136                     } finally {
137                         try {
138                             utx.commit();
139                         } catch (Exception JavaDoc ii) {
140                         }
141                     }
142                 }
143                 isInit = true;
144             }
145             // Check that all is OK. Sometimes, a test has failed and has corrupted
146
// the bean state in the database. We must unload and reload the bean then.
147
nbtry++;
148             try {
149                 if (initStateOK()) {
150                     ok = true;
151                 }
152             } catch (Exception JavaDoc e) {
153             }
154             if (!ok) {
155                 isInit = false;
156                 unloadBeans("omu");
157             }
158         }
159     }
160
161     /*
162      * Check that we are in the same state as after the tables creation for thoses beans A and B
163      * (ie if it is the initial state)
164      */

165     boolean initStateOK() throws Exception JavaDoc {
166         boolean isOk = true;
167         msgerror = new StringBuffer JavaDoc();
168         for (Enumeration JavaDoc ea = tbRelationA2B.keys(); ea.hasMoreElements();) {
169             String JavaDoc aname = (String JavaDoc) (ea.nextElement());
170             ARemote a = ahome.findByPrimaryKey(aname);
171             Collection JavaDoc colActual = a.retrieveB();
172             ArrayList JavaDoc colExpected = (ArrayList JavaDoc) (tbRelationA2B.get(aname));
173             debug("Relation for " + aname + " :: expected:" + colExpected + ", found:" + colActual );
174             if (!isCollectionEqual(colExpected, colActual)) {
175                 isOk = false;
176                 msgerror = msgerror.append("Wrong relation for " + aname
177                                            + " (expected:" + colExpected
178                                            + ", found:" + colActual + ")");
179             }
180         }
181         return isOk;
182     }
183
184     /**
185      * read A, but not B
186      */

187     public void testReadA() throws Exception JavaDoc {
188         Collection JavaDoc c = ahome.findAll();
189         for (Iterator JavaDoc i = c.iterator(); i.hasNext(); ) {
190             ARemote a = (ARemote) PortableRemoteObject.narrow(i.next(), ARemote.class);
191             String JavaDoc n = a.getId();
192         }
193     }
194
195     /**
196      * Check that the bean 'as0' has no relation.
197      */

198     public void _testBasicGetEmpty(int tx) throws Exception JavaDoc {
199         Collection JavaDoc c = null;
200         if ((tx == TX_CALL) || (tx == TX_RB)) {
201             utx.begin();
202         }
203         ARemote a = ahome.findByPrimaryKey("as0");
204         if (tx == TX_CONT) {
205             c = a.retrieveBInNewTx();
206         } else {
207             c = a.retrieveB();
208         }
209         if (tx == TX_CALL) {
210             utx.commit();
211         } else if (tx == TX_RB) {
212             utx.rollback();
213         }
214         checkIsInitialState();
215     }
216
217     public void testBasicGetEmptyTxNo() throws Exception JavaDoc {
218         _testBasicGetEmpty(TX_NO);
219     }
220
221     public void testBasicGetEmptyTxCall() throws Exception JavaDoc {
222         _testBasicGetEmpty(TX_CALL);
223     }
224
225     public void testBasicGetEmptyTxCont() throws Exception JavaDoc {
226         _testBasicGetEmpty(TX_CONT);
227     }
228
229     /**
230      * Check that the bean 'as1' has only one relation with 'bs1_1'.
231      */

232     public void _testBasicGetOne(int tx) throws Exception JavaDoc {
233
234         Collection JavaDoc c = null;
235         if ((tx == TX_CALL) || (tx == TX_RB)) {
236             utx.begin();
237         }
238         ARemote a = ahome.findByPrimaryKey("as1");
239         if (tx == TX_CONT) {
240             c = a.retrieveBInNewTx();
241         } else {
242             c = a.retrieveB();
243         }
244         if (tx == TX_CALL) {
245             utx.commit();
246         } else if (tx == TX_RB) {
247             utx.rollback();
248         }
249         // checking
250
assertEquals("Wrong relations size for as1: ", 1, c.size());
251         String JavaDoc s = "bs1_1";
252         assertTrue("Relation missing " + s + " for as1: ", c.contains(s));
253         checkIsInitialState();
254     }
255
256     public void testBasicGetOneTxNo() throws Exception JavaDoc {
257         _testBasicGetOne(TX_NO);
258     }
259
260     public void testBasicGetOneTxCall() throws Exception JavaDoc {
261         _testBasicGetOne(TX_CALL);
262     }
263
264     public void testBasicGetOneTxCont() throws Exception JavaDoc {
265         _testBasicGetOne(TX_CONT);
266     }
267
268     public void testBasicGetOneTxRb() throws Exception JavaDoc {
269         _testBasicGetOne(TX_RB);
270     }
271
272     /**
273      * Check that the bean 'as2' has many relations with 'bs2_1', 'bs2_2', 'bs2_3'.
274      */

275     public void _testBasicGetMany(int tx) throws Exception JavaDoc {
276
277         Collection JavaDoc c = null;
278         if ((tx == TX_CALL) || (tx == TX_RB)) {
279             utx.begin();
280         }
281         ARemote a = ahome.findByPrimaryKey("as2");
282         if (tx == TX_CONT) {
283             c = a.retrieveBInNewTx();
284         } else {
285             c = a.retrieveB();
286         }
287         if (tx == TX_CALL) {
288             utx.commit();
289         } else if (tx == TX_RB) {
290             utx.rollback();
291         }
292         // checking
293
assertEquals("Relations size of: ", 3, c.size());
294         String JavaDoc s = "bs2_1";
295         assertTrue("Relation missing " + s + ": ", c.contains(s));
296         s = "bs2_2";
297         assertTrue("Relation missing " + s + ": ", c.contains(s));
298         s = "bs2_3";
299         assertTrue("Relation missing " + s + ": ", c.contains(s));
300         checkIsInitialState();
301     }
302
303     public void testBasicGetManyTxNo() throws Exception JavaDoc {
304         _testBasicGetMany(TX_NO);
305     }
306
307     public void testBasicGetManyTxCall() throws Exception JavaDoc {
308         _testBasicGetMany(TX_CALL);
309     }
310
311     public void testBasicGetManyTxCont() throws Exception JavaDoc {
312         _testBasicGetMany(TX_CONT);
313     }
314
315
316     /**
317      * Set a relation to empty.
318      * Before: as1 -> bs1_1
319      * After: as1 bs1_1
320      */

321     public void _testBasicSetEmpty(int tx) throws Exception JavaDoc {
322         ArrayList JavaDoc ce = new ArrayList JavaDoc();
323         if ((tx == TX_CALL) || (tx == TX_RB)) {
324             utx.begin();
325         }
326         ARemote a = ahome.findByPrimaryKey("as1");
327         if (tx == TX_CONT) {
328             a.assignBInNewTx(ce);
329         } else {
330             a.assignB(ce);
331         }
332         if (tx == TX_CALL) {
333             utx.commit();
334         } else if (tx == TX_RB) {
335             utx.rollback();
336         }
337         // checking
338
Collection JavaDoc ca = a.retrieveB();
339         if (tx != TX_RB) {
340             assertEquals("Relations size of: ", 0, ca.size());
341         } else {
342             assertEquals("Relations size of: ", 1, ca.size());
343         }
344         // undo
345
if (tx != TX_RB) {
346             a.addInB("bs1_1");
347             // Check undo
348
ca = a.retrieveB();
349             assertEquals("After undo, Relations size of: ", 1, ca.size());
350         }
351         checkIsInitialState();
352     }
353
354     public void testBasicSetEmptyTxNo() throws Exception JavaDoc {
355         _testBasicSetEmpty(TX_NO);
356     }
357
358     public void testBasicSetEmptyTxCall() throws Exception JavaDoc {
359         _testBasicSetEmpty(TX_CALL);
360     }
361
362     public void testBasicSetEmptyTxCont() throws Exception JavaDoc {
363         _testBasicSetEmpty(TX_CONT);
364     }
365
366     public void testBasicSetEmptyTxRb() throws Exception JavaDoc {
367         _testBasicSetEmpty(TX_RB);
368     }
369
370
371     /**
372      * Set a relation to empty by clearing the collection.
373      * Before: as1 -> bs1_1
374      * After: as1 bs1_1
375      */

376     public void _testBasicClear(int tx) throws Exception JavaDoc {
377         ArrayList JavaDoc ce = new ArrayList JavaDoc();
378         if ((tx == TX_CALL) || (tx == TX_RB)) {
379             utx.begin();
380         }
381         ARemote a = ahome.findByPrimaryKey("as1");
382         if (tx == TX_CONT) {
383             a.clearBInNewTx();
384         } else {
385             a.clearB();
386         }
387         if (tx == TX_CALL) {
388             utx.commit();
389         } else if (tx == TX_RB) {
390             utx.rollback();
391         }
392         // checking
393
Collection JavaDoc ca = a.retrieveB();
394         if (tx != TX_RB) {
395             assertEquals("Relations size of: ", 0, ca.size());
396         } else {
397             assertEquals("Relations size of: ", 1, ca.size());
398         }
399         // undo
400
if (tx != TX_RB) {
401             a.addInB("bs1_1");
402             // Check undo
403
ca = a.retrieveB();
404             assertEquals("After undo, Relations size of: ", 1, ca.size());
405         }
406         checkIsInitialState();
407     }
408
409     public void testBasicClearTxNo() throws Exception JavaDoc {
410         _testBasicClear(TX_NO);
411     }
412
413     public void testBasicClearTxCall() throws Exception JavaDoc {
414         _testBasicClear(TX_CALL);
415     }
416
417     public void testBasicClearTxCont() throws Exception JavaDoc {
418         _testBasicClear(TX_CONT);
419     }
420
421     public void testBasicClearTxRb() throws Exception JavaDoc {
422         _testBasicClear(TX_RB);
423     }
424
425
426     /**
427      * Set a empty relation with 1 element.
428      * This added element was'nt already in an other relation.
429      * Before: as0 b0
430      * After: as0 -> b0
431      */

432     public void _testBasicSetOne(int tx) throws Exception JavaDoc {
433         ArrayList JavaDoc ce = new ArrayList JavaDoc();
434         ce.add("b0");
435         if ((tx == TX_CALL) || (tx == TX_RB)) {
436             utx.begin();
437         }
438         ARemote a = ahome.findByPrimaryKey("as0");
439         if (tx == TX_CONT) {
440             a.assignBInNewTx(ce);
441         } else {
442             a.assignB(ce);
443         }
444         if (tx == TX_CALL) {
445             utx.commit();
446         } else if (tx == TX_RB) {
447             utx.rollback();
448         }
449         // checking
450
Collection JavaDoc ca = a.retrieveB();
451         if (tx != TX_RB) {
452             assertTrue("Wrong relations as0 (required:" + ce + ", found:" + ca
453                        + ")", isCollectionEqual(ce, ca));
454         } else {
455             assertEquals("Relations size of: ", 0, ca.size());
456         }
457         // undo
458
if (tx != TX_RB) {
459             a.assignB(new ArrayList JavaDoc());
460             // Check undo
461
ca = a.retrieveB();
462             assertEquals("After undo, Relations size of: ", 0, ca.size());
463         }
464         checkIsInitialState();
465     }
466
467     public void testBasicSetOneTxNo() throws Exception JavaDoc {
468         _testBasicSetOne(TX_NO);
469     }
470
471     public void testBasicSetOneTxCall() throws Exception JavaDoc {
472         _testBasicSetOne(TX_CALL);
473     }
474
475     public void testBasicSetOneTxCont() throws Exception JavaDoc {
476         _testBasicSetOne(TX_CONT);
477     }
478
479     public void testBasicSetOneTxRb() throws Exception JavaDoc {
480         _testBasicSetOne(TX_RB);
481     }
482
483
484     /**
485      * Add an element to a empty relation.
486      * Same as _testBasicSetOne except that we add the element in the collection
487      * instead of we set directly the collection.
488      * Before: as0 bs0
489      * After: as0 -> bs0
490      */

491     public void _testBasicAddOne(int tx) throws Exception JavaDoc {
492         ArrayList JavaDoc ce = new ArrayList JavaDoc();
493         ce.add("bs0");
494         if ((tx == TX_CALL) || (tx == TX_RB)) {
495             utx.begin();
496         }
497         ARemote a = ahome.findByPrimaryKey("as0");
498         if (tx == TX_CONT) {
499             a.addInBInNewTx("bs0");
500         } else {
501             a.addInB("bs0");
502         }
503         if (tx == TX_CALL) {
504             utx.commit();
505         } else if (tx == TX_RB) {
506             utx.rollback();
507         }
508         // checking
509
Collection JavaDoc ca = a.retrieveB();
510         if (tx != TX_RB) {
511             assertTrue("Wrong relations as0 (required:" + ce + ", found:" + ca
512                        + ")", isCollectionEqual(ce, ca));
513         } else {
514             assertEquals("Relations size of: ", 0, ca.size());
515         }
516         // undo
517
if (tx != TX_RB) {
518             a.assignB(new ArrayList JavaDoc());
519             // Check undo
520
ca = a.retrieveB();
521             assertEquals("After undo, Relations size of: ", 0, ca.size());
522         }
523         checkIsInitialState();
524     }
525
526     public void testBasicAddOneTxNo() throws Exception JavaDoc {
527         _testBasicAddOne(TX_NO);
528     }
529
530     public void testBasicAddOneTxCall() throws Exception JavaDoc {
531         _testBasicAddOne(TX_CALL);
532     }
533
534     public void testBasicAddOneTxCont() throws Exception JavaDoc {
535         _testBasicAddOne(TX_CONT);
536     }
537
538     public void testBasicAddOneTxRb() throws Exception JavaDoc {
539         _testBasicAddOne(TX_RB);
540     }
541
542
543     /**
544      * Re-Set a relation with N element.
545      * Before: as1 -> bs1_1
546      * bs0
547      * bs3
548      * After: as1 -> bs0
549      * as1 -> bs3
550      * bs1_1
551      *
552      */

553     public void _testBasicSetMulti(int tx) throws Exception JavaDoc {
554         ArrayList JavaDoc ce = new ArrayList JavaDoc(2);
555         ce.add("bs0");
556         ce.add("bs3");
557         if ((tx == TX_CALL) || (tx == TX_RB)) {
558             utx.begin();
559         }
560         ARemote a = ahome.findByPrimaryKey("as1");
561         Collection JavaDoc co = a.retrieveB();
562         if (tx == TX_CONT) {
563             a.assignBInNewTx(ce);
564         } else {
565             a.assignB(ce);
566         }
567         if (tx == TX_CALL) {
568             utx.commit();
569         } else if (tx == TX_RB) {
570             utx.rollback();
571         }
572         // checking
573
Collection JavaDoc ca = a.retrieveB();
574         if (tx != TX_RB) {
575             assertTrue("Wrong relations as1 (required:" + ce + ", found:" + ca
576                        + ")", isCollectionEqual(ce, ca));
577         } else {
578             assertTrue("Wrong relations as1 (required:" + co + ", found:" + ca
579                        + ")", isCollectionEqual(co, ca));
580         }
581         // undo
582
if (tx != TX_RB) {
583             a.assignB(co);
584             // Check undo
585
ca = a.retrieveB();
586             assertTrue("After undo, wrong relations: ", isCollectionEqual(co, ca));
587         }
588         checkIsInitialState();
589     }
590
591     public void testBasicSetMultiTxNo() throws Exception JavaDoc {
592         _testBasicSetMulti(TX_NO);
593     }
594
595     public void testBasicSetMultiTxCall() throws Exception JavaDoc {
596         _testBasicSetMulti(TX_CALL);
597     }
598
599     public void testBasicSetMultiTxCont() throws Exception JavaDoc {
600         _testBasicSetMulti(TX_CONT);
601     }
602
603     public void testBasicSetMultiTxRb() throws Exception JavaDoc {
604         _testBasicSetMulti(TX_RB);
605     }
606
607
608     /**
609      * Add an element to a relation already multiple.
610      * Before: as1 -> bs1_1
611      * bs0
612      * After: as1 -> bs1_1
613      * as1 -> bs0
614      */

615     public void _testBasicAddMulti(int tx) throws Exception JavaDoc {
616         ArrayList JavaDoc ce = (ArrayList JavaDoc) tbRelationA2B.get("as1");
617         if ((tx == TX_CALL) || (tx == TX_RB)) {
618             utx.begin();
619         }
620         ARemote a = ahome.findByPrimaryKey("as1");
621         if (tx == TX_CONT) {
622             a.addInBInNewTx("bs0");
623         } else {
624             a.addInB("bs0");
625         }
626         ce.add("bs0");
627         if (tx == TX_CALL) {
628             utx.commit();
629         } else if (tx == TX_RB) {
630             utx.rollback();
631         }
632         // checking
633
Collection JavaDoc ca = a.retrieveB();
634         if (tx == TX_RB)
635             ce.remove("bs0");
636         try {
637             assertTrue(
638                        "Wrong relations as1 (required:" + ce + ", found:" + ca + ")",
639                        isCollectionEqual(ce, ca));
640         } finally {
641             if (tx != TX_RB)
642                 ce.remove("bs0");
643         }
644         // undo
645
if (tx != TX_RB) {
646             a.removeFromB("bs0");
647             // Check undo
648
ca = a.retrieveB();
649             assertTrue("After undo, wrong relations: (required:" + tbRelationA2B.get("as1") + ", found:" + ca
650                        + ")", isCollectionEqual((Collection JavaDoc) tbRelationA2B.get("as1"), ca));
651         }
652         checkIsInitialState();
653     }
654
655     public void testBasicAddMultiTxNo() throws Exception JavaDoc {
656         _testBasicAddMulti(TX_NO);
657     }
658
659     public void testBasicAddMultiTxCall() throws Exception JavaDoc {
660         _testBasicAddMulti(TX_CALL);
661     }
662
663     public void testBasicAddMultiTxCont() throws Exception JavaDoc {
664         _testBasicAddMulti(TX_CONT);
665     }
666
667     public void testBasicAddMultiTxRb() throws Exception JavaDoc {
668         _testBasicAddMulti(TX_RB);
669     }
670
671     /**
672      * Remove an element in a relation.
673      * Before: a3 -> b3_1
674      * Change: a3.retrieveB().remove(b3_1)
675      * After: a3 b3_1
676      *
677      * See Spec chapter 10.3.7.4, change example: a1.getB().remove(b1n)
678      */

679     public void _testBasicRemoveInRel(int tx) throws Exception JavaDoc {
680         String JavaDoc bRemoved = "b3_1";
681         if ((tx == TX_CALL) || (tx == TX_RB)) {
682             utx.begin();
683         }
684         ARemote a = ahome.findByPrimaryKey("a3");
685         if (tx == TX_CONT) {
686             a.removeFromBInNewTx(bRemoved);
687         } else {
688             a.removeFromB(bRemoved);
689         }
690         if (tx == TX_CALL) {
691             utx.commit();
692         } else if (tx == TX_RB) {
693             utx.rollback();
694         }
695         // checking
696
Collection JavaDoc ca = a.retrieveB();
697         if (tx != TX_RB) {
698             assertEquals("Wrong relations size for a3: ", 0, ca.size());
699         } else {
700             assertEquals("Wrong relations size for a3: ", 1, ca.size());
701         }
702
703         // undo
704
if (tx != TX_RB) {
705             a.addInB(bRemoved);
706         }
707         // check to initial state
708
checkIsInitialState();
709
710     }
711
712     public void testBasicRemoveInRelTxNo() throws Exception JavaDoc {
713         _testBasicRemoveInRel(TX_NO);
714     }
715
716     public void testBasicRemoveInRelTxCall() throws Exception JavaDoc {
717         _testBasicRemoveInRel(TX_CALL);
718     }
719
720     public void testBasicRemoveInRelTxCont() throws Exception JavaDoc {
721         _testBasicRemoveInRel(TX_CONT);
722     }
723
724     public void testBasicRemoveInRelTxRb() throws Exception JavaDoc {
725         _testBasicRemoveInRel(TX_RB);
726     }
727
728
729     /**
730      * Remove a bean B which is in a relation.
731      * Before: a3 -> b3_1
732      * change: B.remove(b3_1)
733      * After: a3
734      */

735     public void _testCohRemove(int tx) throws Exception JavaDoc {
736         if (tx == TX_CONT) {
737             // The transaction attribute of the remove method is TX_SUPPORT,
738
// so the transaction cannot be initiate by the container
739
fail("Transaction cannot be initiate by the container for this test");
740         }
741         String JavaDoc bRemoved = "b3_1";
742         if ((tx == TX_CALL) || (tx == TX_RB)) {
743             utx.begin();
744         }
745         bhome.remove(bRemoved);
746         if (tx == TX_CALL) {
747             utx.commit();
748         } else if (tx == TX_RB) {
749             utx.rollback();
750         }
751         // checking
752
ARemote a = ahome.findByPrimaryKey("a3");
753         Collection JavaDoc ca = a.retrieveB();
754         if (tx != TX_RB) {
755             assertEquals("Wrong relations size for a3: ", 0, ca.size());
756
757             // undo
758
bhome.create(bRemoved);
759             a.addInBInNewTx(bRemoved);
760         } else {
761             assertEquals("Wrong relations size for a3: ", 1, ca.size());
762         }
763         // check to initial state
764
checkIsInitialState();
765     }
766
767     public void testCohRemoveTxNo() throws Exception JavaDoc {
768         _testCohRemove(TX_NO);
769     }
770
771     public void testCohRemoveTxCall() throws Exception JavaDoc {
772         _testCohRemove(TX_CALL);
773     }
774
775     public void testCohRemoveTxRb() throws Exception JavaDoc {
776         _testCohRemove(TX_RB);
777     }
778
779
780     /**
781      * Remove a bean B which is in a relation.
782      * Same as _testCohRemove except that the rcalled remove method is on the bean
783      * instead of the home.
784      * Before: a3 -> b3_1
785      * change: B.remove(b3_1)
786      * After: a3
787      */

788     public void _testCohBeanRemove(int tx) throws Exception JavaDoc {
789         if (tx == TX_CONT) {
790             // The transaction attribute of the remove method is TX_SUPPORT,
791
// so the transaction cannot be initiate by the container
792
fail("Transaction cannot be initiate by the container for this test");
793         }
794         String JavaDoc bRemoved = "b3_1";
795         if ((tx == TX_CALL) || (tx == TX_RB)) {
796             utx.begin();
797         }
798         BRemote b = bhome.findByPrimaryKey(bRemoved);
799         b.remove();
800         if (tx == TX_CALL) {
801             utx.commit();
802         } else if (tx == TX_RB) {
803             utx.rollback();
804         }
805         // checking
806
ARemote a = ahome.findByPrimaryKey("a3");
807         Collection JavaDoc ca = a.retrieveB();
808         if (tx != TX_RB) {
809             assertEquals("Wrong relations size for a3: ", 0, ca.size());
810
811             // undo
812
bhome.create(bRemoved);
813             a.addInBInNewTx(bRemoved);
814         } else {
815             assertEquals("Wrong relations size for a3: ", 1, ca.size());
816         }
817         // check to initial state
818
checkIsInitialState();
819     }
820
821     public void testCohBeanRemoveTxNo() throws Exception JavaDoc {
822         _testCohBeanRemove(TX_NO);
823     }
824
825     public void testCohBeanRemoveTxCall() throws Exception JavaDoc {
826         _testCohBeanRemove(TX_CALL);
827     }
828
829     public void testCohBeanRemoveTxRb() throws Exception JavaDoc {
830         _testCohBeanRemove(TX_RB);
831     }
832
833
834     /**
835      * Set a empty relation with 1 element.
836      * This added element was already in a other relation.
837      * Before: a0
838      * a3 -> b3_1
839      * Change: a0.assignB({b3_1})
840      * After: a0 -> b3_1
841      * a3
842      */

843     public void _testCohSetOne(int tx) throws Exception JavaDoc {
844         ArrayList JavaDoc ce = new ArrayList JavaDoc();
845         String JavaDoc bAdded = "b3_1";
846         ce.add(bAdded);
847         if ((tx == TX_CALL) || (tx == TX_RB)) {
848             utx.begin();
849         }
850         ARemote a = ahome.findByPrimaryKey("a0");
851         if (tx == TX_CONT) {
852             a.assignBInNewTx(ce);
853         } else {
854             a.assignB(ce);
855         }
856         if (tx == TX_CALL) {
857             utx.commit();
858         } else if (tx == TX_RB) {
859             utx.rollback();
860         }
861         // checking
862
Collection JavaDoc ca = a.retrieveB();
863         ARemote a3 = ahome.findByPrimaryKey("a3");
864         Collection JavaDoc ca3 = a3.retrieveB();
865         if (tx != TX_RB) {
866             assertTrue("Wrong relations a0 (required:" + ce + ", found:" + ca
867                        + ")", isCollectionEqual(ce, ca));
868             assertTrue("Wrong relations for " + bAdded + ": found:" + ca3 ,
869                        !ca3.contains(bAdded));
870
871             // undo
872
a.assignB(new ArrayList JavaDoc());
873             a3.addInB(bAdded);
874             // Check undo
875
ca = a.retrieveB();
876             assertEquals("After undo, Relations size of: ", 0, ca.size());
877         } else {
878             assertTrue("Wrong relations for " + bAdded + ": ", a3.containInB(bAdded));
879         }
880         checkIsInitialState();
881     }
882
883     public void testCohSetOneTxNo() throws Exception JavaDoc {
884         _testCohSetOne(TX_NO);
885     }
886
887     public void testCohSetOneTxCall() throws Exception JavaDoc {
888         _testCohSetOne(TX_CALL);
889     }
890
891     public void testCohSetOneTxCont() throws Exception JavaDoc {
892         _testCohSetOne(TX_CONT);
893     }
894
895     public void testCohSetOneTxRb() throws Exception JavaDoc {
896         _testCohSetOne(TX_RB);
897     }
898
899
900     /**
901      * Add an element to a relation already multiple.
902      * The element was already in a relation.
903      * Check also the assignement rules for relationships.
904      * Before: a1 -> b1_1
905      * -> b1_2
906      * -> b1_3
907      * a3 -> b3_1
908      * Change: a1.addInB(b3_1)
909      * After: a1 -> b1_1
910      * -> b1_2
911      * -> b1_3
912      * -> b3_1
913      * a3
914      *
915      * See Spec chapter 10.3.7.4, change example: a1.getB().add(b2m)
916      */

917     public void _testCohAddOneInMulti(int tx) throws Exception JavaDoc {
918         ArrayList JavaDoc ce = new ArrayList JavaDoc();
919         ce.add("b1_1");
920         ce.add("b1_2");
921         ce.add("b1_3");
922         String JavaDoc bAdded = "b3_1";
923         if ((tx == TX_CALL) || (tx == TX_RB)) {
924             utx.begin();
925         }
926         ARemote a = ahome.findByPrimaryKey("a1");
927         Collection JavaDoc co = a.retrieveB();
928         if (tx == TX_CONT) {
929             a.addInBInNewTx(bAdded);
930         } else {
931             a.addInB(bAdded);
932         }
933         ce.add(bAdded);
934         if (tx == TX_CALL) {
935             utx.commit();
936         } else if (tx == TX_RB) {
937             utx.rollback();
938         }
939         // checking
940
Collection JavaDoc ca = a.retrieveB();
941         ARemote a3 = ahome.findByPrimaryKey("a3");
942         if (tx == TX_RB) {
943             assertTrue("Wrong relations a1 (required:" + co + ", found:" + ca
944                        + ") (Rollback problem)", isCollectionEqual(co, ca));
945             assertTrue("Wrong relations for " + bAdded + ": (coherence/rollback problem)", a3.containInB(bAdded));
946         } else {
947             try {
948                 assertTrue("Wrong relations: ", isCollectionEqual(ce, ca));
949                 assertTrue("The added element (" + bAdded
950                            + ") is yet in the relation a3", !a3.containInB(bAdded));
951             } finally {
952                 // undo
953
a.removeFromB(bAdded);
954                 a3.addInB(bAdded);
955             }
956         }
957         checkIsInitialState();
958     }
959
960     public void testCohAddOneInMultiTxNo() throws Exception JavaDoc {
961         _testCohAddOneInMulti(TX_NO);
962     }
963
964     public void testCohAddOneInMultiTxCall() throws Exception JavaDoc {
965         _testCohAddOneInMulti(TX_CALL);
966     }
967
968     public void testCohAddOneInMultiTxCont() throws Exception JavaDoc {
969         _testCohAddOneInMulti(TX_CONT);
970     }
971
972     public void testCohAddOneInMultiTxRb() throws Exception JavaDoc {
973         _testCohAddOneInMulti(TX_RB);
974     }
975
976     /**
977      * Add elements in an relationship.
978      * The relationship was not already empty.
979      * One element added had no relation, the other element had a relation
980      */

981     public void _testCohAddAllInMulti(int tx) throws Exception JavaDoc {
982         ArrayList JavaDoc c = new ArrayList JavaDoc();
983         c.add("b0");
984         c.add("b3_1");
985         if ((tx == TX_CALL) || (tx == TX_RB)) {
986             utx.begin();
987         }
988         ARemote a = ahome.findByPrimaryKey("a1");
989         Collection JavaDoc co = a.retrieveB();
990         if (tx == TX_CONT) {
991             a.addAllInBInNewTx(c);
992         } else {
993             a.addAllInB(c);
994         }
995         c.addAll(co); // c, added collection, becomes the expected collection
996
if (tx == TX_CALL) {
997             utx.commit();
998         } else if (tx == TX_RB) {
999             utx.rollback();
1000        }
1001        // checking
1002
Collection JavaDoc ca = a.retrieveB();
1003        ARemote a3 = ahome.findByPrimaryKey("a3");
1004        if (tx != TX_RB) {
1005            assertTrue("Wrong relations: ", isCollectionEqual(c, ca));
1006            assertTrue("Wrong relations for a3 ", !a3.containInB("b3_1"));
1007        } else {
1008            assertTrue("Wrong relations: ", isCollectionEqual(co, ca));
1009            assertTrue("Wrong relations for a3 ", a3.containInB("b3_1"));
1010        }
1011        // undo
1012
if (tx != TX_RB) {
1013            a.removeFromB("b0");
1014            a3.addInB("b3_1");
1015        }
1016        // check to initial
1017
checkIsInitialState();
1018
1019    }
1020
1021    public void testCohAddAllInMultiTxNo() throws Exception JavaDoc {
1022        _testCohAddAllInMulti(TX_NO);
1023    }
1024
1025    public void testCohAddAllInMultiTxCall() throws Exception JavaDoc {
1026        _testCohAddAllInMulti(TX_CALL);
1027    }
1028
1029    public void testCohAddAllInMultiTxCont() throws Exception JavaDoc {
1030        _testCohAddAllInMulti(TX_CONT);
1031    }
1032
1033    public void testCohAddAllInMultiTxRb() throws Exception JavaDoc {
1034        _testCohAddAllInMulti(TX_RB);
1035    }
1036
1037    /*
1038     * Set a relation to multiple.
1039     * All of element was already in an other relation.
1040     * Check also the assignement rules for relationships.
1041     * Before: a1 -> b1_1
1042     * -> b1_2
1043     * -> b1_3
1044     * a2 -> b2_1
1045     * -> b2_2
1046     * -> b2_3
1047     * Change: a1.assignB(a2.retrieveB())
1048     * After: a1 -> b2_1
1049     * -> b2_2
1050     * -> b2_3
1051     * a2
1052     * b1_1
1053     * b1_2
1054     * b1_3
1055     *
1056     * Spec chapter 10.3.7.4, change example: a1.setB(a2.getB())
1057     */

1058    public void _testCohSetMulti(int tx) throws Exception JavaDoc {
1059        if ((tx == TX_CALL) || (tx == TX_RB)) {
1060            utx.begin();
1061        }
1062        ARemote a1 = ahome.findByPrimaryKey("a1");
1063        Collection JavaDoc co1 = a1.retrieveB();
1064        ARemote a2 = ahome.findByPrimaryKey("a2");
1065        Collection JavaDoc co2 = a2.retrieveB();
1066        if (tx == TX_CONT) {
1067            a1.assignBInNewTx(co2);
1068        } else {
1069            a1.assignB(co2);
1070        }
1071        if (tx == TX_CALL) {
1072            utx.commit();
1073        } else if (tx == TX_RB) {
1074            utx.rollback();
1075        }
1076        // checking
1077
if (tx != TX_RB) {
1078            Collection JavaDoc ca1 = a1.retrieveB();
1079            assertTrue("Wrong relations for a1 (required:" + co2 + ", found:" + ca1 + ")", isCollectionEqual(co2, ca1));
1080            Collection JavaDoc ca2 = a2.retrieveB();
1081            assertEquals("Wrong relations size for a2: ", 0, ca2.size());
1082            // Undo
1083
a1.assignB(co1);
1084            a2.assignB(co2);
1085        }
1086        checkIsInitialState();
1087    }
1088
1089    public void testCohSetMultiTxNo() throws Exception JavaDoc {
1090        _testCohSetMulti(TX_NO);
1091    }
1092
1093    public void testCohSetMultiTxCall() throws Exception JavaDoc {
1094        _testCohSetMulti(TX_CALL);
1095    }
1096
1097    public void testCohSetMultiTxCont() throws Exception JavaDoc {
1098        _testCohSetMulti(TX_CONT);
1099    }
1100
1101    public void testCohSetMultiTxRb() throws Exception JavaDoc {
1102        _testCohSetMulti(TX_RB);
1103    }
1104
1105    /**
1106     * Set a relation to multiple
1107     * Some of element was already in this relation, other was in other relation,
1108     * and other wasn't in any relation.
1109     * Check also the assignement rules for relationships.
1110     */

1111    public void _testCohSetMultiVarious(int tx) throws Exception JavaDoc {
1112        ArrayList JavaDoc ce = new ArrayList JavaDoc();
1113        ce.add("b0");
1114        ce.add("b1_2");
1115        String JavaDoc b3Added = "b3_1";
1116        ce.add(b3Added);
1117        if ((tx == TX_CALL) || (tx == TX_RB)) {
1118            utx.begin();
1119        }
1120        ARemote a = ahome.findByPrimaryKey("a1");
1121        if (tx == TX_CONT) {
1122            a.assignBInNewTx(ce);
1123        } else {
1124            a.assignB(ce);
1125        }
1126        if (tx == TX_CALL) {
1127            utx.commit();
1128        } else if (tx == TX_RB) {
1129            utx.rollback();
1130        }
1131        // checking
1132
Collection JavaDoc co = new ArrayList JavaDoc(3);
1133        co.add("b1_1");
1134        co.add("b1_2");
1135        co.add("b1_3");
1136        Collection JavaDoc ca = a.retrieveB();
1137        ARemote a3 = ahome.findByPrimaryKey("a3");
1138        if (tx != TX_RB) {
1139            assertTrue("Wrong relations: ", isCollectionEqual(ce, ca));
1140            assertTrue("Wrong relations for " + b3Added + ": ", !a3.containInB(b3Added));
1141
1142            //Undo
1143
a.assignB(co);
1144            a3.addInB(b3Added);
1145            // Check undo
1146
ca = a.retrieveB();
1147            if (!isCollectionEqual(co, ca)) {
1148                fail("After undo, wrong relations: expected:" + co + ", found:" + ca + ")");
1149            }
1150        }
1151        checkIsInitialState();
1152    }
1153
1154    public void testCohSetMultiVariousTxNo() throws Exception JavaDoc {
1155        _testCohSetMultiVarious(TX_NO);
1156    }
1157
1158    public void testCohSetMultiVariousTxCall() throws Exception JavaDoc {
1159        _testCohSetMultiVarious(TX_CALL);
1160    }
1161
1162    public void testCohSetMultiVariousTxCont() throws Exception JavaDoc {
1163        _testCohSetMultiVarious(TX_CONT);
1164    }
1165
1166    public void testCohSetMultiVariousTxRb() throws Exception JavaDoc {
1167        _testCohSetMultiVarious(TX_RB);
1168    }
1169
1170
1171
1172    /**
1173     * Test that a muti-valued cmr field is initialized to an empty collection after its creation.
1174     * See spec. 2.0, & 10.5.5 page 171
1175     */

1176    public void _testCmrMultiIsInit(int tx) throws Exception JavaDoc {
1177        if (tx == TX_CONT) {
1178            // The transaction attribute of the create method is TX_SUPPORT,
1179
// so the transaction cannot be initiate by the container
1180
fail("Transaction cannot be initiate by the container for this test");
1181        }
1182        if (tx == TX_RB) {
1183            fail("Transaction attribute 'TX_RB' has no sense for this test");
1184        }
1185        if (tx == TX_CALL) {
1186            utx.begin();
1187        }
1188        ARemote a = ahome.create("aX");
1189        if (tx == TX_CALL) {
1190            utx.commit();
1191        }
1192        // checking
1193
Collection JavaDoc cb = a.retrieveB();
1194        assertEquals("The multi-valued cmr field 'b' not initialized with an empty collection",
1195                     0, cb.size());
1196        if (tx != TX_RB) {
1197            // undo
1198
a.remove();
1199        }
1200        // check to initial state
1201
checkIsInitialState();
1202    }
1203
1204    public void testCmrMultiIsInitTxNo() throws Exception JavaDoc {
1205        _testCmrMultiIsInit(TX_NO);
1206    }
1207
1208    public void testCmrMultiIsInitTxCall() throws Exception JavaDoc {
1209        _testCmrMultiIsInit(TX_CALL);
1210    }
1211
1212
1213    public static Test suite() {
1214        return new TestSuite(F_Relation_omuEC2.class);
1215    }
1216
1217    public static void main(String JavaDoc args[]) {
1218        String JavaDoc testtorun = null;
1219        // Get args
1220
for (int argn = 0; argn < args.length; argn++) {
1221            String JavaDoc s_arg = args[argn];
1222            Integer JavaDoc i_arg;
1223            if (s_arg.equals("-n")) {
1224                testtorun = args[++argn];
1225            }
1226        }
1227        if (testtorun == null) {
1228            junit.textui.TestRunner.run(suite());
1229        } else {
1230            junit.textui.TestRunner.run(new F_Relation_omuEC2(testtorun));
1231        }
1232    }
1233
1234}
1235
Popular Tags