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_oobEC2.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.oob.AHomeRemote;
035: import org.objectweb.jonas.jtests.beans.relation.oob.ARemote;
036: import org.objectweb.jonas.jtests.beans.relation.oob.BHomeRemote;
037: import org.objectweb.jonas.jtests.beans.relation.oob.BRemote;
038:
039: /**
040: * This is an advanced test suite for home interface on entity bean CMP2.
041: * Beans used: oob
042: */
043: public class G_Relation_oobEC2 extends A_Cmp2Util {
044:
045: private static String BEAN_HOME_A = "relation_oob_AHome";
046: private static String BEAN_HOME_B = "relation_oob_BHome";
047: protected static AHomeRemote ahome = null;
048: protected static BHomeRemote bhome = null;
049:
050: public G_Relation_oobEC2(String name) {
051: super (name);
052: }
053:
054: protected boolean isInit = false;
055:
056: protected void setUp() {
057: super .setUp();
058: boolean ok = false;
059: int nbtry = 0;
060: while (!ok && nbtry < 3) {
061: if (!isInit) {
062: // load bean if not loaded yet
063: useBeans("oob", false);
064: // lookup home used in the tests
065: try {
066: ahome = (AHomeRemote) PortableRemoteObject
067: .narrow(ictx.lookup(BEAN_HOME_A),
068: AHomeRemote.class);
069: bhome = (BHomeRemote) PortableRemoteObject
070: .narrow(ictx.lookup(BEAN_HOME_B),
071: BHomeRemote.class);
072: } catch (NamingException e) {
073: fail("Cannot get bean home: " + e.getMessage());
074: }
075: // check if tables have been initialized
076: try {
077: ahome.findByPrimaryKey("a2");
078: } catch (Exception e) {
079: // Make the initialization needed for the tests
080: try {
081: utx.begin();
082: ARemote a1 = ahome.create("a1");
083: ARemote a2 = ahome.create("a2");
084: ahome.create("a3");
085: bhome.create("b1");
086: bhome.create("b2");
087: bhome.create("b3");
088: a1.assignB("b1");
089: a2.assignB("b2");
090: } catch (Exception i) {
091: fail("InitialState creation problem: " + i);
092: } finally {
093: try {
094: utx.commit();
095: } catch (Exception ii) {
096: }
097: }
098: }
099: isInit = true;
100: }
101: // Check that all is OK. Sometimes, a test has failed and has corrupted
102: // the bean state in the database. We must unload and reload the bean then.
103: nbtry++;
104: try {
105: if (initStateOK()) {
106: ok = true;
107: }
108: } catch (Exception e) {
109: }
110: if (!ok) {
111: isInit = false;
112: unloadBeans("oob");
113: }
114: }
115: }
116:
117: /*
118: * Check that we are in the same state as after the tables creation for thoses beans A and B
119: * (ie if it is the initial state)
120: */
121: boolean initStateOK() throws Exception {
122: boolean isOk = true;
123:
124: msgerror = new StringBuffer();
125:
126: ARemote a1 = ahome.findByPrimaryKey("a1");
127: BRemote b1 = bhome.findByPrimaryKey("b1");
128: ARemote a2 = ahome.findByPrimaryKey("a2");
129: BRemote b2 = bhome.findByPrimaryKey("b2");
130: ARemote a3 = ahome.findByPrimaryKey("a3");
131: BRemote b3 = bhome.findByPrimaryKey("b3");
132: String idB1 = a1.retrieveB();
133: String idB2 = a2.retrieveB();
134: String idB3 = a3.retrieveB();
135: String idA1 = b1.retrieveA();
136: String idA2 = b2.retrieveA();
137: String idA3 = b3.retrieveA();
138:
139: if (idB1 != null && !idB1.equals("b1")) {
140: isOk = false;
141: msgerror.append("\nWrong relation for a1->b1"
142: + "(expected: A1.retrieveB()='b1'" + ", found:"
143: + idB1 + ")");
144: }
145: if (idB2 != null && !idB2.equals("b2")) {
146: isOk = false;
147: msgerror.append("\nWrong relation for a2->b2"
148: + "(expected: A2.retrieveB()='b2'" + ", found:"
149: + idB2 + ")");
150: }
151: if (idA1 != null && !idA1.equals("a1")) {
152: isOk = false;
153: msgerror.append("\nWrong relation for b1->a1"
154: + "(expected: B1.retrieveA()='a1'" + ", found:"
155: + idA1 + ")");
156: }
157: if (idA2 != null && !idA2.equals("a2")) {
158: isOk = false;
159: msgerror.append("\nWrong relation for b2->a2"
160: + "(expected: B2.retrieveA()='a2'" + ", found:"
161: + idA2 + ")");
162: }
163: if (idA3 != null) {
164: isOk = false;
165: msgerror.append("\nWrong relation for a3->b3"
166: + "(expected: A3.retrieveB()=null" + ", found:"
167: + idA3 + ")");
168: }
169: if (idB3 != null) {
170: isOk = false;
171: msgerror.append("\nWrong relation for b3->a3"
172: + "(expected: B3.retrieveA()=null" + ", found:"
173: + idB3 + ")");
174: }
175: return isOk;
176: }
177:
178: /**
179: * Check that the bean 'a3' has no relation.
180: */
181: public void _testBasicGetEmpty(int tx) throws Exception {
182: String idB = null;
183: if ((tx == TX_CALL) || (tx == TX_RB)) {
184: utx.begin();
185: }
186: ARemote a = ahome.findByPrimaryKey("a3");
187: if (tx == TX_CONT) {
188: idB = a.retrieveBInNewTx();
189: } else {
190: idB = a.retrieveB();
191: }
192: if (tx == TX_CALL) {
193: utx.commit();
194: } else if (tx == TX_RB) {
195: utx.rollback();
196: }
197: checkIsInitialState();
198: }
199:
200: /**
201: * Set a relation to empty : a3.assignB("b3")
202: */
203: public void _testBasicSetEmpty(int tx) throws Exception {
204: if ((tx == TX_CALL) || (tx == TX_RB)) {
205: utx.begin();
206: }
207: ARemote a = ahome.findByPrimaryKey("a3");
208: if (tx == TX_CONT) {
209: a.assignBInNewTx("b3");
210: } else {
211: a.assignB("b3");
212: }
213: if (tx == TX_CALL) {
214: utx.commit();
215: } else if (tx == TX_RB) {
216: utx.rollback();
217: }
218: // checking
219: String idB = a.retrieveB();
220: if (tx != TX_RB) {
221: assertEquals("Wrong assign null for relation a3->b3 : ",
222: "b3", idB);
223: } else {
224: assertNull("Wrong assign null for relation a3->b3 : ", idB);
225: }
226: // undo
227: if (tx != TX_RB) {
228: a.assignB(null);
229: }
230: checkIsInitialState();
231: }
232:
233: public void testBasicSetEmptyTxNo() throws Exception {
234: _testBasicSetEmpty(TX_NO);
235: }
236:
237: public void testBasicSetEmptyTxCall() throws Exception {
238: _testBasicSetEmpty(TX_CALL);
239: }
240:
241: public void testBasicSetEmptyTxCont() throws Exception {
242: _testBasicSetEmpty(TX_CONT);
243: }
244:
245: /**
246: * Set a relation to empty : a3.assignB(null)
247: */
248: public void _testBasicSetEmptyNull(int tx) throws Exception {
249: if ((tx == TX_CALL) || (tx == TX_RB)) {
250: utx.begin();
251: }
252: ARemote a = ahome.findByPrimaryKey("a1");
253: if (tx == TX_CONT) {
254: a.assignBInNewTx(null);
255: } else {
256: a.assignB(null);
257: }
258: if (tx == TX_CALL) {
259: utx.commit();
260: } else if (tx == TX_RB) {
261: utx.rollback();
262: }
263: // checking
264: String idB = a.retrieveB();
265: if (tx == TX_RB) {
266: assertEquals("Wrong assign null for relation a1->b1: ",
267: "b1", idB);
268: } else {
269: assertNull("Wrong assign null for relation a1->b1: " + idB,
270: idB);
271: }
272: // undo
273: if (tx != TX_RB) {
274: a.assignB("b1");
275: }
276: checkIsInitialState();
277: }
278:
279: /**
280: * test coherence relation one to one bidirectionnel,
281: * A1.assignB(B2) => A1.retreiveB()=B2 && B2.retreiveA()=A1 && B1.retreiveA()=null && A2.retreiveB()=null
282: */
283: public void _testCohSetOne(int tx) throws Exception {
284: if ((tx == TX_CALL) || (tx == TX_RB)) {
285: utx.begin();
286: }
287: ARemote a1 = ahome.findByPrimaryKey("a1");
288: BRemote b1 = bhome.findByPrimaryKey("b1");
289: ARemote a2 = ahome.findByPrimaryKey("a2");
290: BRemote b2 = bhome.findByPrimaryKey("b2");
291: ARemote a3 = ahome.findByPrimaryKey("a3");
292: BRemote b3 = bhome.findByPrimaryKey("b3");
293: // change the relation
294: if (tx == TX_CONT) {
295: a1.assignBInNewTx("b2");
296: } else {
297: a1.assignB("b2");
298: }
299: if (tx == TX_CALL) {
300: utx.commit();
301: } else if (tx == TX_RB) {
302: utx.rollback();
303: }
304: if (tx != TX_RB) {
305: // Verify expected result
306: assertNull(
307: "Bad coherence of relation null : expected for a2.retreiveB() found :"
308: + a2.retrieveB(), a2.retrieveB());
309: assertNull(
310: "Bad coherence of relation null : expected for b1.retreiveA() found :"
311: + b1.retrieveA(), b1.retrieveA());
312: assertEquals(
313: "Bad coherence of relation a1 : expected for b2.retreiveA() found :"
314: + b2.retrieveA(), "a1", b2.retrieveA());
315: assertEquals(
316: "Bad coherence of relation b2 : expected for a1.retreiveB() found :"
317: + a1.retrieveB(), "b2", a1.retrieveB());
318:
319: // undo
320: a1.assignB("b1");
321: a2.assignB("b2");
322: }
323: checkIsInitialState();
324: }
325:
326: public static Test suite() {
327: return new TestSuite(G_Relation_oobEC2.class);
328: }
329:
330: public static void main(String args[]) {
331: String testtorun = null;
332: // Get args
333: for (int argn = 0; argn < args.length; argn++) {
334: String s_arg = args[argn];
335: Integer i_arg;
336: if (s_arg.equals("-n")) {
337: testtorun = args[++argn];
338: }
339: }
340: if (testtorun == null) {
341: junit.textui.TestRunner.run(suite());
342: } else {
343: junit.textui.TestRunner
344: .run(new G_Relation_oobEC2(testtorun));
345: }
346: }
347: }
|