KickJava   Java API By Example, From Geeks To Geeks.

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


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_mnuEC2.java,v 1.3 2004/05/20 23:28:50 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.mnu.AHomeRemote;
40 import org.objectweb.jonas.jtests.beans.relation.mnu.ARemote;
41 import org.objectweb.jonas.jtests.beans.relation.mnu.BHomeRemote;
42 import org.objectweb.jonas.jtests.beans.relation.mnu.BRemote;
43
44 /**
45  * For testing many-to-many unidirectional relationships
46  * @author S.Chassande-Barrioz, J. Camilleri
47  *
48  **/

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

150     boolean initStateOK() throws Exception JavaDoc {
151         boolean isOk = true;
152         msgerror = new StringBuffer JavaDoc();
153         for (Enumeration JavaDoc ea = tbRelationA2B.keys(); ea.hasMoreElements();) {
154             String JavaDoc aname = (String JavaDoc) (ea.nextElement());
155             ARemote a = ahome.findByPrimaryKey(aname);
156             Collection JavaDoc colActual = a.retrieveB();
157             ArrayList JavaDoc colExpected = (ArrayList JavaDoc) (tbRelationA2B.get(aname));
158             if (!isCollectionEqual(colExpected, colActual)) {
159                 isOk = false;
160                 msgerror = msgerror.append("Wrong relation for " + aname
161                                            +" (expected:" + colExpected
162                                            + ", found:" + colActual + ")");
163             }
164         }
165         return isOk;
166     }
167
168
169     /**
170      * Check that the bean 'a3' has no relation.
171      */

172     public void _testBasicGetEmpty(int tx) throws Exception JavaDoc {
173         Collection JavaDoc c;
174         if ((tx == TX_CALL) || (tx == TX_RB)) {
175             utx.begin();
176         }
177         ARemote a = ahome.findByPrimaryKey("a0");
178         if (tx == TX_CONT) {
179             c = a.retrieveBInNewTx();
180         } else {
181             c = a.retrieveB();
182         }
183         if (tx == TX_CALL) {
184             utx.commit();
185         } else if (tx == TX_RB) {
186             utx.rollback();
187         }
188
189         checkIsInitialState();
190     }
191     public void testBasicGetEmptyTxNo() throws Exception JavaDoc {
192         _testBasicGetEmpty(TX_NO);
193     }
194     public void testBasicGetEmptyTxCall() throws Exception JavaDoc {
195         _testBasicGetEmpty(TX_CALL);
196     }
197     public void testBasicGetEmptyTxCont() throws Exception JavaDoc {
198         _testBasicGetEmpty(TX_CONT);
199     }
200    
201
202     /**
203      * Ckeck the new relation a0-b0
204      */

205     public void _testBasicSetEmpty(int tx) throws Exception JavaDoc {
206         ArrayList JavaDoc c = new ArrayList JavaDoc(1);
207         c.add("b0");
208         if ((tx == TX_CALL) || (tx == TX_RB)) {
209             utx.begin();
210         }
211         ARemote a = ahome.findByPrimaryKey("a0");
212         if (tx == TX_CONT) {
213             a.assignBInNewTx(c);
214         } else {
215             a.assignB(c);
216         }
217         if (tx == TX_CALL) {
218             utx.commit();
219         } else if (tx == TX_RB) {
220             utx.rollback();
221         }
222         // checking
223
Collection JavaDoc c_value = a.retrieveB();
224         if (tx != TX_RB) {
225             assertTrue("Wrong relations a0 (required:" + c + ", found:" + c_value
226                        + ")", isCollectionEqual(c_value, c));
227             // undo
228
a.assignB(new ArrayList JavaDoc(0));
229         }
230         checkIsInitialState();
231
232     }
233     public void testBasicSetEmptyTxNo() throws Exception JavaDoc {
234         _testBasicSetEmpty(TX_NO);
235     }
236     public void testBasicSetEmptyTxCall() throws Exception JavaDoc {
237         _testBasicSetEmpty(TX_CALL);
238     }
239     public void testBasicSetEmptyTxCont() throws Exception JavaDoc {
240         _testBasicSetEmpty(TX_CONT);
241     }
242     /**
243      * See 10.3.7.7 of spec : a1.assignB(a3.retrieveB()) =>
244      * a1.retreiveB().contains("b2") &&
245      * a1.retreiveB().contains("b3") &&
246      * a1.retreiveB().contains("b4") &&
247      * a3.retreiveB().contains("b2") &&
248      * a3.retreiveB().contains("b3") &&
249      * a3.retreiveB().contains("b4") &&
250      */

251     public void _testBasicSetFirst(int tx) throws Exception JavaDoc {
252         String JavaDoc c = null;
253         if ((tx == TX_CALL) || (tx == TX_RB)) {
254             utx.begin();
255         }
256         ARemote a1 = ahome.findByPrimaryKey("a1");
257         ARemote a3 = ahome.findByPrimaryKey("a3");
258         Collection JavaDoc c1 = a1.retrieveB();
259         if (tx == TX_CONT) {
260             a1.assignBInNewTx(a3.retrieveB());
261         } else {
262             a1.assignB(a3.retrieveB());
263         }
264         if (tx == TX_CALL) {
265             utx.commit();
266         } else if (tx == TX_RB) {
267             utx.rollback();
268         }
269         // checking
270
if (tx != TX_RB) {
271             ArrayList JavaDoc c1_att = new ArrayList JavaDoc(3);
272             c1_att.add("b2");
273             c1_att.add("b3");
274             c1_att.add("b4");
275             assertTrue("Wrong test of coherence First before commit: ", isCollectionEqual(a1.retrieveB(),c1_att));
276         } else {
277             assertTrue("Wrong test of coherence First before roolback: ", isCollectionEqual(a1.retrieveB(),c1));
278         }
279         // undo
280
if (tx != TX_RB) {
281             a1.assignB(null);
282             a1.assignB(c1);
283         }
284         checkIsInitialState();
285     }
286    
287   
288
289     /**
290      * See 10.3.7.7 of spec : a1.getB().add(b3) =>
291      * a1.retreiveB().contains("b1") &&
292      * a1.retreiveB().contains("b2") &&
293      * a1.retreiveB().contains("b3") &&
294      */

295     public void _testBasicSetSecond(int tx) throws Exception JavaDoc {
296         String JavaDoc c = null;
297
298         if ((tx == TX_CALL) || (tx == TX_RB)) {
299             utx.begin();
300         }
301         ARemote a1 = ahome.findByPrimaryKey("a1");
302         Collection JavaDoc c1 = a1.retrieveB();
303   
304         if (tx == TX_CONT) {
305             a1.addInB("b3");
306         } else {
307             a1.addInB("b3");
308         }
309         if (tx == TX_CALL) {
310             utx.commit();
311         } else if (tx == TX_RB) {
312             utx.rollback();
313         }
314         // checking
315
if (tx != TX_RB) {
316             ArrayList JavaDoc c1_att = new ArrayList JavaDoc(3);
317             c1_att.add("b1");
318             c1_att.add("b2");
319             c1_att.add("b3");
320             assertTrue("Wrong test of coherence Second before commit: ", isCollectionEqual(a1.retrieveB(),c1_att));
321         } else {
322             assertTrue("Wrong test of coherence Second before roolback: ", isCollectionEqual(a1.retrieveB(),c1));
323         }
324         // undo
325
if (tx != TX_RB) {
326             a1.assignB(null);
327             a1.assignB(c1);
328         }
329         checkIsInitialState();
330     }
331
332  
333   
334
335
336     /**
337      * See 10.3.7.7 of spec : a2.getB().remove(b2) =>
338      * a2.retreiveB().contains("b1") &&
339      * a2.retreiveB().contains("b3") &&
340      */

341     public void _testBasicSetRemove(int tx) throws Exception JavaDoc {
342         String JavaDoc c = null;
343
344         if ((tx == TX_CALL) || (tx == TX_RB)) {
345             utx.begin();
346         }
347         ARemote a2 = ahome.findByPrimaryKey("a2");
348         Collection JavaDoc c2 = a2.retrieveB();
349         if (tx == TX_CONT) {
350             a2.removeFromB("b2");
351         } else {
352             a2.removeFromB("b2");
353         }
354         if (tx == TX_CALL) {
355             utx.commit();
356         } else if (tx == TX_RB) {
357             utx.rollback();
358         }
359         // checking
360
if (tx != TX_RB) {
361             ArrayList JavaDoc c2_att = new ArrayList JavaDoc(3);
362             c2_att.add("b1");
363             c2_att.add("b3");
364             assertTrue("Wrong test of coherence Remove before commit: ", isCollectionEqual(a2.retrieveB(),c2_att));
365         } else {
366             assertTrue("Wrong test of coherence Remove before roolback: ", isCollectionEqual(a2.retrieveB(),c2));
367         }
368         // undo
369
if (tx != TX_RB) {
370             a2.assignB(c2);
371         }
372         checkIsInitialState();
373     }
374
375   
376  
377     /**
378      * Ckeck clear function
379      */

380     public void _testBasicClear(int tx) throws Exception JavaDoc {
381         if ((tx == TX_CALL) || (tx == TX_RB)) {
382             utx.begin();
383         }
384         ARemote a2 = ahome.findByPrimaryKey("a2");
385         if (tx == TX_CONT) {
386             a2.clearBInNewTx();
387         } else {
388             a2.clearB();
389         }
390         if (tx == TX_CALL) {
391             utx.commit();
392         } else if (tx == TX_RB) {
393             utx.rollback();
394         }
395         // checking
396
Collection JavaDoc a2_value = a2.retrieveB();
397         BRemote b1 = bhome.findByPrimaryKey("b1");
398         BRemote b2 = bhome.findByPrimaryKey("b2");
399         BRemote b3 = bhome.findByPrimaryKey("b3");
400         ArrayList JavaDoc collb1 = new ArrayList JavaDoc(1);
401         collb1.add("a1");
402         ArrayList JavaDoc collb2 = new ArrayList JavaDoc(2);
403         collb2.add("a1");
404         collb2.add("a3");
405         ArrayList JavaDoc collb3 = new ArrayList JavaDoc(1);
406         collb3.add("a3");
407         Collection JavaDoc col=null;
408         if (tx != TX_RB) {
409             assertTrue("Wrong relations a2 (required: Empty "+", found:" + a2_value
410                        + ")", a2_value.isEmpty());
411        
412             // undo
413
ArrayList JavaDoc a2undo = new ArrayList JavaDoc(3);
414             a2undo.add("b1");
415             a2undo.add("b2");
416             a2undo.add("b3");
417             a2.assignB(a2undo);
418         }
419         checkIsInitialState();
420     }
421
422   
423    
424
425     /**
426      * Remove an element in a relation.
427      */

428     public void _testCohRemoveInRel(int tx) throws Exception JavaDoc {
429         String JavaDoc bRemoved = "b4";
430         if ((tx == TX_CALL) || (tx == TX_RB)) {
431             utx.begin();
432         }
433         ARemote a = ahome.findByPrimaryKey("a3");
434         if (tx == TX_CONT) {
435             a.removeFromBInNewTx(bRemoved);
436         } else {
437             a.removeFromB(bRemoved);
438         }
439         if (tx == TX_CALL) {
440             utx.commit();
441         } else if (tx == TX_RB) {
442             utx.rollback();
443         }
444         // checking
445
Collection JavaDoc ca = a.retrieveB();
446         if (tx != TX_RB) {
447             assertEquals("Wrong relations size for a3: ", 2, ca.size());
448         } else {
449             assertEquals("Wrong relations size for a3: ", 3, ca.size());
450         }
451         // undo
452
if (tx != TX_RB) {
453             a.addInB(bRemoved);
454         }
455         // check to initial state
456
checkIsInitialState();
457
458     }
459
460    
461     public static Test suite() {
462         return new TestSuite(G_Relation_mnuEC2.class);
463     }
464
465     public static void main(String JavaDoc args[]) {
466         String JavaDoc testtorun = null;
467         // Get args
468
for (int argn = 0; argn < args.length; argn++) {
469             String JavaDoc s_arg = args[argn];
470             Integer JavaDoc i_arg;
471             if (s_arg.equals("-n")) {
472                 testtorun = args[++argn];
473             }
474         }
475         if (testtorun == null) {
476             junit.textui.TestRunner.run(suite());
477         } else {
478             junit.textui.TestRunner.run(new G_Relation_mnuEC2(testtorun));
479         }
480     }
481
482 }
483
Popular Tags