KickJava   Java API By Example, From Geeks To Geeks.

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


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_oobEC2.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 javax.naming.NamingException JavaDoc;
29 import javax.rmi.PortableRemoteObject JavaDoc;
30
31 import junit.framework.Test;
32 import junit.framework.TestSuite;
33
34 import org.objectweb.jonas.jtests.beans.relation.oob.AHomeRemote;
35 import org.objectweb.jonas.jtests.beans.relation.oob.ARemote;
36 import org.objectweb.jonas.jtests.beans.relation.oob.BHomeRemote;
37 import org.objectweb.jonas.jtests.beans.relation.oob.BRemote;
38
39 /**
40  * This is an advanced test suite for home interface on entity bean CMP2.
41  * Beans used: oob
42  */

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

124     boolean initStateOK() throws Exception JavaDoc {
125         boolean isOk = true;
126
127         msgerror = new StringBuffer JavaDoc();
128
129         ARemote a1 = ahome.findByPrimaryKey("a1");
130         BRemote b1 = bhome.findByPrimaryKey("b1");
131         ARemote a2 = ahome.findByPrimaryKey("a2");
132         BRemote b2 = bhome.findByPrimaryKey("b2");
133         ARemote a3 = ahome.findByPrimaryKey("a3");
134         BRemote b3 = bhome.findByPrimaryKey("b3");
135         String JavaDoc idB1 = a1.retrieveB();
136         String JavaDoc idB2 = a2.retrieveB();
137         String JavaDoc idB3 = a3.retrieveB();
138         String JavaDoc idA1 = b1.retrieveA();
139         String JavaDoc idA2 = b2.retrieveA();
140         String JavaDoc idA3 = b3.retrieveA();
141
142         if (idB1 != null && !idB1.equals("b1")) {
143             isOk = false;
144             msgerror.append("\nWrong relation for a1->b1"
145                             + "(expected: A1.retrieveB()='b1'"
146                             + ", found:" + idB1 + ")");
147         }
148         if (idB2 != null && !idB2.equals("b2")) {
149             isOk = false;
150             msgerror.append("\nWrong relation for a2->b2"
151                             + "(expected: A2.retrieveB()='b2'"
152                             + ", found:" + idB2 + ")");
153         }
154         if (idA1 != null && !idA1.equals("a1")) {
155             isOk = false;
156             msgerror.append("\nWrong relation for b1->a1"
157                             + "(expected: B1.retrieveA()='a1'"
158                             + ", found:" + idA1 + ")");
159         }
160         if (idA2 != null && !idA2.equals("a2")) {
161             isOk = false;
162             msgerror.append("\nWrong relation for b2->a2"
163                             + "(expected: B2.retrieveA()='a2'"
164                             + ", found:" + idA2 + ")");
165         }
166         if (idA3 != null) {
167             isOk = false;
168             msgerror.append("\nWrong relation for a3->b3"
169                             + "(expected: A3.retrieveB()=null"
170                             + ", found:" + idA3 + ")");
171         }
172         if (idB3 != null) {
173             isOk = false;
174             msgerror.append("\nWrong relation for b3->a3"
175                             + "(expected: B3.retrieveA()=null"
176                             + ", found:" + idB3 + ")");
177         }
178         return isOk;
179     }
180
181
182     /**
183      * Check that the bean 'a3' has no relation.
184      */

185     public void _testBasicGetEmpty(int tx) throws Exception JavaDoc {
186         String JavaDoc idB = null;
187         if ((tx == TX_CALL) || (tx == TX_RB)) {
188             utx.begin();
189         }
190         ARemote a = ahome.findByPrimaryKey("a3");
191         if (tx == TX_CONT) {
192             idB = a.retrieveBInNewTx();
193         } else {
194             idB = a.retrieveB();
195         }
196         if (tx == TX_CALL) {
197             utx.commit();
198         } else if (tx == TX_RB) {
199             utx.rollback();
200         }
201         checkIsInitialState();
202     }
203
204    
205
206     /**
207      * Set a relation to empty : a3.assignB("b3")
208      */

209     public void _testBasicSetEmpty(int tx) throws Exception JavaDoc {
210         if ((tx == TX_CALL) || (tx == TX_RB)) {
211             utx.begin();
212         }
213         ARemote a = ahome.findByPrimaryKey("a3");
214         if (tx == TX_CONT) {
215             a.assignBInNewTx("b3");
216         } else {
217             a.assignB("b3");
218         }
219         if (tx == TX_CALL) {
220             utx.commit();
221         } else if (tx == TX_RB) {
222             utx.rollback();
223         }
224         // checking
225
String JavaDoc idB = a.retrieveB();
226         if (tx != TX_RB) {
227             assertEquals("Wrong assign null for relation a3->b3 : ","b3", idB);
228         } else {
229             assertNull("Wrong assign null for relation a3->b3 : ", idB);
230         }
231         // undo
232
if (tx != TX_RB) {
233             a.assignB(null);
234         }
235         checkIsInitialState();
236     }
237
238     public void testBasicSetEmptyTxNo() throws Exception JavaDoc {
239         _testBasicSetEmpty(TX_NO);
240     }
241     public void testBasicSetEmptyTxCall() throws Exception JavaDoc {
242         _testBasicSetEmpty(TX_CALL);
243     }
244     public void testBasicSetEmptyTxCont() throws Exception JavaDoc {
245         _testBasicSetEmpty(TX_CONT);
246     }
247    
248     /**
249      * Set a relation to empty : a3.assignB(null)
250      */

251     public void _testBasicSetEmptyNull(int tx) throws Exception JavaDoc {
252         if ((tx == TX_CALL) || (tx == TX_RB)) {
253             utx.begin();
254         }
255         ARemote a = ahome.findByPrimaryKey("a1");
256         if (tx == TX_CONT) {
257             a.assignBInNewTx(null);
258         } else {
259             a.assignB(null);
260         }
261         if (tx == TX_CALL) {
262             utx.commit();
263         } else if (tx == TX_RB) {
264             utx.rollback();
265         }
266         // checking
267
String JavaDoc idB = a.retrieveB();
268         if (tx == TX_RB) {
269             assertEquals("Wrong assign null for relation a1->b1: ", "b1", idB);
270         } else {
271             assertNull("Wrong assign null for relation a1->b1: " + idB, idB);
272         }
273         // undo
274
if (tx != TX_RB) {
275             a.assignB("b1");
276         }
277         checkIsInitialState();
278     }
279
280    
281     /**
282      * test coherence relation one to one bidirectionnel,
283      * A1.assignB(B2) => A1.retreiveB()=B2 && B2.retreiveA()=A1 && B1.retreiveA()=null && A2.retreiveB()=null
284      */

285     public void _testCohSetOne(int tx) throws Exception JavaDoc {
286         if ((tx == TX_CALL) || (tx == TX_RB)) {
287             utx.begin();
288         }
289         ARemote a1 = ahome.findByPrimaryKey("a1");
290         BRemote b1 = bhome.findByPrimaryKey("b1");
291         ARemote a2 = ahome.findByPrimaryKey("a2");
292         BRemote b2 = bhome.findByPrimaryKey("b2");
293         ARemote a3 = ahome.findByPrimaryKey("a3");
294         BRemote b3 = bhome.findByPrimaryKey("b3");
295         // change the relation
296
if (tx == TX_CONT) {
297             a1.assignBInNewTx("b2");
298         } else {
299             a1.assignB("b2");
300         }
301         if (tx == TX_CALL) {
302             utx.commit();
303         } else if (tx == TX_RB) {
304             utx.rollback();
305         }
306         if (tx != TX_RB) {
307             // Verify expected result
308
assertNull("Bad coherence of relation null : expected for a2.retreiveB() found :" + a2.retrieveB(), a2.retrieveB());
309             assertNull("Bad coherence of relation null : expected for b1.retreiveA() found :" + b1.retrieveA(), b1.retrieveA());
310             assertEquals("Bad coherence of relation a1 : expected for b2.retreiveA() found :" + b2.retrieveA(), "a1", b2.retrieveA());
311             assertEquals("Bad coherence of relation b2 : expected for a1.retreiveB() found :" + a1.retrieveB(), "b2", a1.retrieveB());
312
313             // undo
314
a1.assignB("b1");
315             a2.assignB("b2");
316         }
317         checkIsInitialState();
318     }
319
320    
321
322
323     public static Test suite() {
324         return new TestSuite(G_Relation_oobEC2.class);
325     }
326
327     public static void main (String JavaDoc args[]) {
328         String JavaDoc testtorun = null;
329         // Get args
330
for (int argn = 0; argn < args.length; argn++) {
331             String JavaDoc s_arg = args[argn];
332             Integer JavaDoc i_arg;
333             if (s_arg.equals("-n")) {
334                 testtorun = args[++argn];
335             }
336         }
337         if (testtorun == null) {
338             junit.textui.TestRunner.run(suite());
339         } else {
340             junit.textui.TestRunner.run(new G_Relation_oobEC2(testtorun));
341         }
342     }
343 }
344
Popular Tags