1 6 package org.logicalcobwebs.proxool; 7 8 import java.sql.CallableStatement ; 9 import java.sql.Connection ; 10 import java.sql.DriverManager ; 11 import java.sql.PreparedStatement ; 12 import java.sql.Statement ; 13 import java.util.Properties ; 14 15 import org.apache.commons.logging.Log; 16 import org.apache.commons.logging.LogFactory; 17 18 26 public class ProxyStatementTest extends AbstractProxoolTest { 27 28 private static final Log LOG = LogFactory.getLog(ProxyStatementTest.class); 29 30 public ProxyStatementTest(String alias) { 31 super(alias); 32 } 33 34 37 public void testDelegateStatement() throws Exception 38 { 39 String testName = "delegateStatement"; 40 41 String url = registerPool(testName); 43 44 Connection c = DriverManager.getConnection(url); 46 Statement s = c.createStatement(); 47 Statement delegateStatement = ProxoolFacade.getDelegateStatement(s); 48 Class delegateStatementClass = delegateStatement.getClass(); 49 s.close(); 50 51 LOG.debug("Statement " + s.getClass() + " is delegating to " + delegateStatementClass); 52 53 Connection realConnection = TestHelper.getDirectConnection(); 55 Statement realStatement = realConnection.createStatement(); 56 Class realStatementClass = realStatement.getClass(); 57 58 realStatement.close(); 59 realConnection.close(); 60 61 assertEquals("Delegate statement isn't of the expected type.", realStatementClass, delegateStatementClass); 63 } 64 65 66 69 public void testPreparedStatement() throws Exception { 70 71 String testName = "preparedStatement"; 72 73 String url = registerPool(testName); 75 76 Connection c = DriverManager.getConnection(url); 78 PreparedStatement s = c.prepareStatement(TestConstants.HYPERSONIC_TEST_SQL); 79 Statement delegateStatement = ProxoolFacade.getDelegateStatement(s); 80 Class delegateStatementClass = delegateStatement.getClass(); 81 s.close(); 82 c.close(); 83 84 LOG.debug("Statement " + s.getClass() + " is delegating to " + delegateStatementClass); 85 86 Connection realConnection = TestHelper.getDirectConnection(); 88 Statement realStatement = realConnection.prepareStatement(TestConstants.HYPERSONIC_TEST_SQL); 89 Class realStatementClass = realStatement.getClass(); 90 91 realStatement.close(); 92 realConnection.close(); 93 94 assertEquals("Delegate statement isn't of the expected type.", realStatementClass, delegateStatementClass); 96 } 97 98 99 102 public void testCallableStatement() throws Exception { 103 104 String testName = "callableStatement"; 105 106 String url = registerPool(testName); 108 109 Connection c = DriverManager.getConnection(url); 111 CallableStatement s = c.prepareCall(TestConstants.HYPERSONIC_TEST_SQL); 112 Statement delegateStatement = ProxoolFacade.getDelegateStatement(s); 113 Class delegateStatementClass = delegateStatement.getClass(); 114 s.close(); 115 116 LOG.debug("Statement " + s.getClass() + " is delegating to " + delegateStatementClass); 117 118 Connection realConnection = TestHelper.getDirectConnection(); 120 Statement realStatement = realConnection.prepareCall(TestConstants.HYPERSONIC_TEST_SQL); 121 Class realStatementClass = realStatement.getClass(); 122 123 realStatement.close(); 124 realConnection.close(); 125 126 assertEquals("Delegate statement isn't of the expected type.", realStatementClass, delegateStatementClass ); 128 } 129 130 131 134 public void testDelegateConnection() throws Exception { 135 136 String testName = "delegateConnection"; 137 138 String url = registerPool(testName); 140 141 Connection c = DriverManager.getConnection(url); 143 Connection delegateConnection = ProxoolFacade.getDelegateConnection(c); 144 Class delegateConnectionClass = delegateConnection.getClass(); 145 146 LOG.debug("Connection " + c + " is delegating to " + delegateConnectionClass); 147 c.close(); 148 149 Connection realConnection = TestHelper.getDirectConnection(); 151 Class realConnectionClass = realConnection.getClass(); 152 realConnection.close(); 153 154 assertEquals("Connection isn't of the expected.", realConnectionClass, delegateConnectionClass); 155 } 156 157 158 163 private String registerPool(String alias) throws Exception 164 { 165 String url = TestHelper.buildProxoolUrl(alias, 166 TestConstants.HYPERSONIC_DRIVER, 167 TestConstants.HYPERSONIC_TEST_URL); 168 Properties info = new Properties (); 169 info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER); 170 info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD); 171 ProxoolFacade.registerConnectionPool(url, info); 172 173 return url; 174 } 175 176 public void testCloseByStatement() throws Exception { 177 178 String testName = "snapshot"; 179 final String alias = testName; 180 String url = TestHelper.buildProxoolUrl(alias, 181 TestConstants.HYPERSONIC_DRIVER, 182 TestConstants.HYPERSONIC_TEST_URL); 183 Properties info = new Properties (); 184 info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER); 185 info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD); 186 ProxoolFacade.registerConnectionPool(url, info); 187 188 Connection c = DriverManager.getConnection(url); 190 Statement s = c.createStatement(); 191 s.getConnection().close(); 193 assertEquals("servedCount", 0, ProxoolFacade.getSnapshot(alias).getActiveConnectionCount()); 194 195 } 196 } 197 198 199 | Popular Tags |