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.oneToManyBidirectional;
023:
024: import java.util.Collection;
025: import java.util.Iterator;
026: import javax.naming.InitialContext;
027: import junit.framework.Test;
028: import org.jboss.test.util.ejb.EJBTestCase;
029: import org.jboss.test.JBossTestCase;
030:
031: public class ABTest extends EJBTestCase {
032: static org.apache.log4j.Category log = org.apache.log4j.Category
033: .getInstance(ABTest.class);
034:
035: public static Test suite() throws Exception {
036: return JBossTestCase.getDeploySetup(ABTest.class,
037: "cmp2-relationship.jar");
038: }
039:
040: public ABTest(String name) {
041: super (name);
042: }
043:
044: private AHome getTableAHome() {
045: try {
046: InitialContext jndiContext = new InitialContext();
047:
048: return (AHome) jndiContext
049: .lookup("relation/oneToMany/bidirectional/table/A");
050: } catch (Exception e) {
051: log.debug("failed", e);
052: fail("Exception in getTableAHome: " + e.getMessage());
053: }
054: return null;
055: }
056:
057: private BHome getTableBHome() {
058: try {
059: InitialContext jndiContext = new InitialContext();
060:
061: return (BHome) jndiContext
062: .lookup("relation/oneToMany/bidirectional/table/B");
063: } catch (Exception e) {
064: log.debug("failed", e);
065: fail("Exception in getTableBHome: " + e.getMessage());
066: }
067: return null;
068: }
069:
070: private AHome getFKAHome() {
071: try {
072: InitialContext jndiContext = new InitialContext();
073:
074: return (AHome) jndiContext
075: .lookup("relation/oneToMany/bidirectional/fk/A");
076: } catch (Exception e) {
077: log.debug("failed", e);
078: fail("Exception in getFKAHome: " + e.getMessage());
079: }
080: return null;
081: }
082:
083: private BHome getFKBHome() {
084: try {
085: InitialContext jndiContext = new InitialContext();
086:
087: return (BHome) jndiContext
088: .lookup("relation/oneToMany/bidirectional/fk/B");
089: } catch (Exception e) {
090: log.debug("failed", e);
091: fail("Exception in getFKBHome: " + e.getMessage());
092: }
093: return null;
094: }
095:
096: // a1.setB(a2.getB());
097: public void test_a1SetB_a2GetB_Table() throws Exception {
098: AHome aHome = getTableAHome();
099: BHome bHome = getTableBHome();
100: a1SetB_a2GetB(aHome, bHome);
101: }
102:
103: // a1.setB(a2.getB());
104: public void test_a1SetB_a2GetB_FK() throws Exception {
105: AHome aHome = getFKAHome();
106: BHome bHome = getFKBHome();
107: a1SetB_a2GetB(aHome, bHome);
108: }
109:
110: // a1.setB(a2.getB());
111: private void a1SetB_a2GetB(AHome aHome, BHome bHome)
112: throws Exception {
113: // Before change:
114: A a1 = aHome.create(new Integer(1));
115: A a2 = aHome.create(new Integer(2));
116:
117: Collection b1 = a1.getB();
118: Collection b2 = a2.getB();
119:
120: B[] b1x = new B[20];
121: B[] b2x = new B[30];
122:
123: for (int i = 0; i < b1x.length; i++) {
124: b1x[i] = bHome.create(new Integer(10000 + i));
125: b1.add(b1x[i]);
126: }
127:
128: for (int i = 0; i < b2x.length; i++) {
129: b2x[i] = bHome.create(new Integer(20000 + i));
130: b2.add(b2x[i]);
131: }
132:
133: // B b11, b12, ... , b1n; members of b1
134: for (int i = 0; i < b1x.length; i++) {
135: assertTrue(b1.contains(b1x[i]));
136: }
137:
138: // B b21, b22, ... , b2m; members of b2
139: for (int i = 0; i < b2x.length; i++) {
140: assertTrue(b2.contains(b2x[i]));
141: }
142:
143: // Change:
144: a1.setB(a2.getB());
145:
146: // Expected result:
147:
148: // a2.getB().isEmpty()
149: assertTrue(a2.getB().isEmpty());
150:
151: // b2.isEmpty()
152: assertTrue(b2.isEmpty());
153:
154: // b1 == a1.getB()
155: assertTrue(b1 == a1.getB());
156:
157: // b2 == a2.getB()
158: assertTrue(b2 == a2.getB());
159:
160: // a1.getB().contains(b21)
161: // a1.getB().contains(b22)
162: // a1.getB().contains(...)
163: // a1.getB().contains(b2m)
164: for (int i = 0; i < b2x.length; i++) {
165: assertTrue(a1.getB().contains(b2x[i]));
166: }
167:
168: // b11.getA() == null
169: // b12.getA() == null
170: // ....getA() == null
171: // b1n.getA() == null
172: for (int i = 0; i < b1x.length; i++) {
173: assertTrue(b1x[i].getA() == null);
174: }
175:
176: // a1.isIdentical(b21.getA())
177: // a1.isIdentical(b22.getA())
178: // a1.isIdentical(....getA())
179: // a1.isIdentical(b2m.getA()))
180: for (int i = 0; i < b2x.length; i++) {
181: assertTrue(a1.isIdentical(b2x[i].getA()));
182: }
183: }
184:
185: // b2m.setA(b1n.getA());
186: public void test_b2mSetA_b1nGetA_Table() throws Exception {
187: AHome aHome = getTableAHome();
188: BHome bHome = getTableBHome();
189: b2mSetA_b1nGetA(aHome, bHome);
190: }
191:
192: // b2m.setA(b1n.getA());
193: public void test_b2mSetA_b1nGetA_FK() throws Exception {
194: AHome aHome = getFKAHome();
195: BHome bHome = getFKBHome();
196: b2mSetA_b1nGetA(aHome, bHome);
197: }
198:
199: // b2m.setA(b1n.getA());
200: public void b2mSetA_b1nGetA(AHome aHome, BHome bHome)
201: throws Exception {
202: // Before change:
203: A a1 = aHome.create(new Integer(1));
204: A a2 = aHome.create(new Integer(2));
205:
206: Collection b1 = a1.getB();
207: Collection b2 = a2.getB();
208:
209: B[] b1x = new B[20];
210: B[] b2x = new B[30];
211:
212: for (int i = 0; i < b1x.length; i++) {
213: b1x[i] = bHome.create(new Integer(10000 + i));
214: b1.add(b1x[i]);
215: }
216:
217: for (int i = 0; i < b2x.length; i++) {
218: b2x[i] = bHome.create(new Integer(20000 + i));
219: b2.add(b2x[i]);
220: }
221:
222: // B b11, b12, ... , b1n; members of b1
223: for (int i = 0; i < b1x.length; i++) {
224: assertTrue(b1.contains(b1x[i]));
225: }
226:
227: // B b21, b22, ... , b2m; members of b2
228: for (int i = 0; i < b2x.length; i++) {
229: assertTrue(b2.contains(b2x[i]));
230: }
231:
232: // Change:
233:
234: // b2m.setA(b1n.getA());
235: b2x[b2x.length - 1].setA(b1x[b1x.length - 1].getA());
236:
237: // Expected result:
238:
239: // b1.contains(b11)
240: // b1.contains(b12)
241: // b1.contains(...)
242: // b1.contains(b1n)
243: for (int i = 0; i < b1x.length; i++) {
244: assertTrue(b1.contains(b1x[i]));
245: }
246:
247: // b1.contains(b2m)
248: assertTrue(b1.contains(b2x[b2x.length - 1]));
249:
250: // b2.contains(b21)
251: // b2.contains(b22)
252: // b2.contains(...)
253: // b2.contains(b2m_1)
254: for (int i = 0; i < b2x.length - 1; i++) {
255: assertTrue(b2.contains(b2x[i]));
256: }
257:
258: // a1.isIdentical(b11.getA())
259: // a1.isIdentical(b12.getA())
260: // a1.isIdentical(....getA())
261: // a1.isIdentical(b1n.getA())
262: for (int i = 0; i < b1x.length; i++) {
263: assertTrue(a1.isIdentical(b1x[i].getA()));
264: }
265:
266: // a2.isIdentical(b21.getA())
267: // a2.isIdentical(b22.getA())
268: // a2.isIdentical(....getA())
269: // a2.isIdentical(b2m_1.getA())
270: for (int i = 0; i < b2x.length - 1; i++) {
271: assertTrue(a2.isIdentical(b2x[i].getA()));
272: }
273:
274: // a1.isIdentical(b2m.getA())
275: assertTrue(a1.isIdentical(b2x[b2x.length - 1].getA()));
276: }
277:
278: // a1.getB().add(b2m);
279: public void test_a1GetB_addB2m_Table() throws Exception {
280: AHome aHome = getTableAHome();
281: BHome bHome = getTableBHome();
282: a1GetB_addB2m(aHome, bHome);
283: }
284:
285: // a1.getB().add(b2m);
286: public void test_a1GetB_addB2m_FK() throws Exception {
287: AHome aHome = getFKAHome();
288: BHome bHome = getFKBHome();
289: a1GetB_addB2m(aHome, bHome);
290: }
291:
292: // a1.getB().add(b2m);
293: private void a1GetB_addB2m(AHome aHome, BHome bHome)
294: throws Exception {
295: // Before change:
296: A a1 = aHome.create(new Integer(1));
297: A a2 = aHome.create(new Integer(2));
298:
299: Collection b1 = a1.getB();
300: Collection b2 = a2.getB();
301:
302: B[] b1x = new B[20];
303: B[] b2x = new B[30];
304:
305: for (int i = 0; i < b1x.length; i++) {
306: b1x[i] = bHome.create(new Integer(10000 + i));
307: b1.add(b1x[i]);
308: }
309:
310: for (int i = 0; i < b2x.length; i++) {
311: b2x[i] = bHome.create(new Integer(20000 + i));
312: b2.add(b2x[i]);
313: }
314:
315: // B b11, b12, ... , b1n; members of b1
316: for (int i = 0; i < b1x.length; i++) {
317: assertTrue(b1.contains(b1x[i]));
318: }
319:
320: // B b21, b22, ... , b2m; members of b2
321: for (int i = 0; i < b2x.length; i++) {
322: assertTrue(b2.contains(b2x[i]));
323: }
324:
325: // Change:
326:
327: // a1.getB().add(b2m);
328: a1.getB().add(b2x[b2x.length - 1]);
329:
330: // Expected result:
331:
332: // b1.contains(b11)
333: // b1.contains(b12)
334: // b1.contains(...)
335: // b1.contains(b1n)
336: for (int i = 0; i < b1x.length; i++) {
337: assertTrue(b1.contains(b1x[i]));
338: }
339:
340: // b1.contains(b2m)
341: assertTrue(b1.contains(b2x[b2x.length - 1]));
342:
343: // b2.contains(b21)
344: // b2.contains(b22)
345: // b2.contains(...)
346: // b2.contains(b2m_1)
347: for (int i = 0; i < b2x.length - 1; i++) {
348: assertTrue(b2.contains(b2x[i]));
349: }
350:
351: // a1.isIdentical(b11.getA())
352: // a1.isIdentical(b12.getA())
353: // a1.isIdentical(....getA())
354: // a1.isIdentical(b1n.getA())
355: for (int i = 0; i < b1x.length; i++) {
356: assertTrue(a1.isIdentical(b1x[i].getA()));
357: }
358:
359: // a2.isIdentical(b21.getA())
360: // a2.isIdentical(b22.getA())
361: // a2.isIdentical(....getA())
362: // a2.isIdentical(b2m_1.getA())
363: for (int i = 0; i < b2x.length - 1; i++) {
364: assertTrue(a2.isIdentical(b2x[i].getA()));
365: }
366:
367: // a1.isIdentical(b2m.getA())
368: assertTrue(a1.isIdentical(b2x[b2x.length - 1].getA()));
369: }
370:
371: // a1.getB().remove(b1n);
372: public void test_a1GetB_removeB1n_Table() throws Exception {
373: AHome aHome = getTableAHome();
374: BHome bHome = getTableBHome();
375: a1GetB_removeB1n(aHome, bHome);
376: }
377:
378: // a1.getB().remove(b1n);
379: public void test_a1GetB_removeB1n_FK() throws Exception {
380: AHome aHome = getFKAHome();
381: BHome bHome = getFKBHome();
382: a1GetB_removeB1n(aHome, bHome);
383: }
384:
385: // a1.getB().remove(b1n);
386: private void a1GetB_removeB1n(AHome aHome, BHome bHome)
387: throws Exception {
388: // Before change:
389: A a1 = aHome.create(new Integer(1));
390: A a2 = aHome.create(new Integer(2));
391:
392: Collection b1 = a1.getB();
393: Collection b2 = a2.getB();
394:
395: B[] b1x = new B[20];
396: B[] b2x = new B[30];
397:
398: for (int i = 0; i < b1x.length; i++) {
399: b1x[i] = bHome.create(new Integer(10000 + i));
400: b1.add(b1x[i]);
401: }
402:
403: for (int i = 0; i < b2x.length; i++) {
404: b2x[i] = bHome.create(new Integer(20000 + i));
405: b2.add(b2x[i]);
406: }
407:
408: // B b11, b12, ... , b1n; members of b1
409: for (int i = 0; i < b1x.length; i++) {
410: assertTrue(b1.contains(b1x[i]));
411: }
412:
413: // B b21, b22, ... , b2m; members of b2
414: for (int i = 0; i < b2x.length; i++) {
415: assertTrue(b2.contains(b2x[i]));
416: }
417:
418: // Change:
419:
420: // a1.getB().remove(b1n);
421: a1.getB().remove(b1x[b1x.length - 1]);
422:
423: // Expected result:
424:
425: // b1n.getA() == null
426: assertTrue(b1x[b1x.length - 1].getA() == null);
427:
428: // b1 == a1.getB()
429: assertTrue(b1 == a1.getB());
430:
431: // b1.contains(b11)
432: // b1.contains(b12)
433: // b1.contains(...)
434: // b1.contains(b1n_1)
435: for (int i = 0; i < b1x.length - 1; i++) {
436: assertTrue(b1.contains(b1x[i]));
437: }
438:
439: // !(b1.contains(b1n))
440: assertTrue(!(b1.contains(b1x[b1x.length - 1])));
441: }
442:
443: public void setUpEJB() throws Exception {
444: AHome aHome;
445: BHome bHome;
446:
447: aHome = getTableAHome();
448: bHome = getTableBHome();
449: deleteAllAsAndBs(aHome, bHome);
450:
451: aHome = getFKAHome();
452: bHome = getFKBHome();
453: deleteAllAsAndBs(aHome, bHome);
454: }
455:
456: public void tearDownEJB() throws Exception {
457: }
458:
459: public void deleteAllAsAndBs(AHome aHome, BHome bHome)
460: throws Exception {
461: // delete all As
462: Iterator currentAs = aHome.findAll().iterator();
463: while (currentAs.hasNext()) {
464: A a = (A) currentAs.next();
465: a.remove();
466: }
467:
468: // delete all Bs
469: Iterator currentBs = bHome.findAll().iterator();
470: while (currentBs.hasNext()) {
471: B b = (B) currentBs.next();
472: b.remove();
473: }
474: }
475: }
|