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: F_Relation_pkcompEC2.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: import javax.naming.NamingException;
033: import javax.rmi.PortableRemoteObject;
034: import junit.framework.Test;
035: import junit.framework.TestSuite;
036: import org.objectweb.jonas.jtests.beans.relation.pkcomp.APK;
037: import org.objectweb.jonas.jtests.beans.relation.pkcomp.AHomeRemote;
038: import org.objectweb.jonas.jtests.beans.relation.pkcomp.ARemote;
039: import org.objectweb.jonas.jtests.beans.relation.pkcomp.BPK;
040: import org.objectweb.jonas.jtests.beans.relation.pkcomp.BHomeRemote;
041: import org.objectweb.jonas.jtests.beans.relation.pkcomp.BRemote;
042:
043: /**
044: * For testing many-to-many bidirectional relationships of beans with composite Primary Keys.
045: * This test is like F_Relation_mnbEC2 test.
046: * @author J. Camilleri, H. Joanin
047: *
048: * This is an advanced test suite for home interface on entity bean CMP2.
049: * Beans used: pkcomp/A1, pkcomp/A2
050: *
051: **/
052: public class F_Relation_pkcompEC2 extends A_Cmp2Util {
053:
054: static Hashtable tbRelationA2B = new Hashtable();
055:
056: static {
057: tbRelationA2B.put("a-0", new String[] {});
058: tbRelationA2B.put("a-1", new String[] { "b-1", "b-2" });
059: tbRelationA2B.put("a-2", new String[] { "b-1", "b-2", "b-3" });
060: tbRelationA2B.put("a-3", new String[] { "b-2", "b-3", "b-4" });
061: // Translate the String[] to a Collection of BPK (Primary Key of the bean B)
062: for (Enumeration ea = tbRelationA2B.keys(); ea
063: .hasMoreElements();) {
064: String aname = (String) (ea.nextElement());
065: String[] tb = (String[]) tbRelationA2B.get(aname);
066: ArrayList col = new ArrayList(tb.length);
067: for (int i = 0; i < tb.length; i++) {
068: BPK bpk = new BPK(getStringBeforeDash(tb[i]),
069: getIntAfterDash(tb[i]));
070: col.add(bpk);
071: }
072: tbRelationA2B.put(aname, col);
073: }
074: }
075:
076: static Hashtable tbRelationB2A = new Hashtable();
077:
078: static {
079: tbRelationB2A.put("b-0", new String[] {});
080: tbRelationB2A.put("b-1", new String[] { "a-1", "a-2" });
081: tbRelationB2A.put("b-2", new String[] { "a-1", "a-2", "a-3" });
082: tbRelationB2A.put("b-3", new String[] { "a-2", "a-3" });
083: tbRelationB2A.put("b-4", new String[] { "a-3" });
084: // Translate the String[] to a Collection of APK (Primary Key of the bean A)
085: for (Enumeration eb = tbRelationB2A.keys(); eb
086: .hasMoreElements();) {
087: String bname = (String) (eb.nextElement());
088: String[] tb = (String[]) tbRelationB2A.get(bname);
089: ArrayList col = new ArrayList(tb.length);
090: for (int i = 0; i < tb.length; i++) {
091: APK apk = new APK(getStringBeforeDash(tb[i]),
092: getIntAfterDash(tb[i]));
093: col.add(apk);
094: }
095: tbRelationB2A.put(bname, col);
096: }
097: }
098:
099: private static String BEAN_HOME_A = "relation_pkcomp_AHome";
100: protected static AHomeRemote ahome = null;
101: private static String BEAN_HOME_B = "relation_pkcomp_BHome";
102: protected static BHomeRemote bhome = null;
103:
104: public F_Relation_pkcompEC2(String name) {
105: super (name);
106: }
107:
108: protected boolean isInit = false;
109:
110: protected void setUp() {
111: super .setUp();
112: boolean ok = false;
113: int nbtry = 0;
114: while (!ok && nbtry < 3) {
115: if (!isInit) {
116: // load bean if not loaded yet
117: useBeans("pkcomp", false);
118: // lookup home used in the tests
119: try {
120: ahome = (AHomeRemote) PortableRemoteObject
121: .narrow(ictx.lookup(BEAN_HOME_A),
122: AHomeRemote.class);
123: bhome = (BHomeRemote) PortableRemoteObject
124: .narrow(ictx.lookup(BEAN_HOME_B),
125: BHomeRemote.class);
126: } catch (NamingException e) {
127: fail("Cannot get bean home: " + e.getMessage());
128: }
129: // check if tables have been initialized
130: try {
131: ahome.findByPrimaryKey(new APK("a", 0));
132: } catch (Exception e) {
133: try {
134: utx.begin();
135: ARemote a0 = ahome.create("a", 0);
136: ARemote a1 = ahome.create("a", 1);
137: ARemote a2 = ahome.create("a", 2);
138: ARemote a3 = ahome.create("a", 3);
139: bhome.create("b", 0);
140: bhome.create("b", 1);
141: bhome.create("b", 2);
142: bhome.create("b", 3);
143: bhome.create("b", 4);
144: a1.assignB((Collection) tbRelationA2B
145: .get("a-1"));
146: a2.assignB((Collection) tbRelationA2B
147: .get("a-2"));
148: a3.assignB((Collection) tbRelationA2B
149: .get("a-3"));
150: } catch (Exception i) {
151: fail("InitialState creation problem: " + i);
152: } finally {
153: try {
154: utx.commit();
155: } catch (Exception ii) {
156: debug("Cannot commit table creation" + ii);
157: }
158: }
159: }
160: isInit = true;
161: }
162: // Check that all is OK. Sometimes, a test has failed and has corrupted
163: // the bean state in the database. We must unload and reload the bean then.
164: nbtry++;
165: try {
166: if (initStateOK()) {
167: ok = true;
168: }
169: } catch (Exception e) {
170: debug("Cannot check initial state: " + e);
171: }
172: if (!ok) {
173: isInit = false;
174: unloadBeans("pkcomp");
175: }
176: }
177: }
178:
179: /*
180: * Check that we are in the same state as after the tables creation for thoses beans A and B
181: * (ie if it is the initial state)
182: */
183: boolean initStateOK() throws Exception {
184: boolean isOk = true;
185: msgerror = new StringBuffer();
186: // Check relations A to B
187: for (Enumeration ea = tbRelationA2B.keys(); ea
188: .hasMoreElements();) {
189: String aname = (String) (ea.nextElement());
190: APK apk = new APK(getStringBeforeDash(aname),
191: getIntAfterDash(aname));
192: ARemote a = ahome.findByPrimaryKey(apk);
193: Collection colActual = a.retrieveB();
194: ArrayList colExpected = (ArrayList) (tbRelationA2B
195: .get(aname));
196: if (!isCollectionEqual(colExpected, colActual)) {
197: isOk = false;
198: msgerror = msgerror.append("Wrong relation for "
199: + aname + " (expected:" + colExpected
200: + ", found:" + colActual + ")");
201: }
202: }
203: // Check Relations B to A
204: for (Enumeration eb = tbRelationB2A.keys(); eb
205: .hasMoreElements();) {
206: String bname = (String) (eb.nextElement());
207: BPK bpk = new BPK(getStringBeforeDash(bname),
208: getIntAfterDash(bname));
209: BRemote b = bhome.findByPrimaryKey(bpk);
210: Collection colActual1 = b.retrieveA();
211: ArrayList colExpected1 = (ArrayList) (tbRelationB2A
212: .get(bname));
213: if (!isCollectionEqual(colExpected1, colActual1)) {
214: isOk = false;
215: msgerror = msgerror.append("Wrong relation for "
216: + bname + " (expected:" + colExpected1
217: + ", found:" + colActual1 + ")");
218: }
219: }
220: return isOk;
221: }
222:
223: /**
224: * Remove an element in a relation.
225: */
226: public void _testCohRemoveInRel(int tx) throws Exception {
227: String bRemovedString = "b";
228: int bRemovedInt = 4;
229: if ((tx == TX_CALL) || (tx == TX_RB)) {
230: utx.begin();
231: }
232: ARemote a = ahome.findByPrimaryKey(new APK("a", 3));
233: if (tx == TX_CONT) {
234: a.removeFromBInNewTx(new BPK(bRemovedString, bRemovedInt));
235: } else {
236: a.removeFromB(new BPK(bRemovedString, bRemovedInt));
237: }
238: if (tx == TX_CALL) {
239: utx.commit();
240: } else if (tx == TX_RB) {
241: utx.rollback();
242: }
243: // checking
244: Collection ca = a.retrieveB();
245: BRemote b4 = bhome.findByPrimaryKey(new BPK("b", 4));
246: Collection cb4 = b4.retrieveA();
247: if (tx != TX_RB) {
248: assertEquals("Wrong relations size for a-3: ", 2, ca.size());
249: assertEquals("Wrong relations size for b-4: ", 0, cb4
250: .size());
251: } else {
252: assertEquals("Wrong relations size for a-3: ", 3, ca.size());
253: assertEquals("Wrong relations size for b-4: ", 1, cb4
254: .size());
255: }
256: // undo
257: if (tx != TX_RB) {
258: a.addInB(new BPK(bRemovedString, bRemovedInt));
259: }
260: // check to initial state
261: checkIsInitialState();
262:
263: }
264:
265: public void testCohRemoveInRelTxNo() throws Exception {
266: _testCohRemoveInRel(TX_NO);
267: }
268:
269: public void testCohRemoveInRelTxCall() throws Exception {
270: _testCohRemoveInRel(TX_CALL);
271: }
272:
273: public void testCohRemoveInRelTxCont() throws Exception {
274: _testCohRemoveInRel(TX_CONT);
275: }
276:
277: public void testCohRemoveInRelTxRb() throws Exception {
278: _testCohRemoveInRel(TX_RB);
279: }
280:
281: public static Test suite() {
282: return new TestSuite(F_Relation_pkcompEC2.class);
283: }
284:
285: public static void main(String args[]) {
286: String testtorun = null;
287: // Get args
288: for (int argn = 0; argn < args.length; argn++) {
289: String s_arg = args[argn];
290: Integer i_arg;
291: if (s_arg.equals("-n")) {
292: testtorun = args[++argn];
293: }
294: }
295: if (testtorun == null) {
296: junit.textui.TestRunner.run(suite());
297: } else {
298: junit.textui.TestRunner.run(new F_Relation_pkcompEC2(
299: testtorun));
300: }
301: }
302:
303: }
|