KickJava   Java API By Example, From Geeks To Geeks.

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


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_s1pkcompEC2.java,v 1.4 2003/08/07 13:02:49 durieuxp Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.clients.entity;
27
28 import junit.framework.Test;
29 import junit.framework.TestSuite;
30 import org.objectweb.jonas.jtests.beans.relation.s1pkcomp.AHomeRemote;
31 import org.objectweb.jonas.jtests.beans.relation.s1pkcomp.ARemote;
32 import org.objectweb.jonas.jtests.beans.relation.s1pkcomp.BHomeRemote;
33 import org.objectweb.jonas.jtests.beans.relation.s1pkcomp.BRemote;
34 import org.objectweb.jonas.jtests.beans.relation.s1pkcomp.PK;
35
36 import javax.naming.NamingException JavaDoc;
37 import javax.rmi.PortableRemoteObject JavaDoc;
38 import java.util.ArrayList JavaDoc;
39 import java.util.Collection JavaDoc;
40 import java.util.Enumeration JavaDoc;
41 import java.util.Hashtable JavaDoc;
42
43 /**
44  * Test composite PK for CMP2 entity beans with relationships M-N
45  * this test uses beans s1pkcomp/*
46  * @author S.Chassande-Barrioz
47  */

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

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

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