KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > proxool > HouseKeeperTest


1 /*
2  * This software is released under a licence similar to the Apache Software Licence.
3  * See org.logicalcobwebs.proxool.package.html for details.
4  * The latest version is available at http://proxool.sourceforge.net
5  */

6 package org.logicalcobwebs.proxool;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10 import org.logicalcobwebs.proxool.admin.SnapshotIF;
11
12 import java.sql.Connection JavaDoc;
13 import java.sql.DriverManager JavaDoc;
14 import java.sql.Statement JavaDoc;
15 import java.sql.SQLException JavaDoc;
16 import java.util.Properties JavaDoc;
17
18 /**
19  * Test the house keeper in ConnectionPool
20  *
21  * @author bill
22  * @author $Author: billhorsman $ (current maintainer)
23  * @version $Revision: 1.13 $, $Date: 2006/03/24 00:17:32 $
24  * @since Proxool 0.8
25  */

26 public class HouseKeeperTest extends AbstractProxoolTest {
27
28     private static final Log LOG = LogFactory.getLog(HouseKeeperTest.class);
29
30     public HouseKeeperTest(String JavaDoc alias) {
31         super(alias);
32     }
33
34     /**
35      * Test that connections that remain active for longer than the configured
36      * time are closed (and destroyed) automatically.
37      */

38     public void testMaximumActiveTime() throws Exception JavaDoc {
39
40         ConnectionResetter.setTriggerResetException(true);
41         String JavaDoc testName = "maximumActiveTime";
42         String JavaDoc alias = testName;
43
44         String JavaDoc url = TestHelper.buildProxoolUrl(alias,
45                 TestConstants.HYPERSONIC_DRIVER,
46                 TestConstants.HYPERSONIC_TEST_URL);
47         Properties JavaDoc info = new Properties JavaDoc();
48         info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
49         info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD);
50         info.setProperty(ProxoolConstants.MAXIMUM_ACTIVE_TIME_PROPERTY, "1000");
51         info.setProperty(ProxoolConstants.MINIMUM_CONNECTION_COUNT_PROPERTY, "1");
52         info.setProperty(ProxoolConstants.TRACE_PROPERTY, "true");
53         info.setProperty(ProxoolConstants.HOUSE_KEEPING_SLEEP_TIME_PROPERTY, "1000");
54         ProxoolFacade.registerConnectionPool(url, info);
55
56         assertEquals("Shouldn't be any active connections yet", 0, ProxoolFacade.getSnapshot(alias, false).getServedCount());
57
58         final Connection JavaDoc connection = DriverManager.getConnection(url);
59         connection.setAutoCommit(false);
60         connection.createStatement().executeQuery(TestConstants.HYPERSONIC_TEST_SQL);
61         long start = System.currentTimeMillis();
62
63         assertEquals("We just opened 1 connection", 1, ProxoolFacade.getSnapshot(alias, false).getServedCount());
64
65         new ResultMonitor() {
66             public boolean check() throws Exception JavaDoc {
67                 return connection.isClosed();
68             }
69         }.getResult();
70         try {
71             Thread.sleep(3000);
72         } catch (InterruptedException JavaDoc e) {
73             LOG.debug("Awoken.");
74         }
75
76         long elapsed = System.currentTimeMillis() - start;
77         assertTrue("Connection has not been closed after " + elapsed + " milliseconds as expected", connection.isClosed());
78         assertEquals("Expected the connection to be inactive", 0, ProxoolFacade.getSnapshot(alias, false).getActiveConnectionCount());
79
80         try {
81             connection.createStatement().executeQuery(TestConstants.HYPERSONIC_TEST_SQL);
82             fail("Calling createStatement() on a closed connection should fail");
83         } catch (Exception JavaDoc e) {
84             // s'okay. We expected this
85
LOG.debug("Ignoring expected exception: " + e.getMessage());
86         }
87
88         // Now close the connection ourselves. It's already been closed by the House Keeper but nothing bad should
89
// happen if we do it again now.
90
connection.close();
91
92         // Let's see if the prototyper builds another one
93
try {
94             Thread.sleep(3000);
95         } catch (InterruptedException JavaDoc e) {
96             LOG.debug("Awoken.");
97         }
98         SnapshotIF snapshot = ProxoolFacade.getSnapshot(alias, false);
99         assertEquals("activeConnectionCount", 0, snapshot.getActiveConnectionCount());
100         assertEquals("availableConnectionCount", 1, snapshot.getAvailableConnectionCount());
101         assertEquals("connectionCount", 1, snapshot.getConnectionCount());
102
103     }
104
105     /**
106      * Test that connections that remain active for longer than the configured
107      * time are closed (and destroyed) automatically. Also, it gets errors during
108      * reset. We don't want the connectionCount to be decremented twice.
109      */

110     public void testMaximumActiveTimeWithResetFailure() throws Exception JavaDoc {
111
112         try {
113             ConnectionResetter.setTriggerResetException(true);
114             String JavaDoc testName = "maximumActiveTimeWithResetFailure";
115             String JavaDoc alias = testName;
116
117             String JavaDoc url = TestHelper.buildProxoolUrl(alias,
118                     TestConstants.HYPERSONIC_DRIVER,
119                     TestConstants.HYPERSONIC_TEST_URL);
120             Properties JavaDoc info = new Properties JavaDoc();
121             info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
122             info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD);
123             info.setProperty(ProxoolConstants.MAXIMUM_ACTIVE_TIME_PROPERTY, "1000");
124             info.setProperty(ProxoolConstants.TRACE_PROPERTY, "true");
125             info.setProperty(ProxoolConstants.HOUSE_KEEPING_SLEEP_TIME_PROPERTY, "1000");
126             ProxoolFacade.registerConnectionPool(url, info);
127
128             assertEquals("Shouldn't be any active connections yet", 0, ProxoolFacade.getSnapshot(alias, false).getServedCount());
129
130             final Connection JavaDoc connection = DriverManager.getConnection(url);
131             connection.setAutoCommit(false);
132             connection.createStatement().executeQuery(TestConstants.HYPERSONIC_TEST_SQL);
133             long start = System.currentTimeMillis();
134
135             assertEquals("We just opened 1 connection", 1, ProxoolFacade.getSnapshot(alias, false).getServedCount());
136
137             new ResultMonitor() {
138                 public boolean check() throws Exception JavaDoc {
139                     return connection.isClosed();
140                 }
141             }.getResult();
142             try {
143                 Thread.sleep(3000);
144             } catch (InterruptedException JavaDoc e) {
145                 LOG.debug("Awoken.");
146             }
147
148             long elapsed = System.currentTimeMillis() - start;
149             assertTrue("Connection has not been closed after " + elapsed + " milliseconds as expected", connection.isClosed());
150             assertEquals("Expected the connection to be inactive", 0, ProxoolFacade.getSnapshot(alias, false).getActiveConnectionCount());
151
152             try {
153                 connection.createStatement().executeQuery(TestConstants.HYPERSONIC_TEST_SQL);
154                 fail("Calling createStatement() on a closed connection should fail");
155             } catch (Exception JavaDoc e) {
156                 // s'okay. We expected this
157
LOG.debug("Ignoring expected exception: " + e.getMessage());
158             }
159
160             // Now close the connection ourselves. It's already been closed by the House Keeper but nothing bad should
161
// happen if we do it again now.
162
connection.close();
163
164             // Let's see if the prototyper builds another one
165
try {
166                 Thread.sleep(3000);
167             } catch (InterruptedException JavaDoc e) {
168                 LOG.debug("Awoken.");
169             }
170             SnapshotIF snapshot = ProxoolFacade.getSnapshot(alias, false);
171             assertEquals("activeConnectionCount", 0, snapshot.getActiveConnectionCount());
172             assertEquals("availableConnectionCount", 0, snapshot.getAvailableConnectionCount());
173             assertEquals("connectionCount", 0, snapshot.getConnectionCount());
174         } finally {
175             // Back to normal
176
ConnectionResetter.setTriggerResetException(false);
177         }
178
179     }
180
181     /**
182      * Test that house keeper destroys connections that fail configured
183      * the test sql
184      */

185     public void testHouseKeeperTestSql() throws Exception JavaDoc {
186
187         String JavaDoc testName = "houseKeeperTestSql";
188         String JavaDoc alias = testName;
189
190         String JavaDoc url = TestHelper.buildProxoolUrl(alias,
191                 TestConstants.HYPERSONIC_DRIVER,
192                 TestConstants.HYPERSONIC_TEST_URL);
193         Properties JavaDoc info = new Properties JavaDoc();
194         info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
195         info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD);
196         info.setProperty(ProxoolConstants.HOUSE_KEEPING_TEST_SQL_PROPERTY, "SELECT NOW");
197         info.setProperty(ProxoolConstants.HOUSE_KEEPING_SLEEP_TIME_PROPERTY, "1000");
198         ProxoolFacade.registerConnectionPool(url, info);
199
200         DriverManager.getConnection(url).close();
201
202         try {
203             Thread.sleep(3000);
204         } catch (InterruptedException JavaDoc e) {
205             LOG.debug("Awoken.");
206         }
207
208         DriverManager.getConnection(url).close();
209     }
210
211     /**
212      * Test that house keeper destroys connections that fail configured
213      * the test sql
214      */

215     public void testInvalidBeforeUse() throws Exception JavaDoc {
216
217         String JavaDoc testName = "invalidBeforeUse";
218         String JavaDoc alias = testName;
219
220         String JavaDoc url = TestHelper.buildProxoolUrl(alias,
221                 TestConstants.HYPERSONIC_DRIVER,
222                 TestConstants.HYPERSONIC_TEST_URL);
223         Properties JavaDoc info = new Properties JavaDoc();
224         info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
225         info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD);
226         info.setProperty(ProxoolConstants.HOUSE_KEEPING_TEST_SQL_PROPERTY, "Invalid test");
227         info.setProperty(ProxoolConstants.TEST_BEFORE_USE_PROPERTY, Boolean.TRUE.toString());
228         info.setProperty(ProxoolConstants.VERBOSE_PROPERTY, Boolean.TRUE.toString());
229         info.setProperty(ProxoolConstants.TRACE_PROPERTY, Boolean.TRUE.toString());
230         ProxoolFacade.registerConnectionPool(url, info);
231
232         // This should trigger a test followed the actual executed command. Because we've
233
// deliberately made the test invalid, we should get an exception when getting a
234
// connection
235
Connection JavaDoc connection = null;
236         Statement JavaDoc s = null;
237         try {
238             connection = DriverManager.getConnection(url);
239             s = connection.createStatement();
240             s.execute(TestConstants.HYPERSONIC_TEST_SQL);
241             fail("Expected to get an exception because the test failed");
242         } catch (SQLException JavaDoc e) {
243             // Log message only so we don't get a worrying stack trace
244
LOG.debug("Expected exception: " + e.getMessage());
245         }
246
247     }
248
249     /**
250      * Test that house keeper destroys connections that fail configured
251      * the test sql
252      */

253     public void testInvalidAfterUse() throws Exception JavaDoc {
254
255         String JavaDoc testName = "invalidAfterUse";
256         String JavaDoc alias = testName;
257
258         String JavaDoc url = TestHelper.buildProxoolUrl(alias,
259                 TestConstants.HYPERSONIC_DRIVER,
260                 TestConstants.HYPERSONIC_TEST_URL);
261         Properties JavaDoc info = new Properties JavaDoc();
262         info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
263         info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD);
264         info.setProperty(ProxoolConstants.HOUSE_KEEPING_TEST_SQL_PROPERTY, "Invalid test");
265         info.setProperty(ProxoolConstants.TEST_AFTER_USE_PROPERTY, Boolean.TRUE.toString());
266         info.setProperty(ProxoolConstants.VERBOSE_PROPERTY, Boolean.TRUE.toString());
267         info.setProperty(ProxoolConstants.TRACE_PROPERTY, Boolean.TRUE.toString());
268         ProxoolFacade.registerConnectionPool(url, info);
269
270         // This should trigger a test as soon as we close the connection. Because we've
271
// deliberately made the test invalid then it should get thrown away
272
Connection JavaDoc connection = null;
273         Statement JavaDoc s = null;
274         try {
275             connection = DriverManager.getConnection(url);
276             s = connection.createStatement();
277             s.execute(TestConstants.HYPERSONIC_TEST_SQL);
278         } finally {
279             if (connection != null) {
280                 connection.close();
281             }
282         }
283
284         // There should be no available connections. We don't have a minimum setup and the one we
285
// just created on demand got thrown away because it failed its test
286
assertEquals("Available connections", 0, ProxoolFacade.getSnapshot(alias).getAvailableConnectionCount());
287
288     }
289
290     public void testBeforeAndAfterUse() throws Exception JavaDoc {
291
292         String JavaDoc testName = "beforeAndAfterUse";
293         String JavaDoc alias = testName;
294
295         String JavaDoc url = TestHelper.buildProxoolUrl(alias,
296                 TestConstants.HYPERSONIC_DRIVER,
297                 TestConstants.HYPERSONIC_TEST_URL);
298         Properties JavaDoc info = new Properties JavaDoc();
299         info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
300         info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD);
301         info.setProperty(ProxoolConstants.HOUSE_KEEPING_TEST_SQL_PROPERTY, TestConstants.HYPERSONIC_TEST_SQL);
302         info.setProperty(ProxoolConstants.TEST_BEFORE_USE_PROPERTY, Boolean.TRUE.toString());
303         info.setProperty(ProxoolConstants.TEST_AFTER_USE_PROPERTY, Boolean.TRUE.toString());
304         info.setProperty(ProxoolConstants.VERBOSE_PROPERTY, Boolean.TRUE.toString());
305         info.setProperty(ProxoolConstants.TRACE_PROPERTY, Boolean.TRUE.toString());
306         ProxoolFacade.registerConnectionPool(url, info);
307
308         Connection JavaDoc connection = null;
309         Statement JavaDoc s = null;
310         try {
311             connection = DriverManager.getConnection(url);
312             s = connection.createStatement();
313             s.execute(TestConstants.HYPERSONIC_TEST_SQL);
314         } finally {
315             if (connection != null) {
316                 connection.close();
317             }
318         }
319
320         // There should be one available connection.
321
assertEquals("Available connections", 1, ProxoolFacade.getSnapshot(alias).getAvailableConnectionCount());
322
323     }
324
325 }
326
327 /*
328 Revision history:
329 $Log: HouseKeeperTest.java,v $
330 Revision 1.13 2006/03/24 00:17:32 billhorsman
331 Correct alias name
332
333 Revision 1.12 2006/01/18 14:40:06 billhorsman
334 Unbundled Jakarta's Commons Logging.
335
336 Revision 1.11 2005/10/07 08:11:34 billhorsman
337 New test for reset failure
338
339 Revision 1.10 2005/10/02 12:30:59 billhorsman
340 Improved test by checking connectionCount
341
342 Revision 1.9 2004/06/02 21:05:19 billhorsman
343 Don't log worrying stack traces for expected exceptions.
344
345 Revision 1.8 2003/09/30 18:40:16 billhorsman
346 New tests for test-before-use and test-after-use
347
348 Revision 1.7 2003/09/11 23:58:05 billhorsman
349 New test for house-keeper-test-sql
350
351 Revision 1.6 2003/03/04 10:24:40 billhorsman
352 removed try blocks around each test
353
354 Revision 1.5 2003/03/03 17:08:57 billhorsman
355 all tests now extend AbstractProxoolTest
356
357 Revision 1.4 2003/03/03 11:12:04 billhorsman
358 fixed licence
359
360 Revision 1.3 2003/03/02 00:53:49 billhorsman
361 more robust wait
362
363 Revision 1.2 2003/03/01 15:27:24 billhorsman
364 checkstyle
365
366 Revision 1.1 2003/02/27 18:01:48 billhorsman
367 completely rethought the test structure. it's now
368 more obvious. no new tests yet though.
369
370 */
Popular Tags