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 F_Cascade 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_Cascade(String name) { 62 super(name); 63 } 64 65 protected static boolean isInit = false; 66 67 protected void setUp() { 68 super.setUp(); 69 if (!isInit) { 70 useBeans("cascade", false); 71 try { 72 customerhome = (CustomerHR) PortableRemoteObject.narrow(ictx.lookup("cascadeCustomerHomeRemote"), 73 CustomerHR.class); 74 creditcardhome = (CreditCardHR) PortableRemoteObject.narrow(ictx.lookup("cascadeCreditCardHomeRemote"), 75 CreditCardHR.class); 76 addresshome = (AddressHR) PortableRemoteObject.narrow(ictx.lookup("cascadeAddressHomeRemote"), 77 AddressHR.class); 78 phonehome = (PhoneHR) PortableRemoteObject.narrow(ictx.lookup("cascadePhoneHomeRemote"), 79 PhoneHR.class); 80 carhome = (CarHR) PortableRemoteObject.narrow(ictx.lookup("cascadeCarHomeRemote"), 81 CarHR.class); 82 } catch (NamingException e) { 83 fail("Cannot get bean home: " + e.getMessage()); 84 } 85 isInit = true; 86 } 87 } 88 89 93 public void testCascadeDeleteCC() throws Exception { 94 CustomerR customer = customerhome.create(new Integer (15)); 96 String lastname = "Dupont"; 97 Name name = new Name(lastname, "Jean"); 98 customer.setName(name); 99 customer.setCreditCard(new Date (), "154563", lastname); 101 creditcardhome.findByName(lastname); 103 customer.remove(); 105 try { 107 creditcardhome.findByName(lastname); 108 fail("credit card has not been removed by cascade delete"); 109 } catch (FinderException e) { 110 } 111 } 112 113 117 public void testCascadeDeleteAd() throws Exception { 118 CustomerR customer = customerhome.create(new Integer (16)); 120 String lastname = "Durand"; 121 Name name = new Name(lastname, "Jacques"); 122 customer.setName(name); 123 String zip = "38170"; 125 customer.setAddress("rue Quirole", "Seyssinet", "France", zip); 126 Collection coll = addresshome.findByZip(zip); 128 assertEquals("wrong number of Addresses", 1, coll.size()); 129 customer.remove(); 131 try { 133 coll = addresshome.findByZip(zip); 134 assertEquals("Address have not been removed", 0, coll.size()); 135 } catch (FinderException e) { 136 } 137 } 138 139 143 public void testCascadeDeletePhs() throws Exception { 144 CustomerR customer = customerhome.create(new Integer (17)); 146 String lastname = "Bertrand"; 147 Name name = new Name(lastname, "Jules"); 148 customer.setName(name); 149 customer.addPhoneNumber("1111", (byte) 1); 151 customer.addPhoneNumber("2222", (byte) 2); 152 customer.addPhoneNumber("3333", (byte) 3); 153 Collection coll = phonehome.findByName(lastname); 155 assertEquals("wrong number of phones", 3, coll.size()); 156 customer.remove(); 158 coll = phonehome.findByName(lastname); 160 assertEquals("phones have not been removed", 0, coll.size()); 161 } 162 163 167 public void testCascadeDeleteCars() throws Exception { 168 CustomerR customer = customerhome.create(new Integer (17)); 170 String lastname = "Bertrand"; 171 Name name = new Name(lastname, "Jules"); 172 customer.setName(name); 173 customer.addCar("1111", (byte) 1); 175 customer.addCar("2222", (byte) 2); 176 customer.addCar("3333", (byte) 3); 177 Collection coll = carhome.findByName(lastname); 179 assertEquals("wrong number of cars", 3, coll.size()); 180 customer.remove(); 182 coll = carhome.findByName(lastname); 184 assertEquals("cars have not been removed", 0, coll.size()); 185 } 186 187 public void testCrash1() throws Exception { 188 CustomerR customer = customerhome.create(new Integer (17)); 190 Name name = new Name("Bertrand", "Jules"); 191 customer.setName(name); 192 customer.addCar("1111", (byte) 1); 194 customer.accident("1111", "111101"); 196 customer.remove(); 198 } 199 200 203 public void testCreateInPostCreate() throws Exception { 204 String zip = "30010"; 205 AddressDO addr = new AddressDO("St1", "C1", "E1", zip); 206 CustomerR customer = customerhome.createWithAddress(new Integer (18), addr); 207 Collection coll = addresshome.findByZip(zip); 209 assertEquals("Address not created", 1, coll.size()); 210 Iterator i = coll.iterator(); 211 AddressR a = (AddressR) PortableRemoteObject.narrow(i.next(), AddressR.class); 212 Integer cid = a.getCustomerId(); 213 assertEquals("Coherence not done", customer.getId().intValue(), cid.intValue()); 214 customer.remove(); 216 } 217 218 protected boolean initStateOK() throws Exception { 219 return true; 220 } 221 222 223 public static Test suite() { 224 return new TestSuite(F_Cascade.class); 225 } 226 227 public static void main (String args[]) { 228 String testtorun = null; 229 for (int argn = 0; argn < args.length; argn++) { 231 String sarg = args[argn]; 232 if (sarg.equals("-n")) { 233 testtorun = args[++argn]; 234 } 235 } 236 if (testtorun == null) { 237 junit.textui.TestRunner.run(suite()); 238 } else { 239 junit.textui.TestRunner.run(new F_Cascade(testtorun)); 240 } 241 } 242 } 243 | Popular Tags |