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 OneToManyBiTest extends EJBTestCase {
031:
032: public OneToManyBiTest(String name) {
033: super (name);
034: }
035:
036: private OrderHome getOrderHome() {
037: try {
038: InitialContext jndiContext = new InitialContext();
039:
040: return (OrderHome) jndiContext.lookup("commerce/Order");
041: } catch (Exception e) {
042: e.printStackTrace();
043: fail("Exception in getOrder: " + 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 Order a1;
062: private Order a2;
063:
064: private Collection b1;
065: private Collection b2;
066:
067: private LineItem[] b1x = new LineItem[20];
068: private LineItem[] b2x = new LineItem[30];
069:
070: public void setUpEJB() throws Exception {
071: OrderHome orderHome = getOrderHome();
072: LineItemHome lineItemHome = getLineItemHome();
073:
074: // clean out the db
075: deleteAllOrders(orderHome);
076: deleteAllLineItems(lineItemHome);
077:
078: // setup the before change part of the test
079: beforeChange(orderHome, lineItemHome);
080: }
081:
082: private void beforeChange(OrderHome orderHome,
083: LineItemHome lineItemHome) throws Exception {
084:
085: // Before change:
086: a1 = orderHome.create();
087: a2 = orderHome.create();
088:
089: b1 = a1.getLineItems();
090: b2 = a2.getLineItems();
091:
092: for (int i = 0; i < b1x.length; i++) {
093: b1x[i] = lineItemHome.create();
094: b1.add(b1x[i]);
095: }
096:
097: for (int i = 0; i < b2x.length; i++) {
098: b2x[i] = lineItemHome.create();
099: b2.add(b2x[i]);
100: }
101:
102: // B b11, b12, ... , b1n; members of b1
103: for (int i = 0; i < b1x.length; i++) {
104: assertTrue(b1.contains(b1x[i]));
105: }
106:
107: // B b21, b22, ... , b2m; members of b2
108: for (int i = 0; i < b2x.length; i++) {
109: assertTrue(b2.contains(b2x[i]));
110: }
111: }
112:
113: public void setUp() {
114: // get the collections again as they are only
115: // valid for the tx length, and were from last tx
116: b1 = a1.getLineItems();
117: b2 = a2.getLineItems();
118: }
119:
120: // a1.setB(a2.getB());
121: public void test_a1SetB_a2GetB() {
122: // Change:
123: a1.setLineItems(a2.getLineItems());
124:
125: // Expected result:
126:
127: // a2.getB().isEmpty()
128: assertTrue(a2.getLineItems().isEmpty());
129:
130: // b2.isEmpty()
131: assertTrue(b2.isEmpty());
132:
133: // b1 == a1.getB()
134: assertTrue(b1 == a1.getLineItems());
135:
136: // b2 == a2.getB()
137: assertTrue(b2 == a2.getLineItems());
138:
139: // a1.getB().contains(b21)
140: // a1.getB().contains(b22)
141: // a1.getB().contains(...)
142: // a1.getB().contains(b2m)
143: for (int i = 0; i < b2x.length; i++) {
144: assertTrue(a1.getLineItems().contains(b2x[i]));
145: }
146:
147: // b11.getA() == null
148: // b12.getA() == null
149: // ....getA() == null
150: // b1n.getA() == null
151: for (int i = 0; i < b1x.length; i++) {
152: assertTrue(b1x[i].getOrder() == null);
153: }
154:
155: // a1.isIdentical(b21.getA())
156: // a1.isIdentical(b22.getA())
157: // a1.isIdentical(....getA())
158: // a1.isIdentical(b2m.getA()))
159: for (int i = 0; i < b2x.length; i++) {
160: assertTrue(a1.isIdentical(b2x[i].getOrder()));
161: }
162: }
163:
164: // b2m.setA(b1n.getA());
165: public void test_b2mSetA_b1nGetA() {
166: // Change:
167:
168: // b2m.setA(b1n.getA());
169: b2x[b2x.length - 1].setOrder(b1x[b1x.length - 1].getOrder());
170:
171: // Expected result:
172:
173: // b1.contains(b11)
174: // b1.contains(b12)
175: // b1.contains(...)
176: // b1.contains(b1n)
177: for (int i = 0; i < b1x.length; i++) {
178: assertTrue(b1.contains(b1x[i]));
179: }
180:
181: // b1.contains(b2m)
182: assertTrue(b1.contains(b2x[b2x.length - 1]));
183:
184: // b2.contains(b21)
185: // b2.contains(b22)
186: // b2.contains(...)
187: // b2.contains(b2m_1)
188: for (int i = 0; i < b2x.length - 1; i++) {
189: assertTrue(b2.contains(b2x[i]));
190: }
191:
192: // a1.isIdentical(b11.getA())
193: // a1.isIdentical(b12.getA())
194: // a1.isIdentical(....getA())
195: // a1.isIdentical(b1n.getA())
196: for (int i = 0; i < b1x.length; i++) {
197: assertTrue(a1.isIdentical(b1x[i].getOrder()));
198: }
199:
200: // a2.isIdentical(b21.getA())
201: // a2.isIdentical(b22.getA())
202: // a2.isIdentical(....getA())
203: // a2.isIdentical(b2m_1.getA())
204: for (int i = 0; i < b2x.length - 1; i++) {
205: assertTrue(a2.isIdentical(b2x[i].getOrder()));
206: }
207:
208: // a1.isIdentical(b2m.getA())
209: assertTrue(a1.isIdentical(b2x[b2x.length - 1].getOrder()));
210: }
211:
212: // a1.getB().add(b2m);
213: public void test_a1GetB_addB2m() {
214: // Change:
215:
216: // a1.getB().add(b2m);
217: a1.getLineItems().add(b2x[b2x.length - 1]);
218:
219: // Expected result:
220:
221: // b1.contains(b11)
222: // b1.contains(b12)
223: // b1.contains(...)
224: // b1.contains(b1n)
225: for (int i = 0; i < b1x.length; i++) {
226: assertTrue(b1.contains(b1x[i]));
227: }
228:
229: // b1.contains(b2m)
230: assertTrue(b1.contains(b2x[b2x.length - 1]));
231:
232: // b2.contains(b21)
233: // b2.contains(b22)
234: // b2.contains(...)
235: // b2.contains(b2m_1)
236: for (int i = 0; i < b2x.length - 1; i++) {
237: assertTrue(b2.contains(b2x[i]));
238: }
239:
240: // a1.isIdentical(b11.getA())
241: // a1.isIdentical(b12.getA())
242: // a1.isIdentical(....getA())
243: // a1.isIdentical(b1n.getA())
244: for (int i = 0; i < b1x.length; i++) {
245: assertTrue(a1.isIdentical(b1x[i].getOrder()));
246: }
247:
248: // a2.isIdentical(b21.getA())
249: // a2.isIdentical(b22.getA())
250: // a2.isIdentical(....getA())
251: // a2.isIdentical(b2m_1.getA())
252: for (int i = 0; i < b2x.length - 1; i++) {
253: assertTrue(a2.isIdentical(b2x[i].getOrder()));
254: }
255:
256: // a1.isIdentical(b2m.getA())
257: assertTrue(a1.isIdentical(b2x[b2x.length - 1].getOrder()));
258: }
259:
260: // a1.getB().remove(b1n);
261: public void test_a1GetB_removeB1n() {
262: // Change:
263:
264: // a1.getB().remove(b1n);
265: a1.getLineItems().remove(b1x[b1x.length - 1]);
266:
267: // Expected result:
268:
269: // b1n.getA() == null
270: assertTrue(b1x[b1x.length - 1].getOrder() == null);
271:
272: // b1 == a1.getB()
273: assertTrue(b1 == a1.getLineItems());
274:
275: // b1.contains(b11)
276: // b1.contains(b12)
277: // b1.contains(...)
278: // b1.contains(b1n_1)
279: for (int i = 0; i < b1x.length - 1; i++) {
280: assertTrue(b1.contains(b1x[i]));
281: }
282:
283: // !(b1.contains(b1n))
284: 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: // clean out the db
292: deleteAllOrders(orderHome);
293: deleteAllLineItems(lineItemHome);
294: }
295:
296: public void deleteAllOrders(OrderHome orderHome) throws Exception {
297: // delete all Orders
298: 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)
306: throws Exception {
307: // delete all LineItems
308: Iterator currentLineItems = lineItemHome.findAll().iterator();
309: while (currentLineItems.hasNext()) {
310: LineItem l = (LineItem) currentLineItems.next();
311: l.remove();
312: }
313: }
314: }
|