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.relationship.manyToManyBidirectional;
023:
024: import java.util.Collection;
025: import java.util.Iterator;
026: import javax.naming.InitialContext;
027: import junit.framework.Test;
028: import junit.framework.TestCase;
029: import org.jboss.test.util.ejb.EJBTestCase;
030: import org.jboss.test.JBossTestCase;
031:
032: public class ABTest extends EJBTestCase {
033:
034: static org.apache.log4j.Category log = org.apache.log4j.Category
035: .getInstance(ABTest.class);
036:
037: public static Test suite() throws Exception {
038: return JBossTestCase.getDeploySetup(ABTest.class,
039: "cmp2-relationship.jar");
040: }
041:
042: public ABTest(String name) {
043: super (name);
044: }
045:
046: private AHome getAHome() {
047: try {
048: InitialContext jndiContext = new InitialContext();
049:
050: return (AHome) jndiContext
051: .lookup("relation/manyToMany/bidirectional/A");
052: } catch (Exception e) {
053: log.debug("failed", e);
054: fail("Exception in getAHome: " + e.getMessage());
055: }
056: return null;
057: }
058:
059: private BHome getBHome() {
060: try {
061: InitialContext jndiContext = new InitialContext();
062:
063: return (BHome) jndiContext
064: .lookup("relation/manyToMany/bidirectional/B");
065: } catch (Exception e) {
066: log.debug("failed", e);
067: fail("Exception in getBHome: " + e.getMessage());
068: }
069: return null;
070: }
071:
072: // a1.setB(a3.getB());
073: public void test_a1SetB_a3GetB() throws Exception {
074: AHome aHome = getAHome();
075: BHome bHome = getBHome();
076:
077: // Before change:
078: A a1 = aHome.create(new Integer(1));
079: A a2 = aHome.create(new Integer(2));
080: A a3 = aHome.create(new Integer(3));
081: A a4 = aHome.create(new Integer(4));
082: A a5 = aHome.create(new Integer(5));
083:
084: B b1 = bHome.create(new Integer(-1));
085: B b2 = bHome.create(new Integer(-2));
086: B b3 = bHome.create(new Integer(-3));
087: B b4 = bHome.create(new Integer(-4));
088: B b5 = bHome.create(new Integer(-5));
089:
090: a1.getB().add(b1);
091: a1.getB().add(b2);
092: a2.getB().add(b1);
093: a2.getB().add(b2);
094: a2.getB().add(b3);
095: a3.getB().add(b2);
096: a3.getB().add(b3);
097: a3.getB().add(b4);
098: a4.getB().add(b3);
099: a4.getB().add(b4);
100: a4.getB().add(b5);
101: a5.getB().add(b4);
102: a5.getB().add(b5);
103:
104: assertTrue(a1.getB().contains(b1));
105: assertTrue(a1.getB().contains(b2));
106: assertTrue(a2.getB().contains(b1));
107: assertTrue(a2.getB().contains(b2));
108: assertTrue(a2.getB().contains(b3));
109: assertTrue(a3.getB().contains(b2));
110: assertTrue(a3.getB().contains(b3));
111: assertTrue(a3.getB().contains(b4));
112: assertTrue(a4.getB().contains(b3));
113: assertTrue(a4.getB().contains(b4));
114: assertTrue(a4.getB().contains(b5));
115: assertTrue(a5.getB().contains(b4));
116: assertTrue(a5.getB().contains(b5));
117:
118: assertTrue(b1.getA().contains(a1));
119: assertTrue(b1.getA().contains(a2));
120: assertTrue(b2.getA().contains(a1));
121: assertTrue(b2.getA().contains(a2));
122: assertTrue(b2.getA().contains(a3));
123: assertTrue(b3.getA().contains(a2));
124: assertTrue(b3.getA().contains(a3));
125: assertTrue(b3.getA().contains(a4));
126: assertTrue(b4.getA().contains(a3));
127: assertTrue(b4.getA().contains(a4));
128: assertTrue(b4.getA().contains(a5));
129: assertTrue(b5.getA().contains(a4));
130: assertTrue(b5.getA().contains(a5));
131:
132: // Change:
133: a1.setB(a3.getB());
134:
135: // Expected result:
136: assertTrue(!a1.getB().contains(b1));
137: assertTrue(a1.getB().contains(b2));
138: assertTrue(a1.getB().contains(b3));
139: assertTrue(a1.getB().contains(b4));
140:
141: assertTrue(a2.getB().contains(b1));
142: assertTrue(a2.getB().contains(b2));
143: assertTrue(a2.getB().contains(b3));
144:
145: assertTrue(a3.getB().contains(b2));
146: assertTrue(a3.getB().contains(b3));
147: assertTrue(a3.getB().contains(b4));
148:
149: assertTrue(a4.getB().contains(b3));
150: assertTrue(a4.getB().contains(b4));
151: assertTrue(a4.getB().contains(b5));
152:
153: assertTrue(a5.getB().contains(b4));
154: assertTrue(a5.getB().contains(b5));
155:
156: assertTrue(!b1.getA().contains(a1));
157: assertTrue(b1.getA().contains(a2));
158:
159: assertTrue(b2.getA().contains(a1));
160: assertTrue(b2.getA().contains(a2));
161: assertTrue(b2.getA().contains(a3));
162:
163: assertTrue(b3.getA().contains(a1));
164: assertTrue(b3.getA().contains(a2));
165: assertTrue(b3.getA().contains(a3));
166: assertTrue(b3.getA().contains(a4));
167:
168: assertTrue(b4.getA().contains(a1));
169: assertTrue(b4.getA().contains(a3));
170: assertTrue(b4.getA().contains(a4));
171: assertTrue(b4.getA().contains(a5));
172:
173: assertTrue(b5.getA().contains(a4));
174: assertTrue(b5.getA().contains(a5));
175: }
176:
177: // a1.getB().add(b3);
178: public void test_a1GetB_addB3() throws Exception {
179: AHome aHome = getAHome();
180: BHome bHome = getBHome();
181:
182: // Before change:
183: A a1 = aHome.create(new Integer(1));
184: A a2 = aHome.create(new Integer(2));
185: A a3 = aHome.create(new Integer(3));
186: A a4 = aHome.create(new Integer(4));
187: A a5 = aHome.create(new Integer(5));
188:
189: B b1 = bHome.create(new Integer(-1));
190: B b2 = bHome.create(new Integer(-2));
191: B b3 = bHome.create(new Integer(-3));
192: B b4 = bHome.create(new Integer(-4));
193: B b5 = bHome.create(new Integer(-5));
194:
195: a1.getB().add(b1);
196: a1.getB().add(b2);
197: a2.getB().add(b1);
198: a2.getB().add(b2);
199: a2.getB().add(b3);
200: a3.getB().add(b2);
201: a3.getB().add(b3);
202: a3.getB().add(b4);
203: a4.getB().add(b3);
204: a4.getB().add(b4);
205: a4.getB().add(b5);
206: a5.getB().add(b4);
207: a5.getB().add(b5);
208:
209: assertTrue(a1.getB().contains(b1));
210: assertTrue(a1.getB().contains(b2));
211: assertTrue(a2.getB().contains(b1));
212: assertTrue(a2.getB().contains(b2));
213: assertTrue(a2.getB().contains(b3));
214: assertTrue(a3.getB().contains(b2));
215: assertTrue(a3.getB().contains(b3));
216: assertTrue(a3.getB().contains(b4));
217: assertTrue(a4.getB().contains(b3));
218: assertTrue(a4.getB().contains(b4));
219: assertTrue(a4.getB().contains(b5));
220: assertTrue(a5.getB().contains(b4));
221: assertTrue(a5.getB().contains(b5));
222:
223: assertTrue(b1.getA().contains(a1));
224: assertTrue(b1.getA().contains(a2));
225: assertTrue(b2.getA().contains(a1));
226: assertTrue(b2.getA().contains(a2));
227: assertTrue(b2.getA().contains(a3));
228: assertTrue(b3.getA().contains(a2));
229: assertTrue(b3.getA().contains(a3));
230: assertTrue(b3.getA().contains(a4));
231: assertTrue(b4.getA().contains(a3));
232: assertTrue(b4.getA().contains(a4));
233: assertTrue(b4.getA().contains(a5));
234: assertTrue(b5.getA().contains(a4));
235: assertTrue(b5.getA().contains(a5));
236:
237: // Change:
238: a1.getB().add(b3);
239:
240: // Expected result:
241: assertTrue(a1.getB().contains(b1));
242: assertTrue(a1.getB().contains(b2));
243: assertTrue(a1.getB().contains(b3));
244:
245: assertTrue(a2.getB().contains(b1));
246: assertTrue(a2.getB().contains(b2));
247: assertTrue(a2.getB().contains(b3));
248:
249: assertTrue(a3.getB().contains(b2));
250: assertTrue(a3.getB().contains(b3));
251: assertTrue(a3.getB().contains(b4));
252:
253: assertTrue(a4.getB().contains(b3));
254: assertTrue(a4.getB().contains(b4));
255: assertTrue(a4.getB().contains(b5));
256:
257: assertTrue(a5.getB().contains(b4));
258: assertTrue(a5.getB().contains(b5));
259:
260: assertTrue(b1.getA().contains(a1));
261: assertTrue(b1.getA().contains(a2));
262:
263: assertTrue(b2.getA().contains(a1));
264: assertTrue(b2.getA().contains(a2));
265: assertTrue(b2.getA().contains(a3));
266:
267: assertTrue(b3.getA().contains(a1));
268: assertTrue(b3.getA().contains(a2));
269: assertTrue(b3.getA().contains(a3));
270: assertTrue(b3.getA().contains(a4));
271:
272: assertTrue(b4.getA().contains(a3));
273: assertTrue(b4.getA().contains(a4));
274: assertTrue(b4.getA().contains(a5));
275:
276: assertTrue(b5.getA().contains(a4));
277: assertTrue(b5.getA().contains(a5));
278: }
279:
280: // a2.getB().remove(b2);
281: public void test_a2GetB_removeB2() throws Exception {
282: AHome aHome = getAHome();
283: BHome bHome = getBHome();
284:
285: // Before change:
286: A a1 = aHome.create(new Integer(1));
287: A a2 = aHome.create(new Integer(2));
288: A a3 = aHome.create(new Integer(3));
289: A a4 = aHome.create(new Integer(4));
290: A a5 = aHome.create(new Integer(5));
291:
292: B b1 = bHome.create(new Integer(-1));
293: B b2 = bHome.create(new Integer(-2));
294: B b3 = bHome.create(new Integer(-3));
295: B b4 = bHome.create(new Integer(-4));
296: B b5 = bHome.create(new Integer(-5));
297:
298: a1.getB().add(b1);
299: a1.getB().add(b2);
300: a2.getB().add(b1);
301: a2.getB().add(b2);
302: a2.getB().add(b3);
303: a3.getB().add(b2);
304: a3.getB().add(b3);
305: a3.getB().add(b4);
306: a4.getB().add(b3);
307: a4.getB().add(b4);
308: a4.getB().add(b5);
309: a5.getB().add(b4);
310: a5.getB().add(b5);
311:
312: assertTrue(a1.getB().contains(b1));
313: assertTrue(a1.getB().contains(b2));
314: assertTrue(a2.getB().contains(b1));
315: assertTrue(a2.getB().contains(b2));
316: assertTrue(a2.getB().contains(b3));
317: assertTrue(a3.getB().contains(b2));
318: assertTrue(a3.getB().contains(b3));
319: assertTrue(a3.getB().contains(b4));
320: assertTrue(a4.getB().contains(b3));
321: assertTrue(a4.getB().contains(b4));
322: assertTrue(a4.getB().contains(b5));
323: assertTrue(a5.getB().contains(b4));
324: assertTrue(a5.getB().contains(b5));
325:
326: assertTrue(b1.getA().contains(a1));
327: assertTrue(b1.getA().contains(a2));
328: assertTrue(b2.getA().contains(a1));
329: assertTrue(b2.getA().contains(a2));
330: assertTrue(b2.getA().contains(a3));
331: assertTrue(b3.getA().contains(a2));
332: assertTrue(b3.getA().contains(a3));
333: assertTrue(b3.getA().contains(a4));
334: assertTrue(b4.getA().contains(a3));
335: assertTrue(b4.getA().contains(a4));
336: assertTrue(b4.getA().contains(a5));
337: assertTrue(b5.getA().contains(a4));
338: assertTrue(b5.getA().contains(a5));
339:
340: // Change:
341: a2.getB().remove(b2);
342:
343: // Expected result:
344: assertTrue(a1.getB().contains(b1));
345: assertTrue(a1.getB().contains(b2));
346:
347: assertTrue(a2.getB().contains(b1));
348: assertTrue(!a2.getB().contains(b2));
349: assertTrue(a2.getB().contains(b3));
350:
351: assertTrue(a3.getB().contains(b2));
352: assertTrue(a3.getB().contains(b3));
353: assertTrue(a3.getB().contains(b4));
354:
355: assertTrue(a4.getB().contains(b3));
356: assertTrue(a4.getB().contains(b4));
357: assertTrue(a4.getB().contains(b5));
358:
359: assertTrue(a5.getB().contains(b4));
360: assertTrue(a5.getB().contains(b5));
361:
362: assertTrue(b1.getA().contains(a1));
363: assertTrue(b1.getA().contains(a2));
364:
365: assertTrue(b2.getA().contains(a1));
366: assertTrue(!b2.getA().contains(a2));
367: assertTrue(b2.getA().contains(a3));
368:
369: assertTrue(b3.getA().contains(a2));
370: assertTrue(b3.getA().contains(a3));
371: assertTrue(b3.getA().contains(a4));
372:
373: assertTrue(b4.getA().contains(a3));
374: assertTrue(b4.getA().contains(a4));
375: assertTrue(b4.getA().contains(a5));
376:
377: assertTrue(b5.getA().contains(a4));
378: assertTrue(b5.getA().contains(a5));
379: }
380:
381: public void setUpEJB() throws Exception {
382: deleteAllAsAndBs(getAHome(), getBHome());
383: }
384:
385: public void tearDownEJB() throws Exception {
386: }
387:
388: public void deleteAllAsAndBs(AHome aHome, BHome bHome)
389: throws Exception {
390: // delete all As
391: Iterator currentAs = aHome.findAll().iterator();
392: while (currentAs.hasNext()) {
393: A a = (A) currentAs.next();
394: a.remove();
395: }
396:
397: // delete all Bs
398: Iterator currentBs = bHome.findAll().iterator();
399: while (currentBs.hasNext()) {
400: B b = (B) currentBs.next();
401: b.remove();
402: }
403: }
404: }
|