KickJava   Java API By Example, From Geeks To Geeks.

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


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_pkcompEC2.java,v 1.9 2003/06/04 14:56:00 durieuxp 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 javax.naming.NamingException JavaDoc;
33 import javax.rmi.PortableRemoteObject JavaDoc;
34 import junit.framework.Test;
35 import junit.framework.TestSuite;
36 import org.objectweb.jonas.jtests.beans.relation.pkcomp.APK;
37 import org.objectweb.jonas.jtests.beans.relation.pkcomp.AHomeRemote;
38 import org.objectweb.jonas.jtests.beans.relation.pkcomp.ARemote;
39 import org.objectweb.jonas.jtests.beans.relation.pkcomp.BPK;
40 import org.objectweb.jonas.jtests.beans.relation.pkcomp.BHomeRemote;
41 import org.objectweb.jonas.jtests.beans.relation.pkcomp.BRemote;
42
43 /**
44  * For testing many-to-many bidirectional relationships of beans with composite Primary Keys.
45  * This test is like F_Relation_mnbEC2 test.
46  * @author J. Camilleri, H. Joanin
47  *
48  * This is an advanced test suite for home interface on entity bean CMP2.
49  * Beans used: pkcomp/A1, pkcomp/A2
50  *
51  **/

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

176     boolean initStateOK() throws Exception JavaDoc {
177         boolean isOk = true;
178         msgerror = new StringBuffer JavaDoc();
179         // Check relations A to B
180
for (Enumeration JavaDoc ea = tbRelationA2B.keys(); ea.hasMoreElements();) {
181             String JavaDoc aname = (String JavaDoc) (ea.nextElement());
182             APK apk = new APK(getStringBeforeDash(aname), getIntAfterDash(aname));
183             ARemote a = ahome.findByPrimaryKey(apk);
184             Collection JavaDoc colActual = a.retrieveB();
185             ArrayList JavaDoc colExpected = (ArrayList JavaDoc) (tbRelationA2B.get(aname));
186             if (!isCollectionEqual(colExpected, colActual)) {
187                 isOk = false;
188                 msgerror = msgerror.append("Wrong relation for " + aname
189                                            + " (expected:" + colExpected
190                                            + ", found:" + colActual + ")");
191             }
192         }
193         // Check Relations B to A
194
for (Enumeration JavaDoc eb = tbRelationB2A.keys(); eb.hasMoreElements();) {
195             String JavaDoc bname = (String JavaDoc) (eb.nextElement());
196             BPK bpk = new BPK(getStringBeforeDash(bname), getIntAfterDash(bname));
197             BRemote b = bhome.findByPrimaryKey(bpk);
198             Collection JavaDoc colActual1 = b.retrieveA();
199             ArrayList JavaDoc colExpected1 = (ArrayList JavaDoc) (tbRelationB2A.get(bname));
200             if (!isCollectionEqual(colExpected1, colActual1)) {
201                 isOk = false;
202                 msgerror = msgerror.append("Wrong relation for " + bname
203                                            + " (expected:" + colExpected1
204                                            + ", found:" + colActual1 + ")");
205             }
206         }
207         return isOk;
208     }
209
210     /**
211      * Remove an element in a relation.
212      */

213     public void _testCohRemoveInRel(int tx) throws Exception JavaDoc {
214         String JavaDoc bRemovedString = "b";
215         int bRemovedInt = 4;
216         if ((tx == TX_CALL) || (tx == TX_RB)) {
217             utx.begin();
218         }
219         ARemote a = ahome.findByPrimaryKey(new APK("a",3));
220         if (tx == TX_CONT) {
221             a.removeFromBInNewTx(new BPK(bRemovedString, bRemovedInt));
222         } else {
223             a.removeFromB(new BPK(bRemovedString, bRemovedInt));
224         }
225         if (tx == TX_CALL) {
226             utx.commit();
227         } else if (tx == TX_RB) {
228             utx.rollback();
229         }
230         // checking
231
Collection JavaDoc ca = a.retrieveB();
232         BRemote b4 = bhome.findByPrimaryKey(new BPK("b", 4));
233         Collection JavaDoc cb4 = b4.retrieveA();
234         if (tx != TX_RB) {
235             assertEquals("Wrong relations size for a-3: ", 2, ca.size());
236             assertEquals("Wrong relations size for b-4: ", 0, cb4.size());
237         } else {
238             assertEquals("Wrong relations size for a-3: ", 3, ca.size());
239             assertEquals("Wrong relations size for b-4: ", 1, cb4.size());
240         }
241         // undo
242
if (tx != TX_RB) {
243             a.addInB(new BPK(bRemovedString, bRemovedInt));
244         }
245         // check to initial state
246
checkIsInitialState();
247
248     }
249
250     public void testCohRemoveInRelTxNo() throws Exception JavaDoc {
251         _testCohRemoveInRel(TX_NO);
252     }
253
254     public void testCohRemoveInRelTxCall() throws Exception JavaDoc {
255         _testCohRemoveInRel(TX_CALL);
256     }
257
258     public void testCohRemoveInRelTxCont() throws Exception JavaDoc {
259         _testCohRemoveInRel(TX_CONT);
260     }
261
262     public void testCohRemoveInRelTxRb() throws Exception JavaDoc {
263         _testCohRemoveInRel(TX_RB);
264     }
265
266     public static Test suite() {
267         return new TestSuite(F_Relation_pkcompEC2.class);
268     }
269
270     public static void main(String JavaDoc args[]) {
271         String JavaDoc testtorun = null;
272         // Get args
273
for (int argn = 0; argn < args.length; argn++) {
274             String JavaDoc s_arg = args[argn];
275             Integer JavaDoc i_arg;
276             if (s_arg.equals("-n")) {
277                 testtorun = args[++argn];
278             }
279         }
280         if (testtorun == null) {
281             junit.textui.TestRunner.run(suite());
282         } else {
283             junit.textui.TestRunner.run(new F_Relation_pkcompEC2(testtorun));
284         }
285     }
286
287 }
288
289
290
Popular Tags