1 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 ; 12 import java.sql.Date ; 13 import java.sql.DriverManager ; 14 import java.sql.PreparedStatement ; 15 import java.sql.SQLException ; 16 import java.sql.Statement ; 17 import java.util.Calendar ; 18 import java.util.Properties ; 19 20 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 41 public ConnectionListenerTest(String s) { 42 super(s); 43 } 44 45 50 public void testAddConnectionListener() throws Exception { 51 clear(); 52 String alias = "connectionListenerTest"; 53 String url = TestHelper.buildProxoolUrl(alias, 54 TestConstants.HYPERSONIC_DRIVER, 55 TestConstants.HYPERSONIC_TEST_URL); 56 Properties info = new Properties (); 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 connection1 = DriverManager.getConnection(url, info); 63 ProxoolFacade.addConnectionListener(alias, new TestConnectionListener()); 64 ProxoolFacade.addConnectionListener(alias, new TestConnectionListener()); 65 Connection connection2 = DriverManager.getConnection(url); 66 67 boolean errorOccured = false; 69 try { 70 connection1.createStatement().executeQuery("DINGO"); 71 } catch (SQLException e) { 72 errorOccured = true; 74 } 75 assertTrue("We failed to provoke a connection failure.", errorOccured); 76 77 connection2.createStatement().executeQuery(TestConstants.HYPERSONIC_TEST_SQL); 79 80 connection1.close(); 82 connection2.close(); 83 84 ProxoolFacade.removeConnectionPool(alias); 86 87 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 99 public void testExecuteCommand() throws Exception { 100 clear(); 101 String alias = "executeCommand"; 102 String url = TestHelper.buildProxoolUrl(alias, 103 TestConstants.HYPERSONIC_DRIVER, 104 TestConstants.HYPERSONIC_TEST_URL); 105 Properties info = new Properties (); 106 info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER); 107 info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD); 108 Connection connection1 = DriverManager.getConnection(url, info); 109 final TestConnectionListener tcl = new TestConnectionListener(); 110 ProxoolFacade.addConnectionListener(alias, tcl); 111 112 Statement createStatement = connection1.createStatement(); 113 createStatement.execute("CREATE TABLE NOTHING (a boolean, b datetime, c integer, d decimal, e varchar)"); 114 115 116 java.util.Date date = new java.util.Date (); 118 PreparedStatement 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 (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 final String 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 149 public void testRemoveConnectionListener() throws Exception { 150 clear(); 151 String alias = "removeConnectionListenerTest"; 152 String url = TestHelper.buildProxoolUrl(alias, 153 TestConstants.HYPERSONIC_DRIVER, 154 TestConstants.HYPERSONIC_TEST_URL); 155 Properties info = new Properties (); 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 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 connection2 = DriverManager.getConnection(url, info); 170 171 boolean errorOccured = false; 173 try { 174 connection1.createStatement().executeQuery("DINGO"); 175 } catch (SQLException e) { 176 errorOccured = true; 178 } 179 assertTrue("We failed to proovoke a connection failure.", errorOccured); 180 181 connection2.createStatement().executeQuery(TestConstants.HYPERSONIC_TEST_SQL); 183 184 connection1.close(); 186 connection2.close(); 187 188 ProxoolFacade.removeConnectionPool(alias); 190 191 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 214 class TestConnectionListener implements ConnectionListenerIF { 215 216 String command; 217 218 public void onBirth(Connection connection) throws SQLException { 219 onBirthCalls++; 220 } 221 222 public void onDeath(Connection connection) throws SQLException { 223 onDeathCalls++; 224 } 225 226 public void onExecute(String command, long elapsedTime) { 227 onExecuteCalls++; 228 this.command = command; 229 } 230 231 public void onFail(String command, Exception exception) { 232 onFailCalls++; 233 this.command = command; 234 } 235 236 public String getCommand() { 237 return command; 238 } 239 240 public void clear() { 241 command = null; 242 } 243 } 244 } 245 246 301 | Popular Tags |