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 ManyToManyBiTest extends EJBTestCase {
031:
032: public ManyToManyBiTest(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 ProductCategoryHome getProductCategoryHome() {
049: try {
050: InitialContext jndiContext = new InitialContext();
051:
052: return (ProductCategoryHome) jndiContext
053: .lookup("commerce/ProductCategory");
054: } catch (Exception e) {
055: e.printStackTrace();
056: fail("Exception in getProductCategory: " + e.getMessage());
057: }
058: return null;
059: }
060:
061: private Product a1;
062: private Product a2;
063: private Product a3;
064: private Product a4;
065: private Product a5;
066:
067: private ProductCategory b1;
068: private ProductCategory b2;
069: private ProductCategory b3;
070: private ProductCategory b4;
071: private ProductCategory b5;
072:
073: public void setUpEJB() throws Exception {
074: ProductHome productHome = getProductHome();
075: ProductCategoryHome productCategoryHome = getProductCategoryHome();
076:
077: // clean out the db
078: deleteAllProducts(productHome);
079: deleteAllProductCategories(productCategoryHome);
080:
081: // setup the before change part of the test
082: beforeChange(productHome, productCategoryHome);
083: }
084:
085: private void beforeChange(ProductHome productHome,
086: ProductCategoryHome productCategoryHome) throws Exception {
087:
088: // Before change:
089: a1 = productHome.create();
090: a2 = productHome.create();
091: a3 = productHome.create();
092: a4 = productHome.create();
093: a5 = productHome.create();
094:
095: b1 = productCategoryHome.create();
096: b2 = productCategoryHome.create();
097: b3 = productCategoryHome.create();
098: b4 = productCategoryHome.create();
099: b5 = productCategoryHome.create();
100:
101: a1.getProductCategories().add(b1);
102: a1.getProductCategories().add(b2);
103: a2.getProductCategories().add(b1);
104: a2.getProductCategories().add(b2);
105: a2.getProductCategories().add(b3);
106: a3.getProductCategories().add(b2);
107: a3.getProductCategories().add(b3);
108: a3.getProductCategories().add(b4);
109: a4.getProductCategories().add(b3);
110: a4.getProductCategories().add(b4);
111: a4.getProductCategories().add(b5);
112: a5.getProductCategories().add(b4);
113: a5.getProductCategories().add(b5);
114:
115: assertTrue(a1.getProductCategories().contains(b1));
116: assertTrue(a1.getProductCategories().contains(b2));
117: assertTrue(a2.getProductCategories().contains(b1));
118: assertTrue(a2.getProductCategories().contains(b2));
119: assertTrue(a2.getProductCategories().contains(b3));
120: assertTrue(a3.getProductCategories().contains(b2));
121: assertTrue(a3.getProductCategories().contains(b3));
122: assertTrue(a3.getProductCategories().contains(b4));
123: assertTrue(a4.getProductCategories().contains(b3));
124: assertTrue(a4.getProductCategories().contains(b4));
125: assertTrue(a4.getProductCategories().contains(b5));
126: assertTrue(a5.getProductCategories().contains(b4));
127: assertTrue(a5.getProductCategories().contains(b5));
128:
129: assertTrue(b1.getProducts().contains(a1));
130: assertTrue(b1.getProducts().contains(a2));
131: assertTrue(b2.getProducts().contains(a1));
132: assertTrue(b2.getProducts().contains(a2));
133: assertTrue(b2.getProducts().contains(a3));
134: assertTrue(b3.getProducts().contains(a2));
135: assertTrue(b3.getProducts().contains(a3));
136: assertTrue(b3.getProducts().contains(a4));
137: assertTrue(b4.getProducts().contains(a3));
138: assertTrue(b4.getProducts().contains(a4));
139: assertTrue(b4.getProducts().contains(a5));
140: assertTrue(b5.getProducts().contains(a4));
141: assertTrue(b5.getProducts().contains(a5));
142: }
143:
144: // a1.setB(a3.getB());
145: public void test_a1SetB_a3GetB() {
146: // Change:
147: a1.setProductCategories(a3.getProductCategories());
148:
149: // Expected result:
150: assertTrue(!a1.getProductCategories().contains(b1));
151: assertTrue(a1.getProductCategories().contains(b2));
152: assertTrue(a1.getProductCategories().contains(b3));
153: assertTrue(a1.getProductCategories().contains(b4));
154:
155: assertTrue(a2.getProductCategories().contains(b1));
156: assertTrue(a2.getProductCategories().contains(b2));
157: assertTrue(a2.getProductCategories().contains(b3));
158:
159: assertTrue(a3.getProductCategories().contains(b2));
160: assertTrue(a3.getProductCategories().contains(b3));
161: assertTrue(a3.getProductCategories().contains(b4));
162:
163: assertTrue(a4.getProductCategories().contains(b3));
164: assertTrue(a4.getProductCategories().contains(b4));
165: assertTrue(a4.getProductCategories().contains(b5));
166:
167: assertTrue(a5.getProductCategories().contains(b4));
168: assertTrue(a5.getProductCategories().contains(b5));
169:
170: assertTrue(!b1.getProducts().contains(a1));
171: assertTrue(b1.getProducts().contains(a2));
172:
173: assertTrue(b2.getProducts().contains(a1));
174: assertTrue(b2.getProducts().contains(a2));
175: assertTrue(b2.getProducts().contains(a3));
176:
177: assertTrue(b3.getProducts().contains(a1));
178: assertTrue(b3.getProducts().contains(a2));
179: assertTrue(b3.getProducts().contains(a3));
180: assertTrue(b3.getProducts().contains(a4));
181:
182: assertTrue(b4.getProducts().contains(a1));
183: assertTrue(b4.getProducts().contains(a3));
184: assertTrue(b4.getProducts().contains(a4));
185: assertTrue(b4.getProducts().contains(a5));
186:
187: assertTrue(b5.getProducts().contains(a4));
188: assertTrue(b5.getProducts().contains(a5));
189: }
190:
191: // a1.getB().add(b3);
192: public void test_a1GetB_addB3() {
193: // Change:
194: a1.getProductCategories().add(b3);
195:
196: // Expected result:
197: assertTrue(a1.getProductCategories().contains(b1));
198: assertTrue(a1.getProductCategories().contains(b2));
199: assertTrue(a1.getProductCategories().contains(b3));
200:
201: assertTrue(a2.getProductCategories().contains(b1));
202: assertTrue(a2.getProductCategories().contains(b2));
203: assertTrue(a2.getProductCategories().contains(b3));
204:
205: assertTrue(a3.getProductCategories().contains(b2));
206: assertTrue(a3.getProductCategories().contains(b3));
207: assertTrue(a3.getProductCategories().contains(b4));
208:
209: assertTrue(a4.getProductCategories().contains(b3));
210: assertTrue(a4.getProductCategories().contains(b4));
211: assertTrue(a4.getProductCategories().contains(b5));
212:
213: assertTrue(a5.getProductCategories().contains(b4));
214: assertTrue(a5.getProductCategories().contains(b5));
215:
216: assertTrue(b1.getProducts().contains(a1));
217: assertTrue(b1.getProducts().contains(a2));
218:
219: assertTrue(b2.getProducts().contains(a1));
220: assertTrue(b2.getProducts().contains(a2));
221: assertTrue(b2.getProducts().contains(a3));
222:
223: assertTrue(b3.getProducts().contains(a1));
224: assertTrue(b3.getProducts().contains(a2));
225: assertTrue(b3.getProducts().contains(a3));
226: assertTrue(b3.getProducts().contains(a4));
227:
228: assertTrue(b4.getProducts().contains(a3));
229: assertTrue(b4.getProducts().contains(a4));
230: assertTrue(b4.getProducts().contains(a5));
231:
232: assertTrue(b5.getProducts().contains(a4));
233: assertTrue(b5.getProducts().contains(a5));
234: }
235:
236: // a2.getB().remove(b2);
237: public void test_a2GetB_removeB2() {
238: // Change:
239: a2.getProductCategories().remove(b2);
240:
241: // Expected result:
242: assertTrue(a1.getProductCategories().contains(b1));
243: assertTrue(a1.getProductCategories().contains(b2));
244:
245: assertTrue(a2.getProductCategories().contains(b1));
246: assertTrue(!a2.getProductCategories().contains(b2));
247: assertTrue(a2.getProductCategories().contains(b3));
248:
249: assertTrue(a3.getProductCategories().contains(b2));
250: assertTrue(a3.getProductCategories().contains(b3));
251: assertTrue(a3.getProductCategories().contains(b4));
252:
253: assertTrue(a4.getProductCategories().contains(b3));
254: assertTrue(a4.getProductCategories().contains(b4));
255: assertTrue(a4.getProductCategories().contains(b5));
256:
257: assertTrue(a5.getProductCategories().contains(b4));
258: assertTrue(a5.getProductCategories().contains(b5));
259:
260: assertTrue(b1.getProducts().contains(a1));
261: assertTrue(b1.getProducts().contains(a2));
262:
263: assertTrue(b2.getProducts().contains(a1));
264: assertTrue(!b2.getProducts().contains(a2));
265: assertTrue(b2.getProducts().contains(a3));
266:
267: assertTrue(b3.getProducts().contains(a2));
268: assertTrue(b3.getProducts().contains(a3));
269: assertTrue(b3.getProducts().contains(a4));
270:
271: assertTrue(b4.getProducts().contains(a3));
272: assertTrue(b4.getProducts().contains(a4));
273: assertTrue(b4.getProducts().contains(a5));
274:
275: assertTrue(b5.getProducts().contains(a4));
276: assertTrue(b5.getProducts().contains(a5));
277: }
278:
279: public void tearDownEJB() throws Exception {
280: }
281:
282: public void deleteAllProducts(ProductHome productHome)
283: throws Exception {
284: // delete all Products
285: Iterator currentProducts = productHome.findAll().iterator();
286: while (currentProducts.hasNext()) {
287: Product p = (Product) currentProducts.next();
288: p.remove();
289: }
290: }
291:
292: public void deleteAllProductCategories(
293: ProductCategoryHome productCategoryHome) throws Exception {
294: // delete all ProductCategories
295: Iterator currentProductCategories = productCategoryHome
296: .findAll().iterator();
297: while (currentProductCategories.hasNext()) {
298: ProductCategory pc = (ProductCategory) currentProductCategories
299: .next();
300: pc.remove();
301: }
302: }
303:
304: }
|