You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2023/08/27 20:30:20 UTC

[commons-dbcp] branch master updated (2a10ef8a -> c1a70745)

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-dbcp.git


    from 2a10ef8a Use try-with-resources
     new c2c60e72 Use try-with-resources
     new 90d8d2bf Use try-with-resources
     new c1a70745 Use assertThrows()

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../dbcp2/TestDataSourceConnectionFactory.java     | 24 ++++++----
 .../dbcp2/TestDelegatingCallableStatement.java     | 14 ++++--
 .../commons/dbcp2/TestDelegatingConnection.java    | 55 ++++++++++------------
 3 files changed, 48 insertions(+), 45 deletions(-)


[commons-dbcp] 01/03: Use try-with-resources

Posted by gg...@apache.org.
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

commit c2c60e72252bac19d6497d01d29897ecacb9e9c7
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Aug 27 16:18:55 2023 -0400

    Use try-with-resources
---
 .../dbcp2/TestDataSourceConnectionFactory.java     | 24 ++++++++++++++--------
 1 file changed, 15 insertions(+), 9 deletions(-)

diff --git a/src/test/java/org/apache/commons/dbcp2/TestDataSourceConnectionFactory.java b/src/test/java/org/apache/commons/dbcp2/TestDataSourceConnectionFactory.java
index fb4345ef..298747b6 100644
--- a/src/test/java/org/apache/commons/dbcp2/TestDataSourceConnectionFactory.java
+++ b/src/test/java/org/apache/commons/dbcp2/TestDataSourceConnectionFactory.java
@@ -70,10 +70,12 @@ public class TestDataSourceConnectionFactory {
 
         @Override
         public void setLoginTimeout(final int seconds) throws SQLException {
+            // noop
         }
 
         @Override
         public void setLogWriter(final PrintWriter out) throws SQLException {
+            // noop
         }
 
         @Override
@@ -94,27 +96,31 @@ public class TestDataSourceConnectionFactory {
     @Test
     public void testCredentials() throws SQLException {
         final DataSourceConnectionFactory factory = new DataSourceConnectionFactory(datasource, "foo", "bar");
-        final Connection conn = factory.createConnection();
-        assertEquals("foo", ((TesterConnection) conn).getUserName());
+        try (Connection conn = factory.createConnection()) {
+            assertEquals("foo", ((TesterConnection) conn).getUserName());
+        }
     }
 
     @Test
     public void testDefaultValues() throws SQLException {
-        final Connection conn = factory.createConnection();
-        assertNull(((TesterConnection) conn).getUserName());
+        try (Connection conn = factory.createConnection()) {
+            assertNull(((TesterConnection) conn).getUserName());
+        }
     }
 
     @Test
     public void testEmptyPassword() throws SQLException {
         final DataSourceConnectionFactory factory = new DataSourceConnectionFactory(datasource, "foo", (char[]) null);
-        final Connection conn = factory.createConnection();
-        assertEquals("foo", ((TesterConnection) conn).getUserName());
+        try (Connection conn = factory.createConnection()) {
+            assertEquals("foo", ((TesterConnection) conn).getUserName());
+        }
     }
 
     @Test
     public void testEmptyUser() throws SQLException {
-        final DataSourceConnectionFactory factory = new DataSourceConnectionFactory(datasource, null, new char[] {'a'});
-        final Connection conn = factory.createConnection();
-        assertNull(((TesterConnection) conn).getUserName());
+        final DataSourceConnectionFactory factory = new DataSourceConnectionFactory(datasource, null, new char[] { 'a' });
+        try (Connection conn = factory.createConnection()) {
+            assertNull(((TesterConnection) conn).getUserName());
+        }
     }
 }


[commons-dbcp] 02/03: Use try-with-resources

Posted by gg...@apache.org.
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

commit 90d8d2bfd3345b20e375c421f35b7be9730c21d9
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Aug 27 16:21:34 2023 -0400

    Use try-with-resources
---
 .../commons/dbcp2/TestDelegatingCallableStatement.java     | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/src/test/java/org/apache/commons/dbcp2/TestDelegatingCallableStatement.java b/src/test/java/org/apache/commons/dbcp2/TestDelegatingCallableStatement.java
index 6d9be2b8..eca74a27 100644
--- a/src/test/java/org/apache/commons/dbcp2/TestDelegatingCallableStatement.java
+++ b/src/test/java/org/apache/commons/dbcp2/TestDelegatingCallableStatement.java
@@ -26,6 +26,7 @@ import static org.mockito.Mockito.verify;
 
 import java.sql.CallableStatement;
 import java.sql.Connection;
+import java.sql.ResultSet;
 import java.sql.SQLException;
 
 import org.junit.jupiter.api.BeforeEach;
@@ -42,22 +43,25 @@ public class TestDelegatingCallableStatement {
     public void setUp() throws Exception {
         conn = new TesterConnection("test", "test");
         obj = mock(CallableStatement.class);
-        final DelegatingConnection<Connection> delegatingConnection = new DelegatingConnection<>(conn);
-        delegate = new DelegatingCallableStatement(delegatingConnection, obj);
+        delegate = new DelegatingCallableStatement(new DelegatingConnection<>(conn), obj);
     }
 
     @Test
     public void testExecuteQueryReturnsNotNull() throws Exception {
-        final TesterCallableStatement delegateStmt = new TesterCallableStatement(conn,"select * from foo");
+        final TesterCallableStatement delegateStmt = new TesterCallableStatement(conn, "select * from foo");
         obj = new DelegatingCallableStatement(new DelegatingConnection<>(conn), delegateStmt);
-        assertNotNull(obj.executeQuery());
+        try (ResultSet rs = obj.executeQuery()) {
+            assertNotNull(rs);
+        }
     }
 
     @Test
     public void testExecuteQueryReturnsNull() throws Exception {
         final TesterCallableStatement delegateStmt = new TesterCallableStatement(conn,"null");
         obj = new DelegatingCallableStatement(new DelegatingConnection<>(conn), delegateStmt);
-        assertNull(obj.executeQuery());
+        try (ResultSet rs = obj.executeQuery()) {
+            assertNotNull(rs);
+        }
     }
 
     @Test


[commons-dbcp] 03/03: Use assertThrows()

Posted by gg...@apache.org.
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

commit c1a70745c4ff6de5650b38ba14363df172dba84f
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Aug 27 16:30:15 2023 -0400

    Use assertThrows()
    
    Use try-with-resources
---
 .../commons/dbcp2/TestDelegatingConnection.java    | 55 ++++++++++------------
 1 file changed, 24 insertions(+), 31 deletions(-)

diff --git a/src/test/java/org/apache/commons/dbcp2/TestDelegatingConnection.java b/src/test/java/org/apache/commons/dbcp2/TestDelegatingConnection.java
index 3519b171..963ac71b 100644
--- a/src/test/java/org/apache/commons/dbcp2/TestDelegatingConnection.java
+++ b/src/test/java/org/apache/commons/dbcp2/TestDelegatingConnection.java
@@ -20,6 +20,7 @@ package org.apache.commons.dbcp2;
 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.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.junit.jupiter.api.Assertions.fail;
 
@@ -42,6 +43,7 @@ public class TestDelegatingConnection {
      * setAutoCommit and discard it, to keep false.
      */
     static class NoReadOnlyOrAutoCommitConnection extends TesterConnection {
+
         private final boolean readOnly = false;
         private final boolean autoCommit = false;
 
@@ -104,13 +106,12 @@ public class TestDelegatingConnection {
 
     @Test
     public void testAutoCommitCaching() throws SQLException {
-        final Connection con = new NoReadOnlyOrAutoCommitConnection();
-        final DelegatingConnection<Connection> delCon = new DelegatingConnection<>(con);
-
-        delCon.setAutoCommit(true);
-
-        assertFalse(con.getAutoCommit());
-        assertFalse(delCon.getAutoCommit());
+        try (Connection con = new NoReadOnlyOrAutoCommitConnection(); 
+                DelegatingConnection<Connection> delCon = new DelegatingConnection<>(con)) {
+            delCon.setAutoCommit(true);
+            assertFalse(con.getAutoCommit());
+            assertFalse(delCon.getAutoCommit());
+        }
     }
 
     @Test
@@ -147,26 +148,19 @@ public class TestDelegatingConnection {
             assertTrue(ex.getMessage().endsWith("is null."));
         }
 
-        try {
-            final PoolingConnection pc = new PoolingConnection(connection2);
+        try (PoolingConnection pc = new PoolingConnection(connection2)) {
             pc.setStatementPool(new GenericKeyedObjectPool<>(pc));
             delegatingConnection = new DelegatingConnection<>(pc);
-            pc.close();
-            delegatingConnection.close();
-            try (PreparedStatement ps = delegatingConnection.prepareStatement("")){}
-            fail("Expecting SQLException");
-        } catch (final SQLException ex) {
-            assertTrue(ex.getMessage().endsWith("is closed."));
         }
+        delegatingConnection.close();
 
-        try {
-            delegatingConnection = new DelegatingConnection<>(new RTEGeneratingConnection());
-            delegatingConnection.close();
-            delegatingConnection.checkOpen();
-            fail("Expecting SQLException");
-        } catch (final SQLException ex) {
-            assertTrue(ex.getMessage().endsWith("is closed."));
-        }
+        SQLException e = assertThrows(SQLException.class, () -> delegatingConnection.prepareStatement(""));
+        assertTrue(e.getMessage().endsWith("is closed."));
+        
+        delegatingConnection = new DelegatingConnection<>(new RTEGeneratingConnection());
+        delegatingConnection.close();
+        SQLException ex = assertThrows(SQLException.class, delegatingConnection::checkOpen);
+        assertTrue(ex.getMessage().endsWith("is closed."));
     }
 
     @Test
@@ -226,7 +220,7 @@ public class TestDelegatingConnection {
             Assertions.assertEquals(2, ((SQLExceptionList) e).getCauseList().size());
         } finally {
             testerStatement.setSqlExceptionOnClose(false);
-        testerResultSet.setSqlExceptionOnClose(false);
+            testerResultSet.setSqlExceptionOnClose(false);
         }
     }
 
@@ -247,13 +241,12 @@ public class TestDelegatingConnection {
 
     @Test
     public void testReadOnlyCaching() throws SQLException {
-        final Connection con = new NoReadOnlyOrAutoCommitConnection();
-        final DelegatingConnection<Connection> delCon = new DelegatingConnection<>(con);
-
-        delCon.setReadOnly(true);
-
-        assertFalse(con.isReadOnly());
-        assertFalse(delCon.isReadOnly());
+        try (Connection con = new NoReadOnlyOrAutoCommitConnection();
+                DelegatingConnection<Connection> delCon = new DelegatingConnection<>(con)) {
+            delCon.setReadOnly(true);
+            assertFalse(con.isReadOnly());
+            assertFalse(delCon.isReadOnly());
+        }
     }
 
 }