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_transactionTest.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: * Tests Jonas Connector Architecture Local Transaction.
042: * F_transactionTests tests Jonas CA Local Transaction
043: */
044:
045: public class F_transactionTest extends JTestCase {
046:
047: private static UserTransaction utx = null;
048:
049: static Context ctx = null;
050:
051: // Lookup bean home
052:
053: protected static String BEAN_HOME = "transactedCAHome";
054: protected static transactedCAHome home = null;
055: private static final String RAR_JNDI_NAME = "FictionalLoTransaction";
056: final public int CLOSE_HANDLE = 0;
057: final public int CLOSE_PHYSICAL = 1;
058: public int CloseType = 0;
059: static transactedCA bean = null;
060: public String UseBeans = "j2eeca";
061:
062: public F_transactionTest(String name) {
063: super (name);
064: }
065:
066: protected void setUp() {
067: super .setUp();
068: try {
069: // get JNDI initial context
070:
071: if (ctx == null) {
072: ctx = new InitialContext();
073: }
074:
075: // We want to start transactions from client: get UserTransaction
076:
077: if (utx == null) {
078: utx = (UserTransaction) ctx
079: .lookup("javax.transaction.UserTransaction");
080: }
081:
082: if (home == null) {
083: useBeans(UseBeans, false);
084: }
085: getBean();
086: assertTrue(6 == 6);
087:
088: } catch (Exception e) {
089: fail("Cannot lookup UserTransaction in setUp: " + e);
090: }
091: }
092:
093: private void getBean() throws Exception {
094: // Connecting to transactedCAHome thru JNDI
095:
096: if (home == null) {
097: home = (transactedCAHome) PortableRemoteObject.narrow(ctx
098: .lookup(BEAN_HOME), transactedCAHome.class);
099: bean = home.create();
100: }
101: }
102:
103: protected void tearDown() throws Exception {
104: //bean.closeUp(CloseType);
105: }
106:
107: protected void startUp(String testName) {
108: try {
109: bean.method1(RAR_JNDI_NAME, testName);
110: } catch (Exception ee) {
111: ee.printStackTrace();
112: System.exit(2);
113: }
114: }
115:
116: // test list ****************************************************
117:
118: /**
119: Test Local Transaction with component-managed demarcation.
120:
121: The application component uses a transaction demarcation API specific
122: to an EIS. The resource adapter needs to notify the application server of
123: the events (transaction begin, commit, and rollback) related to the local
124: transaction. An application server uses this part of the contract.
125:
126: */
127: public void testTransaction1() {
128: CloseType = CLOSE_PHYSICAL;
129: startUp("testTransaction1");
130: try {
131: bean.beginLoTransaction();
132: assertTrue(6 == 6);
133: bean.closeUp(CloseType);
134: bean.commitLoTransaction();
135: assertTrue(6 == 6);
136: } catch (Exception e) {
137: assertTrue(6 == 7);
138: }
139: }
140:
141: public void testTransaction2() {
142: CloseType = CLOSE_PHYSICAL;
143: startUp("testTransaction2");
144: try {
145: bean.closeUp(CloseType);
146: bean.commitLoTransaction();
147: assertTrue(6 == 7);
148: } catch (Exception e) {
149: assertTrue(6 == 6); // should get here
150: }
151: }
152:
153: public static Test suite() {
154: return new TestSuite(F_transactionTest.class);
155: }
156:
157: public static void main(String args[]) {
158:
159: String testtorun = null;
160:
161: // Get args
162:
163: for (int argn = 0; argn < args.length; argn++) {
164:
165: String s_arg = args[argn];
166: Integer i_arg;
167:
168: if (s_arg.equals("-n")) {
169: testtorun = args[++argn];
170: }
171: }
172:
173: if (testtorun == null) {
174: junit.textui.TestRunner.run(suite());
175: } else {
176: junit.textui.TestRunner
177: .run(new F_transactionTest(testtorun));
178: }
179: }
180: }
|