1 22 package org.jboss.test.cmp2.commerce; 23 24 import java.util.Collection ; 25 import java.util.Iterator ; 26 import javax.naming.InitialContext ; 27 import junit.framework.TestCase; 28 import net.sourceforge.junitejb.EJBTestCase; 29 30 public class OneToOneUniTest extends EJBTestCase { 31 32 public OneToOneUniTest(String name) { 33 super(name); 34 } 35 36 private OrderHome getOrderHome() { 37 try { 38 InitialContext jndiContext = new InitialContext (); 39 40 return (OrderHome) jndiContext.lookup("commerce/Order"); 41 } catch(Exception e) { 42 e.printStackTrace(); 43 fail("Exception in getOrder: " + e.getMessage()); 44 } 45 return null; 46 } 47 48 private AddressHome getAddressHome() { 49 try { 50 InitialContext jndiContext = new InitialContext (); 51 52 return (AddressHome) jndiContext.lookup("commerce/Address"); 53 } catch(Exception e) { 54 e.printStackTrace(); 55 fail("Exception in getAddressHome: " + e.getMessage()); 56 } 57 return null; 58 } 59 60 private Order a1; 61 private Order a2; 62 63 private Address b1; 64 private Address b2; 65 66 public void setUpEJB() throws Exception { 67 OrderHome orderHome = getOrderHome(); 68 AddressHome addressHome = getAddressHome(); 69 70 deleteAllOrders(orderHome); 72 deleteAllAddresses(addressHome); 73 74 beforeChange(orderHome, addressHome); 76 } 77 78 private void beforeChange(OrderHome orderHome, AddressHome addressHome) 79 throws Exception { 80 81 a1 = orderHome.create(); 83 a2 = orderHome.create(); 84 85 b1 = addressHome.create(); 86 a1.setShippingAddress(b1); 87 88 b2 = addressHome.create(); 89 a2.setShippingAddress(b2); 90 91 assertTrue(a1.getShippingAddress().isIdentical(b1)); 92 assertTrue(a2.getShippingAddress().isIdentical(b2)); 93 } 94 95 public void test_a1setB_a2getB() { 97 a1.setShippingAddress(a2.getShippingAddress()); 100 101 assertTrue(b2.isIdentical(a1.getShippingAddress())); 104 assertTrue(a2.getShippingAddress() == null); 105 } 106 107 public void tearDownEJB() throws Exception { 108 } 109 110 public void deleteAllOrders(OrderHome orderHome) throws Exception { 111 Iterator currentOrders = orderHome.findAll().iterator(); 113 while(currentOrders.hasNext()) { 114 Order o = (Order)currentOrders.next(); 115 o.remove(); 116 } 117 } 118 119 public void deleteAllAddresses(AddressHome addressHome) throws Exception { 120 Iterator currentAddresses = addressHome.findAll().iterator(); 122 while(currentAddresses.hasNext()) { 123 Address a = (Address)currentAddresses.next(); 124 a.remove(); 125 } 126 } 127 } 128 129 130 131 | Popular Tags |