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.DriverManager ; 13 import java.sql.SQLException ; 14 import java.sql.Statement ; 15 import java.util.Properties ; 16 17 25 public class FatalSqlExceptionTest extends AbstractProxoolTest { 26 27 private static final Log LOG = LogFactory.getLog(FatalSqlExceptionTest.class); 28 29 public FatalSqlExceptionTest(String alias) { 30 super(alias); 31 } 32 33 34 public void testFatalSqlException() throws Exception { 35 36 String testName = "fatalSqlException"; 37 String alias = testName; 38 39 String url = TestHelper.buildProxoolUrl(alias, 40 TestConstants.HYPERSONIC_DRIVER, 41 TestConstants.HYPERSONIC_TEST_URL); 42 Properties info = new Properties (); 43 info.setProperty(ProxoolConstants.FATAL_SQL_EXCEPTION_PROPERTY, TestConstants.FATAL_SQL_EXCEPTION); 44 info.setProperty(ProxoolConstants.VERBOSE_PROPERTY, String.valueOf(Boolean.TRUE)); 45 info.setProperty(ProxoolConstants.TRACE_PROPERTY, String.valueOf(Boolean.TRUE)); 46 info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER); 47 info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD); 48 ProxoolFacade.registerConnectionPool(url, info); 49 50 Connection c1 = null; 51 long id1 = 0; 52 try { 53 c1 = DriverManager.getConnection(url); 54 id1 = ProxoolFacade.getId(c1); 55 } finally { 56 if (c1 != null) { 57 c1.close(); 58 } 59 } 60 61 Connection c2 = null; 62 long id2 = 0; 63 try { 64 c2 = DriverManager.getConnection(url); 65 id2 = ProxoolFacade.getId(c2); 66 assertTrue("Expected same connection back", id1 == id2); 67 Statement s = c2.createStatement(); 68 s.execute(TestConstants.FATAL_SQL_STATEMENT); 70 s.execute(TestConstants.FATAL_SQL_STATEMENT); 71 } catch (SQLException e) { 72 assertTrue("Didn't expect a " + FatalSQLException.class.getName(), !(e instanceof FatalSQLException)); 73 } finally { 76 if (c2 != null) { 77 c2.close(); 78 } 79 } 80 81 Connection c3 = null; 82 long id3 = 0; 83 try { 84 c3 = DriverManager.getConnection(url); 85 id3 = ProxoolFacade.getId(c3); 86 assertTrue("Expected a different connection", id1 != id3); 87 } finally { 88 if (c3 != null) { 89 c3.close(); 90 } 91 } 92 93 } 94 95 96 public void testWrappedFatalSqlException() throws Exception { 97 98 String testName = "wrappedFatalSqlException"; 99 String alias = testName; 100 101 String url = TestHelper.buildProxoolUrl(alias, 102 TestConstants.HYPERSONIC_DRIVER, 103 TestConstants.HYPERSONIC_TEST_URL); 104 Properties info = new Properties (); 105 info.setProperty(ProxoolConstants.FATAL_SQL_EXCEPTION_PROPERTY, TestConstants.FATAL_SQL_EXCEPTION); 106 info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER); 107 info.setProperty(ProxoolConstants.FATAL_SQL_EXCEPTION_WRAPPER_CLASS_PROPERTY, FatalSQLException.class.getName()); 108 info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD); 109 ProxoolFacade.registerConnectionPool(url, info); 110 111 Connection c = null; 112 try { 113 c = DriverManager.getConnection(url); 114 Statement s = c.createStatement(); 115 s.execute(TestConstants.FATAL_SQL_STATEMENT); 116 } catch (SQLException e) { 117 assertTrue("Expected a " + FatalSQLException.class.getName() + " but got a " + e.getClass().getName() + " instead", e instanceof FatalSQLException); 118 } 121 122 try { 123 if (c != null) { 124 c.close(); 125 } 126 } catch (SQLException e) { 127 LOG.debug("Couldn't close connection", e); 128 } 129 130 Thread.sleep(1000); 131 132 assertEquals("availableConnectionCount", 0L, ProxoolFacade.getSnapshot(alias, false).getAvailableConnectionCount()); 134 135 } 136 137 public void testWrappedFatalRuntimeException() throws Exception { 138 139 String testName = "wrappedFatalRuntimeException"; 140 String alias = testName; 141 142 String url = TestHelper.buildProxoolUrl(alias, 143 TestConstants.HYPERSONIC_DRIVER, 144 TestConstants.HYPERSONIC_TEST_URL); 145 Properties info = new Properties (); 146 info.setProperty(ProxoolConstants.FATAL_SQL_EXCEPTION_PROPERTY, TestConstants.FATAL_SQL_EXCEPTION); 147 info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER); 148 info.setProperty(ProxoolConstants.FATAL_SQL_EXCEPTION_WRAPPER_CLASS_PROPERTY, FatalRuntimeException.class.getName()); 149 info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD); 150 ProxoolFacade.registerConnectionPool(url, info); 151 152 Connection c = null; 153 try { 154 c = DriverManager.getConnection(url); 155 Statement s = c.createStatement(); 156 s.execute(TestConstants.FATAL_SQL_STATEMENT); 157 } catch (RuntimeException e) { 158 assertTrue("Expected a " + FatalRuntimeException.class.getName() + " but got a " + e.getClass().getName() + " instead", e instanceof FatalRuntimeException); 159 LOG.debug("Expected exception (safe to ignore)", e); 161 } 162 163 try { 164 if (c != null) { 165 c.close(); 166 } 167 } catch (SQLException e) { 168 LOG.debug("Couldn't close connection", e); 169 } 170 171 Thread.sleep(1000); 172 173 assertEquals("availableConnectionCount", 0L, ProxoolFacade.getSnapshot(alias, false).getAvailableConnectionCount()); 175 176 } 177 178 public void testFatalSqlExceptionWrapperNotFound() throws Exception { 179 180 String testName = "fatalSqlExceptionWrapperNotFound"; 181 String alias = testName; 182 183 String url = TestHelper.buildProxoolUrl(alias, 184 TestConstants.HYPERSONIC_DRIVER, 185 TestConstants.HYPERSONIC_TEST_URL); 186 Properties info = new Properties (); 187 info.setProperty(ProxoolConstants.FATAL_SQL_EXCEPTION_PROPERTY, TestConstants.FATAL_SQL_EXCEPTION); 188 info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER); 189 info.setProperty(ProxoolConstants.FATAL_SQL_EXCEPTION_WRAPPER_CLASS_PROPERTY, "org.does.not.Exist"); 190 info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD); 191 try { 192 ProxoolFacade.registerConnectionPool(url, info); 193 fail("Registration was expected to have failed"); 194 } catch (ProxoolException e) { 195 LOG.debug("Expected exception", e); 196 } 198 199 } 200 201 public void testFatalSqlExceptionWrapperInvalid() throws Exception { 202 203 String testName = "fatalSqlExceptionWrapperInvalid"; 204 String alias = testName; 205 206 String url = TestHelper.buildProxoolUrl(alias, 207 TestConstants.HYPERSONIC_DRIVER, 208 TestConstants.HYPERSONIC_TEST_URL); 209 Properties info = new Properties (); 210 info.setProperty(ProxoolConstants.FATAL_SQL_EXCEPTION_PROPERTY, TestConstants.FATAL_SQL_EXCEPTION); 211 info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER); 212 info.setProperty(ProxoolConstants.FATAL_SQL_EXCEPTION_WRAPPER_CLASS_PROPERTY, ProxoolException.class.getName()); 214 info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD); 215 try { 216 ProxoolFacade.registerConnectionPool(url, info); 217 fail("Registration was expected to have failed"); 218 } catch (ProxoolException e) { 219 LOG.debug("Expected exception", e); 220 } 222 223 } 224 } 225 226 227 | Popular Tags |