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