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 OneToManyBiTest extends EJBTestCase { 31 32 public OneToManyBiTest(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 LineItemHome getLineItemHome() { 49 try { 50 InitialContext jndiContext = new InitialContext (); 51 52 return (LineItemHome) jndiContext.lookup("commerce/LineItem"); 53 } catch(Exception e) { 54 e.printStackTrace(); 55 fail("Exception in getLineItemHome: " + e.getMessage()); 56 } 57 return null; 58 } 59 60 private Order a1; 61 private Order a2; 62 63 private Collection b1; 64 private Collection b2; 65 66 private LineItem[] b1x = new LineItem[20]; 67 private LineItem[] b2x = new LineItem[30]; 68 69 public void setUpEJB() throws Exception { 70 OrderHome orderHome = getOrderHome(); 71 LineItemHome lineItemHome = getLineItemHome(); 72 73 deleteAllOrders(orderHome); 75 deleteAllLineItems(lineItemHome); 76 77 beforeChange(orderHome, lineItemHome); 79 } 80 81 private void beforeChange(OrderHome orderHome, LineItemHome lineItemHome) 82 throws Exception { 83 84 a1 = orderHome.create(); 86 a2 = orderHome.create(); 87 88 b1 = a1.getLineItems(); 89 b2 = a2.getLineItems(); 90 91 for(int i=0; i<b1x.length; i++) { 92 b1x[i] = lineItemHome.create(); 93 b1.add(b1x[i]); 94 } 95 96 for(int i=0; i<b2x.length; i++) { 97 b2x[i] = lineItemHome.create(); 98 b2.add(b2x[i]); 99 } 100 101 for(int i=0; i<b1x.length; i++) { 103 assertTrue(b1.contains(b1x[i])); 104 } 105 106 for(int i=0; i<b2x.length; i++) { 108 assertTrue(b2.contains(b2x[i])); 109 } 110 } 111 112 public void setUp() { 113 b1 = a1.getLineItems(); 116 b2 = a2.getLineItems(); 117 } 118 119 public void test_a1SetB_a2GetB() { 121 a1.setLineItems(a2.getLineItems()); 123 124 126 assertTrue(a2.getLineItems().isEmpty()); 128 129 assertTrue(b2.isEmpty()); 131 132 assertTrue(b1 == a1.getLineItems()); 134 135 assertTrue(b2 == a2.getLineItems()); 137 138 for(int i=0; i<b2x.length; i++) { 143 assertTrue(a1.getLineItems().contains(b2x[i])); 144 } 145 146 for(int i=0; i<b1x.length; i++) { 151 assertTrue(b1x[i].getOrder() == null); 152 } 153 154 155 for(int i=0; i<b2x.length; i++) { 160 assertTrue(a1.isIdentical(b2x[i].getOrder())); 161 } 162 } 163 164 public void test_b2mSetA_b1nGetA() { 166 168 b2x[b2x.length-1].setOrder(b1x[b1x.length-1].getOrder()); 170 171 173 for(int i=0; i<b1x.length; i++) { 178 assertTrue(b1.contains(b1x[i])); 179 } 180 181 assertTrue(b1.contains(b2x[b2x.length-1])); 183 184 for(int i=0; i<b2x.length-1; i++) { 189 assertTrue(b2.contains(b2x[i])); 190 } 191 192 for(int i=0; i<b1x.length; i++) { 197 assertTrue(a1.isIdentical(b1x[i].getOrder())); 198 } 199 200 for(int i=0; i<b2x.length-1; i++) { 205 assertTrue(a2.isIdentical(b2x[i].getOrder())); 206 } 207 208 assertTrue(a1.isIdentical(b2x[b2x.length-1].getOrder())); 210 } 211 212 public void test_a1GetB_addB2m() { 214 216 a1.getLineItems().add(b2x[b2x.length-1]); 218 219 221 for(int i=0; i<b1x.length; i++) { 226 assertTrue(b1.contains(b1x[i])); 227 } 228 229 assertTrue(b1.contains(b2x[b2x.length-1])); 231 232 for(int i=0; i<b2x.length-1; i++) { 237 assertTrue(b2.contains(b2x[i])); 238 } 239 240 for(int i=0; i<b1x.length; i++) { 245 assertTrue(a1.isIdentical(b1x[i].getOrder())); 246 } 247 248 for(int i=0; i<b2x.length-1; i++) { 253 assertTrue(a2.isIdentical(b2x[i].getOrder())); 254 } 255 256 assertTrue(a1.isIdentical(b2x[b2x.length-1].getOrder())); 258 } 259 260 public void test_a1GetB_removeB1n() { 262 264 a1.getLineItems().remove(b1x[b1x.length-1]); 266 267 269 assertTrue(b1x[b1x.length-1].getOrder() == null); 271 272 assertTrue(b1 == a1.getLineItems()); 274 275 for(int i=0; i<b1x.length-1; i++) { 280 assertTrue(b1.contains(b1x[i])); 281 } 282 283 assertTrue(!(b1.contains(b1x[b1x.length-1]))); 285 } 286 287 public void tearDownEJB() throws Exception { 288 OrderHome orderHome = getOrderHome(); 289 LineItemHome lineItemHome = getLineItemHome(); 290 291 deleteAllOrders(orderHome); 293 deleteAllLineItems(lineItemHome); 294 } 295 296 public void deleteAllOrders(OrderHome orderHome) throws Exception { 297 Iterator currentOrders = orderHome.findAll().iterator(); 299 while(currentOrders.hasNext()) { 300 Order o = (Order)currentOrders.next(); 301 o.remove(); 302 } 303 } 304 305 public void deleteAllLineItems(LineItemHome lineItemHome) throws Exception { 306 Iterator currentLineItems = lineItemHome.findAll().iterator(); 308 while(currentLineItems.hasNext()) { 309 LineItem l = (LineItem)currentLineItems.next(); 310 l.remove(); 311 } 312 } 313 } 314 315 316 317 | Popular Tags |