001: /*
002: * This software is released under a licence similar to the Apache Software Licence.
003: * See org.logicalcobwebs.proxool.package.html for details.
004: * The latest version is available at http://proxool.sourceforge.net
005: */
006: package org.logicalcobwebs.proxool;
007:
008: import org.apache.commons.logging.Log;
009: import org.apache.commons.logging.LogFactory;
010:
011: import java.sql.Connection;
012: import java.sql.Date;
013: import java.sql.DriverManager;
014: import java.sql.PreparedStatement;
015: import java.sql.SQLException;
016: import java.sql.Statement;
017: import java.util.Calendar;
018: import java.util.Properties;
019:
020: /**
021: * Test that registering a {@link ConnectionListenerIF} with the {@link ProxoolFacade}
022: * works.
023: *
024: * @version $Revision: 1.15 $, $Date: 2006/03/23 11:42:20 $
025: * @author Christian Nedregaard (christian_nedregaard@email.com)
026: * @author $Author: billhorsman $ (current maintainer)
027: * @since Proxool 0.7
028: */
029: public class ConnectionListenerTest extends AbstractProxoolTest {
030:
031: private static final Log LOG = LogFactory
032: .getLog(ConnectionListenerTest.class);
033:
034: private int onBirthCalls;
035: private int onDeathCalls;
036: private int onExecuteCalls;
037: private int onFailCalls;
038:
039: /**
040: * @see junit.framework.TestCase#TestCase
041: */
042: public ConnectionListenerTest(String s) {
043: super (s);
044: }
045:
046: /**
047: * Test that multiple connection listeners can be added through ProxoolFacade,
048: * and that they get the expected events.
049: * @throws Exception if the test fails.
050: */
051: public void testAddConnectionListener() throws Exception {
052: clear();
053: String alias = "connectionListenerTest";
054: String url = TestHelper.buildProxoolUrl(alias,
055: TestConstants.HYPERSONIC_DRIVER,
056: TestConstants.HYPERSONIC_TEST_URL);
057: Properties info = new Properties();
058: info.setProperty(ProxoolConstants.USER_PROPERTY,
059: TestConstants.HYPERSONIC_USER);
060: info.setProperty(ProxoolConstants.PASSWORD_PROPERTY,
061: TestConstants.HYPERSONIC_PASSWORD);
062: info
063: .setProperty(
064: ProxoolConstants.MAXIMUM_CONNECTION_COUNT_PROPERTY,
065: "2");
066: info.setProperty(
067: ProxoolConstants.SIMULTANEOUS_BUILD_THROTTLE_PROPERTY,
068: "1");
069: info
070: .setProperty(
071: ProxoolConstants.MINIMUM_CONNECTION_COUNT_PROPERTY,
072: "0");
073: Connection connection1 = DriverManager.getConnection(url, info);
074: ProxoolFacade.addConnectionListener(alias,
075: new TestConnectionListener());
076: ProxoolFacade.addConnectionListener(alias,
077: new TestConnectionListener());
078: Connection connection2 = DriverManager.getConnection(url);
079:
080: // provoke execution error
081: boolean errorOccured = false;
082: try {
083: connection1.createStatement().executeQuery("DINGO");
084: } catch (SQLException e) {
085: // we want this.
086: errorOccured = true;
087: }
088: assertTrue("We failed to provoke a connection failure.",
089: errorOccured);
090:
091: // following statement should be ok
092: connection2.createStatement().executeQuery(
093: TestConstants.HYPERSONIC_TEST_SQL);
094:
095: // close both connections
096: connection1.close();
097: connection2.close();
098:
099: // shutdown connection pool
100: ProxoolFacade.removeConnectionPool(alias);
101:
102: // test results
103: assertTrue("Expected 2 onBirth calls, but got "
104: + this .onBirthCalls + ".", this .onBirthCalls == 2);
105: assertTrue("Expected 2 onExecute calls, but got "
106: + this .onExecuteCalls + ".", this .onExecuteCalls == 2);
107: assertTrue("Expected 2 onFail calls, but got "
108: + this .onFailCalls + ".", this .onFailCalls == 2);
109: assertTrue("Expected 4 onDeath calls, but got "
110: + this .onDeathCalls + ".", this .onDeathCalls == 4);
111: }
112:
113: /**
114: * See whether the command parameter passed to {@link ConnectionListenerIF#onFail(java.lang.String, java.lang.Exception)}
115: * is correct. And assume it is also right for onExecute.
116: * @throws Exception if the test fails.
117: */
118: public void testExecuteCommand() throws Exception {
119: clear();
120: String alias = "executeCommand";
121: String url = TestHelper.buildProxoolUrl(alias,
122: TestConstants.HYPERSONIC_DRIVER,
123: TestConstants.HYPERSONIC_TEST_URL);
124: Properties info = new Properties();
125: info.setProperty(ProxoolConstants.USER_PROPERTY,
126: TestConstants.HYPERSONIC_USER);
127: info.setProperty(ProxoolConstants.PASSWORD_PROPERTY,
128: TestConstants.HYPERSONIC_PASSWORD);
129: Connection connection1 = DriverManager.getConnection(url, info);
130: final TestConnectionListener tcl = new TestConnectionListener();
131: ProxoolFacade.addConnectionListener(alias, tcl);
132:
133: Statement createStatement = connection1.createStatement();
134: createStatement
135: .execute("CREATE TABLE NOTHING (a boolean, b datetime, c integer, d decimal, e varchar)");
136:
137: // provoke execution error
138: java.util.Date date = new java.util.Date();
139: PreparedStatement ps = connection1
140: .prepareStatement("select * from NOTHING where a = ? and b = ? and c = ? and d = ? and e = ?");
141: ps.setBoolean(1, true);
142: ps.setDate(2, new Date(date.getTime()));
143: ps.setInt(3, 3);
144: ps.setDouble(4, 4.0);
145: ps.setString(5, "test");
146: ps.execute();
147: LOG.debug(tcl.getCommand());
148: assertEquals("command",
149: "select * from NOTHING where a = true and b = '"
150: + AbstractProxyStatement.getDateAsString(date)
151: + "' and c = 3 and d = 4.0 and e = 'test';",
152: tcl.getCommand().trim());
153:
154: // Check that it works with no parameters
155: final String s2 = "select * from NOTHING;";
156: tcl.clear();
157: ps = connection1.prepareStatement(s2);
158: ps.execute();
159: LOG.debug(tcl.getCommand());
160: assertEquals("command", s2, tcl.getCommand().trim());
161:
162: tcl.clear();
163: ps = connection1.prepareStatement(s2);
164: ps.execute();
165: LOG.debug(tcl.getCommand());
166: assertEquals("command", s2, tcl.getCommand().trim());
167:
168: }
169:
170: /**
171: * Test that multiple connection listeners can be added through ProxoolFacade,
172: * and then removed, and that they do not receive events after they have been removed.
173: * @throws Exception if the test fails.
174: */
175: public void testRemoveConnectionListener() throws Exception {
176: clear();
177: String alias = "removeConnectionListenerTest";
178: String url = TestHelper.buildProxoolUrl(alias,
179: TestConstants.HYPERSONIC_DRIVER,
180: TestConstants.HYPERSONIC_TEST_URL);
181: Properties info = new Properties();
182: info.setProperty(ProxoolConstants.USER_PROPERTY,
183: TestConstants.HYPERSONIC_USER);
184: info.setProperty(ProxoolConstants.PASSWORD_PROPERTY,
185: TestConstants.HYPERSONIC_PASSWORD);
186: info
187: .setProperty(
188: ProxoolConstants.MAXIMUM_CONNECTION_COUNT_PROPERTY,
189: "2");
190: info.setProperty(
191: ProxoolConstants.SIMULTANEOUS_BUILD_THROTTLE_PROPERTY,
192: "1");
193: info
194: .setProperty(
195: ProxoolConstants.MINIMUM_CONNECTION_COUNT_PROPERTY,
196: "0");
197: Connection connection1 = DriverManager.getConnection(url, info);
198: TestConnectionListener testConnectionListener1 = new TestConnectionListener();
199: TestConnectionListener testConnectionListener2 = new TestConnectionListener();
200: ProxoolFacade.addConnectionListener(alias,
201: testConnectionListener1);
202: ProxoolFacade.addConnectionListener(alias,
203: testConnectionListener2);
204: assertTrue("Failed to remove testConnectionListener1",
205: ProxoolFacade.removeConnectionListener(alias,
206: testConnectionListener1));
207: assertTrue("Failed to remove testConnectionListener2",
208: ProxoolFacade.removeConnectionListener(alias,
209: testConnectionListener2));
210: ProxoolFacade.removeConnectionListener(alias,
211: testConnectionListener2);
212: Connection connection2 = DriverManager.getConnection(url, info);
213:
214: // provoke execution error
215: boolean errorOccured = false;
216: try {
217: connection1.createStatement().executeQuery("DINGO");
218: } catch (SQLException e) {
219: // we want this.
220: errorOccured = true;
221: }
222: assertTrue("We failed to proovoke a connection failure.",
223: errorOccured);
224:
225: // following statement should be ok
226: connection2.createStatement().executeQuery(
227: TestConstants.HYPERSONIC_TEST_SQL);
228:
229: // close connections
230: connection1.close();
231: connection2.close();
232:
233: // shutdown connection pool
234: ProxoolFacade.removeConnectionPool(alias);
235:
236: // validate results
237: assertTrue("Expected 0 onBirth calls, but got "
238: + this .onBirthCalls + ".", this .onBirthCalls == 0);
239: assertTrue("Expected 0 onExecute calls, but got "
240: + this .onExecuteCalls + ".", this .onExecuteCalls == 0);
241: assertTrue("Expected 0 onFail calls, but got "
242: + this .onFailCalls + ".", this .onFailCalls == 0);
243: assertTrue("Expected 0 onDeath calls, but got "
244: + this .onDeathCalls + ".", this .onDeathCalls == 0);
245: }
246:
247: private void clear() {
248: this .onBirthCalls = 0;
249: this .onDeathCalls = 0;
250: this .onExecuteCalls = 0;
251: this .onFailCalls = 0;
252: }
253:
254: // /**
255: // * Calls {@link AbstractProxoolTest#setUp}
256: // * @see junit.framework.TestCase#setUp
257: // */
258: // protected void setUp() throws Exception {
259: // super.setUp();
260: // Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");
261: // }
262:
263: class TestConnectionListener implements ConnectionListenerIF {
264:
265: String command;
266:
267: public void onBirth(Connection connection) throws SQLException {
268: onBirthCalls++;
269: }
270:
271: public void onDeath(Connection connection) throws SQLException {
272: onDeathCalls++;
273: }
274:
275: public void onExecute(String command, long elapsedTime) {
276: onExecuteCalls++;
277: this .command = command;
278: }
279:
280: public void onFail(String command, Exception exception) {
281: onFailCalls++;
282: this .command = command;
283: }
284:
285: public String getCommand() {
286: return command;
287: }
288:
289: public void clear() {
290: command = null;
291: }
292: }
293: }
294:
295: /*
296: Revision history:
297: $Log: ConnectionListenerTest.java,v $
298: Revision 1.15 2006/03/23 11:42:20 billhorsman
299: 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.
300:
301: Revision 1.14 2006/01/18 14:40:06 billhorsman
302: Unbundled Jakarta's Commons Logging.
303:
304: Revision 1.13 2004/06/02 20:04:00 billhorsman
305: Added test for onExecute command
306:
307: Revision 1.12 2004/05/26 17:19:10 brenuart
308: Allow JUnit tests to be executed against another database.
309: By default the test configuration will be taken from the 'testconfig-hsqldb.properties' file located in the org.logicalcobwebs.proxool package.
310: This behavior can be overriden by setting the 'testConfig' environment property to another location.
311:
312: Revision 1.11 2003/03/10 23:31:04 billhorsman
313: fixed deprecated properties and doc
314:
315: Revision 1.10 2003/03/04 10:58:43 billhorsman
316: checkstyle
317:
318: Revision 1.9 2003/03/04 10:24:40 billhorsman
319: removed try blocks around each test
320:
321: Revision 1.8 2003/03/03 17:08:55 billhorsman
322: all tests now extend AbstractProxoolTest
323:
324: Revision 1.7 2003/03/03 11:12:04 billhorsman
325: fixed licence
326:
327: Revision 1.6 2003/03/01 15:27:24 billhorsman
328: checkstyle
329:
330: Revision 1.5 2003/02/28 10:26:38 billhorsman
331: removed killAllConnections call which should be unnecessary
332: and forced me to fix bug in ConnectionPool.shutdown where
333: onDeath wasn't getting called. Also used constants for properties
334: and used database in db directory (to clean up files)
335:
336: Revision 1.4 2003/02/19 15:14:22 billhorsman
337: fixed copyright (copy and paste error,
338: not copyright change)
339:
340: Revision 1.3 2003/02/19 13:47:51 chr32
341: Fixed wrong proxool parameters.
342:
343: Revision 1.2 2003/02/18 16:58:12 chr32
344: Checkstyle.
345:
346: Revision 1.1 2003/02/18 16:51:20 chr32
347: Added tests for ConnectionListeners.
348:
349: */
|