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 ManyToOneUniTest extends EJBTestCase { 31 32 public ManyToOneUniTest(String name) { 33 super(name); 34 } 35 36 private ProductHome getProductHome() { 37 try { 38 InitialContext jndiContext = new InitialContext (); 39 40 return (ProductHome) jndiContext.lookup("commerce/Product"); 41 } catch(Exception e) { 42 e.printStackTrace(); 43 fail("Exception in getProduct: " + 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 Product a1; 61 private Product a2; 62 63 private LineItem[] b1x = new LineItem[20]; 64 private LineItem[] b2x = new LineItem[30]; 65 66 public void setUpEJB() throws Exception { 67 ProductHome productHome = getProductHome(); 68 LineItemHome lineItemHome = getLineItemHome(); 69 70 deleteAllProducts(productHome); 72 deleteAllLineItems(lineItemHome); 73 74 beforeChange(productHome, lineItemHome); 76 } 77 78 private void beforeChange(ProductHome productHome, LineItemHome lineItemHome) 79 throws Exception { 80 81 a1 = productHome.create(); 83 a2 = productHome.create(); 84 85 for(int i=0; i<b1x.length; i++) { 86 b1x[i] = lineItemHome.create(); 87 b1x[i].setProduct(a1); 88 } 89 90 for(int i=0; i<b2x.length; i++) { 91 b2x[i] = lineItemHome.create(); 92 b2x[i].setProduct(a2); 93 } 94 95 for(int i=0; i<b1x.length; i++) { 97 a1.isIdentical(b1x[i].getProduct()); 98 } 99 100 for(int i=0; i<b2x.length; i++) { 102 a2.isIdentical(b2x[i].getProduct()); 103 } 104 } 105 106 public void test_b1jSetA_b2kGetA() { 108 110 int j = b1x.length / 3; 112 int k = b2x.length / 2; 113 b1x[j].setProduct(b2x[k].getProduct()); 114 115 117 for(int i=0; i<b1x.length; i++) { 124 if(i != j) { 125 assertTrue(a1.isIdentical(b1x[i].getProduct())); 126 } else { 127 assertTrue(a2.isIdentical(b1x[i].getProduct())); 128 } 129 } 130 131 for(int i=0; i<b2x.length; i++) { 138 assertTrue(a2.isIdentical(b2x[i].getProduct())); 139 } 140 } 141 142 public void tearDownEJB() throws Exception { 143 ProductHome productHome = getProductHome(); 144 LineItemHome lineItemHome = getLineItemHome(); 145 deleteAllProducts(productHome); 147 deleteAllLineItems(lineItemHome); 148 } 149 150 public void deleteAllProducts(ProductHome productHome) throws Exception { 151 Iterator currentProducts = productHome.findAll().iterator(); 153 while(currentProducts.hasNext()) { 154 Product p = (Product)currentProducts.next(); 155 p.remove(); 156 } 157 } 158 159 public void deleteAllLineItems(LineItemHome lineItemHome) throws Exception { 160 Iterator currentLineItems = lineItemHome.findAll().iterator(); 162 while(currentLineItems.hasNext()) { 163 LineItem l = (LineItem)currentLineItems.next(); 164 l.remove(); 165 } 166 } 167 } 168 169 170 171 | Popular Tags |