KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2005 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_Cascade.java,v 1.9 2005/05/16 15:34:01 benoitf 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 cascade-delete facility
51  * @author Philippe Durieux
52  */

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 JavaDoc 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 JavaDoc e) {
83                 fail("Cannot get bean home: " + e.getMessage());
84             }
85             isInit = true;
86         }
87     }
88
89     /**
90      * test cascade delete in case of a relation one-to-one
91      * We use the bean customer and its credit card.
92      */

93     public void testCascadeDeleteCC() throws Exception JavaDoc {
94         // create new customer
95
CustomerR customer = customerhome.create(new Integer JavaDoc(15));
96         String JavaDoc lastname = "Dupont";
97         Name name = new Name(lastname, "Jean");
98         customer.setName(name);
99         // set its credit card
100
customer.setCreditCard(new Date JavaDoc(), "154563", lastname);
101         // check we can retrieve it
102
creditcardhome.findByName(lastname);
103         // remove customer
104
customer.remove();
105         // check its credit card has been removed
106
try {
107             creditcardhome.findByName(lastname);
108             fail("credit card has not been removed by cascade delete");
109         } catch (FinderException JavaDoc e) {
110         }
111     }
112
113     /**
114      * test cascade delete in case of a relation one-to-one
115      * We use the bean customer and its address.
116      */

117     public void testCascadeDeleteAd() throws Exception JavaDoc {
118         // create new customer
119
CustomerR customer = customerhome.create(new Integer JavaDoc(16));
120         String JavaDoc lastname = "Durand";
121         Name name = new Name(lastname, "Jacques");
122         customer.setName(name);
123         // set its address
124
String JavaDoc zip = "38170";
125         customer.setAddress("rue Quirole", "Seyssinet", "France", zip);
126         // check we can retrieve it
127
Collection JavaDoc coll = addresshome.findByZip(zip);
128         assertEquals("wrong number of Addresses", 1, coll.size());
129         // remove customer
130
customer.remove();
131         // check its address has been removed
132
try {
133             coll = addresshome.findByZip(zip);
134             assertEquals("Address have not been removed", 0, coll.size());
135         } catch (FinderException JavaDoc e) {
136         }
137     }
138
139     /**
140      * test cascade delete in case of a relation one-to-many
141      * We use the bean customer and its phones.
142      */

143     public void testCascadeDeletePhs() throws Exception JavaDoc {
144         // create new customer
145
CustomerR customer = customerhome.create(new Integer JavaDoc(17));
146         String JavaDoc lastname = "Bertrand";
147         Name name = new Name(lastname, "Jules");
148         customer.setName(name);
149         // add phones
150
customer.addPhoneNumber("1111", (byte) 1);
151         customer.addPhoneNumber("2222", (byte) 2);
152         customer.addPhoneNumber("3333", (byte) 3);
153         // check we can retrieve them
154
Collection JavaDoc coll = phonehome.findByName(lastname);
155         assertEquals("wrong number of phones", 3, coll.size());
156         // remove customer
157
customer.remove();
158         // check its phones have been removed
159
coll = phonehome.findByName(lastname);
160         assertEquals("phones have not been removed", 0, coll.size());
161     }
162
163     /**
164      * test cascade delete in case of a relation one-to-many
165      * We use the bean customer and its cars.
166      */

167     public void testCascadeDeleteCars() throws Exception JavaDoc {
168         // create new customer
169
CustomerR customer = customerhome.create(new Integer JavaDoc(17));
170         String JavaDoc lastname = "Bertrand";
171         Name name = new Name(lastname, "Jules");
172         customer.setName(name);
173         // add cars
174
customer.addCar("1111", (byte) 1);
175         customer.addCar("2222", (byte) 2);
176         customer.addCar("3333", (byte) 3);
177         // check we can retrieve them
178
Collection JavaDoc coll = carhome.findByName(lastname);
179         assertEquals("wrong number of cars", 3, coll.size());
180         // remove customer
181
customer.remove();
182         // check its cars have been removed
183
coll = carhome.findByName(lastname);
184         assertEquals("cars have not been removed", 0, coll.size());
185     }
186
187     public void testCrash1() throws Exception JavaDoc {
188         // create new customer
189
CustomerR customer = customerhome.create(new Integer JavaDoc(17));
190         Name name = new Name("Bertrand", "Jules");
191         customer.setName(name);
192         // add cars
193
customer.addCar("1111", (byte) 1);
194         // crash
195
customer.accident("1111", "111101");
196         // remove customer
197
customer.remove();
198     }
199
200     /**
201      * test create in postCreate
202      */

203     public void testCreateInPostCreate() throws Exception JavaDoc {
204         String JavaDoc zip = "30010";
205         AddressDO addr = new AddressDO("St1", "C1", "E1", zip);
206         CustomerR customer = customerhome.createWithAddress(new Integer JavaDoc(18), addr);
207         // Check address has been created
208
Collection JavaDoc coll = addresshome.findByZip(zip);
209         assertEquals("Address not created", 1, coll.size());
210         Iterator JavaDoc i = coll.iterator();
211         AddressR a = (AddressR) PortableRemoteObject.narrow(i.next(), AddressR.class);
212         Integer JavaDoc cid = a.getCustomerId();
213         assertEquals("Coherence not done", customer.getId().intValue(), cid.intValue());
214         // cleaning
215
customer.remove();
216     }
217
218     protected boolean initStateOK() throws Exception JavaDoc {
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 JavaDoc args[]) {
228         String JavaDoc testtorun = null;
229         // Get args
230
for (int argn = 0; argn < args.length; argn++) {
231             String JavaDoc 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