KickJava   Java API By Example, From Geeks To Geeks.

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


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: G_Relation_ombEC2.java,v 1.1 2003/10/08 08:16:57 legrasi 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 import java.util.Iterator JavaDoc;
33
34 import javax.naming.NamingException JavaDoc;
35 import javax.rmi.PortableRemoteObject JavaDoc;
36
37 import junit.framework.Test;
38 import junit.framework.TestSuite;
39
40 import org.objectweb.jonas.jtests.beans.relation.omb.AHomeRemote;
41 import org.objectweb.jonas.jtests.beans.relation.omb.ARemote;
42 import org.objectweb.jonas.jtests.beans.relation.omb.BHomeRemote;
43 import org.objectweb.jonas.jtests.beans.relation.omb.BRemote;
44
45 /**
46  * For testing one-to-many bidirectional relationships
47  * @author Helene Joanin
48  */

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

183     boolean initStateOK() throws Exception JavaDoc {
184         boolean isOk = true;
185         msgerror = new StringBuffer JavaDoc();
186         // Check the relations A => B
187
for (Enumeration JavaDoc ea = a2b.keys(); ea.hasMoreElements();) {
188             String JavaDoc aname = (String JavaDoc) (ea.nextElement());
189             ARemote a = ahome.findByPrimaryKey(aname);
190             Collection JavaDoc colActual = a.retrieveB();
191             Collection JavaDoc colExpected = (Collection JavaDoc) (a2b.get(aname));
192             if (!isCollectionEqual(colExpected, colActual)) {
193                 isOk = false;
194                 msgerror = msgerror.append("\nWrong relation for " + aname
195                                            + " (expected:" + colExpected
196                                            + ", found:" + colActual + ")");
197             }
198         }
199         // Check the relation B => A
200
for (int i = 0; i < b2a.length; i++) {
201             BRemote b = bhome.findByPrimaryKey(b2a[i][0]);
202             String JavaDoc pkb = b.getId();
203             String JavaDoc pka = b.retrieveA();
204             if (b2a[i][1] == null && pka != null
205                 || b2a[i][1] != null && !b2a[i][1].equals(pka)) {
206                 isOk = false;
207                 msgerror = msgerror.append("\nWrong relation for " + pkb
208                                            + " (expected:" + b2a[i][1]
209                                            + ", found:" + pka + ")");
210             }
211
212         }
213         return isOk;
214     }
215
216
217     /**
218      * Check that the bean 'as0' has no relation.
219      */

220     public void tBasicGetEmptyA2B(int tx) throws Exception JavaDoc {
221         Collection JavaDoc c = null;
222         if ((tx == TX_CALL) || (tx == TX_RB)) {
223             utx.begin();
224         }
225         ARemote a = ahome.findByPrimaryKey("as0");
226         if (tx == TX_CONT) {
227             c = a.retrieveBInNewTx();
228         } else {
229             c = a.retrieveB();
230         }
231         if (tx == TX_CALL) {
232             utx.commit();
233         } else if (tx == TX_RB) {
234             utx.rollback();
235         }
236         // checking
237
checkIsInitialState();
238     }
239
240     public void testBasicGetEmptyA2BTxNo() throws Exception JavaDoc {
241         tBasicGetEmptyA2B(TX_NO);
242     }
243     public void testBasicGetEmptyA2BTxCall() throws Exception JavaDoc {
244         tBasicGetEmptyA2B(TX_CALL);
245     }
246     public void testBasicGetEmptyA2BTxCont() throws Exception JavaDoc {
247         tBasicGetEmptyA2B(TX_CONT);
248     }
249    
250
251     /**
252      * Check that the bean 'bs0' has no relation.
253      */

254     public void tBasicGetEmptyB2A(int tx) throws Exception JavaDoc {
255         if ((tx == TX_CALL) || (tx == TX_RB)) {
256             utx.begin();
257         }
258         String JavaDoc pka = null;
259         BRemote b = bhome.findByPrimaryKey("bs0");
260         if (tx == TX_CONT) {
261             pka = b.retrieveAInNewTx();
262         } else {
263             pka = b.retrieveA();
264         }
265         if (tx == TX_CALL) {
266             utx.commit();
267         } else if (tx == TX_RB) {
268             utx.rollback();
269         }
270         // checking
271
checkIsInitialState();
272     }
273     public void testBasicGetEmptyB2ANoTx() throws Exception JavaDoc {
274         tBasicGetEmptyB2A(TX_NO);
275     }
276     public void testBasicGetEmptyB2ATxCall() throws Exception JavaDoc {
277         tBasicGetEmptyB2A(TX_CALL);
278     }
279     public void testBasicGetEmptyB2ATxCont() throws Exception JavaDoc {
280         tBasicGetEmptyB2A(TX_CONT);
281     }
282   
283
284
285     /**
286      * Check that the bean 'as1' has only one relation with 'bs1_1'.
287      */

288     public void tBasicGetOneA2B(int tx) throws Exception JavaDoc {
289
290         Collection JavaDoc c = null;
291         if ((tx == TX_CALL) || (tx == TX_RB)) {
292             utx.begin();
293         }
294         ARemote a = ahome.findByPrimaryKey("as1");
295         if (tx == TX_CONT) {
296             c = a.retrieveBInNewTx();
297         } else {
298             c = a.retrieveB();
299         }
300         if (tx == TX_CALL) {
301             utx.commit();
302         } else if (tx == TX_RB) {
303             utx.rollback();
304         }
305         // checking
306
assertEquals("Relations size of: ", 1, c.size());
307         String JavaDoc s = "bs1_1";
308         assertTrue("Relation missing " + s + ": ", c.contains(s));
309         checkIsInitialState();
310     }
311
312    
313     /**
314      * Check that the bean 'as2' has many relations with 'bs2_1', 'bs2_2', 'bs2_3'.
315      */

316     public void tBasicGetManyA2B(int tx) throws Exception JavaDoc {
317
318         Collection JavaDoc c = null;
319         if ((tx == TX_CALL) || (tx == TX_RB)) {
320             utx.begin();
321         }
322         ARemote a = ahome.findByPrimaryKey("as2");
323         if (tx == TX_CONT) {
324             c = a.retrieveBInNewTx();
325         } else {
326             c = a.retrieveB();
327         }
328         if (tx == TX_CALL) {
329             utx.commit();
330         } else if (tx == TX_RB) {
331             utx.rollback();
332         }
333         // checking
334
assertEquals("Relations size of: ", 3, c.size());
335         String JavaDoc s = "bs2_1";
336         assertTrue("Relation missing " + s + ": ", c.contains(s));
337         s = "bs2_2";
338         assertTrue("Relation missing " + s + ": ", c.contains(s));
339         s = "bs2_3";
340         assertTrue("Relation missing " + s + ": ", c.contains(s));
341         checkIsInitialState();
342     }
343
344  
345
346     /**
347      * Set a A2B relation to empty.
348      * Before: as1 <-> bs1_1
349      * After: as1 bs1_1
350      * Check also the assignement rules for relationships.
351      */

352     public void tCohSetEmptyA2B(int tx) throws Exception JavaDoc {
353         ArrayList JavaDoc ce = new ArrayList JavaDoc();
354         if ((tx == TX_CALL) || (tx == TX_RB)) {
355             utx.begin();
356         }
357         ARemote a = ahome.findByPrimaryKey("as1");
358         if (tx == TX_CONT) {
359             a.assignBInNewTx(ce);
360         } else {
361             a.assignB(ce);
362         }
363         if (tx == TX_CALL) {
364             utx.commit();
365         } else if (tx == TX_RB) {
366             utx.rollback();
367         }
368         if (tx != TX_RB) {
369             // checking
370
Collection JavaDoc ca = a.retrieveB();
371             assertEquals("Relations size for as1 ", 0, ca.size());
372             BRemote b = bhome.findByPrimaryKey("bs1_1");
373             assertNull("Bad relation for bs1_1", b.retrieveA());
374             // undo
375
a.addInB("bs1_1");
376         }
377         checkIsInitialState();
378     }
379
380   
381
382     /**
383      * Set a A2B relation to empty by clearing the collection.
384      * Before: as1 <-> bs1_1
385      * After: as1 bs1_1
386      * Check also the assignement rules for relationships.
387      */

388     public void tCohClearA2B(int tx) throws Exception JavaDoc {
389         if ((tx == TX_CALL) || (tx == TX_RB)) {
390             utx.begin();
391         }
392         ARemote a = ahome.findByPrimaryKey("as1");
393         if (tx == TX_CONT) {
394             a.clearBInNewTx();
395         } else {
396             a.clearB();
397         }
398         if (tx == TX_CALL) {
399             utx.commit();
400         } else if (tx == TX_RB) {
401             utx.rollback();
402         }
403         if (tx != TX_RB) {
404             // checking
405
Collection JavaDoc c = a.retrieveB();
406             BRemote b = bhome.findByPrimaryKey("bs1_1");
407             String JavaDoc r = b.retrieveA();
408             assertEquals("Expected empty collection for as1 relation: ", 0, c.size());
409             assertNull("Expected no relation for bs1_1", r);
410             // undo
411
b.assignA("as1");
412         }
413         checkIsInitialState();
414     }
415
416    
417     /**
418      * Set an empty A2B relation with 1 element.
419      * This added element was'nt already in an other relation.
420      * Before: as0 b0
421      * After: as0 <-> b0
422      * Check also the assignement rules for relationships.
423      */

424     public void tCohSetOneA2B(int tx) throws Exception JavaDoc {
425         ArrayList JavaDoc ce = new ArrayList JavaDoc();
426         ce.add("b0");
427         if ((tx == TX_CALL) || (tx == TX_RB)) {
428             utx.begin();
429         }
430         ARemote a = ahome.findByPrimaryKey("as0");
431         if (tx == TX_CONT) {
432             a.assignBInNewTx(ce);
433         } else {
434             a.assignB(ce);
435         }
436         if (tx == TX_CALL) {
437             utx.commit();
438         } else if (tx == TX_RB) {
439             utx.rollback();
440         }
441         if (tx != TX_RB) {
442             // checking
443
Collection JavaDoc ca = a.retrieveB();
444             BRemote b = bhome.findByPrimaryKey("b0");
445             String JavaDoc r = b.retrieveA();
446             assertTrue("Bad relations for as0 (required:" + ce + ", found:" + ca
447                        + ")", isCollectionEqual(ce, ca));
448             assertEquals("Bad relation for b0: ", "as0", r);
449             // undo
450
a.assignB(new ArrayList JavaDoc());
451         }
452         checkIsInitialState();
453     }
454
455    
456
457
458     /**
459      * Set a null B2A relation with 1 element.
460      * This element was'nt already in an other relation.
461      * Before: b0 as0
462      * After: b0 <-> as0
463      * Check also the assignement rules for relationships.
464      */

465     public void tCohSet1B2A(int tx) throws Exception JavaDoc {
466         if ((tx == TX_CALL) || (tx == TX_RB)) {
467             utx.begin();
468         }
469         BRemote b = bhome.findByPrimaryKey("b0");
470         if (tx == TX_CONT) {
471             b.assignAInNewTx("as0");
472         } else {
473             b.assignA("as0");
474         }
475         if (tx == TX_CALL) {
476             utx.commit();
477         } else if (tx == TX_RB) {
478             utx.rollback();
479         }
480         // checking
481
String JavaDoc r = b.retrieveA();
482         ARemote a = ahome.findByPrimaryKey("as0");
483         Collection JavaDoc ca = a.retrieveB();
484         ArrayList JavaDoc ce = new ArrayList JavaDoc();
485         ce.add("b0");
486         if (tx != TX_RB) {
487             assertEquals("Bad relation for b0: ", "as0", r);
488             assertTrue("Bad relations for as0 (required:" + ce + ", found:" + ca
489                        + ")", isCollectionEqual(ce, ca));
490             // undo
491
b.assignA(null);
492         }
493         checkIsInitialState();
494     }
495
496  
497
498
499     /**
500      * Add an element to a empty relation.
501      * Same as tCohSetOneA2B except that we add the element in the collection
502      * instead of we set directly the collection.
503      * Before: as0 bs0
504      * After: as0 <-> bs0
505      * Check also the assignement rules for relationships.
506      */

507     public void tCohAddOneA2B(int tx) throws Exception JavaDoc {
508         ArrayList JavaDoc ce = new ArrayList JavaDoc();
509         ce.add("bs0");
510         if ((tx == TX_CALL) || (tx == TX_RB)) {
511             utx.begin();
512         }
513         ARemote a = ahome.findByPrimaryKey("as0");
514         if (tx == TX_CONT) {
515             a.addInBInNewTx("bs0");
516         } else {
517             a.addInB("bs0");
518         }
519         if (tx == TX_CALL) {
520             utx.commit();
521         } else if (tx == TX_RB) {
522             utx.rollback();
523         }
524         if (tx != TX_RB) {
525             // checking
526
Collection JavaDoc ca = a.retrieveB();
527             BRemote b = bhome.findByPrimaryKey("bs0");
528             String JavaDoc r = b.retrieveA();
529             assertTrue("Bad relations for as0 (required:" + ce + ", found:" + ca
530                        + ")", isCollectionEqual(ce, ca));
531             assertEquals("Bad relation for bs0", "as0", r);
532             // undo
533
a.assignB(new ArrayList JavaDoc());
534         }
535         checkIsInitialState();
536     }
537
538  
539     /**
540      * Re-Set a relation with N element.
541      * Before: as1 <-> bs1_1
542      * bs0
543      * bs3
544      * After: bs1_1
545      * as1 <-> bs0
546      * as1 <-> bs3
547      */

548     public void tCohSetMultiA2B(int tx) throws Exception JavaDoc {
549         ArrayList JavaDoc ce = new ArrayList JavaDoc(2);
550         ce.add("bs0");
551         ce.add("bs3");
552         if ((tx == TX_CALL) || (tx == TX_RB)) {
553             utx.begin();
554         }
555         ARemote a = ahome.findByPrimaryKey("as1");
556         Collection JavaDoc co = a.retrieveB();
557         if (tx == TX_CONT) {
558             a.assignBInNewTx(ce);
559         } else {
560             a.assignB(ce);
561         }
562         if (tx == TX_CALL) {
563             utx.commit();
564         } else if (tx == TX_RB) {
565             utx.rollback();
566         }
567         if (tx != TX_RB) {
568             // checking
569
Collection JavaDoc ca = a.retrieveB();
570             BRemote bs1_1 = bhome.findByPrimaryKey("bs1_1");
571             BRemote bs0 = bhome.findByPrimaryKey("bs0");
572             BRemote bs3 = bhome.findByPrimaryKey("bs3");
573             assertTrue("Bad relations for as1 (required:" + ce + ", found:" + ca
574                        + ")", isCollectionEqual(ce, ca));
575             assertNull("Not null relation for bs1_1", bs1_1.retrieveA());
576             assertEquals("Bad relation for bs0: ", "as1", bs0.retrieveA());
577             assertEquals("Bad relation for bs3: ", "as1", bs3.retrieveA());
578             // undo
579
a.assignB(co);
580         }
581         checkIsInitialState();
582     }
583
584    
585
586
587     /**
588      * Set a B2A relation with 1 element.
589      * This element wasn't already in an other relation.
590      * Before: bs1_1 <-> as1
591      * as0
592      * Change: bs1_1.assignA(as0);
593      * After: bs1_1 <-> as0
594      * as1
595      * Check also the assignement rules for relationships.
596      *
597      */

598     public void tCohSet2B2A(int tx) throws Exception JavaDoc {
599         if ((tx == TX_CALL) || (tx == TX_RB)) {
600             utx.begin();
601         }
602         BRemote b = bhome.findByPrimaryKey("bs1_1");
603         if (tx == TX_CONT) {
604             b.assignAInNewTx("as0");
605         } else {
606             b.assignA("as0");
607         }
608         if (tx == TX_CALL) {
609             utx.commit();
610         } else if (tx == TX_RB) {
611             utx.rollback();
612         }
613         if (tx != TX_RB) {
614             // checking
615
String JavaDoc r = b.retrieveA();
616             assertEquals("Bad relation for bs1_1: ", "as0", r);
617             ARemote as0 = ahome.findByPrimaryKey("as0");
618             Collection JavaDoc cas0 = as0.retrieveB();
619             ARemote as1 = ahome.findByPrimaryKey("as1");
620             Collection JavaDoc cas1 = as1.retrieveB();
621             ArrayList JavaDoc ce = new ArrayList JavaDoc();
622             ce.add("bs1_1");
623             assertTrue("Bad relations for as0 (required:" + ce + ", found:" + cas0
624                        + ")", isCollectionEqual(ce, cas0));
625             ce = new ArrayList JavaDoc();
626             assertTrue("Bad relations for as1 (required:" + ce + ", found:" + cas1
627                        + ")", isCollectionEqual(ce, cas1));
628             // undo
629
b.assignA("as1");
630         }
631         checkIsInitialState();
632     }
633
634    
635
636
637     /**
638      * Set a B2A relation with 1 element.
639      * This element was already in an other relation.
640      * Before: bs1_1 <-> as1
641      * b3_1 <-> a3
642      * Change: bs1_1.assignA(b3_1.retrieveA())
643      * After: bs1_1 <-> a3
644      * b3_1 <->
645      * as1
646      * Check also the assignement rules for relationships.
647      *
648      * See Spec chapter 10.3.7.3, change example: b2m.setA(b1n.getA())
649      */

650     public void tCohSet3B2A(int tx) throws Exception JavaDoc {
651         if ((tx == TX_CALL) || (tx == TX_RB)) {
652             utx.begin();
653         }
654         BRemote bs1_1 = bhome.findByPrimaryKey("bs1_1");
655         if (tx == TX_CONT) {
656             bs1_1.assignAInNewTx("a3");
657         } else {
658             bs1_1.assignA("a3");
659         }
660         if (tx == TX_CALL) {
661             utx.commit();
662         } else if (tx == TX_RB) {
663             utx.rollback();
664         }
665         if (tx != TX_RB) {
666             // checking
667
String JavaDoc rbs1_1 = bs1_1.retrieveA();
668             ARemote as1 = ahome.findByPrimaryKey("as1");
669             Collection JavaDoc cas1 = as1.retrieveB();
670             ARemote a3 = ahome.findByPrimaryKey("a3");
671             Collection JavaDoc ca3 = a3.retrieveB();
672             assertEquals("Bad relation for bs1_1: ", "a3", rbs1_1);
673             ArrayList JavaDoc ce = new ArrayList JavaDoc();
674             ce.add("bs1_1");
675             ce.add("b3_1");
676             assertTrue("Bad relations for a3 (required:" + ce + ", found:" + ca3
677                        + ")", isCollectionEqual(ce, ca3));
678             ce = new ArrayList JavaDoc();
679             assertTrue("Bad relations for as1 (required:" + ce + ", found:" + cas1
680                        + ")", isCollectionEqual(ce, cas1));
681             // undo
682
bs1_1.assignA("as1");
683         }
684         checkIsInitialState();
685     }
686
687
688
689     /**
690      * Remove an element in a relation.
691      * Before: a3 <-> b3_1
692      * Change: a3.retrieveB().remove(b3_1)
693      * After: a3 b3_1
694      * See Spec chapter 10.3.7.3, change example: a1.getB().remove(b1n)
695      */

696     public void tCohRemoveInRelA2B(int tx) throws Exception JavaDoc {
697         if ((tx == TX_CALL) || (tx == TX_RB)) {
698             utx.begin();
699         }
700         ARemote a = ahome.findByPrimaryKey("a3");
701         if (tx == TX_CONT) {
702             a.removeFromBInNewTx("b3_1");
703         } else {
704             a.removeFromB("b3_1");
705         }
706         if (tx == TX_CALL) {
707             utx.commit();
708         } else if (tx == TX_RB) {
709             utx.rollback();
710         }
711         if (tx != TX_RB) {
712             // checking
713
Collection JavaDoc ca = a.retrieveB();
714             BRemote b = bhome.findByPrimaryKey("b3_1");
715             String JavaDoc r = b.retrieveA();
716             ArrayList JavaDoc ce = new ArrayList JavaDoc();
717             assertTrue("Bad relations for a3: (required:" + ce + ", found:" + ca
718                        + ")", isCollectionEqual(ce, ca));
719             assertNull("Bad not null relation for b3_1", r);
720             // undo
721
a.addInB("b3_1");
722         }
723         // check to initial state
724
checkIsInitialState();
725
726     }
727
728   
729
730     /**
731      * Remove a bean B which is in a relation.
732      * Before: a3 <-> b3_1
733      * change: B.remove(b3_1)
734      * After: a3
735      *
736      */

737     public void tCohRemoveB(int tx) throws Exception JavaDoc {
738         if (tx == TX_CONT) {
739             // The transaction attribute of the remove method is TX_SUPPORT,
740
// so the transaction cannot be initiate by the container
741
fail("Transaction cannot be initiate by the container for this test");
742         }
743         if ((tx == TX_CALL) || (tx == TX_RB)) {
744             utx.begin();
745         }
746         bhome.remove("b3_1");
747         if (tx == TX_CALL) {
748             utx.commit();
749         } else if (tx == TX_RB) {
750             utx.rollback();
751         }
752         if (tx != TX_RB) {
753             // checking
754
ARemote a = ahome.findByPrimaryKey("a3");
755             Collection JavaDoc ca = a.retrieveB();
756             assertEquals("Wrong relations size for a3: (expected: 0 found:"
757                          + ca.size(), 0, ca.size());
758             // undo
759
bhome.create("b3_1");
760             a.addInBInNewTx("b3_1");
761         }
762         checkIsInitialState();
763     }
764
765     
766
767     /**
768      * Remove a bean B which is in a relation.
769      * Same as tCohRemoveB except that the called remove method is on the bean
770      * instead of the home.
771      * Before: a3 <-> b3_1
772      * change: B.remove(b3_1)
773      * After: a3
774      *
775      */

776     public void tCohBeanRemoveB(int tx) throws Exception JavaDoc {
777         if (tx == TX_CONT) {
778             // The transaction attribute of the remove method is TX_SUPPORT,
779
// so the transaction cannot be initiate by the container
780
fail("Transaction cannot be initiate by the container for this test");
781         }
782         if ((tx == TX_CALL) || (tx == TX_RB)) {
783             utx.begin();
784         }
785         BRemote b = bhome.findByPrimaryKey("b3_1");
786         b.remove();
787         if (tx == TX_CALL) {
788             utx.commit();
789         } else if (tx == TX_RB) {
790             utx.rollback();
791         }
792         if (tx != TX_RB) {
793             // checking
794
ARemote a = ahome.findByPrimaryKey("a3");
795             Collection JavaDoc ca = a.retrieveB();
796             assertEquals("Wrong relations size for a3: (expected: 0 found:"
797                          + ca.size(), 0, ca.size());
798             // undo
799
bhome.create("b3_1");
800             a.addInBInNewTx("b3_1");
801         }
802         checkIsInitialState();
803     }
804
805   
806
807
808
809     /**
810      * Set a empty relation A2B with 1 element.
811      * This added element was already in a other relation.
812      * Before: a0
813      * a3 <-> b3_1
814      * Change: a0.assignB({b3_1})
815      * After: a0 <-> b3_1
816      * a3
817      */

818     public void tCohSetOne2A2B(int tx) throws Exception JavaDoc {
819         ArrayList JavaDoc ce = new ArrayList JavaDoc();
820         ce.add("b3_1");
821         if ((tx == TX_CALL) || (tx == TX_RB)) {
822             utx.begin();
823         }
824         ARemote a = ahome.findByPrimaryKey("a0");
825         if (tx == TX_CONT) {
826             a.assignBInNewTx(ce);
827         } else {
828             a.assignB(ce);
829         }
830         if (tx == TX_CALL) {
831             utx.commit();
832         } else if (tx == TX_RB) {
833             utx.rollback();
834         }
835         if (tx != TX_RB) {
836             // checking
837
Collection JavaDoc ca = a.retrieveB();
838             ARemote a3 = ahome.findByPrimaryKey("a3");
839             Collection JavaDoc ca3 = a3.retrieveB();
840             BRemote b = bhome.findByPrimaryKey("b3_1");
841             String JavaDoc r = b.retrieveA();
842             assertTrue("Wrong relations a0 (required:" + ce + ", found:" + ca
843                        + ")", isCollectionEqual(ce, ca));
844             assertTrue("Wrong relations for b3_1" + ": found:" + ca3 ,
845                        !ca3.contains("b3_1"));
846             assertEquals("Bad relation for b3_1: ", "a0", r);
847             // undo
848
a.assignB(new ArrayList JavaDoc());
849             a3.addInB("b3_1");
850         }
851         checkIsInitialState();
852     }
853
854    
855
856
857     /**
858      * Add an element to a relation A2B already multiple.
859      * The element was already in a relation.
860      * Check also the assignement rules for relationships.
861      * Before: a1 <-> b1_1
862      * <-> b1_2
863      * <-> b1_3
864      * a3 <-> b3_1
865      * Change: a1.addInB(b3_1)
866      * After: a1 <-> b1_1
867      * <-> b1_2
868      * <-> b1_3
869      * <-> b3_1
870      * a3
871      *
872      * See Spec chapter 10.3.7.3, change example: a1.getB().add(b2m)
873      *
874      */

875     public void tCohAddOneInMultiA2B(int tx) throws Exception JavaDoc {
876
877         ArrayList JavaDoc ce = new ArrayList JavaDoc();
878         ce.add("b1_1");
879         ce.add("b1_2");
880         ce.add("b1_3");
881         String JavaDoc bAdded = "b3_1";
882         if ((tx == TX_CALL) || (tx == TX_RB)) {
883             utx.begin();
884         }
885         ARemote a = ahome.findByPrimaryKey("a1");
886         if (tx == TX_CONT) {
887             a.addInBInNewTx(bAdded);
888         } else {
889             a.addInB(bAdded);
890         }
891         ce.add(bAdded);
892         if (tx == TX_CALL) {
893             utx.commit();
894         } else if (tx == TX_RB) {
895             utx.rollback();
896         }
897         if (tx != TX_RB) {
898             // checking
899
Collection JavaDoc ca1 = a.retrieveB();
900             ARemote a3 = ahome.findByPrimaryKey("a3");
901             Collection JavaDoc ca3 = a3.retrieveB();
902             assertTrue("Bad relations for a1 (required:" + ce + ", found:" + ca1 + ")",
903                        isCollectionEqual(ce, ca1));
904             ce = new ArrayList JavaDoc();
905             assertTrue("Bad relations for a3 (required:" + ce + ", found:" + ca3 + ")",
906                        isCollectionEqual(ce, ca3));
907             BRemote b = bhome.findByPrimaryKey(bAdded);
908             assertEquals("Bad relation for b3_1: ", "a1", b.retrieveA());
909             // undo
910
a3.addInB(bAdded);
911         }
912
913         checkIsInitialState();
914     }
915
916    
917     /*
918      * Set a relation to multiple.
919      * All of element was already in an other relation.
920      * Check also the assignement rules for relationships.
921      * Before: a1 <-> b1_1
922      * <-> b1_2
923      * <-> b1_3
924      * a2 <-> b2_1
925      * <-> b2_2
926      * <-> b2_3
927      * Change: a1.assignB(a2.retrieveB())
928      * After: a1 <-> b2_1
929      * <-> b2_2
930      * <-> b2_3
931      * a2
932      * b1_1
933      * b1_2
934      * b1_3
935      *
936      * Spec chapter 10.3.7.3, change example: a1.setB(a2.getB())
937      *
938      */

939     public void tCohSetMultiBisA2B(int tx) throws Exception JavaDoc {
940         if ((tx == TX_CALL) || (tx == TX_RB)) {
941             utx.begin();
942         }
943         ARemote a1 = ahome.findByPrimaryKey("a1");
944         Collection JavaDoc co1 = a1.retrieveB();
945         ARemote a2 = ahome.findByPrimaryKey("a2");
946         Collection JavaDoc co2 = a2.retrieveB();
947         if (tx == TX_CONT) {
948             a1.assignBInNewTx(co2);
949         } else {
950             a1.assignB(co2);
951         }
952         if (tx == TX_CALL) {
953             utx.commit();
954         } else if (tx == TX_RB) {
955             utx.rollback();
956         }
957         if (tx != TX_RB) {
958             // checking
959
Collection JavaDoc ca1 = a1.retrieveB();
960             assertTrue("Wrong relations for a1 (required:" + co2 + ", found:" + ca1 + ")", isCollectionEqual(co2, ca1));
961             Collection JavaDoc ca2 = a2.retrieveB();
962             assertEquals("Wrong relations size for a2: ", 0, ca2.size());
963             BRemote b;
964             b = bhome.findByPrimaryKey("b1_1");
965             assertNull("Wrong not null relation for b1_1: ", b.retrieveA());
966             b = bhome.findByPrimaryKey("b1_2");
967             assertNull("Wrong not null relation for b1_2: ", b.retrieveA());
968             b = bhome.findByPrimaryKey("b1_3");
969             assertNull("Wrong not null relation for b1_3: ", b.retrieveA());
970             b = bhome.findByPrimaryKey("b2_1");
971             assertEquals("Wrong relation for b2_1: ", "a1", b.retrieveA());
972             b = bhome.findByPrimaryKey("b2_2");
973             assertEquals("Wrong relation for b2_2: ", "a1", b.retrieveA());
974             b = bhome.findByPrimaryKey("b2_3");
975             assertEquals("Wrong relation for b2_3: ", "a1", b.retrieveA());
976             // Undo
977
a1.assignB(co1);
978             a2.assignB(co2);
979         }
980         checkIsInitialState();
981     }
982
983     
984
985  
986
987     public static Test suite() {
988         return new TestSuite(G_Relation_ombEC2.class);
989     }
990
991     public static void main(String JavaDoc args[]) {
992         String JavaDoc testtorun = null;
993         // Get args
994
for (int argn = 0; argn < args.length; argn++) {
995             String JavaDoc s_arg = args[argn];
996             Integer JavaDoc i_arg;
997             if (s_arg.equals("-n")) {
998                 testtorun = args[++argn];
999             }
1000        }
1001        if (testtorun == null) {
1002            junit.textui.TestRunner.run(suite());
1003        } else {
1004            junit.textui.TestRunner.run(new G_Relation_ombEC2(testtorun));
1005        }
1006    }
1007
1008}
1009
Popular Tags