KickJava   Java API By Example, From Geeks To Geeks.

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


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_oouEC2.java,v 1.23 2004/09/23 10:02:08 durieuxp Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.clients.entity;
27
28 import javax.ejb.ObjectNotFoundException JavaDoc;
29 import javax.naming.NamingException JavaDoc;
30 import javax.rmi.PortableRemoteObject JavaDoc;
31
32 import junit.framework.Test;
33 import junit.framework.TestSuite;
34
35 import org.objectweb.jonas.jtests.beans.relation.oou.AHomeRemote;
36 import org.objectweb.jonas.jtests.beans.relation.oou.ARemote;
37 import org.objectweb.jonas.jtests.beans.relation.oou.BHomeRemote;
38 import org.objectweb.jonas.jtests.beans.relation.oou.BRemote;
39
40 /**
41  * This is an advanced test suite for home interface on entity bean CMP2.
42  */

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

120     boolean initStateOK() throws Exception JavaDoc {
121         boolean isOk = true;
122
123         msgerror = new StringBuffer JavaDoc();
124
125         ARemote a1 = ahome.findByPrimaryKey("a1");
126         BRemote b1 = bhome.findByPrimaryKey("b1");
127         ARemote a2 = ahome.findByPrimaryKey("a2");
128         BRemote b2 = bhome.findByPrimaryKey("b2");
129         ARemote a3 = ahome.findByPrimaryKey("a3");
130         BRemote b3 = bhome.findByPrimaryKey("b3");
131         String JavaDoc idB1 = a1.retrieveB();
132         String JavaDoc idB2 = a2.retrieveB();
133         String JavaDoc idB3 = a3.retrieveB();
134
135         if (idB1!=null && !idB1.equals("b1")){
136             isOk = false;
137             msgerror.append("Wrong relation for a1->b1"
138                             +"(expected: A1.retrieveB()='b1'"
139                             + ", found:" + idB1 + ")");
140         }
141         else if (idB2!=null && !idB2.equals("b2")){
142             isOk = false;
143             msgerror.append("Wrong relation for a2->b2"
144                             +"(expected: A2.retrieveB()='b2'"
145                             + ", found:" + idB2 + ")");
146         }
147         else if (idB3!=null){
148             isOk = false;
149             msgerror.append("Wrong relation for b3->a3"
150                             +"(expected: B3.retrieveA()=null"
151                             + ", found:" + idB3 + ")");
152         }
153         return isOk;
154     }
155
156
157     /**
158      * Check that the bean 'a3' has no relation.
159      */

160     public void _testBasicGetEmpty(int tx) throws Exception JavaDoc {
161         String JavaDoc idB = null;
162         if ((tx == TX_CALL) || (tx == TX_RB)) {
163             utx.begin();
164         }
165         ARemote a = ahome.findByPrimaryKey("a3");
166         if (tx == TX_CONT) {
167             idB = a.retrieveBInNewTx();
168         } else {
169             idB = a.retrieveB();
170         }
171         if (tx == TX_CALL) {
172             utx.commit();
173         } else if (tx == TX_RB) {
174             utx.rollback();
175         }
176
177         checkIsInitialState();
178     }
179     public void testBasicGetEmptyTxNo() throws Exception JavaDoc {
180         _testBasicGetEmpty(TX_NO);
181     }
182     public void testBasicGetEmptyTxCall() throws Exception JavaDoc {
183         _testBasicGetEmpty(TX_CALL);
184     }
185     public void testBasicGetEmptyTxCont() throws Exception JavaDoc {
186         _testBasicGetEmpty(TX_CONT);
187     }
188
189     /**
190      * Set a relation to empty : a3.assignB("b3")
191      */

192     public void _testBasicSetEmpty(int tx) throws Exception JavaDoc {
193         if ((tx == TX_CALL) || (tx == TX_RB)) {
194             utx.begin();
195         }
196         ARemote a = ahome.findByPrimaryKey("a3");
197         if (tx == TX_CONT) {
198             a.assignBInNewTx("b3");
199         } else {
200             a.assignB("b3");
201         }
202         if (tx == TX_CALL) {
203             utx.commit();
204         } else if (tx == TX_RB) {
205             utx.rollback();
206         }
207         // checking
208
String JavaDoc idB = a.retrieveB();
209         if (tx != TX_RB) {
210             assertEquals("Wrong assign null for relation a3->b3 : ","b3", idB);
211         } else {
212             assertNull("Wrong assign null for relation a3->b3 : ", idB);
213         }
214         // undo
215
if (tx != TX_RB) {
216             a.assignB(null);
217         }
218         checkIsInitialState();
219     }
220
221     public void testBasicSetEmptyTxNo() throws Exception JavaDoc {
222         _testBasicSetEmpty(TX_NO);
223     }
224     public void testBasicSetEmptyTxCall() throws Exception JavaDoc {
225         _testBasicSetEmpty(TX_CALL);
226     }
227     public void testBasicSetEmptyTxCont() throws Exception JavaDoc {
228         _testBasicSetEmpty(TX_CONT);
229     }
230     public void testBasicSetEmptyTxRb() throws Exception JavaDoc {
231         _testBasicSetEmpty(TX_RB);
232     }
233
234     /**
235      * Set a relation to empty : a3.assignB(null)
236      */

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

285     public void _testCohSetOne(int tx) throws Exception JavaDoc {
286
287         if ((tx == TX_CALL) || (tx == TX_RB)) {
288             utx.begin();
289         }
290         ARemote a1 = ahome.findByPrimaryKey("a1");
291         ARemote a2 = ahome.findByPrimaryKey("a2");
292         // change the relation
293
if (tx == TX_CONT) {
294             a1.assignBInNewTx("b2");
295         } else {
296             a1.assignB("b2");
297         }
298         if (tx == TX_CALL) {
299             utx.commit();
300         } else if (tx == TX_RB) {
301             utx.rollback();
302         }
303         if (tx != TX_RB) {
304             // Verify expected result
305
assertNull("Bad coherence of relation null : expected for a2.retreiveB() found :" + a2.retrieveB(), a2.retrieveB());
306             assertEquals("Bad coherence of relation b2 : expected for a1.retreiveB() found :" + a1.retrieveB(), "b2", a1.retrieveB());
307
308             // undo
309
a2.assignB("b2");
310             a1.assignB("b1");
311         }
312         checkIsInitialState();
313     }
314
315     public void testCohSetOneTxNo() throws Exception JavaDoc {
316         _testCohSetOne(TX_NO);
317     }
318     public void testCohSetOneTxCall() throws Exception JavaDoc {
319         _testCohSetOne(TX_CALL);
320     }
321
322     public void testCohSetOneTxCont() throws Exception JavaDoc {
323         _testCohSetOne(TX_CONT);
324     }
325
326     public void testCohSetOneTxRb() throws Exception JavaDoc {
327         _testCohSetOne(TX_RB);
328     }
329
330     /**
331      * test coherence relation one to one bidirectionnel,
332      * A3.assignB(B3)=>A3.retreiveB()==B3
333      */

334     public void _testCohWithoutRelation(int tx) throws Exception JavaDoc {
335
336         if ((tx == TX_CALL) || (tx == TX_RB)) {
337             utx.begin();
338         }
339         ARemote a3 = ahome.findByPrimaryKey("a3");
340         // change the relation
341
if (tx == TX_CONT) {
342             a3.assignBInNewTx("b3");
343         } else {
344             a3.assignB("b3");
345         }
346         if (tx == TX_CALL) {
347             utx.commit();
348         } else if (tx == TX_RB) {
349             utx.rollback();
350         }
351         if (tx != TX_RB) {
352             // Verify expected result
353
assertEquals("Bad coherence of relation : b3 expected for a3.retreiveB() found :"+a3.retrieveB(),"b3", a3.retrieveB());
354
355             // undo
356
a3.assignB(null);
357         }
358         checkIsInitialState();
359     }
360
361     public void testBasicCohWithoutRelation() throws Exception JavaDoc {
362         _testCohWithoutRelation(TX_NO);
363     }
364     public void testBasicCohWithoutRelationTxCall() throws Exception JavaDoc {
365         _testCohWithoutRelation(TX_CALL);
366     }
367
368     /**
369      * test coherence relation one to one bidirectionnel,
370      * A1.assignB(B3)=>A1.retreiveB()==B3
371      */

372     public void _testCohAlreadyAssign(int tx) throws Exception JavaDoc {
373
374         if ((tx == TX_CALL) || (tx == TX_RB)) {
375             utx.begin();
376         }
377         ARemote a1 = ahome.findByPrimaryKey("a1");
378         // change the relation
379
if (tx == TX_CONT) {
380             a1.assignBInNewTx("b3");
381         } else {
382             a1.assignB("b3");
383         }
384         if (tx == TX_CALL) {
385             utx.commit();
386         } else if (tx == TX_RB) {
387             utx.rollback();
388         }
389         if (tx != TX_RB) {
390             // Verify expected result
391
assertEquals("Bad coherence of relation : b3 expected for a1.retreiveB() found :"+a1.retrieveB(),"b3", a1.retrieveB());
392
393             // undo
394
a1.assignB("b1");
395         }
396         checkIsInitialState();
397     }
398
399     public void testCohAlreadyAssign() throws Exception JavaDoc {
400         _testCohAlreadyAssign(TX_NO);
401     }
402     public void testCohAlreadyAssignTxCall() throws Exception JavaDoc {
403         _testCohAlreadyAssign(TX_CALL);
404     }
405
406     /**
407      * test coherence relation one to one bidirectionnel,
408      * A1.assignB(null)=>A1.retreiveB()==null
409      */

410     public void _testCohSetNull(int tx) throws Exception JavaDoc {
411
412         if ((tx == TX_CALL) || (tx == TX_RB)) {
413             utx.begin();
414         }
415         ARemote a1 = ahome.findByPrimaryKey("a1");
416         // change the relation
417
if (tx == TX_CONT) {
418             a1.assignBInNewTx(null);
419         } else {
420             a1.assignB(null);
421         }
422         if (tx == TX_CALL) {
423             utx.commit();
424         } else if (tx == TX_RB) {
425             utx.rollback();
426         }
427         if (tx != TX_RB) {
428             // Verify expected result
429
assertNull("Bad coherence of relation : null expected for a1.retreiveB() found :"+a1.retrieveB(), a1.retrieveB());
430
431             // undo
432
a1.assignB("b1");
433         }
434         checkIsInitialState();
435     }
436
437     public void testCohSetNull() throws Exception JavaDoc {
438         _testCohSetNull(TX_NO);
439     }
440     public void testCohSetNullTxCall() throws Exception JavaDoc {
441         _testCohSetNull(TX_CALL);
442     }
443
444
445     /**
446      * test coherence relation one to one bidirectionnel,
447      * Bhome.remove(b1)=>B1 removed && A1.retreiveB()==null
448      */

449     public void _testCohRemove(int tx) throws Exception JavaDoc {
450         if (tx == TX_CONT) {
451             // The transaction attribute of the remove method is TX_SUPPORT,
452
// so the transaction cannot be initiate by the container
453
fail("Transaction cannot be initiate by the container for this test");
454         }
455         if ((tx == TX_CALL) || (tx == TX_RB)) {
456             utx.begin();
457         }
458         ARemote a1 = ahome.findByPrimaryKey("a1");
459         try {
460             // remove the bean
461
bhome.remove("b1");
462         } finally {
463             if (tx == TX_CALL) {
464                 utx.commit();
465             } else if (tx == TX_RB) {
466                 utx.rollback();
467             }
468         }
469         if (tx != TX_RB) {
470             // Verify expected result
471
assertNull("Bad coherence of relation : null expected for a1.retreiveB() found :"+a1.retrieveB(), a1.retrieveB());
472             try {
473                 BRemote b1 = bhome.findByPrimaryKey("b1");
474                 fail("BA is not removed");
475             } catch (ObjectNotFoundException JavaDoc e) {
476             }
477
478             // undo
479
bhome.create("b1");
480             a1.assignB("b1");
481         }
482         checkIsInitialState();
483     }
484
485     public void testCohRemoveNoTx() throws Exception JavaDoc {
486         _testCohRemove(TX_NO);
487     }
488     public void testCohRemoveTxCall() throws Exception JavaDoc {
489         _testCohRemove(TX_CALL);
490     }
491     public void testCohRemoveTxRb() throws Exception JavaDoc {
492         _testCohRemove(TX_RB);
493     }
494
495     public void testRemoveCreate() throws Exception JavaDoc {
496         ARemote a1 = ahome.findByPrimaryKey("a1");
497         bhome.remove("b1");
498         BRemote b1 = null;
499         try {
500             b1 = bhome.findByPrimaryKey("b1");
501             fail("BA is not removed");
502         } catch (ObjectNotFoundException JavaDoc e) {
503         }
504         
505         // undo
506
bhome.create("b1");
507
508         utx.begin();
509         try {
510             // remove the bean
511
bhome.remove("b1");
512         } finally {
513             utx.commit();
514         }
515         try {
516             b1 = bhome.findByPrimaryKey("b1");
517             fail("BA is not removed");
518         } catch (ObjectNotFoundException JavaDoc e) {
519         }
520         
521         // undo
522
b1 = bhome.create("b1");
523         a1.assignB("b1");
524
525         // check initial state
526
//ARemote a2 = ahome.findByPrimaryKey("a2");
527
BRemote b2 = bhome.findByPrimaryKey("b2");
528         try {
529             String JavaDoc idB1 = a1.retrieveB();
530         } catch (Exception JavaDoc e) {
531             fail("a1.retrieveB():" + e);
532         }
533
534     }
535
536
537     /**
538      * test coherence relation one to one bidirectionnel,
539      * B1.remove=>B1 removed && A1.retrieveB()==null
540      * Same as _testCohRemove except that the called remove method is on the bean
541      * instead of the home.
542      */

543     public void _testCohBeanRemove(int tx) throws Exception JavaDoc {
544         if (tx == TX_CONT) {
545             // The transaction attribute of the remove method is TX_SUPPORT,
546
// so the transaction cannot be initiate by the container
547
fail("Transaction cannot be initiate by the container for this test");
548         }
549
550         if ((tx == TX_CALL) || (tx == TX_RB)) {
551             utx.begin();
552         }
553         ARemote a1 = ahome.findByPrimaryKey("a1");
554         BRemote b1 = bhome.findByPrimaryKey("b1");
555         try {
556             // remove the bean
557
b1.remove();
558         } finally {
559             if (tx == TX_CALL) {
560                 utx.commit();
561             } else if (tx == TX_RB) {
562                 utx.rollback();
563             }
564         }
565         if (tx != TX_RB) {
566             // Verify expected result
567
assertNull("Bad coherence of relation : null expected for a1.retreiveB() found :"+a1.retrieveB(), a1.retrieveB());
568             boolean not_found=false;
569             try {
570                 b1 = bhome.findByPrimaryKey("b1");
571             } catch (ObjectNotFoundException JavaDoc e) {
572                 not_found=true;
573             }
574             assertTrue("BA is not removed",not_found);
575             // undo
576
bhome.create("b1");
577             a1.assignB("b1");
578         }
579         checkIsInitialState();
580     }
581
582     public void testCohBeanRemoveNoTx() throws Exception JavaDoc {
583         _testCohBeanRemove(TX_NO);
584     }
585     public void testCohBeanRemoveTxCall() throws Exception JavaDoc {
586         _testCohBeanRemove(TX_CALL);
587     }
588     public void testCohBeanRemoveTxRb() throws Exception JavaDoc {
589         _testCohBeanRemove(TX_RB);
590     }
591
592     public static Test suite() {
593         return new TestSuite(F_Relation_oouEC2.class);
594     }
595
596     public static void main (String JavaDoc args[]) {
597         String JavaDoc testtorun = null;
598         // Get args
599
for (int argn = 0; argn < args.length; argn++) {
600             String JavaDoc s_arg = args[argn];
601             Integer JavaDoc i_arg;
602             if (s_arg.equals("-n")) {
603                 testtorun = args[++argn];
604             }
605         }
606         if (testtorun == null) {
607             junit.textui.TestRunner.run(suite());
608         } else {
609             junit.textui.TestRunner.run(new F_Relation_oouEC2(testtorun));
610         }
611     }
612 }
613
614
Popular Tags