001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2005 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: F_Cascade.java 10113 2007-03-28 13:16:40Z durieuxp $
022: * --------------------------------------------------------------------------
023: */package org.objectweb.jonas.jtests.clients.entity;
024:
025: import java.util.Collection;
026: import java.util.Date;
027: import java.util.Iterator;
028:
029: import javax.ejb.FinderException;
030: import javax.naming.NamingException;
031: import javax.rmi.PortableRemoteObject;
032:
033: import junit.framework.Test;
034: import junit.framework.TestSuite;
035:
036: import org.objectweb.jonas.jtests.beans.relation.cascade.AddressBean;
037: import org.objectweb.jonas.jtests.beans.relation.cascade.AddressDO;
038: import org.objectweb.jonas.jtests.beans.relation.cascade.AddressHR;
039: import org.objectweb.jonas.jtests.beans.relation.cascade.AddressR;
040: import org.objectweb.jonas.jtests.beans.relation.cascade.CreditCardHR;
041: import org.objectweb.jonas.jtests.beans.relation.cascade.CustomerHR;
042: import org.objectweb.jonas.jtests.beans.relation.cascade.CustomerR;
043: import org.objectweb.jonas.jtests.beans.relation.cascade.Name;
044: import org.objectweb.jonas.jtests.beans.relation.cascade.PhoneHR;
045: import org.objectweb.jonas.jtests.beans.relation.cascade.CarHR;
046: import org.objectweb.jonas.jtests.util.JTestCase;
047:
048: /**
049: * This is a test suite on cascade-delete facility
050: * @author Philippe Durieux
051: */
052: public class F_Cascade extends JTestCase {
053:
054: private static CustomerHR customerhome = null;
055: private static CreditCardHR creditcardhome = null;
056: private static AddressHR addresshome = null;
057: private static PhoneHR phonehome = null;
058: private static CarHR carhome = null;
059:
060: public F_Cascade(String name) {
061: super (name);
062: }
063:
064: protected boolean isInit = false;
065:
066: protected void setUp() {
067: super .setUp();
068: if (!isInit) {
069: useBeans("cascade", false);
070: try {
071: customerhome = (CustomerHR) PortableRemoteObject
072: .narrow(ictx
073: .lookup("cascadeCustomerHomeRemote"),
074: CustomerHR.class);
075: creditcardhome = (CreditCardHR) PortableRemoteObject
076: .narrow(ictx
077: .lookup("cascadeCreditCardHomeRemote"),
078: CreditCardHR.class);
079: addresshome = (AddressHR) PortableRemoteObject.narrow(
080: ictx.lookup("cascadeAddressHomeRemote"),
081: AddressHR.class);
082: phonehome = (PhoneHR) PortableRemoteObject.narrow(ictx
083: .lookup("cascadePhoneHomeRemote"),
084: PhoneHR.class);
085: carhome = (CarHR) PortableRemoteObject.narrow(ictx
086: .lookup("cascadeCarHomeRemote"), CarHR.class);
087: } catch (NamingException e) {
088: fail("Cannot get bean home: " + e.getMessage());
089: }
090: isInit = true;
091: }
092: }
093:
094: /**
095: * test cascade delete in case of a relation one-to-one
096: * We use the bean customer and its credit card.
097: */
098: public void testCascadeDeleteCC() throws Exception {
099: // create new customer
100: CustomerR customer = customerhome.create(new Integer(15));
101: String lastname = "Dupont";
102: Name name = new Name(lastname, "Jean");
103: customer.setName(name);
104: // set its credit card
105: customer.setCreditCard(new Date(), "154563", lastname);
106: // check we can retrieve it
107: creditcardhome.findByName(lastname);
108: // remove customer
109: customer.remove();
110: // check its credit card has been removed
111: try {
112: creditcardhome.findByName(lastname);
113: fail("credit card has not been removed by cascade delete");
114: } catch (FinderException e) {
115: }
116: }
117:
118: /**
119: * test cascade delete in case of a relation one-to-one
120: * We use the bean customer and its address.
121: */
122: public void testCascadeDeleteAd() throws Exception {
123: // create new customer
124: CustomerR customer = customerhome.create(new Integer(16));
125: String lastname = "Durand";
126: Name name = new Name(lastname, "Jacques");
127: customer.setName(name);
128: // set its address
129: String zip = "38170";
130: customer.setAddress("rue Quirole", "Seyssinet", "France", zip);
131: // check we can retrieve it
132: Collection coll = addresshome.findByZip(zip);
133: assertEquals("wrong number of Addresses", 1, coll.size());
134: // remove customer
135: customer.remove();
136: // check its address has been removed
137: try {
138: coll = addresshome.findByZip(zip);
139: assertEquals("Address have not been removed", 0, coll
140: .size());
141: } catch (FinderException e) {
142: }
143: }
144:
145: /**
146: * test cascade delete in case of a relation one-to-many
147: * We use the bean customer and its phones.
148: */
149: public void testCascadeDeletePhs() throws Exception {
150: // create new customer
151: CustomerR customer = customerhome.create(new Integer(17));
152: String lastname = "Bertrand";
153: Name name = new Name(lastname, "Jules");
154: customer.setName(name);
155: // add phones
156: customer.addPhoneNumber("1111", (byte) 1);
157: customer.addPhoneNumber("2222", (byte) 2);
158: customer.addPhoneNumber("3333", (byte) 3);
159: // check we can retrieve them
160: Collection coll = phonehome.findByName(lastname);
161: assertEquals("wrong number of phones", 3, coll.size());
162: // remove customer
163: customer.remove();
164: // check its phones have been removed
165: coll = phonehome.findByName(lastname);
166: assertEquals("phones have not been removed", 0, coll.size());
167: }
168:
169: /**
170: * test Cascade delete with Address + PhoneMumbers
171: */
172: public void testCDAdPh() throws Exception {
173: // create new customer
174: CustomerR customer = customerhome.create(new Integer(17));
175: String lastname = "Bertrand";
176: Name name = new Name(lastname, "Jules");
177: customer.setName(name);
178: // add phones
179: customer.addPhoneNumber("1111", (byte) 1);
180: customer.addPhoneNumber("2222", (byte) 2);
181: customer.addPhoneNumber("3333", (byte) 3);
182: // check we can retrieve them
183: Collection coll = phonehome.findByName(lastname);
184: assertEquals("wrong number of phones", 3, coll.size());
185: // set its address
186: String zip = "38170";
187: customer.setAddress("rue Quirole", "Seyssinet", "France", zip);
188: // check we can retrieve it
189: coll = addresshome.findByZip(zip);
190: assertEquals("wrong number of Addresses", 1, coll.size());
191: // remove customer
192: customer.remove();
193: // check its phones have been removed
194: coll = phonehome.findByName(lastname);
195: assertEquals("phones have not been removed", 0, coll.size());
196: }
197:
198: /**
199: * test Cascade delete with Address + PhoneMumbers
200: * This test works only if cascade-delete is set on customer from address.
201: */
202: public void _testCDAdPhRemByAd() throws Exception {
203: // create new customer
204: CustomerR customer = customerhome.create(new Integer(17));
205: String lastname = "Bertrand";
206: Name name = new Name(lastname, "Jules");
207: customer.setName(name);
208: // add phones
209: customer.addPhoneNumber("1111", (byte) 1);
210: customer.addPhoneNumber("2222", (byte) 2);
211: customer.addPhoneNumber("3333", (byte) 3);
212: // check we can retrieve them
213: Collection coll = phonehome.findByName(lastname);
214: assertEquals("wrong number of phones", 3, coll.size());
215: // set its address
216: String zip = "38170";
217: customer.setAddress("rue Quirole", "Seyssinet", "France", zip);
218: // check we can retrieve it
219: coll = addresshome.findByZip(zip);
220: assertEquals("wrong number of Addresses", 1, coll.size());
221: AddressR ad = (AddressR) coll.iterator().next();
222: // remove customer by its adress
223: ad.remove();
224: // check its phones have been removed
225: coll = phonehome.findByName(lastname);
226: assertEquals("phones have not been removed", 0, coll.size());
227: }
228:
229: /**
230: * test cascade delete in case of a relation one-to-many
231: * We use the bean customer and its cars.
232: */
233: public void testCascadeDeleteCars() throws Exception {
234: // create new customer
235: CustomerR customer = customerhome.create(new Integer(17));
236: String lastname = "Bertrand";
237: Name name = new Name(lastname, "Jules");
238: customer.setName(name);
239: // add cars
240: customer.addCar("1111", (byte) 1);
241: customer.addCar("2222", (byte) 2);
242: customer.addCar("3333", (byte) 3);
243: // check we can retrieve them
244: Collection coll = carhome.findByName(lastname);
245: assertEquals("wrong number of cars", 3, coll.size());
246: // remove customer
247: customer.remove();
248: // check its cars have been removed
249: coll = carhome.findByName(lastname);
250: assertEquals("cars have not been removed", 0, coll.size());
251: }
252:
253: public void testCrash1() throws Exception {
254: // create new customer
255: CustomerR customer = customerhome.create(new Integer(17));
256: Name name = new Name("Bertrand", "Jules");
257: customer.setName(name);
258: // add cars
259: customer.addCar("1111", (byte) 1);
260: // crash
261: customer.accident("1111", "111101");
262: // remove customer
263: customer.remove();
264: }
265:
266: /**
267: * test create in postCreate
268: */
269: public void testCreateInPostCreate() throws Exception {
270: String zip = "30010";
271: AddressDO addr = new AddressDO("St1", "C1", "E1", zip);
272: CustomerR customer = customerhome.createWithAddress(
273: new Integer(18), addr);
274: // Check address has been created
275: Collection coll = addresshome.findByZip(zip);
276: assertEquals("Address not created", 1, coll.size());
277: Iterator i = coll.iterator();
278: AddressR a = (AddressR) PortableRemoteObject.narrow(i.next(),
279: AddressR.class);
280: Integer cid = a.getCustomerId();
281: assertEquals("Coherence not done", customer.getId().intValue(),
282: cid.intValue());
283: // cleaning
284: customer.remove();
285: }
286:
287: protected boolean initStateOK() throws Exception {
288: return true;
289: }
290:
291: public static Test suite() {
292: return new TestSuite(F_Cascade.class);
293: }
294:
295: public static void main(String args[]) {
296: String testtorun = null;
297: // Get args
298: for (int argn = 0; argn < args.length; argn++) {
299: String sarg = args[argn];
300: if (sarg.equals("-n")) {
301: testtorun = args[++argn];
302: }
303: }
304: if (testtorun == null) {
305: junit.textui.TestRunner.run(suite());
306: } else {
307: junit.textui.TestRunner.run(new F_Cascade(testtorun));
308: }
309: }
310: }
|