KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > clients > entity > G_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: G_Relation_mnbEC2.java,v 1.2 2004/03/19 11:57:15 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 G_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 G_Relation_mnbEC2(String JavaDoc name) {
99         super(name);
100     }
101   
102   
103     public void testEmpty() throws Exception JavaDoc {
104     }
105     protected static boolean isInit = false;
106
107     protected void setUp() {
108         super.setUp();
109         boolean ok = false;
110         int nbtry = 0;
111         while (!ok && nbtry < 3) {
112             if (!isInit) {
113                 // load bean if not loaded yet
114
useBeans("mnb", false);
115                 // lookup home used in the tests
116
try {
117                     ahome = (AHomeRemote) PortableRemoteObject.narrow(ictx.lookup(BEAN_HOME_A),
118                                                                       AHomeRemote.class);
119                     bhome = (BHomeRemote) PortableRemoteObject.narrow(ictx.lookup(BEAN_HOME_B),
120                                                                       BHomeRemote.class);
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                         // IMPORTANT: comments begin/commit to highlight a bug
130
utx.begin();
131                         ahome.create("a0");
132                         ahome.create("a1");
133                         ahome.create("a2");
134                         ahome.create("a3");
135                         bhome.create("b0");
136                         bhome.create("b1");
137                         bhome.create("b2");
138                         bhome.create("b3");
139                         bhome.create("b4");
140                         ARemote a0 = ahome.findByPrimaryKey("a0");
141                         ARemote a1 = ahome.findByPrimaryKey("a1");
142                         ARemote a2 = ahome.findByPrimaryKey("a2");
143                         ARemote a3 = ahome.findByPrimaryKey("a3");
144                         Collection JavaDoc rel_a0 = (Collection JavaDoc) (tbRelationA2B.get("a0"));
145                         a0.assignB(rel_a0);
146                         Collection JavaDoc rel_a1 = (Collection JavaDoc) (tbRelationA2B.get("a1"));
147                         a1.assignB(rel_a1);
148                         Collection JavaDoc rel_a2 = (Collection JavaDoc) (tbRelationA2B.get("a2"));
149                         a2.assignB(rel_a2);
150                         Collection JavaDoc rel_a3 = (Collection JavaDoc) (tbRelationA2B.get("a3"));
151                         a3.assignB(rel_a3);
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                 debug("F_Relation_mnbEC2 setUp: unload bean and retry");
174                 isInit = false;
175                 unloadBeans("mnb");
176             }
177         }
178     }
179
180     /*
181      * Check that we are in the same state as after the tables creation for thoses beans A and B
182      * (ie if it is the initial state)
183      */

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

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

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

300     public void _testBasicClear(int tx) throws Exception JavaDoc {
301         if ((tx == TX_CALL) || (tx == TX_RB)) {
302             utx.begin();
303         }
304         ARemote a2 = ahome.findByPrimaryKey("a2");
305         if (tx == TX_CONT) {
306             a2.clearBInNewTx();
307         } else {
308             a2.clearB();
309         }
310         if (tx == TX_CALL) {
311             utx.commit();
312         } else if (tx == TX_RB) {
313             utx.rollback();
314         }
315         // checking
316
Collection JavaDoc a2_value = a2.retrieveB();
317         BRemote b1 = bhome.findByPrimaryKey("b1");
318         BRemote b2 = bhome.findByPrimaryKey("b2");
319         BRemote b3 = bhome.findByPrimaryKey("b3");
320         ArrayList JavaDoc collb1 = new ArrayList JavaDoc(1);
321         collb1.add("a1");
322         ArrayList JavaDoc collb2 = new ArrayList JavaDoc(2);
323         collb2.add("a1");
324         collb2.add("a3");
325         ArrayList JavaDoc collb3 = new ArrayList JavaDoc(1);
326         collb3.add("a3");
327         Collection JavaDoc col=null;
328         if (tx != TX_RB) {
329             assertTrue("Wrong relations a2 (required: Empty "+", found:" + a2_value
330                        + ")", a2_value.isEmpty());
331             col = bhome.findByPrimaryKey("b1").retrieveA();
332             assertTrue("Wrong collection returned by b1.getA(): expected:" + collb1
333                        + " found: " + col, isCollectionEqual(collb1, col));
334             col = bhome.findByPrimaryKey("b2").retrieveA();
335             assertTrue("Wrong collection returned by b2.getA(): expected:" + collb2
336                        + " found: " + col, isCollectionEqual(collb2, col));
337             col = bhome.findByPrimaryKey("b3").retrieveA();
338             assertTrue("Wrong collection returned by b3.getA(): expected:" + collb3
339                        + " found: " + col, isCollectionEqual(collb3, col));
340             // undo
341
ArrayList JavaDoc a2undo = new ArrayList JavaDoc(3);
342             a2undo.add("b1");
343             a2undo.add("b2");
344             a2undo.add("b3");
345             a2.assignB(a2undo);
346         }
347         checkIsInitialState();
348     }
349
350   
351  
352
353     public static Test suite() {
354         return new TestSuite(G_Relation_mnbEC2.class);
355     }
356
357     public static void main(String JavaDoc args[]) {
358         String JavaDoc testtorun = null;
359         // Get args
360
for (int argn = 0; argn < args.length; argn++) {
361             String JavaDoc s_arg = args[argn];
362             Integer JavaDoc i_arg;
363             if (s_arg.equals("-n")) {
364                 testtorun = args[++argn];
365             }
366         }
367         if (testtorun == null) {
368             junit.textui.TestRunner.run(suite());
369         } else {
370             junit.textui.TestRunner.run(new G_Relation_mnbEC2(testtorun));
371         }
372     }
373
374 }
375
Popular Tags