001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: * --------------------------------------------------------------------------
021: * $Id: G_MultiRelation.java 7533 2005-10-19 15:55:05Z durieuxp $
022: * --------------------------------------------------------------------------
023: */
024:
025: package org.objectweb.jonas.jtests.clients.entity;
026:
027: import java.util.Collection;
028: import java.util.Date;
029: import java.util.Iterator;
030:
031: import javax.ejb.FinderException;
032: import javax.naming.NamingException;
033: import javax.rmi.PortableRemoteObject;
034:
035: import junit.framework.Test;
036: import junit.framework.TestSuite;
037:
038: import org.objectweb.jonas.jtests.beans.relation.cascade.AddressDO;
039: import org.objectweb.jonas.jtests.beans.relation.cascade.AddressHR;
040: import org.objectweb.jonas.jtests.beans.relation.cascade.AddressR;
041: import org.objectweb.jonas.jtests.beans.relation.cascade.CreditCardHR;
042: import org.objectweb.jonas.jtests.beans.relation.cascade.CustomerHR;
043: import org.objectweb.jonas.jtests.beans.relation.cascade.CustomerR;
044: import org.objectweb.jonas.jtests.beans.relation.cascade.Name;
045: import org.objectweb.jonas.jtests.beans.relation.cascade.PhoneHR;
046: import org.objectweb.jonas.jtests.beans.relation.cascade.CarHR;
047: import org.objectweb.jonas.jtests.util.JTestCase;
048:
049: /**
050: * This is a test suite on multiple threads accessing relationship beans
051: * @author Philippe Durieux
052: */
053: public class G_MultiRelation extends JTestCase {
054:
055: private static CustomerHR customerhome = null;
056: private static CreditCardHR creditcardhome = null;
057: private static AddressHR addresshome = null;
058: private static PhoneHR phonehome = null;
059: private static CarHR carhome = null;
060:
061: public G_MultiRelation(String name) {
062: super (name);
063: }
064:
065: protected boolean isInit = false;
066: protected boolean threadfail = false;
067:
068: protected void setUp() {
069: super .setUp();
070: if (!isInit) {
071: useBeans("cascade", false);
072: try {
073: customerhome = (CustomerHR) PortableRemoteObject
074: .narrow(ictx
075: .lookup("cascadeCustomerHomeRemote"),
076: CustomerHR.class);
077: creditcardhome = (CreditCardHR) PortableRemoteObject
078: .narrow(ictx
079: .lookup("cascadeCreditCardHomeRemote"),
080: CreditCardHR.class);
081: addresshome = (AddressHR) PortableRemoteObject.narrow(
082: ictx.lookup("cascadeAddressHomeRemote"),
083: AddressHR.class);
084: phonehome = (PhoneHR) PortableRemoteObject.narrow(ictx
085: .lookup("cascadePhoneHomeRemote"),
086: PhoneHR.class);
087: carhome = (CarHR) PortableRemoteObject.narrow(ictx
088: .lookup("cascadeCarHomeRemote"), CarHR.class);
089: } catch (NamingException e) {
090: fail("Cannot get bean home: " + e.getMessage());
091: }
092: isInit = true;
093: }
094: }
095:
096: private CustomerR createFullCustomer(int pk, String lname, String cc)
097: throws Exception {
098: // create new customer
099: CustomerR customer = customerhome.create(new Integer(pk));
100: Name name = new Name(lname, "Jean");
101: customer.setName(name);
102: // set its credit card
103: customer.setCreditCard(new Date(), cc, lname);
104: // set its address
105: String zip = "38170";
106: customer.setAddress("rue des fleurs", "Seyssinet", "France",
107: zip);
108: // add phones
109: customer.addPhoneNumber("1111", (byte) 1);
110: customer.addPhoneNumber("2222", (byte) 2);
111: customer.addPhoneNumber("3333", (byte) 3);
112: return customer;
113: }
114:
115: private void removeFullCustomer(int pk) throws Exception {
116: CustomerR customer = customerhome.findByPrimaryKey(new Integer(
117: pk));
118: customer.remove();
119: }
120:
121: private void runThreads(int threads, int pkmin, int pkmax)
122: throws Exception {
123: // Create and start all threads
124: Ithread[] t_thr = new Ithread[threads];
125: for (int i = 0; i < threads; i++) {
126: t_thr[i] = new Ithread(pkmin, pkmax);
127: t_thr[i].start();
128: }
129:
130: // Wait end of all threads
131: for (int p = 0; p < threads; p++) {
132: t_thr[p].join();
133: }
134:
135: // Check if all threads run ok
136: if (threadfail) {
137: throw new Error("thread error");
138: }
139: }
140:
141: public void testBasicCreateRemove() throws Exception {
142: int pk = 7;
143: createFullCustomer(pk, "Galtier", "65423");
144: removeFullCustomer(pk);
145: }
146:
147: public void testCreateRemoveTwice() throws Exception {
148: int pk = 8;
149: createFullCustomer(pk, "Galtier", "65423");
150: removeFullCustomer(pk);
151: createFullCustomer(pk, "Galtier", "65423");
152: removeFullCustomer(pk);
153: }
154:
155: public void testManyThreads() throws Exception {
156: stopTxAt(200);
157: int pkmin = 100;
158: int pkmax = 300;
159: for (int pk = pkmin; pk < pkmax; pk++) {
160: CustomerR customer = createFullCustomer(pk, "Nom" + pk,
161: "65423" + pk);
162: // add cars
163: String carnb = "car-pk" + pk;
164: customer.addCar("1111" + pk, (byte) 1);
165: customer.addCar(carnb, (byte) 2);
166: customer.addCar("1131" + pk, (byte) 3);
167: // crash
168: customer.accident(carnb, carnb + "-inv");
169: }
170: runThreads(80, pkmin, pkmax);
171: for (int pk = pkmin; pk < pkmax; pk++) {
172: removeFullCustomer(pk);
173: }
174: }
175:
176: protected boolean initStateOK() throws Exception {
177: return true;
178: }
179:
180: public static Test suite() {
181: return new TestSuite(G_MultiRelation.class);
182: }
183:
184: public static void main(String args[]) {
185: String testtorun = null;
186: // Get args
187: for (int argn = 0; argn < args.length; argn++) {
188: String sarg = args[argn];
189: if (sarg.equals("-n")) {
190: testtorun = args[++argn];
191: }
192: }
193: if (testtorun == null) {
194: junit.textui.TestRunner.run(suite());
195: } else {
196: junit.textui.TestRunner.run(new G_MultiRelation(testtorun));
197: }
198: }
199:
200: class Ithread extends Thread {
201: int pkmin;
202: int pkmax;
203:
204: public Ithread(int pkmin, int pkmax) {
205: this .pkmin = pkmin;
206: this .pkmax = pkmax;
207: }
208:
209: public void run() {
210: try {
211: for (int pk = pkmin; pk < pkmax; pk++) {
212: CustomerR customer = customerhome
213: .findByPrimaryKey(new Integer(pk));
214: yield();
215: AddressDO addr = customer.getAddress();
216: }
217: } catch (Exception e) {
218: threadfail = true;
219: }
220: }
221: }
222: }
|