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_xatransactionTest.java 4604 2004-04-15 17:15:45Z bobkruse $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.clients.jca15;
027:
028: import junit.framework.*;
029: import java.io.BufferedReader;
030: import java.io.FileInputStream;
031: import java.io.InputStreamReader;
032: import java.io.IOException;
033: import java.lang.String;
034: import java.rmi.RemoteException;
035: import javax.rmi.PortableRemoteObject;
036: import java.util.Collection;
037: import java.util.Enumeration;
038: import java.util.Hashtable;
039: import java.util.Properties;
040: import javax.ejb.FinderException;
041: import javax.ejb.RemoveException;
042: import javax.naming.Context;
043: import javax.naming.InitialContext;
044: import javax.naming.NamingException;
045: import javax.transaction.UserTransaction;
046: import javax.transaction.SystemException;
047:
048: import org.objectweb.jonas.jtests.beans.jca15.TransactedCA;
049: import org.objectweb.jonas.jtests.beans.jca15.TransactedCAHome;
050: import org.objectweb.jonas.jtests.beans.jca15.Utility;
051: import org.objectweb.jonas.jtests.util.JTestCase;
052: import org.objectweb.jonas.jtests.clients.jca15.F_securityTest;
053:
054: /**
055: * F_xatransactionTests tests JOnAS Connection Architecture XA Transaction
056: */
057:
058: public class F_xatransactionTest extends JTestCase {
059:
060: static Context ctx = null;
061:
062: // Lookup bean home
063:
064: protected static String BEAN_HOME = "TransactedCAHome";
065: protected static TransactedCAHome home = null;
066: private static final String RAR_JNDI_NAME = "eis/ErsatzXATransaction";
067: final public int CLOSE_HANDLE = 0;
068: final public int CLOSE_PHYSICAL = 1;
069: public int CloseType = 0;
070: static TransactedCA bean = null;
071: public String UseBeans = "jca15";
072: String err = "";
073:
074: public F_xatransactionTest(String name) {
075: super (name);
076: }
077:
078: protected void setUp() {
079: super .setUp();
080: try {
081: // get JNDI initial context
082:
083: if (ctx == null) {
084: err = "InitialContext()";
085: ctx = new InitialContext();
086: }
087: //
088: // We want to start transactions for client: get UserTransaction
089: //
090: if (utx == null) {
091: err = "lookup(javax.transaction.UserTransaction)";
092: utx = (UserTransaction) ctx
093: .lookup(Utility.USER_TRANSACTION);
094: }
095: if (home == null) {
096: useBeans(UseBeans, false);
097: }
098: getBean();
099: assertTrue(6 == 6);
100:
101: } catch (SystemException se1) {
102: String s = "setUp error: Unable to " + err
103: + " SystemException=" + se1;
104: fail(s);
105: } catch (Exception e) {
106: fail("Cannot do setUp. Error on " + err + " Exception=" + e);
107: }
108: }
109:
110: private void getBean() throws Exception {
111: // Connecting to TransactedCAHome thru JNDI
112:
113: if (home == null) {
114: home = (TransactedCAHome) PortableRemoteObject.narrow(ctx
115: .lookup(BEAN_HOME), TransactedCAHome.class);
116: bean = home.create();
117: }
118: }
119:
120: protected void tearDown() throws Exception {
121: //bean.closeUp(CloseType);
122: }
123:
124: protected void startUp(String testName) {
125: try {
126: bean.method1(RAR_JNDI_NAME, testName);
127: } catch (Exception ee) {
128: ee.printStackTrace();
129: System.exit(2);
130: }
131: }
132:
133: // test list ****************************************************
134:
135: /**
136: * The application server invokes the ManagedConnection.getXAResource method
137: * to get the XAResource instance associated with the ManagedConnection instance.
138: * Then, application server calls the TM, which in turn calls the
139: * XAResourceImpl.start() method.
140: *
141: * This test verifies the existence of the instance, assigned Xid, and start()
142: */
143: public void testIsXaresource() {
144: CloseType = CLOSE_PHYSICAL;
145:
146: try {
147: startUp("testIsXaresource");
148: assertTrue(5 == 5);
149: String ans = bean.getXid();
150: assertTrue(5 == 5);
151: assertEquals("OK", ans);
152: bean.closeUp(CloseType);
153: assertTrue(5 == 5);
154: } catch (Exception e) {
155: System.err
156: .println("XatransactionTest: Exception in TransactedCASLR");
157: e.printStackTrace();
158: } catch (Throwable t) {
159: System.err
160: .println("XatransactionTest: Exception in TransactedCASLR");
161: t.printStackTrace();
162: }
163: }
164:
165: /**
166: * At connection close, the application server performs "transactional" cleanup.
167: * The application server dissociates the XAResource instance (corresponding
168: * to the ManagedConnection "mc" instance) from the transaction manager.
169: * The transaction manager calls XAResource.end(Xid, flag)
170: * <p>
171: * The ConnectionEvent is CLOSE_CONNECTION. This test verifies that application
172: * server called TM which in turn called XAResource.end().
173: *
174: *
175: */
176: public void testIsXAend1() {
177: CloseType = CLOSE_HANDLE; // leave physical connection open
178:
179: try {
180: startUp("testIsXAend1");
181: assertTrue(5 == 5);
182: assertTrue(bean.getCMInstance()); // is physical connection o.k.
183: bean.closeUp(CloseType);
184: assertTrue(bean.getCMInstance()); // should still be o.k.
185: assertTrue(5 == 5);
186: } catch (Exception e) {
187: System.err
188: .println("testIsXAend1: Exception in TransactedCASLR");
189: e.printStackTrace();
190: } catch (Throwable t) {
191: System.err
192: .println("testIsXAend1: Exception in TransactedCASLR");
193: t.printStackTrace();
194: }
195: }
196:
197: /**
198: * At connection close, the application server performs "transactional" cleanup.
199: * The application server dissociates the XAResource instance (corresponding
200: * to the ManagedConnection "mc" instance) from the transaction manager.
201: * The transaction manager calls XAResource.end(Xid, flag)
202: * <p>
203: * The ConnectionEvent is CONNECTION_ERROR_OCCURRED to force a physical close.
204: * verifies mc.XAR reference is null
205: *
206: */
207: public void testIsXAend2() {
208: CloseType = CLOSE_PHYSICAL;
209:
210: try {
211: startUp("testIsXAend2");
212: String ans = bean.getXid();
213: assertEquals("FAIL", ans); // XAR is dissociated, should FAIL
214: // testIsXAend2 should not work. end() not called by TM-
215: } catch (Exception e) {
216: System.err
217: .println("testIsXAend2: Exception in TransactedCASLR");
218: e.printStackTrace();
219: } catch (Throwable t) {
220: System.err
221: .println("testIsXAend2: Exception in TransactedCASLR");
222: t.printStackTrace();
223: }
224: }
225:
226: public static Test suite() {
227: return new TestSuite(F_xatransactionTest.class);
228: }
229:
230: public static void main(String args[]) {
231:
232: String testtorun = null;
233:
234: // Get args
235:
236: for (int argn = 0; argn < args.length; argn++) {
237:
238: String s_arg = args[argn];
239: Integer i_arg;
240:
241: if (s_arg.equals("-n")) {
242: testtorun = args[++argn];
243: }
244: }
245:
246: if (testtorun == null) {
247: junit.textui.TestRunner.run(suite());
248: } else {
249: junit.textui.TestRunner.run(new F_xatransactionTest(
250: testtorun));
251: }
252: }
253: }
|