KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > clients > jca15 > F_xatransactionTest


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: F_xatransactionTest.java,v 1.1 2004/04/15 17:09:34 bobkruse Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.clients.jca15;
27
28 import junit.framework.*;
29 import java.io.BufferedReader JavaDoc;
30 import java.io.FileInputStream JavaDoc;
31 import java.io.InputStreamReader JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.lang.String JavaDoc;
34 import java.rmi.RemoteException JavaDoc;
35 import javax.rmi.PortableRemoteObject JavaDoc;
36 import java.util.Collection JavaDoc;
37 import java.util.Enumeration JavaDoc;
38 import java.util.Hashtable JavaDoc;
39 import java.util.Properties JavaDoc;
40 import javax.ejb.FinderException JavaDoc;
41 import javax.ejb.RemoveException JavaDoc;
42 import javax.naming.Context JavaDoc;
43 import javax.naming.InitialContext JavaDoc;
44 import javax.naming.NamingException JavaDoc;
45 import javax.transaction.UserTransaction JavaDoc;
46 import javax.transaction.SystemException JavaDoc;
47
48 import org.objectweb.jonas.jtests.beans.jca15.TransactedCA;
49 import org.objectweb.jonas.jtests.beans.jca15.TransactedCAHome;
50 import org.objectweb.jonas.jtests.beans.jca15.Utility;
51 import org.objectweb.jonas.jtests.util.JTestCase;
52 import org.objectweb.jonas.jtests.clients.jca15.F_securityTest;
53
54 /**
55  * F_xatransactionTests tests JOnAS Connection Architecture XA Transaction
56  */

57
58 public class F_xatransactionTest extends JTestCase {
59
60     static Context JavaDoc ctx = null;
61
62     // Lookup bean home
63

64     protected static String JavaDoc BEAN_HOME = "TransactedCAHome";
65     protected static TransactedCAHome home = null;
66     private static final String JavaDoc RAR_JNDI_NAME = "eis/ErsatzXATransaction";
67     final public int CLOSE_HANDLE = 0;
68     final public int CLOSE_PHYSICAL = 1;
69     public int CloseType = 0;
70     static TransactedCA bean = null;
71     public String JavaDoc UseBeans="jca15";
72     String JavaDoc err="";
73     
74     public F_xatransactionTest(String JavaDoc name) {
75         super(name);
76     }
77
78     protected void setUp() {
79         super.setUp();
80         try {
81             // get JNDI initial context
82

83             if (ctx == null) {
84                 err="InitialContext()";
85                 ctx = new InitialContext JavaDoc();
86             }
87         //
88
// We want to start transactions for client: get UserTransaction
89
//
90
if (utx == null) {
91                 err="lookup(javax.transaction.UserTransaction)";
92                 utx = (UserTransaction JavaDoc) ctx.lookup(Utility.USER_TRANSACTION);
93             }
94             if (home == null) {
95                 useBeans(UseBeans, false);
96             }
97             getBean();
98             assertTrue (6 == 6);
99
100         } catch (SystemException JavaDoc se1) {
101             String JavaDoc s="setUp error: Unable to "+err+" SystemException="+se1 ;
102             fail(s);
103         } catch (Exception JavaDoc e) {
104             fail("Cannot do setUp. Error on "+err+" Exception="+e);
105         }
106     }
107     private void getBean() throws Exception JavaDoc {
108         // Connecting to TransactedCAHome thru JNDI
109

110         if (home == null) {
111             home = (TransactedCAHome)PortableRemoteObject.narrow(
112                                 ctx.lookup(BEAN_HOME),
113                                 TransactedCAHome.class);
114             bean = home.create();
115         }
116     }
117     protected void tearDown() throws Exception JavaDoc {
118         //bean.closeUp(CloseType);
119
}
120     protected void startUp(String JavaDoc testName) {
121         try {
122             bean.method1(RAR_JNDI_NAME, testName);
123         } catch (Exception JavaDoc ee) {
124             ee.printStackTrace();
125             System.exit(2);
126         }
127     }
128
129 // test list ****************************************************
130

131     /**
132      * The application server invokes the ManagedConnection.getXAResource method
133      * to get the XAResource instance associated with the ManagedConnection instance.
134      * Then, application server calls the TM, which in turn calls the
135      * XAResourceImpl.start() method.
136      *
137      * This test verifies the existence of the instance, assigned Xid, and start()
138      */

139     public void testIsXaresource() {
140         CloseType=CLOSE_PHYSICAL;
141         
142         try {
143             startUp("testIsXaresource");
144             assertTrue(5 == 5);
145             String JavaDoc ans = bean.getXid();
146             assertTrue(5 == 5);
147             assertEquals("OK", ans);
148             bean.closeUp(CloseType);
149             assertTrue(5 == 5);
150         } catch (Exception JavaDoc e) {
151             System.err.println("XatransactionTest: Exception in TransactedCASLR");
152             e.printStackTrace();
153         } catch (Throwable JavaDoc t) {
154             System.err.println("XatransactionTest: Exception in TransactedCASLR");
155             t.printStackTrace();
156         }
157     }
158     /**
159      * At connection close, the application server performs "transactional" cleanup.
160      * The application server dissociates the XAResource instance (corresponding
161      * to the ManagedConnection "mc" instance) from the transaction manager.
162      * The transaction manager calls XAResource.end(Xid, flag)
163      * <p>
164      * The ConnectionEvent is CLOSE_CONNECTION. This test verifies that application
165      * server called TM which in turn called XAResource.end().
166      *
167      *
168      */

169     public void testIsXAend1() {
170         CloseType=CLOSE_HANDLE; // leave physical connection open
171

172         try {
173             startUp("testIsXAend1");
174             assertTrue(5 == 5);
175             assertTrue(bean.getCMInstance()); // is physical connection o.k.
176
bean.closeUp(CloseType);
177             assertTrue(bean.getCMInstance()); // should still be o.k.
178
assertTrue(5 == 5);
179         } catch (Exception JavaDoc e) {
180             System.err.println("testIsXAend1: Exception in TransactedCASLR");
181             e.printStackTrace();
182         } catch (Throwable JavaDoc t) {
183             System.err.println("testIsXAend1: Exception in TransactedCASLR");
184             t.printStackTrace();
185         }
186     }
187     /**
188      * At connection close, the application server performs "transactional" cleanup.
189      * The application server dissociates the XAResource instance (corresponding
190      * to the ManagedConnection "mc" instance) from the transaction manager.
191      * The transaction manager calls XAResource.end(Xid, flag)
192      * <p>
193      * The ConnectionEvent is CONNECTION_ERROR_OCCURRED to force a physical close.
194      * verifies mc.XAR reference is null
195      *
196      */

197     public void testIsXAend2() {
198         CloseType=CLOSE_PHYSICAL;
199         
200         try {
201             startUp("testIsXAend2");
202             String JavaDoc ans = bean.getXid();
203             assertEquals("FAIL", ans); // XAR is dissociated, should FAIL
204
// testIsXAend2 should not work. end() not called by TM-
205
} catch (Exception JavaDoc e) {
206             System.err.println("testIsXAend2: Exception in TransactedCASLR");
207             e.printStackTrace();
208         } catch (Throwable JavaDoc t) {
209             System.err.println("testIsXAend2: Exception in TransactedCASLR");
210             t.printStackTrace();
211         }
212     }
213
214     public static Test suite() {
215         return new TestSuite(F_xatransactionTest.class);
216     }
217
218     public static void main (String JavaDoc args[]) {
219
220         String JavaDoc testtorun = null;
221
222         // Get args
223

224         for (int argn = 0; argn < args.length; argn++) {
225
226             String JavaDoc s_arg = args[argn];
227             Integer JavaDoc i_arg;
228
229             if (s_arg.equals("-n")) {
230                 testtorun = args[++argn];
231             }
232         }
233
234         if (testtorun == null) {
235             junit.textui.TestRunner.run(suite());
236         } else {
237             junit.textui.TestRunner.run(new F_xatransactionTest(testtorun));
238         }
239     }
240 }
241
Popular Tags