KickJava   Java API By Example, From Geeks To Geeks.

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


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.jca.test;
23
24 import java.util.HashSet JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Set JavaDoc;
27
28 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
29 import javax.transaction.RollbackException JavaDoc;
30 import javax.transaction.TransactionManager JavaDoc;
31 import javax.transaction.xa.XAException JavaDoc;
32
33 import junit.framework.Test;
34 import junit.framework.TestSuite;
35
36 import org.jboss.logging.Logger;
37 import org.jboss.resource.connectionmanager.CachedConnectionManager;
38 import org.jboss.resource.connectionmanager.InternalManagedConnectionPool;
39 import org.jboss.resource.connectionmanager.JBossManagedConnectionPool;
40 import org.jboss.resource.connectionmanager.ManagedConnectionPool;
41 import org.jboss.resource.connectionmanager.TransactionSynchronizer;
42 import org.jboss.resource.connectionmanager.TxConnectionManager;
43 import org.jboss.test.JBossTestCase;
44 import org.jboss.test.jca.adapter.TestConnection;
45 import org.jboss.test.jca.adapter.TestConnectionRequestInfo;
46 import org.jboss.test.jca.adapter.TestManagedConnection;
47 import org.jboss.test.jca.adapter.TestManagedConnectionFactory;
48 import org.jboss.tm.TransactionManagerLocator;
49 import org.jboss.tm.TxUtils;
50 import org.jboss.tm.usertx.client.ServerVMClientUserTransaction;
51
52 /**
53  * XATxConnectionManagerUnitTestCase.java
54  *
55  *
56  * Created: Mon Jan 14 00:43:40 2002
57  *
58  * @author <a HREF="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
59  * @version $Revision: 57146 $
60  */

61 public class XATxConnectionManagerUnitTestCase extends JBossTestCase
62 {
63    Logger poolLog = Logger.getLogger(JBossManagedConnectionPool.class);
64    Logger log = Logger.getLogger(getClass());
65
66    private TransactionManager JavaDoc tm;
67    private ServerVMClientUserTransaction ut;
68    private CachedConnectionManager ccm;
69    private TestManagedConnectionFactory mcf;
70    private TxConnectionManager cm;
71    private ConnectionRequestInfo JavaDoc cri;
72
73    private int poolSize = 5;
74
75    public static Test suite() throws Exception JavaDoc
76    {
77       // JBAS-3603, the execution order of tests in this test case is important
78
// so it must be defined explicitly when running under some JVMs
79
TestSuite suite = new TestSuite();
80       suite.addTest(new XATxConnectionManagerUnitTestCase("testGetConnection"));
81       suite.addTest(new XATxConnectionManagerUnitTestCase("testEnlistInExistingTx"));
82       suite.addTest(new XATxConnectionManagerUnitTestCase("testEnlistCheckedOutConnectionInNewTx"));
83       suite.addTest(new XATxConnectionManagerUnitTestCase("testReconnectConnectionHandlesOnNotification"));
84       suite.addTest(new XATxConnectionManagerUnitTestCase("testEnlistAfterMarkRollback"));
85       suite.addTest(new XATxConnectionManagerUnitTestCase("testBrokenConnectionAndTrackByTx"));
86       suite.addTest(new XATxConnectionManagerUnitTestCase("testFailedStartTx"));
87       suite.addTest(new XATxConnectionManagerUnitTestCase("testFailedEndTx"));
88
89       return suite;
90    }
91    
92    public XATxConnectionManagerUnitTestCase (String JavaDoc name)
93    {
94       super(name);
95    }
96
97    protected void setUp() throws Exception JavaDoc
98    {
99       log.debug("================> Start " + getName());
100       tm = TransactionManagerLocator.getInstance().locate();
101       TransactionSynchronizer.setTransactionManager(tm);
102       ut = new ServerVMClientUserTransaction(tm);
103       ccm = new CachedConnectionManager();
104       ut.registerTxStartedListener(ccm);
105
106       mcf = new TestManagedConnectionFactory();
107       InternalManagedConnectionPool.PoolParams pp = new InternalManagedConnectionPool.PoolParams();
108       pp.minSize = 0;
109       pp.maxSize = poolSize;
110       pp.blockingTimeout = 100;
111       pp.idleTimeout = 500;
112       ManagedConnectionPool poolingStrategy = new JBossManagedConnectionPool.OnePool(mcf, pp, false, poolLog);
113       cri = new TestConnectionRequestInfo();
114       cm = new TxConnectionManager(ccm, poolingStrategy, tm);
115       cm.setLocalTransactions(false);
116       poolingStrategy.setConnectionListenerFactory(cm);
117    }
118
119    protected void tearDown() throws Exception JavaDoc
120    {
121       JBossManagedConnectionPool.OnePool pool = (JBossManagedConnectionPool.OnePool) cm.getPoolingStrategy();
122       pool.shutdown();
123       ut = null;
124       log.debug("================> End " + getName());
125    }
126
127    public void testGetConnection() throws Exception JavaDoc
128    {
129       getLog().info("testGetConnection");
130       TestConnection c = (TestConnection)cm.allocateConnection(mcf, cri);
131       assertTrue("Connection is null", c != null);
132       c.close();
133    }
134
135    public void testEnlistInExistingTx() throws Exception JavaDoc
136    {
137       getLog().info("testEnlistInExistingTx");
138       ut.begin();
139       TestConnection c = null;
140       try
141       {
142          c = (TestConnection)cm.allocateConnection(mcf, cri);
143          try
144          {
145             assertTrue("Connection not enlisted in tx!", c.isInTx());
146          }
147          finally
148          {
149             c.close();
150          }
151          assertTrue("Connection still enlisted in tx!", !c.isInTx());
152       }
153       finally
154       {
155          if (TxUtils.isActive(ut))
156             ut.commit();
157          else
158             ut.rollback();
159       }
160       assertTrue("Connection still enlisted in tx!", !c.isInTx());
161    }
162
163    public void testEnlistCheckedOutConnectionInNewTx() throws Exception JavaDoc
164    {
165       getLog().info("testEnlistCheckedOutConnectionInNewTx");
166       Object JavaDoc key = this;
167       Set JavaDoc unshared = new HashSet JavaDoc();
168       ccm.pushMetaAwareObject(key, unshared);
169       try
170       {
171          TestConnection c = (TestConnection)cm.allocateConnection(mcf, cri);
172          try
173          {
174             assertTrue("Connection already enlisted in tx!", !c.isInTx());
175             ut.begin();
176             try
177             {
178                assertTrue("Connection not enlisted in tx!", c.isInTx());
179             }
180             finally
181             {
182                if (TxUtils.isActive(ut))
183                   ut.commit();
184                else
185                   ut.rollback();
186             }
187             assertTrue("Connection still enlisted in tx!", !c.isInTx());
188          }
189          finally
190          {
191             c.close();
192          }
193       }
194       finally
195       {
196          ccm.popMetaAwareObject(unshared);
197          
198       }
199    }
200
201    /** Tests the spec required behavior of reconnecting connection
202     * handles left open on return from an ejb method call. Since this
203     * behavior is normally turned off, we must set SpecCompliant on
204     * the ccm to true first.
205     */

206    public void testReconnectConnectionHandlesOnNotification() throws Exception JavaDoc
207    {
208       getLog().info("testReconnectConnectionHandlesOnNotification");
209       ccm.setSpecCompliant(true);
210       Object JavaDoc key1 = new Object JavaDoc();
211       Object JavaDoc key2 = new Object JavaDoc();
212       Set JavaDoc unshared = new HashSet JavaDoc();
213       ccm.pushMetaAwareObject(key1, unshared);
214       try
215       {
216          TestConnection c = null;
217          ut.begin();
218          try
219          {
220             ccm.pushMetaAwareObject(key2, unshared);
221             try
222             {
223                c = (TestConnection)cm.allocateConnection(mcf, cri);
224                assertTrue("Connection not enlisted in tx!", c.isInTx());
225             }
226             finally
227             {
228                ccm.popMetaAwareObject(unshared);//key2
229
}
230          }
231          finally
232          {
233             if (TxUtils.isActive(ut))
234                ut.commit();
235             else
236                ut.rollback();
237          }
238          ut.begin();
239          try
240          {
241             ccm.pushMetaAwareObject(key2, unshared);
242             try
243             {
244                assertTrue("Connection not enlisted in tx!", c.isInTx());
245             }
246             finally
247             {
248                ccm.popMetaAwareObject(unshared);//key2
249
}
250          }
251          finally
252          {
253             if (TxUtils.isActive(ut))
254                ut.commit();
255             else
256                ut.rollback();
257          }
258          assertTrue("Connection still enlisted in tx!", !c.isInTx());
259          ccm.pushMetaAwareObject(key2, unshared);
260          try
261          {
262             if (c != null)
263                c.close();
264          }
265          finally
266          {
267             ccm.popMetaAwareObject(unshared);//key2
268
}
269       }
270       finally
271       {
272          ccm.popMetaAwareObject(unshared);//key1
273
}
274   }
275
276   public void testEnlistAfterMarkRollback() throws Exception JavaDoc
277   {
278      // Get a transaction and mark it for rollback
279
tm.begin();
280      try
281      {
282         tm.setRollbackOnly();
283         // Allocate a connection upto the pool size all should fail
284
for (int i = 0; i < poolSize; ++i)
285         {
286            try
287            {
288               cm.allocateConnection(mcf, cri);
289               fail("Should not be allowed to allocate a connection with setRollbackOnly()");
290            }
291            catch (Exception JavaDoc e)
292            {
293               log.debug("Error allocating connection", e);
294            }
295         }
296      }
297      finally
298      {
299         tm.rollback();
300      }
301
302      // We should be able to get a connection now
303
testGetConnection();
304   }
305
306   public void testBrokenConnectionAndTrackByTx() throws Exception JavaDoc
307   {
308      getLog().info("testBrokenConnectionAndTrackByTx");
309      cm.setTrackConnectionByTx(true);
310      ut.begin();
311      TestConnection c = (TestConnection)cm.allocateConnection(mcf, cri);
312      c.fireConnectionError();
313      try
314      {
315         c.close();
316      }
317      catch (Exception JavaDoc ignored)
318      {
319      }
320      try
321      {
322         ut.commit();
323         fail("Should not be here");
324      }
325      catch (RollbackException JavaDoc expected)
326      {
327      }
328      assertTrue("Connection still enlisted in tx!", !c.isInTx());
329   }
330   
331   public void testFailedStartTx() throws Exception JavaDoc
332   {
333      TestManagedConnection.setFailInStart(false, XAException.XAER_RMFAIL);
334      tm.begin();
335      TestConnection conn = null;
336      TestConnection conn2 = null;
337      
338      try
339      {
340         assertTrue("Connection in pool!", cm.getPoolingStrategy().getConnectionCount() == 0);
341         conn = (TestConnection)cm.allocateConnection(mcf, cri);
342         
343         //One should have been created
344
assertTrue(cm.getPoolingStrategy().getConnectionCount() == 1);
345
346         TestManagedConnection.setFailInStart(true, XAException.XAER_RMFAIL);
347         
348         conn2 = (TestConnection)cm.allocateConnection(mcf, cri);
349         
350         fail("Should not be here.");
351      
352      }
353      catch (Throwable JavaDoc e)
354      {
355      }
356      conn.close();
357      tm.rollback();
358      assertTrue(conn2 == null);
359      assertTrue(cm.getPoolingStrategy().getConnectionCount() == 1);
360   }
361   
362   public void testFailedEndTx() throws Exception JavaDoc
363   {
364      TestManagedConnection.setFailInStart(false, XAException.XAER_RMFAIL);
365      TestManagedConnection.setFailInEnd(false, XAException.XAER_RMFAIL);
366      tm.begin();
367      TestConnection conn = null;
368      TestConnection conn2 = null;
369      
370      try
371      {
372         assertTrue("Connection in pool!", cm.getPoolingStrategy().getConnectionCount() == 0);
373         conn = (TestConnection)cm.allocateConnection(mcf, cri);
374         
375         //One should have been created
376
assertTrue(cm.getPoolingStrategy().getConnectionCount() == 1);
377         conn.close();
378
379         TestManagedConnection.setFailInEnd(true, XAException.XAER_RMFAIL);
380         
381         conn2 = (TestConnection)cm.allocateConnection(mcf, cri);
382         conn2.close();
383         tm.commit();
384         
385         fail("Should not be here.");
386      
387      }
388      catch (Throwable JavaDoc e)
389      {
390      }
391      
392      TestManagedConnection.setFailInEnd(false, 0);
393      TestManagedConnection.setFailInStart(false, 0);
394
395      assertTrue(conn2.getMCIsNull());
396      assertTrue("Connection count" + cm.getPoolingStrategy().getConnectionCount(), cm.getPoolingStrategy().getConnectionCount() == 0);
397      assertTrue("Failed endTx should destroy Connection", cm.getPoolingStrategy().getConnectionDestroyedCount() > 0);
398
399   }
400   
401   
402 }
403
Popular Tags