1 22 package org.jboss.test.cmp2.commerce; 23 24 import java.util.Iterator ; 25 import javax.ejb.ObjectNotFoundException ; 26 import javax.naming.InitialContext ; 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 36 { 37 return JBossTestCase.getDeploySetup(CascadeDeleteTest.class, "cmp2-commerce.jar"); 38 } 39 40 public CascadeDeleteTest(String name) 41 { 42 super(name); 43 } 44 45 private OrderHome getOrderHome() 46 { 47 try 48 { 49 InitialContext jndiContext = new InitialContext (); 50 return (OrderHome) jndiContext.lookup("commerce/Order"); 51 } 52 catch(Exception 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 jndiContext = new InitialContext (); 65 return (ProductCategoryHome) jndiContext.lookup("commerce/ProductCategory"); 66 } 67 catch(Exception 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 jndiContext = new InitialContext (); 80 return (ProductCategoryHome) jndiContext.lookup("commerce/ProductCategoryBatchDelete"); 81 } 82 catch(Exception 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 jndiContext = new InitialContext (); 95 return (ProductCategoryTypeHome) jndiContext.lookup("commerce/ProductCategoryType"); 96 } 97 catch(Exception 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 jndiContext = new InitialContext (); 110 return (ProductCategoryTypeHome) jndiContext.lookup("commerce/ProductCategoryTypeBatchDelete"); 111 } 112 catch(Exception 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 jndiContext = new InitialContext (); 125 return (LineItemHome) jndiContext.lookup("commerce/LineItem"); 126 } 127 catch(Exception 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 jndiContext = new InitialContext (); 140 return (AddressHome) jndiContext.lookup("commerce/Address"); 141 } 142 catch(Exception e) 143 { 144 e.printStackTrace(); 145 fail("Exception in getAddressHome: " + e.getMessage()); 146 } 147 return null; 148 } 149 150 public void testCascadeDelete() throws Exception 151 { 152 OrderHome orderHome = getOrderHome(); 153 AddressHome addressHome = getAddressHome(); 154 LineItemHome lineItemHome = getLineItemHome(); 155 156 Order order = orderHome.create(); 157 Long orderNumber = order.getOrdernumber(); 158 159 Long shipId = new Long (99999); 160 Address ship = addressHome.create(shipId); 161 ship.setState("CA"); 162 order.setShippingAddress(ship); 163 164 Long billId = new Long (88888); 165 Address bill = addressHome.create(billId); 166 bill.setState("CA"); 167 order.setBillingAddress(bill); 168 169 Long 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 e) 183 { 184 } 186 187 try 188 { 189 addressHome.findByPrimaryKey(billId); 190 fail("Billing address should have been deleted"); 191 } 192 catch(ObjectNotFoundException e) 193 { 194 } 196 197 try 198 { 199 lineItemHome.findByPrimaryKey(lineItemId); 200 fail("Line item should have been deleted"); 201 } 202 catch(ObjectNotFoundException e) 203 { 204 } 206 207 try 208 { 209 addressHome.findByPrimaryKey(shipId); 210 fail("Shipping address should have been deleted"); 211 } 212 catch(ObjectNotFoundException e) 213 { 214 } 216 } 217 218 public void testCategory_Type() throws Exception 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 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 e) 247 { 248 } 250 251 try 252 { 253 ch.findByPrimaryKey(childId); 254 fail("ProductCategory should have beed deleted."); 255 } 256 catch(ObjectNotFoundException e) 257 { 258 } 260 261 try 262 { 263 ch.findByPrimaryKey(grandChildId); 264 fail("ProductCategory should have beed deleted."); 265 } 266 catch(ObjectNotFoundException e) 267 { 268 } 270 271 try 272 { 273 th.findByPrimaryKey(typeId); 274 fail("ProductCategoryType should have beed deleted."); 275 } 276 catch(ObjectNotFoundException e) 277 { 278 } 280 } 281 282 public void testCategory_Type_BatchCascadeDelete() throws Exception 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 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 e) 311 { 312 } 314 315 try 316 { 317 ch.findByPrimaryKey(childId); 318 fail("ProductCategory should have beed deleted."); 319 } 320 catch(ObjectNotFoundException e) 321 { 322 } 324 325 try 326 { 327 ch.findByPrimaryKey(grandChildId); 328 fail("ProductCategory should have beed deleted."); 329 } 330 catch(ObjectNotFoundException e) 331 { 332 } 334 335 try 336 { 337 th.findByPrimaryKey(typeId); 338 fail("ProductCategoryType should have beed deleted."); 339 } 340 catch(ObjectNotFoundException e) 341 { 342 } 344 } 345 346 public void tearDownEJB() throws Exception 347 { 348 deleteAllOrders(getOrderHome()); 349 deleteAllLineItems(getLineItemHome()); 350 deleteAllAddresses(getAddressHome()); 351 deleteAllCategories(getProductCategoryHome()); 352 } 353 354 public void deleteAllCategories(ProductCategoryHome catHome) throws Exception 355 { 356 Iterator 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 365 { 366 Iterator 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 375 { 376 Iterator 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 385 { 386 Iterator addresses = addressHome.findAll().iterator(); 387 while(addresses.hasNext()) 388 { 389 Address address = (Address) addresses.next(); 390 address.remove(); 391 } 392 } 393 } 394 | Popular Tags |