KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jca > test > LocalTransactionTidyupUnitTestCase


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, 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.jca.test;
23
24 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
25 import javax.transaction.TransactionManager JavaDoc;
26
27 import org.jboss.logging.Logger;
28 import org.jboss.resource.connectionmanager.CachedConnectionManager;
29 import org.jboss.resource.connectionmanager.InternalManagedConnectionPool;
30 import org.jboss.resource.connectionmanager.JBossManagedConnectionPool;
31 import org.jboss.resource.connectionmanager.ManagedConnectionPool;
32 import org.jboss.resource.connectionmanager.TransactionSynchronizer;
33 import org.jboss.resource.connectionmanager.TxConnectionManager;
34 import org.jboss.test.JBossTestCase;
35 import org.jboss.test.jca.adapter.TestConnection;
36 import org.jboss.test.jca.adapter.TestConnectionRequestInfo;
37 import org.jboss.test.jca.adapter.TestManagedConnection;
38 import org.jboss.test.jca.adapter.TestManagedConnectionFactory;
39 import org.jboss.tm.TransactionManagerLocator;
40 import org.jboss.tm.usertx.client.ServerVMClientUserTransaction;
41
42 /**
43  * A LocalTransactionTidyupUnitTestCase.
44  *
45  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
46  * @version $Revision: 43427 $
47  */

48 public class LocalTransactionTidyupUnitTestCase extends JBossTestCase
49 {
50    Logger poolLog = Logger.getLogger(JBossManagedConnectionPool.class);
51    Logger log = Logger.getLogger(getClass());
52
53    private TransactionManager JavaDoc tm;
54    private ServerVMClientUserTransaction ut;
55    private CachedConnectionManager ccm;
56    private TestManagedConnectionFactory mcf;
57    private TxConnectionManager cm;
58    private ConnectionRequestInfo JavaDoc cri;
59
60    private int poolSize = 5;
61
62    public LocalTransactionTidyupUnitTestCase (String JavaDoc name)
63    {
64       super(name);
65    }
66
67    protected void setUp() throws Exception JavaDoc
68    {
69       log.debug("================> Start " + getName());
70       tm = TransactionManagerLocator.getInstance().locate();
71       TransactionSynchronizer.setTransactionManager(tm);
72       ut = new ServerVMClientUserTransaction(tm);
73       ccm = new CachedConnectionManager();
74       ut.registerTxStartedListener(ccm);
75
76       mcf = new TestManagedConnectionFactory();
77       InternalManagedConnectionPool.PoolParams pp = new InternalManagedConnectionPool.PoolParams();
78       pp.minSize = 0;
79       pp.maxSize = poolSize;
80       pp.blockingTimeout = 100;
81       pp.idleTimeout = 500;
82       ManagedConnectionPool poolingStrategy = new JBossManagedConnectionPool.OnePool(mcf, pp, false, poolLog);
83       cri = new TestConnectionRequestInfo();
84       cm = new TxConnectionManager(ccm, poolingStrategy, tm);
85       cm.setLocalTransactions(true);
86       poolingStrategy.setConnectionListenerFactory(cm);
87    }
88
89    protected void tearDown() throws Exception JavaDoc
90    {
91       JBossManagedConnectionPool.OnePool pool = (JBossManagedConnectionPool.OnePool) cm.getPoolingStrategy();
92       pool.shutdown();
93       ut = null;
94       log.debug("================> End " + getName());
95    }
96
97    public void testSimple() throws Exception JavaDoc
98    {
99       TestConnection c = (TestConnection)cm.allocateConnection(mcf, cri);
100       assertNotNull(c);
101       c.close();
102    }
103
104    public void testManualTransactionCommit() throws Exception JavaDoc
105    {
106       TestConnection c = (TestConnection)cm.allocateConnection(mcf, cri);
107       assertNotNull(c);
108       try
109       {
110          c.begin();
111          assertEquals(TestManagedConnection.LOCAL_TRANSACTION, c.getLocalState());
112          c.commit();
113          assertEquals(TestManagedConnection.LOCAL_COMMITTED, c.getLocalState());
114       }
115       finally
116       {
117          c.close();
118       }
119    }
120
121    public void testManualTransactionRollback() throws Exception JavaDoc
122    {
123       TestConnection c = (TestConnection)cm.allocateConnection(mcf, cri);
124       assertNotNull(c);
125       try
126       {
127          c.begin();
128          assertEquals(TestManagedConnection.LOCAL_TRANSACTION, c.getLocalState());
129          c.rollback();
130          assertEquals(TestManagedConnection.LOCAL_ROLLEDBACK, c.getLocalState());
131       }
132       finally
133       {
134          c.close();
135       }
136    }
137
138    public void testManualTransactionForgetToCommitRollback() throws Exception JavaDoc
139    {
140       TestConnection c = (TestConnection)cm.allocateConnection(mcf, cri);
141       assertNotNull(c);
142       try
143       {
144          c.begin();
145          assertEquals(TestManagedConnection.LOCAL_TRANSACTION, c.getLocalState());
146       }
147       finally
148       {
149          c.close();
150          assertEquals(TestManagedConnection.LOCAL_ROLLEDBACK, c.getLocalState());
151       }
152    }
153
154    public void testJTATransactionCommit() throws Exception JavaDoc
155    {
156       tm.begin();
157       TestConnection c = null;
158       try
159       {
160          c = (TestConnection)cm.allocateConnection(mcf, cri);
161          assertNotNull(c);
162          try
163          {
164             assertEquals(TestManagedConnection.LOCAL_TRANSACTION, c.getLocalState());
165          }
166          finally
167          {
168             c.close();
169             assertEquals(TestManagedConnection.LOCAL_TRANSACTION, c.getLocalState());
170          }
171       }
172       finally
173       {
174          tm.commit();
175          if (c != null)
176             assertEquals(TestManagedConnection.LOCAL_COMMITTED, c.getLocalState());
177       }
178    }
179
180    public void testJTATransactionRollback() throws Exception JavaDoc
181    {
182       tm.begin();
183       TestConnection c = null;
184       try
185       {
186          c = (TestConnection)cm.allocateConnection(mcf, cri);
187          assertNotNull(c);
188          try
189          {
190             assertEquals(TestManagedConnection.LOCAL_TRANSACTION, c.getLocalState());
191          }
192          finally
193          {
194             c.close();
195             assertEquals(TestManagedConnection.LOCAL_TRANSACTION, c.getLocalState());
196          }
197       }
198       finally
199       {
200          tm.rollback();
201          if (c != null)
202             assertEquals(TestManagedConnection.LOCAL_ROLLEDBACK, c.getLocalState());
203       }
204    }
205
206    public void testUserTransactionCommit() throws Exception JavaDoc
207    {
208       ut.begin();
209       TestConnection c = null;
210       try
211       {
212          c = (TestConnection)cm.allocateConnection(mcf, cri);
213          assertNotNull(c);
214          try
215          {
216             assertEquals(TestManagedConnection.LOCAL_TRANSACTION, c.getLocalState());
217          }
218          finally
219          {
220             c.close();
221             assertEquals(TestManagedConnection.LOCAL_TRANSACTION, c.getLocalState());
222          }
223       }
224       finally
225       {
226          ut.commit();
227          if (c != null)
228             assertEquals(TestManagedConnection.LOCAL_COMMITTED, c.getLocalState());
229       }
230    }
231
232    public void testUserTransactionRollback() throws Exception JavaDoc
233    {
234       ut.begin();
235       TestConnection c = null;
236       try
237       {
238          c = (TestConnection)cm.allocateConnection(mcf, cri);
239          assertNotNull(c);
240          try
241          {
242             assertEquals(TestManagedConnection.LOCAL_TRANSACTION, c.getLocalState());
243          }
244          finally
245          {
246             c.close();
247             assertEquals(TestManagedConnection.LOCAL_TRANSACTION, c.getLocalState());
248          }
249       }
250       finally
251       {
252          ut.rollback();
253          if (c != null)
254             assertEquals(TestManagedConnection.LOCAL_ROLLEDBACK, c.getLocalState());
255       }
256    }
257
258    public void testLazyUserTransactionCommit() throws Exception JavaDoc
259    {
260       ccm.pushMetaAwareObject(this, null);
261       try
262       {
263          TestConnection c = (TestConnection)cm.allocateConnection(mcf, cri);
264          assertNotNull(c);
265          try
266          {
267             assertEquals(TestManagedConnection.LOCAL_NONE, c.getLocalState());
268             ut.begin();
269             try
270             {
271                assertEquals(TestManagedConnection.LOCAL_TRANSACTION, c.getLocalState());
272             }
273             finally
274             {
275                ut.commit();
276                assertEquals(TestManagedConnection.LOCAL_COMMITTED, c.getLocalState());
277             }
278          }
279          finally
280          {
281             c.close();
282             assertEquals(TestManagedConnection.LOCAL_COMMITTED, c.getLocalState());
283          }
284       }
285       finally
286       {
287          ccm.popMetaAwareObject(null);
288       }
289    }
290
291    public void testLazyUserTransactionRollback() throws Exception JavaDoc
292    {
293       ccm.pushMetaAwareObject(this, null);
294       try
295       {
296          TestConnection c = (TestConnection)cm.allocateConnection(mcf, cri);
297          assertNotNull(c);
298          try
299          {
300             assertEquals(TestManagedConnection.LOCAL_NONE, c.getLocalState());
301             ut.begin();
302             try
303             {
304                assertEquals(TestManagedConnection.LOCAL_TRANSACTION, c.getLocalState());
305             }
306             finally
307             {
308                ut.rollback();
309                assertEquals(TestManagedConnection.LOCAL_ROLLEDBACK, c.getLocalState());
310             }
311          }
312          finally
313          {
314             c.close();
315             assertEquals(TestManagedConnection.LOCAL_ROLLEDBACK, c.getLocalState());
316          }
317       }
318       finally
319       {
320          ccm.popMetaAwareObject(null);
321       }
322    }
323 }
324
Popular Tags