KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > test > stateless > StatelessBeanTxTests


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 1999 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: StatelessBeanTxTests.java 1096 2004-03-26 21:41:16Z dblevins $
44  */

45 package org.openejb.test.stateless;
46
47 import java.util.Properties JavaDoc;
48
49 import javax.ejb.EJBMetaData JavaDoc;
50 import javax.ejb.Handle JavaDoc;
51 import javax.ejb.HomeHandle JavaDoc;
52 import javax.naming.Context JavaDoc;
53 import javax.naming.InitialContext JavaDoc;
54 import javax.transaction.RollbackException JavaDoc;
55
56 import org.openejb.test.TestManager;
57 import org.openejb.test.object.Account;
58 import org.openejb.test.object.Transaction;
59
60 /**
61  * [1] Should be run as the first test suite of the StatelessTestClients
62  *
63  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
64  * @author <a HREF="mailto:Richard@Monson-Haefel.com">Richard Monson-Haefel</a>
65  */

66 public class StatelessBeanTxTests extends org.openejb.test.NamedTestCase{
67
68     public final static String JavaDoc jndiEJBHomeEntry = "client/tests/stateless/BeanManagedTransactionTests/EJBHome";
69
70     protected BeanTxStatelessHome ejbHome;
71     protected BeanTxStatelessObject ejbObject;
72
73     protected EJBMetaData JavaDoc ejbMetaData;
74     protected HomeHandle JavaDoc ejbHomeHandle;
75     protected Handle JavaDoc ejbHandle;
76     protected Integer JavaDoc ejbPrimaryKey;
77
78     protected InitialContext JavaDoc initialContext;
79
80     public StatelessBeanTxTests(){
81         super("Stateless.BeanManagedTransaction.");
82     }
83
84     /**
85      * Sets up the fixture, for example, open a network connection.
86      * This method is called before a test is executed.
87      */

88     protected void setUp() throws Exception JavaDoc {
89
90         Properties JavaDoc properties = TestManager.getServer().getContextEnvironment();
91         properties.put(Context.SECURITY_PRINCIPAL, "STATELESS_test00_CLIENT");
92         properties.put(Context.SECURITY_CREDENTIALS, "STATELESS_test00_CLIENT");
93
94         initialContext = new InitialContext JavaDoc(properties);
95
96         /*[1] Get bean */
97         Object JavaDoc obj = initialContext.lookup(jndiEJBHomeEntry);
98         ejbHome = (BeanTxStatelessHome)javax.rmi.PortableRemoteObject.narrow( obj, BeanTxStatelessHome.class);
99         ejbObject = ejbHome.create();
100
101         /*[2] Create database table */
102         TestManager.getDatabase().createAccountTable();
103     }
104
105     /**
106      * Tears down the fixture, for example, close a network connection.
107      * This method is called after a test is executed.
108      */

109     protected void tearDown() throws Exception JavaDoc {
110         /*[1] Drop database table */
111         TestManager.getDatabase().dropAccountTable();
112     }
113
114
115     /**
116      * <B>11.6.1 Bean-managed transaction demarcation</B>
117      * <P>
118      * The Container must make the javax.transaction.UserTransaction interface available to
119      * the enterprise bean’s business method via the javax.ejb.EJBContext interface and under the
120      * environment entry java:comp/UserTransaction. When an instance uses the javax.trans-action.
121      * UserTransaction interface to demarcate a transaction, the Container must enlist all the
122      * resource managers used by the instance between the begin() and commit()—or rollback()—
123      * methods with the transaction. When the instance attempts to commit the transaction, the Container is
124      * responsible for the global coordination of the transaction commit.
125      * </P>
126      * <P>--------------------------------------------------------</P>
127      * <P>
128      * Check that a javax.transaction.UserTransaction can be obtained from
129      * the javax.ejb.EJBContext
130      * </P>
131      */

132     public void test01_EJBContext_getUserTransaction(){
133         try{
134            Transaction t = ejbObject.getUserTransaction();
135            assertNotNull("UserTransaction is null.", t);
136         } catch (Exception JavaDoc e){
137             fail("Received Exception "+e.getClass()+ " : "+e.getMessage());
138         }
139     }
140
141     /**
142      *
143      * <B>11.6.1 Bean-managed transaction demarcation</B>
144      * <P>
145      * The Container must make the javax.transaction.UserTransaction interface available to
146      * the enterprise bean’s business method via the javax.ejb.EJBContext interface and under the
147      * environment entry java:comp/UserTransaction. When an instance uses the javax.trans-action.
148      * UserTransaction interface to demarcate a transaction, the Container must enlist all the
149      * resource managers used by the instance between the begin() and commit()—or rollback()—
150      * methods with the transaction. When the instance attempts to commit the transaction, the Container is
151      * responsible for the global coordination of the transaction commit.
152      * </P>
153      * <P>--------------------------------------------------------</P>
154      * <P>
155      * Check that a javax.transaction.UserTransaction can be obtained from
156      * the environment entry java:comp/UserTransaction
157      * </P>
158      */

159     public void test02_java_comp_UserTransaction(){
160         try{
161             Transaction t = ejbObject.jndiUserTransaction();
162             assertNotNull("UserTransaction is null. Could not retreive a UserTransaction from the bean's JNDI namespace.", t);
163         } catch (Exception JavaDoc e){
164             fail("Could not retreive a UserTransaction from the bean's JNDI namespace. Received Exception "+e.getClass()+ " : "+e.getMessage());
165         }
166     }
167
168     /**
169      * <B>11.6.1 Bean-managed transaction demarcation</B>
170      * <P>
171      * The Container must throw the java.lang.IllegalStateException if an instance of a bean
172      * with bean-managed transaction demarcation attempts to invoke the setRollbackOnly() or
173      * getRollbackOnly() method of the javax.ejb.EJBContext interface.
174      * </P>
175      * <P>--------------------------------------------------------</P>
176      * <P>
177      * Test that setRollbackOnly() throws a java.lang.IllegalStateException
178      * </P>
179      */

180     public void TODO_test03_EJBContext_setRollbackOnly(){
181         try{
182
183         } catch (Exception JavaDoc e){
184             fail("Received Exception "+e.getClass()+ " : "+e.getMessage());
185         }
186     }
187
188     /**
189      * <B>11.6.1 Bean-managed transaction demarcation</B>
190      * <P>
191      * The Container must throw the java.lang.IllegalStateException if an instance of a bean
192      * with bean-managed transaction demarcation attempts to invoke the setRollbackOnly() or
193      * getRollbackOnly() method of the javax.ejb.EJBContext interface.
194      * </P>
195      * <P>--------------------------------------------------------</P>
196      * <P>
197      * Test that getRollbackOnly() throws a java.lang.IllegalStateException
198      * </P>
199      */

200     public void TODO_test04_EJBContext_getRollbackOnly(){
201         try{
202
203         } catch (Exception JavaDoc e){
204             fail("Received Exception "+e.getClass()+ " : "+e.getMessage());
205         }
206     }
207
208     /**
209      *
210      */

211     public void test05_singleTransactionCommit(){
212         try{
213             Account expected = new Account("123-45-6789","Joe","Cool",40000);
214             Account actual = new Account();
215
216             ejbObject.openAccount(expected, new Boolean JavaDoc(false));
217             actual = ejbObject.retreiveAccount( expected.getSsn() );
218
219             assertNotNull( "The transaction was not commited. The record is null", actual );
220             assertEquals( "The transaction was not commited cleanly.", expected, actual );
221         } catch (RollbackException JavaDoc re){
222             fail("Transaction was rolledback. Received Exception "+re.getClass()+ " : "+re.getMessage());
223         } catch (Exception JavaDoc e){
224             fail("Received Exception "+e.getClass()+ " : "+e.getMessage());
225         }
226     }
227
228     /**
229      * This test does work for the IntraVM Server, but it fails on
230      * the Remote Server. For some reason, when the RollbackException is
231      * sent to the client, the server blocks.
232      */

233     public void BUG_test06_singleTransactionRollback(){
234         Account expected = new Account("234-56-7890","Charlie","Brown", 20000);
235         Account actual = new Account();
236
237         // Try and add the account in a transaction. This should fail and
238
// throw a RollbackException
239
try{
240             ejbObject.openAccount(expected, new Boolean JavaDoc(true));
241             fail( "A javax.transaction.RollbackException should have been thrown." );
242         } catch (RollbackException JavaDoc re){
243             // Good.
244
} catch (Exception JavaDoc e){
245             fail("Received Exception "+e.getClass()+ " : "+e.getMessage());
246         }
247         
248       //// Now check that the account really wasn't added.
249
//try{
250
// actual = ejbObject.retreiveAccount( expected.getSsn() );
251
// //assertTrue( "The transaction was commited when it should have been rolledback.", !expected.equals(actual) );
252
//} catch (Exception e){
253
// fail("Received Exception "+e.getClass()+ " : "+e.getMessage());
254
//}
255
}
256
257
258     /**
259      * <B>11.6.1 Bean-managed transaction demarcation</B>
260      * <P>
261      * The Container must allow the enterprise bean instance to serially perform several transactions in a
262      * method.
263      * </P>
264      */

265     public void TODO_test07_serialTransactions(){
266         try{
267
268         } catch (Exception JavaDoc e){
269             fail("Received Exception "+e.getClass()+ " : "+e.getMessage());
270         }
271     }
272
273     /**
274      * <B>11.6.1 Bean-managed transaction demarcation</B>
275      * <P>
276      * When an instance attempts to start a transaction using the
277      * begin() method of the javax.transaction.UserTransaction
278      * interface while the instance has not committed the previous
279      * transaction, the Container must throw the
280      * javax.transaction.NotSupportedException in the begin() method.
281      * </P>
282      */

283     public void TODO_test08_nestedTransactions(){
284         try{
285
286         } catch (Exception JavaDoc e){
287             fail("Received Exception "+e.getClass()+ " : "+e.getMessage());
288         }
289     }
290
291
292     /**
293      * <B>11.6.1 Bean-managed transaction demarcation</B>
294      * <P>
295      * If a stateless session bean instance starts a transaction in a
296      * business method, it must commit the transaction before the
297      * business method returns. The Container must detect the case in
298      * which a transaction was started, but not completed, in the
299      * business method, and handle it as follows:
300      * <UL>
301      * <LI>Log this as an application error to alert the system administrator.
302      * <LI>Roll back the started transaction.
303      * <LI>Discard the instance of the session bean.
304      * <LI>Throw the java.rmi.RemoteException to the client.
305      * </UL>
306      * </P>
307      */

308     public void TODO_test09_beginWithNoCommit(){
309         try{
310
311         } catch (Exception JavaDoc e){
312             fail("Received Exception "+e.getClass()+ " : "+e.getMessage());
313         }
314     }
315
316         /**
317      * <B>11.6.1 Bean-managed transaction demarcation</B>
318      * <P>
319      * The actions performed by the Container for an instance with bean-managed transaction are summarized
320      * by the following table. T1 is a transaction associated with a client request, T2 is a transaction that is cur-rently
321      * associated with the instance (i.e. a transaction that was started but not completed by a previous
322      * business method).
323      * </P>
324      * <PRE>
325      * =========================================================================
326      * Container’s actions for methods of beans with bean-managed transaction
327      * =========================================================================
328      *
329      * | IF | AND | THEN
330      * scenario | Client’s | Transaction currently | Transaction associated
331      * | transaction | associated with instance | with the method is
332      * ___________|_____________|__________________________|________________________
333      * | | |
334      * 1 | none | none | none
335      * ___________|_____________|__________________________|________________________
336      * | | |
337      * 2 | T1 | none | none
338      * ___________|_____________|__________________________|________________________
339      * | | |
340      * 3 | none | T2 | T2
341      * ___________|_____________|__________________________|________________________
342      * | | |
343      * 4 | T1 | T2 | T2
344      * ___________|_____________|__________________________|________________________
345      * </PRE>
346      * <P>
347      * If the client request is not associated with a transaction and the instance is not associated with a
348      * transaction, the container invokes the instance with an unspecified transaction context.
349      * </P>
350      * <P>--------------------------------------------------------</P>
351      * <P>
352      * Test scenario 1: none none<BR>
353      * If the client's transaction is none and the transaction currently
354      * associated with instance none then the transaction associated with the method is none.
355      * </P>
356      */

357     public void TODO_test10_scenario1_NoneNone(){
358         try{
359
360         } catch (Exception JavaDoc e){
361             fail("Received Exception "+e.getClass()+ " : "+e.getMessage());
362         }
363     }
364
365     /**
366      * <B>11.6.1 Bean-managed transaction demarcation</B>
367      * <P>
368      * The actions performed by the Container for an instance with bean-managed transaction are summarized
369      * by the following table. T1 is a transaction associated with a client request, T2 is a transaction that is cur-rently
370      * associated with the instance (i.e. a transaction that was started but not completed by a previous
371      * business method).
372      * </P>
373      * <PRE>
374      * =========================================================================
375      * Container’s actions for methods of beans with bean-managed transaction
376      * =========================================================================
377      *
378      * | IF | AND | THEN
379      * scenario | Client’s | Transaction currently | Transaction associated
380      * | transaction | associated with instance | with the method is
381      * ___________|_____________|__________________________|________________________
382      * | | |
383      * 1 | none | none | none
384      * ___________|_____________|__________________________|________________________
385      * | | |
386      * 2 | T1 | none | none
387      * ___________|_____________|__________________________|________________________
388      * | | |
389      * 3 | none | T2 | T2
390      * ___________|_____________|__________________________|________________________
391      * | | |
392      * 4 | T1 | T2 | T2
393      * ___________|_____________|__________________________|________________________
394      * </PRE>
395      * <P>
396      * If the client is associated with a transaction T1, and the instance is not associated with a transaction,
397      * the container suspends the client’s transaction association and invokes the method with
398      * an unspecified transaction context. The container resumes the client’s ntransaction association
399      * (T1) when the method completes.
400      * </P>
401      * <P>--------------------------------------------------------</P>
402      * <P>
403      * Test scenario 2: T1 none<BR>
404      * </P>
405      */

406     public void TODO_test11_scenario2_T1None(){
407         try{
408
409         } catch (Exception JavaDoc e){
410             fail("Received Exception "+e.getClass()+ " : "+e.getMessage());
411         }
412     }
413
414 }
415
416
Popular Tags