1 24 25 package org.objectweb.jonas.jtests.clients.entity; 26 27 import java.util.Collection ; 28 import java.util.Date ; 29 import java.util.Iterator ; 30 31 import javax.ejb.FinderException ; 32 import javax.naming.NamingException ; 33 import javax.rmi.PortableRemoteObject ; 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 53 public class G_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 G_MultiRelation(String 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 e) { 84 fail("Cannot get bean home: " + e.getMessage()); 85 } 86 isInit = true; 87 } 88 } 89 90 private CustomerR createFullCustomer(int pk, String lname, String cc) throws Exception { 91 CustomerR customer = customerhome.create(new Integer (pk)); 93 Name name = new Name(lname, "Jean"); 94 customer.setName(name); 95 customer.setCreditCard(new Date (), cc, lname); 97 String zip = "38170"; 99 customer.setAddress("rue des fleurs", "Seyssinet", "France", zip); 100 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 { 108 CustomerR customer = customerhome.findByPrimaryKey(new Integer (pk)); 109 customer.remove(); 110 } 111 112 private void runThreads(int threads, int pkmin, int pkmax) throws Exception { 113 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 for (int p = 0; p < threads; p++) { 122 t_thr[p].join(); 123 } 124 125 if (threadfail) { 127 throw new Error ("thread error"); 128 } 129 } 130 131 public void testBasicCreateRemove() throws Exception { 132 int pk = 7; 133 createFullCustomer(pk, "Galtier", "65423"); 134 removeFullCustomer(pk); 135 } 136 137 public void testCreateRemoveTwice() throws Exception { 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 { 146 stopTxAt(200); 147 int pkmin = 100; 148 int pkmax = 300; 149 for (int pk = pkmin; pk < pkmax; pk++) { 150 CustomerR customer = createFullCustomer(pk, "Nom" + pk, "65423" + pk); 151 String 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 customer.accident(carnb, carnb + "-inv"); 158 } 159 runThreads(80, pkmin, pkmax); 160 for (int pk = pkmin; pk < pkmax; pk++) { 161 removeFullCustomer(pk); 162 } 163 } 164 165 protected boolean initStateOK() throws Exception { 166 return true; 167 } 168 169 170 public static Test suite() { 171 return new TestSuite(G_MultiRelation.class); 172 } 173 174 public static void main (String args[]) { 175 String testtorun = null; 176 for (int argn = 0; argn < args.length; argn++) { 178 String 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 G_MultiRelation(testtorun)); 187 } 188 } 189 190 class Ithread extends Thread { 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 (pk)); 203 yield(); 204 AddressDO addr = customer.getAddress(); 205 } 206 } catch (Exception e) { 207 threadfail = true; 208 } 209 } 210 } 211 } 212 | Popular Tags |