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_Relation2_mouEC2.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.Iterator;
030: import javax.naming.NamingException;
031: import javax.rmi.PortableRemoteObject;
032: import junit.framework.Test;
033: import junit.framework.TestSuite;
034: import org.objectweb.jonas.jtests.beans.relation.mou.AHomeRemote;
035: import org.objectweb.jonas.jtests.beans.relation.mou.ARemote;
036: import org.objectweb.jonas.jtests.beans.relation.mou.BHomeRemote;
037: import org.objectweb.jonas.jtests.beans.relation.mou.BRemote;
038: import org.objectweb.jonas.jtests.util.JTestCase;
039:
040: /**
041: * For testing many-to-one unidirectional relationships
042: * with relations created in the ejbPostCreate, as the EJB spec recommands it.
043: * @author Philippe Durieux
044: **/
045: public class F_Relation2_mouEC2 extends JTestCase {
046:
047: private static String BEAN_HOME_A = "relation_mou_A1Home";
048: protected static AHomeRemote ahome = null;
049: private static String BEAN_HOME_B = "relation_mou_B1Home";
050: protected static BHomeRemote bhome = null;
051:
052: public F_Relation2_mouEC2(String name) {
053: super (name);
054: }
055:
056: protected boolean isInit = false;
057:
058: protected void setUp() {
059: super .setUp();
060: if (!isInit) {
061: useBeans("mou", true);
062: try {
063: ahome = (AHomeRemote) PortableRemoteObject.narrow(ictx
064: .lookup(BEAN_HOME_A), AHomeRemote.class);
065: bhome = (BHomeRemote) PortableRemoteObject.narrow(ictx
066: .lookup(BEAN_HOME_B), BHomeRemote.class);
067: } catch (NamingException e) {
068: fail("Cannot get bean home: " + e.getMessage());
069: }
070: isInit = true;
071: }
072: }
073:
074: /**
075: * teardown is called after each test case.
076: * Notes for debugging :
077: * To see DataBase state after a test is passed :
078: * Replace the cleanall() by sync(false).
079: */
080: protected void tearDown() throws Exception {
081: cleanall();
082: }
083:
084: private void cleanall() throws Exception {
085: debug("remove all beans");
086:
087: // Start the Transaction. In case a Transaction is still running,
088: // roll it back first and start a new one.
089: try {
090: utx.begin();
091: } catch (Exception e) {
092: debug("rollback first and start a new tx");
093: utx.rollback();
094: utx.begin();
095: }
096: // In case there is a problem during remove, we must unload
097: // the bean to restart properly.
098: try {
099: Collection c = ahome.findAll();
100: for (Iterator i = c.iterator(); i.hasNext();) {
101: ARemote a = (ARemote) PortableRemoteObject.narrow(i
102: .next(), ARemote.class);
103: a.remove();
104: }
105: c = bhome.findAll();
106: for (Iterator i = c.iterator(); i.hasNext();) {
107: BRemote b = (BRemote) PortableRemoteObject.narrow(i
108: .next(), BRemote.class);
109: b.remove();
110: }
111: utx.commit();
112: } catch (Exception e) {
113: error("Exception in cleanup: " + e);
114: try {
115: utx.rollback();
116: } finally {
117: unloadBeans("mou");
118: isInit = false;
119: }
120: }
121: }
122:
123: public void testCreateB() throws Exception {
124: bhome.create("b0");
125: assertTrue(null != bhome.findByPrimaryKey("b0"));
126: }
127:
128: public void testCreateBA() throws Exception {
129: bhome.create("b0");
130: ahome.create("a0", "b0");
131: assertEquals("b0", ahome.findByPrimaryKey("a0").retrieveB());
132: }
133:
134: public static Test suite() {
135: return new TestSuite(F_Relation2_mouEC2.class);
136: }
137:
138: public static void main(String args[]) {
139: String testtorun = null;
140: // Get args
141: for (int argn = 0; argn < args.length; argn++) {
142: String s_arg = args[argn];
143: Integer i_arg;
144: if (s_arg.equals("-n")) {
145: testtorun = args[++argn];
146: }
147: }
148: if (testtorun == null) {
149: junit.textui.TestRunner.run(suite());
150: } else {
151: junit.textui.TestRunner.run(new F_Relation2_mouEC2(
152: testtorun));
153: }
154: }
155:
156: }
|