This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-dbcp.git
The following commit(s) were added to refs/heads/master by this push: new 3c26e90c Use try-with-resources and JUnit 5 API 3c26e90c is described below commit 3c26e90caa8c66caa33b632af179c5e06286b0ef Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Fri Jan 20 10:07:44 2023 -0500 Use try-with-resources and JUnit 5 API --- .../datasources/TestPerUserPoolDataSource.java | 213 ++++++------- .../datasources/TestSharedPoolDataSource.java | 353 ++++++++++----------- 2 files changed, 271 insertions(+), 295 deletions(-) diff --git a/src/test/java/org/apache/commons/dbcp2/datasources/TestPerUserPoolDataSource.java b/src/test/java/org/apache/commons/dbcp2/datasources/TestPerUserPoolDataSource.java index fb21c7a0..d68fbd56 100644 --- a/src/test/java/org/apache/commons/dbcp2/datasources/TestPerUserPoolDataSource.java +++ b/src/test/java/org/apache/commons/dbcp2/datasources/TestPerUserPoolDataSource.java @@ -21,6 +21,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; @@ -226,48 +227,49 @@ public class TestPerUserPoolDataSource extends TestConnectionPool { @SuppressWarnings("deprecation") @Test public void testDepreactedAccessors() { - final PerUserPoolDataSource ds = new PerUserPoolDataSource(); - int i = 0; - // - i++; - ds.setDefaultMaxWaitMillis(i); - assertEquals(i, ds.getDefaultMaxWaitMillis()); - assertEquals(Duration.ofMillis(i), ds.getDefaultMaxWait()); - // - i++; - ds.setDefaultMinEvictableIdleTimeMillis(i); - assertEquals(i, ds.getDefaultMinEvictableIdleTimeMillis()); - assertEquals(Duration.ofMillis(i), ds.getDefaultMinEvictableIdleDuration()); - // - i++; - ds.setDefaultSoftMinEvictableIdleTimeMillis(i); - assertEquals(i, ds.getDefaultSoftMinEvictableIdleTimeMillis()); - assertEquals(Duration.ofMillis(i), ds.getDefaultSoftMinEvictableIdleDuration()); - // - i++; - ds.setDefaultTimeBetweenEvictionRunsMillis(i); - assertEquals(i, ds.getDefaultTimeBetweenEvictionRunsMillis()); - assertEquals(Duration.ofMillis(i), ds.getDefaultDurationBetweenEvictionRuns()); - // - i++; - ds.setPerUserMaxWaitMillis(user, Long.valueOf(i)); - assertEquals(i, ds.getPerUserMaxWaitMillis(user)); - assertEquals(Duration.ofMillis(i), ds.getPerUserMaxWaitDuration(user)); - // - i++; - ds.setPerUserMinEvictableIdleTimeMillis(user, Long.valueOf(i)); - assertEquals(i, ds.getPerUserMinEvictableIdleTimeMillis(user)); - assertEquals(Duration.ofMillis(i), ds.getPerUserMinEvictableIdleDuration(user)); - // - i++; - ds.setPerUserSoftMinEvictableIdleTimeMillis(user, Long.valueOf(i)); - assertEquals(i, ds.getPerUserSoftMinEvictableIdleTimeMillis(user)); - assertEquals(Duration.ofMillis(i), ds.getPerUserSoftMinEvictableIdleDuration(user)); - // - i++; - ds.setPerUserTimeBetweenEvictionRunsMillis(user, Long.valueOf(i)); - assertEquals(i, ds.getPerUserTimeBetweenEvictionRunsMillis(user)); - assertEquals(Duration.ofMillis(i), ds.getPerUserDurationBetweenEvictionRuns(user)); + try (final PerUserPoolDataSource ds = new PerUserPoolDataSource()) { + int i = 0; + // + i++; + ds.setDefaultMaxWaitMillis(i); + assertEquals(i, ds.getDefaultMaxWaitMillis()); + assertEquals(Duration.ofMillis(i), ds.getDefaultMaxWait()); + // + i++; + ds.setDefaultMinEvictableIdleTimeMillis(i); + assertEquals(i, ds.getDefaultMinEvictableIdleTimeMillis()); + assertEquals(Duration.ofMillis(i), ds.getDefaultMinEvictableIdleDuration()); + // + i++; + ds.setDefaultSoftMinEvictableIdleTimeMillis(i); + assertEquals(i, ds.getDefaultSoftMinEvictableIdleTimeMillis()); + assertEquals(Duration.ofMillis(i), ds.getDefaultSoftMinEvictableIdleDuration()); + // + i++; + ds.setDefaultTimeBetweenEvictionRunsMillis(i); + assertEquals(i, ds.getDefaultTimeBetweenEvictionRunsMillis()); + assertEquals(Duration.ofMillis(i), ds.getDefaultDurationBetweenEvictionRuns()); + // + i++; + ds.setPerUserMaxWaitMillis(user, Long.valueOf(i)); + assertEquals(i, ds.getPerUserMaxWaitMillis(user)); + assertEquals(Duration.ofMillis(i), ds.getPerUserMaxWaitDuration(user)); + // + i++; + ds.setPerUserMinEvictableIdleTimeMillis(user, Long.valueOf(i)); + assertEquals(i, ds.getPerUserMinEvictableIdleTimeMillis(user)); + assertEquals(Duration.ofMillis(i), ds.getPerUserMinEvictableIdleDuration(user)); + // + i++; + ds.setPerUserSoftMinEvictableIdleTimeMillis(user, Long.valueOf(i)); + assertEquals(i, ds.getPerUserSoftMinEvictableIdleTimeMillis(user)); + assertEquals(Duration.ofMillis(i), ds.getPerUserSoftMinEvictableIdleDuration(user)); + // + i++; + ds.setPerUserTimeBetweenEvictionRunsMillis(user, Long.valueOf(i)); + assertEquals(i, ds.getPerUserTimeBetweenEvictionRunsMillis(user)); + assertEquals(Duration.ofMillis(i), ds.getPerUserDurationBetweenEvictionRuns(user)); + } } /** @@ -276,14 +278,9 @@ public class TestPerUserPoolDataSource extends TestConnectionPool { * https://issues.apache.org/bugzilla/show_bug.cgi?id=18905 */ @Test - public void testIncorrectPassword() throws Exception { + public void testIncorrectPassword() throws SQLException { // Use bad password - try (Connection c = ds.getConnection("u1", "zlsafjk")){ - fail("Able to retrieve connection with incorrect password"); - } catch (final SQLException e1) { - // should fail - - } + assertThrows(SQLException.class, () -> ds.getConnection("u1", "zlsafjk")); // Use good password ds.getConnection("u1", "p1").close(); @@ -301,28 +298,22 @@ public class TestPerUserPoolDataSource extends TestConnectionPool { // Try related users and passwords ds.getConnection(user, "bar").close(); - try (Connection c = ds.getConnection("foob", "ar")) { - fail("Should have caused an SQLException"); - } catch (final SQLException expected) { - } - try (Connection c = ds.getConnection(user, "baz")){ - fail("Should have generated SQLException"); - } catch (final SQLException expected) { - } + assertThrows(SQLException.class, () -> ds.getConnection("foob", "ar")); + assertThrows(SQLException.class, () -> ds.getConnection(user, "baz")); } @Override @Test public void testMaxTotal() throws Exception { final Connection[] c = new Connection[getMaxTotal()]; - for (int i=0; i<c.length; i++) { + for (int i = 0; i < c.length; i++) { c[i] = ds.getConnection(); assertNotNull(c[i]); } - try (Connection conn = ds.getConnection()){ + try (Connection conn = ds.getConnection()) { fail("Allowed to open more than DefaultMaxTotal connections."); - } catch(final java.sql.SQLException e) { + } catch (final java.sql.SQLException e) { // should only be able to open 10 connections, so this test should // throw an exception } @@ -341,13 +332,9 @@ public class TestPerUserPoolDataSource extends TestConnectionPool { final PerUserPoolDataSource tds = (PerUserPoolDataSource) ds; tds.setDefaultMaxWait(Duration.ZERO); tds.setPerUserMaxTotal("u1", 1); - final Connection conn = tds.getConnection("u1", "p1"); - try (Connection c2 = tds.getConnection("u1", "p1")){ - fail("Expecting Pool Exhausted exception"); - } catch (final SQLException ex) { - // expected + try (final Connection conn = tds.getConnection("u1", "p1")) { + assertThrows(SQLException.class, () -> tds.getConnection("u1", "p1")); } - conn.close(); } @Test @@ -972,16 +959,15 @@ public class TestPerUserPoolDataSource extends TestConnectionPool { assertEquals(0, tds.getNumIdle("u1")); assertEquals(0, tds.getNumIdle("u2")); - Connection conn = tds.getConnection(); - assertNotNull(conn); - assertEquals(1, tds.getNumActive()); - assertEquals(0, tds.getNumActive("u1")); - assertEquals(0, tds.getNumActive("u2")); - assertEquals(0, tds.getNumIdle()); - assertEquals(0, tds.getNumIdle("u1")); - assertEquals(0, tds.getNumIdle("u2")); - - conn.close(); + try (Connection conn = tds.getConnection()) { + assertNotNull(conn); + assertEquals(1, tds.getNumActive()); + assertEquals(0, tds.getNumActive("u1")); + assertEquals(0, tds.getNumActive("u2")); + assertEquals(0, tds.getNumIdle()); + assertEquals(0, tds.getNumIdle("u1")); + assertEquals(0, tds.getNumIdle("u2")); + } assertEquals(0, tds.getNumActive()); assertEquals(0, tds.getNumActive("u1")); assertEquals(0, tds.getNumActive("u2")); @@ -989,16 +975,16 @@ public class TestPerUserPoolDataSource extends TestConnectionPool { assertEquals(0, tds.getNumIdle("u1")); assertEquals(0, tds.getNumIdle("u2")); - conn = tds.getConnection("u1", "p1"); - assertNotNull(conn); - assertEquals(0, tds.getNumActive()); - assertEquals(1, tds.getNumActive("u1")); - assertEquals(0, tds.getNumActive("u2")); - assertEquals(1, tds.getNumIdle()); - assertEquals(0, tds.getNumIdle("u1")); - assertEquals(0, tds.getNumIdle("u2")); + try (Connection conn = tds.getConnection("u1", "p1")) { + assertNotNull(conn); + assertEquals(0, tds.getNumActive()); + assertEquals(1, tds.getNumActive("u1")); + assertEquals(0, tds.getNumActive("u2")); + assertEquals(1, tds.getNumIdle()); + assertEquals(0, tds.getNumIdle("u1")); + assertEquals(0, tds.getNumIdle("u2")); + } - conn.close(); assertEquals(0, tds.getNumActive()); assertEquals(0, tds.getNumActive("u1")); assertEquals(0, tds.getNumActive("u2")); @@ -1567,30 +1553,26 @@ public class TestPerUserPoolDataSource extends TestConnectionPool { @Override @Test - public void testSimple() throws Exception - { - final Connection conn = ds.getConnection(); - assertNotNull(conn); - final PreparedStatement stmt = conn.prepareStatement("select * from dual"); - assertNotNull(stmt); - final ResultSet rset = stmt.executeQuery(); - assertNotNull(rset); - assertTrue(rset.next()); - rset.close(); - stmt.close(); - conn.close(); + public void testSimple() throws Exception { + try (final Connection conn = ds.getConnection()) { + assertNotNull(conn); + try (final PreparedStatement stmt = conn.prepareStatement("select * from dual")) { + assertNotNull(stmt); + try (final ResultSet rset = stmt.executeQuery()) { + assertNotNull(rset); + assertTrue(rset.next()); + } + } + } } @Override @Test - public void testSimple2() - throws Exception - { + public void testSimple2() throws Exception { Connection conn = ds.getConnection(); assertNotNull(conn); - PreparedStatement stmt = - conn.prepareStatement("select * from dual"); + PreparedStatement stmt = conn.prepareStatement("select * from dual"); assertNotNull(stmt); ResultSet rset = stmt.executeQuery(); assertNotNull(rset); @@ -1607,9 +1589,9 @@ public class TestPerUserPoolDataSource extends TestConnectionPool { stmt.close(); conn.close(); - try (Statement s = conn.createStatement()){ + try (Statement s = conn.createStatement()) { fail("Can't use closed connections"); - } catch(final SQLException e) { + } catch (final SQLException e) { // expected } @@ -1637,18 +1619,17 @@ public class TestPerUserPoolDataSource extends TestConnectionPool { } @Test - public void testSimpleWithUsername() throws Exception - { - final Connection conn = ds.getConnection("u1", "p1"); - assertNotNull(conn); - final PreparedStatement stmt = conn.prepareStatement("select * from dual"); - assertNotNull(stmt); - final ResultSet rset = stmt.executeQuery(); - assertNotNull(rset); - assertTrue(rset.next()); - rset.close(); - stmt.close(); - conn.close(); + public void testSimpleWithUsername() throws Exception { + try (final Connection conn = ds.getConnection("u1", "p1")) { + assertNotNull(conn); + try (final PreparedStatement stmt = conn.prepareStatement("select * from dual")) { + assertNotNull(stmt); + try (final ResultSet rset = stmt.executeQuery()) { + assertNotNull(rset); + assertTrue(rset.next()); + } + } + } } @Test diff --git a/src/test/java/org/apache/commons/dbcp2/datasources/TestSharedPoolDataSource.java b/src/test/java/org/apache/commons/dbcp2/datasources/TestSharedPoolDataSource.java index a817bb2c..ee7fd08d 100644 --- a/src/test/java/org/apache/commons/dbcp2/datasources/TestSharedPoolDataSource.java +++ b/src/test/java/org/apache/commons/dbcp2/datasources/TestSharedPoolDataSource.java @@ -89,7 +89,7 @@ public class TestSharedPoolDataSource extends TestConnectionPool { private static abstract class PrepareStatementCallback { protected Connection conn; - abstract PreparedStatement getPreparedStatement() throws SQLException; + abstract PreparedStatement prepareStatement() throws SQLException; void setConnection(final Connection conn) { this.conn = conn; @@ -98,42 +98,42 @@ public class TestSharedPoolDataSource extends TestConnectionPool { private static class PscbString extends PrepareStatementCallback { @Override - PreparedStatement getPreparedStatement() throws SQLException { + PreparedStatement prepareStatement() throws SQLException { return conn.prepareStatement("select * from dual"); } } private static class PscbStringInt extends PrepareStatementCallback { @Override - PreparedStatement getPreparedStatement() throws SQLException { + PreparedStatement prepareStatement() throws SQLException { return conn.prepareStatement("select * from dual", 0); } } private static class PscbStringIntArray extends PrepareStatementCallback { @Override - PreparedStatement getPreparedStatement() throws SQLException { + PreparedStatement prepareStatement() throws SQLException { return conn.prepareStatement("select * from dual", ArrayUtils.EMPTY_INT_ARRAY); } } private static class PscbStringIntInt extends PrepareStatementCallback { @Override - PreparedStatement getPreparedStatement() throws SQLException { + PreparedStatement prepareStatement() throws SQLException { return conn.prepareStatement("select * from dual", 0, 0); } } private static class PscbStringIntIntInt extends PrepareStatementCallback { @Override - PreparedStatement getPreparedStatement() throws SQLException { + PreparedStatement prepareStatement() throws SQLException { return conn.prepareStatement("select * from dual", 0, 0, 0); } } private static class PscbStringStringArray extends PrepareStatementCallback { @Override - PreparedStatement getPreparedStatement() throws SQLException { + PreparedStatement prepareStatement() throws SQLException { return conn.prepareStatement("select * from dual", ArrayUtils.EMPTY_STRING_ARRAY); } } @@ -158,6 +158,7 @@ public class TestSharedPoolDataSource extends TestConnectionPool { spDs.setDefaultMaxWait(getMaxWaitDuration()); spDs.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); + @SuppressWarnings("resource") final DataSource myDs = spDs; try (Connection conn = ds.getConnection()) { @@ -168,7 +169,7 @@ public class TestSharedPoolDataSource extends TestConnectionPool { final long l2HashCode; try (CallableStatement stmt = callBack.getCallableStatement()) { assertNotNull(stmt); - l1HashCode = ((DelegatingStatement) stmt).getDelegate().hashCode(); + l1HashCode = getDelegateHashCode(stmt); try (ResultSet rset = stmt.executeQuery()) { assertNotNull(rset); assertTrue(rset.next()); @@ -177,7 +178,7 @@ public class TestSharedPoolDataSource extends TestConnectionPool { try (CallableStatement stmt = callBack.getCallableStatement()) { assertNotNull(stmt); - l2HashCode = ((DelegatingStatement) stmt).getDelegate().hashCode(); + l2HashCode = getDelegateHashCode(stmt); try (ResultSet rset = stmt.executeQuery()) { assertNotNull(rset); assertTrue(rset.next()); @@ -195,7 +196,7 @@ public class TestSharedPoolDataSource extends TestConnectionPool { final long l4HashCode; try (CallableStatement stmt = callBack.getCallableStatement()) { assertNotNull(stmt); - l3HashCode = ((DelegatingStatement) stmt).getDelegate().hashCode(); + l3HashCode = getDelegateHashCode(stmt); try (ResultSet rset = stmt.executeQuery()) { assertNotNull(rset); assertTrue(rset.next()); @@ -204,7 +205,7 @@ public class TestSharedPoolDataSource extends TestConnectionPool { try (CallableStatement stmt = callBack.getCallableStatement()) { assertNotNull(stmt); - l4HashCode = ((DelegatingStatement) stmt).getDelegate().hashCode(); + l4HashCode = getDelegateHashCode(stmt); try (ResultSet rset = stmt.executeQuery()) { assertNotNull(rset); assertTrue(rset.next()); @@ -217,10 +218,8 @@ public class TestSharedPoolDataSource extends TestConnectionPool { } } - private void doTestPoolPreparedStatements(final PrepareStatementCallback callBack) - throws Exception { + private void doTestPoolPreparedStatements(final PrepareStatementCallback psCallBack) throws Exception { final DriverAdapterCPDS mypcds = new DriverAdapterCPDS(); - DataSource myds; mypcds.setDriver("org.apache.commons.dbcp2.TesterDriver"); mypcds.setUrl("jdbc:apache:commons:testdriver"); mypcds.setUser("foo"); @@ -228,71 +227,70 @@ public class TestSharedPoolDataSource extends TestConnectionPool { mypcds.setPoolPreparedStatements(true); mypcds.setMaxPreparedStatements(10); - final SharedPoolDataSource tds = new SharedPoolDataSource(); - tds.setConnectionPoolDataSource(mypcds); - tds.setMaxTotal(getMaxTotal()); - tds.setDefaultMaxWait(getMaxWaitDuration()); - tds.setDefaultTransactionIsolation( - Connection.TRANSACTION_READ_COMMITTED); - - myds = tds; - - Connection conn = ds.getConnection(); - callBack.setConnection(conn); - PreparedStatement stmt; - ResultSet rset; - - assertNotNull(conn); - - stmt = callBack.getPreparedStatement(); - assertNotNull(stmt); - final long l1HashCode = ((DelegatingStatement) stmt).getDelegate().hashCode(); - rset = stmt.executeQuery(); - assertNotNull(rset); - assertTrue(rset.next()); - rset.close(); - stmt.close(); - - stmt = callBack.getPreparedStatement(); - assertNotNull(stmt); - final long l2HashCode = ((DelegatingStatement) stmt).getDelegate().hashCode(); - rset = stmt.executeQuery(); - assertNotNull(rset); - assertTrue(rset.next()); - rset.close(); - stmt.close(); - - // statement pooling is not enabled, we should get different statements - assertTrue(l1HashCode != l2HashCode); - conn.close(); - conn = null; - - conn = myds.getConnection(); - callBack.setConnection(conn); - - stmt = callBack.getPreparedStatement(); - assertNotNull(stmt); - final long l3HashCode = ((DelegatingStatement) stmt).getDelegate().hashCode(); - rset = stmt.executeQuery(); - assertNotNull(rset); - assertTrue(rset.next()); - rset.close(); - stmt.close(); - - stmt = callBack.getPreparedStatement(); - assertNotNull(stmt); - final long l4HashCode = ((DelegatingStatement) stmt).getDelegate().hashCode(); - rset = stmt.executeQuery(); - assertNotNull(rset); - assertTrue(rset.next()); - rset.close(); - stmt.close(); - - // prepared statement pooling is working - assertEquals(l3HashCode, l4HashCode); - conn.close(); - conn = null; - tds.close(); + try (final SharedPoolDataSource tds = new SharedPoolDataSource()) { + tds.setConnectionPoolDataSource(mypcds); + tds.setMaxTotal(getMaxTotal()); + tds.setDefaultMaxWait(getMaxWaitDuration()); + tds.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); + + @SuppressWarnings("resource") + DataSource myDs = tds; + + try (Connection conn = ds.getConnection()) { + final long l1HashCode; + final long l2HashCode; + assertNotNull(conn); + psCallBack.setConnection(conn); + try (PreparedStatement stmt = psCallBack.prepareStatement()) { + assertNotNull(stmt); + l1HashCode = getDelegateHashCode(stmt); + try (ResultSet resultSet = stmt.executeQuery()) { + assertNotNull(resultSet); + assertTrue(resultSet.next()); + } + } + + try (PreparedStatement stmt = psCallBack.prepareStatement()) { + assertNotNull(stmt); + l2HashCode = getDelegateHashCode(stmt); + try (ResultSet resultSet = stmt.executeQuery()) { + assertNotNull(resultSet); + assertTrue(resultSet.next()); + } + } + + // statement pooling is not enabled, we should get different statements + assertTrue(l1HashCode != l2HashCode); + } + + try (Connection conn = myDs.getConnection()) { + final long l3HashCode; + final long l4HashCode; + + assertNotNull(conn); + psCallBack.setConnection(conn); + try (PreparedStatement stmt = psCallBack.prepareStatement()) { + assertNotNull(stmt); + l3HashCode = getDelegateHashCode(stmt); + try (ResultSet resultSet = stmt.executeQuery()) { + assertNotNull(resultSet); + assertTrue(resultSet.next()); + } + } + + try (PreparedStatement stmt = psCallBack.prepareStatement()) { + assertNotNull(stmt); + l4HashCode = getDelegateHashCode(stmt); + try (ResultSet resultSet = stmt.executeQuery()) { + assertNotNull(resultSet); + assertTrue(resultSet.next()); + } + } + + // prepared statement pooling is working + assertEquals(l3HashCode, l4HashCode); + } + } } @Override @@ -300,6 +298,11 @@ public class TestSharedPoolDataSource extends TestConnectionPool { return ds.getConnection("foo","bar"); } + @SuppressWarnings("resource") + private int getDelegateHashCode(final Statement stmt) { + return ((DelegatingStatement) stmt).getDelegate().hashCode(); + } + @BeforeEach public void setUp() throws Exception { pcds = new DriverAdapterCPDS(); @@ -323,36 +326,27 @@ public class TestSharedPoolDataSource extends TestConnectionPool { // See DBCP-8 @Test public void testChangePassword() throws Exception { - try (Connection c = ds.getConnection("foo", "bay")){ - fail("Should have generated SQLException"); - } catch (final SQLException expected) { - } + assertThrows(SQLException.class, () -> ds.getConnection("foo", "bay")); final Connection con1 = ds.getConnection("foo", "bar"); final Connection con2 = ds.getConnection("foo", "bar"); final Connection con3 = ds.getConnection("foo", "bar"); con1.close(); con2.close(); - TesterDriver.addUser("foo","bay"); // change the user/password setting + TesterDriver.addUser("foo", "bay"); // change the user/password setting try (Connection con4 = ds.getConnection("foo", "bay")) { // new password // Idle instances with old password should have been cleared - assertEquals(0, ((SharedPoolDataSource) ds).getNumIdle(), - "Should be no idle connections in the pool"); + assertEquals(0, ((SharedPoolDataSource) ds).getNumIdle(), "Should be no idle connections in the pool"); con4.close(); // Should be one idle instance with new pwd - assertEquals(1, ((SharedPoolDataSource) ds).getNumIdle(), - "Should be one idle connection in the pool"); - try (Connection con4b = ds.getConnection("foo", "bar")) { // old password - fail("Should have generated SQLException"); - } catch (final SQLException expected) { + assertEquals(1, ((SharedPoolDataSource) ds).getNumIdle(), "Should be one idle connection in the pool"); + assertThrows(SQLException.class, () -> ds.getConnection("foo", "bar")); // old password + try (final Connection con5 = ds.getConnection("foo", "bay")) { // take the idle one + con3.close(); // Return a connection with the old password + ds.getConnection("foo", "bay").close(); // will try bad returned connection and destroy it + assertEquals(1, ((SharedPoolDataSource) ds).getNumIdle(), "Should be one idle connection in the pool"); } - final Connection con5 = ds.getConnection("foo", "bay"); // take the idle one - con3.close(); // Return a connection with the old password - ds.getConnection("foo", "bay").close(); // will try bad returned connection and destroy it - assertEquals(1, ((SharedPoolDataSource) ds).getNumIdle(), - "Should be one idle connection in the pool"); - con5.close(); } finally { - TesterDriver.addUser("foo","bar"); + TesterDriver.addUser("foo", "bar"); } } @@ -363,10 +357,11 @@ public class TestSharedPoolDataSource extends TestConnectionPool { */ @Test public void testClosePool() throws Exception { - ((SharedPoolDataSource)ds).close(); - final SharedPoolDataSource tds = new SharedPoolDataSource(); - // NPE before BZ 37359 fix - tds.close(); + ((SharedPoolDataSource) ds).close(); + @SuppressWarnings("resource") // closed below + final SharedPoolDataSource tds = new SharedPoolDataSource(); + // NPE before BZ 37359 fix + tds.close(); } @Override @@ -457,7 +452,7 @@ public class TestSharedPoolDataSource extends TestConnectionPool { * JIRA: DBCP-245 */ @Test - public void testIncorrectPassword() throws Exception { + public void testIncorrectPassword() throws SQLException { ds.getConnection("u2", "p2").close(); try (Connection c = ds.getConnection("u1", "zlsafjk")){ // Use bad password fail("Able to retrieve connection with incorrect password"); @@ -588,19 +583,18 @@ public class TestSharedPoolDataSource extends TestConnectionPool { * Bugzilla Bug 24136 ClassCastException in DriverAdapterCPDS when setPoolPreparedStatements(true) */ @Test - public void testPoolPrepareCall() throws Exception { + public void testPoolPrepareCall() throws SQLException { pcds.setPoolPreparedStatements(true); - - final Connection conn = ds.getConnection(); - assertNotNull(conn); - final PreparedStatement stmt = conn.prepareCall("{call home()}"); - assertNotNull(stmt); - final ResultSet rset = stmt.executeQuery(); - assertNotNull(rset); - assertTrue(rset.next()); - rset.close(); - stmt.close(); - conn.close(); + try (final Connection conn = ds.getConnection()) { + assertNotNull(conn); + try (final PreparedStatement stmt = conn.prepareCall("{call home()}")) { + assertNotNull(stmt); + try (final ResultSet rset = stmt.executeQuery()) { + assertNotNull(rset); + assertTrue(rset.next()); + } + } + } } @Test @@ -621,85 +615,86 @@ public class TestSharedPoolDataSource extends TestConnectionPool { } @Test - public void testPoolPrepareStatement() throws Exception { + public void testPoolPrepareStatement() throws SQLException { pcds.setPoolPreparedStatements(true); - final Connection conn = ds.getConnection(); - assertNotNull(conn); - final PreparedStatement stmt = conn.prepareStatement("select * from dual"); - assertNotNull(stmt); - final ResultSet rset = stmt.executeQuery(); - assertNotNull(rset); - assertTrue(rset.next()); - rset.close(); - stmt.close(); - conn.close(); + try (final Connection conn = ds.getConnection()) { + assertNotNull(conn); + try (final PreparedStatement stmt = conn.prepareStatement("select * from dual")) { + assertNotNull(stmt); + try (final ResultSet rset = stmt.executeQuery()) { + assertNotNull(rset); + assertTrue(rset.next()); + } + } + } } @Override @Test public void testSimple() throws Exception { - final Connection conn = ds.getConnection(); - assertNotNull(conn); - final PreparedStatement stmt = conn.prepareStatement("select * from dual"); - assertNotNull(stmt); - final ResultSet rset = stmt.executeQuery(); - assertNotNull(rset); - assertTrue(rset.next()); - rset.close(); - stmt.close(); - conn.close(); + try (final Connection conn = ds.getConnection()) { + assertNotNull(conn); + try (final PreparedStatement stmt = conn.prepareStatement("select * from dual")) { + assertNotNull(stmt); + try (final ResultSet rset = stmt.executeQuery()) { + assertNotNull(rset); + assertTrue(rset.next()); + } + } + } } @Override @Test - public void testSimple2() throws Exception { - Connection conn = ds.getConnection(); - assertNotNull(conn); - - PreparedStatement stmt = conn.prepareStatement("select * from dual"); - assertNotNull(stmt); - ResultSet rset = stmt.executeQuery(); - assertNotNull(rset); - assertTrue(rset.next()); - rset.close(); - stmt.close(); - - stmt = conn.prepareStatement("select * from dual"); - assertNotNull(stmt); - rset = stmt.executeQuery(); - assertNotNull(rset); - assertTrue(rset.next()); - rset.close(); - stmt.close(); - - conn.close(); - try (Statement s = conn.createStatement()) { - fail("Can't use closed connections"); - } catch (final SQLException e) { - // expected - } + public void testSimple2() throws SQLException { + { + Connection conn = ds.getConnection(); + assertNotNull(conn); - conn = ds.getConnection(); - assertNotNull(conn); + try (PreparedStatement stmt = conn.prepareStatement("select * from dual")) { + assertNotNull(stmt); + try (ResultSet rset = stmt.executeQuery()) { + assertNotNull(rset); + assertTrue(rset.next()); + } + } - stmt = conn.prepareStatement("select * from dual"); - assertNotNull(stmt); - rset = stmt.executeQuery(); - assertNotNull(rset); - assertTrue(rset.next()); - rset.close(); - stmt.close(); + try (PreparedStatement stmt = conn.prepareStatement("select * from dual")) { + assertNotNull(stmt); + try (ResultSet rset = stmt.executeQuery()) { + assertNotNull(rset); + assertTrue(rset.next()); + } + } - stmt = conn.prepareStatement("select * from dual"); - assertNotNull(stmt); - rset = stmt.executeQuery(); - assertNotNull(rset); - assertTrue(rset.next()); - rset.close(); - stmt.close(); + conn.close(); + try (Statement s = conn.createStatement()) { + fail("Can't use closed connections"); + } catch (final SQLException e) { + // expected + } + } + try (Connection conn = ds.getConnection()) { + assertNotNull(conn); - conn.close(); + try (PreparedStatement stmt = conn.prepareStatement("select * from dual")) { + assertNotNull(stmt); + try (ResultSet rset = stmt.executeQuery()) { + assertNotNull(rset); + assertTrue(rset.next()); + } + } + + try (PreparedStatement stmt = conn.prepareStatement("select * from dual")) { + assertNotNull(stmt); + try (ResultSet rset = stmt.executeQuery()) { + assertNotNull(rset); + assertTrue(rset.next()); + } + } + + } } @Test