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: G_Relation_oouEC2.java 7533 2005-10-19 15:55:05Z durieuxp $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.clients.entity;
027:
028: import javax.naming.NamingException;
029: import javax.rmi.PortableRemoteObject;
030:
031: import junit.framework.Test;
032: import junit.framework.TestSuite;
033:
034: import org.objectweb.jonas.jtests.beans.relation.oou.AHomeRemote;
035: import org.objectweb.jonas.jtests.beans.relation.oou.ARemote;
036: import org.objectweb.jonas.jtests.beans.relation.oou.BHomeRemote;
037: import org.objectweb.jonas.jtests.beans.relation.oou.BRemote;
038:
039: /**
040: * This is an advanced test suite for home interface on entity bean CMP2.
041: */
042: public class G_Relation_oouEC2 extends A_Cmp2Util {
043:
044: private static String BEAN_HOME_A = "relation_oou_AHome";
045: private static String BEAN_HOME_B = "relation_oou_BHome";
046: protected static AHomeRemote ahome = null;
047: protected static BHomeRemote bhome = null;
048:
049: public G_Relation_oouEC2(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: // lookup home used in the tests
064: try {
065: ahome = (AHomeRemote) PortableRemoteObject
066: .narrow(ictx.lookup(BEAN_HOME_A),
067: AHomeRemote.class);
068: bhome = (BHomeRemote) PortableRemoteObject
069: .narrow(ictx.lookup(BEAN_HOME_B),
070: BHomeRemote.class);
071: } catch (NamingException e) {
072: fail("Cannot get bean home: " + e.getMessage());
073: }
074: // check if tables have been initialized
075: try {
076: ahome.findByPrimaryKey("a2");
077: } catch (Exception e) {
078: // Make the initialization needed for the tests
079: try {
080: utx.begin();
081: ARemote a1 = ahome.create("a1");
082: ARemote a2 = ahome.create("a2");
083: ahome.create("a3");
084: bhome.create("b1");
085: bhome.create("b2");
086: bhome.create("b3");
087: a1.assignB("b1");
088: a2.assignB("b2");
089: } catch (Exception i) {
090: fail("InitialState creation problem: " + i);
091: } finally {
092: try {
093: utx.commit();
094: } catch (Exception ii) {
095: }
096: }
097: }
098: isInit = true;
099: }
100: // Check that all is OK. Sometimes, a test has failed and has corrupted
101: // the bean state in the database. We must unload and reload the bean then.
102: nbtry++;
103: try {
104: if (initStateOK()) {
105: ok = true;
106: }
107: } catch (Exception e) {
108: }
109: if (!ok) {
110: isInit = false;
111: unloadBeans("oou");
112: }
113: }
114: }
115:
116: /*
117: * Check that we are in the same state as after the tables creation for thoses beans A and B
118: * (ie if it is the initial state)
119: */
120: boolean initStateOK() throws Exception {
121: boolean isOk = true;
122:
123: msgerror = new StringBuffer();
124:
125: ARemote a1 = ahome.findByPrimaryKey("a1");
126: BRemote b1 = bhome.findByPrimaryKey("b1");
127: ARemote a2 = ahome.findByPrimaryKey("a2");
128: BRemote b2 = bhome.findByPrimaryKey("b2");
129: ARemote a3 = ahome.findByPrimaryKey("a3");
130: BRemote b3 = bhome.findByPrimaryKey("b3");
131: String idB1 = a1.retrieveB();
132: String idB2 = a2.retrieveB();
133: String idB3 = a3.retrieveB();
134:
135: if (idB1 != null && !idB1.equals("b1")) {
136: isOk = false;
137: msgerror.append("Wrong relation for a1->b1"
138: + "(expected: A1.retrieveB()='b1'" + ", found:"
139: + idB1 + ")");
140: } else if (idB2 != null && !idB2.equals("b2")) {
141: isOk = false;
142: msgerror.append("Wrong relation for a2->b2"
143: + "(expected: A2.retrieveB()='b2'" + ", found:"
144: + idB2 + ")");
145: } else if (idB3 != null) {
146: isOk = false;
147: msgerror.append("Wrong relation for b3->a3"
148: + "(expected: B3.retrieveA()=null" + ", found:"
149: + idB3 + ")");
150: }
151: return isOk;
152: }
153:
154: /**
155: * Set a relation to empty : a3.assignB("b3")
156: */
157: public void _testBasicSetEmpty(int tx) throws Exception {
158: if ((tx == TX_CALL) || (tx == TX_RB)) {
159: utx.begin();
160: }
161: ARemote a = ahome.findByPrimaryKey("a3");
162: if (tx == TX_CONT) {
163: a.assignBInNewTx("b3");
164: } else {
165: a.assignB("b3");
166: }
167: if (tx == TX_CALL) {
168: utx.commit();
169: } else if (tx == TX_RB) {
170: utx.rollback();
171: }
172: // checking
173: String idB = a.retrieveB();
174: if (tx != TX_RB) {
175: assertEquals("Wrong assign null for relation a3->b3 : ",
176: "b3", idB);
177: } else {
178: assertNull("Wrong assign null for relation a3->b3 : ", idB);
179: }
180: // undo
181: if (tx != TX_RB) {
182: a.assignB(null);
183: }
184: checkIsInitialState();
185: }
186:
187: public void testBasicSetEmptyTxNo() throws Exception {
188: _testBasicSetEmpty(TX_NO);
189: }
190:
191: public void testBasicSetEmptyTxCall() throws Exception {
192: _testBasicSetEmpty(TX_CALL);
193: }
194:
195: public void testBasicSetEmptyTxCont() throws Exception {
196: _testBasicSetEmpty(TX_CONT);
197: }
198:
199: public static Test suite() {
200: return new TestSuite(G_Relation_oouEC2.class);
201: }
202:
203: public static void main(String args[]) {
204: String testtorun = null;
205: // Get args
206: for (int argn = 0; argn < args.length; argn++) {
207: String s_arg = args[argn];
208: Integer i_arg;
209: if (s_arg.equals("-n")) {
210: testtorun = args[++argn];
211: }
212: }
213: if (testtorun == null) {
214: junit.textui.TestRunner.run(suite());
215: } else {
216: junit.textui.TestRunner
217: .run(new G_Relation_oouEC2(testtorun));
218: }
219: }
220: }
|