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_mnbEC2.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.ArrayList;
029: import java.util.Collection;
030: import java.util.Enumeration;
031: import java.util.Hashtable;
032:
033: import javax.naming.NamingException;
034: import javax.rmi.PortableRemoteObject;
035:
036: import junit.framework.Test;
037: import junit.framework.TestSuite;
038:
039: import org.objectweb.jonas.jtests.beans.relation.mnb.AHomeRemote;
040: import org.objectweb.jonas.jtests.beans.relation.mnb.ARemote;
041: import org.objectweb.jonas.jtests.beans.relation.mnb.BHomeRemote;
042: import org.objectweb.jonas.jtests.beans.relation.mnb.BRemote;
043:
044: /**
045: * For testing many-to-many bidirectional relationships
046: * @author S.Chassande-Barrioz, J. Camilleri
047: *
048: *
049: **/
050: public class G_Relation_mnbEC2 extends A_Cmp2Util {
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: private static String BEAN_HOME_A = "relation_mnb_AHome";
094: protected static AHomeRemote ahome = null;
095: private static String BEAN_HOME_B = "relation_mnb_BHome";
096: protected static BHomeRemote bhome = null;
097:
098: public G_Relation_mnbEC2(String name) {
099: super (name);
100: }
101:
102: public void testEmpty() throws Exception {
103: }
104:
105: protected boolean isInit = false;
106:
107: protected void setUp() {
108: super .setUp();
109: boolean ok = false;
110: int nbtry = 0;
111: while (!ok && nbtry < 3) {
112: if (!isInit) {
113: // load bean if not loaded yet
114: useBeans("mnb", false);
115: // lookup home used in the tests
116: try {
117: ahome = (AHomeRemote) PortableRemoteObject
118: .narrow(ictx.lookup(BEAN_HOME_A),
119: AHomeRemote.class);
120: bhome = (BHomeRemote) PortableRemoteObject
121: .narrow(ictx.lookup(BEAN_HOME_B),
122: BHomeRemote.class);
123: } catch (NamingException e) {
124: fail("Cannot get bean home: " + e.getMessage());
125: }
126: // check if tables have been initialized
127: try {
128: ahome.findByPrimaryKey("a0");
129: } catch (Exception e) {
130: try {
131: // IMPORTANT: comments begin/commit to highlight a bug
132: utx.begin();
133: ahome.create("a0");
134: ahome.create("a1");
135: ahome.create("a2");
136: ahome.create("a3");
137: bhome.create("b0");
138: bhome.create("b1");
139: bhome.create("b2");
140: bhome.create("b3");
141: bhome.create("b4");
142: ARemote a0 = ahome.findByPrimaryKey("a0");
143: ARemote a1 = ahome.findByPrimaryKey("a1");
144: ARemote a2 = ahome.findByPrimaryKey("a2");
145: ARemote a3 = ahome.findByPrimaryKey("a3");
146: Collection rel_a0 = (Collection) (tbRelationA2B
147: .get("a0"));
148: a0.assignB(rel_a0);
149: Collection rel_a1 = (Collection) (tbRelationA2B
150: .get("a1"));
151: a1.assignB(rel_a1);
152: Collection rel_a2 = (Collection) (tbRelationA2B
153: .get("a2"));
154: a2.assignB(rel_a2);
155: Collection rel_a3 = (Collection) (tbRelationA2B
156: .get("a3"));
157: a3.assignB(rel_a3);
158: } catch (Exception i) {
159: fail("InitialState creation problem: " + i);
160: } finally {
161: try {
162: utx.commit();
163: } catch (Exception ii) {
164: }
165: }
166: }
167: isInit = true;
168: }
169: // Check that all is OK. Sometimes, a test has failed and has corrupted
170: // the bean state in the database. We must unload and reload the bean then.
171: nbtry++;
172: try {
173: if (initStateOK()) {
174: ok = true;
175: }
176: } catch (Exception e) {
177: }
178: if (!ok) {
179: debug("F_Relation_mnbEC2 setUp: unload bean and retry");
180: isInit = false;
181: unloadBeans("mnb");
182: }
183: }
184: }
185:
186: /*
187: * Check that we are in the same state as after the tables creation for thoses beans A and B
188: * (ie if it is the initial state)
189: */
190: boolean initStateOK() throws Exception {
191: boolean isOk = true;
192: msgerror = new StringBuffer();
193: // Check relations A to B
194: for (Enumeration ea = tbRelationA2B.keys(); ea
195: .hasMoreElements();) {
196: String aname = (String) (ea.nextElement());
197: ARemote a = ahome.findByPrimaryKey(aname);
198: Collection colActual = a.retrieveB();
199: ArrayList colExpected = (ArrayList) (tbRelationA2B
200: .get(aname));
201: if (!isCollectionEqual(colExpected, colActual)) {
202: isOk = false;
203: msgerror = msgerror.append("Wrong relation for "
204: + aname + " (expected:" + colExpected
205: + ", found:" + colActual + ")");
206: }
207: }
208: // Check Relations B to A
209: for (Enumeration ea1 = tbRelationB2A.keys(); ea1
210: .hasMoreElements();) {
211: String aname1 = (String) (ea1.nextElement());
212: BRemote b = bhome.findByPrimaryKey(aname1);
213: Collection colActual1 = b.retrieveA();
214: ArrayList colExpected1 = (ArrayList) (tbRelationB2A
215: .get(aname1));
216: if (!isCollectionEqual(colExpected1, colActual1)) {
217: isOk = false;
218: msgerror = msgerror.append("Wrong relation for "
219: + aname1 + " (expected:" + colExpected1
220: + ", found:" + colActual1 + ")");
221: }
222: }
223: return isOk;
224: }
225:
226: /**
227: * Check that the bean 'a0' has no relation.
228: */
229: public void _testBasicGetEmpty(int tx) throws Exception {
230: Collection c;
231: if ((tx == TX_CALL) || (tx == TX_RB)) {
232: utx.begin();
233: }
234: ARemote a = ahome.findByPrimaryKey("a0");
235: if (tx == TX_CONT) {
236: c = a.retrieveBInNewTx();
237: } else {
238: c = a.retrieveB();
239: }
240: if (tx == TX_CALL) {
241: utx.commit();
242: } else if (tx == TX_RB) {
243: utx.rollback();
244: }
245: checkIsInitialState();
246: }
247:
248: public void testBasicGetEmptyTxNo() throws Exception {
249: _testBasicGetEmpty(TX_NO);
250: }
251:
252: public void testBasicGetEmptyTxCall() throws Exception {
253: _testBasicGetEmpty(TX_CALL);
254: }
255:
256: public void testBasicGetEmptyTxCont() throws Exception {
257: _testBasicGetEmpty(TX_CONT);
258: }
259:
260: /**
261: * Ckeck the new relation a0-b0
262: */
263: public void _testBasicSetEmpty(int tx) throws Exception {
264: ArrayList c = new ArrayList(1);
265: c.add("b0");
266: if ((tx == TX_CALL) || (tx == TX_RB)) {
267: utx.begin();
268: }
269: ARemote a = ahome.findByPrimaryKey("a0");
270: if (tx == TX_CONT) {
271: a.assignBInNewTx(c);
272: } else {
273: a.assignB(c);
274: }
275: if (tx == TX_CALL) {
276: utx.commit();
277: } else if (tx == TX_RB) {
278: utx.rollback();
279: }
280: // checking
281: Collection c_value = a.retrieveB();
282: if (tx != TX_RB) {
283: assertTrue("Wrong relations a0 (required:" + c + ", found:"
284: + c_value + ")", isCollectionEqual(c_value, c));
285: } else {
286: assertTrue("Wrong relation a0->b0 : ", c_value.isEmpty());
287: }
288: // undo
289: if (tx != TX_RB) {
290: a.assignB(new ArrayList());
291: }
292: checkIsInitialState();
293: }
294:
295: public void testBasicSetEmptyTxNo() throws Exception {
296: _testBasicSetEmpty(TX_NO);
297: }
298:
299: public void testBasicSetEmptyTxCall() throws Exception {
300: _testBasicSetEmpty(TX_CALL);
301: }
302:
303: public void testBasicSetEmptyTxCont() throws Exception {
304: _testBasicSetEmpty(TX_CONT);
305: }
306:
307: /**
308: * Ckeck clear function
309: */
310: public void _testBasicClear(int tx) throws Exception {
311: if ((tx == TX_CALL) || (tx == TX_RB)) {
312: utx.begin();
313: }
314: ARemote a2 = ahome.findByPrimaryKey("a2");
315: if (tx == TX_CONT) {
316: a2.clearBInNewTx();
317: } else {
318: a2.clearB();
319: }
320: if (tx == TX_CALL) {
321: utx.commit();
322: } else if (tx == TX_RB) {
323: utx.rollback();
324: }
325: // checking
326: Collection a2_value = a2.retrieveB();
327: BRemote b1 = bhome.findByPrimaryKey("b1");
328: BRemote b2 = bhome.findByPrimaryKey("b2");
329: BRemote b3 = bhome.findByPrimaryKey("b3");
330: ArrayList collb1 = new ArrayList(1);
331: collb1.add("a1");
332: ArrayList collb2 = new ArrayList(2);
333: collb2.add("a1");
334: collb2.add("a3");
335: ArrayList collb3 = new ArrayList(1);
336: collb3.add("a3");
337: Collection col = null;
338: if (tx != TX_RB) {
339: assertTrue("Wrong relations a2 (required: Empty "
340: + ", found:" + a2_value + ")", a2_value.isEmpty());
341: col = bhome.findByPrimaryKey("b1").retrieveA();
342: assertTrue(
343: "Wrong collection returned by b1.getA(): expected:"
344: + collb1 + " found: " + col,
345: isCollectionEqual(collb1, col));
346: col = bhome.findByPrimaryKey("b2").retrieveA();
347: assertTrue(
348: "Wrong collection returned by b2.getA(): expected:"
349: + collb2 + " found: " + col,
350: isCollectionEqual(collb2, col));
351: col = bhome.findByPrimaryKey("b3").retrieveA();
352: assertTrue(
353: "Wrong collection returned by b3.getA(): expected:"
354: + collb3 + " found: " + col,
355: isCollectionEqual(collb3, col));
356: // undo
357: ArrayList a2undo = new ArrayList(3);
358: a2undo.add("b1");
359: a2undo.add("b2");
360: a2undo.add("b3");
361: a2.assignB(a2undo);
362: }
363: checkIsInitialState();
364: }
365:
366: public static Test suite() {
367: return new TestSuite(G_Relation_mnbEC2.class);
368: }
369:
370: public static void main(String args[]) {
371: String testtorun = null;
372: // Get args
373: for (int argn = 0; argn < args.length; argn++) {
374: String s_arg = args[argn];
375: Integer i_arg;
376: if (s_arg.equals("-n")) {
377: testtorun = args[++argn];
378: }
379: }
380: if (testtorun == null) {
381: junit.textui.TestRunner.run(suite());
382: } else {
383: junit.textui.TestRunner
384: .run(new G_Relation_mnbEC2(testtorun));
385: }
386: }
387:
388: }
|