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_oou.java 9711 2006-10-10 11:51:23Z durieuxp $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.clients.entity;
027:
028: import javax.ejb.ObjectNotFoundException;
029: import javax.naming.NamingException;
030: import javax.rmi.PortableRemoteObject;
031:
032: import junit.framework.Test;
033: import junit.framework.TestSuite;
034:
035: import org.objectweb.jonas.jtests.beans.relation.oou.AHomeRemote;
036: import org.objectweb.jonas.jtests.beans.relation.oou.ARemote;
037: import org.objectweb.jonas.jtests.beans.relation.oou.BHomeRemote;
038: import org.objectweb.jonas.jtests.beans.relation.oou.BRemote;
039:
040: /**
041: * This is an advanced test suite for home interface on entity bean CMP2.
042: */
043: public abstract class A_oou extends A_Cmp2Util {
044:
045: public abstract AHomeRemote getAHome();
046:
047: public abstract BHomeRemote getBHome();
048:
049: public A_oou(String name) {
050: super (name);
051: }
052:
053: protected boolean isInit = false;
054:
055: protected void setUp() {
056: super .setUp();
057: boolean ok = false;
058: int nbtry = 0;
059: while (!ok && nbtry < 3) {
060: if (!isInit) {
061: // load bean if not loaded yet
062: useBeans("oou", false);
063: // check if tables have been initialized
064: try {
065: getAHome().findByPrimaryKey("a2");
066: } catch (Exception e) {
067: // Make the initialization needed for the tests
068: try {
069: utx.begin();
070: ARemote a1 = getAHome().create("a1");
071: ARemote a2 = getAHome().create("a2");
072: getAHome().create("a3");
073: getBHome().create("b1");
074: getBHome().create("b2");
075: getBHome().create("b3");
076: a1.assignB("b1");
077: a2.assignB("b2");
078: } catch (Exception i) {
079: fail("InitialState creation problem: " + i);
080: } finally {
081: try {
082: utx.commit();
083: } catch (Exception ii) {
084: }
085: }
086: }
087: isInit = true;
088: }
089: // Check that all is OK. Sometimes, a test has failed and has corrupted
090: // the bean state in the database. We must unload and reload the bean then.
091: nbtry++;
092: try {
093: if (initStateOK()) {
094: ok = true;
095: }
096: } catch (Exception e) {
097: }
098: if (!ok) {
099: isInit = false;
100: unloadBeans("oou");
101: }
102: }
103: }
104:
105: /*
106: * Check that we are in the same state as after the tables creation for thoses beans A and B
107: * (ie if it is the initial state)
108: */
109: boolean initStateOK() throws Exception {
110: boolean isOk = true;
111:
112: msgerror = new StringBuffer();
113:
114: ARemote a1 = getAHome().findByPrimaryKey("a1");
115: BRemote b1 = getBHome().findByPrimaryKey("b1");
116: ARemote a2 = getAHome().findByPrimaryKey("a2");
117: BRemote b2 = getBHome().findByPrimaryKey("b2");
118: ARemote a3 = getAHome().findByPrimaryKey("a3");
119: BRemote b3 = getBHome().findByPrimaryKey("b3");
120: String idB1 = a1.retrieveB();
121: String idB2 = a2.retrieveB();
122: String idB3 = a3.retrieveB();
123:
124: if (idB1 != null && !idB1.equals("b1")) {
125: isOk = false;
126: msgerror.append("Wrong relation for a1->b1"
127: + "(expected: A1.retrieveB()='b1'" + ", found:"
128: + idB1 + ")");
129: } else if (idB2 != null && !idB2.equals("b2")) {
130: isOk = false;
131: msgerror.append("Wrong relation for a2->b2"
132: + "(expected: A2.retrieveB()='b2'" + ", found:"
133: + idB2 + ")");
134: } else if (idB3 != null) {
135: isOk = false;
136: msgerror.append("Wrong relation for b3->a3"
137: + "(expected: B3.retrieveA()=null" + ", found:"
138: + idB3 + ")");
139: }
140: return isOk;
141: }
142:
143: /**
144: * Test inactivityTimeout with CMR fields
145: */
146: public void testInactivityCMR() throws Exception {
147: ARemote a1 = getAHome().findByPrimaryKey("a1");
148: sleep(6000);
149: a1.retrieveB();
150: }
151:
152: /**
153: * Check that the bean 'a3' has no relation.
154: */
155: public void _testBasicGetEmpty(int tx) throws Exception {
156: String idB = null;
157: if ((tx == TX_CALL) || (tx == TX_RB)) {
158: utx.begin();
159: }
160: ARemote a = getAHome().findByPrimaryKey("a3");
161: if (tx == TX_CONT) {
162: idB = a.retrieveBInNewTx();
163: } else {
164: idB = a.retrieveB();
165: }
166: if (tx == TX_CALL) {
167: utx.commit();
168: } else if (tx == TX_RB) {
169: utx.rollback();
170: }
171:
172: checkIsInitialState();
173: }
174:
175: public void testBasicGetEmptyTxNo() throws Exception {
176: _testBasicGetEmpty(TX_NO);
177: }
178:
179: public void testBasicGetEmptyTxCall() throws Exception {
180: _testBasicGetEmpty(TX_CALL);
181: }
182:
183: public void testBasicGetEmptyTxCont() throws Exception {
184: _testBasicGetEmpty(TX_CONT);
185: }
186:
187: /**
188: * Set a relation to empty : a3.assignB("b3")
189: */
190: public void _testBasicSetEmpty(int tx) throws Exception {
191: if ((tx == TX_CALL) || (tx == TX_RB)) {
192: utx.begin();
193: }
194: ARemote a = getAHome().findByPrimaryKey("a3");
195: if (tx == TX_CONT) {
196: a.assignBInNewTx("b3");
197: } else {
198: a.assignB("b3");
199: }
200: if (tx == TX_CALL) {
201: utx.commit();
202: } else if (tx == TX_RB) {
203: utx.rollback();
204: }
205: // checking
206: String idB = a.retrieveB();
207: if (tx != TX_RB) {
208: assertEquals("Wrong assign null for relation a3->b3 : ",
209: "b3", idB);
210: } else {
211: assertNull("Wrong assign null for relation a3->b3 : ", idB);
212: }
213: // undo
214: if (tx != TX_RB) {
215: a.assignB(null);
216: }
217: checkIsInitialState();
218: }
219:
220: public void testBasicSetEmptyTxNo() throws Exception {
221: _testBasicSetEmpty(TX_NO);
222: }
223:
224: public void testBasicSetEmptyTxCall() throws Exception {
225: _testBasicSetEmpty(TX_CALL);
226: }
227:
228: public void testBasicSetEmptyTxCont() throws Exception {
229: _testBasicSetEmpty(TX_CONT);
230: }
231:
232: public void testBasicSetEmptyTxRb() throws Exception {
233: _testBasicSetEmpty(TX_RB);
234: }
235:
236: /**
237: * Set a relation to empty : a3.assignB(null)
238: */
239: public void _testBasicSetEmptyNull(int tx) throws Exception {
240: if ((tx == TX_CALL) || (tx == TX_RB)) {
241: utx.begin();
242: }
243: ARemote a = getAHome().findByPrimaryKey("a1");
244: if (tx == TX_CONT) {
245: a.assignBInNewTx(null);
246: } else {
247: a.assignB(null);
248: }
249: if (tx == TX_CALL) {
250: utx.commit();
251: } else if (tx == TX_RB) {
252: utx.rollback();
253: }
254: // checking
255: String idB = a.retrieveB();
256: if (tx == TX_RB) {
257: assertEquals("Wrong assign null for relation a1->b1: ",
258: "b1", idB);
259: } else {
260: assertNull("Wrong assign null for relation a1->b1: " + idB,
261: idB);
262: }
263: // undo
264: if (tx != TX_RB) {
265: a.assignB("b1");
266: }
267:
268: checkIsInitialState();
269:
270: }
271:
272: public void testBasicSetEmptyNullTxNo() throws Exception {
273: _testBasicSetEmptyNull(TX_NO);
274: }
275:
276: public void testBasicSetEmptyNullTxCall() throws Exception {
277: _testBasicSetEmptyNull(TX_CALL);
278: }
279:
280: public void testBasicSetEmptyNullTxCont() throws Exception {
281: _testBasicSetEmptyNull(TX_CONT);
282: }
283:
284: public void testBasicSetEmptyNullTxRb() throws Exception {
285: _testBasicSetEmptyNull(TX_RB);
286: }
287:
288: /**
289: * test coherence relation one to one bidirectionnel,
290: * A1.assignB(B2) => A1.retreiveB()=B2 && A2.retreiveB()=null
291: */
292: public void _testCohSetOne(int tx) throws Exception {
293:
294: if ((tx == TX_CALL) || (tx == TX_RB)) {
295: utx.begin();
296: }
297: ARemote a1 = getAHome().findByPrimaryKey("a1");
298: ARemote a2 = getAHome().findByPrimaryKey("a2");
299: // change the relation
300: if (tx == TX_CONT) {
301: a1.assignBInNewTx("b2");
302: } else {
303: a1.assignB("b2");
304: }
305: if (tx == TX_CALL) {
306: utx.commit();
307: } else if (tx == TX_RB) {
308: utx.rollback();
309: }
310: if (tx != TX_RB) {
311: // Verify expected result
312: assertNull(
313: "Bad coherence of relation null : expected for a2.retreiveB() found :"
314: + a2.retrieveB(), a2.retrieveB());
315: assertEquals(
316: "Bad coherence of relation b2 : expected for a1.retreiveB() found :"
317: + a1.retrieveB(), "b2", a1.retrieveB());
318:
319: // undo
320: a2.assignB("b2");
321: a1.assignB("b1");
322: }
323: checkIsInitialState();
324: }
325:
326: public void testCohSetOneTxNo() throws Exception {
327: _testCohSetOne(TX_NO);
328: }
329:
330: public void testCohSetOneTxCall() throws Exception {
331: _testCohSetOne(TX_CALL);
332: }
333:
334: public void testCohSetOneTxCont() throws Exception {
335: _testCohSetOne(TX_CONT);
336: }
337:
338: public void testCohSetOneTxRb() throws Exception {
339: _testCohSetOne(TX_RB);
340: }
341:
342: /**
343: * test coherence relation one to one bidirectionnel,
344: * A3.assignB(B3)=>A3.retreiveB()==B3
345: */
346: public void _testCohWithoutRelation(int tx) throws Exception {
347:
348: if ((tx == TX_CALL) || (tx == TX_RB)) {
349: utx.begin();
350: }
351: ARemote a3 = getAHome().findByPrimaryKey("a3");
352: // change the relation
353: if (tx == TX_CONT) {
354: a3.assignBInNewTx("b3");
355: } else {
356: a3.assignB("b3");
357: }
358: if (tx == TX_CALL) {
359: utx.commit();
360: } else if (tx == TX_RB) {
361: utx.rollback();
362: }
363: if (tx != TX_RB) {
364: // Verify expected result
365: assertEquals(
366: "Bad coherence of relation : b3 expected for a3.retreiveB() found :"
367: + a3.retrieveB(), "b3", a3.retrieveB());
368:
369: // undo
370: a3.assignB(null);
371: }
372: checkIsInitialState();
373: }
374:
375: public void testBasicCohWithoutRelation() throws Exception {
376: _testCohWithoutRelation(TX_NO);
377: }
378:
379: public void testBasicCohWithoutRelationTxCall() throws Exception {
380: _testCohWithoutRelation(TX_CALL);
381: }
382:
383: /**
384: * test coherence relation one to one bidirectionnel,
385: * A1.assignB(B3)=>A1.retreiveB()==B3
386: */
387: public void _testCohAlreadyAssign(int tx) throws Exception {
388:
389: if ((tx == TX_CALL) || (tx == TX_RB)) {
390: utx.begin();
391: }
392: ARemote a1 = getAHome().findByPrimaryKey("a1");
393: // change the relation
394: if (tx == TX_CONT) {
395: a1.assignBInNewTx("b3");
396: } else {
397: a1.assignB("b3");
398: }
399: if (tx == TX_CALL) {
400: utx.commit();
401: } else if (tx == TX_RB) {
402: utx.rollback();
403: }
404: if (tx != TX_RB) {
405: // Verify expected result
406: assertEquals(
407: "Bad coherence of relation : b3 expected for a1.retreiveB() found :"
408: + a1.retrieveB(), "b3", a1.retrieveB());
409:
410: // undo
411: a1.assignB("b1");
412: }
413: checkIsInitialState();
414: }
415:
416: public void testCohAlreadyAssign() throws Exception {
417: _testCohAlreadyAssign(TX_NO);
418: }
419:
420: public void testCohAlreadyAssignTxCall() throws Exception {
421: _testCohAlreadyAssign(TX_CALL);
422: }
423:
424: /**
425: * test coherence relation one to one bidirectionnel,
426: * A1.assignB(null)=>A1.retreiveB()==null
427: */
428: public void _testCohSetNull(int tx) throws Exception {
429:
430: if ((tx == TX_CALL) || (tx == TX_RB)) {
431: utx.begin();
432: }
433: ARemote a1 = getAHome().findByPrimaryKey("a1");
434: // change the relation
435: if (tx == TX_CONT) {
436: a1.assignBInNewTx(null);
437: } else {
438: a1.assignB(null);
439: }
440: if (tx == TX_CALL) {
441: utx.commit();
442: } else if (tx == TX_RB) {
443: utx.rollback();
444: }
445: if (tx != TX_RB) {
446: // Verify expected result
447: assertNull(
448: "Bad coherence of relation : null expected for a1.retreiveB() found :"
449: + a1.retrieveB(), a1.retrieveB());
450:
451: // undo
452: a1.assignB("b1");
453: }
454: checkIsInitialState();
455: }
456:
457: public void testCohSetNull() throws Exception {
458: _testCohSetNull(TX_NO);
459: }
460:
461: public void testCohSetNullTxCall() throws Exception {
462: _testCohSetNull(TX_CALL);
463: }
464:
465: /**
466: * test coherence relation one to one bidirectionnel,
467: * getBHome().remove(b1)=>B1 removed && A1.retreiveB()==null
468: */
469: public void _testCohRemove(int tx) throws Exception {
470: if (tx == TX_CONT) {
471: // The transaction attribute of the remove method is TX_SUPPORT,
472: // so the transaction cannot be initiate by the container
473: fail("Transaction cannot be initiate by the container for this test");
474: }
475: if ((tx == TX_CALL) || (tx == TX_RB)) {
476: utx.begin();
477: }
478: ARemote a1 = getAHome().findByPrimaryKey("a1");
479: try {
480: // remove the bean
481: getBHome().remove("b1");
482: } finally {
483: if (tx == TX_CALL) {
484: utx.commit();
485: } else if (tx == TX_RB) {
486: utx.rollback();
487: }
488: }
489: if (tx != TX_RB) {
490: // Verify expected result
491: assertNull(
492: "Bad coherence of relation : null expected for a1.retreiveB() found :"
493: + a1.retrieveB(), a1.retrieveB());
494: try {
495: BRemote b1 = getBHome().findByPrimaryKey("b1");
496: fail("BA is not removed");
497: } catch (ObjectNotFoundException e) {
498: }
499:
500: // undo
501: getBHome().create("b1");
502: a1.assignB("b1");
503: }
504: checkIsInitialState();
505: }
506:
507: public void testCohRemoveNoTx() throws Exception {
508: _testCohRemove(TX_NO);
509: }
510:
511: public void testCohRemoveTxCall() throws Exception {
512: _testCohRemove(TX_CALL);
513: }
514:
515: public void testCohRemoveTxRb() throws Exception {
516: _testCohRemove(TX_RB);
517: }
518:
519: public void testRemoveCreate() throws Exception {
520: ARemote a1 = getAHome().findByPrimaryKey("a1");
521: getBHome().remove("b1");
522: BRemote b1 = null;
523: try {
524: b1 = getBHome().findByPrimaryKey("b1");
525: fail("BA is not removed");
526: } catch (ObjectNotFoundException e) {
527: }
528:
529: // undo
530: getBHome().create("b1");
531:
532: utx.begin();
533: try {
534: // remove the bean
535: getBHome().remove("b1");
536: } finally {
537: utx.commit();
538: }
539: try {
540: b1 = getBHome().findByPrimaryKey("b1");
541: fail("BA is not removed");
542: } catch (ObjectNotFoundException e) {
543: }
544:
545: // undo
546: b1 = getBHome().create("b1");
547: a1.assignB("b1");
548:
549: // check initial state
550: //ARemote a2 = getAHome().findByPrimaryKey("a2");
551: BRemote b2 = getBHome().findByPrimaryKey("b2");
552: try {
553: String idB1 = a1.retrieveB();
554: } catch (Exception e) {
555: fail("a1.retrieveB():" + e);
556: }
557:
558: }
559:
560: /**
561: * test coherence relation one to one bidirectionnel,
562: * B1.remove=>B1 removed && A1.retrieveB()==null
563: * Same as _testCohRemove except that the called remove method is on the bean
564: * instead of the home.
565: */
566: public void _testCohBeanRemove(int tx) throws Exception {
567: if (tx == TX_CONT) {
568: // The transaction attribute of the remove method is TX_SUPPORT,
569: // so the transaction cannot be initiate by the container
570: fail("Transaction cannot be initiate by the container for this test");
571: }
572:
573: if ((tx == TX_CALL) || (tx == TX_RB)) {
574: utx.begin();
575: }
576: ARemote a1 = getAHome().findByPrimaryKey("a1");
577: BRemote b1 = getBHome().findByPrimaryKey("b1");
578: try {
579: // remove the bean
580: b1.remove();
581: } finally {
582: if (tx == TX_CALL) {
583: utx.commit();
584: } else if (tx == TX_RB) {
585: utx.rollback();
586: }
587: }
588: if (tx != TX_RB) {
589: // Verify expected result
590: assertNull(
591: "Bad coherence of relation : null expected for a1.retreiveB() found :"
592: + a1.retrieveB(), a1.retrieveB());
593: boolean not_found = false;
594: try {
595: b1 = getBHome().findByPrimaryKey("b1");
596: } catch (ObjectNotFoundException e) {
597: not_found = true;
598: }
599: assertTrue("BA is not removed", not_found);
600: // undo
601: getBHome().create("b1");
602: a1.assignB("b1");
603: }
604: checkIsInitialState();
605: }
606:
607: public void testCohBeanRemoveNoTx() throws Exception {
608: _testCohBeanRemove(TX_NO);
609: }
610:
611: public void testCohBeanRemoveTxCall() throws Exception {
612: _testCohBeanRemove(TX_CALL);
613: }
614:
615: public void testCohBeanRemoveTxRb() throws Exception {
616: _testCohBeanRemove(TX_RB);
617: }
618:
619: public void testRollback() throws Exception {
620: ARemote a = getAHome().findByPrimaryKey("a1");
621: String orig = a.retrieveB();
622:
623: // disassociate and commit
624: utx.begin();
625: a.assignB(null);
626: utx.commit();
627: assertEquals(null, a.retrieveB());
628:
629: // revert to original state outside of a user transaction
630: a.assignB(orig);
631:
632: // retrieveB inside a transaction
633: utx.begin();
634: assertEquals(orig, a.retrieveB());
635: utx.rollback();
636:
637: // verify that retrieveB returns correct value after rollback
638: try {
639: assertEquals(orig, a.retrieveB());
640: } finally {
641: // force sync to cleanup
642: sync(true);
643: }
644: checkIsInitialState();
645: }
646:
647: }
|