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_s3pkcompEC2.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 java.util.Iterator;
033:
034: import javax.naming.NamingException;
035: import javax.rmi.PortableRemoteObject;
036:
037: import junit.framework.Test;
038: import junit.framework.TestSuite;
039:
040: import org.objectweb.jonas.jtests.beans.relation.s3pkcomp.AHomeRemote;
041: import org.objectweb.jonas.jtests.beans.relation.s3pkcomp.ARemote;
042: import org.objectweb.jonas.jtests.beans.relation.s3pkcomp.BHomeRemote;
043: import org.objectweb.jonas.jtests.beans.relation.s3pkcomp.Pk;
044:
045: /**
046: * For testing one-to-many unidirectional relationships
047: * this test uses beans s3pkcomp/*
048: * @author Helene Joanin
049: */
050: public class F_Relation_s3pkcompEC2 extends A_Cmp2Util {
051:
052: private static String BEAN_HOME_A = "relation_s3pkcomp_AHome";
053: protected static AHomeRemote ahome = null;
054: private static String BEAN_HOME_B = "relation_s3pkcomp_BHome";
055: protected static BHomeRemote bhome = null;
056:
057: static Hashtable tbRelationA2B = new Hashtable();
058:
059: static {
060: tbRelationA2B.put(new Integer(0), new int[] {});
061: tbRelationA2B.put(new Integer(1),
062: new int[] { 1001, 1002, 1003 });
063: tbRelationA2B.put(new Integer(2),
064: new int[] { 2001, 2002, 2003 });
065: tbRelationA2B.put(new Integer(3), new int[] { 3001 });
066: // Translate the int[] to a Collection of Pk
067: for (Enumeration ea = tbRelationA2B.keys(); ea
068: .hasMoreElements();) {
069: Integer aid = (Integer) (ea.nextElement());
070: int[] tb = (int[]) tbRelationA2B.get(aid);
071: ArrayList col = new ArrayList(tb.length);
072: for (int i = 0; i < tb.length; i++) {
073: col.add(new Pk(tb[i]));
074: }
075: tbRelationA2B.put(aid, col);
076: }
077: }
078:
079: public F_Relation_s3pkcompEC2(String name) {
080: super (name);
081: debug("F_Relation_s3pkcompEC2(" + name + ")");
082: }
083:
084: protected boolean isInit = false;
085:
086: protected void setUp() {
087: super .setUp();
088: boolean ok = false;
089: int nbtry = 0;
090: while (!ok && nbtry < 3) {
091: if (!isInit) {
092: // load bean if not loaded yet
093: useBeans("s3pkcomp", false);
094: try {
095: ahome = (AHomeRemote) PortableRemoteObject
096: .narrow(ictx.lookup(BEAN_HOME_A),
097: AHomeRemote.class);
098: bhome = (BHomeRemote) PortableRemoteObject
099: .narrow(ictx.lookup(BEAN_HOME_B),
100: BHomeRemote.class);
101: } catch (NamingException e) {
102: fail("Cannot get bean home: " + e.getMessage());
103: }
104: // check if tables have been initialized
105: try {
106: ahome.findByPrimaryKey(new Pk(0));
107: } catch (Exception e) {
108: try {
109: utx.begin();
110: bhome.create(0);
111: bhome.create(1001);
112: bhome.create(1002);
113: bhome.create(1003);
114: bhome.create(2001);
115: bhome.create(2002);
116: bhome.create(2003);
117: bhome.create(3001);
118: ARemote a0 = ahome.create(0);
119: ARemote a1 = ahome.create(1);
120: ARemote a2 = ahome.create(2);
121: ARemote a3 = ahome.create(3);
122: a1.assignB((Collection) tbRelationA2B
123: .get(new Integer(1)));
124: a2.assignB((Collection) tbRelationA2B
125: .get(new Integer(2)));
126: a3.assignB((Collection) tbRelationA2B
127: .get(new Integer(3)));
128: } catch (Exception i) {
129: fail("InitialState creation problem:: " + i);
130: } finally {
131: try {
132: utx.commit();
133: } catch (Exception ii) {
134: }
135: }
136: }
137: isInit = true;
138: }
139: // Check that all is OK. Sometimes, a test has failed and has corrupted
140: // the bean state in the database. We must unload and reload the bean then.
141: nbtry++;
142: try {
143: if (initStateOK()) {
144: ok = true;
145: }
146: } catch (Exception e) {
147: }
148: if (!ok) {
149: isInit = false;
150: unloadBeans("s3pkcomp");
151: }
152: }
153: }
154:
155: /*
156: * Check that we are in the same state as after the tables creation for thoses beans A and B
157: * (ie if it is the initial state)
158: */
159: boolean initStateOK() throws Exception {
160: boolean isOk = true;
161: msgerror = new StringBuffer();
162: for (Enumeration ea = tbRelationA2B.keys(); ea
163: .hasMoreElements();) {
164: Integer aid = (Integer) (ea.nextElement());
165: ARemote a = ahome.findByPrimaryKey(new Pk(aid));
166: Collection colActual = a.retrieveB();
167: ArrayList colExpected = (ArrayList) (tbRelationA2B.get(aid));
168: debug("Relation for " + aid + " : expected:" + colExpected
169: + ", found:" + colActual);
170: if (!isCollectionEqual(colExpected, colActual)) {
171: isOk = false;
172: msgerror = msgerror.append("Wrong relation for " + aid
173: + " (expected:" + colExpected + ", found:"
174: + colActual + ")");
175: }
176: }
177: return isOk;
178: }
179:
180: /**
181: * Not really a test, just to verify the checkIsInitialState() method.
182: */
183: public void testEmpty() throws Exception {
184: checkIsInitialState();
185: }
186:
187: /**
188: * Test the calling of the ahome.findAll() method
189: */
190: public void _testFindAllA(int tx) throws Exception {
191: if (tx == TX_CONT) {
192: // The transaction attribute of the findAll method is TX_SUPPORT,
193: // so the transaction cannot be initiate by the container
194: fail("Transaction cannot be initiate by the container for this test");
195: }
196: if ((tx == TX_CALL) || (tx == TX_RB)) {
197: utx.begin();
198: }
199: Collection cA = ahome.findAll();
200: if (tx == TX_CALL) {
201: utx.commit();
202: } else if (tx == TX_RB) {
203: utx.rollback();
204: }
205: // Check
206: int nbA = 0;
207: for (Iterator iA = cA.iterator(); iA.hasNext(); iA.next()) {
208: nbA++;
209: }
210: assertEquals("Wrong number for beans A for findAll: ",
211: tbRelationA2B.size(), nbA);
212: }
213:
214: public void testFindAllATxNo() throws Exception {
215: _testFindAllA(TX_NO);
216: }
217:
218: public void testFindAllATxCall() throws Exception {
219: _testFindAllA(TX_CALL);
220: }
221:
222: public void testFindAllATxRb() throws Exception {
223: _testFindAllA(TX_RB);
224: }
225:
226: public static Test suite() {
227: return new TestSuite(F_Relation_s3pkcompEC2.class);
228: }
229:
230: public static void main(String args[]) {
231: String testtorun = null;
232: // Get args
233: for (int argn = 0; argn < args.length; argn++) {
234: String s_arg = args[argn];
235: Integer i_arg;
236: if (s_arg.equals("-n")) {
237: testtorun = args[++argn];
238: }
239: }
240: if (testtorun == null) {
241: junit.textui.TestRunner.run(suite());
242: } else {
243: junit.textui.TestRunner.run(new F_Relation_s3pkcompEC2(
244: testtorun));
245: }
246: }
247:
248: }
|