001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: A_mnb.java 10108 2007-03-28 09:21:26Z durieuxp $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.jtests.clients.entity;
025:
026: import java.util.ArrayList;
027: import java.util.Collection;
028: import java.util.Enumeration;
029: import java.util.Hashtable;
030:
031: import javax.naming.NamingException;
032: import javax.rmi.PortableRemoteObject;
033:
034: import junit.framework.Test;
035: import junit.framework.TestSuite;
036:
037: import org.objectweb.jonas.jtests.beans.relation.mnb.AHomeRemote;
038: import org.objectweb.jonas.jtests.beans.relation.mnb.ARemote;
039: import org.objectweb.jonas.jtests.beans.relation.mnb.BHomeRemote;
040: import org.objectweb.jonas.jtests.beans.relation.mnb.BRemote;
041:
042: /**
043: * For testing many-to-many bidirectional relationships
044: * @author Ph Durieux
045: **/
046: public abstract class A_mnb extends A_Cmp2Util {
047:
048: public abstract AHomeRemote getAHome();
049:
050: public abstract BHomeRemote getBHome();
051:
052: static Hashtable tbRelationA2B = new Hashtable();
053:
054: static {
055: tbRelationA2B.put("a0", new String[] {});
056: tbRelationA2B.put("a1", new String[] { "b1", "b2" });
057: tbRelationA2B.put("a2", new String[] { "b1", "b2", "b3" });
058: tbRelationA2B.put("a3", new String[] { "b2", "b3", "b4" });
059: // Translate the String[] to a Collection of String
060: for (Enumeration ea = tbRelationA2B.keys(); ea
061: .hasMoreElements();) {
062: String aname = (String) (ea.nextElement());
063: String[] tb = (String[]) tbRelationA2B.get(aname);
064: ArrayList col = new ArrayList(tb.length);
065: for (int i = 0; i < tb.length; i++) {
066: col.add(tb[i]);
067: }
068: tbRelationA2B.put(aname, col);
069: }
070: }
071:
072: static Hashtable tbRelationB2A = new Hashtable();
073:
074: static {
075: tbRelationB2A.put("b0", new String[] {});
076: tbRelationB2A.put("b1", new String[] { "a1", "a2" });
077: tbRelationB2A.put("b2", new String[] { "a1", "a2", "a3" });
078: tbRelationB2A.put("b3", new String[] { "a2", "a3" });
079: tbRelationB2A.put("b4", new String[] { "a3" });
080: // Translate the String[] to a Collection of String
081: for (Enumeration ea1 = tbRelationB2A.keys(); ea1
082: .hasMoreElements();) {
083: String aname1 = (String) (ea1.nextElement());
084: String[] tb1 = (String[]) tbRelationB2A.get(aname1);
085: ArrayList col1 = new ArrayList(tb1.length);
086: for (int j = 0; j < tb1.length; j++) {
087: col1.add(tb1[j]);
088: }
089: tbRelationB2A.put(aname1, col1);
090: }
091: }
092:
093: public A_mnb(String name) {
094: super (name);
095: }
096:
097: protected boolean isInit = false;
098:
099: protected void setUp() {
100: super .setUp();
101: boolean ok = false;
102: int nbtry = 0;
103: while (!ok && nbtry < 3) {
104: if (!isInit) {
105: // load bean if not loaded yet
106: useBeans("mnb", false);
107: // check if tables have been initialized
108: try {
109: getAHome().findByPrimaryKey("a0");
110: } catch (Exception e) {
111: try {
112: // IMPORTANT: comments begin/commit to highlight a bug
113: utx.begin();
114: getAHome().create("a0");
115: getAHome().create("a1");
116: getAHome().create("a2");
117: getAHome().create("a3");
118: getBHome().create("b0");
119: getBHome().create("b1");
120: getBHome().create("b2");
121: getBHome().create("b3");
122: getBHome().create("b4");
123: ARemote a0 = getAHome().findByPrimaryKey("a0");
124: ARemote a1 = getAHome().findByPrimaryKey("a1");
125: ARemote a2 = getAHome().findByPrimaryKey("a2");
126: ARemote a3 = getAHome().findByPrimaryKey("a3");
127: Collection rel_a0 = (Collection) (tbRelationA2B
128: .get("a0"));
129: a0.assignB(rel_a0);
130: Collection rel_a1 = (Collection) (tbRelationA2B
131: .get("a1"));
132: a1.assignB(rel_a1);
133: Collection rel_a2 = (Collection) (tbRelationA2B
134: .get("a2"));
135: a2.assignB(rel_a2);
136: Collection rel_a3 = (Collection) (tbRelationA2B
137: .get("a3"));
138: a3.assignB(rel_a3);
139: } catch (Exception i) {
140: fail("InitialState creation problem: " + i);
141: } finally {
142: try {
143: utx.commit();
144: } catch (Exception ii) {
145: }
146: }
147: }
148: isInit = true;
149: }
150: // Check that all is OK. Sometimes, a test has failed and has corrupted
151: // the bean state in the database. We must unload and reload the bean then.
152: nbtry++;
153: try {
154: if (initStateOK()) {
155: ok = true;
156: }
157: } catch (Exception e) {
158: }
159: if (!ok) {
160: debug("F_Relation_mnbEC2 setUp: unload bean and retry");
161: isInit = false;
162: unloadBeans("mnb");
163: }
164: }
165: }
166:
167: /*
168: * Check that we are in the same state as after the tables creation for thoses beans A and B
169: * (ie if it is the initial state)
170: */
171: boolean initStateOK() throws Exception {
172: boolean isOk = true;
173: msgerror = new StringBuffer();
174: // Check relations A to B
175: for (Enumeration ea = tbRelationA2B.keys(); ea
176: .hasMoreElements();) {
177: String aname = (String) (ea.nextElement());
178: ARemote a = getAHome().findByPrimaryKey(aname);
179: Collection colActual = a.retrieveB();
180: ArrayList colExpected = (ArrayList) (tbRelationA2B
181: .get(aname));
182: if (!isCollectionEqual(colExpected, colActual)) {
183: isOk = false;
184: msgerror = msgerror.append("Wrong relation for "
185: + aname + " (expected:" + colExpected
186: + ", found:" + colActual + ")");
187: }
188: }
189: // Check Relations B to A
190: for (Enumeration ea1 = tbRelationB2A.keys(); ea1
191: .hasMoreElements();) {
192: String aname1 = (String) (ea1.nextElement());
193: BRemote b = getBHome().findByPrimaryKey(aname1);
194: Collection colActual1 = b.retrieveA();
195: ArrayList colExpected1 = (ArrayList) (tbRelationB2A
196: .get(aname1));
197: if (!isCollectionEqual(colExpected1, colActual1)) {
198: isOk = false;
199: msgerror = msgerror.append("Wrong relation for "
200: + aname1 + " (expected:" + colExpected1
201: + ", found:" + colActual1 + ")");
202: }
203: }
204: return isOk;
205: }
206:
207: /**
208: * Check that the bean 'a0' has no relation.
209: */
210: public void _testBasicGetEmpty(int tx) throws Exception {
211: Collection c;
212: if ((tx == TX_CALL) || (tx == TX_RB)) {
213: utx.begin();
214: }
215: ARemote a = getAHome().findByPrimaryKey("a0");
216: if (tx == TX_CONT) {
217: c = a.retrieveBInNewTx();
218: } else {
219: c = a.retrieveB();
220: }
221: if (tx == TX_CALL) {
222: utx.commit();
223: } else if (tx == TX_RB) {
224: utx.rollback();
225: }
226: checkIsInitialState();
227: }
228:
229: public void testBasicGetEmptyTxNo() throws Exception {
230: _testBasicGetEmpty(TX_NO);
231: }
232:
233: public void testBasicGetEmptyTxCall() throws Exception {
234: _testBasicGetEmpty(TX_CALL);
235: }
236:
237: public void testBasicGetEmptyTxCont() throws Exception {
238: _testBasicGetEmpty(TX_CONT);
239: }
240:
241: public void testBasicGetEmptyTxRb() throws Exception {
242: _testBasicGetEmpty(TX_RB);
243: }
244:
245: /**
246: * Check the new relation a0-b0
247: */
248: public void _testBasicSetEmpty(int tx) throws Exception {
249: ArrayList c = new ArrayList(1);
250: c.add("b0");
251: if ((tx == TX_CALL) || (tx == TX_RB)) {
252: utx.begin();
253: }
254: ARemote a = getAHome().findByPrimaryKey("a0");
255: if (tx == TX_CONT) {
256: a.assignBInNewTx(c);
257: } else {
258: a.assignB(c);
259: }
260: if (tx == TX_CALL) {
261: utx.commit();
262: } else if (tx == TX_RB) {
263: utx.rollback();
264: }
265: // checking
266: Collection c_value = a.retrieveB();
267: if (tx != TX_RB) {
268: assertTrue("Wrong relations a0 (required:" + c + ", found:"
269: + c_value + ")", isCollectionEqual(c_value, c));
270: } else {
271: assertTrue("Wrong relation a0->b0 : ", c_value.isEmpty());
272: }
273: // undo
274: if (tx != TX_RB) {
275: a.assignB(new ArrayList());
276: }
277: checkIsInitialState();
278: }
279:
280: public void testBasicSetEmptyTxNo() throws Exception {
281: _testBasicSetEmpty(TX_NO);
282: }
283:
284: public void testBasicSetEmptyTxCall() throws Exception {
285: _testBasicSetEmpty(TX_CALL);
286: }
287:
288: public void testBasicSetEmptyTxCont() throws Exception {
289: _testBasicSetEmpty(TX_CONT);
290: }
291:
292: public void testBasicSetEmptyTxRb() throws Exception {
293: _testBasicSetEmpty(TX_RB);
294: }
295:
296: public void testPhil() throws Exception {
297: utx.begin();
298: ARemote a2 = getAHome().findByPrimaryKey("a2");
299: a2.clearB();
300: utx.rollback();
301: //checkIsInitialState();
302: }
303:
304: /**
305: * Check clear function
306: */
307: public void _testBasicClear(int tx) throws Exception {
308: if ((tx == TX_CALL) || (tx == TX_RB)) {
309: utx.begin();
310: }
311: ARemote a2 = getAHome().findByPrimaryKey("a2");
312: if (tx == TX_CONT) {
313: a2.clearBInNewTx();
314: } else {
315: a2.clearB();
316: }
317: if (tx == TX_CALL) {
318: utx.commit();
319: } else if (tx == TX_RB) {
320: utx.rollback();
321: }
322: // checking
323: Collection a2_value = a2.retrieveB();
324: BRemote b1 = getBHome().findByPrimaryKey("b1");
325: BRemote b2 = getBHome().findByPrimaryKey("b2");
326: BRemote b3 = getBHome().findByPrimaryKey("b3");
327: ArrayList collb1 = new ArrayList(1);
328: collb1.add("a1");
329: ArrayList collb2 = new ArrayList(2);
330: collb2.add("a1");
331: collb2.add("a3");
332: ArrayList collb3 = new ArrayList(1);
333: collb3.add("a3");
334: Collection col = null;
335: if (tx != TX_RB) {
336: assertTrue("Wrong relations a2 (required: Empty "
337: + ", found:" + a2_value + ")", a2_value.isEmpty());
338: col = getBHome().findByPrimaryKey("b1").retrieveA();
339: assertTrue(
340: "Wrong collection returned by b1.getA(): expected:"
341: + collb1 + " found: " + col,
342: isCollectionEqual(collb1, col));
343: col = getBHome().findByPrimaryKey("b2").retrieveA();
344: assertTrue(
345: "Wrong collection returned by b2.getA(): expected:"
346: + collb2 + " found: " + col,
347: isCollectionEqual(collb2, col));
348: col = getBHome().findByPrimaryKey("b3").retrieveA();
349: assertTrue(
350: "Wrong collection returned by b3.getA(): expected:"
351: + collb3 + " found: " + col,
352: isCollectionEqual(collb3, col));
353: // undo
354: ArrayList a2undo = new ArrayList(3);
355: a2undo.add("b1");
356: a2undo.add("b2");
357: a2undo.add("b3");
358: a2.assignB(a2undo);
359: }
360: checkIsInitialState();
361: }
362:
363: public void testBasicClearTxNo() throws Exception {
364: _testBasicClear(TX_NO);
365: }
366:
367: public void testBasicClearTxCall() throws Exception {
368: _testBasicClear(TX_CALL);
369: }
370:
371: public void testBasicClearTxCont() throws Exception {
372: _testBasicClear(TX_CONT);
373: }
374:
375: public void testBasicClearTxRb() throws Exception {
376: _testBasicClear(TX_RB);
377: }
378:
379: /**
380: * Remove an element in a relation.
381: */
382: public void _testCohRemoveInRel(int tx) throws Exception {
383: String bRemoved = "b4";
384: if ((tx == TX_CALL) || (tx == TX_RB)) {
385: utx.begin();
386: }
387: ARemote a = getAHome().findByPrimaryKey("a3");
388: if (tx == TX_CONT) {
389: a.removeFromBInNewTx(bRemoved);
390: } else {
391: a.removeFromB(bRemoved);
392: }
393: if (tx == TX_CALL) {
394: utx.commit();
395: } else if (tx == TX_RB) {
396: utx.rollback();
397: }
398: // checking
399: Collection ca = a.retrieveB();
400: BRemote b4 = getBHome().findByPrimaryKey("b4");
401: Collection cb4 = b4.retrieveA();
402: if (tx != TX_RB) {
403: assertEquals("Wrong relations size for a3: ", 2, ca.size());
404: assertEquals("Wrong relations size for b4: ", 0, cb4.size());
405: } else {
406: assertEquals("Wrong relations size for a3: ", 3, ca.size());
407: assertEquals("Wrong relations size for b4: ", 1, cb4.size());
408: }
409: // undo
410: if (tx != TX_RB) {
411: a.addInB(bRemoved);
412: }
413: // check to initial state
414: checkIsInitialState();
415:
416: }
417:
418: public void testCohRemoveInRelTxNo() throws Exception {
419: _testCohRemoveInRel(TX_NO);
420: }
421:
422: public void testCohRemoveInRelTxCall() throws Exception {
423: _testCohRemoveInRel(TX_CALL);
424: }
425:
426: public void testCohRemoveInRelTxCont() throws Exception {
427: _testCohRemoveInRel(TX_CONT);
428: }
429:
430: public void testCohRemoveInRelTxRb() throws Exception {
431: _testCohRemoveInRel(TX_RB);
432: }
433:
434: /**
435: * Check the coherence 10.3.7.6: a1.assignB(a3.retrieveB()) =>
436: * a1.retreiveB().contains("b2") &&
437: * a1.retreiveB().contains("b3") &&
438: * a1.retreiveB().contains("b4") &&
439: * a3.retreiveB().contains("b2") &&
440: * a3.retreiveB().contains("b3") &&
441: * a3.retreiveB().contains("b4") &&
442: * b1.retreiveB().contains("a2") &&
443: * b2.retreiveB().contains("a1") &&
444: * b2.retreiveB().contains("a2") &&
445: * b2.retreiveB().contains("a3") &&
446: * b3.retreiveB().contains("b4") &&
447: * b3.retreiveB().contains("a1") &&
448: * b3.retreiveB().contains("a2") &&
449: * b3.retreiveB().contains("a3") &&
450: * b4.retreiveB().contains("a1") &&
451: * b4.retreiveB().contains("a3") &&
452: * b4.retreiveB().contains("a4") &&
453: * b4.retreiveB().contains("a5") &&
454: */
455: public void _testCoSetFirst(int tx) throws Exception {
456: String c = null;
457: if ((tx == TX_CALL) || (tx == TX_RB)) {
458: utx.begin();
459: }
460: ARemote a1 = getAHome().findByPrimaryKey("a1");
461: ARemote a3 = getAHome().findByPrimaryKey("a3");
462: Collection c1 = a1.retrieveB();
463: if (tx == TX_CONT) {
464: a1.assignBInNewTx(a3.retrieveB());
465: } else {
466: a1.assignB(a3.retrieveB());
467: }
468: if (tx == TX_CALL) {
469: utx.commit();
470: } else if (tx == TX_RB) {
471: utx.rollback();
472: }
473:
474: // checking
475: if (tx == TX_RB) {
476: checkIsInitialState();
477: return;
478: }
479:
480: ArrayList al = new ArrayList(3);
481: Collection col = null;
482: al.add("b2");
483: al.add("b3");
484: al.add("b4");
485: col = a1.retrieveB();
486: assertTrue("Wrong collection returned by a1.getB(): expected:"
487: + al + " found: " + col, isCollectionEqual(al, col));
488: col = a3.retrieveB();
489: assertTrue("Wrong collection returned by a3.getB(): expected:"
490: + al + " found: " + col, isCollectionEqual(al, col));
491:
492: al = new ArrayList(1);
493: al.add("a2");
494: col = getBHome().findByPrimaryKey("b1").retrieveA();
495: assertTrue("Wrong collection returned by b1.getA(): expected:"
496: + al + " found: " + col, isCollectionEqual(al, col));
497:
498: al = new ArrayList(3);
499: al.add("a1");
500: al.add("a2");
501: al.add("a3");
502: col = getBHome().findByPrimaryKey("b2").retrieveA();
503: assertTrue("Wrong collection returned by b2.getA(): expected:"
504: + al + " found: " + col, isCollectionEqual(al, col));
505:
506: al = new ArrayList(2);
507: al.add("a1");
508: al.add("a2");
509: al.add("a3");
510: col = getBHome().findByPrimaryKey("b3").retrieveA();
511: assertTrue("Wrong collection returned by b3.getA(): expected:"
512: + al + " found: " + col, isCollectionEqual(al, col));
513:
514: al = new ArrayList(2);
515: al.add("a1");
516: al.add("a3");
517: col = getBHome().findByPrimaryKey("b4").retrieveA();
518: assertTrue("Wrong collection returned by b4.getA(): expected:"
519: + al + " found: " + col, isCollectionEqual(al, col));
520:
521: a1.assignB(null);
522: al = new ArrayList(2);
523: al.add("b1");
524: al.add("b2");
525: a1.assignBInNewTx(al);
526: checkIsInitialState();
527:
528: }
529:
530: public void testCoSetFirstTxNo() throws Exception {
531: _testCoSetFirst(TX_NO);
532: }
533:
534: public void testCoSetFirstTxCall() throws Exception {
535: _testCoSetFirst(TX_CALL);
536: }
537:
538: public void testCoSetFirstTxCont() throws Exception {
539: _testCoSetFirst(TX_CONT);
540: }
541:
542: public void testCoSetFirstTxRb() throws Exception {
543: _testCoSetFirst(TX_RB);
544: }
545:
546: /**
547: * Check the coherence 10.3.7.6 : a1.getB().add(b3) =>
548: * a1.retreiveB().contains("b1") &&
549: * a1.retreiveB().contains("b2") &&
550: * a1.retreiveB().contains("b3") &&
551: * b3.retreiveB().contains("a1") &&
552: * b3.retreiveB().contains("a2") &&
553: * b3.retreiveB().contains("a3") &&
554: * b3.retreiveB().contains("a4") &&
555: */
556: public void _testCoSetSecond(int tx) throws Exception {
557: String c = null;
558:
559: if ((tx == TX_CALL) || (tx == TX_RB)) {
560: utx.begin();
561: }
562: ARemote a1 = getAHome().findByPrimaryKey("a1");
563:
564: if (tx == TX_CONT) {
565: a1.addInBInNewTx("b3");
566: } else {
567: a1.addInB("b3");
568: }
569: if (tx == TX_CALL) {
570: utx.commit();
571: } else if (tx == TX_RB) {
572: utx.rollback();
573: }
574: // checking
575: if (tx != TX_RB) {
576: ArrayList al = new ArrayList(3);
577: Collection col = null;
578: al.add("b1");
579: al.add("b2");
580: al.add("b3");
581: col = a1.retrieveB();
582: assertTrue(
583: "Wrong collection returned by a1.getB(): expected:"
584: + al + " found: " + col, isCollectionEqual(
585: al, col));
586:
587: al = new ArrayList(3);
588: al.add("a1");
589: al.add("a2");
590: al.add("a3");
591:
592: col = getBHome().findByPrimaryKey("b3").retrieveA();
593: assertTrue(
594: "Wrong collection returned by b3.getA(): expected:"
595: + al + " found: " + col, isCollectionEqual(
596: al, col));
597:
598: // undo
599: a1.removeFromB("b3");
600: }
601: checkIsInitialState();
602: }
603:
604: public void testCoSetSecondTxNo() throws Exception {
605: _testCoSetSecond(TX_NO);
606: }
607:
608: public void testCoSetSecondTxCall() throws Exception {
609: _testCoSetSecond(TX_CALL);
610: }
611:
612: public void testCoSetSecondTxCont() throws Exception {
613: _testCoSetSecond(TX_CONT);
614: }
615:
616: public void testCoSetSecondTxRb() throws Exception {
617: _testCoSetSecond(TX_RB);
618: }
619:
620: /**
621: * Check the coherence 10.3.7.6 : a2.getB().remove(b2) =>
622: * a2.retreiveB().contains("b1") &&
623: * a2.retreiveB().contains("b3") &&
624: * b2.retreiveB().contains("a1") &&
625: * b2.retreiveB().contains("a3") &&
626: * !b2.retreiveB().contains("a2")
627: */
628: public void _testCoSetRemove(int tx) throws Exception {
629:
630: if (tx == TX_CONT) {
631: // The transaction attribute of the remove method is TX_SUPPORT,
632: // so the transaction cannot be initiate by the container
633: fail("Transaction cannot be initiate by the container for this test");
634: }
635:
636: String c = null;
637:
638: if ((tx == TX_CALL) || (tx == TX_RB)) {
639: utx.begin();
640: }
641: ARemote a2 = getAHome().findByPrimaryKey("a2");
642: BRemote b2 = getBHome().findByPrimaryKey("b2");
643: Collection c2 = a2.retrieveB();
644: if (tx == TX_CONT) {
645: a2.removeFromBInNewTx("b2");
646: } else {
647: a2.removeFromB("b2");
648: }
649: if (tx == TX_CALL) {
650: utx.commit();
651: } else if (tx == TX_RB) {
652: utx.rollback();
653: }
654: // checking
655: if (tx != TX_RB) {
656: ArrayList al = new ArrayList(2);
657: Collection col = null;
658: al.add("b1");
659: al.add("b3");
660: col = a2.retrieveB();
661: assertTrue(
662: "Wrong collection returned by a2.getB(): expected:"
663: + al + " found: " + col, isCollectionEqual(
664: al, col));
665:
666: al = new ArrayList(2);
667: al.add("a1");
668: al.add("a3");
669: col = b2.retrieveA();
670: assertTrue(
671: "Wrong collection returned by b2.getA(): expected:"
672: + al + " found: " + col, isCollectionEqual(
673: al, col));
674:
675: // undo
676: a2.addInB("b2");
677: }
678: checkIsInitialState();
679: }
680:
681: public void testCoSetRemoveTxNo() throws Exception {
682: _testCoSetRemove(TX_NO);
683: }
684:
685: public void testCoSetRemoveTxCall() throws Exception {
686: _testCoSetRemove(TX_CALL);
687: }
688:
689: public void testCoSetRemoveTxRb() throws Exception {
690: _testCoSetRemove(TX_RB);
691: }
692:
693: /**
694: * Test of coherence b2.remove() => a1.retrieveB()=null && a2.retrieveB()=null && a3.retrieveB()=null
695: */
696: public void _testCohRemoveB(int tx) throws Exception {
697:
698: if (tx == TX_CONT) {
699: // The transaction attribute of the remove method is TX_SUPPORT,
700: // so the transaction cannot be initiate by the container
701: fail("Transaction cannot be initiate by the container for this test");
702: }
703:
704: if ((tx == TX_CALL) || (tx == TX_RB)) {
705: utx.begin();
706: }
707: getBHome().remove("b2");
708: if (tx == TX_CALL) {
709: utx.commit();
710: } else if (tx == TX_RB) {
711: utx.rollback();
712: }
713: // checking
714:
715: if (tx != TX_RB) {
716: ARemote a1 = getAHome().findByPrimaryKey("a1");
717: ARemote a2 = getAHome().findByPrimaryKey("a2");
718: ARemote a3 = getAHome().findByPrimaryKey("a3");
719: Collection idrela1 = a1.retrieveB();
720: Collection idrela2 = a2.retrieveB();
721: Collection idrela3 = a3.retrieveB();
722: ArrayList col_a1 = new ArrayList(1);
723: col_a1.add("b1");
724: ArrayList col_a2 = new ArrayList(2);
725: col_a2.add("b1");
726: col_a2.add("b3");
727: ArrayList col_a3 = new ArrayList(2);
728: col_a3.add("b3");
729: col_a3.add("b4");
730: assertTrue("Wrong relation a1 : Expected [b1] , found "
731: + idrela1, isCollectionEqual(idrela1, col_a1));
732: assertTrue("Wrong relation a2 : Expected [b1, b3] , found "
733: + idrela2, isCollectionEqual(idrela2, col_a2));
734: assertTrue("Wrong relation a3 : Expected [b3, b4] , found "
735: + idrela3, isCollectionEqual(idrela3, col_a3));
736: // undo
737: BRemote b2 = getBHome().create("b2");
738: col_a2 = new ArrayList(3);
739: col_a2.add("a1");
740: col_a2.add("a2");
741: col_a2.add("a3");
742:
743: b2.assignAInNewTx(col_a2);
744: }
745: checkIsInitialState();
746: }
747:
748: public void testCohRemoveBTxNo() throws Exception {
749: _testCohRemoveB(TX_NO);
750: }
751:
752: public void testCohRemoveBTxCall() throws Exception {
753: _testCohRemoveB(TX_CALL);
754: }
755:
756: public void testCohRemoveBTxRb() throws Exception {
757: _testCohRemoveB(TX_RB);
758: }
759:
760: /**
761: * Test of coherence b2.remove() => a1.retrieveB()=null && a2.retrieveB()=null && a3.retrieveB()=null
762: * Same as _testCohRemoveB except that the called remove method is on the bean
763: * instead of the home.
764: */
765: public void _testCohBeanRemoveB(int tx) throws Exception {
766:
767: if (tx == TX_CONT) {
768: // The transaction attribute of the remove method is TX_SUPPORT,
769: // so the transaction cannot be initiate by the container
770: fail("Transaction cannot be initiate by the container for this test");
771: }
772:
773: if ((tx == TX_CALL) || (tx == TX_RB)) {
774: utx.begin();
775: }
776: BRemote b = getBHome().findByPrimaryKey("b2");
777: b.remove();
778: if (tx == TX_CALL) {
779: utx.commit();
780: } else if (tx == TX_RB) {
781: utx.rollback();
782: }
783: // checking
784:
785: if (tx != TX_RB) {
786: ARemote a1 = getAHome().findByPrimaryKey("a1");
787: ARemote a2 = getAHome().findByPrimaryKey("a2");
788: ARemote a3 = getAHome().findByPrimaryKey("a3");
789: Collection idrela1 = a1.retrieveB();
790: Collection idrela2 = a2.retrieveB();
791: Collection idrela3 = a3.retrieveB();
792: ArrayList col_a1 = new ArrayList(1);
793: col_a1.add("b1");
794: ArrayList col_a2 = new ArrayList(2);
795: col_a2.add("b1");
796: col_a2.add("b3");
797: ArrayList col_a3 = new ArrayList(2);
798: col_a3.add("b3");
799: col_a3.add("b4");
800: assertTrue("Wrong relation a1 : Expected [b1] , found "
801: + idrela1, isCollectionEqual(idrela1, col_a1));
802: assertTrue("Wrong relation a2 : Expected [b1, b3] , found "
803: + idrela2, isCollectionEqual(idrela2, col_a2));
804: assertTrue("Wrong relation a3 : Expected [b3, b4] , found "
805: + idrela3, isCollectionEqual(idrela3, col_a3));
806: // undo
807: BRemote b2 = getBHome().create("b2");
808: col_a2 = new ArrayList(3);
809: col_a2.add("a1");
810: col_a2.add("a2");
811: col_a2.add("a3");
812:
813: b2.assignAInNewTx(col_a2);
814: }
815: checkIsInitialState();
816: }
817:
818: public void testCohBeanRemoveBTxNo() throws Exception {
819: _testCohBeanRemoveB(TX_NO);
820: }
821:
822: public void testCohBeanRemoveBTxCall() throws Exception {
823: _testCohBeanRemoveB(TX_CALL);
824: }
825:
826: public void testCohBeanRemoveBTxRb() throws Exception {
827: _testCohBeanRemoveB(TX_RB);
828: }
829:
830: /**
831: * Test of coherence a2.remove() => b1.retrieveB()=null && b2.retrieveB()=null && b3.retrieveB()=null
832: */
833: public void _testCohRemoveA(int tx) throws Exception {
834:
835: if (tx == TX_CONT) {
836: // The transaction attribute of the remove method is TX_SUPPORT,
837: // so the transaction cannot be initiate by the container
838: fail("Transaction cannot be initiate by the container for this test");
839: }
840:
841: if ((tx == TX_CALL) || (tx == TX_RB)) {
842: utx.begin();
843: }
844: getAHome().remove("a2");
845: if (tx == TX_CALL) {
846: utx.commit();
847: } else if (tx == TX_RB) {
848: utx.rollback();
849: }
850: // checking
851:
852: if (tx != TX_RB) {
853: BRemote b1 = getBHome().findByPrimaryKey("b1");
854: BRemote b2 = getBHome().findByPrimaryKey("b2");
855: BRemote b3 = getBHome().findByPrimaryKey("b3");
856: Collection idrelb1 = b1.retrieveA();
857: Collection idrelb2 = b2.retrieveA();
858: Collection idrelb3 = b3.retrieveA();
859: ArrayList col_b1 = new ArrayList(1);
860: col_b1.add("a1");
861: ArrayList col_b2 = new ArrayList(2);
862: col_b2.add("a1");
863: col_b2.add("a3");
864: ArrayList col_b3 = new ArrayList(1);
865: col_b3.add("a3");
866: assertTrue("Wrong relation b1 : Expected [a1] , found "
867: + idrelb1, isCollectionEqual(idrelb1, col_b1));
868: assertTrue("Wrong relation b2 : Expected [a1, a3] , found "
869: + idrelb2, isCollectionEqual(idrelb2, col_b2));
870: assertTrue("Wrong relation b3 : Expected [a3] , found "
871: + idrelb3, isCollectionEqual(idrelb3, col_b3));
872: // undo
873: getAHome().create("a2");
874: ARemote a2 = getAHome().findByPrimaryKey("a2");
875: ArrayList col_a2 = new ArrayList(3);
876: col_a2.add("b1");
877: col_a2.add("b2");
878: col_a2.add("b3");
879:
880: a2.assignB(col_a2);
881: }
882: checkIsInitialState();
883: }
884:
885: public void testCohRemoveATxNo() throws Exception {
886: _testCohRemoveA(TX_NO);
887: }
888:
889: public void testCohRemoveATxCall() throws Exception {
890: _testCohRemoveA(TX_CALL);
891: }
892:
893: public void testCohRemoveATxRb() throws Exception {
894: _testCohRemoveA(TX_RB);
895: }
896:
897: /**
898: * Test of coherence a2.remove() => b1.retrieveB()=null && b2.retrieveB()=null && b3.retrieveB()=null
899: * Same as _testCohRemoveA except that the called remove method is on the bean
900: * instead of the home.
901: */
902: public void _testCohBeanRemoveA(int tx) throws Exception {
903:
904: if (tx == TX_CONT) {
905: // The transaction attribute of the remove method is TX_SUPPORT,
906: // so the transaction cannot be initiate by the container
907: fail("Transaction cannot be initiate by the container for this test");
908: }
909:
910: if ((tx == TX_CALL) || (tx == TX_RB)) {
911: utx.begin();
912: }
913: ARemote a = getAHome().findByPrimaryKey("a2");
914: a.remove();
915: if (tx == TX_CALL) {
916: utx.commit();
917: } else if (tx == TX_RB) {
918: utx.rollback();
919: }
920: // checking
921:
922: if (tx != TX_RB) {
923: BRemote b1 = getBHome().findByPrimaryKey("b1");
924: BRemote b2 = getBHome().findByPrimaryKey("b2");
925: BRemote b3 = getBHome().findByPrimaryKey("b3");
926: Collection idrelb1 = b1.retrieveA();
927: Collection idrelb2 = b2.retrieveA();
928: Collection idrelb3 = b3.retrieveA();
929: ArrayList col_b1 = new ArrayList(1);
930: col_b1.add("a1");
931: ArrayList col_b2 = new ArrayList(2);
932: col_b2.add("a1");
933: col_b2.add("a3");
934: ArrayList col_b3 = new ArrayList(1);
935: col_b3.add("a3");
936: assertTrue("Wrong relation b1 : Expected [a1] , found "
937: + idrelb1, isCollectionEqual(idrelb1, col_b1));
938: assertTrue("Wrong relation b2 : Expected [a1, a3] , found "
939: + idrelb2, isCollectionEqual(idrelb2, col_b2));
940: assertTrue("Wrong relation b3 : Expected [a3] , found "
941: + idrelb3, isCollectionEqual(idrelb3, col_b3));
942: // undo
943: getAHome().create("a2");
944: ARemote a2 = getAHome().findByPrimaryKey("a2");
945: ArrayList col_a2 = new ArrayList(3);
946: col_a2.add("b1");
947: col_a2.add("b2");
948: col_a2.add("b3");
949:
950: a2.assignB(col_a2);
951: }
952: checkIsInitialState();
953: }
954:
955: public void testCohBeanRemoveATxNo() throws Exception {
956: _testCohBeanRemoveA(TX_NO);
957: }
958:
959: public void testCohBeanRemoveATxCall() throws Exception {
960: _testCohBeanRemoveA(TX_CALL);
961: }
962:
963: public void testCohBeanRemoveATxRb() throws Exception {
964: _testCohBeanRemoveA(TX_RB);
965: }
966:
967: }
|