KickJava   Java API By Example, From Geeks To Geeks.

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


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_s3pkcompEC2.java,v 1.5 2004/03/19 11:57:15 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.clients.entity;
27
28 import java.util.ArrayList JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Enumeration JavaDoc;
31 import java.util.Hashtable JavaDoc;
32 import java.util.Iterator JavaDoc;
33
34 import javax.naming.NamingException JavaDoc;
35 import javax.rmi.PortableRemoteObject JavaDoc;
36
37 import junit.framework.Test;
38 import junit.framework.TestSuite;
39
40 import org.objectweb.jonas.jtests.beans.relation.s3pkcomp.AHomeRemote;
41 import org.objectweb.jonas.jtests.beans.relation.s3pkcomp.ARemote;
42 import org.objectweb.jonas.jtests.beans.relation.s3pkcomp.BHomeRemote;
43 import org.objectweb.jonas.jtests.beans.relation.s3pkcomp.Pk;
44
45
46 /**
47  * For testing one-to-many unidirectional relationships
48  * this test uses beans s3pkcomp/*
49  * @author Helene Joanin
50  */

51 public class F_Relation_s3pkcompEC2 extends A_Cmp2Util {
52
53     private static String JavaDoc BEAN_HOME_A = "relation_s3pkcomp_AHome";
54     protected static AHomeRemote ahome = null;
55     private static String JavaDoc BEAN_HOME_B = "relation_s3pkcomp_BHome";
56     protected static BHomeRemote bhome = null;
57
58     static Hashtable JavaDoc tbRelationA2B = new Hashtable JavaDoc();
59
60     static {
61         tbRelationA2B.put(new Integer JavaDoc(0), new int[]{});
62         tbRelationA2B.put(new Integer JavaDoc(1), new int[]{1001, 1002, 1003});
63         tbRelationA2B.put(new Integer JavaDoc(2), new int[]{2001, 2002, 2003});
64         tbRelationA2B.put(new Integer JavaDoc(3), new int[]{3001});
65         // Translate the int[] to a Collection of Pk
66
for (Enumeration JavaDoc ea = tbRelationA2B.keys(); ea.hasMoreElements();) {
67             Integer JavaDoc aid = (Integer JavaDoc) (ea.nextElement());
68             int[] tb = (int[]) tbRelationA2B.get(aid);
69             ArrayList JavaDoc col = new ArrayList JavaDoc(tb.length);
70             for (int i = 0; i < tb.length; i++) {
71                 col.add(new Pk(tb[i]));
72             }
73             tbRelationA2B.put(aid, col);
74         }
75     }
76
77     public F_Relation_s3pkcompEC2(String JavaDoc name) {
78         super(name);
79     debug("F_Relation_s3pkcompEC2("+name+")");
80     }
81
82     protected static boolean isInit = false;
83
84     protected void setUp() {
85         super.setUp();
86     boolean ok = false;
87     int nbtry = 0;
88     while (!ok && nbtry < 3) {
89         if (!isInit) {
90         // load bean if not loaded yet
91
useBeans("s3pkcomp", false);
92         try {
93             ahome = (AHomeRemote) PortableRemoteObject.narrow(ictx.lookup(BEAN_HOME_A),
94                                       AHomeRemote.class);
95             bhome = (BHomeRemote) PortableRemoteObject.narrow(ictx.lookup(BEAN_HOME_B),
96                                       BHomeRemote.class);
97         } catch (NamingException JavaDoc e) {
98             fail("Cannot get bean home: " + e.getMessage());
99         }
100         // check if tables have been initialized
101
try {
102             ahome.findByPrimaryKey(new Pk(0));
103         } catch (Exception JavaDoc e) {
104             try {
105             utx.begin();
106             bhome.create(0);
107             bhome.create(1001);
108             bhome.create(1002);
109             bhome.create(1003);
110             bhome.create(2001);
111             bhome.create(2002);
112             bhome.create(2003);
113             bhome.create(3001);
114             ARemote a0 = ahome.create(0);
115             ARemote a1 = ahome.create(1);
116             ARemote a2 = ahome.create(2);
117             ARemote a3 = ahome.create(3);
118             a1.assignB((Collection JavaDoc)tbRelationA2B.get(new Integer JavaDoc(1)));
119             a2.assignB((Collection JavaDoc)tbRelationA2B.get(new Integer JavaDoc(2)));
120             a3.assignB((Collection JavaDoc)tbRelationA2B.get(new Integer JavaDoc(3)));
121             } catch (Exception JavaDoc i) {
122             fail("InitialState creation problem:: "+i);
123             } finally {
124             try {
125                 utx.commit();
126             } catch (Exception JavaDoc ii) {
127             }
128             }
129         }
130         isInit = true;
131         }
132         // Check that all is OK. Sometimes, a test has failed and has corrupted
133
// the bean state in the database. We must unload and reload the bean then.
134
nbtry++;
135         try {
136         if (initStateOK()) {
137             ok = true;
138         }
139         } catch (Exception JavaDoc e) {
140         }
141         if (!ok) {
142         isInit = false;
143         unloadBeans("s3pkcomp");
144         }
145     }
146     }
147
148     /*
149      * Check that we are in the same state as after the tables creation for thoses beans A and B
150      * (ie if it is the initial state)
151      */

152     boolean initStateOK() throws Exception JavaDoc {
153         boolean isOk = true;
154         msgerror = new StringBuffer JavaDoc();
155         for (Enumeration JavaDoc ea = tbRelationA2B.keys(); ea.hasMoreElements();) {
156             Integer JavaDoc aid = (Integer JavaDoc) (ea.nextElement());
157             ARemote a = ahome.findByPrimaryKey(new Pk(aid));
158             Collection JavaDoc colActual = a.retrieveB();
159             ArrayList JavaDoc colExpected = (ArrayList JavaDoc) (tbRelationA2B.get(aid));
160         debug("Relation for " + aid + " : expected:" + colExpected + ", found:" + colActual );
161             if (!isCollectionEqual(colExpected, colActual)) {
162                 isOk = false;
163                 msgerror = msgerror.append("Wrong relation for " + aid
164                     + " (expected:" + colExpected
165                     + ", found:" + colActual + ")");
166             }
167         }
168     return isOk;
169     }
170
171
172     /**
173      * Not really a test, just to verify the checkIsInitialState() method.
174      */

175     public void testEmpty() throws Exception JavaDoc {
176     checkIsInitialState();
177     }
178
179     /**
180      * Test the calling of the ahome.findAll() method
181      */

182     public void _testFindAllA(int tx) throws Exception JavaDoc {
183     if (tx == TX_CONT) {
184         // The transaction attribute of the findAll method is TX_SUPPORT,
185
// so the transaction cannot be initiate by the container
186
fail("Transaction cannot be initiate by the container for this test");
187     }
188         if ((tx == TX_CALL) || (tx == TX_RB)) {
189             utx.begin();
190         }
191     Collection JavaDoc cA = ahome.findAll();
192         if (tx == TX_CALL) {
193             utx.commit();
194         } else if (tx == TX_RB) {
195             utx.rollback();
196         }
197     // Check
198
int nbA = 0;
199     for (Iterator JavaDoc iA = cA.iterator(); iA.hasNext(); iA.next()) {
200         nbA++;
201     }
202     assertEquals("Wrong number for beans A for findAll: ", tbRelationA2B.size(), nbA);
203     }
204     
205     public void testFindAllATxNo() throws Exception JavaDoc {
206     _testFindAllA(TX_NO);
207     }
208
209     public void testFindAllATxCall() throws Exception JavaDoc {
210     _testFindAllA(TX_CALL);
211     }
212
213     public void testFindAllATxRb() throws Exception JavaDoc {
214     _testFindAllA(TX_RB);
215     }
216
217
218     public static Test suite() {
219         return new TestSuite(F_Relation_s3pkcompEC2.class);
220     }
221
222     public static void main(String JavaDoc args[]) {
223         String JavaDoc testtorun = null;
224         // Get args
225
for (int argn = 0; argn < args.length; argn++) {
226             String JavaDoc s_arg = args[argn];
227             Integer JavaDoc i_arg;
228             if (s_arg.equals("-n")) {
229                 testtorun = args[++argn];
230             }
231         }
232         if (testtorun == null) {
233             junit.textui.TestRunner.run(suite());
234         } else {
235             junit.textui.TestRunner.run(new F_Relation_s3pkcompEC2(testtorun));
236         }
237     }
238
239 }
240
Popular Tags