KickJava   Java API By Example, From Geeks To Geeks.

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


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
11 import java.sql.Connection JavaDoc;
12 import java.sql.Date JavaDoc;
13 import java.sql.DriverManager JavaDoc;
14 import java.sql.PreparedStatement JavaDoc;
15 import java.sql.SQLException JavaDoc;
16 import java.sql.Statement JavaDoc;
17 import java.util.Calendar JavaDoc;
18 import java.util.Properties JavaDoc;
19
20 /**
21  * Test that registering a {@link ConnectionListenerIF} with the {@link ProxoolFacade}
22  * works.
23  *
24  * @version $Revision: 1.15 $, $Date: 2006/03/23 11:42:20 $
25  * @author Christian Nedregaard (christian_nedregaard@email.com)
26  * @author $Author: billhorsman $ (current maintainer)
27  * @since Proxool 0.7
28  */

29 public class ConnectionListenerTest extends AbstractProxoolTest {
30
31     private static final Log LOG = LogFactory.getLog(ConnectionListenerTest.class);
32
33     private int onBirthCalls;
34     private int onDeathCalls;
35     private int onExecuteCalls;
36     private int onFailCalls;
37
38     /**
39      * @see junit.framework.TestCase#TestCase
40      */

41     public ConnectionListenerTest(String JavaDoc s) {
42         super(s);
43     }
44
45     /**
46      * Test that multiple connection listeners can be added through ProxoolFacade,
47      * and that they get the expected events.
48      * @throws Exception if the test fails.
49      */

50     public void testAddConnectionListener() throws Exception JavaDoc {
51         clear();
52         String JavaDoc alias = "connectionListenerTest";
53         String JavaDoc url = TestHelper.buildProxoolUrl(alias,
54                 TestConstants.HYPERSONIC_DRIVER,
55                 TestConstants.HYPERSONIC_TEST_URL);
56         Properties JavaDoc info = new Properties JavaDoc();
57         info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
58         info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD);
59         info.setProperty(ProxoolConstants.MAXIMUM_CONNECTION_COUNT_PROPERTY, "2");
60         info.setProperty(ProxoolConstants.SIMULTANEOUS_BUILD_THROTTLE_PROPERTY, "1");
61         info.setProperty(ProxoolConstants.MINIMUM_CONNECTION_COUNT_PROPERTY, "0");
62         Connection JavaDoc connection1 = DriverManager.getConnection(url, info);
63         ProxoolFacade.addConnectionListener(alias, new TestConnectionListener());
64         ProxoolFacade.addConnectionListener(alias, new TestConnectionListener());
65         Connection JavaDoc connection2 = DriverManager.getConnection(url);
66         
67         // provoke execution error
68
boolean errorOccured = false;
69         try {
70             connection1.createStatement().executeQuery("DINGO");
71         } catch (SQLException JavaDoc e) {
72             // we want this.
73
errorOccured = true;
74         }
75         assertTrue("We failed to provoke a connection failure.", errorOccured);
76         
77         // following statement should be ok
78
connection2.createStatement().executeQuery(TestConstants.HYPERSONIC_TEST_SQL);
79         
80         // close both connections
81
connection1.close();
82         connection2.close();
83         
84         // shutdown connection pool
85
ProxoolFacade.removeConnectionPool(alias);
86         
87         // test results
88
assertTrue("Expected 2 onBirth calls, but got " + this.onBirthCalls + ".", this.onBirthCalls == 2);
89         assertTrue("Expected 2 onExecute calls, but got " + this.onExecuteCalls + ".", this.onExecuteCalls == 2);
90         assertTrue("Expected 2 onFail calls, but got " + this.onFailCalls + ".", this.onFailCalls == 2);
91         assertTrue("Expected 4 onDeath calls, but got " + this.onDeathCalls + ".", this.onDeathCalls == 4);
92     }
93
94     /**
95      * See whether the command parameter passed to {@link ConnectionListenerIF#onFail(java.lang.String, java.lang.Exception)}
96      * is correct. And assume it is also right for onExecute.
97      * @throws Exception if the test fails.
98      */

99     public void testExecuteCommand() throws Exception JavaDoc {
100         clear();
101         String JavaDoc alias = "executeCommand";
102         String JavaDoc url = TestHelper.buildProxoolUrl(alias,
103                 TestConstants.HYPERSONIC_DRIVER,
104                 TestConstants.HYPERSONIC_TEST_URL);
105         Properties JavaDoc info = new Properties JavaDoc();
106         info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
107         info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD);
108         Connection JavaDoc connection1 = DriverManager.getConnection(url, info);
109         final TestConnectionListener tcl = new TestConnectionListener();
110         ProxoolFacade.addConnectionListener(alias, tcl);
111
112         Statement JavaDoc createStatement = connection1.createStatement();
113         createStatement.execute("CREATE TABLE NOTHING (a boolean, b datetime, c integer, d decimal, e varchar)");
114
115
116         // provoke execution error
117
java.util.Date JavaDoc date = new java.util.Date JavaDoc();
118         PreparedStatement JavaDoc ps = connection1.prepareStatement("select * from NOTHING where a = ? and b = ? and c = ? and d = ? and e = ?");
119         ps.setBoolean(1, true);
120         ps.setDate(2, new Date JavaDoc(date.getTime()));
121         ps.setInt(3, 3);
122         ps.setDouble(4, 4.0);
123         ps.setString(5, "test");
124         ps.execute();
125         LOG.debug(tcl.getCommand());
126         assertEquals("command", "select * from NOTHING where a = true and b = '" + AbstractProxyStatement.getDateAsString(date) + "' and c = 3 and d = 4.0 and e = 'test';", tcl.getCommand().trim());
127
128         // Check that it works with no parameters
129
final String JavaDoc s2 = "select * from NOTHING;";
130         tcl.clear();
131         ps = connection1.prepareStatement(s2);
132         ps.execute();
133         LOG.debug(tcl.getCommand());
134         assertEquals("command", s2, tcl.getCommand().trim());
135
136         tcl.clear();
137         ps = connection1.prepareStatement(s2);
138         ps.execute();
139         LOG.debug(tcl.getCommand());
140         assertEquals("command", s2, tcl.getCommand().trim());
141
142     }
143
144     /**
145      * Test that multiple connection listeners can be added through ProxoolFacade,
146      * and then removed, and that they do not receive events after they have been removed.
147      * @throws Exception if the test fails.
148      */

149     public void testRemoveConnectionListener() throws Exception JavaDoc {
150         clear();
151         String JavaDoc alias = "removeConnectionListenerTest";
152         String JavaDoc url = TestHelper.buildProxoolUrl(alias,
153                 TestConstants.HYPERSONIC_DRIVER,
154                 TestConstants.HYPERSONIC_TEST_URL);
155         Properties JavaDoc info = new Properties JavaDoc();
156         info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
157         info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD);
158         info.setProperty(ProxoolConstants.MAXIMUM_CONNECTION_COUNT_PROPERTY, "2");
159         info.setProperty(ProxoolConstants.SIMULTANEOUS_BUILD_THROTTLE_PROPERTY, "1");
160         info.setProperty(ProxoolConstants.MINIMUM_CONNECTION_COUNT_PROPERTY, "0");
161         Connection JavaDoc connection1 = DriverManager.getConnection(url, info);
162         TestConnectionListener testConnectionListener1 = new TestConnectionListener();
163         TestConnectionListener testConnectionListener2 = new TestConnectionListener();
164         ProxoolFacade.addConnectionListener(alias, testConnectionListener1);
165         ProxoolFacade.addConnectionListener(alias, testConnectionListener2);
166         assertTrue("Failed to remove testConnectionListener1", ProxoolFacade.removeConnectionListener(alias, testConnectionListener1));
167         assertTrue("Failed to remove testConnectionListener2", ProxoolFacade.removeConnectionListener(alias, testConnectionListener2));
168         ProxoolFacade.removeConnectionListener(alias, testConnectionListener2);
169         Connection JavaDoc connection2 = DriverManager.getConnection(url, info);
170         
171         // provoke execution error
172
boolean errorOccured = false;
173         try {
174             connection1.createStatement().executeQuery("DINGO");
175         } catch (SQLException JavaDoc e) {
176             // we want this.
177
errorOccured = true;
178         }
179         assertTrue("We failed to proovoke a connection failure.", errorOccured);
180         
181         // following statement should be ok
182
connection2.createStatement().executeQuery(TestConstants.HYPERSONIC_TEST_SQL);
183         
184         // close connections
185
connection1.close();
186         connection2.close();
187         
188         // shutdown connection pool
189
ProxoolFacade.removeConnectionPool(alias);
190         
191         // validate results
192
assertTrue("Expected 0 onBirth calls, but got " + this.onBirthCalls + ".", this.onBirthCalls == 0);
193         assertTrue("Expected 0 onExecute calls, but got " + this.onExecuteCalls + ".", this.onExecuteCalls == 0);
194         assertTrue("Expected 0 onFail calls, but got " + this.onFailCalls + ".", this.onFailCalls == 0);
195         assertTrue("Expected 0 onDeath calls, but got " + this.onDeathCalls + ".", this.onDeathCalls == 0);
196     }
197
198     private void clear() {
199         this.onBirthCalls = 0;
200         this.onDeathCalls = 0;
201         this.onExecuteCalls = 0;
202         this.onFailCalls = 0;
203     }
204
205 // /**
206
// * Calls {@link AbstractProxoolTest#setUp}
207
// * @see junit.framework.TestCase#setUp
208
// */
209
// protected void setUp() throws Exception {
210
// super.setUp();
211
// Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");
212
// }
213

214     class TestConnectionListener implements ConnectionListenerIF {
215
216         String JavaDoc command;
217
218         public void onBirth(Connection JavaDoc connection) throws SQLException JavaDoc {
219             onBirthCalls++;
220         }
221
222         public void onDeath(Connection JavaDoc connection) throws SQLException JavaDoc {
223             onDeathCalls++;
224         }
225
226         public void onExecute(String JavaDoc command, long elapsedTime) {
227             onExecuteCalls++;
228             this.command = command;
229         }
230
231         public void onFail(String JavaDoc command, Exception JavaDoc exception) {
232             onFailCalls++;
233             this.command = command;
234         }
235
236         public String JavaDoc getCommand() {
237             return command;
238         }
239
240         public void clear() {
241             command = null;
242         }
243     }
244 }
245
246 /*
247  Revision history:
248  $Log: ConnectionListenerTest.java,v $
249  Revision 1.15 2006/03/23 11:42:20 billhorsman
250  Create dummy table so test statements work. With HSQL 1.8 it is the prepareStatement() that throws the exception, not the execute() which means the listeners never gets the event.
251
252  Revision 1.14 2006/01/18 14:40:06 billhorsman
253  Unbundled Jakarta's Commons Logging.
254
255  Revision 1.13 2004/06/02 20:04:00 billhorsman
256  Added test for onExecute command
257
258  Revision 1.12 2004/05/26 17:19:10 brenuart
259  Allow JUnit tests to be executed against another database.
260  By default the test configuration will be taken from the 'testconfig-hsqldb.properties' file located in the org.logicalcobwebs.proxool package.
261  This behavior can be overriden by setting the 'testConfig' environment property to another location.
262
263  Revision 1.11 2003/03/10 23:31:04 billhorsman
264  fixed deprecated properties and doc
265
266  Revision 1.10 2003/03/04 10:58:43 billhorsman
267  checkstyle
268
269  Revision 1.9 2003/03/04 10:24:40 billhorsman
270  removed try blocks around each test
271
272  Revision 1.8 2003/03/03 17:08:55 billhorsman
273  all tests now extend AbstractProxoolTest
274
275  Revision 1.7 2003/03/03 11:12:04 billhorsman
276  fixed licence
277
278  Revision 1.6 2003/03/01 15:27:24 billhorsman
279  checkstyle
280
281  Revision 1.5 2003/02/28 10:26:38 billhorsman
282  removed killAllConnections call which should be unnecessary
283  and forced me to fix bug in ConnectionPool.shutdown where
284  onDeath wasn't getting called. Also used constants for properties
285  and used database in db directory (to clean up files)
286
287  Revision 1.4 2003/02/19 15:14:22 billhorsman
288  fixed copyright (copy and paste error,
289  not copyright change)
290
291  Revision 1.3 2003/02/19 13:47:51 chr32
292  Fixed wrong proxool parameters.
293
294  Revision 1.2 2003/02/18 16:58:12 chr32
295  Checkstyle.
296
297  Revision 1.1 2003/02/18 16:51:20 chr32
298  Added tests for ConnectionListeners.
299
300 */

301
Popular Tags