KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cmp2 > commerce > CascadeDeleteTest


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.cmp2.commerce;
23
24 import java.util.Iterator JavaDoc;
25 import javax.ejb.ObjectNotFoundException JavaDoc;
26 import javax.naming.InitialContext JavaDoc;
27
28 import junit.framework.Test;
29 import net.sourceforge.junitejb.EJBTestCase;
30
31 import org.jboss.test.JBossTestCase;
32
33 public class CascadeDeleteTest extends EJBTestCase
34 {
35    public static Test suite() throws Exception JavaDoc
36    {
37       return JBossTestCase.getDeploySetup(CascadeDeleteTest.class, "cmp2-commerce.jar");
38    }
39
40    public CascadeDeleteTest(String JavaDoc name)
41    {
42       super(name);
43    }
44
45    private OrderHome getOrderHome()
46    {
47       try
48       {
49          InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
50          return (OrderHome) jndiContext.lookup("commerce/Order");
51       }
52       catch(Exception JavaDoc e)
53       {
54          e.printStackTrace();
55          fail("Exception in getOrderHome: " + e.getMessage());
56       }
57       return null;
58    }
59
60    private ProductCategoryHome getProductCategoryHome()
61    {
62       try
63       {
64          InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
65          return (ProductCategoryHome) jndiContext.lookup("commerce/ProductCategory");
66       }
67       catch(Exception JavaDoc e)
68       {
69          e.printStackTrace();
70          fail("Exception in getProductCategoryHome: " + e.getMessage());
71       }
72       return null;
73    }
74
75    private ProductCategoryHome getProductCategoryBatchDeleteHome()
76    {
77       try
78       {
79          InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
80          return (ProductCategoryHome) jndiContext.lookup("commerce/ProductCategoryBatchDelete");
81       }
82       catch(Exception JavaDoc e)
83       {
84          e.printStackTrace();
85          fail("Exception in getProductCategoryBatchDeleteHome: " + e.getMessage());
86       }
87       return null;
88    }
89
90    private ProductCategoryTypeHome getProductCategoryTypeHome()
91    {
92       try
93       {
94          InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
95          return (ProductCategoryTypeHome) jndiContext.lookup("commerce/ProductCategoryType");
96       }
97       catch(Exception JavaDoc e)
98       {
99          e.printStackTrace();
100          fail("Exception in getProductCategoryTypeHome: " + e.getMessage());
101       }
102       return null;
103    }
104
105    private ProductCategoryTypeHome getProductCategoryTypeBatchDeleteHome()
106    {
107       try
108       {
109          InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
110          return (ProductCategoryTypeHome) jndiContext.lookup("commerce/ProductCategoryTypeBatchDelete");
111       }
112       catch(Exception JavaDoc e)
113       {
114          e.printStackTrace();
115          fail("Exception in getProductCategoryTypeBatchDeleteHome: " + e.getMessage());
116       }
117       return null;
118    }
119
120    private LineItemHome getLineItemHome()
121    {
122       try
123       {
124          InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
125          return (LineItemHome) jndiContext.lookup("commerce/LineItem");
126       }
127       catch(Exception JavaDoc e)
128       {
129          e.printStackTrace();
130          fail("Exception in getLineItemHome: " + e.getMessage());
131       }
132       return null;
133    }
134
135    private AddressHome getAddressHome()
136    {
137       try
138       {
139          InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
140          return (AddressHome) jndiContext.lookup("commerce/Address");
141       }
142       catch(Exception JavaDoc e)
143       {
144          e.printStackTrace();
145          fail("Exception in getAddressHome: " + e.getMessage());
146       }
147       return null;
148    }
149
150    public void testCascadeDelete() throws Exception JavaDoc
151    {
152       OrderHome orderHome = getOrderHome();
153       AddressHome addressHome = getAddressHome();
154       LineItemHome lineItemHome = getLineItemHome();
155
156       Order order = orderHome.create();
157       Long JavaDoc orderNumber = order.getOrdernumber();
158
159       Long JavaDoc shipId = new Long JavaDoc(99999);
160       Address ship = addressHome.create(shipId);
161       ship.setState("CA");
162       order.setShippingAddress(ship);
163
164       Long JavaDoc billId = new Long JavaDoc(88888);
165       Address bill = addressHome.create(billId);
166       bill.setState("CA");
167       order.setBillingAddress(bill);
168
169       // lineItemId and shipId are the same to check for
170
// weird cascade delete problems
171
Long JavaDoc lineItemId = shipId;
172       LineItem lineItem = lineItemHome.create(lineItemId);
173       lineItem.setOrder(order);
174
175       order.remove();
176
177       try
178       {
179          orderHome.findByPrimaryKey(orderNumber);
180          fail("Order should have been deleted");
181       }
182       catch(ObjectNotFoundException JavaDoc e)
183       {
184          // expected
185
}
186
187       try
188       {
189          addressHome.findByPrimaryKey(billId);
190          fail("Billing address should have been deleted");
191       }
192       catch(ObjectNotFoundException JavaDoc e)
193       {
194          // expected
195
}
196
197       try
198       {
199          lineItemHome.findByPrimaryKey(lineItemId);
200          fail("Line item should have been deleted");
201       }
202       catch(ObjectNotFoundException JavaDoc e)
203       {
204          // expected
205
}
206
207       try
208       {
209          addressHome.findByPrimaryKey(shipId);
210          fail("Shipping address should have been deleted");
211       }
212       catch(ObjectNotFoundException JavaDoc e)
213       {
214          // expected
215
}
216    }
217
218    public void testCategory_Type() throws Exception JavaDoc
219    {
220       ProductCategoryHome ch = getProductCategoryHome();
221
222       ProductCategory parent = ch.create();
223       CompositeId parentId = parent.getPK();
224
225       ProductCategory child = ch.create();
226       child.setParent(parent);
227       CompositeId childId = child.getPK();
228
229       ProductCategory grandChild = ch.create();
230       grandChild.setParent(parent);
231       CompositeId grandChildId = grandChild.getPK();
232
233       ProductCategoryTypeHome th = getProductCategoryTypeHome();
234       ProductCategoryType type = th.create();
235       parent.setType(type);
236       child.setType(type);
237       Long JavaDoc typeId = type.getId();
238
239       type.remove();
240
241       try
242       {
243          ch.findByPrimaryKey(parentId);
244          fail("ProductCategory should have beed deleted.");
245       }
246       catch(ObjectNotFoundException JavaDoc e)
247       {
248          // expected
249
}
250
251       try
252       {
253          ch.findByPrimaryKey(childId);
254          fail("ProductCategory should have beed deleted.");
255       }
256       catch(ObjectNotFoundException JavaDoc e)
257       {
258          // expected
259
}
260
261       try
262       {
263          ch.findByPrimaryKey(grandChildId);
264          fail("ProductCategory should have beed deleted.");
265       }
266       catch(ObjectNotFoundException JavaDoc e)
267       {
268          // expected
269
}
270
271       try
272       {
273          th.findByPrimaryKey(typeId);
274          fail("ProductCategoryType should have beed deleted.");
275       }
276       catch(ObjectNotFoundException JavaDoc e)
277       {
278          // expected
279
}
280    }
281
282    public void testCategory_Type_BatchCascadeDelete() throws Exception JavaDoc
283    {
284       ProductCategoryHome ch = getProductCategoryBatchDeleteHome();
285
286       ProductCategory parent = ch.create();
287       CompositeId parentId = parent.getPK();
288
289       ProductCategory child = ch.create();
290       child.setParent(parent);
291       CompositeId childId = child.getPK();
292
293       ProductCategory grandChild = ch.create();
294       grandChild.setParent(parent);
295       CompositeId grandChildId = grandChild.getPK();
296
297       ProductCategoryTypeHome th = getProductCategoryTypeBatchDeleteHome();
298       ProductCategoryType type = th.create();
299       parent.setType(type);
300       child.setType(type);
301       Long JavaDoc typeId = type.getId();
302
303       type.remove();
304
305       try
306       {
307          ch.findByPrimaryKey(parentId);
308          fail("ProductCategory should have beed deleted.");
309       }
310       catch(ObjectNotFoundException JavaDoc e)
311       {
312          // expected
313
}
314
315       try
316       {
317          ch.findByPrimaryKey(childId);
318          fail("ProductCategory should have beed deleted.");
319       }
320       catch(ObjectNotFoundException JavaDoc e)
321       {
322          // expected
323
}
324
325       try
326       {
327          ch.findByPrimaryKey(grandChildId);
328          fail("ProductCategory should have beed deleted.");
329       }
330       catch(ObjectNotFoundException JavaDoc e)
331       {
332          // expected
333
}
334
335       try
336       {
337          th.findByPrimaryKey(typeId);
338          fail("ProductCategoryType should have beed deleted.");
339       }
340       catch(ObjectNotFoundException JavaDoc e)
341       {
342          // expected
343
}
344    }
345
346    public void tearDownEJB() throws Exception JavaDoc
347    {
348       deleteAllOrders(getOrderHome());
349       deleteAllLineItems(getLineItemHome());
350       deleteAllAddresses(getAddressHome());
351       deleteAllCategories(getProductCategoryHome());
352    }
353
354    public void deleteAllCategories(ProductCategoryHome catHome) throws Exception JavaDoc
355    {
356       Iterator JavaDoc cats = catHome.findAll().iterator();
357       while(cats.hasNext())
358       {
359          ProductCategory cat = (ProductCategory) cats.next();
360          cat.remove();
361       }
362    }
363
364    public void deleteAllOrders(OrderHome orderHome) throws Exception JavaDoc
365    {
366       Iterator JavaDoc orders = orderHome.findAll().iterator();
367       while(orders.hasNext())
368       {
369          Order order = (Order) orders.next();
370          order.remove();
371       }
372    }
373
374    public void deleteAllLineItems(LineItemHome lineItemHome) throws Exception JavaDoc
375    {
376       Iterator JavaDoc lineItems = lineItemHome.findAll().iterator();
377       while(lineItems.hasNext())
378       {
379          LineItem lineItem = (LineItem) lineItems.next();
380          lineItem.remove();
381       }
382    }
383
384    public void deleteAllAddresses(AddressHome addressHome) throws Exception JavaDoc
385    {
386       Iterator JavaDoc addresses = addressHome.findAll().iterator();
387       while(addresses.hasNext())
388       {
389          Address address = (Address) addresses.next();
390          address.remove();
391       }
392    }
393 }
394
Popular Tags