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