KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 import javax.resource.ResourceException JavaDoc;
29 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
30 import javax.resource.spi.ManagedConnectionFactory JavaDoc;
31 import javax.security.auth.Subject JavaDoc;
32
33 import junit.framework.TestCase;
34
35 import org.jboss.logging.Logger;
36 import org.jboss.resource.connectionmanager.BaseConnectionManager2;
37 import org.jboss.resource.connectionmanager.CachedConnectionManager;
38 import org.jboss.resource.connectionmanager.ConnectionListener;
39 import org.jboss.resource.connectionmanager.InternalManagedConnectionPool;
40 import org.jboss.resource.connectionmanager.JBossManagedConnectionPool;
41 import org.jboss.resource.connectionmanager.ManagedConnectionPool;
42 import org.jboss.resource.connectionmanager.NoTxConnectionManager;
43 import org.jboss.resource.connectionmanager.JBossManagedConnectionPool.BasePool;
44 import org.jboss.test.jca.adapter.TestConnectionRequestInfo;
45 import org.jboss.test.jca.adapter.TestManagedConnectionFactory;
46
47 /**
48  * Unit Test for class ManagedConnectionPool
49  *
50  *
51  * Created: Wed Jan 2 00:06:35 2002
52  *
53  * @author <a HREF="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
54  * @version
55  */

56 public class BaseConnectionManagerUnitTestCase extends TestCase
57 {
58
59    Logger log = Logger.getLogger(getClass());
60
61
62    Subject JavaDoc subject = new Subject JavaDoc();
63    ConnectionRequestInfo JavaDoc cri = new TestConnectionRequestInfo();
64    CachedConnectionManager ccm = new CachedConnectionManager();
65
66
67    /**
68     * Creates a new <code>BaseConnectionManagerUnitTestCase</code> instance.
69     *
70     * @param name test name
71     */

72    public BaseConnectionManagerUnitTestCase (String JavaDoc name)
73    {
74       super(name);
75    }
76
77
78    private BaseConnectionManager2 getCM(
79       InternalManagedConnectionPool.PoolParams pp)
80       throws Exception JavaDoc
81    {
82       ManagedConnectionFactory JavaDoc mcf = new TestManagedConnectionFactory();
83       ManagedConnectionPool poolingStrategy = new JBossManagedConnectionPool.OnePool(mcf, pp, false, log);
84       BaseConnectionManager2 cm = new NoTxConnectionManager(ccm, poolingStrategy);
85       poolingStrategy.setConnectionListenerFactory(cm);
86       
87       
88       if(pp.prefill){
89          
90          BasePool bp = (BasePool)poolingStrategy;
91          bp.prefill(null, null, false);
92          
93       }
94       
95       return cm;
96    }
97
98    private void shutdown(BaseConnectionManager2 cm)
99    {
100       JBossManagedConnectionPool.OnePool pool = (JBossManagedConnectionPool.OnePool) cm.getPoolingStrategy();
101       pool.shutdown();
102    }
103
104    protected void setUp()
105    {
106       log.debug("================> Start " + getName());
107    }
108
109    protected void tearDown()
110    {
111       log.debug("================> End " + getName());
112    }
113    
114    public void testGetManagedConnections() throws Exception JavaDoc
115    {
116       InternalManagedConnectionPool.PoolParams pp = new InternalManagedConnectionPool.PoolParams();
117       pp.minSize = 0;
118       pp.maxSize = 5;
119       pp.blockingTimeout = 100;
120       pp.idleTimeout = 500;
121       BaseConnectionManager2 cm = getCM(pp);
122       try
123       {
124          ArrayList JavaDoc cs = new ArrayList JavaDoc();
125          for (int i = 0; i < pp.maxSize; i++)
126          {
127             ConnectionListener cl = cm.getManagedConnection(null, null);
128             assertTrue("Got a null connection!", cl.getManagedConnection() != null);
129             cs.add(cl);
130          } // end of for ()
131
assertTrue("Wrong number of connections counted: " + cm.getConnectionCount(), cm.getConnectionCount() == pp.maxSize);
132          try
133          {
134             cm.getManagedConnection(null, null);
135             fail("Got a connection more than maxSize!");
136          }
137          catch (ResourceException JavaDoc re)
138          {
139             //expected
140
} // end of try-catch
141
for (Iterator JavaDoc i = cs.iterator(); i.hasNext();)
142          {
143             cm.returnManagedConnection((ConnectionListener)i.next(), true);
144          } // end of for ()
145
assertTrue("Wrong number of connections counted: " + cm.getConnectionCount(), cm.getConnectionCount() == 0);
146       }
147       finally
148       {
149          shutdown(cm);
150       }
151    }
152
153    public void testIdleTimeout() throws Exception JavaDoc
154    {
155       InternalManagedConnectionPool.PoolParams pp = new InternalManagedConnectionPool.PoolParams();
156       pp.minSize = 0;
157       pp.maxSize = 5;
158       pp.blockingTimeout = 10;
159       pp.idleTimeout = 1000;
160       BaseConnectionManager2 cm = getCM(pp);
161       try
162       {
163          Collection JavaDoc mcs = new ArrayList JavaDoc(pp.maxSize);
164          for (int i = 0 ; i < pp.maxSize; i++)
165             mcs.add(cm.getManagedConnection(subject, cri));
166          for (Iterator JavaDoc i = mcs.iterator(); i.hasNext(); )
167             cm.returnManagedConnection((ConnectionListener)i.next(), false);
168
169          assertTrue("Wrong number of connections counted: " + cm.getConnectionCount(), cm.getConnectionCount() == pp.maxSize);
170          // Let the idle remover kick in
171
Thread.sleep(2500);
172          assertTrue("Wrong number of connections counted: " + cm.getConnectionCount(), cm.getConnectionCount() == 0);
173       }
174       finally
175       {
176          shutdown(cm);
177       }
178    }
179
180    public void testPartialIdleTimeout() throws Exception JavaDoc
181    {
182       InternalManagedConnectionPool.PoolParams pp = new InternalManagedConnectionPool.PoolParams();
183       pp.minSize = 0;
184       pp.maxSize = 5;
185       pp.blockingTimeout = 10;
186       pp.idleTimeout = 2000;
187       BaseConnectionManager2 cm = getCM(pp);
188       try
189       {
190          Collection JavaDoc mcs = new ArrayList JavaDoc(pp.maxSize);
191          for (int i = 0 ; i < pp.maxSize; i++)
192             mcs.add(cm.getManagedConnection(subject, cri));
193          for (Iterator JavaDoc i = mcs.iterator(); i.hasNext(); )
194             cm.returnManagedConnection((ConnectionListener)i.next(), false);
195
196          assertTrue("Wrong number of connections counted: " + cm.getConnectionCount(), cm.getConnectionCount() == pp.maxSize);
197          Thread.sleep(1500);
198          ConnectionListener cl = cm.getManagedConnection(subject, cri);
199          cm.returnManagedConnection(cl, false);
200
201          // Let the idle remover kick in
202
Thread.sleep(1500);
203          assertTrue("Wrong number of connections counted: " + cm.getConnectionCount(), cm.getConnectionCount() == 1);
204
205          // Let the idle remover kick in
206
Thread.sleep(1500);
207          assertTrue("Wrong number of connections counted: " + cm.getConnectionCount(), cm.getConnectionCount() == 0);
208       }
209       finally
210       {
211          shutdown(cm);
212       }
213    }
214
215    public void testFillToMin() throws Exception JavaDoc
216    {
217       InternalManagedConnectionPool.PoolParams pp = new InternalManagedConnectionPool.PoolParams();
218       pp.minSize = 3;
219       pp.maxSize = 5;
220       pp.blockingTimeout = 10;
221       pp.idleTimeout = 2000;
222       BaseConnectionManager2 cm = getCM(pp);
223       try
224       {
225          ConnectionListener cl = cm.getManagedConnection(subject, cri);
226          cm.returnManagedConnection(cl, false);
227          // Allow fill to min to work
228
Thread.sleep(1000);
229          assertTrue("Wrong number of connections counted: " + cm.getConnectionCount(), cm.getConnectionCount() == pp.minSize);
230          // Allow the idle remover to work
231
Thread.sleep(3000);
232          assertTrue("Wrong number of connections counted: " + cm.getConnectionCount(), cm.getConnectionCount() == pp.minSize);
233       }
234       finally
235       {
236          shutdown(cm);
237       }
238    }
239    
240    public void testPrefillPool() throws Exception JavaDoc{
241
242       InternalManagedConnectionPool.PoolParams pp = new InternalManagedConnectionPool.PoolParams();
243       pp.minSize = 1;
244       pp.maxSize = 5;
245       pp.blockingTimeout = 10;
246       pp.idleTimeout = 2000;
247       pp.prefill = true;
248       
249       BaseConnectionManager2 cm = getCM(pp);
250       //Sleep to let pool filler do it's job
251
Thread.sleep(3000);
252       assertTrue("Prefilled pool: " + cm.getConnectionCount(), pp.minSize == cm.getConnectionCount());
253       
254    }
255    
256    public void testNonStrictMinPool() throws Exception JavaDoc
257    {
258       InternalManagedConnectionPool.PoolParams pp = new InternalManagedConnectionPool.PoolParams();
259       pp.minSize = 10;
260       pp.maxSize = 15;
261       pp.blockingTimeout = 10;
262       pp.idleTimeout = 1000;
263       pp.prefill = true;
264       
265       BaseConnectionManager2 cm = getCM(pp);
266       JBossManagedConnectionPool.OnePool pool = (JBossManagedConnectionPool.OnePool) cm.getPoolingStrategy();
267       Thread.sleep(5000);
268       assertTrue("Non StrictMin pool should allow destroyed connections below minimum connections", pool.getConnectionDestroyedCount() > 0);
269
270    }
271    
272    public void testStrictMinPool() throws Exception JavaDoc
273    {
274       InternalManagedConnectionPool.PoolParams pp = new InternalManagedConnectionPool.PoolParams();
275       pp.minSize = 10;
276       pp.maxSize = 15;
277       pp.blockingTimeout = 10;
278       pp.idleTimeout = 1000;
279       pp.prefill = true;
280       pp.stictMin = true;
281       
282       BaseConnectionManager2 cm = getCM(pp);
283       JBossManagedConnectionPool.OnePool pool = (JBossManagedConnectionPool.OnePool) cm.getPoolingStrategy();
284       //Let Idle remover run
285
Thread.sleep(3500);
286       assertTrue("StrictMin pool should not destroy below minimum connections", pool.getConnectionDestroyedCount() == 0);
287             
288    }
289    
290    public void testMisConfiguredFillToMin() throws Exception JavaDoc
291    {
292       InternalManagedConnectionPool.PoolParams pp = new InternalManagedConnectionPool.PoolParams();
293       pp.minSize = 6;
294       pp.maxSize = 5;
295       pp.blockingTimeout = 10;
296       pp.idleTimeout = 2000;
297       BaseConnectionManager2 cm = getCM(pp);
298       try
299       {
300          ConnectionListener cl = cm.getManagedConnection(subject, cri);
301          cm.returnManagedConnection(cl, false);
302          // Allow fill to min to work
303
Thread.sleep(1000);
304          assertTrue("Wrong number of connections counted: " + cm.getConnectionCount(), cm.getConnectionCount() == pp.maxSize);
305          // Allow the idle remover to work
306
Thread.sleep(3000);
307          assertTrue("Wrong number of connections counted: " + cm.getConnectionCount(), cm.getConnectionCount() == pp.maxSize);
308       }
309       finally
310       {
311          shutdown(cm);
312       }
313    }
314
315    public void testChangedMaximum() throws Exception JavaDoc
316    {
317       InternalManagedConnectionPool.PoolParams pp = new InternalManagedConnectionPool.PoolParams();
318       pp.minSize = 0;
319       pp.maxSize = 5;
320       pp.blockingTimeout = 100;
321       pp.idleTimeout = 0;
322       BaseConnectionManager2 cm = getCM(pp);
323       JBossManagedConnectionPool.OnePool pool = (JBossManagedConnectionPool.OnePool) cm.getPoolingStrategy();
324       try
325       {
326          // Checkout all the connections
327
ArrayList JavaDoc cs = new ArrayList JavaDoc();
328          for (int i = 0; i < pp.maxSize; i++)
329          {
330             ConnectionListener cl = cm.getManagedConnection(null, null);
331             assertTrue("Got a null connection!", cl.getManagedConnection() != null);
332             cs.add(cl);
333          }
334          assertTrue("Wrong number of connections counted: " + cm.getConnectionCount(), cm.getConnectionCount() == pp.maxSize);
335
336          // Reconfigure
337
pp.maxSize = 6;
338          pool.flush();
339
340          // Put the connections back (should destroy/close them with no errors)
341
for (Iterator JavaDoc i = cs.iterator(); i.hasNext();)
342          {
343             cm.returnManagedConnection((ConnectionListener)i.next(), true);
344          }
345
346          // Checkout all the connections with the new maximum size
347
cs = new ArrayList JavaDoc();
348          for (int i = 0; i < pp.maxSize; i++)
349          {
350             ConnectionListener cl = cm.getManagedConnection(null, null);
351             assertTrue("Got a null connection!", cl.getManagedConnection() != null);
352             cs.add(cl);
353          }
354          assertTrue("Wrong number of connections counted: " + cm.getConnectionCount(), cm.getConnectionCount() == pp.maxSize);
355
356          try
357          {
358             cm.getManagedConnection(null, null);
359             fail("Got a connection more than maxSize!");
360          }
361          catch (ResourceException JavaDoc expected)
362          {
363          }
364
365          // Put the connections back into the new pool
366
for (Iterator JavaDoc i = cs.iterator(); i.hasNext();)
367             cm.returnManagedConnection((ConnectionListener)i.next(), true);
368          assertTrue("Wrong number of connections counted: " + cm.getConnectionCount(), cm.getConnectionCount() == 0);
369       }
370       finally
371       {
372          shutdown(cm);
373       }
374    }
375
376
377 }//
378
Popular Tags