KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > clients > j2eeca > 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.2 2004/03/19 11:57:19 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.clients.j2eeca;
27
28 import javax.naming.Context JavaDoc;
29 import javax.naming.InitialContext JavaDoc;
30 import javax.rmi.PortableRemoteObject JavaDoc;
31 import javax.transaction.UserTransaction JavaDoc;
32
33 import junit.framework.Test;
34 import junit.framework.TestSuite;
35
36 import org.objectweb.jonas.jtests.beans.j2eeca.transactedCA;
37 import org.objectweb.jonas.jtests.beans.j2eeca.transactedCAHome;
38 import org.objectweb.jonas.jtests.util.JTestCase;
39
40 /**
41  * F_xatransactionTests tests JOnAS Connection Architecture XA Transaction
42  */

43
44 public class F_xatransactionTest extends JTestCase {
45
46     private static UserTransaction JavaDoc utx = null;
47
48     static Context JavaDoc ctx = null;
49
50     // Lookup bean home
51

52     protected static String JavaDoc BEAN_HOME = "transactedCAHome";
53     protected static transactedCAHome home = null;
54     private static final String JavaDoc RAR_JNDI_NAME = "FictionalXATransaction";
55     final public int CLOSE_HANDLE = 0;
56     final public int CLOSE_PHYSICAL = 1;
57     public int CloseType = 0;
58     static transactedCA bean = null;
59     public String JavaDoc UseBeans="j2eeca";
60     
61     public F_xatransactionTest(String JavaDoc name) {
62         super(name);
63     }
64
65     protected void setUp() {
66         super.setUp();
67         try {
68             // get JNDI initial context
69

70             if (ctx == null) {
71                 ctx = new InitialContext JavaDoc();
72             }
73
74             // We want to start transactions from client: get UserTransaction
75

76             if (utx == null) {
77                 utx = (UserTransaction JavaDoc) ctx.lookup("javax.transaction.UserTransaction");
78             }
79
80             if (home == null) {
81                 useBeans(UseBeans, false);
82             }
83             getBean();
84             assertTrue (6 == 6);
85
86         } catch (Exception JavaDoc e) {
87             fail("Cannot lookup UserTransaction in setUp: " +e);
88         }
89     }
90     private void getBean() throws Exception JavaDoc {
91         // Connecting to transactedCAHome thru JNDI
92

93         if (home == null) {
94             home = (transactedCAHome)PortableRemoteObject.narrow(
95                                 ctx.lookup(BEAN_HOME),
96                                 transactedCAHome.class);
97             bean = home.create();
98         }
99     }
100     protected void tearDown() throws Exception JavaDoc {
101         //bean.closeUp(CloseType);
102
}
103     protected void startUp(String JavaDoc testName) {
104         try {
105             bean.method1(RAR_JNDI_NAME, testName);
106         } catch (Exception JavaDoc ee) {
107             ee.printStackTrace();
108             System.exit(2);
109         }
110     }
111
112 // test list ****************************************************
113

114     /**
115      * The application server invokes the ManagedConnection.getXAResource method
116      * to get the XAResource instance associated with the ManagedConnection instance.
117      * Then, application server calls the TM, which in turn calls the
118      * XAResourceImpl.start() method.
119      *
120      * This test verifies the existence of the instance, assigned Xid, and start()
121      */

122     public void testIsXaresource() {
123         CloseType=CLOSE_PHYSICAL;
124         
125         try {
126             utx.begin();
127             startUp("testIsXaresource");
128             String JavaDoc ans = bean.getXid();
129             assertEquals("OK", ans);
130             bean.closeUp(CloseType);
131             utx.commit();
132             assertTrue(5 == 5);
133         } catch (Exception JavaDoc e) {
134             try {
135                 utx.rollback();
136             } catch (Exception JavaDoc f) {
137                 assertTrue(5 == 5);
138             }
139             assertTrue(5 == 5);
140         }
141     }
142     /**
143      * At connection close, the application server performs "transactional" cleanup.
144      * The application server dissociates the XAResource instance (corresponding
145      * to the ManagedConnection "mc" instance) from the transaction manager.
146      * The transaction manager calls XAResource.end(Xid, flag)
147      * <p>
148      * The ConnectionEvent is CLOSE_CONNECTION. This test verifies that application
149      * server called TM which in turn called XAResource.end().
150      *
151      *
152      */

153     public void testIsXAend1() {
154         CloseType=CLOSE_HANDLE; // leave physical connection open
155

156         try {
157             utx.begin();
158             startUp("testIsXAend1");
159             assertTrue(bean.getCMInstance()); // is physical connection o.k.
160
bean.closeUp(CloseType);
161             assertTrue(bean.getCMInstance()); // should still be o.k.
162
utx.commit();
163             assertTrue(5 == 5);
164         } catch (Exception JavaDoc e) {
165             try {
166                 utx.rollback();
167             } catch (Exception JavaDoc f) {
168                 assertTrue(5 == 5);
169             }
170             assertTrue(5 == 5);
171         }
172     }
173     /**
174      * At connection close, the application server performs "transactional" cleanup.
175      * The application server dissociates the XAResource instance (corresponding
176      * to the ManagedConnection "mc" instance) from the transaction manager.
177      * The transaction manager calls XAResource.end(Xid, flag)
178      * <p>
179      * The ConnectionEvent is CONNECTION_ERROR_OCCURRED to force a physical close.
180      * verifies mc.XAR reference is null
181      *
182      */

183     public void testIsXAend2() {
184         CloseType=CLOSE_PHYSICAL;
185         
186         try {
187             utx.begin();
188             startUp("testIsXAend2");
189             assertTrue(bean.getCMInstance()); // is physical connection o.k.
190
bean.closeUp(CloseType);
191             String JavaDoc ans = bean.getXid();
192             //System.out.print(" -testIsXAend2 does not work. end() not called by TM-");
193
//System.out.println("");
194
assertEquals("FAIL", ans); // XAR is dissociated
195
utx.commit();
196             assertTrue(5 == 5);
197         } catch (Exception JavaDoc e) {
198             try {
199                 utx.rollback();
200             } catch (Exception JavaDoc f) {
201                 assertTrue(5 == 5);
202             }
203             assertFalse(5 == 5);
204         }
205     }
206
207     public static Test suite() {
208         return new TestSuite(F_xatransactionTest.class);
209     }
210
211     public static void main (String JavaDoc args[]) {
212
213         String JavaDoc testtorun = null;
214
215         // Get args
216

217         for (int argn = 0; argn < args.length; argn++) {
218
219             String JavaDoc s_arg = args[argn];
220             Integer JavaDoc i_arg;
221
222             if (s_arg.equals("-n")) {
223                 testtorun = args[++argn];
224             }
225         }
226
227         if (testtorun == null) {
228             junit.textui.TestRunner.run(suite());
229         } else {
230             junit.textui.TestRunner.run(new F_xatransactionTest(testtorun));
231         }
232     }
233 }
234
Popular Tags