001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: A_TxAttribute.java 4406 2004-03-19 11:57:20Z benoitf $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.clients.transaction;
027:
028: import java.rmi.RemoteException;
029:
030: import javax.transaction.TransactionRequiredException;
031:
032: import org.objectweb.jonas.jtests.beans.transacted.Simple;
033: import org.objectweb.jonas.jtests.util.JTestCase;
034:
035: /**
036: * Transactional attribute test cases
037: * tests here are common to entity and session beans.
038: * beans used : transacted
039: * @author Ph.Coq, Ph.Durieux (jonas team)
040: */
041: public abstract class A_TxAttribute extends JTestCase {
042:
043: /**
044: * constructor
045: * @param name name of the test suite.
046: */
047: public A_TxAttribute(String name) {
048: super (name);
049: }
050:
051: /**
052: * Sets up the fixture, here load the beans if not loaded yet.
053: * This method is called before a test is executed.
054: */
055: protected void setUp() {
056: super .setUp();
057: useBeans("transacted", true);
058: }
059:
060: /**
061: * Get an instance of the bean.
062: * This method depends on the home used to get it.
063: * For entity bean, the arg is used to get a particular instance.
064: * For session beans, we get any session bean from the pool.
065: */
066: public abstract Simple getSimple(int i) throws Exception;
067:
068: // --------------------------------------------------------------------
069: // test cases
070: // --------------------------------------------------------------------
071:
072: /**
073: * Test of NotSupported attribute
074: * A business method with NotSupported is called outside TX
075: * this method returns if the thread is associated to a transaction
076: *
077: * the expected value is false
078: */
079: public void testNotSupported() throws Exception {
080:
081: Simple s = getSimple(10);
082: assertEquals(false, s.opwith_notsupported());
083: s.remove();
084: }
085:
086: /**
087: * Test of RequiresNew attribute
088: * A business method with RequiresNew is called outside TX
089: * this method returns if the thread is associated to a transaction
090: *
091: * the expected value is true
092: */
093: public void testRequiresNew() throws Exception {
094:
095: Simple s = getSimple(11);
096: assertEquals(true, s.opwith_requires_new());
097: s.remove();
098: }
099:
100: /**
101: * Test of Required attribute
102: * A business method with Required is called outside TX
103: * this method returns if the thread is associated to a transaction
104: *
105: * the expected value is true
106: */
107: public void testRequired() throws Exception {
108:
109: Simple s = getSimple(12);
110: assertEquals(true, s.opwith_required());
111: s.remove();
112: }
113:
114: /**
115: * Test a "Required" method calling a "Requires_new" method.
116: * the expected value is true
117: */
118: public void testRequiredRequiresNew() throws Exception {
119:
120: Simple s = getSimple(12);
121: assertEquals(true, s.required_call_requires_new());
122: s.remove();
123: }
124:
125: /**
126: * Test a "Required" method calling a "Requires_new" method on
127: * another bean instance.
128: * the expected value is true
129: */
130: public void testRequiredRequiresNew2() throws Exception {
131:
132: Simple s = getSimple(20);
133: Simple s2 = getSimple(21);
134: assertEquals(true, s.call_requires_new_on(s2));
135: s2.remove();
136: s.remove();
137: }
138:
139: /**
140: * Test of Mandatory attribute
141: * A business method with Mandatory is called outside TX
142: * this method returns if the thread is associated to a transaction
143: *
144: * A javax.transaction.TransactionRequiredException must be received
145: */
146:
147: public void testMandatory() throws Exception {
148:
149: Simple s = getSimple(13);
150: try {
151: s.opwith_mandatory();
152: fail("mandatory: should raise exception");
153: } catch (javax.transaction.TransactionRequiredException e) {
154: } catch (RemoteException e) {
155: assertTrue(e.detail instanceof TransactionRequiredException);
156: }
157: s.remove();
158: }
159:
160: /**
161: * Test of Never attribute
162: * A business method with Never is called outside TX
163: * this method returns if the thread is associated to a transaction
164: *
165: * the expected value is false
166: */
167: public void testNever() throws Exception {
168:
169: Simple s = getSimple(14);
170: assertEquals(false, s.opwith_never());
171: s.remove();
172: }
173:
174: /**
175: * Test of Supports attribute
176: * A business method with Supports is called outside TX
177: * this method returns if the thread is associated to a transaction
178: *
179: * the expected value is false
180: */
181: public void testSupports() throws Exception {
182:
183: Simple s = getSimple(15);
184: assertEquals(false, s.opwith_supports());
185: s.remove();
186: }
187:
188: /**
189: * Test of NotSupported attribute
190: * A business method with NotSupported is called inside TX
191: * this method returns if the thread is associated to a transaction
192: *
193: * the expected value is false
194: */
195: public void testNotSupportedTx() throws Exception {
196:
197: Simple s = getSimple(20);
198: utx.begin();
199: try {
200: assertEquals(false, s.opwith_notsupported());
201: } finally {
202: utx.rollback();
203: s.remove();
204: }
205: }
206:
207: /**
208: * Test of RequiresNew attribute
209: * A business method with RequiresNew is called inside TX
210: * this method returns if the thread is associated to a transaction
211: *
212: * the expected value is true
213: */
214: public void testRequiresNewTx() throws Exception {
215: Simple s = getSimple(21);
216: utx.begin();
217: try {
218: assertEquals(true, s.opwith_requires_new());
219: } finally {
220: utx.rollback();
221: s.remove();
222: }
223: }
224:
225: /**
226: * Test of Required attribute
227: * A business method with Required is called inside TX
228: * this method returns if the thread is associated to a transaction
229: *
230: * the expected value is true
231: */
232: public void testRequiredTx() throws Exception {
233:
234: Simple s = getSimple(22);
235: utx.begin();
236: try {
237: assertEquals(true, s.opwith_required());
238: } finally {
239: utx.rollback();
240: s.remove();
241: }
242:
243: }
244:
245: /**
246: * Test of Mandatory attribute
247: * A business method with Mandatory is called inside TX
248: * this method returns if the thread is associated to a transaction
249: *
250: * the expected value is true
251: */
252: public void testMandatoryTx() throws Exception {
253:
254: Simple s = getSimple(23);
255: utx.begin();
256: try {
257: assertEquals(true, s.opwith_mandatory());
258: } finally {
259: utx.rollback();
260: s.remove();
261: }
262: }
263:
264: /**
265: * Test of Never attribute
266: * A business method with Mandatory is called inside TX
267: * this method returns if the thread is associated to a transaction
268: *
269: * A java.rmi.RemoteException must be received
270: */
271: public void testNeverTx() throws Exception {
272:
273: Simple s = getSimple(24);
274: utx.begin();
275: try {
276: s.opwith_never();
277: fail("never: should raise exception");
278: } catch (RemoteException e) {
279: } finally {
280: utx.rollback();
281: s.remove();
282: }
283: }
284:
285: /**
286: * Test of Supports attribute
287: * A business method with Supports is called inside TX
288: * this method returns if the thread is associated to a transaction
289: *
290: * the expected value is true
291: */
292: public void testSupportsTx() throws Exception {
293:
294: Simple s = getSimple(25);
295: utx.begin();
296: try {
297: assertEquals(true, s.opwith_supports());
298: } finally {
299: utx.rollback();
300: s.remove();
301: }
302:
303: }
304:
305: /**
306: * Test the sequence of several calls to methods
307: * with different transactional contexts
308: */
309: public void testNoTx() throws Exception {
310:
311: Simple s = getSimple(1);
312: assertEquals(false, s.opwith_notsupported());
313: assertEquals(true, s.opwith_requires_new());
314: assertEquals(true, s.opwith_required());
315: assertEquals(false, s.opwith_never());
316: assertEquals(false, s.opwith_supports());
317: s.remove();
318: }
319:
320: }
|