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.jca.test;
023:
024: import javax.resource.spi.ManagedConnectionFactory;
025: import javax.transaction.Transaction;
026: import javax.transaction.TransactionManager;
027:
028: import junit.framework.TestCase;
029:
030: import org.jboss.logging.Logger;
031: import org.jboss.resource.connectionmanager.BaseConnectionManager2;
032: import org.jboss.resource.connectionmanager.CachedConnectionManager;
033: import org.jboss.resource.connectionmanager.InternalManagedConnectionPool;
034: import org.jboss.resource.connectionmanager.JBossManagedConnectionPool;
035: import org.jboss.resource.connectionmanager.ManagedConnectionPool;
036: import org.jboss.resource.connectionmanager.NoTxConnectionManager;
037: import org.jboss.resource.connectionmanager.PreFillPoolSupport;
038: import org.jboss.resource.connectionmanager.TransactionSynchronizer;
039: import org.jboss.resource.connectionmanager.TxConnectionManager;
040: import org.jboss.test.jca.adapter.TestConnection;
041: import org.jboss.test.jca.adapter.TestConnectionRequestInfo;
042: import org.jboss.test.jca.adapter.TestManagedConnection;
043: import org.jboss.test.jca.adapter.TestManagedConnectionFactory;
044: import org.jboss.test.jca.support.PoolHelper;
045: import org.jboss.test.jca.support.PoolHelper.PoolType;
046: import org.jboss.tm.TransactionManagerLocator;
047:
048: /**
049: * Unit Tests for pooling strategies
050: *
051: * @author <a href="mailto:adrian@jboss.org">Adrian Brock</a>
052: * @version $Revision: 57211 $
053: */
054: public class PoolingUnitTestCase extends TestCase {
055:
056: Logger log = Logger.getLogger(getClass());
057:
058: static CachedConnectionManager ccm = new CachedConnectionManager();
059: static TransactionManager tm = TransactionManagerLocator
060: .getInstance().locate();
061: static TestConnectionRequestInfo cri1 = new TestConnectionRequestInfo(
062: "info1");
063: static TestConnectionRequestInfo cri2 = new TestConnectionRequestInfo(
064: "info2");
065:
066: public PoolingUnitTestCase(String name) {
067: super (name);
068: }
069:
070: private ManagedConnectionPool getPool(PoolType type,
071: boolean noTxnSeperatePools, ManagedConnectionFactory mcf,
072: InternalManagedConnectionPool.PoolParams pp) {
073:
074: return PoolHelper.getManagedConnectionPool(type, mcf,
075: noTxnSeperatePools, pp, log);
076: }
077:
078: private ManagedConnectionPool getOnePool(int maxSize)
079: throws Exception {
080:
081: InternalManagedConnectionPool.PoolParams pp = new InternalManagedConnectionPool.PoolParams();
082: pp.minSize = 0;
083: pp.maxSize = maxSize;
084: pp.blockingTimeout = 10000;
085: pp.idleTimeout = 0;
086: ManagedConnectionFactory mcf = new TestManagedConnectionFactory();
087: ManagedConnectionPool poolingStrategy = getPool(
088: PoolHelper.PoolType.ONE_POOL, false, mcf, pp);
089: return poolingStrategy;
090: }
091:
092: private ManagedConnectionPool getPoolByCri(int maxSize)
093: throws Exception {
094: InternalManagedConnectionPool.PoolParams pp = new InternalManagedConnectionPool.PoolParams();
095: pp.minSize = 0;
096: pp.maxSize = maxSize;
097: pp.blockingTimeout = 10000;
098: pp.idleTimeout = 10000;
099: ManagedConnectionFactory mcf = new TestManagedConnectionFactory();
100: ManagedConnectionPool poolingStrategy = getPool(
101: PoolType.CRI_POOL, false, mcf, pp);
102: return poolingStrategy;
103: }
104:
105: private BaseConnectionManager2 getNoTxCM(
106: ManagedConnectionPool poolingStrategy) {
107: TransactionSynchronizer.setTransactionManager(tm);
108: BaseConnectionManager2 cm = new NoTxConnectionManager(ccm,
109: poolingStrategy);
110: poolingStrategy.setConnectionListenerFactory(cm);
111: return cm;
112: }
113:
114: private BaseConnectionManager2 getTxCM(
115: ManagedConnectionPool poolingStrategy) {
116: TransactionSynchronizer.setTransactionManager(tm);
117: BaseConnectionManager2 cm = new TxConnectionManager(ccm,
118: poolingStrategy, tm);
119: poolingStrategy.setConnectionListenerFactory(cm);
120: return cm;
121: }
122:
123: private BaseConnectionManager2 getTxTrackCM(
124: ManagedConnectionPool poolingStrategy) {
125: TransactionSynchronizer.setTransactionManager(tm);
126: TxConnectionManager cm = new TxConnectionManager(ccm,
127: poolingStrategy, tm);
128: cm.setTrackConnectionByTx(true);
129: poolingStrategy.setConnectionListenerFactory(cm);
130: return cm;
131: }
132:
133: private TestConnection allocate(BaseConnectionManager2 cm,
134: TestConnectionRequestInfo cri) throws Exception {
135: JBossManagedConnectionPool.BasePool mcp = (JBossManagedConnectionPool.BasePool) cm
136: .getPoolingStrategy();
137: ManagedConnectionFactory mcf = mcp
138: .getManagedConnectionFactory();
139: return (TestConnection) cm.allocateConnection(mcf, cri);
140: }
141:
142: private void shutdown(ManagedConnectionPool mcp) {
143: JBossManagedConnectionPool.BasePool pool = (JBossManagedConnectionPool.BasePool) mcp;
144: pool.shutdown();
145: }
146:
147: protected void setUp() {
148: log.debug("================> Start " + getName());
149: }
150:
151: protected void tearDown() {
152: log.debug("================> End " + getName());
153: }
154:
155: public void testOnePoolNoTx() throws Exception {
156: ManagedConnectionPool mcp = getOnePool(1);
157: BaseConnectionManager2 cm = getNoTxCM(mcp);
158: try {
159: doOnePool(cm);
160: } finally {
161: shutdown(mcp);
162: }
163: }
164:
165: public void testOnePoolTx() throws Exception {
166: ManagedConnectionPool mcp = getOnePool(1);
167: BaseConnectionManager2 cm = getTxCM(mcp);
168: try {
169: // Test before a transaction
170: doOnePool(cm);
171: tm.begin();
172: // Test during a transaction
173: doOnePool(cm);
174: tm.commit();
175: // Test after a transaction
176: doOnePool(cm);
177: } finally {
178: shutdown(mcp);
179: }
180: }
181:
182: public void testOnePoolTxTrack() throws Exception {
183: ManagedConnectionPool mcp = getOnePool(1);
184: BaseConnectionManager2 cm = getTxTrackCM(mcp);
185: try {
186: // Test before a transaction
187: doOnePool(cm);
188: tm.begin();
189: // Test during a transaction
190: doOnePool(cm);
191: tm.commit();
192: // Test after a transaction
193: doOnePool(cm);
194: } finally {
195: shutdown(mcp);
196: }
197: }
198:
199: public void testTrackConnectionByTx() throws Exception {
200: ManagedConnectionPool mcp = getOnePool(2);
201: BaseConnectionManager2 cm = getTxTrackCM(mcp);
202: try {
203: tm.begin();
204: TestConnection c1 = allocate(cm, cri1);
205: TestManagedConnection mc1 = c1.getMC();
206: c1.close();
207: TestConnection c2 = allocate(cm, cri1);
208: TestManagedConnection mc2 = c2.getMC();
209: c2.close();
210: assertTrue(
211: "Connections should be equal in same transaction",
212: mc1.equals(mc2));
213: Transaction tx1 = tm.suspend();
214: tm.begin();
215: c2 = allocate(cm, cri1);
216: mc2 = c2.getMC();
217: c2.close();
218: assertTrue(
219: "Connections should not be equal in a different transaction",
220: mc1.equals(mc2) == false);
221: tm.commit();
222: c2 = allocate(cm, cri1);
223: mc2 = c2.getMC();
224: c2.close();
225: assertTrue(
226: "Connections should not be equal outside a transaction",
227: mc1.equals(mc2) == false);
228: tm.resume(tx1);
229: c2 = allocate(cm, cri1);
230: mc2 = c2.getMC();
231: c2.close();
232: assertTrue(
233: "Connections should still be equal in same transaction",
234: mc1.equals(mc2));
235: tm.commit();
236: assertTrue("All connections should be recycled", mcp
237: .getAvailableConnectionCount() == 2);
238: } finally {
239: shutdown(mcp);
240: }
241: }
242:
243: public void testTrackConnectionByTxAndCRI() throws Exception {
244: ManagedConnectionPool mcp = getPoolByCri(2);
245: BaseConnectionManager2 cm = getTxTrackCM(mcp);
246: try {
247: tm.begin();
248: TestConnection c1 = allocate(cm, cri1);
249: TestManagedConnection mc1 = c1.getMC();
250: c1.close();
251: TestConnection c2 = allocate(cm, cri1);
252: TestManagedConnection mc2 = c2.getMC();
253: c2.close();
254: assertTrue(
255: "Connections should be equal in same transaction and criteria",
256: mc1.equals(mc2));
257: c2 = allocate(cm, cri2);
258: mc2 = c2.getMC();
259: c2.close();
260: assertTrue(
261: "Connections should not be equal in same transaction but different criteria",
262: mc1.equals(mc2) == false);
263: tm.commit();
264: } finally {
265: shutdown(mcp);
266: }
267: }
268:
269: public void doOnePool(BaseConnectionManager2 cm) throws Exception {
270: TestConnection c1 = allocate(cm, cri1);
271: TestManagedConnection mc1 = c1.getMC();
272: c1.close();
273: TestConnection c2 = allocate(cm, cri1);
274: TestManagedConnection mc2 = c2.getMC();
275: c2.close();
276: assertEquals(
277: "Should get the same connection for same criteria",
278: mc1, mc2);
279: c2 = allocate(cm, cri2);
280: mc2 = c2.getMC();
281: c2.close();
282: assertEquals(
283: "Should get the same connection for different cri",
284: mc1, mc2);
285: }
286:
287: }
|