KickJava   Java API By Example, From Geeks To Geeks.

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


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_mnbEC2.java,v 1.22 2004/05/24 07:55:01 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

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

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

181     boolean initStateOK() throws Exception JavaDoc {
182         boolean isOk = true;
183         msgerror = new StringBuffer JavaDoc();
184         // Check relations A to B
185
for (Enumeration JavaDoc ea = tbRelationA2B.keys(); ea.hasMoreElements();) {
186             String JavaDoc aname = (String JavaDoc) (ea.nextElement());
187             ARemote a = ahome.findByPrimaryKey(aname);
188             Collection JavaDoc colActual = a.retrieveB();
189             ArrayList JavaDoc colExpected = (ArrayList JavaDoc) (tbRelationA2B.get(aname));
190             if (!isCollectionEqual(colExpected, colActual)) {
191                 isOk = false;
192                 msgerror = msgerror.append("Wrong relation for " + aname
193                                            + " (expected:" + colExpected
194                                            + ", found:" + colActual + ")");
195             }
196         }
197         // Check Relations B to A
198
for (Enumeration JavaDoc ea1 = tbRelationB2A.keys(); ea1.hasMoreElements();) {
199             String JavaDoc aname1 = (String JavaDoc) (ea1.nextElement());
200             BRemote b = bhome.findByPrimaryKey(aname1);
201             Collection JavaDoc colActual1 = b.retrieveA();
202             ArrayList JavaDoc colExpected1 = (ArrayList JavaDoc) (tbRelationB2A.get(aname1));
203             if (!isCollectionEqual(colExpected1, colActual1)) {
204                 isOk = false;
205                 msgerror = msgerror.append("Wrong relation for " + aname1
206                                            + " (expected:" + colExpected1
207                                            + ", found:" + colActual1 + ")");
208             }
209         }
210         return isOk;
211     }
212
213     /**
214      * Check that the bean 'a0' has no relation.
215      */

216     public void _testBasicGetEmpty(int tx) throws Exception JavaDoc {
217         Collection JavaDoc c;
218         if ((tx == TX_CALL) || (tx == TX_RB)) {
219             utx.begin();
220         }
221         ARemote a = ahome.findByPrimaryKey("a0");
222         if (tx == TX_CONT) {
223             c = a.retrieveBInNewTx();
224         } else {
225             c = a.retrieveB();
226         }
227         if (tx == TX_CALL) {
228             utx.commit();
229         } else if (tx == TX_RB) {
230             utx.rollback();
231         }
232         checkIsInitialState();
233     }
234
235     public void testBasicGetEmptyTxNo() throws Exception JavaDoc {
236         _testBasicGetEmpty(TX_NO);
237     }
238
239     public void testBasicGetEmptyTxCall() throws Exception JavaDoc {
240         _testBasicGetEmpty(TX_CALL);
241     }
242
243     public void testBasicGetEmptyTxCont() throws Exception JavaDoc {
244         _testBasicGetEmpty(TX_CONT);
245     }
246
247     public void testBasicGetEmptyTxRb() throws Exception JavaDoc {
248         _testBasicGetEmpty(TX_RB);
249     }
250
251     /**
252      * Check the new relation a0-b0
253      */

254     public void _testBasicSetEmpty(int tx) throws Exception JavaDoc {
255         ArrayList JavaDoc c = new ArrayList JavaDoc(1);
256         c.add("b0");
257         if ((tx == TX_CALL) || (tx == TX_RB)) {
258             utx.begin();
259         }
260         ARemote a = ahome.findByPrimaryKey("a0");
261         if (tx == TX_CONT) {
262             a.assignBInNewTx(c);
263         } else {
264             a.assignB(c);
265         }
266         if (tx == TX_CALL) {
267             utx.commit();
268         } else if (tx == TX_RB) {
269             utx.rollback();
270         }
271         // checking
272
Collection JavaDoc c_value = a.retrieveB();
273         if (tx != TX_RB) {
274             assertTrue("Wrong relations a0 (required:" + c + ", found:" + c_value
275                        + ")", isCollectionEqual(c_value, c));
276         } else {
277             assertTrue("Wrong relation a0->b0 : ", c_value.isEmpty());
278         }
279         // undo
280
if (tx != TX_RB) {
281             a.assignB(new ArrayList JavaDoc());
282         }
283         checkIsInitialState();
284     }
285
286     public void testBasicSetEmptyTxNo() throws Exception JavaDoc {
287         _testBasicSetEmpty(TX_NO);
288     }
289
290     public void testBasicSetEmptyTxCall() throws Exception JavaDoc {
291         _testBasicSetEmpty(TX_CALL);
292     }
293
294     public void testBasicSetEmptyTxCont() throws Exception JavaDoc {
295         _testBasicSetEmpty(TX_CONT);
296     }
297
298     public void testBasicSetEmptyTxRb() throws Exception JavaDoc {
299         _testBasicSetEmpty(TX_RB);
300     }
301
302     public void testPhil() throws Exception JavaDoc {
303         utx.begin();
304         ARemote a2 = ahome.findByPrimaryKey("a2");
305         a2.clearB();
306         utx.rollback();
307         //checkIsInitialState();
308
}
309
310     /**
311      * Check clear function
312      */

313     public void _testBasicClear(int tx) throws Exception JavaDoc {
314         if ((tx == TX_CALL) || (tx == TX_RB)) {
315             utx.begin();
316         }
317         ARemote a2 = ahome.findByPrimaryKey("a2");
318         if (tx == TX_CONT) {
319             a2.clearBInNewTx();
320         } else {
321             a2.clearB();
322         }
323         if (tx == TX_CALL) {
324             utx.commit();
325         } else if (tx == TX_RB) {
326             utx.rollback();
327         }
328         // checking
329
Collection JavaDoc a2_value = a2.retrieveB();
330         BRemote b1 = bhome.findByPrimaryKey("b1");
331         BRemote b2 = bhome.findByPrimaryKey("b2");
332         BRemote b3 = bhome.findByPrimaryKey("b3");
333         ArrayList JavaDoc collb1 = new ArrayList JavaDoc(1);
334         collb1.add("a1");
335         ArrayList JavaDoc collb2 = new ArrayList JavaDoc(2);
336         collb2.add("a1");
337         collb2.add("a3");
338         ArrayList JavaDoc collb3 = new ArrayList JavaDoc(1);
339         collb3.add("a3");
340         Collection JavaDoc col=null;
341         if (tx != TX_RB) {
342             assertTrue("Wrong relations a2 (required: Empty "+", found:" + a2_value
343                        + ")", a2_value.isEmpty());
344             col = bhome.findByPrimaryKey("b1").retrieveA();
345             assertTrue("Wrong collection returned by b1.getA(): expected:" + collb1
346                        + " found: " + col, isCollectionEqual(collb1, col));
347             col = bhome.findByPrimaryKey("b2").retrieveA();
348             assertTrue("Wrong collection returned by b2.getA(): expected:" + collb2
349                        + " found: " + col, isCollectionEqual(collb2, col));
350             col = bhome.findByPrimaryKey("b3").retrieveA();
351             assertTrue("Wrong collection returned by b3.getA(): expected:" + collb3
352                        + " found: " + col, isCollectionEqual(collb3, col));
353             // undo
354
ArrayList JavaDoc a2undo = new ArrayList JavaDoc(3);
355             a2undo.add("b1");
356             a2undo.add("b2");
357             a2undo.add("b3");
358             a2.assignB(a2undo);
359         }
360         checkIsInitialState();
361     }
362
363     public void testBasicClearTxNo() throws Exception JavaDoc {
364         _testBasicClear(TX_NO);
365     }
366
367     public void testBasicClearTxCall() throws Exception JavaDoc {
368         _testBasicClear(TX_CALL);
369     }
370
371     public void testBasicClearTxCont() throws Exception JavaDoc {
372         _testBasicClear(TX_CONT);
373     }
374
375     public void testBasicClearTxRb() throws Exception JavaDoc {
376         _testBasicClear(TX_RB);
377     }
378
379     /**
380      * Remove an element in a relation.
381      */

382     public void _testCohRemoveInRel(int tx) throws Exception JavaDoc {
383         String JavaDoc bRemoved = "b4";
384         if ((tx == TX_CALL) || (tx == TX_RB)) {
385             utx.begin();
386         }
387         ARemote a = ahome.findByPrimaryKey("a3");
388         if (tx == TX_CONT) {
389             a.removeFromBInNewTx(bRemoved);
390         } else {
391             a.removeFromB(bRemoved);
392         }
393         if (tx == TX_CALL) {
394             utx.commit();
395         } else if (tx == TX_RB) {
396             utx.rollback();
397         }
398         // checking
399
Collection JavaDoc ca = a.retrieveB();
400         BRemote b4 = bhome.findByPrimaryKey("b4");
401         Collection JavaDoc cb4 = b4.retrieveA();
402         if (tx != TX_RB) {
403             assertEquals("Wrong relations size for a3: ", 2, ca.size());
404             assertEquals("Wrong relations size for b4: ", 0, cb4.size());
405         } else {
406             assertEquals("Wrong relations size for a3: ", 3, ca.size());
407             assertEquals("Wrong relations size for b4: ", 1, cb4.size());
408         }
409         // undo
410
if (tx != TX_RB) {
411             a.addInB(bRemoved);
412         }
413         // check to initial state
414
checkIsInitialState();
415
416     }
417
418     public void testCohRemoveInRelTxNo() throws Exception JavaDoc {
419         _testCohRemoveInRel(TX_NO);
420     }
421
422     public void testCohRemoveInRelTxCall() throws Exception JavaDoc {
423         _testCohRemoveInRel(TX_CALL);
424     }
425
426     public void testCohRemoveInRelTxCont() throws Exception JavaDoc {
427         _testCohRemoveInRel(TX_CONT);
428     }
429
430     public void testCohRemoveInRelTxRb() throws Exception JavaDoc {
431         _testCohRemoveInRel(TX_RB);
432     }
433
434     /**
435      * Check the coherence 10.3.7.6: a1.assignB(a3.retrieveB()) =>
436      * a1.retreiveB().contains("b2") &&
437      * a1.retreiveB().contains("b3") &&
438      * a1.retreiveB().contains("b4") &&
439      * a3.retreiveB().contains("b2") &&
440      * a3.retreiveB().contains("b3") &&
441      * a3.retreiveB().contains("b4") &&
442      * b1.retreiveB().contains("a2") &&
443      * b2.retreiveB().contains("a1") &&
444      * b2.retreiveB().contains("a2") &&
445      * b2.retreiveB().contains("a3") &&
446      * b3.retreiveB().contains("b4") &&
447      * b3.retreiveB().contains("a1") &&
448      * b3.retreiveB().contains("a2") &&
449      * b3.retreiveB().contains("a3") &&
450      * b4.retreiveB().contains("a1") &&
451      * b4.retreiveB().contains("a3") &&
452      * b4.retreiveB().contains("a4") &&
453      * b4.retreiveB().contains("a5") &&
454      */

455     public void _testCoSetFirst(int tx) throws Exception JavaDoc {
456         String JavaDoc c = null;
457         if ((tx == TX_CALL) || (tx == TX_RB)) {
458             utx.begin();
459         }
460         ARemote a1 = ahome.findByPrimaryKey("a1");
461         ARemote a3 = ahome.findByPrimaryKey("a3");
462         Collection JavaDoc c1 = a1.retrieveB();
463         if (tx == TX_CONT) {
464             a1.assignBInNewTx(a3.retrieveB());
465         } else {
466             a1.assignB(a3.retrieveB());
467         }
468         if (tx == TX_CALL) {
469             utx.commit();
470         } else if (tx == TX_RB) {
471             utx.rollback();
472         }
473
474         // checking
475
if (tx == TX_RB) {
476             checkIsInitialState();
477             return;
478         }
479
480         ArrayList JavaDoc al = new ArrayList JavaDoc(3);
481         Collection JavaDoc col = null;
482         al.add("b2");
483         al.add("b3");
484         al.add("b4");
485         col = a1.retrieveB();
486         assertTrue("Wrong collection returned by a1.getB(): expected:" + al
487                    + " found: " + col, isCollectionEqual(al, col));
488         col = a3.retrieveB();
489         assertTrue("Wrong collection returned by a3.getB(): expected:" + al
490                    + " found: " + col, isCollectionEqual(al, col));
491
492         al = new ArrayList JavaDoc(1);
493         al.add("a2");
494         col = bhome.findByPrimaryKey("b1").retrieveA();
495         assertTrue("Wrong collection returned by b1.getA(): expected:" + al
496                    + " found: " + col, isCollectionEqual(al, col));
497
498         al = new ArrayList JavaDoc(3);
499         al.add("a1");
500         al.add("a2");
501         al.add("a3");
502         col = bhome.findByPrimaryKey("b2").retrieveA();
503         assertTrue("Wrong collection returned by b2.getA(): expected:" + al
504                    + " found: " + col, isCollectionEqual(al, col));
505
506         al = new ArrayList JavaDoc(2);
507         al.add("a1");
508         al.add("a2");
509         al.add("a3");
510         col = bhome.findByPrimaryKey("b3").retrieveA();
511         assertTrue("Wrong collection returned by b3.getA(): expected:" + al
512                    + " found: " + col, isCollectionEqual(al, col));
513
514         al = new ArrayList JavaDoc(2);
515         al.add("a1");
516         al.add("a3");
517         col = bhome.findByPrimaryKey("b4").retrieveA();
518         assertTrue("Wrong collection returned by b4.getA(): expected:" + al
519                    + " found: " + col, isCollectionEqual(al, col));
520
521         a1.assignB(null);
522         al = new ArrayList JavaDoc(2);
523         al.add("b1");
524         al.add("b2");
525         a1.assignBInNewTx(al);
526         checkIsInitialState();
527
528     }
529
530     public void testCoSetFirstTxNo() throws Exception JavaDoc {
531         _testCoSetFirst(TX_NO);
532     }
533
534     public void testCoSetFirstTxCall() throws Exception JavaDoc {
535         _testCoSetFirst(TX_CALL);
536     }
537
538     public void testCoSetFirstTxCont() throws Exception JavaDoc {
539         _testCoSetFirst(TX_CONT);
540     }
541
542     public void testCoSetFirstTxRb() throws Exception JavaDoc {
543         _testCoSetFirst(TX_RB);
544     }
545
546     /**
547      * Check the coherence 10.3.7.6 : a1.getB().add(b3) =>
548      * a1.retreiveB().contains("b1") &&
549      * a1.retreiveB().contains("b2") &&
550      * a1.retreiveB().contains("b3") &&
551      * b3.retreiveB().contains("a1") &&
552      * b3.retreiveB().contains("a2") &&
553      * b3.retreiveB().contains("a3") &&
554      * b3.retreiveB().contains("a4") &&
555      */

556     public void _testCoSetSecond(int tx) throws Exception JavaDoc {
557         String JavaDoc c = null;
558
559         if ((tx == TX_CALL) || (tx == TX_RB)) {
560             utx.begin();
561         }
562         ARemote a1 = ahome.findByPrimaryKey("a1");
563
564         if (tx == TX_CONT) {
565             a1.addInBInNewTx("b3");
566         } else {
567             a1.addInB("b3");
568         }
569         if (tx == TX_CALL) {
570             utx.commit();
571         } else if (tx == TX_RB) {
572             utx.rollback();
573         }
574         // checking
575
if (tx != TX_RB) {
576             ArrayList JavaDoc al = new ArrayList JavaDoc(3);
577             Collection JavaDoc col = null;
578             al.add("b1");
579             al.add("b2");
580             al.add("b3");
581             col = a1.retrieveB();
582             assertTrue("Wrong collection returned by a1.getB(): expected:" + al
583                        + " found: " + col, isCollectionEqual(al, col));
584
585             al = new ArrayList JavaDoc(3);
586             al.add("a1");
587             al.add("a2");
588             al.add("a3");
589
590             col = bhome.findByPrimaryKey("b3").retrieveA();
591             assertTrue("Wrong collection returned by b3.getA(): expected:" + al
592                        + " found: " + col, isCollectionEqual(al, col));
593
594             // undo
595
a1.removeFromB("b3");
596         }
597         checkIsInitialState();
598     }
599
600     public void testCoSetSecondTxNo() throws Exception JavaDoc {
601         _testCoSetSecond(TX_NO);
602     }
603
604     public void testCoSetSecondTxCall() throws Exception JavaDoc {
605         _testCoSetSecond(TX_CALL);
606     }
607
608     public void testCoSetSecondTxCont() throws Exception JavaDoc {
609         _testCoSetSecond(TX_CONT);
610     }
611
612     public void testCoSetSecondTxRb() throws Exception JavaDoc {
613         _testCoSetSecond(TX_RB);
614     }
615
616     /**
617      * Check the coherence 10.3.7.6 : a2.getB().remove(b2) =>
618      * a2.retreiveB().contains("b1") &&
619      * a2.retreiveB().contains("b3") &&
620      * b2.retreiveB().contains("a1") &&
621      * b2.retreiveB().contains("a3") &&
622      * !b2.retreiveB().contains("a2")
623      */

624     public void _testCoSetRemove(int tx) throws Exception JavaDoc {
625
626         if (tx == TX_CONT) {
627             // The transaction attribute of the remove method is TX_SUPPORT,
628
// so the transaction cannot be initiate by the container
629
fail("Transaction cannot be initiate by the container for this test");
630         }
631
632         String JavaDoc c = null;
633
634         if ((tx == TX_CALL) || (tx == TX_RB)) {
635             utx.begin();
636         }
637         ARemote a2 = ahome.findByPrimaryKey("a2");
638         BRemote b2 = bhome.findByPrimaryKey("b2");
639         Collection JavaDoc c2 = a2.retrieveB();
640         if (tx == TX_CONT) {
641             a2.removeFromBInNewTx("b2");
642         } else {
643             a2.removeFromB("b2");
644         }
645         if (tx == TX_CALL) {
646             utx.commit();
647         } else if (tx == TX_RB) {
648             utx.rollback();
649         }
650         // checking
651
if (tx != TX_RB) {
652             ArrayList JavaDoc al = new ArrayList JavaDoc(2);
653             Collection JavaDoc col = null;
654             al.add("b1");
655             al.add("b3");
656             col = a2.retrieveB();
657             assertTrue("Wrong collection returned by a2.getB(): expected:" + al
658                        + " found: " + col, isCollectionEqual(al, col));
659
660             al = new ArrayList JavaDoc(2);
661             al.add("a1");
662             al.add("a3");
663             col = b2.retrieveA();
664             assertTrue("Wrong collection returned by b2.getA(): expected:" + al
665                        + " found: " + col, isCollectionEqual(al, col));
666
667             // undo
668
a2.addInB("b2");
669         }
670         checkIsInitialState();
671     }
672
673     public void testCoSetRemoveTxNo() throws Exception JavaDoc {
674         _testCoSetRemove(TX_NO);
675     }
676
677     public void testCoSetRemoveTxCall() throws Exception JavaDoc {
678         _testCoSetRemove(TX_CALL);
679     }
680
681     public void testCoSetRemoveTxRb() throws Exception JavaDoc {
682         _testCoSetRemove(TX_RB);
683     }
684
685     /**
686      * Test of coherence b2.remove() => a1.retrieveB()=null && a2.retrieveB()=null && a3.retrieveB()=null
687      */

688     public void _testCohRemoveB(int tx) throws Exception JavaDoc {
689
690         if (tx == TX_CONT) {
691             // The transaction attribute of the remove method is TX_SUPPORT,
692
// so the transaction cannot be initiate by the container
693
fail("Transaction cannot be initiate by the container for this test");
694         }
695
696         if ((tx == TX_CALL) || (tx == TX_RB)) {
697             utx.begin();
698         }
699         bhome.remove("b2");
700         if (tx == TX_CALL) {
701             utx.commit();
702         } else if (tx == TX_RB) {
703             utx.rollback();
704         }
705         // checking
706

707         if (tx != TX_RB) {
708             ARemote a1 = ahome.findByPrimaryKey("a1");
709             ARemote a2 = ahome.findByPrimaryKey("a2");
710             ARemote a3 = ahome.findByPrimaryKey("a3");
711             Collection JavaDoc idrela1 = a1.retrieveB();
712             Collection JavaDoc idrela2 = a2.retrieveB();
713             Collection JavaDoc idrela3 = a3.retrieveB();
714             ArrayList JavaDoc col_a1 = new ArrayList JavaDoc(1);
715             col_a1.add("b1");
716             ArrayList JavaDoc col_a2 = new ArrayList JavaDoc(2);
717             col_a2.add("b1");
718             col_a2.add("b3");
719             ArrayList JavaDoc col_a3 = new ArrayList JavaDoc(2);
720             col_a3.add("b3");
721             col_a3.add("b4");
722             assertTrue("Wrong relation a1 : Expected [b1] , found "+idrela1, isCollectionEqual(idrela1,col_a1));
723             assertTrue("Wrong relation a2 : Expected [b1, b3] , found "+idrela2 , isCollectionEqual(idrela2,col_a2));
724             assertTrue("Wrong relation a3 : Expected [b3, b4] , found "+idrela3, isCollectionEqual(idrela3,col_a3));
725             // undo
726
BRemote b2 = bhome.create("b2");
727             col_a2 = new ArrayList JavaDoc(3);
728             col_a2.add("a1");
729             col_a2.add("a2");
730             col_a2.add("a3");
731
732             b2.assignAInNewTx(col_a2);
733         }
734         checkIsInitialState();
735     }
736
737     public void testCohRemoveBTxNo() throws Exception JavaDoc {
738         _testCohRemoveB(TX_NO);
739     }
740
741     public void testCohRemoveBTxCall() throws Exception JavaDoc {
742         _testCohRemoveB(TX_CALL);
743     }
744
745     public void testCohRemoveBTxRb() throws Exception JavaDoc {
746         _testCohRemoveB(TX_RB);
747     }
748
749     /**
750      * Test of coherence b2.remove() => a1.retrieveB()=null && a2.retrieveB()=null && a3.retrieveB()=null
751      * Same as _testCohRemoveB except that the called remove method is on the bean
752      * instead of the home.
753      */

754     public void _testCohBeanRemoveB(int tx) throws Exception JavaDoc {
755
756         if (tx == TX_CONT) {
757             // The transaction attribute of the remove method is TX_SUPPORT,
758
// so the transaction cannot be initiate by the container
759
fail("Transaction cannot be initiate by the container for this test");
760         }
761
762         if ((tx == TX_CALL) || (tx == TX_RB)) {
763             utx.begin();
764         }
765         BRemote b = bhome.findByPrimaryKey("b2");
766         b.remove();
767         if (tx == TX_CALL) {
768             utx.commit();
769         } else if (tx == TX_RB) {
770             utx.rollback();
771         }
772         // checking
773

774         if (tx != TX_RB) {
775             ARemote a1 = ahome.findByPrimaryKey("a1");
776             ARemote a2 = ahome.findByPrimaryKey("a2");
777             ARemote a3 = ahome.findByPrimaryKey("a3");
778             Collection JavaDoc idrela1 = a1.retrieveB();
779             Collection JavaDoc idrela2 = a2.retrieveB();
780             Collection JavaDoc idrela3 = a3.retrieveB();
781             ArrayList JavaDoc col_a1 = new ArrayList JavaDoc(1);
782             col_a1.add("b1");
783             ArrayList JavaDoc col_a2 = new ArrayList JavaDoc(2);
784             col_a2.add("b1");
785             col_a2.add("b3");
786             ArrayList JavaDoc col_a3 = new ArrayList JavaDoc(2);
787             col_a3.add("b3");
788             col_a3.add("b4");
789             assertTrue("Wrong relation a1 : Expected [b1] , found "+idrela1, isCollectionEqual(idrela1,col_a1));
790             assertTrue("Wrong relation a2 : Expected [b1, b3] , found "+idrela2 , isCollectionEqual(idrela2,col_a2));
791             assertTrue("Wrong relation a3 : Expected [b3, b4] , found "+idrela3, isCollectionEqual(idrela3,col_a3));
792             // undo
793
BRemote b2 = bhome.create("b2");
794             col_a2 = new ArrayList JavaDoc(3);
795             col_a2.add("a1");
796             col_a2.add("a2");
797             col_a2.add("a3");
798
799             b2.assignAInNewTx(col_a2);
800         }
801         checkIsInitialState();
802     }
803
804     public void testCohBeanRemoveBTxNo() throws Exception JavaDoc {
805         _testCohBeanRemoveB(TX_NO);
806     }
807
808     public void testCohBeanRemoveBTxCall() throws Exception JavaDoc {
809         _testCohBeanRemoveB(TX_CALL);
810     }
811
812     public void testCohBeanRemoveBTxRb() throws Exception JavaDoc {
813         _testCohBeanRemoveB(TX_RB);
814     }
815
816     /**
817      * Test of coherence a2.remove() => b1.retrieveB()=null && b2.retrieveB()=null && b3.retrieveB()=null
818      */

819     public void _testCohRemoveA(int tx) throws Exception JavaDoc {
820
821         if (tx == TX_CONT) {
822             // The transaction attribute of the remove method is TX_SUPPORT,
823
// so the transaction cannot be initiate by the container
824
fail("Transaction cannot be initiate by the container for this test");
825         }
826
827         if ((tx == TX_CALL) || (tx == TX_RB)) {
828             utx.begin();
829         }
830         ahome.remove("a2");
831         if (tx == TX_CALL) {
832             utx.commit();
833         } else if (tx == TX_RB) {
834             utx.rollback();
835         }
836         // checking
837

838         if (tx != TX_RB) {
839             BRemote b1 = bhome.findByPrimaryKey("b1");
840             BRemote b2 = bhome.findByPrimaryKey("b2");
841             BRemote b3 = bhome.findByPrimaryKey("b3");
842             Collection JavaDoc idrelb1 = b1.retrieveA();
843             Collection JavaDoc idrelb2 = b2.retrieveA();
844             Collection JavaDoc idrelb3 = b3.retrieveA();
845             ArrayList JavaDoc col_b1 = new ArrayList JavaDoc(1);
846             col_b1.add("a1");
847             ArrayList JavaDoc col_b2 = new ArrayList JavaDoc(2);
848             col_b2.add("a1");
849             col_b2.add("a3");
850             ArrayList JavaDoc col_b3 = new ArrayList JavaDoc(1);
851             col_b3.add("a3");
852             assertTrue("Wrong relation b1 : Expected [a1] , found "+idrelb1, isCollectionEqual(idrelb1,col_b1));
853             assertTrue("Wrong relation b2 : Expected [a1, a3] , found "+idrelb2 , isCollectionEqual(idrelb2,col_b2));
854             assertTrue("Wrong relation b3 : Expected [a3] , found "+idrelb3, isCollectionEqual(idrelb3,col_b3));
855             // undo
856
ahome.create("a2");
857             ARemote a2 = ahome.findByPrimaryKey("a2");
858             ArrayList JavaDoc col_a2 = new ArrayList JavaDoc(3);
859             col_a2.add("b1");
860             col_a2.add("b2");
861             col_a2.add("b3");
862
863             a2.assignB(col_a2);
864         }
865         checkIsInitialState();
866     }
867
868     public void testCohRemoveATxNo() throws Exception JavaDoc {
869         _testCohRemoveA(TX_NO);
870     }
871
872     public void testCohRemoveATxCall() throws Exception JavaDoc {
873         _testCohRemoveA(TX_CALL);
874     }
875
876     public void testCohRemoveATxRb() throws Exception JavaDoc {
877         _testCohRemoveA(TX_RB);
878     }
879
880     /**
881      * Test of coherence a2.remove() => b1.retrieveB()=null && b2.retrieveB()=null && b3.retrieveB()=null
882      * Same as _testCohRemoveA except that the called remove method is on the bean
883      * instead of the home.
884      */

885     public void _testCohBeanRemoveA(int tx) throws Exception JavaDoc {
886
887         if (tx == TX_CONT) {
888             // The transaction attribute of the remove method is TX_SUPPORT,
889
// so the transaction cannot be initiate by the container
890
fail("Transaction cannot be initiate by the container for this test");
891         }
892
893         if ((tx == TX_CALL) || (tx == TX_RB)) {
894             utx.begin();
895         }
896         ARemote a = ahome.findByPrimaryKey("a2");
897         a.remove();
898         if (tx == TX_CALL) {
899             utx.commit();
900         } else if (tx == TX_RB) {
901             utx.rollback();
902         }
903         // checking
904

905         if (tx != TX_RB) {
906             BRemote b1 = bhome.findByPrimaryKey("b1");
907             BRemote b2 = bhome.findByPrimaryKey("b2");
908             BRemote b3 = bhome.findByPrimaryKey("b3");
909             Collection JavaDoc idrelb1 = b1.retrieveA();
910             Collection JavaDoc idrelb2 = b2.retrieveA();
911             Collection JavaDoc idrelb3 = b3.retrieveA();
912             ArrayList JavaDoc col_b1 = new ArrayList JavaDoc(1);
913             col_b1.add("a1");
914             ArrayList JavaDoc col_b2 = new ArrayList JavaDoc(2);
915             col_b2.add("a1");
916             col_b2.add("a3");
917             ArrayList JavaDoc col_b3 = new ArrayList JavaDoc(1);
918             col_b3.add("a3");
919             assertTrue("Wrong relation b1 : Expected [a1] , found "+idrelb1, isCollectionEqual(idrelb1,col_b1));
920             assertTrue("Wrong relation b2 : Expected [a1, a3] , found "+idrelb2 , isCollectionEqual(idrelb2,col_b2));
921             assertTrue("Wrong relation b3 : Expected [a3] , found "+idrelb3, isCollectionEqual(idrelb3,col_b3));
922             // undo
923
ahome.create("a2");
924             ARemote a2 = ahome.findByPrimaryKey("a2");
925             ArrayList JavaDoc col_a2 = new ArrayList JavaDoc(3);
926             col_a2.add("b1");
927             col_a2.add("b2");
928             col_a2.add("b3");
929
930             a2.assignB(col_a2);
931         }
932         checkIsInitialState();
933     }
934
935     public void testCohBeanRemoveATxNo() throws Exception JavaDoc {
936         _testCohBeanRemoveA(TX_NO);
937     }
938
939     public void testCohBeanRemoveATxCall() throws Exception JavaDoc {
940         _testCohBeanRemoveA(TX_CALL);
941     }
942
943     public void testCohBeanRemoveATxRb() throws Exception JavaDoc {
944         _testCohBeanRemoveA(TX_RB);
945     }
946
947     public static Test suite() {
948         return new TestSuite(F_Relation_mnbEC2.class);
949     }
950
951     public static void main(String JavaDoc args[]) {
952         String JavaDoc testtorun = null;
953         // Get args
954
for (int argn = 0; argn < args.length; argn++) {
955             String JavaDoc s_arg = args[argn];
956             Integer JavaDoc i_arg;
957             if (s_arg.equals("-n")) {
958                 testtorun = args[++argn];
959             }
960         }
961         if (testtorun == null) {
962             junit.textui.TestRunner.run(suite());
963         } else {
964             junit.textui.TestRunner.run(new F_Relation_mnbEC2(testtorun));
965         }
966     }
967
968 }
969
Popular Tags