001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.tm.test;
023:
024: import javax.transaction.Transaction;
025: import javax.transaction.TransactionManager;
026:
027: import org.jboss.test.JBossTestCase;
028: import org.jboss.tm.TransactionLocal;
029: import org.jboss.tm.TxManager;
030:
031: public class TransactionLocalUnitTestCase extends JBossTestCase {
032: //protected TransactionManager tm = TransactionManagerLocator.getInstance().locate();
033: protected TransactionManager tm = TxManager.getInstance();
034:
035: public void testSimpleSetGet() throws Exception {
036: TransactionLocal local = new TransactionLocal(tm);
037: tm.begin();
038: try {
039: local.set("Simple");
040: assertEquals("Simple", local.get());
041: } finally {
042: tm.commit();
043: }
044: }
045:
046: public void testSimpleSetNull() throws Exception {
047: TransactionLocal local = new TransactionLocal(tm);
048: tm.begin();
049: try {
050: local.set(null);
051: } finally {
052: tm.commit();
053: }
054: }
055:
056: public void testSimpleGetWithNoInitial() throws Exception {
057: TransactionLocal local = new TransactionLocal(tm);
058: tm.begin();
059: try {
060: assertEquals(null, local.get());
061: } finally {
062: tm.commit();
063: }
064: }
065:
066: public void testSimpleGetWithInitial() throws Exception {
067: TransactionLocal local = new TransactionLocal(tm) {
068: protected Object initialValue() {
069: return "Initial";
070: }
071: };
072: tm.begin();
073: try {
074: assertEquals("Initial", local.get());
075: } finally {
076: tm.commit();
077: }
078: }
079:
080: public void testGetNoTx() throws Exception {
081: TransactionLocal local = new TransactionLocal(tm);
082: assertEquals(null, local.get(null));
083: }
084:
085: public void testGetNoTxInitial() throws Exception {
086: TransactionLocal local = new TransactionLocal(tm) {
087: protected Object initialValue() {
088: return "Initial";
089: }
090: };
091: assertEquals("Initial", local.get());
092: }
093:
094: public void testSetNoTx() throws Exception {
095: TransactionLocal local = new TransactionLocal(tm);
096: try {
097: local.set("Something");
098: fail("Should not be here");
099: } catch (IllegalStateException expected) {
100: }
101: }
102:
103: public void testSimpleSetGetExplicit() throws Exception {
104: TransactionLocal local = new TransactionLocal(tm);
105: tm.begin();
106: Transaction tx = tm.suspend();
107: try {
108: local.set(tx, "Simple");
109: assertEquals("Simple", local.get(tx));
110: } finally {
111: tm.resume(tx);
112: tm.commit();
113: }
114: }
115:
116: public void testSimplePutNullExplicit() throws Exception {
117: TransactionLocal local = new TransactionLocal(tm);
118: tm.begin();
119: Transaction tx = tm.suspend();
120: try {
121: local.set(tx, null);
122: } finally {
123: tm.resume(tx);
124: tm.commit();
125: }
126: }
127:
128: public void testSimpleGetWithNoInitialExplicit() throws Exception {
129: TransactionLocal local = new TransactionLocal(tm);
130: tm.begin();
131: Transaction tx = tm.suspend();
132: try {
133: assertEquals(null, local.get(tx));
134: } finally {
135: tm.resume(tx);
136: tm.commit();
137: }
138: }
139:
140: public void testSimpleGetWithInitialExplicit() throws Exception {
141: TransactionLocal local = new TransactionLocal(tm) {
142: protected Object initialValue() {
143: return "Initial";
144: }
145: };
146: tm.begin();
147: Transaction tx = tm.suspend();
148: try {
149: assertEquals("Initial", local.get(tx));
150: } finally {
151: tm.resume(tx);
152: tm.commit();
153: }
154: }
155:
156: public void testGetNoTxExplicit() throws Exception {
157: TransactionLocal local = new TransactionLocal(tm);
158: assertEquals(null, local.get(null));
159: }
160:
161: public void testGetNoTxInitialExplicit() throws Exception {
162: TransactionLocal local = new TransactionLocal(tm) {
163: protected Object initialValue() {
164: return "Initial";
165: }
166: };
167: assertEquals("Initial", local.get(null));
168: }
169:
170: public void testSetNoTxExplicit() throws Exception {
171: TransactionLocal local = new TransactionLocal(tm);
172: try {
173: local.set(null, "Something");
174: fail("Should not be here");
175: } catch (IllegalStateException expected) {
176: }
177: }
178:
179: public void testGetAfterCommit() throws Exception {
180: TransactionLocal local = new TransactionLocal(tm);
181: tm.begin();
182: try {
183: local.set("Something");
184: } finally {
185: tm.commit();
186: }
187: assertEquals(null, local.get());
188: }
189:
190: public void testGetInitialAfterCommit() throws Exception {
191: TransactionLocal local = new TransactionLocal(tm) {
192: protected Object initialValue() {
193: return "Initial";
194: }
195: };
196: tm.begin();
197: try {
198: local.set("Something");
199: assertEquals("Something", local.get());
200: } finally {
201: tm.commit();
202: }
203: assertEquals("Initial", local.get());
204: }
205:
206: public void testGetMarkedRolledBack() throws Exception {
207: TransactionLocal local = new TransactionLocal(tm);
208: tm.begin();
209: tm.setRollbackOnly();
210: try {
211: assertEquals(null, local.get());
212: } finally {
213: tm.rollback();
214: }
215: }
216:
217: public void testGetInitialMarkedRolledBack() throws Exception {
218: TransactionLocal local = new TransactionLocal(tm) {
219: protected Object initialValue() {
220: return "Initial";
221: }
222: };
223: tm.begin();
224: tm.setRollbackOnly();
225: try {
226: assertEquals("Initial", local.get());
227: } finally {
228: tm.rollback();
229: }
230: }
231:
232: public void testSetMarkedRolledBack() throws Exception {
233: TransactionLocal local = new TransactionLocal(tm);
234: tm.begin();
235: tm.setRollbackOnly();
236: try {
237: local.set("Something");
238: assertEquals("Something", local.get());
239: } finally {
240: tm.rollback();
241: }
242: }
243:
244: public void testGetAfterComplete() throws Exception {
245: TransactionLocal local = new TransactionLocal(tm);
246: tm.begin();
247: Transaction tx = tm.getTransaction();
248: try {
249: local.set("Something");
250: } finally {
251: tx.commit();
252: }
253: assertEquals(null, local.get());
254: tm.suspend();
255: }
256:
257: public void testGetInitialAfterComplete() throws Exception {
258: TransactionLocal local = new TransactionLocal(tm) {
259: protected Object initialValue() {
260: return "Initial";
261: }
262: };
263: tm.begin();
264: Transaction tx = tm.getTransaction();
265: try {
266: local.set("Something");
267: assertEquals("Something", local.get());
268: } finally {
269: tx.commit();
270: }
271: assertEquals("Initial", local.get());
272: tm.suspend();
273: }
274:
275: public void testSuspendResume() throws Exception {
276: TransactionLocal local = new TransactionLocal(tm);
277: tm.begin();
278: Transaction tx1 = tm.getTransaction();
279: try {
280: local.set("Something");
281: assertEquals("Something", local.get());
282: tm.suspend();
283: tm.begin();
284: try {
285: Transaction tx2 = tm.getTransaction();
286: assertEquals(null, local.get());
287: assertEquals("Something", local.get(tx1));
288: tm.suspend();
289: tm.resume(tx1);
290: assertEquals("Something", local.get());
291: assertEquals(null, local.get(tx2));
292: tm.suspend();
293: tm.resume(tx2);
294: } finally {
295: tm.commit();
296: }
297: } finally {
298: tm.resume(tx1);
299: tm.commit();
300: }
301: }
302:
303: public TransactionLocalUnitTestCase(String name) {
304: super(name);
305: }
306: }
|