KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > clients > entity > G_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: G_Relation_omuEC2.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.omu.AHomeRemote;
40 import org.objectweb.jonas.jtests.beans.relation.omu.ARemote;
41 import org.objectweb.jonas.jtests.beans.relation.omu.BHomeRemote;
42
43
44 /**
45  * For testing one-to-many unidirectional relationships
46  * @author S.Chassande-Barrioz, Helene Joanin
47  */

48
49 public class G_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 G_Relation_omuEC2(String JavaDoc name) {
80         super(name);
81         // debug("F_Relation_omuEC2("+name+")");
82
}
83   
84   
85     public void testEmpty() throws Exception JavaDoc {
86     }
87     protected static boolean isInit = false;
88
89     protected void setUp() {
90         super.setUp();
91         boolean ok = false;
92         int nbtry = 0;
93         while (!ok && nbtry < 3) {
94             if (!isInit) {
95                 // load bean if not loaded yet
96
useBeans("omu", false);
97                 try {
98                     ahome = (AHomeRemote) PortableRemoteObject.narrow(ictx.lookup(BEAN_HOME_A),
99                                                                       AHomeRemote.class);
100                     bhome = (BHomeRemote) PortableRemoteObject.narrow(ictx.lookup(BEAN_HOME_B),
101                                                                       BHomeRemote.class);
102                 } catch (NamingException JavaDoc e) {
103                     fail("Cannot get bean home: " + e.getMessage());
104                 }
105                 // check if tables have been initialized
106
try {
107                     ahome.findByPrimaryKey("a0");
108                 } catch (Exception JavaDoc e) {
109                     try {
110                         utx.begin();
111                         bhome.create("bs0");
112                         bhome.create("bs1_1");
113                         bhome.create("bs2_1");
114                         bhome.create("bs2_2");
115                         bhome.create("bs2_3");
116                         bhome.create("bs3");
117                         bhome.create("b0");
118                         bhome.create("b1_1");
119                         bhome.create("b1_2");
120                         bhome.create("b1_3");
121                         bhome.create("b2_1");
122                         bhome.create("b2_2");
123                         bhome.create("b2_3");
124                         bhome.create("b3_1");
125                         ARemote as0 = ahome.create("as0");
126                         ARemote as1 = ahome.create("as1");
127                         ARemote as2 = ahome.create("as2");
128                         ARemote a0 = ahome.create("a0");
129                         ARemote a1 = ahome.create("a1");
130                         ARemote a2 = ahome.create("a2");
131                         ARemote a3 = ahome.create("a3");
132                         as1.assignB((Collection JavaDoc)tbRelationA2B.get("as1"));
133                         as2.assignB((Collection JavaDoc)tbRelationA2B.get("as2"));
134                         a1.assignB((Collection JavaDoc)tbRelationA2B.get("a1"));
135                         a2.assignB((Collection JavaDoc)tbRelationA2B.get("a2"));
136                         a3.assignB((Collection JavaDoc)tbRelationA2B.get("a3"));
137                     } catch (Exception JavaDoc i) {
138                         fail("InitialState creation problem:: "+i);
139                     } finally {
140                         try {
141                             utx.commit();
142                         } catch (Exception JavaDoc ii) {
143                         }
144                     }
145                 }
146                 isInit = true;
147             }
148             // Check that all is OK. Sometimes, a test has failed and has corrupted
149
// the bean state in the database. We must unload and reload the bean then.
150
nbtry++;
151             try {
152                 if (initStateOK()) {
153                     ok = true;
154                 }
155             } catch (Exception JavaDoc e) {
156             }
157             if (!ok) {
158                 isInit = false;
159                 unloadBeans("omu");
160             }
161         }
162     }
163
164     /*
165      * Check that we are in the same state as after the tables creation for thoses beans A and B
166      * (ie if it is the initial state)
167      */

168     boolean initStateOK() throws Exception JavaDoc {
169         boolean isOk = true;
170         msgerror = new StringBuffer JavaDoc();
171         for (Enumeration JavaDoc ea = tbRelationA2B.keys(); ea.hasMoreElements();) {
172             String JavaDoc aname = (String JavaDoc) (ea.nextElement());
173             ARemote a = ahome.findByPrimaryKey(aname);
174             Collection JavaDoc colActual = a.retrieveB();
175             ArrayList JavaDoc colExpected = (ArrayList JavaDoc) (tbRelationA2B.get(aname));
176             debug("Relation for " + aname + " :: expected:" + colExpected + ", found:" + colActual );
177             if (!isCollectionEqual(colExpected, colActual)) {
178                 isOk = false;
179                 msgerror = msgerror.append("Wrong relation for " + aname
180                                            + " (expected:" + colExpected
181                                            + ", found:" + colActual + ")");
182             }
183         }
184         return isOk;
185     }
186
187
188
189     /**
190      * Check that the bean 'as0' has no relation.
191      */

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

226     public void _testBasicGetOne(int tx) throws Exception JavaDoc {
227
228         Collection JavaDoc c = null;
229         if ((tx == TX_CALL) || (tx == TX_RB)) {
230             utx.begin();
231         }
232         ARemote a = ahome.findByPrimaryKey("as1");
233         if (tx == TX_CONT) {
234             c = a.retrieveBInNewTx();
235         } else {
236             c = a.retrieveB();
237         }
238         if (tx == TX_CALL) {
239             utx.commit();
240         } else if (tx == TX_RB) {
241             utx.rollback();
242         }
243         // checking
244
assertEquals("Wrong relations size for as1: ", 1, c.size());
245         String JavaDoc s = "bs1_1";
246         assertTrue("Relation missing " + s + " for as1: ", c.contains(s));
247         checkIsInitialState();
248     }
249
250   
251     /**
252      * Check that the bean 'as2' has many relations with 'bs2_1', 'bs2_2', 'bs2_3'.
253      */

254     public void _testBasicGetMany(int tx) throws Exception JavaDoc {
255
256         Collection JavaDoc c = null;
257         if ((tx == TX_CALL) || (tx == TX_RB)) {
258             utx.begin();
259         }
260         ARemote a = ahome.findByPrimaryKey("as2");
261         if (tx == TX_CONT) {
262             c = a.retrieveBInNewTx();
263         } else {
264             c = a.retrieveB();
265         }
266         if (tx == TX_CALL) {
267             utx.commit();
268         } else if (tx == TX_RB) {
269             utx.rollback();
270         }
271         // checking
272
assertEquals("Relations size of: ", 3, c.size());
273         String JavaDoc s = "bs2_1";
274         assertTrue("Relation missing " + s + ": ", c.contains(s));
275         s = "bs2_2";
276         assertTrue("Relation missing " + s + ": ", c.contains(s));
277         s = "bs2_3";
278         assertTrue("Relation missing " + s + ": ", c.contains(s));
279         checkIsInitialState();
280     }
281
282    
283  public void _testBasicSetEmpty(int tx) throws Exception JavaDoc {
284         ArrayList JavaDoc ce = new ArrayList JavaDoc();
285         if ((tx == TX_CALL) || (tx == TX_RB)) {
286             utx.begin();
287         }
288         ARemote a = ahome.findByPrimaryKey("as1");
289         if (tx == TX_CONT) {
290             a.assignBInNewTx(ce);
291         } else {
292             a.assignB(ce);
293         }
294         if (tx == TX_CALL) {
295             utx.commit();
296         } else if (tx == TX_RB) {
297             utx.rollback();
298         }
299         // checking
300
Collection JavaDoc ca = a.retrieveB();
301         if (tx != TX_RB) {
302             assertEquals("Relations size of: ", 0, ca.size());
303         } else {
304             assertEquals("Relations size of: ", 1, ca.size());
305         }
306         // undo
307
if (tx != TX_RB) {
308             a.addInB("bs1_1");
309             // Check undo
310
ca = a.retrieveB();
311             assertEquals("After undo, Relations size of: ", 1, ca.size());
312         }
313         checkIsInitialState();
314     }
315
316     public void testBasicSetEmptyTxNo() throws Exception JavaDoc {
317         _testBasicSetEmpty(TX_NO);
318     }
319
320     public void testBasicSetEmptyTxCall() throws Exception JavaDoc {
321         _testBasicSetEmpty(TX_CALL);
322     }
323
324     public void testBasicSetEmptyTxCont() throws Exception JavaDoc {
325         _testBasicSetEmpty(TX_CONT);
326     }
327
328
329    
330
331     public static Test suite() {
332         return new TestSuite(G_Relation_omuEC2.class);
333     }
334
335     public static void main(String JavaDoc args[]) {
336         String JavaDoc testtorun = null;
337         // Get args
338
for (int argn = 0; argn < args.length; argn++) {
339             String JavaDoc s_arg = args[argn];
340             Integer JavaDoc i_arg;
341             if (s_arg.equals("-n")) {
342                 testtorun = args[++argn];
343             }
344         }
345         if (testtorun == null) {
346             junit.textui.TestRunner.run(suite());
347         } else {
348             junit.textui.TestRunner.run(new G_Relation_omuEC2(testtorun));
349         }
350     }
351
352 }
353
Popular Tags