KickJava   Java API By Example, From Geeks To Geeks.

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


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_s2pkcompEC2.java,v 1.14 2004/11/19 09:56:35 joaninh Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.clients.entity;
27
28 import java.util.Collection JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import java.util.Hashtable JavaDoc;
31
32 import javax.ejb.ObjectNotFoundException JavaDoc;
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.s2pkcomp.AHomeRemote;
40 import org.objectweb.jonas.jtests.beans.relation.s2pkcomp.ARemote;
41 import org.objectweb.jonas.jtests.beans.relation.s2pkcomp.BHomeRemote;
42 import org.objectweb.jonas.jtests.beans.relation.s2pkcomp.BRemote;
43 import org.objectweb.jonas.jtests.beans.relation.s2pkcomp.Pk;
44
45 /**
46  * This is an advanced test suite for home interface on entity bean CMP2.
47  * Test about beans with mono-valued relation and with composite Pk with same fields names.
48  * Beans used: s2pkcomp
49  * @author S.Chassande-Barrioz
50  */

51 public class F_Relation_s2pkcompEC2 extends A_Cmp2Util {
52
53
54     private static String JavaDoc BEAN_HOME_A = "relation_s2pkcomp_AHome";
55     private static String JavaDoc BEAN_HOME_B = "relation_s2pkcomp_BHome";
56     protected static AHomeRemote ahome = null;
57     protected static BHomeRemote bhome = null;
58
59     static Hashtable JavaDoc a2b = new Hashtable JavaDoc();
60     static Hashtable JavaDoc b2a = new Hashtable JavaDoc();
61     static Pk pkNull = new Pk("null", 0);
62
63     static {
64         a2b.put(new Pk("a",1), new Pk("b",1));
65         a2b.put(new Pk("a",2), new Pk("b",2));
66         a2b.put(new Pk("a",3), pkNull);
67         b2a.put(new Pk("b",1), new Pk("a",1));
68         b2a.put(new Pk("b",2), new Pk("a",2));
69         b2a.put(new Pk("b",3), pkNull);
70     }
71
72     public F_Relation_s2pkcompEC2(String JavaDoc name) {
73         super(name);
74     }
75
76     protected static boolean isInit = false;
77
78     protected void setUp() {
79         super.setUp();
80         boolean ok = false;
81         int nbtry = 0;
82         while (!ok && nbtry < 3) {
83             if (!isInit) {
84                 // load bean if not loaded yet
85
useBeans("s2pkcomp", false);
86                 // lookup home used in the tests
87
try {
88                     ahome = (AHomeRemote) PortableRemoteObject.narrow(ictx.lookup(BEAN_HOME_A),
89                                                                       AHomeRemote.class);
90                     bhome = (BHomeRemote) PortableRemoteObject.narrow(ictx.lookup(BEAN_HOME_B),
91                                                                       BHomeRemote.class);
92                 } catch (NamingException JavaDoc e) {
93                     fail("Cannot get bean home: " + e.getMessage());
94                 }
95                 // check if tables have been initialized
96
try {
97                     ahome.findByPrimaryKey(new Pk("a", 2));
98                 } catch (Exception JavaDoc e) {
99                     // Make the initialization needed for the tests
100
try {
101                         utx.begin();
102                         ARemote a1 = ahome.create("a", 1);
103                         ARemote a2 = ahome.create("a", 2);
104                         ahome.create("a", 3);
105                         bhome.create("b", 1);
106                         bhome.create("b", 2);
107                         bhome.create("b", 3);
108                         a1.assignB(new Pk("b", 1));
109                         a2.assignB(new Pk("b", 2));
110                     } catch (Exception JavaDoc i) {
111                         fail("InitialState creation problem: "+i);
112                     } finally {
113                         try {
114                             utx.commit();
115                         } catch (Exception JavaDoc ii) {
116                         }
117                     }
118                 }
119                 isInit = true;
120             }
121             // Check that all is OK. Sometimes, a test has failed and has corrupted
122
// the bean state in the database. We must unload and reload the bean then.
123
nbtry++;
124             try {
125                 if (initStateOK()) {
126                     ok = true;
127                 }
128             } catch (Exception JavaDoc e) {
129             }
130             if (!ok) {
131                 isInit = false;
132                 unloadBeans("s2pkcomp");
133             }
134         }
135     }
136
137     /*
138      * Check that we are in the same state as after the tables creation for thoses beans A and B
139      * (ie if it is the initial state)
140      */

141     boolean initStateOK() throws Exception JavaDoc {
142         boolean isOk = true;
143         msgerror = new StringBuffer JavaDoc();
144         // Check relations A to B
145
for (Enumeration JavaDoc ea = a2b.keys(); ea.hasMoreElements();) {
146             Pk apk = (Pk) (ea.nextElement());
147             ARemote a = ahome.findByPrimaryKey(apk);
148             Pk bpkActual = a.retrieveB();
149             Pk bpkExpected = (Pk)(a2b.get(apk));
150             if (pkNull.equals(bpkExpected)) {
151                 if (bpkActual!=null) {
152                     isOk = false;
153                     msgerror.append("\nWrong relation for " + apk.toString()
154                                     + " (expected: null"
155                                     + ", found:" + bpkActual.toString() + ")");
156                 }
157             } else {
158                 if (!bpkExpected.equals(bpkActual)) {
159                     isOk = false;
160                     msgerror.append("\nWrong relation for " + apk.toString()
161                                     + " (expected:" + bpkExpected.toString()
162                                     + ", found:" + bpkActual.toString() + ")");
163                 }
164             }
165         }
166         // Check relations B to A
167
for (Enumeration JavaDoc eb = b2a.keys(); eb.hasMoreElements();) {
168             Pk bpk = (Pk) (eb.nextElement());
169             BRemote b = bhome.findByPrimaryKey(bpk);
170             Pk apkActual = b.retrieveA();
171             Pk apkExpected = (Pk)(b2a.get(bpk));
172             if (pkNull.equals(apkExpected)) {
173                 if (apkActual!=null) {
174                     isOk = false;
175                     msgerror.append("\nWrong relation for " + bpk.toString()
176                                     + " (expected: null"
177                                     + ", found:" + apkActual.toString() + ")");
178                 }
179             } else {
180                 if (!apkExpected.equals(apkActual)) {
181                     isOk = false;
182                     msgerror.append("\nWrong relation for " + bpk.toString()
183                                     + " (expected:" + apkExpected.toString()
184                                     + ", found:" + apkActual.toString() + ")");
185                 }
186             }
187         }
188         return isOk;
189     }
190
191     /**
192      * test coherence relation one to one bidirectionnel,
193      * A1.remove=>A1removed && B1.retreiveA()==null
194      */

195     public void _testCohRemoveA(int tx) throws Exception JavaDoc {
196         if (tx == TX_CONT) {
197             // The transaction attribute of the remove method is TX_SUPPORT,
198
// so the transaction cannot be initiate by the container
199
fail("Transaction cannot be initiate by the container for this test");
200         }
201         if ((tx == TX_CALL) || (tx == TX_RB)) {
202             utx.begin();
203         }
204         BRemote b1 = bhome.findByPrimaryKey(new Pk("b", 1));
205         // remove the bean
206
ahome.remove(new Pk("a", 1));
207         if (tx == TX_CALL) {
208             utx.commit();
209         } else if (tx == TX_RB) {
210             utx.rollback();
211         }
212         if (tx != TX_RB) {
213             // Verify expected result
214
assertNull("Bad coherence of relation : null expected for b1.retreiveB() found :"+b1.retrieveA(), b1.retrieveA());
215             boolean not_found=false;
216             try {
217                 ARemote a1 = ahome.findByPrimaryKey(new Pk("a", 1));
218             } catch (ObjectNotFoundException JavaDoc e) {
219                 not_found=true;
220             }
221             assertTrue("a-1 is not removed",not_found);
222             // undo
223
ahome.create("a", 1);
224             ARemote a1 = ahome.findByPrimaryKey(new Pk("a", 1));
225             a1.assignB(new Pk("b", 1));
226         }
227         checkIsInitialState();
228     }
229
230     public void testCohRemoveATxNo() throws Exception JavaDoc {
231         _testCohRemoveA(TX_NO);
232     }
233     public void testCohRemoveATxCall() throws Exception JavaDoc {
234         _testCohRemoveA(TX_CALL);
235     }
236     public void testCohRemoveATxRb() throws Exception JavaDoc {
237         _testCohRemoveA(TX_RB);
238     }
239
240     /**
241      * test coherence relation one to one bidirectionnel,
242      * B1.remove=>B1 removed && A1.retreiveB()==null
243      * Same as _testCohRemoveB except that the called remove method is on the bean
244      * instead of the home.
245      */

246     public void _testCohBeanRemoveB(int tx) throws Exception JavaDoc {
247         if (tx == TX_CONT) {
248             // The transaction attribute of the remove method is TX_SUPPORT,
249
// so the transaction cannot be initiate by the container
250
fail("Transaction cannot be initiate by the container for this test");
251         }
252         if ((tx == TX_CALL) || (tx == TX_RB)) {
253             utx.begin();
254         }
255         ARemote a1 = ahome.findByPrimaryKey(new Pk("a", 1));
256         BRemote b1 = bhome.findByPrimaryKey(new Pk("b", 1));
257         // change the relation
258
b1.remove();
259         if (tx == TX_CALL) {
260             utx.commit();
261         } else if (tx == TX_RB) {
262             utx.rollback();
263         }
264         if (tx != TX_RB) {
265             // Verify expected result
266
assertNull("Bad coherence of relation : null expected for a1.retreiveB() found :"+a1.retrieveB(), a1.retrieveB());
267             boolean not_found=false;
268             try {
269                 b1 = bhome.findByPrimaryKey(new Pk("b", 1));
270             } catch (ObjectNotFoundException JavaDoc e) {
271                 not_found=true;
272             }
273             assertTrue("b-1 is not removed", not_found);
274             // undo
275
bhome.create("b", 1);
276             a1.assignB(new Pk("b", 1));
277         }
278         checkIsInitialState();
279     }
280
281     public void testCohBeanRemoveBTxNo() throws Exception JavaDoc {
282         _testCohBeanRemoveB(TX_NO);
283     }
284     public void testCohBeanRemoveBTxCall() throws Exception JavaDoc {
285         _testCohBeanRemoveB(TX_CALL);
286     }
287     public void testCohBeanRemoveBTxRb() throws Exception JavaDoc {
288         _testCohBeanRemoveB(TX_RB);
289     }
290
291     /**
292      * test a finder method.
293      * (bug #300612)
294      */

295     public void _testFindByBIsNull(int tx) throws Exception JavaDoc {
296         if ((tx == TX_CALL) || (tx == TX_RB)) {
297             utx.begin();
298         }
299         Collection JavaDoc ca = ahome.findByBIsNull();
300         if (tx == TX_CALL) {
301             utx.commit();
302         } else if (tx == TX_RB) {
303             utx.rollback();
304         }
305         // Verify expected result
306
assertEquals("Bad result for findByBIsNull: ", 1, ca.size());
307     }
308
309     public void testFindByBIsNullTxNo() throws Exception JavaDoc {
310         _testFindByBIsNull(TX_NO);
311     }
312     // temporaly commented
313
public void _testFindByBIsNullTxCall() throws Exception JavaDoc {
314         _testFindByBIsNull(TX_CALL);
315     }
316     // temporaly commented
317
public void _testFindByBIsNullTxRb() throws Exception JavaDoc {
318         _testFindByBIsNull(TX_RB);
319     }
320
321     /**
322      * test a finder method.
323      */

324     public void _testFindAById1Id2(int tx) throws Exception JavaDoc {
325         if ((tx == TX_CALL) || (tx == TX_RB)) {
326             utx.begin();
327         }
328         ARemote a1 = ahome.findById1Id2("a", 1);
329         if (tx == TX_CALL) {
330             utx.commit();
331         } else if (tx == TX_RB) {
332             utx.rollback();
333         }
334         // Verify expected result
335
Pk pkA1 = a1.getId();
336         assertEquals("Bad id1 of a1: ", "a", pkA1.id1);
337         assertEquals("Bad id2 of a1: ", 1, pkA1.id2);
338     }
339
340     public void testFindAById1Id2TxNo() throws Exception JavaDoc {
341         _testFindAById1Id2(TX_NO);
342     }
343     public void testFindAById1Id2TxCall() throws Exception JavaDoc {
344         _testFindAById1Id2(TX_CALL);
345     }
346     public void testFindAById1Id2TxRb() throws Exception JavaDoc {
347         _testFindAById1Id2(TX_RB);
348     }
349
350     public static Test suite() {
351         return new TestSuite(F_Relation_s2pkcompEC2.class);
352     }
353
354     public static void main (String JavaDoc args[]) {
355         String JavaDoc testtorun = null;
356         // Get args
357
for (int argn = 0; argn < args.length; argn++) {
358             String JavaDoc s_arg = args[argn];
359             Integer JavaDoc i_arg;
360             if (s_arg.equals("-n")) {
361                 testtorun = args[++argn];
362             }
363         }
364         if (testtorun == null) {
365             junit.textui.TestRunner.run(suite());
366         } else {
367             junit.textui.TestRunner.run(new F_Relation_s2pkcompEC2(testtorun));
368         }
369     }
370 }
371
372
Popular Tags