1 22 package org.jboss.test.cmp2.commerce; 23 24 import java.util.ArrayList ; 25 import java.util.Collection ; 26 import java.util.Iterator ; 27 import java.util.Set ; 28 import javax.naming.InitialContext ; 29 import junit.framework.Test; 30 import net.sourceforge.junitejb.EJBTestCase; 31 32 import org.jboss.test.JBossTestCase; 33 34 public class CommerceTest extends EJBTestCase { 35 public static Test suite() throws Exception { 36 return JBossTestCase.getDeploySetup( 37 CommerceTest.class, 38 "cmp2-commerce.jar"); 39 } 40 41 public CommerceTest(String name) { 42 super(name); 43 } 44 45 private OrderHome getOrderHome() { 46 try { 47 InitialContext jndiContext = new InitialContext (); 48 49 return (OrderHome) jndiContext.lookup("commerce/Order"); 50 } catch(Exception e) { 51 e.printStackTrace(); 52 fail("Exception in getOrderHome: " + e.getMessage()); 53 } 54 return null; 55 } 56 57 private LineItemHome getLineItemHome() { 58 try { 59 InitialContext jndiContext = new InitialContext (); 60 61 return (LineItemHome) jndiContext.lookup("commerce/LineItem"); 62 } catch(Exception e) { 63 e.printStackTrace(); 64 fail("Exception in getLineItemHome: " + e.getMessage()); 65 } 66 return null; 67 } 68 69 private ProductHome getProductHome() { 70 try { 71 InitialContext jndiContext = new InitialContext (); 72 73 return (ProductHome) jndiContext.lookup("commerce/Product"); 74 } catch(Exception e) { 75 e.printStackTrace(); 76 fail("Exception in getProductHome: " + e.getMessage()); 77 } 78 return null; 79 } 80 81 private AddressHome getAddressHome() { 82 try { 83 InitialContext jndiContext = new InitialContext (); 84 85 return (AddressHome) jndiContext.lookup("commerce/Address"); 86 } catch(Exception e) { 87 e.printStackTrace(); 88 fail("Exception in getAddressHome: " + e.getMessage()); 89 } 90 return null; 91 } 92 93 public void test_setInPostCreate() throws Exception { 94 95 OrderHome oh = getOrderHome(); 96 LineItemHome lh = getLineItemHome(); 97 98 LineItem l =lh.create(); 99 100 Order o = oh.create(); 101 l = lh.create(o); 102 assertTrue(l.getOrder().isIdentical(o)); 103 } 104 105 public void test_dvo() throws Exception { 106 OrderHome orderHome = getOrderHome(); 107 108 Order order = orderHome.create(); 109 Long ordernumber = order.getOrdernumber(); 110 111 Card creditCard = new Card(); 113 creditCard.setType(Card.VISA); 114 creditCard.setCardNumber("1111-2222-3333-4444"); 115 creditCard.setCardHolder(new FormalName("Dain", 'S', "Sundstrom")); 116 creditCard.setBillingZip(55414); 117 118 order.setCreditCard(creditCard); 119 assertEquals(order.getCreditCard(), creditCard); 120 121 order = null; 122 order = orderHome.findByPrimaryKey(ordernumber); 123 assertEquals(order.getCreditCard(), creditCard); 124 } 125 126 public void test_getOrdersShippedToCA() throws Exception { 127 OrderHome orderHome = getOrderHome(); 128 AddressHome addressHome = getAddressHome(); 129 130 Order orderCA1 = orderHome.create(); 131 Address shipCA1 = addressHome.create(); 132 shipCA1.setState("CA"); 133 orderCA1.setShippingAddress(shipCA1); 134 135 Order orderCA2 = orderHome.create(); 136 Address shipCA2 = addressHome.create(); 137 shipCA2.setState("CA"); 138 orderCA2.setShippingAddress(shipCA2); 139 140 Order orderMN = orderHome.create(); 141 Address shipMN = addressHome.create(); 142 shipMN.setState("MN"); 143 orderMN.setShippingAddress(shipMN); 144 145 Set s = orderMN.getOrdersShippedToCA(); 146 System.out.println(s); 147 assertTrue(s.contains(orderCA1)); 148 assertTrue(s.contains(orderCA2)); 149 assertTrue(!s.contains(orderMN)); 150 assertTrue(s.size()==2); 151 152 s = orderMN.getOrdersShippedToCA2(); 153 System.out.println(s); 154 assertTrue(s.contains(orderCA1)); 155 assertTrue(s.contains(orderCA2)); 156 assertTrue(!s.contains(orderMN)); 157 assertTrue(s.size()==2); 158 } 159 160 public void test_getStatesShipedTo() throws Exception { 161 OrderHome orderHome = getOrderHome(); 162 AddressHome addressHome = getAddressHome(); 163 164 Order orderCA1 = orderHome.create(); 165 Address shipCA1 = addressHome.create(); 166 shipCA1.setState("CA"); 167 orderCA1.setShippingAddress(shipCA1); 168 169 Order orderCA2 = orderHome.create(); 170 Address shipCA2 = addressHome.create(); 171 shipCA2.setState("CA"); 172 orderCA2.setShippingAddress(shipCA2); 173 174 Order orderMN = orderHome.create(); 175 Address shipMN = addressHome.create(); 176 shipMN.setState("MN"); 177 orderMN.setShippingAddress(shipMN); 178 179 System.out.println("orderMN.getStatesShipedTo();"); 180 Collection c = orderMN.getStatesShipedTo(); 181 System.out.println(c); 182 assertTrue(c.contains("CA")); 183 assertTrue(c.contains("MN")); 184 assertTrue(c.size()==3); 185 186 c = orderMN.getStatesShipedTo2(); 187 System.out.println(c); 188 assertTrue(c.contains("CA")); 189 assertTrue(c.contains("MN")); 190 assertTrue(c.size()==3); 191 } 192 193 public void test_getAddressesInCA() throws Exception { 194 OrderHome orderHome = getOrderHome(); 195 AddressHome addressHome = getAddressHome(); 196 197 Order orderCA1 = orderHome.create(); 198 Address shipCA1 = addressHome.create(); 199 shipCA1.setState("CA"); 200 orderCA1.setShippingAddress(shipCA1); 201 Address billCA1 = addressHome.create(); 202 billCA1.setState("CA"); 203 orderCA1.setBillingAddress(billCA1); 204 205 Order orderCA2 = orderHome.create(); 206 Address shipCA2 = addressHome.create(); 207 shipCA2.setState("CA"); 208 orderCA2.setShippingAddress(shipCA2); 209 orderCA2.setBillingAddress(shipCA2); 210 211 Order orderMN = orderHome.create(); 212 Address shipMN = addressHome.create(); 213 shipMN.setState("MN"); 214 orderMN.setShippingAddress(shipMN); 215 216 Collection c = orderMN.getAddressesInCA(); 217 System.out.println(c); 218 assertTrue(c.contains(shipCA1)); 219 assertTrue(c.contains(shipCA2)); 220 assertTrue(c.contains(billCA1)); 221 assertTrue(c.size()==3); 222 223 c = orderMN.getAddressesInCA2(); 224 System.out.println(c); 225 assertTrue(c.contains(shipCA1)); 226 assertTrue(c.contains(shipCA2)); 227 assertTrue(c.contains(billCA1)); 228 assertTrue(c.size()==3); 229 } 230 231 232 public void test_findDoubleJoin() throws Exception { 233 OrderHome orderHome = getOrderHome(); 234 LineItemHome lineItemHome = getLineItemHome(); 235 236 Order order1 = orderHome.create(); 237 LineItem line1 = lineItemHome.create(); 238 line1.setQuantity(1); 239 order1.getLineItems().add(line1); 240 LineItem line2 = lineItemHome.create(); 241 line2.setQuantity(2); 242 order1.getLineItems().add(line2); 243 244 Order order2 = orderHome.create(); 245 LineItem line3 = lineItemHome.create(); 246 line3.setQuantity(2); 247 order2.getLineItems().add(line3); 248 249 250 Collection c = orderHome.findDoubleJoin(1, 2); 251 assertTrue(c.contains(order1)); 252 assertTrue(!c.contains(order2)); 253 assertTrue(c.size()==1); 254 } 255 256 public void testCMRReset() throws Exception { 257 OrderHome orderHome = getOrderHome(); 258 LineItemHome lineItemHome = getLineItemHome(); 259 260 Order order = orderHome.create(); 261 Collection lineItems = order.getLineItems(); 262 263 LineItem line1 = lineItemHome.create(); 264 line1.setQuantity(1); 265 lineItems.add(line1); 266 267 LineItem line2 = lineItemHome.create(); 268 line2.setQuantity(2); 269 lineItems.add(line2); 270 271 LineItem line3 = lineItemHome.create(); 272 line3.setQuantity(2); 273 lineItems.add(line3); 274 275 276 assertEquals(lineItems.size(), 3); 277 assertTrue(lineItems == order.getLineItems()); 278 order.setLineItems(lineItems); 279 assertEquals(lineItems.size(), 3); 280 assertTrue(lineItems == order.getLineItems()); 281 } 282 283 public void testCMRSetFromNewCollection() throws Exception { 284 OrderHome orderHome = getOrderHome(); 285 LineItemHome lineItemHome = getLineItemHome(); 286 287 Order order = orderHome.create(); 288 Collection lineItems = new ArrayList (); 289 290 LineItem line1 = lineItemHome.create(); 291 line1.setQuantity(1); 292 lineItems.add(line1); 293 294 LineItem line2 = lineItemHome.create(); 295 line2.setQuantity(2); 296 lineItems.add(line2); 297 298 LineItem line3 = lineItemHome.create(); 299 line3.setQuantity(2); 300 lineItems.add(line3); 301 302 303 assertEquals(lineItems.size(), 3); 304 order.setLineItems(lineItems); 305 assertEquals(lineItems.size(), 3); 306 assertEquals(order.getLineItems().size(), 3); 307 } 308 309 public void testIsIdentical() throws Exception 310 { 311 OrderHome orderHome = getOrderHome(); 312 Order order = orderHome.create(new Long (111)); 313 314 LineItemHome liHome = getLineItemHome(); 315 LineItem lineItem = liHome.create(new Long (111)); 316 317 assertTrue("!order.isIdentical(lineItem)", !order.isIdentical(lineItem)); 318 assertTrue("order.isIdentical(order)", order.isIdentical(order)); 319 } 320 321 public void testOverloadedEjbSelects() throws Exception 322 { 323 getAddressHome().selectAddresses(""); 324 } 325 326 public void setUpEJB() throws Exception { 327 deleteAllOrders(getOrderHome()); 328 deleteAllLineItems(getLineItemHome()); 329 deleteAllProducts(getProductHome()); 330 deleteAllAddresses(getAddressHome()); 331 } 332 333 public void tearDownEJB() throws Exception { 334 deleteAllOrders(getOrderHome()); 335 deleteAllLineItems(getLineItemHome()); 336 deleteAllProducts(getProductHome()); 337 deleteAllAddresses(getAddressHome()); 338 } 339 340 public void deleteAllOrders(OrderHome orderHome) throws Exception { 341 Iterator orders = orderHome.findAll().iterator(); 342 while(orders.hasNext()) { 343 Order order = (Order)orders.next(); 344 order.remove(); 345 } 346 } 347 348 public void deleteAllLineItems(LineItemHome lineItemHome) throws Exception { 349 Iterator lineItems = lineItemHome.findAll().iterator(); 350 while(lineItems.hasNext()) { 351 LineItem lineItem = (LineItem)lineItems.next(); 352 lineItem.remove(); 353 } 354 } 355 356 public void deleteAllProducts(ProductHome productHome) throws Exception { 357 Iterator products = productHome.findAll().iterator(); 358 while(products.hasNext()) { 359 Product product = (Product)products.next(); 360 product.remove(); 361 } 362 } 363 364 public void deleteAllAddresses(AddressHome addressHome) throws Exception { 365 Iterator addresses = addressHome.findAll().iterator(); 366 while(addresses.hasNext()) { 367 Address address = (Address)addresses.next(); 368 address.remove(); 369 } 370 } 371 } 372 | Popular Tags |