KickJava   Java API By Example, From Geeks To Geeks.

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


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:
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.clients.entity;
27
28 import java.util.Enumeration JavaDoc;
29 import java.util.Hashtable JavaDoc;
30
31 import javax.naming.NamingException JavaDoc;
32 import javax.rmi.PortableRemoteObject JavaDoc;
33
34 import junit.framework.Test;
35 import junit.framework.TestSuite;
36
37 import org.objectweb.jonas.jtests.beans.relation.mou.AHomeRemote;
38 import org.objectweb.jonas.jtests.beans.relation.mou.ARemote;
39 import org.objectweb.jonas.jtests.beans.relation.mou.BHomeRemote;
40
41 /**
42  * For testing many-to-one unidirectional relationships
43  * @author S.Chassande-Barrioz, J. Camilleri
44  *
45  * This is an advanced test suite for home interface on entity bean CMP2.
46  *
47  **/

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

157     boolean initStateOK() throws Exception JavaDoc {
158         boolean isOk = true;
159         msgerror = new StringBuffer JavaDoc();
160         for (Enumeration JavaDoc ea = tbRelationA2B.keys(); ea.hasMoreElements();) {
161             String JavaDoc aname = (String JavaDoc) (ea.nextElement());
162             ARemote a = ahome.findByPrimaryKey(aname);
163             String JavaDoc BActual = a.retrieveB();
164             // If BActual is empty then convert to "" to match with tbRelationA2B value in this case
165
if (BActual == null) BActual = "";
166             String JavaDoc BExpected = (String JavaDoc) (tbRelationA2B.get(aname));
167             if (!BActual.equals(BExpected)) {
168                 isOk = false;
169                 msgerror = msgerror.append("Wrong relation for " + aname
170                                            + " (expected:" + BExpected
171                                            + ", found:" + BActual + ")");
172             }
173         }
174         return isOk;
175     }
176
177
178
179     /**
180      * Check that the bean 'a3' has no relation.
181      */

182     public void _testBasicGetEmpty(int tx) throws Exception JavaDoc {
183         String JavaDoc c = null;
184         if ((tx == TX_CALL) || (tx == TX_RB)) {
185             utx.begin();
186         }
187         ARemote a = ahome.findByPrimaryKey("a3");
188         if (tx == TX_CONT) {
189             c = a.retrieveBInNewTx();
190         } else {
191             c = a.retrieveB();
192         }
193         if (tx == TX_CALL) {
194             utx.commit();
195         } else if (tx == TX_RB) {
196             utx.rollback();
197         }
198         checkIsInitialState();
199     }
200     public void testBasicGetEmptyTxNo() throws Exception JavaDoc {
201         _testBasicGetEmpty(TX_NO);
202     }
203
204     public void testBasicGetEmptyTxCall() throws Exception JavaDoc {
205         _testBasicGetEmpty(TX_CALL);
206     }
207
208     public void testBasicGetEmptyTxCont() throws Exception JavaDoc {
209         _testBasicGetEmpty(TX_CONT);
210     }
211   
212     /**
213      * Ckeck the new relation a3-b1
214      */

215     public void _testBasicSetEmpty(int tx) throws Exception JavaDoc {
216         String JavaDoc c = null;
217         if ((tx == TX_CALL) || (tx == TX_RB)) {
218             utx.begin();
219         }
220         ARemote a = ahome.findByPrimaryKey("a3");
221         if (tx == TX_CONT) {
222             a.assignBInNewTx("b1");
223         } else {
224             a.assignB("b1");
225         }
226         if (tx == TX_CALL) {
227             utx.commit();
228         } else if (tx == TX_RB) {
229             utx.rollback();
230         }
231         // checking
232
String JavaDoc idB = a.retrieveB();
233         if (tx != TX_RB) {
234             assertEquals("Wrong new relation a3->b1 : ", "b1", idB);
235         } else {
236             assertNull("Wrong new relation a3->b1 : ", idB);
237         }
238         // undo
239
if (tx != TX_RB) {
240             a.assignB(null);
241         }
242         checkIsInitialState();
243     }
244
245     public void testBasicSetEmptyTxNo() throws Exception JavaDoc {
246         _testBasicSetEmpty(TX_NO);
247     }
248
249     public void testBasicSetEmptyTxCall() throws Exception JavaDoc {
250         _testBasicSetEmpty(TX_CALL);
251     }
252
253     public void testBasicSetEmptyTxCont() throws Exception JavaDoc {
254         _testBasicSetEmpty(TX_CONT);
255     }
256
257     /**
258      * Check that a1.retrieveB()=b0
259      */

260     public void _testBasicGet(int tx) throws Exception JavaDoc {
261         String JavaDoc b = null;
262         if ((tx == TX_CALL) || (tx == TX_RB)) {
263             utx.begin();
264         }
265         ARemote a1 = ahome.findByPrimaryKey("a1");
266
267         if (tx == TX_CONT) {
268             b = a1.retrieveBInNewTx();
269         } else {
270             b = a1.retrieveB();
271         }
272         if (tx == TX_CALL) {
273             utx.commit();
274         } else if (tx == TX_RB) {
275             utx.rollback();
276         }
277         // checking
278
if (tx != TX_RB) {
279             assertEquals("Wrong relation a1->b0 : ", "b0", b);
280         }
281         checkIsInitialState();
282     }
283
284    
285
286   
287
288     /**
289      * Check that a1.assignB(null) => a1.retreiveB()=null
290      */

291     public void _testBasicSetNull(int tx) throws Exception JavaDoc {
292         if ((tx == TX_CALL) || (tx == TX_RB)) {
293             utx.begin();
294         }
295         ARemote a1 = ahome.findByPrimaryKey("a1");
296
297         if (tx == TX_CONT) {
298             a1.assignBInNewTx(null);
299         } else {
300             a1.assignB(null);
301         }
302         if (tx == TX_CALL) {
303             utx.commit();
304         } else if (tx == TX_RB) {
305             utx.rollback();
306         }
307         // checking
308
if (tx != TX_RB) {
309             String JavaDoc b = a1.retrieveB();
310             assertNull("Wrong relation a1 : ", b);
311             // Undo
312
a1.assignB("b0");
313         }
314         checkIsInitialState();
315     }
316
317    
318     /**
319      * See 10.3.7.5 : a1j.assignB(a2k.retrieveB()) => a2=a1j.retrieveB()
320      */

321     public void _testBasicSet(int tx) throws Exception JavaDoc {
322         String JavaDoc c = null;
323         if ((tx == TX_CALL) || (tx == TX_RB)) {
324             utx.begin();
325         }
326         ARemote a1 = ahome.findByPrimaryKey("a1");
327         ARemote a21 = ahome.findByPrimaryKey("a21");
328         if (tx == TX_CONT) {
329             a1.assignBInNewTx(a21.retrieveB());
330         } else {
331             a1.assignB(a21.retrieveB());
332         }
333         if (tx == TX_CALL) {
334             utx.commit();
335         } else if (tx == TX_RB) {
336             utx.rollback();
337         }
338         if (tx != TX_RB) {
339             // checking
340
String JavaDoc idB1 = a1.retrieveB();
341             ARemote a0 = ahome.findByPrimaryKey("a0");
342             ARemote a2 = ahome.findByPrimaryKey("a2");
343             ARemote a3 = ahome.findByPrimaryKey("a3");
344             ARemote a20 = ahome.findByPrimaryKey("a20");
345             ARemote a22 = ahome.findByPrimaryKey("a22");
346             String JavaDoc idB0 = a0.retrieveB();
347             String JavaDoc idB2 = a2.retrieveB();
348             String JavaDoc idB3 = a3.retrieveB();
349             String JavaDoc idB20 = a20.retrieveB();
350             String JavaDoc idB21 = a21.retrieveB();
351             String JavaDoc idB22 = a22.retrieveB();
352             assertEquals("Wrong new relation a1->b2 : ", "b2", idB1);
353             assertEquals("Wrong new relation a0->b0 : ", "b0", idB0);
354             assertEquals("Wrong new relation a2->b0 : ", "b0", idB2);
355             assertEquals("Wrong new relation a20->b2 : ", "b2", idB20);
356             assertEquals("Wrong new relation a21->b2 : ", "b2", idB21);
357             assertEquals("Wrong new relation a22->b2 : ", "b2", idB22);
358             // undo
359
a1.assignB("b0");
360         }
361         checkIsInitialState();
362     }
363
364    
365
366  
367
368     /**
369      * Test of coherence a0.remove()
370      */

371     public void _testCohRemoveA(int tx) throws Exception JavaDoc {
372         if (tx == TX_CONT) {
373             // The transaction attribute of the remove method is TX_SUPPORT,
374
// so the transaction cannot be initiate by the container
375
fail("Transaction cannot be initiate by the container for this test");
376         }
377         if ((tx == TX_CALL) || (tx == TX_RB)) {
378             utx.begin();
379         }
380         ahome.remove("a0");
381         if (tx == TX_CALL) {
382             utx.commit();
383         } else if (tx == TX_RB) {
384             utx.rollback();
385         }
386         // undo
387
if (tx != TX_RB) {
388             ARemote a0 = ahome.create("a0");
389             a0.assignBInNewTx("b0");
390         }
391         checkIsInitialState();
392     }
393
394   
395     public static Test suite() {
396         return new TestSuite(G_Relation_mouEC2.class);
397     }
398
399     public static void main(String JavaDoc args[]) {
400         String JavaDoc testtorun = null;
401         // Get args
402
for (int argn = 0; argn < args.length; argn++) {
403             String JavaDoc s_arg = args[argn];
404             Integer JavaDoc i_arg;
405             if (s_arg.equals("-n")) {
406                 testtorun = args[++argn];
407             }
408         }
409         if (testtorun == null) {
410             junit.textui.TestRunner.run(suite());
411         } else {
412             junit.textui.TestRunner.run(new G_Relation_mouEC2(testtorun));
413         }
414     }
415
416 }
417
Popular Tags