KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > db > tests > DBConnectionPoolTest


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.core.db.tests;
66
67 import com.jcorporate.expresso.core.db.DBConnection;
68 import com.jcorporate.expresso.core.db.DBConnectionPool;
69 import com.jcorporate.expresso.core.db.DBException;
70 import com.jcorporate.expresso.core.db.exception.PoolFullException;
71 import com.jcorporate.expresso.core.misc.DateTime;
72 import com.jcorporate.expresso.services.test.ExpressoTestCase;
73 import com.jcorporate.expresso.services.test.TestSystemInitializer;
74 import junit.framework.TestSuite;
75
76
77 /**
78  * Unit test for the DBConnectionPool
79  *
80  * @author Michael Rimov
81  * @version $Revision: 1.2 $ on $Date: 2004/11/17 20:48:22 $
82  */

83
84 public class DBConnectionPoolTest extends ExpressoTestCase {
85     static final int NUM_STRESS_THREADS = 60;
86     static final int NUM_ITERATIONS = 20;
87
88     /**
89      * Default testing constructor
90      *
91      * @param testName the Test Name
92      */

93     public DBConnectionPoolTest(String JavaDoc testName)
94             throws Exception JavaDoc {
95         super(testName);
96     }
97
98     public static void main(String JavaDoc[] args)
99             throws Exception JavaDoc {
100         junit.textui.TestRunner.run(suite());
101     }
102
103     public static junit.framework.Test suite()
104             throws Exception JavaDoc {
105         return new TestSuite(DBConnectionPoolTest.class);
106     }
107
108     public void tearDown() {
109         try {
110             DBConnectionPool.getInstance(TestSystemInitializer.getTestContext()).disconnectAll();
111         } catch (DBException ex) {
112         }
113     }
114
115     /**
116      * Tests to make sure basic connection pool capabilities are there.
117      */

118     public void testConnectionPool() {
119         try {
120             System.out.println("\nBegin connection pool tests:" +
121                     DateTime.getDateTimeString());
122
123             DBConnectionPool myPool = DBConnectionPool.getInstance(TestSystemInitializer.getTestContext());
124             int maxConnections = myPool.getMaxConnections();
125             DBConnection connections[] = new DBConnection[maxConnections];
126             System.out.println("Maximum connections in the pool is: " + maxConnections);
127
128             for (int i = 0; i <= 5000; i++) {
129                 for (int j = 0; j < maxConnections; j++) {
130                     connections[j] = myPool.getConnection("Test " + j);
131                 }
132
133                 for (int j = 0; j < maxConnections; j++) {
134                     myPool.release(connections[j]);
135                 }
136             }
137
138             System.out.println("End connection pool tests:" +
139                     DateTime.getDateTimeString());
140         } catch (DBException ex) {
141             ex.printStackTrace();
142             fail("Caught an exception attempting DBConnectionPool Test " + ex.getMessage());
143         }
144
145     }
146
147     /**
148      * This test attempts to check to make sure the new DBConnection.release()
149      * method is working.
150      */

151     public void testNewRelease() {
152         try {
153             System.out.println("\nBegin new connection pool tests:" +
154                     DateTime.getDateTimeString());
155
156             DBConnectionPool myPool = DBConnectionPool.getInstance(TestSystemInitializer.getTestContext());
157             int maxConnections = myPool.getMaxConnections();
158             DBConnection connections[] = new DBConnection[maxConnections];
159             System.out.println("Maximum connections in the pool is: " + maxConnections);
160
161             for (int i = 0; i <= 100; i++) {
162                 for (int j = 0; j < maxConnections; j++) {
163                     connections[j] = myPool.getConnection("Test " + j);
164                     connections[j].execute("SELECT COUNT(*) FROM CONTROLLERSECURITY");
165                 }
166
167
168                 for (int j = 0; j < maxConnections; j++) {
169                     myPool.release(connections[j]);
170                 }
171             }
172
173             System.out.println("End new connection pool tests:" +
174                     DateTime.getDateTimeString());
175         } catch (DBException ex) {
176             ex.printStackTrace();
177             fail("Caught an exception attempting DBConnectionPool Test " + ex.getMessage());
178         }
179
180     }
181
182     /**
183      * In this test, we try to create the maximum number of connections and then
184      * attempt to allocate one more. It should throw an exception or return
185      * a connection.
186      */

187     public void testFullPool() {
188         try {
189             DBConnectionPool myPool = DBConnectionPool.getInstance(TestSystemInitializer.getTestContext());
190             int maxConnections = myPool.getMaxConnections();
191             DBConnection connections[] = new DBConnection[maxConnections];
192             System.out.println("Maximum connections in the pool is: " + maxConnections);
193
194             for (int j = 0; j < maxConnections; j++) {
195                 connections[j] = myPool.getConnection("Test " + j);
196             }
197
198             try {
199                 DBConnection breakConnection = myPool.getConnection("Breaking Connection");
200                 fail("The last connection SHOULD have thrown an exception");
201             } catch (DBException dbe) {
202                 assertTrue("Must receive a PoolFullException",
203                         dbe instanceof PoolFullException);
204             }
205
206             for (int j = 0; j < maxConnections; j++) {
207                 myPool.release(connections[j]);
208             }
209         } catch (DBException ex) {
210             ex.printStackTrace();
211             fail("Caught an exception attempting DBConnectionPool Test " + ex.getMessage());
212         }
213     }
214
215     /**
216      * Generates lots of threads that bang away on the connection pool attempting to
217      * get it to die.
218      */

219     public void testConnectionPoolStressTest() {
220         DBConnectionPoolStressThread theThreads[] = new DBConnectionPoolStressThread[NUM_STRESS_THREADS];
221
222         for (int i = 0; i < NUM_STRESS_THREADS; i++) {
223             theThreads[i] = new DBConnectionPoolStressThread("DBConnectionPool Thread " + i);
224         }
225
226         for (int i = 0; i < NUM_STRESS_THREADS; i++) {
227             theThreads[i].start();
228         }
229
230         for (int i = 0; i < NUM_STRESS_THREADS; i++) {
231             try {
232                 theThreads[i].join();
233             } catch (InterruptedException JavaDoc ex) {
234             }
235         }
236
237         boolean failures = false;
238         for (int i = 0; i < NUM_STRESS_THREADS; i++) {
239             System.out.println("Status for thread: " + theThreads[i].getName() +
240                     " number of pool fulls: " + theThreads[i].getNumPoolFulls() +
241                     " errors: " + theThreads[i].getErrors());
242
243             if (theThreads[i].getErrors() != null) {
244                 failures = true;
245             }
246         }
247
248         assertTrue("There were failures.", failures == false);
249
250     }
251 }
Popular Tags