001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.cmp2.commerce;
023:
024: import java.util.Iterator;
025: import javax.ejb.ObjectNotFoundException;
026: import javax.naming.InitialContext;
027:
028: import junit.framework.Test;
029: import org.jboss.test.util.ejb.EJBTestCase;
030:
031: import org.jboss.test.JBossTestCase;
032:
033: public class CascadeDeleteTest extends EJBTestCase {
034: public static Test suite() throws Exception {
035: return JBossTestCase.getDeploySetup(CascadeDeleteTest.class,
036: "cmp2-commerce.jar");
037: }
038:
039: public CascadeDeleteTest(String name) {
040: super (name);
041: }
042:
043: private OrderHome getOrderHome() {
044: try {
045: InitialContext jndiContext = new InitialContext();
046: return (OrderHome) jndiContext.lookup("commerce/Order");
047: } catch (Exception e) {
048: e.printStackTrace();
049: fail("Exception in getOrderHome: " + e.getMessage());
050: }
051: return null;
052: }
053:
054: private ProductCategoryHome getProductCategoryHome() {
055: try {
056: InitialContext jndiContext = new InitialContext();
057: return (ProductCategoryHome) jndiContext
058: .lookup("commerce/ProductCategory");
059: } catch (Exception e) {
060: e.printStackTrace();
061: fail("Exception in getProductCategoryHome: "
062: + e.getMessage());
063: }
064: return null;
065: }
066:
067: private ProductCategoryHome getProductCategoryBatchDeleteHome() {
068: try {
069: InitialContext jndiContext = new InitialContext();
070: return (ProductCategoryHome) jndiContext
071: .lookup("commerce/ProductCategoryBatchDelete");
072: } catch (Exception e) {
073: e.printStackTrace();
074: fail("Exception in getProductCategoryBatchDeleteHome: "
075: + e.getMessage());
076: }
077: return null;
078: }
079:
080: private ProductCategoryTypeHome getProductCategoryTypeHome() {
081: try {
082: InitialContext jndiContext = new InitialContext();
083: return (ProductCategoryTypeHome) jndiContext
084: .lookup("commerce/ProductCategoryType");
085: } catch (Exception e) {
086: e.printStackTrace();
087: fail("Exception in getProductCategoryTypeHome: "
088: + e.getMessage());
089: }
090: return null;
091: }
092:
093: private ProductCategoryTypeHome getProductCategoryTypeBatchDeleteHome() {
094: try {
095: InitialContext jndiContext = new InitialContext();
096: return (ProductCategoryTypeHome) jndiContext
097: .lookup("commerce/ProductCategoryTypeBatchDelete");
098: } catch (Exception e) {
099: e.printStackTrace();
100: fail("Exception in getProductCategoryTypeBatchDeleteHome: "
101: + e.getMessage());
102: }
103: return null;
104: }
105:
106: private LineItemHome getLineItemHome() {
107: try {
108: InitialContext jndiContext = new InitialContext();
109: return (LineItemHome) jndiContext
110: .lookup("commerce/LineItem");
111: } catch (Exception e) {
112: e.printStackTrace();
113: fail("Exception in getLineItemHome: " + e.getMessage());
114: }
115: return null;
116: }
117:
118: private AddressHome getAddressHome() {
119: try {
120: InitialContext jndiContext = new InitialContext();
121: return (AddressHome) jndiContext.lookup("commerce/Address");
122: } catch (Exception e) {
123: e.printStackTrace();
124: fail("Exception in getAddressHome: " + e.getMessage());
125: }
126: return null;
127: }
128:
129: public void testCascadeDelete() throws Exception {
130: OrderHome orderHome = getOrderHome();
131: AddressHome addressHome = getAddressHome();
132: LineItemHome lineItemHome = getLineItemHome();
133:
134: Order order = orderHome.create();
135: Long orderNumber = order.getOrdernumber();
136:
137: Long shipId = new Long(99999);
138: Address ship = addressHome.create(shipId);
139: ship.setState("CA");
140: order.setShippingAddress(ship);
141:
142: Long billId = new Long(88888);
143: Address bill = addressHome.create(billId);
144: bill.setState("CA");
145: order.setBillingAddress(bill);
146:
147: // lineItemId and shipId are the same to check for
148: // weird cascade delete problems
149: Long lineItemId = shipId;
150: LineItem lineItem = lineItemHome.create(lineItemId);
151: lineItem.setOrder(order);
152:
153: order.remove();
154:
155: try {
156: orderHome.findByPrimaryKey(orderNumber);
157: fail("Order should have been deleted");
158: } catch (ObjectNotFoundException e) {
159: // expected
160: }
161:
162: try {
163: addressHome.findByPrimaryKey(billId);
164: fail("Billing address should have been deleted");
165: } catch (ObjectNotFoundException e) {
166: // expected
167: }
168:
169: try {
170: lineItemHome.findByPrimaryKey(lineItemId);
171: fail("Line item should have been deleted");
172: } catch (ObjectNotFoundException e) {
173: // expected
174: }
175:
176: try {
177: addressHome.findByPrimaryKey(shipId);
178: fail("Shipping address should have been deleted");
179: } catch (ObjectNotFoundException e) {
180: // expected
181: }
182: }
183:
184: public void testCategory_Type() throws Exception {
185: ProductCategoryHome ch = getProductCategoryHome();
186:
187: ProductCategory parent = ch.create();
188: CompositeId parentId = parent.getPK();
189:
190: ProductCategory child = ch.create();
191: child.setParent(parent);
192: CompositeId childId = child.getPK();
193:
194: ProductCategory grandChild = ch.create();
195: grandChild.setParent(parent);
196: CompositeId grandChildId = grandChild.getPK();
197:
198: ProductCategoryTypeHome th = getProductCategoryTypeHome();
199: ProductCategoryType type = th.create();
200: parent.setType(type);
201: child.setType(type);
202: Long typeId = type.getId();
203:
204: type.remove();
205:
206: try {
207: ch.findByPrimaryKey(parentId);
208: fail("ProductCategory should have beed deleted.");
209: } catch (ObjectNotFoundException e) {
210: // expected
211: }
212:
213: try {
214: ch.findByPrimaryKey(childId);
215: fail("ProductCategory should have beed deleted.");
216: } catch (ObjectNotFoundException e) {
217: // expected
218: }
219:
220: try {
221: ch.findByPrimaryKey(grandChildId);
222: fail("ProductCategory should have beed deleted.");
223: } catch (ObjectNotFoundException e) {
224: // expected
225: }
226:
227: try {
228: th.findByPrimaryKey(typeId);
229: fail("ProductCategoryType should have beed deleted.");
230: } catch (ObjectNotFoundException e) {
231: // expected
232: }
233: }
234:
235: public void testCategory_Type_BatchCascadeDelete() throws Exception {
236: ProductCategoryHome ch = getProductCategoryBatchDeleteHome();
237:
238: ProductCategory parent = ch.create();
239: CompositeId parentId = parent.getPK();
240:
241: ProductCategory child = ch.create();
242: child.setParent(parent);
243: CompositeId childId = child.getPK();
244:
245: ProductCategory grandChild = ch.create();
246: grandChild.setParent(parent);
247: CompositeId grandChildId = grandChild.getPK();
248:
249: ProductCategoryTypeHome th = getProductCategoryTypeBatchDeleteHome();
250: ProductCategoryType type = th.create();
251: parent.setType(type);
252: child.setType(type);
253: Long typeId = type.getId();
254:
255: type.remove();
256:
257: try {
258: ch.findByPrimaryKey(parentId);
259: fail("ProductCategory should have beed deleted.");
260: } catch (ObjectNotFoundException e) {
261: // expected
262: }
263:
264: try {
265: ch.findByPrimaryKey(childId);
266: fail("ProductCategory should have beed deleted.");
267: } catch (ObjectNotFoundException e) {
268: // expected
269: }
270:
271: try {
272: ch.findByPrimaryKey(grandChildId);
273: fail("ProductCategory should have beed deleted.");
274: } catch (ObjectNotFoundException e) {
275: // expected
276: }
277:
278: try {
279: th.findByPrimaryKey(typeId);
280: fail("ProductCategoryType should have beed deleted.");
281: } catch (ObjectNotFoundException e) {
282: // expected
283: }
284: }
285:
286: public void tearDownEJB() throws Exception {
287: deleteAllOrders(getOrderHome());
288: deleteAllLineItems(getLineItemHome());
289: deleteAllAddresses(getAddressHome());
290: deleteAllCategories(getProductCategoryHome());
291: }
292:
293: public void deleteAllCategories(ProductCategoryHome catHome)
294: throws Exception {
295: Iterator cats = catHome.findAll().iterator();
296: while (cats.hasNext()) {
297: ProductCategory cat = (ProductCategory) cats.next();
298: cat.remove();
299: }
300: }
301:
302: public void deleteAllOrders(OrderHome orderHome) throws Exception {
303: Iterator orders = orderHome.findAll().iterator();
304: while (orders.hasNext()) {
305: Order order = (Order) orders.next();
306: order.remove();
307: }
308: }
309:
310: public void deleteAllLineItems(LineItemHome lineItemHome)
311: throws Exception {
312: Iterator lineItems = lineItemHome.findAll().iterator();
313: while (lineItems.hasNext()) {
314: LineItem lineItem = (LineItem) lineItems.next();
315: lineItem.remove();
316: }
317: }
318:
319: public void deleteAllAddresses(AddressHome addressHome)
320: throws Exception {
321: Iterator addresses = addressHome.findAll().iterator();
322: while (addresses.hasNext()) {
323: Address address = (Address) addresses.next();
324: address.remove();
325: }
326: }
327: }
|