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_mou.java 7533 2005-10-19 15:55:05Z durieuxp $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.clients.entity;
027:
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.mou.AHomeRemote;
038: import org.objectweb.jonas.jtests.beans.relation.mou.ARemote;
039: import org.objectweb.jonas.jtests.beans.relation.mou.BHomeRemote;
040: import org.objectweb.jonas.jtests.beans.relation.mou.BRemote;
041:
042: /**
043: * For testing many-to-one unidirectional relationships
044: * @author Ph. Durieux
045: *
046: * This is an advanced test suite for home interface on entity bean CMP2.
047: **/
048: public abstract class A_mou extends A_Cmp2Util {
049:
050: public abstract AHomeRemote getAHome();
051:
052: public abstract BHomeRemote getBHome();
053:
054: static Hashtable tbRelationA2B = new Hashtable();
055:
056: static {
057: tbRelationA2B.put("a20", "b2");
058: tbRelationA2B.put("a21", "b2");
059: tbRelationA2B.put("a22", "b2");
060: tbRelationA2B.put("a0", "b0");
061: tbRelationA2B.put("a1", "b0");
062: tbRelationA2B.put("a2", "b0");
063: tbRelationA2B.put("a3", "");
064: tbRelationA2B.put("a4", "b4");
065: tbRelationA2B.put("a5", "b4");
066: }
067:
068: public A_mou(String name) {
069: super (name);
070: }
071:
072: protected boolean isInit = false;
073:
074: protected void setUp() {
075: super .setUp();
076: boolean ok = false;
077: int nbtry = 0;
078: while (!ok && nbtry < 3) {
079: if (!isInit) {
080: // load bean if not loaded yet
081: useBeans("mou", false);
082: // check if tables have been initialized
083: try {
084: getAHome().findByPrimaryKey("a0");
085: } catch (Exception e) {
086: // Make the initialization needed for the tests
087: try {
088: utx.begin();
089: ARemote a0 = getAHome().create("a0");
090: ARemote a1 = getAHome().create("a1");
091: ARemote a2 = getAHome().create("a2");
092: ARemote a3 = getAHome().create("a3");
093: ARemote a4 = getAHome().create("a4");
094: ARemote a5 = getAHome().create("a5");
095: ARemote a20 = getAHome().create("a20");
096: ARemote a21 = getAHome().create("a21");
097: ARemote a22 = getAHome().create("a22");
098: getBHome().create("b0");
099: getBHome().create("b1");
100: getBHome().create("b2");
101: getBHome().create("b4");
102: // assign relations only if not null (a3)
103: a0.assignB((String) tbRelationA2B.get("a0"));
104: a1.assignB((String) tbRelationA2B.get("a1"));
105: a2.assignB((String) tbRelationA2B.get("a2"));
106: a4.assignB((String) tbRelationA2B.get("a4"));
107: a5.assignB((String) tbRelationA2B.get("a5"));
108: a20.assignB((String) tbRelationA2B.get("a20"));
109: a21.assignB((String) tbRelationA2B.get("a21"));
110: a22.assignB((String) tbRelationA2B.get("a22"));
111: } catch (Exception e2) {
112: fail("InitialState creation problem: " + e2);
113: } finally {
114: try {
115: utx.commit();
116: } catch (Exception e3) {
117: }
118: }
119: }
120: isInit = true;
121: }
122: // Check that all is OK. Sometimes, a test has failed and has corrupted
123: // the bean state in the database. We must unload and reload the bean then.
124: nbtry++;
125: try {
126: if (initStateOK()) {
127: ok = true;
128: }
129: } catch (Exception e) {
130: }
131: if (!ok) {
132: isInit = false;
133: unloadBeans("mou");
134: }
135: }
136: }
137:
138: /*
139: * Check that we are in the same state as after the tables creation for thoses beans A and B
140: * (ie if it is the initial state)
141: */
142: boolean initStateOK() throws Exception {
143: boolean isOk = true;
144: msgerror = new StringBuffer();
145: for (Enumeration ea = tbRelationA2B.keys(); ea
146: .hasMoreElements();) {
147: String aname = (String) (ea.nextElement());
148: ARemote a = getAHome().findByPrimaryKey(aname);
149: String BActual = a.retrieveB();
150: // If BActual is empty then convert to "" to match with tbRelationA2B value in this case
151: if (BActual == null)
152: BActual = "";
153: String BExpected = (String) (tbRelationA2B.get(aname));
154: if (!BActual.equals(BExpected)) {
155: isOk = false;
156: msgerror = msgerror.append("Wrong relation for "
157: + aname + " (expected:" + BExpected
158: + ", found:" + BActual + ")");
159: }
160: }
161: return isOk;
162: }
163:
164: /**
165: * Check that the bean 'a3' has no relation.
166: */
167: public void _testBasicGetEmpty(int tx) throws Exception {
168: String c = null;
169: if ((tx == TX_CALL) || (tx == TX_RB)) {
170: utx.begin();
171: }
172: ARemote a = getAHome().findByPrimaryKey("a3");
173: if (tx == TX_CONT) {
174: c = a.retrieveBInNewTx();
175: } else {
176: c = a.retrieveB();
177: }
178: if (tx == TX_CALL) {
179: utx.commit();
180: } else if (tx == TX_RB) {
181: utx.rollback();
182: }
183: checkIsInitialState();
184: }
185:
186: public void testBasicGetEmptyTxNo() throws Exception {
187: _testBasicGetEmpty(TX_NO);
188: }
189:
190: public void testBasicGetEmptyTxCall() throws Exception {
191: _testBasicGetEmpty(TX_CALL);
192: }
193:
194: public void testBasicGetEmptyTxCont() throws Exception {
195: _testBasicGetEmpty(TX_CONT);
196: }
197:
198: public void testBasicGetEmptyTxRb() throws Exception {
199: _testBasicGetEmpty(TX_RB);
200: }
201:
202: /**
203: * Ckeck the new relation a3-b1
204: */
205: public void _testBasicSetEmpty(int tx) throws Exception {
206: String c = null;
207: if ((tx == TX_CALL) || (tx == TX_RB)) {
208: utx.begin();
209: }
210: ARemote a = getAHome().findByPrimaryKey("a3");
211: if (tx == TX_CONT) {
212: a.assignBInNewTx("b1");
213: } else {
214: a.assignB("b1");
215: }
216: if (tx == TX_CALL) {
217: utx.commit();
218: } else if (tx == TX_RB) {
219: utx.rollback();
220: }
221: // checking
222: String idB = a.retrieveB();
223: if (tx != TX_RB) {
224: assertEquals("Wrong new relation a3->b1 : ", "b1", idB);
225: } else {
226: assertNull("Wrong new relation a3->b1 : ", idB);
227: }
228: // undo
229: if (tx != TX_RB) {
230: a.assignB(null);
231: }
232: checkIsInitialState();
233: }
234:
235: public void testBasicSetEmptyTxNo() throws Exception {
236: _testBasicSetEmpty(TX_NO);
237: }
238:
239: public void testBasicSetEmptyTxCall() throws Exception {
240: _testBasicSetEmpty(TX_CALL);
241: }
242:
243: public void testBasicSetEmptyTxCont() throws Exception {
244: _testBasicSetEmpty(TX_CONT);
245: }
246:
247: public void testBasicSetEmptyTxRb() throws Exception {
248: _testBasicSetEmpty(TX_RB);
249: }
250:
251: /**
252: * Check that a1.retrieveB()=b0
253: */
254: public void _testBasicGet(int tx) throws Exception {
255: String b = null;
256: if ((tx == TX_CALL) || (tx == TX_RB)) {
257: utx.begin();
258: }
259: ARemote a1 = getAHome().findByPrimaryKey("a1");
260:
261: if (tx == TX_CONT) {
262: b = a1.retrieveBInNewTx();
263: } else {
264: b = a1.retrieveB();
265: }
266: if (tx == TX_CALL) {
267: utx.commit();
268: } else if (tx == TX_RB) {
269: utx.rollback();
270: }
271: // checking
272: if (tx != TX_RB) {
273: assertEquals("Wrong relation a1->b0 : ", "b0", b);
274: }
275: checkIsInitialState();
276: }
277:
278: public void testBasicGetTxNo() throws Exception {
279: _testBasicGet(TX_NO);
280: }
281:
282: public void testBasicGetTxCall() throws Exception {
283: _testBasicGet(TX_CALL);
284: }
285:
286: public void testBasicGetTxCont() throws Exception {
287: _testBasicGet(TX_CONT);
288: }
289:
290: public void testBasicGetTxRb() throws Exception {
291: _testBasicGet(TX_RB);
292: }
293:
294: /**
295: * Check that a1.assignB(null) => a1.retreiveB()=null
296: */
297: public void _testBasicSetNull(int tx) throws Exception {
298: if ((tx == TX_CALL) || (tx == TX_RB)) {
299: utx.begin();
300: }
301: ARemote a1 = getAHome().findByPrimaryKey("a1");
302:
303: if (tx == TX_CONT) {
304: a1.assignBInNewTx(null);
305: } else {
306: a1.assignB(null);
307: }
308: if (tx == TX_CALL) {
309: utx.commit();
310: } else if (tx == TX_RB) {
311: utx.rollback();
312: }
313: // checking
314: if (tx != TX_RB) {
315: String b = a1.retrieveB();
316: assertNull("Wrong relation a1 : ", b);
317: // Undo
318: a1.assignB("b0");
319: }
320: checkIsInitialState();
321: }
322:
323: public void testBasicSetNullTxNo() throws Exception {
324: _testBasicSetNull(TX_NO);
325: }
326:
327: public void testBasicSetNullTxCall() throws Exception {
328: _testBasicSetNull(TX_CALL);
329: }
330:
331: public void testBasicSetNullTxCont() throws Exception {
332: _testBasicSetNull(TX_CONT);
333: }
334:
335: public void testBasicSetNullTxRb() throws Exception {
336: _testBasicSetNull(TX_RB);
337: }
338:
339: /**
340: * See 10.3.7.5 : a1j.assignB(a2k.retrieveB()) => a2=a1j.retrieveB()
341: */
342: public void _testBasicSet(int tx) throws Exception {
343: String c = null;
344: if ((tx == TX_CALL) || (tx == TX_RB)) {
345: utx.begin();
346: }
347: ARemote a1 = getAHome().findByPrimaryKey("a1");
348: ARemote a21 = getAHome().findByPrimaryKey("a21");
349: if (tx == TX_CONT) {
350: a1.assignBInNewTx(a21.retrieveB());
351: } else {
352: a1.assignB(a21.retrieveB());
353: }
354: if (tx == TX_CALL) {
355: utx.commit();
356: } else if (tx == TX_RB) {
357: utx.rollback();
358: }
359: if (tx != TX_RB) {
360: // checking
361: String idB1 = a1.retrieveB();
362: ARemote a0 = getAHome().findByPrimaryKey("a0");
363: ARemote a2 = getAHome().findByPrimaryKey("a2");
364: ARemote a3 = getAHome().findByPrimaryKey("a3");
365: ARemote a20 = getAHome().findByPrimaryKey("a20");
366: ARemote a22 = getAHome().findByPrimaryKey("a22");
367: String idB0 = a0.retrieveB();
368: String idB2 = a2.retrieveB();
369: String idB3 = a3.retrieveB();
370: String idB20 = a20.retrieveB();
371: String idB21 = a21.retrieveB();
372: String idB22 = a22.retrieveB();
373: assertEquals("Wrong new relation a1->b2 : ", "b2", idB1);
374: assertEquals("Wrong new relation a0->b0 : ", "b0", idB0);
375: assertEquals("Wrong new relation a2->b0 : ", "b0", idB2);
376: assertEquals("Wrong new relation a20->b2 : ", "b2", idB20);
377: assertEquals("Wrong new relation a21->b2 : ", "b2", idB21);
378: assertEquals("Wrong new relation a22->b2 : ", "b2", idB22);
379: // undo
380: a1.assignB("b0");
381: }
382: checkIsInitialState();
383: }
384:
385: public void testBasicSetTxNo() throws Exception {
386: _testBasicSet(TX_NO);
387: }
388:
389: public void testBasicSetTxCall() throws Exception {
390: _testBasicSet(TX_CALL);
391: }
392:
393: public void testBasicSetTxCont() throws Exception {
394: _testBasicSet(TX_CONT);
395: }
396:
397: public void testBasicSetTxRb() throws Exception {
398: _testBasicSet(TX_RB);
399: }
400:
401: /**
402: * Test of coherence a0.remove()
403: */
404: public void _testCohRemoveA(int tx) throws Exception {
405: if (tx == TX_CONT) {
406: // The transaction attribute of the remove method is TX_SUPPORT,
407: // so the transaction cannot be initiate by the container
408: fail("Transaction cannot be initiate by the container for this test");
409: }
410: if ((tx == TX_CALL) || (tx == TX_RB)) {
411: utx.begin();
412: }
413: getAHome().remove("a0");
414: if (tx == TX_CALL) {
415: utx.commit();
416: } else if (tx == TX_RB) {
417: utx.rollback();
418: }
419: // undo
420: if (tx != TX_RB) {
421: ARemote a0 = getAHome().create("a0");
422: a0.assignBInNewTx("b0");
423: }
424: checkIsInitialState();
425: }
426:
427: public void testCohRemoveATxNo() throws Exception {
428: _testCohRemoveA(TX_NO);
429: }
430:
431: public void testCohRemoveATxCall() throws Exception {
432: _testCohRemoveA(TX_CALL);
433: }
434:
435: public void testCohRemoveATxRb() throws Exception {
436: _testCohRemoveA(TX_RB);
437: }
438:
439: /**
440: * Test of coherence b0.remove()
441: */
442: public void _testCohRemoveB(int tx) throws Exception {
443: if (tx == TX_CONT) {
444: // The transaction attribute of the remove method is TX_SUPPORT,
445: // so the transaction cannot be initiate by the container
446: fail("Transaction cannot be initiate by the container for this test");
447: }
448: if ((tx == TX_CALL) || (tx == TX_RB)) {
449: utx.begin();
450: }
451: getBHome().remove("b0");
452: if (tx == TX_CALL) {
453: utx.commit();
454: } else if (tx == TX_RB) {
455: utx.rollback();
456: }
457: // checking
458: if (tx != TX_RB) {
459: ARemote a0 = getAHome().findByPrimaryKey("a0");
460: ARemote a1 = getAHome().findByPrimaryKey("a1");
461: ARemote a2 = getAHome().findByPrimaryKey("a2");
462: String idrela0 = a0.retrieveB();
463: String idrela1 = a1.retrieveB();
464: String idrela2 = a2.retrieveB();
465:
466: assertNull("Wrong not null relation a0, found: " + idrela0,
467: idrela0);
468: assertNull("Wrong not null relation a1, found: " + idrela1,
469: idrela1);
470: assertNull("Wrong not null relation a2, found: " + idrela2,
471: idrela2);
472: // undo
473: getBHome().create("b0");
474: a0.assignBInNewTx("b0");
475: a1.assignBInNewTx("b0");
476: a2.assignBInNewTx("b0");
477: }
478: checkIsInitialState();
479: }
480:
481: public void testCohRemoveBTxNo() throws Exception {
482: _testCohRemoveB(TX_NO);
483: }
484:
485: public void testCohRemoveBTxCall() throws Exception {
486: _testCohRemoveB(TX_CALL);
487: }
488:
489: public void testCohRemoveBTxRb() throws Exception {
490: _testCohRemoveB(TX_RB);
491: }
492:
493: /**
494: * Test of coherence b0.remove() => a0.retrieveB()=null && a1.retrieveB()=null && a2.retrieveB()=null
495: * Same as _testCohRemove except that the called remove method is on the bean
496: * instead of the home.
497: */
498: public void _testCohBeanRemoveB(int tx) throws Exception {
499: if (tx == TX_CONT) {
500: // The transaction attribute of the remove method is TX_SUPPORT,
501: // so the transaction cannot be initiate by the container
502: fail("Transaction cannot be initiate by the container for this test");
503: }
504: if ((tx == TX_CALL) || (tx == TX_RB)) {
505: utx.begin();
506: }
507: BRemote b = getBHome().findByPrimaryKey("b0");
508: b.remove();
509: if (tx == TX_CALL) {
510: utx.commit();
511: } else if (tx == TX_RB) {
512: utx.rollback();
513: }
514: // checking
515: if (tx != TX_RB) {
516: ARemote a0 = getAHome().findByPrimaryKey("a0");
517: ARemote a1 = getAHome().findByPrimaryKey("a1");
518: ARemote a2 = getAHome().findByPrimaryKey("a2");
519: String idrela0 = a0.retrieveB();
520: String idrela1 = a1.retrieveB();
521: String idrela2 = a2.retrieveB();
522:
523: assertNull("Wrong not null relation a0, found: " + idrela0,
524: idrela0);
525: assertNull("Wrong not null relation a1, found: " + idrela1,
526: idrela1);
527: assertNull("Wrong not null relation a2, found: " + idrela2,
528: idrela2);
529: // undo
530: getBHome().create("b0");
531: a0.assignBInNewTx("b0");
532: a1.assignBInNewTx("b0");
533: a2.assignBInNewTx("b0");
534: }
535: checkIsInitialState();
536: }
537:
538: public void testCohBeanRemoveBTxNo() throws Exception {
539: _testCohBeanRemoveB(TX_NO);
540: }
541:
542: public void testCohBeanRemoveBTxCall() throws Exception {
543: _testCohBeanRemoveB(TX_CALL);
544: }
545:
546: public void testCohBeanRemoveBTxRb() throws Exception {
547: _testCohBeanRemoveB(TX_RB);
548: }
549:
550: public void testCohRemove2() throws Exception {
551: _testCohRemoveB(TX_NO);
552: _testCohRemoveB(TX_CALL);
553: _testCohRemoveB(TX_NO);
554: _testCohRemoveB(TX_RB);
555: }
556:
557: /**
558: * Test that the instances in the pool are correctly reused.
559: * There was a bug because PName was not null for GEnClass at create.
560: */
561: public void testInstanceReuse1() throws Exception {
562:
563: ARemote a0 = getAHome().findByPrimaryKey("a0");
564: ARemote a1 = getAHome().findByPrimaryKey("a1");
565: ARemote a2 = getAHome().findByPrimaryKey("a2");
566: ARemote a21 = getAHome().findByPrimaryKey("a21");
567:
568: //_testBasicSetEmpty(TX_RB);
569: utx.begin();
570: a1.assignB("b1");
571: utx.rollback();
572:
573: //_testBasicSet(TX_RB);
574: utx.begin();
575: String bs = a21.retrieveB();
576: a1.assignB(bs);
577: utx.rollback();
578: a1.retrieveB();
579: a21.retrieveB();
580:
581: //_testCohRemoveB(TX_NO);
582: getBHome().remove("b0");
583: getBHome().create("b0");
584: a0.assignBInNewTx("b0");
585:
586: //_testCohRemoveB(TX_CALL);
587: utx.begin();
588: getBHome().remove("b0");
589: utx.commit();
590: BRemote b0 = getBHome().create("b0");
591: a0.assignBInNewTx("b0");
592:
593: //_testCohRemoveB(TX_RB);
594: utx.begin();
595: getBHome().remove("b0");
596: utx.rollback();
597:
598: //_testCohBeanRemoveB(TX_NO);
599: b0.remove();
600: String idrela0 = a0.retrieveB();
601: assertNull("Wrong not null relation a0, found: " + idrela0,
602: idrela0);
603: // undo
604: getBHome().create("b0");
605: a0.assignBInNewTx("b0");
606: a1.assignBInNewTx("b0");
607: a2.assignBInNewTx("b0");
608: checkIsInitialState();
609: }
610:
611: /**
612: * Test that the instances in the pool are correctly reused.
613: * There was a bug in jonas-3-0-beta0 because GenClass was
614: * already initialized at create.
615: */
616: public void testInstanceReuse2() throws Exception {
617: ARemote a4 = getAHome().findByPrimaryKey("a4");
618: ARemote a5 = getAHome().findByPrimaryKey("a5");
619:
620: utx.begin();
621: a5.assignB("b2");
622: utx.rollback();
623:
624: getBHome().remove("b4");
625: BRemote b4 = getBHome().create("b4");
626: a4.assignB("b4");
627:
628: utx.begin();
629: getBHome().remove("b4");
630: utx.rollback();
631: String s = a4.retrieveB();
632: assertEquals("Broken relation", "b4", s);
633:
634: // remove b4 outside tx
635: b4.remove();
636: s = a4.retrieveB();
637: assertNull("Wrong not null relation a4, found: " + s, s);
638: // undo
639: getBHome().create("b4");
640: a4.assignB("b4");
641: a5.assignB("b4");
642: checkIsInitialState();
643: }
644:
645: /**
646: * This test did not work after optimization on lazy GenClass reading
647: * this test is equivalent to :
648: * _testCohRemoveB(TX_RB)
649: * _testCohRemoveB(TX_NO)
650: */
651: public void testInstanceReuse3() throws Exception {
652: String id;
653: //_testCohRemoveB(TX_RB);
654: utx.begin();
655: getBHome().remove("b0");
656: utx.rollback();
657: //_testCohRemoveB(TX_NO);
658: getBHome().remove("b0");
659: // checking
660: ARemote a0 = getAHome().findByPrimaryKey("a0");
661: id = a0.retrieveB();
662: assertNull("Wrong not null relation a0, found: " + id, id);
663: ARemote a1 = getAHome().findByPrimaryKey("a1");
664: id = a1.retrieveB();
665: assertNull("Wrong not null relation a1, found: " + id, id);
666: ARemote a2 = getAHome().findByPrimaryKey("a2");
667: id = a2.retrieveB();
668: assertNull("Wrong not null relation a2, found: " + id, id);
669: // undo
670: getBHome().create("b0");
671: a0.assignBInNewTx("b0");
672: a1.assignBInNewTx("b0");
673: a2.assignBInNewTx("b0");
674: checkIsInitialState();
675: }
676:
677: public void testRollback() throws Exception {
678: ARemote a = getAHome().findByPrimaryKey("a1");
679: String orig = a.retrieveB();
680:
681: // disassociate and commit
682: utx.begin();
683: a.assignB(null);
684: utx.commit();
685: assertEquals(null, a.retrieveB());
686:
687: // revert to original state outside of a user transaction
688: a.assignB(orig);
689:
690: // retrieveB inside a transaction
691: utx.begin();
692: assertEquals(orig, a.retrieveB());
693: utx.rollback();
694:
695: // verify that retrieveB returns correct value after rollback
696: try {
697: assertEquals(orig, a.retrieveB());
698: } finally {
699: // force sync to cleanup
700: sync(true);
701: }
702: checkIsInitialState();
703: }
704:
705: }
|