KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.resource.spi.ManagedConnectionFactory JavaDoc;
25 import javax.transaction.Transaction JavaDoc;
26 import javax.transaction.TransactionManager JavaDoc;
27
28 import junit.framework.TestCase;
29
30 import org.jboss.logging.Logger;
31 import org.jboss.resource.connectionmanager.BaseConnectionManager2;
32 import org.jboss.resource.connectionmanager.CachedConnectionManager;
33 import org.jboss.resource.connectionmanager.InternalManagedConnectionPool;
34 import org.jboss.resource.connectionmanager.JBossManagedConnectionPool;
35 import org.jboss.resource.connectionmanager.ManagedConnectionPool;
36 import org.jboss.resource.connectionmanager.NoTxConnectionManager;
37 import org.jboss.resource.connectionmanager.PreFillPoolSupport;
38 import org.jboss.resource.connectionmanager.TransactionSynchronizer;
39 import org.jboss.resource.connectionmanager.TxConnectionManager;
40 import org.jboss.test.jca.adapter.TestConnection;
41 import org.jboss.test.jca.adapter.TestConnectionRequestInfo;
42 import org.jboss.test.jca.adapter.TestManagedConnection;
43 import org.jboss.test.jca.adapter.TestManagedConnectionFactory;
44 import org.jboss.test.jca.support.PoolHelper;
45 import org.jboss.test.jca.support.PoolHelper.PoolType;
46 import org.jboss.tm.TransactionManagerLocator;
47
48 /**
49  * Unit Tests for pooling strategies
50  *
51  * @author <a HREF="mailto:adrian@jboss.org">Adrian Brock</a>
52  * @author <a HREF="mailto:weston.price@jboss.com">Weston Price</a>
53  * @version $Revision: 45412 $
54  */

55 public class PoolingUnitTestCase extends TestCase
56 {
57
58    Logger log = Logger.getLogger(getClass());
59
60    static CachedConnectionManager ccm = new CachedConnectionManager();
61    static TransactionManager JavaDoc tm = TransactionManagerLocator.getInstance().locate();
62    static TestConnectionRequestInfo cri1 = new TestConnectionRequestInfo("info1");
63    static TestConnectionRequestInfo cri2 = new TestConnectionRequestInfo("info2");
64    
65    public PoolingUnitTestCase (String JavaDoc name)
66    {
67       super(name);
68    }
69
70    private ManagedConnectionPool getPool(PoolType type, boolean noTxnSeperatePools, ManagedConnectionFactory JavaDoc mcf, InternalManagedConnectionPool.PoolParams pp){
71       
72       return PoolHelper.getManagedConnectionPool(type, mcf, noTxnSeperatePools, pp, log);
73    }
74
75    private ManagedConnectionPool getOnePool(int maxSize)
76       throws Exception JavaDoc
77    {
78      
79       InternalManagedConnectionPool.PoolParams pp = new InternalManagedConnectionPool.PoolParams();
80       pp.minSize = 0;
81       pp.maxSize = maxSize;
82       pp.blockingTimeout = 10000;
83       pp.idleTimeout = 0;
84       ManagedConnectionFactory JavaDoc mcf = new TestManagedConnectionFactory();
85       ManagedConnectionPool poolingStrategy = getPool(PoolHelper.PoolType.ONE_POOL, false, mcf, pp);
86       return poolingStrategy;
87    }
88
89    private ManagedConnectionPool getPoolByCri(int maxSize)
90       throws Exception JavaDoc
91    {
92       InternalManagedConnectionPool.PoolParams pp = new InternalManagedConnectionPool.PoolParams();
93       pp.minSize = 0;
94       pp.maxSize = maxSize;
95       pp.blockingTimeout = 10000;
96       pp.idleTimeout = 10000;
97       ManagedConnectionFactory JavaDoc mcf = new TestManagedConnectionFactory();
98       ManagedConnectionPool poolingStrategy = getPool(PoolType.CRI_POOL, false, mcf, pp);
99       return poolingStrategy;
100    }
101    
102    private BaseConnectionManager2 getNoTxCM(ManagedConnectionPool poolingStrategy)
103    {
104       TransactionSynchronizer.setTransactionManager(tm);
105       BaseConnectionManager2 cm = new NoTxConnectionManager(ccm, poolingStrategy);
106       poolingStrategy.setConnectionListenerFactory(cm);
107       return cm;
108    }
109    
110    private BaseConnectionManager2 getTxCM(ManagedConnectionPool poolingStrategy)
111    {
112       TransactionSynchronizer.setTransactionManager(tm);
113       BaseConnectionManager2 cm = new TxConnectionManager(ccm, poolingStrategy, tm);
114       poolingStrategy.setConnectionListenerFactory(cm);
115       return cm;
116    }
117    
118    private BaseConnectionManager2 getTxTrackCM(ManagedConnectionPool poolingStrategy)
119    {
120       TransactionSynchronizer.setTransactionManager(tm);
121       TxConnectionManager cm = new TxConnectionManager(ccm, poolingStrategy, tm);
122       cm.setTrackConnectionByTx(true);
123       poolingStrategy.setConnectionListenerFactory(cm);
124       return cm;
125    }
126    
127    private TestConnection allocate(BaseConnectionManager2 cm, TestConnectionRequestInfo cri) throws Exception JavaDoc
128    {
129       JBossManagedConnectionPool.BasePool mcp = (JBossManagedConnectionPool.BasePool) cm.getPoolingStrategy();
130       ManagedConnectionFactory JavaDoc mcf = mcp.getManagedConnectionFactory();
131       return (TestConnection) cm.allocateConnection(mcf, cri);
132    }
133
134    private void shutdown(ManagedConnectionPool mcp)
135    {
136       JBossManagedConnectionPool.BasePool pool = (JBossManagedConnectionPool.BasePool) mcp;
137       pool.shutdown();
138    }
139
140    protected void setUp()
141    {
142       log.debug("================> Start " + getName());
143    }
144
145    protected void tearDown()
146    {
147       log.debug("================> End " + getName());
148    }
149  
150    public void testOnePoolNoTx() throws Exception JavaDoc
151    {
152       ManagedConnectionPool mcp = getOnePool(1);
153       BaseConnectionManager2 cm = getNoTxCM(mcp);
154       try
155       {
156          doOnePool(cm);
157       }
158       finally
159       {
160          shutdown(mcp);
161       }
162    }
163
164    public void testOnePoolTx() throws Exception JavaDoc
165    {
166       ManagedConnectionPool mcp = getOnePool(1);
167       BaseConnectionManager2 cm = getTxCM(mcp);
168       try
169       {
170          // Test before a transaction
171
doOnePool(cm);
172          tm.begin();
173          // Test during a transaction
174
doOnePool(cm);
175          tm.commit();
176          // Test after a transaction
177
doOnePool(cm);
178       }
179       finally
180       {
181          shutdown(mcp);
182       }
183    }
184
185    public void testOnePoolTxTrack() throws Exception JavaDoc
186    {
187       ManagedConnectionPool mcp = getOnePool(1);
188       BaseConnectionManager2 cm = getTxTrackCM(mcp);
189       try
190       {
191          // Test before a transaction
192
doOnePool(cm);
193          tm.begin();
194          // Test during a transaction
195
doOnePool(cm);
196          tm.commit();
197          // Test after a transaction
198
doOnePool(cm);
199       }
200       finally
201       {
202          shutdown(mcp);
203       }
204    }
205
206    public void testTrackConnectionByTx() throws Exception JavaDoc
207    {
208       ManagedConnectionPool mcp = getOnePool(2);
209       BaseConnectionManager2 cm = getTxTrackCM(mcp);
210       try
211       {
212          tm.begin();
213          TestConnection c1 = allocate(cm, cri1);
214          TestManagedConnection mc1 = c1.getMC();
215          c1.close();
216          TestConnection c2 = allocate(cm, cri1);
217          TestManagedConnection mc2 = c2.getMC();
218          c2.close();
219          assertTrue("Connections should be equal in same transaction", mc1.equals(mc2));
220          Transaction JavaDoc tx1 = tm.suspend();
221          tm.begin();
222          c2 = allocate(cm, cri1);
223          mc2 = c2.getMC();
224          c2.close();
225          assertTrue("Connections should not be equal in a different transaction", mc1.equals(mc2) == false);
226          tm.commit();
227          c2 = allocate(cm, cri1);
228          mc2 = c2.getMC();
229          c2.close();
230          assertTrue("Connections should not be equal outside a transaction", mc1.equals(mc2) == false);
231          tm.resume(tx1);
232          c2 = allocate(cm, cri1);
233          mc2 = c2.getMC();
234          c2.close();
235          assertTrue("Connections should still be equal in same transaction", mc1.equals(mc2));
236          tm.commit();
237          assertTrue("All connections should be recycled", mcp.getAvailableConnectionCount() == 2);
238       }
239       finally
240       {
241          shutdown(mcp);
242       }
243    }
244
245    public void testTrackConnectionByTxAndCRI() throws Exception JavaDoc
246    {
247       ManagedConnectionPool mcp = getPoolByCri(2);
248       BaseConnectionManager2 cm = getTxTrackCM(mcp);
249       try
250       {
251          tm.begin();
252          TestConnection c1 = allocate(cm, cri1);
253          TestManagedConnection mc1 = c1.getMC();
254          c1.close();
255          TestConnection c2 = allocate(cm, cri1);
256          TestManagedConnection mc2 = c2.getMC();
257          c2.close();
258          assertTrue("Connections should be equal in same transaction and criteria", mc1.equals(mc2));
259          c2 = allocate(cm, cri2);
260          mc2 = c2.getMC();
261          c2.close();
262          assertTrue("Connections should not be equal in same transaction but different criteria", mc1.equals(mc2) == false);
263          tm.commit();
264       }
265       finally
266       {
267          shutdown(mcp);
268       }
269    }
270    
271    public void doOnePool(BaseConnectionManager2 cm) throws Exception JavaDoc
272    {
273       TestConnection c1 = allocate(cm, cri1);
274       TestManagedConnection mc1 = c1.getMC();
275       c1.close();
276       TestConnection c2 = allocate(cm, cri1);
277       TestManagedConnection mc2 = c2.getMC();
278       c2.close();
279       assertEquals("Should get the same connection for same criteria", mc1, mc2);
280       c2 = allocate(cm, cri2);
281       mc2 = c2.getMC();
282       c2.close();
283       assertEquals("Should get the same connection for different cri", mc1, mc2);
284    }
285
286  
287   
288    
289    
290 }
291
Popular Tags