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_mnuEC2.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.mnu.AHomeRemote;
040: import org.objectweb.jonas.jtests.beans.relation.mnu.ARemote;
041: import org.objectweb.jonas.jtests.beans.relation.mnu.BHomeRemote;
042: import org.objectweb.jonas.jtests.beans.relation.mnu.BRemote;
043:
044: /**
045: * For testing many-to-many unidirectional relationships
046: * @author S.Chassande-Barrioz, J. Camilleri
047: *
048: **/
049: public class G_Relation_mnuEC2 extends A_Cmp2Util {
050:
051: static Hashtable tbRelationA2B = new Hashtable();
052: static {
053: tbRelationA2B.put("a0", new String[] {});
054: tbRelationA2B.put("a1", new String[] { "b1", "b2" });
055: tbRelationA2B.put("a2", new String[] { "b1", "b2", "b3" });
056: tbRelationA2B.put("a3", new String[] { "b2", "b3", "b4" });
057: // Translate the String[] to a Collection of String
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: col.add(tb[i]);
065: }
066: tbRelationA2B.put(aname, col);
067: }
068: }
069:
070: private static String BEAN_HOME_A = "relation_mnu_AHome";
071: protected static AHomeRemote ahome = null;
072: private static String BEAN_HOME_B = "relation_mnu_BHome";
073: protected static BHomeRemote bhome = null;
074:
075: public G_Relation_mnuEC2(String name) {
076: super (name);
077: }
078:
079: protected boolean isInit = false;
080:
081: protected void setUp() {
082: super .setUp();
083: boolean ok = false;
084: int nbtry = 0;
085: while (!ok && nbtry < 3) {
086: if (!isInit) {
087: // load bean if not loaded yet
088: useBeans("mnu", false);
089: try {
090: ahome = (AHomeRemote) PortableRemoteObject
091: .narrow(ictx.lookup(BEAN_HOME_A),
092: AHomeRemote.class);
093: bhome = (BHomeRemote) PortableRemoteObject
094: .narrow(ictx.lookup(BEAN_HOME_B),
095: BHomeRemote.class);
096: } catch (NamingException e) {
097: fail("Cannot get bean home: " + e.getMessage());
098: }
099: // check if tables have been initialized
100: try {
101: ahome.findByPrimaryKey("a0");
102: } catch (Exception e) {
103: try {
104: utx.begin();
105: ARemote a0 = ahome.create("a0");
106: ARemote a1 = ahome.create("a1");
107: ARemote a2 = ahome.create("a2");
108: ARemote a3 = ahome.create("a3");
109: bhome.create("b0");
110: bhome.create("b1");
111: bhome.create("b2");
112: bhome.create("b3");
113: bhome.create("b4");
114: a0
115: .assignB((Collection) tbRelationA2B
116: .get("a0"));
117: a1
118: .assignB((Collection) tbRelationA2B
119: .get("a1"));
120: a2
121: .assignB((Collection) tbRelationA2B
122: .get("a2"));
123: a3
124: .assignB((Collection) tbRelationA2B
125: .get("a3"));
126: } catch (Exception i) {
127: fail("InitialState creation problem: " + i);
128: } finally {
129: try {
130: utx.commit();
131: } catch (Exception ii) {
132: }
133: }
134: }
135: isInit = true;
136: }
137: // Check that all is OK. Sometimes, a test has failed and has corrupted
138: // the bean state in the database. We must unload and reload the bean then.
139: nbtry++;
140: try {
141: if (initStateOK()) {
142: ok = true;
143: }
144: } catch (Exception e) {
145: }
146: if (!ok) {
147: isInit = false;
148: unloadBeans("mnu");
149: }
150: }
151: }
152:
153: /*
154: * Check that we are in the same state as after the tables creation for thoses beans A and B
155: * (ie if it is the initial state)
156: */
157: boolean initStateOK() throws Exception {
158: boolean isOk = true;
159: msgerror = new StringBuffer();
160: for (Enumeration ea = tbRelationA2B.keys(); ea
161: .hasMoreElements();) {
162: String aname = (String) (ea.nextElement());
163: ARemote a = ahome.findByPrimaryKey(aname);
164: Collection colActual = a.retrieveB();
165: ArrayList colExpected = (ArrayList) (tbRelationA2B
166: .get(aname));
167: if (!isCollectionEqual(colExpected, colActual)) {
168: isOk = false;
169: msgerror = msgerror.append("Wrong relation for "
170: + aname + " (expected:" + colExpected
171: + ", found:" + colActual + ")");
172: }
173: }
174: return isOk;
175: }
176:
177: /**
178: * Check that the bean 'a3' has no relation.
179: */
180: public void _testBasicGetEmpty(int tx) throws Exception {
181: Collection c;
182: if ((tx == TX_CALL) || (tx == TX_RB)) {
183: utx.begin();
184: }
185: ARemote a = ahome.findByPrimaryKey("a0");
186: if (tx == TX_CONT) {
187: c = a.retrieveBInNewTx();
188: } else {
189: c = a.retrieveB();
190: }
191: if (tx == TX_CALL) {
192: utx.commit();
193: } else if (tx == TX_RB) {
194: utx.rollback();
195: }
196:
197: checkIsInitialState();
198: }
199:
200: public void testBasicGetEmptyTxNo() throws Exception {
201: _testBasicGetEmpty(TX_NO);
202: }
203:
204: public void testBasicGetEmptyTxCall() throws Exception {
205: _testBasicGetEmpty(TX_CALL);
206: }
207:
208: public void testBasicGetEmptyTxCont() throws Exception {
209: _testBasicGetEmpty(TX_CONT);
210: }
211:
212: /**
213: * Ckeck the new relation a0-b0
214: */
215: public void _testBasicSetEmpty(int tx) throws Exception {
216: ArrayList c = new ArrayList(1);
217: c.add("b0");
218: if ((tx == TX_CALL) || (tx == TX_RB)) {
219: utx.begin();
220: }
221: ARemote a = ahome.findByPrimaryKey("a0");
222: if (tx == TX_CONT) {
223: a.assignBInNewTx(c);
224: } else {
225: a.assignB(c);
226: }
227: if (tx == TX_CALL) {
228: utx.commit();
229: } else if (tx == TX_RB) {
230: utx.rollback();
231: }
232: // checking
233: Collection c_value = a.retrieveB();
234: if (tx != TX_RB) {
235: assertTrue("Wrong relations a0 (required:" + c + ", found:"
236: + c_value + ")", isCollectionEqual(c_value, c));
237: // undo
238: a.assignB(new ArrayList(0));
239: }
240: checkIsInitialState();
241:
242: }
243:
244: public void testBasicSetEmptyTxNo() throws Exception {
245: _testBasicSetEmpty(TX_NO);
246: }
247:
248: public void testBasicSetEmptyTxCall() throws Exception {
249: _testBasicSetEmpty(TX_CALL);
250: }
251:
252: public void testBasicSetEmptyTxCont() throws Exception {
253: _testBasicSetEmpty(TX_CONT);
254: }
255:
256: /**
257: * See 10.3.7.7 of spec : a1.assignB(a3.retrieveB()) =>
258: * a1.retreiveB().contains("b2") &&
259: * a1.retreiveB().contains("b3") &&
260: * a1.retreiveB().contains("b4") &&
261: * a3.retreiveB().contains("b2") &&
262: * a3.retreiveB().contains("b3") &&
263: * a3.retreiveB().contains("b4") &&
264: */
265: public void _testBasicSetFirst(int tx) throws Exception {
266: String c = null;
267: if ((tx == TX_CALL) || (tx == TX_RB)) {
268: utx.begin();
269: }
270: ARemote a1 = ahome.findByPrimaryKey("a1");
271: ARemote a3 = ahome.findByPrimaryKey("a3");
272: Collection c1 = a1.retrieveB();
273: if (tx == TX_CONT) {
274: a1.assignBInNewTx(a3.retrieveB());
275: } else {
276: a1.assignB(a3.retrieveB());
277: }
278: if (tx == TX_CALL) {
279: utx.commit();
280: } else if (tx == TX_RB) {
281: utx.rollback();
282: }
283: // checking
284: if (tx != TX_RB) {
285: ArrayList c1_att = new ArrayList(3);
286: c1_att.add("b2");
287: c1_att.add("b3");
288: c1_att.add("b4");
289: assertTrue("Wrong test of coherence First before commit: ",
290: isCollectionEqual(a1.retrieveB(), c1_att));
291: } else {
292: assertTrue(
293: "Wrong test of coherence First before roolback: ",
294: isCollectionEqual(a1.retrieveB(), c1));
295: }
296: // undo
297: if (tx != TX_RB) {
298: a1.assignB(null);
299: a1.assignB(c1);
300: }
301: checkIsInitialState();
302: }
303:
304: /**
305: * See 10.3.7.7 of spec : a1.getB().add(b3) =>
306: * a1.retreiveB().contains("b1") &&
307: * a1.retreiveB().contains("b2") &&
308: * a1.retreiveB().contains("b3") &&
309: */
310: public void _testBasicSetSecond(int tx) throws Exception {
311: String c = null;
312:
313: if ((tx == TX_CALL) || (tx == TX_RB)) {
314: utx.begin();
315: }
316: ARemote a1 = ahome.findByPrimaryKey("a1");
317: Collection c1 = a1.retrieveB();
318:
319: if (tx == TX_CONT) {
320: a1.addInB("b3");
321: } else {
322: a1.addInB("b3");
323: }
324: if (tx == TX_CALL) {
325: utx.commit();
326: } else if (tx == TX_RB) {
327: utx.rollback();
328: }
329: // checking
330: if (tx != TX_RB) {
331: ArrayList c1_att = new ArrayList(3);
332: c1_att.add("b1");
333: c1_att.add("b2");
334: c1_att.add("b3");
335: assertTrue(
336: "Wrong test of coherence Second before commit: ",
337: isCollectionEqual(a1.retrieveB(), c1_att));
338: } else {
339: assertTrue(
340: "Wrong test of coherence Second before roolback: ",
341: isCollectionEqual(a1.retrieveB(), c1));
342: }
343: // undo
344: if (tx != TX_RB) {
345: a1.assignB(null);
346: a1.assignB(c1);
347: }
348: checkIsInitialState();
349: }
350:
351: /**
352: * See 10.3.7.7 of spec : a2.getB().remove(b2) =>
353: * a2.retreiveB().contains("b1") &&
354: * a2.retreiveB().contains("b3") &&
355: */
356: public void _testBasicSetRemove(int tx) throws Exception {
357: String c = null;
358:
359: if ((tx == TX_CALL) || (tx == TX_RB)) {
360: utx.begin();
361: }
362: ARemote a2 = ahome.findByPrimaryKey("a2");
363: Collection c2 = a2.retrieveB();
364: if (tx == TX_CONT) {
365: a2.removeFromB("b2");
366: } else {
367: a2.removeFromB("b2");
368: }
369: if (tx == TX_CALL) {
370: utx.commit();
371: } else if (tx == TX_RB) {
372: utx.rollback();
373: }
374: // checking
375: if (tx != TX_RB) {
376: ArrayList c2_att = new ArrayList(3);
377: c2_att.add("b1");
378: c2_att.add("b3");
379: assertTrue(
380: "Wrong test of coherence Remove before commit: ",
381: isCollectionEqual(a2.retrieveB(), c2_att));
382: } else {
383: assertTrue(
384: "Wrong test of coherence Remove before roolback: ",
385: isCollectionEqual(a2.retrieveB(), c2));
386: }
387: // undo
388: if (tx != TX_RB) {
389: a2.assignB(c2);
390: }
391: checkIsInitialState();
392: }
393:
394: /**
395: * Ckeck clear function
396: */
397: public void _testBasicClear(int tx) throws Exception {
398: if ((tx == TX_CALL) || (tx == TX_RB)) {
399: utx.begin();
400: }
401: ARemote a2 = ahome.findByPrimaryKey("a2");
402: if (tx == TX_CONT) {
403: a2.clearBInNewTx();
404: } else {
405: a2.clearB();
406: }
407: if (tx == TX_CALL) {
408: utx.commit();
409: } else if (tx == TX_RB) {
410: utx.rollback();
411: }
412: // checking
413: Collection a2_value = a2.retrieveB();
414: BRemote b1 = bhome.findByPrimaryKey("b1");
415: BRemote b2 = bhome.findByPrimaryKey("b2");
416: BRemote b3 = bhome.findByPrimaryKey("b3");
417: ArrayList collb1 = new ArrayList(1);
418: collb1.add("a1");
419: ArrayList collb2 = new ArrayList(2);
420: collb2.add("a1");
421: collb2.add("a3");
422: ArrayList collb3 = new ArrayList(1);
423: collb3.add("a3");
424: Collection col = null;
425: if (tx != TX_RB) {
426: assertTrue("Wrong relations a2 (required: Empty "
427: + ", found:" + a2_value + ")", a2_value.isEmpty());
428:
429: // undo
430: ArrayList a2undo = new ArrayList(3);
431: a2undo.add("b1");
432: a2undo.add("b2");
433: a2undo.add("b3");
434: a2.assignB(a2undo);
435: }
436: checkIsInitialState();
437: }
438:
439: /**
440: * Remove an element in a relation.
441: */
442: public void _testCohRemoveInRel(int tx) throws Exception {
443: String bRemoved = "b4";
444: if ((tx == TX_CALL) || (tx == TX_RB)) {
445: utx.begin();
446: }
447: ARemote a = ahome.findByPrimaryKey("a3");
448: if (tx == TX_CONT) {
449: a.removeFromBInNewTx(bRemoved);
450: } else {
451: a.removeFromB(bRemoved);
452: }
453: if (tx == TX_CALL) {
454: utx.commit();
455: } else if (tx == TX_RB) {
456: utx.rollback();
457: }
458: // checking
459: Collection ca = a.retrieveB();
460: if (tx != TX_RB) {
461: assertEquals("Wrong relations size for a3: ", 2, ca.size());
462: } else {
463: assertEquals("Wrong relations size for a3: ", 3, ca.size());
464: }
465: // undo
466: if (tx != TX_RB) {
467: a.addInB(bRemoved);
468: }
469: // check to initial state
470: checkIsInitialState();
471:
472: }
473:
474: public static Test suite() {
475: return new TestSuite(G_Relation_mnuEC2.class);
476: }
477:
478: public static void main(String args[]) {
479: String testtorun = null;
480: // Get args
481: for (int argn = 0; argn < args.length; argn++) {
482: String s_arg = args[argn];
483: Integer i_arg;
484: if (s_arg.equals("-n")) {
485: testtorun = args[++argn];
486: }
487: }
488: if (testtorun == null) {
489: junit.textui.TestRunner.run(suite());
490: } else {
491: junit.textui.TestRunner
492: .run(new G_Relation_mnuEC2(testtorun));
493: }
494: }
495:
496: }
|