001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.test.stateless;
017:
018: import java.util.Properties;
019:
020: import javax.ejb.EJBMetaData;
021: import javax.ejb.Handle;
022: import javax.ejb.HomeHandle;
023: import javax.naming.Context;
024: import javax.naming.InitialContext;
025: import javax.transaction.RollbackException;
026:
027: import org.apache.openejb.test.TestManager;
028: import org.apache.openejb.test.object.Account;
029: import org.apache.openejb.test.object.Transaction;
030:
031: /**
032: * [1] Should be run as the first test suite of the StatelessTestClients
033: *
034: * @author <a href="mailto:david.blevins@visi.com">David Blevins</a>
035: * @author <a href="mailto:Richard@Monson-Haefel.com">Richard Monson-Haefel</a>
036: */
037: public class StatelessBeanTxTests extends
038: org.apache.openejb.test.NamedTestCase {
039:
040: public final static String jndiEJBHomeEntry = "client/tests/stateless/BeanManagedTransactionTests/EJBHome";
041:
042: protected BeanTxStatelessHome ejbHome;
043: protected BeanTxStatelessObject ejbObject;
044:
045: protected EJBMetaData ejbMetaData;
046: protected HomeHandle ejbHomeHandle;
047: protected Handle ejbHandle;
048: protected Integer ejbPrimaryKey;
049:
050: protected InitialContext initialContext;
051:
052: public StatelessBeanTxTests() {
053: super ("Stateless.BeanManagedTransaction.");
054: }
055:
056: /**
057: * Sets up the fixture, for example, open a network connection.
058: * This method is called before a test is executed.
059: */
060: protected void setUp() throws Exception {
061:
062: Properties properties = TestManager.getServer()
063: .getContextEnvironment();
064: //properties.put(Context.SECURITY_PRINCIPAL, "STATELESS_test00_CLIENT");
065: //properties.put(Context.SECURITY_CREDENTIALS, "STATELESS_test00_CLIENT");
066:
067: initialContext = new InitialContext(properties);
068:
069: /*[1] Get bean */
070: Object obj = initialContext.lookup(jndiEJBHomeEntry);
071: ejbHome = (BeanTxStatelessHome) javax.rmi.PortableRemoteObject
072: .narrow(obj, BeanTxStatelessHome.class);
073: ejbObject = ejbHome.create();
074:
075: /*[2] Create database table */
076: TestManager.getDatabase().createAccountTable();
077: }
078:
079: /**
080: * Tears down the fixture, for example, close a network connection.
081: * This method is called after a test is executed.
082: */
083: protected void tearDown() throws Exception {
084: /*[1] Drop database table */
085: TestManager.getDatabase().dropAccountTable();
086: }
087:
088: /**
089: * <B>11.6.1 Bean-managed transaction demarcation</B>
090: * <P>
091: * The Container must make the javax.transaction.UserTransaction interface available to
092: * the enterprise bean's business method via the javax.ejb.EJBContext interface and under the
093: * environment entry java:comp/UserTransaction. When an instance uses the javax.trans-action.
094: * UserTransaction interface to demarcate a transaction, the Container must enlist all the
095: * resource managers used by the instance between the begin() and commit(),or rollback(),
096: * methods with the transaction. When the instance attempts to commit the transaction, the Container is
097: * responsible for the global coordination of the transaction commit.
098: * </P>
099: * <P>--------------------------------------------------------</P>
100: * <P>
101: * Check that a javax.transaction.UserTransaction can be obtained from
102: * the javax.ejb.EJBContext
103: * </P>
104: */
105: public void test01_EJBContext_getUserTransaction() {
106: try {
107: Transaction t = ejbObject.getUserTransaction();
108: assertNotNull("UserTransaction is null.", t);
109: } catch (Exception e) {
110: fail("Received Exception " + e.getClass() + " : "
111: + e.getMessage());
112: }
113: }
114:
115: /**
116: *
117: * <B>11.6.1 Bean-managed transaction demarcation</B>
118: * <P>
119: * The Container must make the javax.transaction.UserTransaction interface available to
120: * the enterprise bean's business method via the javax.ejb.EJBContext interface and under the
121: * environment entry java:comp/UserTransaction. When an instance uses the javax.trans-action.
122: * UserTransaction interface to demarcate a transaction, the Container must enlist all the
123: * resource managers used by the instance between the begin() and commit(),or rollback(),
124: * methods with the transaction. When the instance attempts to commit the transaction, the Container is
125: * responsible for the global coordination of the transaction commit.
126: * </P>
127: * <P>--------------------------------------------------------</P>
128: * <P>
129: * Check that a javax.transaction.UserTransaction can be obtained from
130: * the environment entry java:comp/UserTransaction
131: * </P>
132: */
133: public void test02_java_comp_UserTransaction() {
134: try {
135: Transaction t = ejbObject.jndiUserTransaction();
136: assertNotNull(
137: "UserTransaction is null. Could not retreive a UserTransaction from the bean's JNDI namespace.",
138: t);
139: } catch (Exception e) {
140: fail("Could not retreive a UserTransaction from the bean's JNDI namespace. Received Exception "
141: + e.getClass() + " : " + e.getMessage());
142: }
143: }
144:
145: /**
146: * <B>11.6.1 Bean-managed transaction demarcation</B>
147: * <P>
148: * The Container must throw the java.lang.IllegalStateException if an instance of a bean
149: * with bean-managed transaction demarcation attempts to invoke the setRollbackOnly() or
150: * getRollbackOnly() method of the javax.ejb.EJBContext interface.
151: * </P>
152: * <P>--------------------------------------------------------</P>
153: * <P>
154: * Test that setRollbackOnly() throws a java.lang.IllegalStateException
155: * </P>
156: */
157: public void TODO_test03_EJBContext_setRollbackOnly() {
158: try {
159:
160: } catch (Exception e) {
161: fail("Received Exception " + e.getClass() + " : "
162: + e.getMessage());
163: }
164: }
165:
166: /**
167: * <B>11.6.1 Bean-managed transaction demarcation</B>
168: * <P>
169: * The Container must throw the java.lang.IllegalStateException if an instance of a bean
170: * with bean-managed transaction demarcation attempts to invoke the setRollbackOnly() or
171: * getRollbackOnly() method of the javax.ejb.EJBContext interface.
172: * </P>
173: * <P>--------------------------------------------------------</P>
174: * <P>
175: * Test that getRollbackOnly() throws a java.lang.IllegalStateException
176: * </P>
177: */
178: public void TODO_test04_EJBContext_getRollbackOnly() {
179: try {
180:
181: } catch (Exception e) {
182: fail("Received Exception " + e.getClass() + " : "
183: + e.getMessage());
184: }
185: }
186:
187: /**
188: *
189: */
190: public void test05_singleTransactionCommit() {
191: try {
192: Account expected = new Account("123-45-6789", "Joe",
193: "Cool", 40000);
194: Account actual = new Account();
195:
196: ejbObject.openAccount(expected, new Boolean(false));
197: actual = ejbObject.retreiveAccount(expected.getSsn());
198:
199: assertNotNull(
200: "The transaction was not commited. The record is null",
201: actual);
202: assertEquals("The transaction was not commited cleanly.",
203: expected, actual);
204: } catch (RollbackException re) {
205: fail("Transaction was rolledback. Received Exception "
206: + re.getClass() + " : " + re.getMessage());
207: } catch (Exception e) {
208: fail("Received Exception " + e.getClass() + " : "
209: + e.getMessage());
210: }
211: }
212:
213: /**
214: * This test does work for the IntraVM Server, but it fails on
215: * the Remote Server. For some reason, when the RollbackException is
216: * sent to the client, the server blocks.
217: */
218: public void BUG_test06_singleTransactionRollback() {
219: Account expected = new Account("234-56-7890", "Charlie",
220: "Brown", 20000);
221: Account actual = new Account();
222:
223: // Try and add the account in a transaction. This should fail and
224: // throw a RollbackException
225: try {
226: ejbObject.openAccount(expected, new Boolean(true));
227: fail("A javax.transaction.RollbackException should have been thrown.");
228: } catch (RollbackException re) {
229: // Good.
230: } catch (Exception e) {
231: fail("Received Exception " + e.getClass() + " : "
232: + e.getMessage());
233: }
234:
235: //// Now check that the account really wasn't added.
236: //try{
237: // actual = ejbObject.retreiveAccount( expected.getSsn() );
238: // //assertTrue( "The transaction was commited when it should have been rolledback.", !expected.equals(actual) );
239: //} catch (Exception e){
240: // fail("Received Exception "+e.getClass()+ " : "+e.getMessage());
241: //}
242: }
243:
244: /**
245: * <B>11.6.1 Bean-managed transaction demarcation</B>
246: * <P>
247: * The Container must allow the enterprise bean instance to serially perform several transactions in a
248: * method.
249: * </P>
250: */
251: public void TODO_test07_serialTransactions() {
252: try {
253:
254: } catch (Exception e) {
255: fail("Received Exception " + e.getClass() + " : "
256: + e.getMessage());
257: }
258: }
259:
260: /**
261: * <B>11.6.1 Bean-managed transaction demarcation</B>
262: * <P>
263: * When an instance attempts to start a transaction using the
264: * begin() method of the javax.transaction.UserTransaction
265: * interface while the instance has not committed the previous
266: * transaction, the Container must throw the
267: * javax.transaction.NotSupportedException in the begin() method.
268: * </P>
269: */
270: public void TODO_test08_nestedTransactions() {
271: try {
272:
273: } catch (Exception e) {
274: fail("Received Exception " + e.getClass() + " : "
275: + e.getMessage());
276: }
277: }
278:
279: /**
280: * <B>11.6.1 Bean-managed transaction demarcation</B>
281: * <P>
282: * If a stateless session bean instance starts a transaction in a
283: * business method, it must commit the transaction before the
284: * business method returns. The Container must detect the case in
285: * which a transaction was started, but not completed, in the
286: * business method, and handle it as follows:
287: * <UL>
288: * <LI>Log this as an application error to alert the system administrator.
289: * <LI>Roll back the started transaction.
290: * <LI>Discard the instance of the session bean.
291: * <LI>Throw the java.rmi.RemoteException to the client.
292: * </UL>
293: * </P>
294: */
295: public void TODO_test09_beginWithNoCommit() {
296: try {
297:
298: } catch (Exception e) {
299: fail("Received Exception " + e.getClass() + " : "
300: + e.getMessage());
301: }
302: }
303:
304: /**
305: * <B>11.6.1 Bean-managed transaction demarcation</B>
306: * <P>
307: * The actions performed by the Container for an instance with bean-managed transaction are summarized
308: * by the following table. T1 is a transaction associated with a client request, T2 is a transaction that is cur-rently
309: * associated with the instance (i.e. a transaction that was started but not completed by a previous
310: * business method).
311: * </P>
312: * <PRE>
313: * =========================================================================
314: * Container's actions for methods of beans with bean-managed transaction
315: * =========================================================================
316: *
317: * | IF | AND | THEN
318: * scenario | Client's | Transaction currently | Transaction associated
319: * | transaction | associated with instance | with the method is
320: * ___________|_____________|__________________________|________________________
321: * | | |
322: * 1 | none | none | none
323: * ___________|_____________|__________________________|________________________
324: * | | |
325: * 2 | T1 | none | none
326: * ___________|_____________|__________________________|________________________
327: * | | |
328: * 3 | none | T2 | T2
329: * ___________|_____________|__________________________|________________________
330: * | | |
331: * 4 | T1 | T2 | T2
332: * ___________|_____________|__________________________|________________________
333: * </PRE>
334: * <P>
335: * If the client request is not associated with a transaction and the instance is not associated with a
336: * transaction, the container invokes the instance with an unspecified transaction context.
337: * </P>
338: * <P>--------------------------------------------------------</P>
339: * <P>
340: * Test scenario 1: none none<BR>
341: * If the client's transaction is none and the transaction currently
342: * associated with instance none then the transaction associated with the method is none.
343: * </P>
344: */
345: public void TODO_test10_scenario1_NoneNone() {
346: try {
347:
348: } catch (Exception e) {
349: fail("Received Exception " + e.getClass() + " : "
350: + e.getMessage());
351: }
352: }
353:
354: /**
355: * <B>11.6.1 Bean-managed transaction demarcation</B>
356: * <P>
357: * The actions performed by the Container for an instance with bean-managed transaction are summarized
358: * by the following table. T1 is a transaction associated with a client request, T2 is a transaction that is cur-rently
359: * associated with the instance (i.e. a transaction that was started but not completed by a previous
360: * business method).
361: * </P>
362: * <PRE>
363: * =========================================================================
364: * Container's actions for methods of beans with bean-managed transaction
365: * =========================================================================
366: *
367: * | IF | AND | THEN
368: * scenario | Client's | Transaction currently | Transaction associated
369: * | transaction | associated with instance | with the method is
370: * ___________|_____________|__________________________|________________________
371: * | | |
372: * 1 | none | none | none
373: * ___________|_____________|__________________________|________________________
374: * | | |
375: * 2 | T1 | none | none
376: * ___________|_____________|__________________________|________________________
377: * | | |
378: * 3 | none | T2 | T2
379: * ___________|_____________|__________________________|________________________
380: * | | |
381: * 4 | T1 | T2 | T2
382: * ___________|_____________|__________________________|________________________
383: * </PRE>
384: * <P>
385: * If the client is associated with a transaction T1, and the instance is not associated with a transaction,
386: * the container suspends the client's transaction association and invokes the method with
387: * an unspecified transaction context. The container resumes the client's ntransaction association
388: * (T1) when the method completes.
389: * </P>
390: * <P>--------------------------------------------------------</P>
391: * <P>
392: * Test scenario 2: T1 none<BR>
393: * </P>
394: */
395: public void TODO_test11_scenario2_T1None() {
396: try {
397:
398: } catch (Exception e) {
399: fail("Received Exception " + e.getClass() + " : "
400: + e.getMessage());
401: }
402: }
403:
404: }
|