001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.cmp2.commerce;
023:
024: import java.util.Collection;
025: import java.util.Iterator;
026: import javax.naming.InitialContext;
027: import junit.framework.TestCase;
028: import org.jboss.test.util.ejb.EJBTestCase;
029:
030: public class ManyToOneUniTest extends EJBTestCase {
031:
032: public ManyToOneUniTest(String name) {
033: super (name);
034: }
035:
036: private ProductHome getProductHome() {
037: try {
038: InitialContext jndiContext = new InitialContext();
039:
040: return (ProductHome) jndiContext.lookup("commerce/Product");
041: } catch (Exception e) {
042: e.printStackTrace();
043: fail("Exception in getProduct: " + e.getMessage());
044: }
045: return null;
046: }
047:
048: private LineItemHome getLineItemHome() {
049: try {
050: InitialContext jndiContext = new InitialContext();
051:
052: return (LineItemHome) jndiContext
053: .lookup("commerce/LineItem");
054: } catch (Exception e) {
055: e.printStackTrace();
056: fail("Exception in getLineItemHome: " + e.getMessage());
057: }
058: return null;
059: }
060:
061: private Product a1;
062: private Product a2;
063:
064: private LineItem[] b1x = new LineItem[20];
065: private LineItem[] b2x = new LineItem[30];
066:
067: public void setUpEJB() throws Exception {
068: ProductHome productHome = getProductHome();
069: LineItemHome lineItemHome = getLineItemHome();
070:
071: // clean out the db
072: deleteAllProducts(productHome);
073: deleteAllLineItems(lineItemHome);
074:
075: // setup the before change part of the test
076: beforeChange(productHome, lineItemHome);
077: }
078:
079: private void beforeChange(ProductHome productHome,
080: LineItemHome lineItemHome) throws Exception {
081:
082: // Before change:
083: a1 = productHome.create();
084: a2 = productHome.create();
085:
086: for (int i = 0; i < b1x.length; i++) {
087: b1x[i] = lineItemHome.create();
088: b1x[i].setProduct(a1);
089: }
090:
091: for (int i = 0; i < b2x.length; i++) {
092: b2x[i] = lineItemHome.create();
093: b2x[i].setProduct(a2);
094: }
095:
096: // (a1.isIdentical(b11.getA())) && ... && (a1.isIdentical(b1n.getA()
097: for (int i = 0; i < b1x.length; i++) {
098: a1.isIdentical(b1x[i].getProduct());
099: }
100:
101: // (a2.isIdentical(b21.getA())) && ... && (a2.isIdentical(b2m.getA()
102: for (int i = 0; i < b2x.length; i++) {
103: a2.isIdentical(b2x[i].getProduct());
104: }
105: }
106:
107: // b1j.setA(b2k.getA());
108: public void test_b1jSetA_b2kGetA() {
109: // Change:
110:
111: // b1j.setA(b2k.getA());
112: int j = b1x.length / 3;
113: int k = b2x.length / 2;
114: b1x[j].setProduct(b2x[k].getProduct());
115:
116: // Expected result:
117:
118: // a1.isIdentical(b11.getA())
119: // a1.isIdentical(b12.getA())
120: // ...
121: // a2.isIdentical(b1j.getA())
122: // ...
123: // a1.isIdentical(b1n.getA())
124: for (int i = 0; i < b1x.length; i++) {
125: if (i != j) {
126: assertTrue(a1.isIdentical(b1x[i].getProduct()));
127: } else {
128: assertTrue(a2.isIdentical(b1x[i].getProduct()));
129: }
130: }
131:
132: // a2.isIdentical(b21.getA())
133: // a2.isIdentical(b22.getA())
134: // ...
135: // a2.isIdentical(b2k.getA())
136: // ...
137: // a2.isIdentical(b2m.getA())
138: for (int i = 0; i < b2x.length; i++) {
139: assertTrue(a2.isIdentical(b2x[i].getProduct()));
140: }
141: }
142:
143: public void tearDownEJB() throws Exception {
144: ProductHome productHome = getProductHome();
145: LineItemHome lineItemHome = getLineItemHome();
146: // clean out the db
147: deleteAllProducts(productHome);
148: deleteAllLineItems(lineItemHome);
149: }
150:
151: public void deleteAllProducts(ProductHome productHome)
152: throws Exception {
153: // delete all Products
154: Iterator currentProducts = productHome.findAll().iterator();
155: while (currentProducts.hasNext()) {
156: Product p = (Product) currentProducts.next();
157: p.remove();
158: }
159: }
160:
161: public void deleteAllLineItems(LineItemHome lineItemHome)
162: throws Exception {
163: // delete all LineItems
164: Iterator currentLineItems = lineItemHome.findAll().iterator();
165: while (currentLineItems.hasNext()) {
166: LineItem l = (LineItem) currentLineItems.next();
167: l.remove();
168: }
169: }
170: }
|