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.DriverManager ; 12 import java.sql.Connection ; 13 import java.sql.Statement ; 14 import java.sql.SQLException ; 15 import java.util.Properties ; 16 17 26 public class WrapperTest extends AbstractProxoolTest { 27 28 private static final Log LOG = LogFactory.getLog(WrapperTest.class); 29 30 33 public WrapperTest(String alias) { 34 super(alias); 35 } 36 37 41 public void testDoubleConnection() throws Exception { 42 43 String testName = "alias"; 44 String alias = testName; 45 46 String url = TestHelper.buildProxoolUrl(alias, 48 TestConstants.HYPERSONIC_DRIVER, 49 TestConstants.HYPERSONIC_TEST_URL); 50 Properties info = new Properties (); 51 info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER); 52 info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD); 53 info.setProperty(ProxoolConstants.MAXIMUM_CONNECTION_COUNT, "1"); 54 55 Connection c1 = DriverManager.getConnection(url, info); 56 Connection dc1 = ProxoolFacade.getDelegateConnection(c1); 57 c1.close(); 58 assertEquals("servedCount", 1, ProxoolFacade.getSnapshot(alias).getServedCount()); 59 LOG.debug("c1 = " + c1.toString() + ", dc1 = " + dc1); 60 61 Connection c2 = DriverManager.getConnection(url, info); 62 Connection dc2 = ProxoolFacade.getDelegateConnection(c2); 63 LOG.debug("c2 = " + c2 + ", dc2 = " + dc2); 64 65 assertEquals("Expected the delegate connection to be the same", dc1, dc2); 66 c1.close(); 68 assertTrue("Connection was closed unexpectedly", !c2.isClosed()); 70 71 Statement s = null; 73 try { 74 s = c1.createStatement(); 75 s.execute(TestConstants.HYPERSONIC_TEST_SQL); 76 fail("Expected to get an exception because the test failed"); 77 } catch (SQLException e) { 78 LOG.debug("Expected exception.", e); 79 } 80 81 try { 83 s = c2.createStatement(); 84 s.execute(TestConstants.HYPERSONIC_TEST_SQL); 85 } catch (SQLException e) { 86 fail("Connection failed, but it should have worked"); 87 } 88 89 c2.close(); 90 assertTrue("Connection was not closed", c2.isClosed()); 91 92 } 93 94 99 public void testDoubleClose() throws SQLException , ProxoolException { 100 101 String testName = "alias"; 102 String alias = testName; 103 104 String url = TestHelper.buildProxoolUrl(alias, 106 TestConstants.HYPERSONIC_DRIVER, 107 TestConstants.HYPERSONIC_TEST_URL); 108 Properties info = new Properties (); 109 info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER); 110 info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD); 111 info.setProperty(ProxoolConstants.MAXIMUM_CONNECTION_COUNT, "1"); 112 113 Connection c1 = DriverManager.getConnection(url, info); 114 c1.close(); 115 c1.close(); 116 117 } 118 119 125 public void testCreateStatementAfterClose() throws SQLException , ProxoolException { 126 127 String testName = "alias"; 128 String alias = testName; 129 130 String url = TestHelper.buildProxoolUrl(alias, 132 TestConstants.HYPERSONIC_DRIVER, 133 TestConstants.HYPERSONIC_TEST_URL); 134 Properties info = new Properties (); 135 info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER); 136 info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD); 137 info.setProperty(ProxoolConstants.MAXIMUM_CONNECTION_COUNT, "1"); 138 139 Connection c1 = DriverManager.getConnection(url, info); 140 c1.close(); 141 try { 142 c1.createStatement(); 143 fail("Expected createStatement() to fail after connection was closed"); 144 } catch (SQLException e) { 145 } 147 } 148 149 154 public void testIsClosedAfterClose() throws SQLException , ProxoolException { 155 156 String testName = "alias"; 157 String alias = testName; 158 159 String url = TestHelper.buildProxoolUrl(alias, 161 TestConstants.HYPERSONIC_DRIVER, 162 TestConstants.HYPERSONIC_TEST_URL); 163 Properties info = new Properties (); 164 info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER); 165 info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD); 166 info.setProperty(ProxoolConstants.MAXIMUM_CONNECTION_COUNT, "1"); 167 168 Connection c1 = DriverManager.getConnection(url, info); 169 c1.close(); 170 assertTrue("isClosed()", c1.isClosed()); 171 } 172 173 178 public void testHashCode() throws SQLException , ProxoolException { 179 180 String testName = "alias"; 181 String alias = testName; 182 183 String url = TestHelper.buildProxoolUrl(alias, 185 TestConstants.HYPERSONIC_DRIVER, 186 TestConstants.HYPERSONIC_TEST_URL); 187 Properties info = new Properties (); 188 info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER); 189 info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD); 190 info.setProperty(ProxoolConstants.MAXIMUM_CONNECTION_COUNT, "1"); 191 192 Connection c1 = DriverManager.getConnection(url, info); 193 LOG.debug(c1 + " = " + c1.hashCode()); 194 c1.close(); 195 LOG.debug(c1 + " = " + c1.hashCode()); 196 197 Connection c2 = DriverManager.getConnection(url, info); 198 LOG.debug(c2 + " = " + c2.hashCode()); 199 c2.close(); 200 LOG.debug(c2 + " = " + c2.hashCode()); 201 202 } 203 204 209 public void testEquals() throws SQLException , ProxoolException { 210 211 String testName = "alias"; 212 String alias = testName; 213 214 String url = TestHelper.buildProxoolUrl(alias, 216 TestConstants.HYPERSONIC_DRIVER, 217 TestConstants.HYPERSONIC_TEST_URL); 218 Properties info = new Properties (); 219 info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER); 220 info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD); 221 info.setProperty(ProxoolConstants.MAXIMUM_CONNECTION_COUNT, "1"); 222 223 Connection c1 = DriverManager.getConnection(url, info); 224 assertTrue("c1 == c1", c1.equals(c1)); 225 c1.close(); 226 assertTrue("c1 == c1", c1.equals(c1)); 227 228 Connection c2 = DriverManager.getConnection(url, info); 229 assertTrue("c1 == c2", c1.equals(c2)); 230 c2.close(); 231 assertTrue("c1 == c2", c1.equals(c2)); 232 233 } 234 235 241 public void testId() throws ProxoolException, SQLException { 242 String testName = "alias"; 243 String alias = testName; 244 245 String url = TestHelper.buildProxoolUrl(alias, 247 TestConstants.HYPERSONIC_DRIVER, 248 TestConstants.HYPERSONIC_TEST_URL); 249 Properties info = new Properties (); 250 info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER); 251 info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD); 252 info.setProperty(ProxoolConstants.MAXIMUM_CONNECTION_COUNT, "2"); 253 254 Connection c1 = DriverManager.getConnection(url, info); 255 assertEquals("c1.getId()", 1, ProxoolFacade.getId(c1)); 256 257 Connection c2 = DriverManager.getConnection(url, info); 258 assertEquals("c2.getId()", 2, ProxoolFacade.getId(c2)); 259 c1.close(); 260 assertEquals("c1.getId()", 1, ProxoolFacade.getId(c1)); 261 c2.close(); 262 assertEquals("c2.getId()", 2, ProxoolFacade.getId(c2)); 263 264 } 265 266 272 public void testAlias() throws ProxoolException, SQLException { 273 String testName = "alias"; 274 String alias = testName; 275 276 String url = TestHelper.buildProxoolUrl(alias, 278 TestConstants.HYPERSONIC_DRIVER, 279 TestConstants.HYPERSONIC_TEST_URL); 280 Properties info = new Properties (); 281 info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER); 282 info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD); 283 284 Connection c1 = DriverManager.getConnection(url, info); 285 assertEquals("c1.getAlias()", alias, ProxoolFacade.getAlias(c1)); 286 c1.close(); 287 assertEquals("c1.getAlias()", alias, ProxoolFacade.getAlias(c1)); 288 289 } 290 } 291 292 | Popular Tags |