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:
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.clients.entity;
027:
028: import java.util.Enumeration;
029: import java.util.Hashtable;
030:
031: import javax.naming.NamingException;
032: import javax.rmi.PortableRemoteObject;
033:
034: import junit.framework.Test;
035: import junit.framework.TestSuite;
036:
037: import org.objectweb.jonas.jtests.beans.relation.mou.AHomeRemote;
038: import org.objectweb.jonas.jtests.beans.relation.mou.ARemote;
039: import org.objectweb.jonas.jtests.beans.relation.mou.BHomeRemote;
040:
041: /**
042: * For testing many-to-one unidirectional relationships
043: * @author S.Chassande-Barrioz, J. Camilleri
044: *
045: * This is an advanced test suite for home interface on entity bean CMP2.
046: *
047: **/
048: public class G_Relation_mouEC2 extends A_Cmp2Util {
049:
050: private static String BEAN_HOME_A = "relation_mou_AHome";
051: protected static AHomeRemote ahome = null;
052: private static String BEAN_HOME_B = "relation_mou_BHome";
053: protected static BHomeRemote bhome = null;
054:
055: static Hashtable tbRelationA2B = new Hashtable();
056:
057: static {
058: tbRelationA2B.put("a20", "b2");
059: tbRelationA2B.put("a21", "b2");
060: tbRelationA2B.put("a22", "b2");
061: tbRelationA2B.put("a0", "b0");
062: tbRelationA2B.put("a1", "b0");
063: tbRelationA2B.put("a2", "b0");
064: tbRelationA2B.put("a3", "");
065: tbRelationA2B.put("a4", "b4");
066: tbRelationA2B.put("a5", "b4");
067: }
068:
069: public G_Relation_mouEC2(String name) {
070: super (name);
071: }
072:
073: protected boolean isInit = false;
074:
075: protected void setUp() {
076: super .setUp();
077: boolean ok = false;
078: int nbtry = 0;
079: while (!ok && nbtry < 3) {
080: if (!isInit) {
081: // load bean if not loaded yet
082: useBeans("mou", false);
083: // lookup home used in the tests
084: try {
085: ahome = (AHomeRemote) PortableRemoteObject
086: .narrow(ictx.lookup(BEAN_HOME_A),
087: AHomeRemote.class);
088: bhome = (BHomeRemote) PortableRemoteObject
089: .narrow(ictx.lookup(BEAN_HOME_B),
090: BHomeRemote.class);
091: } catch (NamingException e) {
092: fail("Cannot get bean home: " + e.getMessage());
093: }
094: // check if tables have been initialized
095: try {
096: ahome.findByPrimaryKey("a0");
097: } catch (Exception e) {
098: // Make the initialization needed for the tests
099: try {
100: utx.begin();
101: ARemote a0 = ahome.create("a0");
102: ARemote a1 = ahome.create("a1");
103: ARemote a2 = ahome.create("a2");
104: ARemote a3 = ahome.create("a3");
105: ARemote a4 = ahome.create("a4");
106: ARemote a5 = ahome.create("a5");
107: ARemote a20 = ahome.create("a20");
108: ARemote a21 = ahome.create("a21");
109: ARemote a22 = ahome.create("a22");
110: bhome.create("b0");
111: bhome.create("b1");
112: bhome.create("b2");
113: bhome.create("b4");
114: // assign relations only if not null (a3)
115: a0.assignB((String) tbRelationA2B.get("a0"));
116: a1.assignB((String) tbRelationA2B.get("a1"));
117: a2.assignB((String) tbRelationA2B.get("a2"));
118: a4.assignB((String) tbRelationA2B.get("a4"));
119: a5.assignB((String) tbRelationA2B.get("a5"));
120: a20.assignB((String) tbRelationA2B.get("a20"));
121: a21.assignB((String) tbRelationA2B.get("a21"));
122: a22.assignB((String) tbRelationA2B.get("a22"));
123: } catch (Exception e2) {
124: fail("InitialState creation problem: " + e2);
125: } finally {
126: try {
127: utx.commit();
128: } catch (Exception e3) {
129: }
130: }
131: }
132: isInit = true;
133: }
134: // Check that all is OK. Sometimes, a test has failed and has corrupted
135: // the bean state in the database. We must unload and reload the bean then.
136: nbtry++;
137: try {
138: if (initStateOK()) {
139: ok = true;
140: }
141: } catch (Exception e) {
142: }
143: if (!ok) {
144: isInit = false;
145: unloadBeans("mou");
146: }
147: }
148: }
149:
150: /*
151: * Check that we are in the same state as after the tables creation for thoses beans A and B
152: * (ie if it is the initial state)
153: */
154: boolean initStateOK() throws Exception {
155: boolean isOk = true;
156: msgerror = new StringBuffer();
157: for (Enumeration ea = tbRelationA2B.keys(); ea
158: .hasMoreElements();) {
159: String aname = (String) (ea.nextElement());
160: ARemote a = ahome.findByPrimaryKey(aname);
161: String BActual = a.retrieveB();
162: // If BActual is empty then convert to "" to match with tbRelationA2B value in this case
163: if (BActual == null)
164: BActual = "";
165: String BExpected = (String) (tbRelationA2B.get(aname));
166: if (!BActual.equals(BExpected)) {
167: isOk = false;
168: msgerror = msgerror.append("Wrong relation for "
169: + aname + " (expected:" + BExpected
170: + ", found:" + BActual + ")");
171: }
172: }
173: return isOk;
174: }
175:
176: /**
177: * Check that the bean 'a3' has no relation.
178: */
179: public void _testBasicGetEmpty(int tx) throws Exception {
180: String c = null;
181: if ((tx == TX_CALL) || (tx == TX_RB)) {
182: utx.begin();
183: }
184: ARemote a = ahome.findByPrimaryKey("a3");
185: if (tx == TX_CONT) {
186: c = a.retrieveBInNewTx();
187: } else {
188: c = a.retrieveB();
189: }
190: if (tx == TX_CALL) {
191: utx.commit();
192: } else if (tx == TX_RB) {
193: utx.rollback();
194: }
195: checkIsInitialState();
196: }
197:
198: public void testBasicGetEmptyTxNo() throws Exception {
199: _testBasicGetEmpty(TX_NO);
200: }
201:
202: public void testBasicGetEmptyTxCall() throws Exception {
203: _testBasicGetEmpty(TX_CALL);
204: }
205:
206: public void testBasicGetEmptyTxCont() throws Exception {
207: _testBasicGetEmpty(TX_CONT);
208: }
209:
210: /**
211: * Ckeck the new relation a3-b1
212: */
213: public void _testBasicSetEmpty(int tx) throws Exception {
214: String c = null;
215: if ((tx == TX_CALL) || (tx == TX_RB)) {
216: utx.begin();
217: }
218: ARemote a = ahome.findByPrimaryKey("a3");
219: if (tx == TX_CONT) {
220: a.assignBInNewTx("b1");
221: } else {
222: a.assignB("b1");
223: }
224: if (tx == TX_CALL) {
225: utx.commit();
226: } else if (tx == TX_RB) {
227: utx.rollback();
228: }
229: // checking
230: String idB = a.retrieveB();
231: if (tx != TX_RB) {
232: assertEquals("Wrong new relation a3->b1 : ", "b1", idB);
233: } else {
234: assertNull("Wrong new relation a3->b1 : ", idB);
235: }
236: // undo
237: if (tx != TX_RB) {
238: a.assignB(null);
239: }
240: checkIsInitialState();
241: }
242:
243: public void testBasicSetEmptyTxNo() throws Exception {
244: _testBasicSetEmpty(TX_NO);
245: }
246:
247: public void testBasicSetEmptyTxCall() throws Exception {
248: _testBasicSetEmpty(TX_CALL);
249: }
250:
251: public void testBasicSetEmptyTxCont() throws Exception {
252: _testBasicSetEmpty(TX_CONT);
253: }
254:
255: /**
256: * Check that a1.retrieveB()=b0
257: */
258: public void _testBasicGet(int tx) throws Exception {
259: String b = null;
260: if ((tx == TX_CALL) || (tx == TX_RB)) {
261: utx.begin();
262: }
263: ARemote a1 = ahome.findByPrimaryKey("a1");
264:
265: if (tx == TX_CONT) {
266: b = a1.retrieveBInNewTx();
267: } else {
268: b = a1.retrieveB();
269: }
270: if (tx == TX_CALL) {
271: utx.commit();
272: } else if (tx == TX_RB) {
273: utx.rollback();
274: }
275: // checking
276: if (tx != TX_RB) {
277: assertEquals("Wrong relation a1->b0 : ", "b0", b);
278: }
279: checkIsInitialState();
280: }
281:
282: /**
283: * Check that a1.assignB(null) => a1.retreiveB()=null
284: */
285: public void _testBasicSetNull(int tx) throws Exception {
286: if ((tx == TX_CALL) || (tx == TX_RB)) {
287: utx.begin();
288: }
289: ARemote a1 = ahome.findByPrimaryKey("a1");
290:
291: if (tx == TX_CONT) {
292: a1.assignBInNewTx(null);
293: } else {
294: a1.assignB(null);
295: }
296: if (tx == TX_CALL) {
297: utx.commit();
298: } else if (tx == TX_RB) {
299: utx.rollback();
300: }
301: // checking
302: if (tx != TX_RB) {
303: String b = a1.retrieveB();
304: assertNull("Wrong relation a1 : ", b);
305: // Undo
306: a1.assignB("b0");
307: }
308: checkIsInitialState();
309: }
310:
311: /**
312: * See 10.3.7.5 : a1j.assignB(a2k.retrieveB()) => a2=a1j.retrieveB()
313: */
314: public void _testBasicSet(int tx) throws Exception {
315: String c = null;
316: if ((tx == TX_CALL) || (tx == TX_RB)) {
317: utx.begin();
318: }
319: ARemote a1 = ahome.findByPrimaryKey("a1");
320: ARemote a21 = ahome.findByPrimaryKey("a21");
321: if (tx == TX_CONT) {
322: a1.assignBInNewTx(a21.retrieveB());
323: } else {
324: a1.assignB(a21.retrieveB());
325: }
326: if (tx == TX_CALL) {
327: utx.commit();
328: } else if (tx == TX_RB) {
329: utx.rollback();
330: }
331: if (tx != TX_RB) {
332: // checking
333: String idB1 = a1.retrieveB();
334: ARemote a0 = ahome.findByPrimaryKey("a0");
335: ARemote a2 = ahome.findByPrimaryKey("a2");
336: ARemote a3 = ahome.findByPrimaryKey("a3");
337: ARemote a20 = ahome.findByPrimaryKey("a20");
338: ARemote a22 = ahome.findByPrimaryKey("a22");
339: String idB0 = a0.retrieveB();
340: String idB2 = a2.retrieveB();
341: String idB3 = a3.retrieveB();
342: String idB20 = a20.retrieveB();
343: String idB21 = a21.retrieveB();
344: String idB22 = a22.retrieveB();
345: assertEquals("Wrong new relation a1->b2 : ", "b2", idB1);
346: assertEquals("Wrong new relation a0->b0 : ", "b0", idB0);
347: assertEquals("Wrong new relation a2->b0 : ", "b0", idB2);
348: assertEquals("Wrong new relation a20->b2 : ", "b2", idB20);
349: assertEquals("Wrong new relation a21->b2 : ", "b2", idB21);
350: assertEquals("Wrong new relation a22->b2 : ", "b2", idB22);
351: // undo
352: a1.assignB("b0");
353: }
354: checkIsInitialState();
355: }
356:
357: /**
358: * Test of coherence a0.remove()
359: */
360: public void _testCohRemoveA(int tx) throws Exception {
361: if (tx == TX_CONT) {
362: // The transaction attribute of the remove method is TX_SUPPORT,
363: // so the transaction cannot be initiate by the container
364: fail("Transaction cannot be initiate by the container for this test");
365: }
366: if ((tx == TX_CALL) || (tx == TX_RB)) {
367: utx.begin();
368: }
369: ahome.remove("a0");
370: if (tx == TX_CALL) {
371: utx.commit();
372: } else if (tx == TX_RB) {
373: utx.rollback();
374: }
375: // undo
376: if (tx != TX_RB) {
377: ARemote a0 = ahome.create("a0");
378: a0.assignBInNewTx("b0");
379: }
380: checkIsInitialState();
381: }
382:
383: public static Test suite() {
384: return new TestSuite(G_Relation_mouEC2.class);
385: }
386:
387: public static void main(String args[]) {
388: String testtorun = null;
389: // Get args
390: for (int argn = 0; argn < args.length; argn++) {
391: String s_arg = args[argn];
392: Integer i_arg;
393: if (s_arg.equals("-n")) {
394: testtorun = args[++argn];
395: }
396: }
397: if (testtorun == null) {
398: junit.textui.TestRunner.run(suite());
399: } else {
400: junit.textui.TestRunner
401: .run(new G_Relation_mouEC2(testtorun));
402: }
403: }
404:
405: }
|