KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > clients > entity > G_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: G_Relation_oouEC2.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.oou.AHomeRemote;
35 import org.objectweb.jonas.jtests.beans.relation.oou.ARemote;
36 import org.objectweb.jonas.jtests.beans.relation.oou.BHomeRemote;
37 import org.objectweb.jonas.jtests.beans.relation.oou.BRemote;
38
39 /**
40  * This is an advanced test suite for home interface on entity bean CMP2.
41  */

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

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

164     public void _testBasicSetEmpty(int tx) throws Exception JavaDoc {
165         if ((tx == TX_CALL) || (tx == TX_RB)) {
166             utx.begin();
167         }
168         ARemote a = ahome.findByPrimaryKey("a3");
169         if (tx == TX_CONT) {
170             a.assignBInNewTx("b3");
171         } else {
172             a.assignB("b3");
173         }
174         if (tx == TX_CALL) {
175             utx.commit();
176         } else if (tx == TX_RB) {
177             utx.rollback();
178         }
179         // checking
180
String JavaDoc idB = a.retrieveB();
181         if (tx != TX_RB) {
182             assertEquals("Wrong assign null for relation a3->b3 : ","b3", idB);
183         } else {
184             assertNull("Wrong assign null for relation a3->b3 : ", idB);
185         }
186         // undo
187
if (tx != TX_RB) {
188             a.assignB(null);
189         }
190         checkIsInitialState();
191     }
192
193     public void testBasicSetEmptyTxNo() throws Exception JavaDoc {
194         _testBasicSetEmpty(TX_NO);
195     }
196     public void testBasicSetEmptyTxCall() throws Exception JavaDoc {
197         _testBasicSetEmpty(TX_CALL);
198     }
199     public void testBasicSetEmptyTxCont() throws Exception JavaDoc {
200         _testBasicSetEmpty(TX_CONT);
201     }
202    
203    
204  
205     public static Test suite() {
206         return new TestSuite(G_Relation_oouEC2.class);
207     }
208
209     public static void main (String JavaDoc args[]) {
210         String JavaDoc testtorun = null;
211         // Get args
212
for (int argn = 0; argn < args.length; argn++) {
213             String JavaDoc s_arg = args[argn];
214             Integer JavaDoc i_arg;
215             if (s_arg.equals("-n")) {
216                 testtorun = args[++argn];
217             }
218         }
219         if (testtorun == null) {
220             junit.textui.TestRunner.run(suite());
221         } else {
222             junit.textui.TestRunner.run(new G_Relation_oouEC2(testtorun));
223         }
224     }
225 }
226
227
Popular Tags