KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > clients > j2eeca > F_transactionTest


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_transactionTest.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  * Tests Jonas Connector Architecture Local Transaction.
42  * F_transactionTests tests Jonas CA Local Transaction
43  */

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

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

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

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

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

115     /**
116     Test Local Transaction with component-managed demarcation.
117     
118     The application component uses a transaction demarcation API specific
119     to an EIS. The resource adapter needs to notify the application server of
120     the events (transaction begin, commit, and rollback) related to the local
121     transaction. An application server uses this part of the contract.
122
123      */

124     public void testTransaction1() {
125         CloseType=CLOSE_PHYSICAL;
126         startUp("testTransaction1");
127         try {
128             bean.beginLoTransaction();
129             assertTrue (6 == 6);
130             bean.closeUp(CloseType);
131             bean.commitLoTransaction();
132             assertTrue (6 == 6);
133         } catch (Exception JavaDoc e) {
134             assertTrue(6==7);
135         }
136     }
137
138     public void testTransaction2() {
139         CloseType=CLOSE_PHYSICAL;
140         startUp("testTransaction2");
141         try {
142             bean.closeUp(CloseType);
143             bean.commitLoTransaction();
144             assertTrue (6 == 7);
145         } catch (Exception JavaDoc e) {
146             assertTrue(6==6); // should get here
147
}
148     }
149
150     public static Test suite() {
151         return new TestSuite(F_transactionTest.class);
152     }
153
154     public static void main (String JavaDoc args[]) {
155
156         String JavaDoc testtorun = null;
157
158         // Get args
159

160         for (int argn = 0; argn < args.length; argn++) {
161
162             String JavaDoc s_arg = args[argn];
163             Integer JavaDoc i_arg;
164
165             if (s_arg.equals("-n")) {
166                 testtorun = args[++argn];
167             }
168         }
169
170         if (testtorun == null) {
171             junit.textui.TestRunner.run(suite());
172         } else {
173             junit.textui.TestRunner.run(new F_transactionTest(testtorun));
174         }
175     }
176 }
177
Popular Tags