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_omuEC2.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.omu.AHomeRemote;
040: import org.objectweb.jonas.jtests.beans.relation.omu.ARemote;
041: import org.objectweb.jonas.jtests.beans.relation.omu.BHomeRemote;
042:
043: /**
044: * For testing one-to-many unidirectional relationships
045: * @author S.Chassande-Barrioz, Helene Joanin
046: */
047:
048: public class G_Relation_omuEC2 extends A_Cmp2Util {
049:
050: private static String BEAN_HOME_A = "relation_omu_AHome";
051: protected static AHomeRemote ahome = null;
052: private static String BEAN_HOME_B = "relation_omu_BHome";
053: protected static BHomeRemote bhome = null;
054:
055: static Hashtable tbRelationA2B = new Hashtable();
056:
057: static {
058: tbRelationA2B.put("as0", new String[] {});
059: tbRelationA2B.put("as1", new String[] { "bs1_1" });
060: tbRelationA2B.put("as2", new String[] { "bs2_1", "bs2_2",
061: "bs2_3" });
062: tbRelationA2B.put("a0", new String[] {});
063: tbRelationA2B
064: .put("a1", new String[] { "b1_1", "b1_2", "b1_3" });
065: tbRelationA2B
066: .put("a2", new String[] { "b2_1", "b2_2", "b2_3" });
067: tbRelationA2B.put("a3", new String[] { "b3_1" });
068: // Translate the String[] to a Collection of String
069: for (Enumeration ea = tbRelationA2B.keys(); ea
070: .hasMoreElements();) {
071: String aname = (String) (ea.nextElement());
072: String[] tb = (String[]) tbRelationA2B.get(aname);
073: ArrayList col = new ArrayList(tb.length);
074: for (int i = 0; i < tb.length; i++) {
075: col.add(tb[i]);
076: }
077: tbRelationA2B.put(aname, col);
078: }
079: }
080:
081: public G_Relation_omuEC2(String name) {
082: super (name);
083: }
084:
085: protected boolean isInit = false;
086:
087: protected void setUp() {
088: super .setUp();
089: boolean ok = false;
090: int nbtry = 0;
091: while (!ok && nbtry < 3) {
092: if (!isInit) {
093: // load bean if not loaded yet
094: useBeans("omu", false);
095: try {
096: ahome = (AHomeRemote) PortableRemoteObject
097: .narrow(ictx.lookup(BEAN_HOME_A),
098: AHomeRemote.class);
099: bhome = (BHomeRemote) PortableRemoteObject
100: .narrow(ictx.lookup(BEAN_HOME_B),
101: BHomeRemote.class);
102: } catch (NamingException e) {
103: fail("Cannot get bean home: " + e.getMessage());
104: }
105: // check if tables have been initialized
106: try {
107: ahome.findByPrimaryKey("a0");
108: } catch (Exception e) {
109: try {
110: utx.begin();
111: bhome.create("bs0");
112: bhome.create("bs1_1");
113: bhome.create("bs2_1");
114: bhome.create("bs2_2");
115: bhome.create("bs2_3");
116: bhome.create("bs3");
117: bhome.create("b0");
118: bhome.create("b1_1");
119: bhome.create("b1_2");
120: bhome.create("b1_3");
121: bhome.create("b2_1");
122: bhome.create("b2_2");
123: bhome.create("b2_3");
124: bhome.create("b3_1");
125: ARemote as0 = ahome.create("as0");
126: ARemote as1 = ahome.create("as1");
127: ARemote as2 = ahome.create("as2");
128: ARemote a0 = ahome.create("a0");
129: ARemote a1 = ahome.create("a1");
130: ARemote a2 = ahome.create("a2");
131: ARemote a3 = ahome.create("a3");
132: as1.assignB((Collection) tbRelationA2B
133: .get("as1"));
134: as2.assignB((Collection) tbRelationA2B
135: .get("as2"));
136: a1
137: .assignB((Collection) tbRelationA2B
138: .get("a1"));
139: a2
140: .assignB((Collection) tbRelationA2B
141: .get("a2"));
142: a3
143: .assignB((Collection) tbRelationA2B
144: .get("a3"));
145: } catch (Exception i) {
146: fail("InitialState creation problem:: " + i);
147: } finally {
148: try {
149: utx.commit();
150: } catch (Exception ii) {
151: }
152: }
153: }
154: isInit = true;
155: }
156: // Check that all is OK. Sometimes, a test has failed and has corrupted
157: // the bean state in the database. We must unload and reload the bean then.
158: nbtry++;
159: try {
160: if (initStateOK()) {
161: ok = true;
162: }
163: } catch (Exception e) {
164: }
165: if (!ok) {
166: isInit = false;
167: unloadBeans("omu");
168: }
169: }
170: }
171:
172: /*
173: * Check that we are in the same state as after the tables creation for thoses beans A and B
174: * (ie if it is the initial state)
175: */
176: boolean initStateOK() throws Exception {
177: boolean isOk = true;
178: msgerror = new StringBuffer();
179: for (Enumeration ea = tbRelationA2B.keys(); ea
180: .hasMoreElements();) {
181: String aname = (String) (ea.nextElement());
182: ARemote a = ahome.findByPrimaryKey(aname);
183: Collection colActual = a.retrieveB();
184: ArrayList colExpected = (ArrayList) (tbRelationA2B
185: .get(aname));
186: debug("Relation for " + aname + " :: expected:"
187: + colExpected + ", found:" + colActual);
188: if (!isCollectionEqual(colExpected, colActual)) {
189: isOk = false;
190: msgerror = msgerror.append("Wrong relation for "
191: + aname + " (expected:" + colExpected
192: + ", found:" + colActual + ")");
193: }
194: }
195: return isOk;
196: }
197:
198: /**
199: * Check that the bean 'as0' has no relation.
200: */
201: public void _testBasicGetEmpty(int tx) throws Exception {
202: Collection c = null;
203: if ((tx == TX_CALL) || (tx == TX_RB)) {
204: utx.begin();
205: }
206: ARemote a = ahome.findByPrimaryKey("as0");
207: if (tx == TX_CONT) {
208: c = a.retrieveBInNewTx();
209: } else {
210: c = a.retrieveB();
211: }
212: if (tx == TX_CALL) {
213: utx.commit();
214: } else if (tx == TX_RB) {
215: utx.rollback();
216: }
217: checkIsInitialState();
218: }
219:
220: public void testBasicGetEmptyTxNo() throws Exception {
221: _testBasicGetEmpty(TX_NO);
222: }
223:
224: public void testBasicGetEmptyTxCall() throws Exception {
225: _testBasicGetEmpty(TX_CALL);
226: }
227:
228: public void testBasicGetEmptyTxCont() throws Exception {
229: _testBasicGetEmpty(TX_CONT);
230: }
231:
232: /**
233: * Check that the bean 'as1' has only one relation with 'bs1_1'.
234: */
235: public void _testBasicGetOne(int tx) throws Exception {
236:
237: Collection c = null;
238: if ((tx == TX_CALL) || (tx == TX_RB)) {
239: utx.begin();
240: }
241: ARemote a = ahome.findByPrimaryKey("as1");
242: if (tx == TX_CONT) {
243: c = a.retrieveBInNewTx();
244: } else {
245: c = a.retrieveB();
246: }
247: if (tx == TX_CALL) {
248: utx.commit();
249: } else if (tx == TX_RB) {
250: utx.rollback();
251: }
252: // checking
253: assertEquals("Wrong relations size for as1: ", 1, c.size());
254: String s = "bs1_1";
255: assertTrue("Relation missing " + s + " for as1: ", c
256: .contains(s));
257: checkIsInitialState();
258: }
259:
260: /**
261: * Check that the bean 'as2' has many relations with 'bs2_1', 'bs2_2', 'bs2_3'.
262: */
263: public void _testBasicGetMany(int tx) throws Exception {
264:
265: Collection c = null;
266: if ((tx == TX_CALL) || (tx == TX_RB)) {
267: utx.begin();
268: }
269: ARemote a = ahome.findByPrimaryKey("as2");
270: if (tx == TX_CONT) {
271: c = a.retrieveBInNewTx();
272: } else {
273: c = a.retrieveB();
274: }
275: if (tx == TX_CALL) {
276: utx.commit();
277: } else if (tx == TX_RB) {
278: utx.rollback();
279: }
280: // checking
281: assertEquals("Relations size of: ", 3, c.size());
282: String s = "bs2_1";
283: assertTrue("Relation missing " + s + ": ", c.contains(s));
284: s = "bs2_2";
285: assertTrue("Relation missing " + s + ": ", c.contains(s));
286: s = "bs2_3";
287: assertTrue("Relation missing " + s + ": ", c.contains(s));
288: checkIsInitialState();
289: }
290:
291: public void _testBasicSetEmpty(int tx) throws Exception {
292: ArrayList ce = new ArrayList();
293: if ((tx == TX_CALL) || (tx == TX_RB)) {
294: utx.begin();
295: }
296: ARemote a = ahome.findByPrimaryKey("as1");
297: if (tx == TX_CONT) {
298: a.assignBInNewTx(ce);
299: } else {
300: a.assignB(ce);
301: }
302: if (tx == TX_CALL) {
303: utx.commit();
304: } else if (tx == TX_RB) {
305: utx.rollback();
306: }
307: // checking
308: Collection ca = a.retrieveB();
309: if (tx != TX_RB) {
310: assertEquals("Relations size of: ", 0, ca.size());
311: } else {
312: assertEquals("Relations size of: ", 1, ca.size());
313: }
314: // undo
315: if (tx != TX_RB) {
316: a.addInB("bs1_1");
317: // Check undo
318: ca = a.retrieveB();
319: assertEquals("After undo, Relations size of: ", 1, ca
320: .size());
321: }
322: checkIsInitialState();
323: }
324:
325: public void testBasicSetEmptyTxNo() throws Exception {
326: _testBasicSetEmpty(TX_NO);
327: }
328:
329: public void testBasicSetEmptyTxCall() throws Exception {
330: _testBasicSetEmpty(TX_CALL);
331: }
332:
333: public void testBasicSetEmptyTxCont() throws Exception {
334: _testBasicSetEmpty(TX_CONT);
335: }
336:
337: public static Test suite() {
338: return new TestSuite(G_Relation_omuEC2.class);
339: }
340:
341: public static void main(String args[]) {
342: String testtorun = null;
343: // Get args
344: for (int argn = 0; argn < args.length; argn++) {
345: String s_arg = args[argn];
346: Integer i_arg;
347: if (s_arg.equals("-n")) {
348: testtorun = args[++argn];
349: }
350: }
351: if (testtorun == null) {
352: junit.textui.TestRunner.run(suite());
353: } else {
354: junit.textui.TestRunner
355: .run(new G_Relation_omuEC2(testtorun));
356: }
357: }
358:
359: }
|