KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > tm > test > TransactionLocalUnitTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.tm.test;
23
24 import javax.transaction.Transaction JavaDoc;
25 import javax.transaction.TransactionManager JavaDoc;
26
27 import org.jboss.test.JBossTestCase;
28 import org.jboss.tm.TransactionLocal;
29 import org.jboss.tm.TransactionManagerLocator;
30
31 public class TransactionLocalUnitTestCase extends JBossTestCase
32 {
33    protected TransactionManager JavaDoc tm = TransactionManagerLocator.getInstance().locate();
34    
35    public void testSimpleSetGet() throws Exception JavaDoc
36    {
37       TransactionLocal local = new TransactionLocal(tm);
38       tm.begin();
39       try
40       {
41          local.set("Simple");
42          assertEquals("Simple", local.get());
43       }
44       finally
45       {
46          tm.commit();
47       }
48    }
49    
50    public void testSimpleSetNull() throws Exception JavaDoc
51    {
52       TransactionLocal local = new TransactionLocal(tm);
53       tm.begin();
54       try
55       {
56          local.set(null);
57       }
58       finally
59       {
60          tm.commit();
61       }
62    }
63    
64    public void testSimpleGetWithNoInitial() throws Exception JavaDoc
65    {
66       TransactionLocal local = new TransactionLocal(tm);
67       tm.begin();
68       try
69       {
70          assertEquals(null, local.get());
71       }
72       finally
73       {
74          tm.commit();
75       }
76    }
77    
78    public void testSimpleGetWithInitial() throws Exception JavaDoc
79    {
80       TransactionLocal local = new TransactionLocal(tm)
81       {
82          protected Object JavaDoc initialValue()
83          {
84             return "Initial";
85          }
86       };
87       tm.begin();
88       try
89       {
90          assertEquals("Initial", local.get());
91       }
92       finally
93       {
94          tm.commit();
95       }
96    }
97    
98    public void testGetNoTx() throws Exception JavaDoc
99    {
100       TransactionLocal local = new TransactionLocal(tm);
101       assertEquals(null, local.get(null));
102    }
103    
104    public void testGetNoTxInitial() throws Exception JavaDoc
105    {
106       TransactionLocal local = new TransactionLocal(tm)
107       {
108          protected Object JavaDoc initialValue()
109          {
110             return "Initial";
111          }
112       };
113       assertEquals("Initial", local.get());
114    }
115    
116    public void testSetNoTx() throws Exception JavaDoc
117    {
118       TransactionLocal local = new TransactionLocal(tm);
119       try
120       {
121          local.set("Something");
122          fail("Should not be here");
123       }
124       catch (IllegalStateException JavaDoc expected)
125       {
126       }
127    }
128    
129    public void testSimpleSetGetExplicit() throws Exception JavaDoc
130    {
131       TransactionLocal local = new TransactionLocal(tm);
132       tm.begin();
133       Transaction JavaDoc tx = tm.suspend();
134       try
135       {
136          local.set(tx, "Simple");
137          assertEquals("Simple", local.get(tx));
138       }
139       finally
140       {
141          tm.resume(tx);
142          tm.commit();
143       }
144    }
145    
146    public void testSimplePutNullExplicit() throws Exception JavaDoc
147    {
148       TransactionLocal local = new TransactionLocal(tm);
149       tm.begin();
150       Transaction JavaDoc tx = tm.suspend();
151       try
152       {
153          local.set(tx, null);
154       }
155       finally
156       {
157          tm.resume(tx);
158          tm.commit();
159       }
160    }
161    
162    public void testSimpleGetWithNoInitialExplicit() throws Exception JavaDoc
163    {
164       TransactionLocal local = new TransactionLocal(tm);
165       tm.begin();
166       Transaction JavaDoc tx = tm.suspend();
167       try
168       {
169          assertEquals(null, local.get(tx));
170       }
171       finally
172       {
173          tm.resume(tx);
174          tm.commit();
175       }
176    }
177    
178    public void testSimpleGetWithInitialExplicit() throws Exception JavaDoc
179    {
180       TransactionLocal local = new TransactionLocal(tm)
181       {
182          protected Object JavaDoc initialValue()
183          {
184             return "Initial";
185          }
186       };
187       tm.begin();
188       Transaction JavaDoc tx = tm.suspend();
189       try
190       {
191          assertEquals("Initial", local.get(tx));
192       }
193       finally
194       {
195          tm.resume(tx);
196          tm.commit();
197       }
198    }
199    
200    public void testGetNoTxExplicit() throws Exception JavaDoc
201    {
202       TransactionLocal local = new TransactionLocal(tm);
203       assertEquals(null, local.get(null));
204    }
205    
206    public void testGetNoTxInitialExplicit() throws Exception JavaDoc
207    {
208       TransactionLocal local = new TransactionLocal(tm)
209       {
210          protected Object JavaDoc initialValue()
211          {
212             return "Initial";
213          }
214       };
215       assertEquals("Initial", local.get(null));
216    }
217    
218    public void testSetNoTxExplicit() throws Exception JavaDoc
219    {
220       TransactionLocal local = new TransactionLocal(tm);
221       try
222       {
223          local.set(null, "Something");
224          fail("Should not be here");
225       }
226       catch (IllegalStateException JavaDoc expected)
227       {
228       }
229    }
230    
231    public void testGetAfterCommit() throws Exception JavaDoc
232    {
233       TransactionLocal local = new TransactionLocal(tm);
234       tm.begin();
235       try
236       {
237          local.set("Something");
238       }
239       finally
240       {
241          tm.commit();
242       }
243       assertEquals(null, local.get());
244    }
245    
246    public void testGetInitialAfterCommit() throws Exception JavaDoc
247    {
248       TransactionLocal local = new TransactionLocal(tm)
249       {
250          protected Object JavaDoc initialValue()
251          {
252             return "Initial";
253          }
254       };
255       tm.begin();
256       try
257       {
258          local.set("Something");
259          assertEquals("Something", local.get());
260       }
261       finally
262       {
263          tm.commit();
264       }
265       assertEquals("Initial", local.get());
266    }
267    
268    public void testGetMarkedRolledBack() throws Exception JavaDoc
269    {
270       TransactionLocal local = new TransactionLocal(tm);
271       tm.begin();
272       tm.setRollbackOnly();
273       try
274       {
275          assertEquals(null, local.get());
276       }
277       finally
278       {
279          tm.rollback();
280       }
281    }
282    
283    public void testGetInitialMarkedRolledBack() throws Exception JavaDoc
284    {
285       TransactionLocal local = new TransactionLocal(tm)
286       {
287          protected Object JavaDoc initialValue()
288          {
289             return "Initial";
290          }
291       };
292       tm.begin();
293       tm.setRollbackOnly();
294       try
295       {
296          assertEquals("Initial", local.get());
297       }
298       finally
299       {
300          tm.rollback();
301       }
302    }
303    
304    public void testSetMarkedRolledBack() throws Exception JavaDoc
305    {
306       TransactionLocal local = new TransactionLocal(tm);
307       tm.begin();
308       tm.setRollbackOnly();
309       try
310       {
311          local.set("Something");
312          assertEquals("Something", local.get());
313       }
314       finally
315       {
316          tm.rollback();
317       }
318    }
319    
320    public void testGetAfterComplete() throws Exception JavaDoc
321    {
322       TransactionLocal local = new TransactionLocal(tm);
323       tm.begin();
324       Transaction JavaDoc tx = tm.getTransaction();
325       try
326       {
327          local.set("Something");
328       }
329       finally
330       {
331          tx.commit();
332       }
333       assertEquals(null, local.get());
334       tm.suspend();
335    }
336    
337    public void testGetInitialAfterComplete() throws Exception JavaDoc
338    {
339       TransactionLocal local = new TransactionLocal(tm)
340       {
341          protected Object JavaDoc initialValue()
342          {
343             return "Initial";
344          }
345       };
346       tm.begin();
347       Transaction JavaDoc tx = tm.getTransaction();
348       try
349       {
350          local.set("Something");
351          assertEquals("Something", local.get());
352       }
353       finally
354       {
355          tx.commit();
356       }
357       assertEquals("Initial", local.get());
358       tm.suspend();
359    }
360    
361    public void testSuspendResume() throws Exception JavaDoc
362    {
363       TransactionLocal local = new TransactionLocal(tm);
364       tm.begin();
365       Transaction JavaDoc tx1 = tm.getTransaction();
366       try
367       {
368          local.set("Something");
369          assertEquals("Something", local.get());
370          tm.suspend();
371          tm.begin();
372          try
373          {
374             Transaction JavaDoc tx2 = tm.getTransaction();
375             assertEquals(null, local.get());
376             assertEquals("Something", local.get(tx1));
377             tm.suspend();
378             tm.resume(tx1);
379             assertEquals("Something", local.get());
380             assertEquals(null, local.get(tx2));
381             tm.suspend();
382             tm.resume(tx2);
383          }
384          finally
385          {
386             tm.commit();
387          }
388       }
389       finally
390       {
391          tm.resume(tx1);
392          tm.commit();
393       }
394    }
395    
396    public TransactionLocalUnitTestCase(String JavaDoc name)
397    {
398       super(name);
399    }
400 }
401
Popular Tags