KickJava   Java API By Example, From Geeks To Geeks.

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


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  * $Id: F_MultiRelation.java,v 1.2 2005/02/09 09:22:04 durieuxp Exp $
22  * --------------------------------------------------------------------------
23  */

24
25 package org.objectweb.jonas.jtests.clients.entity;
26
27 import java.util.Collection JavaDoc;
28 import java.util.Date JavaDoc;
29 import java.util.Iterator JavaDoc;
30
31 import javax.ejb.FinderException JavaDoc;
32 import javax.naming.NamingException JavaDoc;
33 import javax.rmi.PortableRemoteObject JavaDoc;
34
35 import junit.framework.Test;
36 import junit.framework.TestSuite;
37
38 import org.objectweb.jonas.jtests.beans.relation.cascade.AddressDO;
39 import org.objectweb.jonas.jtests.beans.relation.cascade.AddressHR;
40 import org.objectweb.jonas.jtests.beans.relation.cascade.AddressR;
41 import org.objectweb.jonas.jtests.beans.relation.cascade.CreditCardHR;
42 import org.objectweb.jonas.jtests.beans.relation.cascade.CustomerHR;
43 import org.objectweb.jonas.jtests.beans.relation.cascade.CustomerR;
44 import org.objectweb.jonas.jtests.beans.relation.cascade.Name;
45 import org.objectweb.jonas.jtests.beans.relation.cascade.PhoneHR;
46 import org.objectweb.jonas.jtests.beans.relation.cascade.CarHR;
47 import org.objectweb.jonas.jtests.util.JTestCase;
48
49 /**
50  * This is a test suite on multiple threads accessing relationship beans
51  * @author Philippe Durieux
52  */

53 public class F_MultiRelation extends JTestCase {
54
55     private static CustomerHR customerhome = null;
56     private static CreditCardHR creditcardhome = null;
57     private static AddressHR addresshome = null;
58     private static PhoneHR phonehome = null;
59     private static CarHR carhome = null;
60
61     public F_MultiRelation(String JavaDoc name) {
62         super(name);
63     }
64
65     protected static boolean isInit = false;
66     protected boolean threadfail = false;
67
68     protected void setUp() {
69         super.setUp();
70         if (!isInit) {
71             useBeans("cascade", false);
72             try {
73                 customerhome = (CustomerHR) PortableRemoteObject.narrow(ictx.lookup("cascadeCustomerHomeRemote"),
74                                                                         CustomerHR.class);
75                 creditcardhome = (CreditCardHR) PortableRemoteObject.narrow(ictx.lookup("cascadeCreditCardHomeRemote"),
76                                                                             CreditCardHR.class);
77                 addresshome = (AddressHR) PortableRemoteObject.narrow(ictx.lookup("cascadeAddressHomeRemote"),
78                                                                       AddressHR.class);
79                 phonehome = (PhoneHR) PortableRemoteObject.narrow(ictx.lookup("cascadePhoneHomeRemote"),
80                                                                   PhoneHR.class);
81                 carhome = (CarHR) PortableRemoteObject.narrow(ictx.lookup("cascadeCarHomeRemote"),
82                                                                   CarHR.class);
83             } catch (NamingException JavaDoc e) {
84                 fail("Cannot get bean home: " + e.getMessage());
85             }
86             isInit = true;
87         }
88     }
89
90     private CustomerR createFullCustomer(int pk, String JavaDoc lname, String JavaDoc cc) throws Exception JavaDoc {
91         // create new customer
92
CustomerR customer = customerhome.create(new Integer JavaDoc(pk));
93         Name name = new Name(lname, "Jean");
94         customer.setName(name);
95         // set its credit card
96
customer.setCreditCard(new Date JavaDoc(), cc, lname);
97         // set its address
98
String JavaDoc zip = "38170";
99         customer.setAddress("rue des fleurs", "Seyssinet", "France", zip);
100         // add phones
101
customer.addPhoneNumber("1111", (byte) 1);
102         customer.addPhoneNumber("2222", (byte) 2);
103         customer.addPhoneNumber("3333", (byte) 3);
104         return customer;
105     }
106     
107     private void removeFullCustomer(int pk) throws Exception JavaDoc {
108         CustomerR customer = customerhome.findByPrimaryKey(new Integer JavaDoc(pk));
109         customer.remove();
110     }
111     
112     private void runThreads(int threads, int pkmin, int pkmax) throws Exception JavaDoc {
113         // Create and start all threads
114
Ithread[] t_thr = new Ithread[threads];
115         for (int i = 0; i < threads; i++) {
116             t_thr[i] = new Ithread(pkmin, pkmax);
117             t_thr[i].start();
118         }
119
120         // Wait end of all threads
121
for (int p = 0; p < threads; p++) {
122             t_thr[p].join();
123         }
124
125         // Check if all threads run ok
126
if (threadfail) {
127             throw new Error JavaDoc("thread error");
128         }
129     }
130     
131     public void testBasicCreateRemove() throws Exception JavaDoc {
132         int pk = 7;
133         createFullCustomer(pk, "Galtier", "65423");
134         removeFullCustomer(pk);
135     }
136
137     public void testCreateRemoveTwice() throws Exception JavaDoc {
138         int pk = 8;
139         createFullCustomer(pk, "Galtier", "65423");
140         removeFullCustomer(pk);
141         createFullCustomer(pk, "Galtier", "65423");
142         removeFullCustomer(pk);
143     }
144     
145     public void testManyThreads() throws Exception JavaDoc {
146         stopTxAt(200);
147         int pkmin = 80;
148         int pkmax = 100;
149         for (int pk = pkmin; pk < pkmax; pk++) {
150             CustomerR customer = createFullCustomer(pk, "Nom" + pk, "65423" + pk);
151             // add cars
152
String JavaDoc carnb = "car-pk" + pk;
153             customer.addCar("1111" + pk, (byte) 1);
154             customer.addCar(carnb, (byte) 2);
155             customer.addCar("1131" + pk, (byte) 3);
156             // crash
157
customer.accident(carnb, carnb + "-inv");
158         }
159         runThreads(10, pkmin, pkmax);
160         for (int pk = pkmin; pk < pkmax; pk++) {
161             removeFullCustomer(pk);
162         }
163     }
164     
165     protected boolean initStateOK() throws Exception JavaDoc {
166         return true;
167     }
168
169     
170     public static Test suite() {
171         return new TestSuite(F_MultiRelation.class);
172     }
173
174     public static void main (String JavaDoc args[]) {
175         String JavaDoc testtorun = null;
176         // Get args
177
for (int argn = 0; argn < args.length; argn++) {
178             String JavaDoc sarg = args[argn];
179             if (sarg.equals("-n")) {
180                 testtorun = args[++argn];
181             }
182         }
183         if (testtorun == null) {
184             junit.textui.TestRunner.run(suite());
185         } else {
186             junit.textui.TestRunner.run(new F_MultiRelation(testtorun));
187         }
188     }
189     
190     class Ithread extends Thread JavaDoc {
191         int pkmin;
192         int pkmax;
193         
194         public Ithread(int pkmin, int pkmax) {
195             this.pkmin = pkmin;
196             this.pkmax = pkmax;
197         }
198
199         public void run() {
200             try {
201                 for (int pk = pkmin; pk < pkmax; pk++) {
202                     CustomerR customer = customerhome.findByPrimaryKey(new Integer JavaDoc(pk));
203                     yield();
204                     AddressDO addr = customer.getAddress();
205                 }
206             } catch (Exception JavaDoc e) {
207                 threadfail = true;
208             }
209         }
210     }
211 }
212
Popular Tags