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_s2pkcompEC2.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.Collection;
029: import java.util.Enumeration;
030: import java.util.Hashtable;
031:
032: import javax.ejb.ObjectNotFoundException;
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.s2pkcomp.AHomeRemote;
040: import org.objectweb.jonas.jtests.beans.relation.s2pkcomp.ARemote;
041: import org.objectweb.jonas.jtests.beans.relation.s2pkcomp.BHomeRemote;
042: import org.objectweb.jonas.jtests.beans.relation.s2pkcomp.BRemote;
043: import org.objectweb.jonas.jtests.beans.relation.s2pkcomp.Pk;
044:
045: /**
046: * This is an advanced test suite for home interface on entity bean CMP2.
047: * Test about beans with mono-valued relation and with composite Pk with same fields names.
048: * Beans used: s2pkcomp
049: * @author S.Chassande-Barrioz
050: */
051: public class F_Relation_s2pkcompEC2 extends A_Cmp2Util {
052:
053: private static String BEAN_HOME_A = "relation_s2pkcomp_AHome";
054: private static String BEAN_HOME_B = "relation_s2pkcomp_BHome";
055: protected static AHomeRemote ahome = null;
056: protected static BHomeRemote bhome = null;
057:
058: static Hashtable a2b = new Hashtable();
059: static Hashtable b2a = new Hashtable();
060: static Pk pkNull = new Pk("null", 0);
061:
062: static {
063: a2b.put(new Pk("a", 1), new Pk("b", 1));
064: a2b.put(new Pk("a", 2), new Pk("b", 2));
065: a2b.put(new Pk("a", 3), pkNull);
066: b2a.put(new Pk("b", 1), new Pk("a", 1));
067: b2a.put(new Pk("b", 2), new Pk("a", 2));
068: b2a.put(new Pk("b", 3), pkNull);
069: }
070:
071: public F_Relation_s2pkcompEC2(String name) {
072: super (name);
073: }
074:
075: protected boolean isInit = false;
076:
077: protected void setUp() {
078: super .setUp();
079: boolean ok = false;
080: int nbtry = 0;
081: while (!ok && nbtry < 3) {
082: if (!isInit) {
083: // load bean if not loaded yet
084: useBeans("s2pkcomp", false);
085: // lookup home used in the tests
086: try {
087: ahome = (AHomeRemote) PortableRemoteObject
088: .narrow(ictx.lookup(BEAN_HOME_A),
089: AHomeRemote.class);
090: bhome = (BHomeRemote) PortableRemoteObject
091: .narrow(ictx.lookup(BEAN_HOME_B),
092: BHomeRemote.class);
093: } catch (NamingException e) {
094: fail("Cannot get bean home: " + e.getMessage());
095: }
096: // check if tables have been initialized
097: try {
098: ahome.findByPrimaryKey(new Pk("a", 2));
099: } catch (Exception e) {
100: // Make the initialization needed for the tests
101: try {
102: utx.begin();
103: ARemote a1 = ahome.create("a", 1);
104: ARemote a2 = ahome.create("a", 2);
105: ahome.create("a", 3);
106: bhome.create("b", 1);
107: bhome.create("b", 2);
108: bhome.create("b", 3);
109: a1.assignB(new Pk("b", 1));
110: a2.assignB(new Pk("b", 2));
111: } catch (Exception i) {
112: fail("InitialState creation problem: " + i);
113: } finally {
114: try {
115: utx.commit();
116: } catch (Exception ii) {
117: }
118: }
119: }
120: isInit = true;
121: }
122: // Check that all is OK. Sometimes, a test has failed and has corrupted
123: // the bean state in the database. We must unload and reload the bean then.
124: nbtry++;
125: try {
126: if (initStateOK()) {
127: ok = true;
128: }
129: } catch (Exception e) {
130: }
131: if (!ok) {
132: isInit = false;
133: unloadBeans("s2pkcomp");
134: }
135: }
136: }
137:
138: /*
139: * Check that we are in the same state as after the tables creation for thoses beans A and B
140: * (ie if it is the initial state)
141: */
142: boolean initStateOK() throws Exception {
143: boolean isOk = true;
144: msgerror = new StringBuffer();
145: // Check relations A to B
146: for (Enumeration ea = a2b.keys(); ea.hasMoreElements();) {
147: Pk apk = (Pk) (ea.nextElement());
148: ARemote a = ahome.findByPrimaryKey(apk);
149: Pk bpkActual = a.retrieveB();
150: Pk bpkExpected = (Pk) (a2b.get(apk));
151: if (pkNull.equals(bpkExpected)) {
152: if (bpkActual != null) {
153: isOk = false;
154: msgerror.append("\nWrong relation for "
155: + apk.toString() + " (expected: null"
156: + ", found:" + bpkActual.toString() + ")");
157: }
158: } else {
159: if (!bpkExpected.equals(bpkActual)) {
160: isOk = false;
161: msgerror.append("\nWrong relation for "
162: + apk.toString() + " (expected:"
163: + bpkExpected.toString() + ", found:"
164: + bpkActual.toString() + ")");
165: }
166: }
167: }
168: // Check relations B to A
169: for (Enumeration eb = b2a.keys(); eb.hasMoreElements();) {
170: Pk bpk = (Pk) (eb.nextElement());
171: BRemote b = bhome.findByPrimaryKey(bpk);
172: Pk apkActual = b.retrieveA();
173: Pk apkExpected = (Pk) (b2a.get(bpk));
174: if (pkNull.equals(apkExpected)) {
175: if (apkActual != null) {
176: isOk = false;
177: msgerror.append("\nWrong relation for "
178: + bpk.toString() + " (expected: null"
179: + ", found:" + apkActual.toString() + ")");
180: }
181: } else {
182: if (!apkExpected.equals(apkActual)) {
183: isOk = false;
184: msgerror.append("\nWrong relation for "
185: + bpk.toString() + " (expected:"
186: + apkExpected.toString() + ", found:"
187: + apkActual.toString() + ")");
188: }
189: }
190: }
191: return isOk;
192: }
193:
194: /**
195: * test coherence relation one to one bidirectionnel,
196: * A1.remove=>A1removed && B1.retreiveA()==null
197: */
198: public void _testCohRemoveA(int tx) throws Exception {
199: if (tx == TX_CONT) {
200: // The transaction attribute of the remove method is TX_SUPPORT,
201: // so the transaction cannot be initiate by the container
202: fail("Transaction cannot be initiate by the container for this test");
203: }
204: if ((tx == TX_CALL) || (tx == TX_RB)) {
205: utx.begin();
206: }
207: BRemote b1 = bhome.findByPrimaryKey(new Pk("b", 1));
208: // remove the bean
209: ahome.remove(new Pk("a", 1));
210: if (tx == TX_CALL) {
211: utx.commit();
212: } else if (tx == TX_RB) {
213: utx.rollback();
214: }
215: if (tx != TX_RB) {
216: // Verify expected result
217: assertNull(
218: "Bad coherence of relation : null expected for b1.retreiveB() found :"
219: + b1.retrieveA(), b1.retrieveA());
220: boolean not_found = false;
221: try {
222: ARemote a1 = ahome.findByPrimaryKey(new Pk("a", 1));
223: } catch (ObjectNotFoundException e) {
224: not_found = true;
225: }
226: assertTrue("a-1 is not removed", not_found);
227: // undo
228: ahome.create("a", 1);
229: ARemote a1 = ahome.findByPrimaryKey(new Pk("a", 1));
230: a1.assignB(new Pk("b", 1));
231: }
232: checkIsInitialState();
233: }
234:
235: public void testCohRemoveATxNo() throws Exception {
236: _testCohRemoveA(TX_NO);
237: }
238:
239: public void testCohRemoveATxCall() throws Exception {
240: _testCohRemoveA(TX_CALL);
241: }
242:
243: public void testCohRemoveATxRb() throws Exception {
244: _testCohRemoveA(TX_RB);
245: }
246:
247: /**
248: * test coherence relation one to one bidirectionnel,
249: * B1.remove=>B1 removed && A1.retreiveB()==null
250: * Same as _testCohRemoveB except that the called remove method is on the bean
251: * instead of the home.
252: */
253: public void _testCohBeanRemoveB(int tx) throws Exception {
254: if (tx == TX_CONT) {
255: // The transaction attribute of the remove method is TX_SUPPORT,
256: // so the transaction cannot be initiate by the container
257: fail("Transaction cannot be initiate by the container for this test");
258: }
259: if ((tx == TX_CALL) || (tx == TX_RB)) {
260: utx.begin();
261: }
262: ARemote a1 = ahome.findByPrimaryKey(new Pk("a", 1));
263: BRemote b1 = bhome.findByPrimaryKey(new Pk("b", 1));
264: // change the relation
265: b1.remove();
266: if (tx == TX_CALL) {
267: utx.commit();
268: } else if (tx == TX_RB) {
269: utx.rollback();
270: }
271: if (tx != TX_RB) {
272: // Verify expected result
273: assertNull(
274: "Bad coherence of relation : null expected for a1.retreiveB() found :"
275: + a1.retrieveB(), a1.retrieveB());
276: boolean not_found = false;
277: try {
278: b1 = bhome.findByPrimaryKey(new Pk("b", 1));
279: } catch (ObjectNotFoundException e) {
280: not_found = true;
281: }
282: assertTrue("b-1 is not removed", not_found);
283: // undo
284: bhome.create("b", 1);
285: a1.assignB(new Pk("b", 1));
286: }
287: checkIsInitialState();
288: }
289:
290: public void testCohBeanRemoveBTxNo() throws Exception {
291: _testCohBeanRemoveB(TX_NO);
292: }
293:
294: public void testCohBeanRemoveBTxCall() throws Exception {
295: _testCohBeanRemoveB(TX_CALL);
296: }
297:
298: public void testCohBeanRemoveBTxRb() throws Exception {
299: _testCohBeanRemoveB(TX_RB);
300: }
301:
302: /**
303: * test a finder method.
304: * (bug #300612)
305: */
306: public void _testFindByBIsNull(int tx) throws Exception {
307: if ((tx == TX_CALL) || (tx == TX_RB)) {
308: utx.begin();
309: }
310: Collection ca = ahome.findByBIsNull();
311: if (tx == TX_CALL) {
312: utx.commit();
313: } else if (tx == TX_RB) {
314: utx.rollback();
315: }
316: // Verify expected result
317: assertEquals("Bad result for findByBIsNull: ", 1, ca.size());
318: }
319:
320: public void testFindByBIsNullTxNo() throws Exception {
321: _testFindByBIsNull(TX_NO);
322: }
323:
324: // temporaly commented
325: public void _testFindByBIsNullTxCall() throws Exception {
326: _testFindByBIsNull(TX_CALL);
327: }
328:
329: // temporaly commented
330: public void _testFindByBIsNullTxRb() throws Exception {
331: _testFindByBIsNull(TX_RB);
332: }
333:
334: /**
335: * test a finder method.
336: */
337: public void _testFindAById1Id2(int tx) throws Exception {
338: if ((tx == TX_CALL) || (tx == TX_RB)) {
339: utx.begin();
340: }
341: ARemote a1 = ahome.findById1Id2("a", 1);
342: if (tx == TX_CALL) {
343: utx.commit();
344: } else if (tx == TX_RB) {
345: utx.rollback();
346: }
347: // Verify expected result
348: Pk pkA1 = a1.getId();
349: assertEquals("Bad id1 of a1: ", "a", pkA1.id1);
350: assertEquals("Bad id2 of a1: ", 1, pkA1.id2);
351: }
352:
353: public void testFindAById1Id2TxNo() throws Exception {
354: _testFindAById1Id2(TX_NO);
355: }
356:
357: public void testFindAById1Id2TxCall() throws Exception {
358: _testFindAById1Id2(TX_CALL);
359: }
360:
361: public void testFindAById1Id2TxRb() throws Exception {
362: _testFindAById1Id2(TX_RB);
363: }
364:
365: public static Test suite() {
366: return new TestSuite(F_Relation_s2pkcompEC2.class);
367: }
368:
369: public static void main(String args[]) {
370: String testtorun = null;
371: // Get args
372: for (int argn = 0; argn < args.length; argn++) {
373: String s_arg = args[argn];
374: Integer i_arg;
375: if (s_arg.equals("-n")) {
376: testtorun = args[++argn];
377: }
378: }
379: if (testtorun == null) {
380: junit.textui.TestRunner.run(suite());
381: } else {
382: junit.textui.TestRunner.run(new F_Relation_s2pkcompEC2(
383: testtorun));
384: }
385: }
386: }
|