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 40a8d659 Use try-with-resources
40a8d659 is described below

commit 40a8d659d9649b56532478a6f582b894d39994e9
Author: Gary Gregory <garydgreg...@gmail.com>
AuthorDate: Sun Jan 22 11:50:33 2023 -0500

    Use try-with-resources
    
    - Use JUnit 5 API
    - Type case not
    - Javadoc
---
 .../datasources/TestPerUserPoolDataSource.java     |  2 +-
 .../datasources/TestSharedPoolDataSource.java      |  6 +-
 .../dbcp2/managed/TestBasicManagedDataSource.java  |  8 +-
 .../dbcp2/managed/TestManagedConnection.java       | 16 +---
 .../dbcp2/managed/TestManagedDataSource.java       | 22 ++---
 .../dbcp2/managed/TestManagedDataSourceInTx.java   | 97 +++++++++++-----------
 .../dbcp2/managed/TestSynchronizationOrder.java    | 10 ++-
 .../dbcp2/managed/TestTransactionContext.java      |  6 +-
 8 files changed, 77 insertions(+), 90 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 d68fbd56..ed0e0211 100644
--- 
a/src/test/java/org/apache/commons/dbcp2/datasources/TestPerUserPoolDataSource.java
+++ 
b/src/test/java/org/apache/commons/dbcp2/datasources/TestPerUserPoolDataSource.java
@@ -1548,7 +1548,7 @@ public class TestPerUserPoolDataSource extends 
TestConnectionPool {
         final Object obj = in.readObject();
         in.close();
 
-        assertEquals( 1, ((PerUserPoolDataSource)obj).getNumIdle() );
+        assertEquals(1, ((PerUserPoolDataSource) obj).getNumIdle());
     }
 
     @Override
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 ee7fd08d..1df4b1df 100644
--- 
a/src/test/java/org/apache/commons/dbcp2/datasources/TestSharedPoolDataSource.java
+++ 
b/src/test/java/org/apache/commons/dbcp2/datasources/TestSharedPoolDataSource.java
@@ -669,11 +669,7 @@ public class TestSharedPoolDataSource extends 
TestConnectionPool {
             }
 
             conn.close();
-            try (Statement s = conn.createStatement()) {
-                fail("Can't use closed connections");
-            } catch (final SQLException e) {
-                // expected
-            }
+            assertThrows(SQLException.class, () -> conn.createStatement(), 
"Can't use closed connections");
         }
         try (Connection conn = ds.getConnection()) {
             assertNotNull(conn);
diff --git 
a/src/test/java/org/apache/commons/dbcp2/managed/TestBasicManagedDataSource.java
 
b/src/test/java/org/apache/commons/dbcp2/managed/TestBasicManagedDataSource.java
index 984135f1..9383fec6 100644
--- 
a/src/test/java/org/apache/commons/dbcp2/managed/TestBasicManagedDataSource.java
+++ 
b/src/test/java/org/apache/commons/dbcp2/managed/TestBasicManagedDataSource.java
@@ -95,12 +95,8 @@ public class TestBasicManagedDataSource extends 
TestBasicDataSource {
             final ManagedConnection<?> conn2 = (ManagedConnection<?>) 
basicManagedDataSource.getConnection();
             conn2.close(); // Return one connection to the pool
             conn.close(); // No room at the inn - this will trigger 
reallyClose(), which should unregister
-            try {
-                
basicManagedDataSource.getTransactionRegistry().getXAResource(conn);
-                fail("Expecting SQLException - XAResources orphaned");
-            } catch (final SQLException ex) {
-                // expected
-            }
+            assertThrows(SQLException.class, () -> 
basicManagedDataSource.getTransactionRegistry().getXAResource(conn),
+                    "Expecting SQLException - XAResources orphaned");
             conn2.close();
         }
     }
diff --git 
a/src/test/java/org/apache/commons/dbcp2/managed/TestManagedConnection.java 
b/src/test/java/org/apache/commons/dbcp2/managed/TestManagedConnection.java
index c74beeac..81ae29e4 100644
--- a/src/test/java/org/apache/commons/dbcp2/managed/TestManagedConnection.java
+++ b/src/test/java/org/apache/commons/dbcp2/managed/TestManagedConnection.java
@@ -18,6 +18,7 @@
 package org.apache.commons.dbcp2.managed;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
 import java.lang.reflect.Field;
 import java.sql.Connection;
@@ -36,7 +37,6 @@ import javax.transaction.xa.XAResource;
 
 import org.apache.commons.dbcp2.ConnectionFactory;
 import org.apache.commons.dbcp2.Constants;
-import org.apache.commons.dbcp2.DelegatingConnection;
 import org.apache.commons.dbcp2.DriverConnectionFactory;
 import org.apache.commons.dbcp2.PoolableConnection;
 import org.apache.commons.dbcp2.PoolableConnectionFactory;
@@ -156,8 +156,7 @@ public class TestManagedConnection {
     }
 
     @BeforeEach
-    public void setUp()
-        throws Exception {
+    public void setUp() throws Exception {
         // create a GeronimoTransactionManager for testing
         transactionManager = new TransactionManagerImpl();
 
@@ -194,17 +193,10 @@ public class TestManagedConnection {
     }
 
     @Test
-    public void testConnectionReturnOnErrorWhenEnlistingXAResource()
-        throws Exception {
+    public void testConnectionReturnOnErrorWhenEnlistingXAResource() throws 
Exception {
         // see DBCP-433
-
         transactionManager.begin();
-        try {
-            final DelegatingConnection<?> connectionA = 
(DelegatingConnection<?>) getConnection();
-            connectionA.close();
-        } catch (final SQLException e) {
-            // expected
-        }
+        assertThrows(SQLException.class, () -> getConnection());
         transactionManager.commit();
         assertEquals(1, pool.getBorrowedCount());
         // assertEquals(1, pool.getReturnedCount());
diff --git 
a/src/test/java/org/apache/commons/dbcp2/managed/TestManagedDataSource.java 
b/src/test/java/org/apache/commons/dbcp2/managed/TestManagedDataSource.java
index 58336525..12382821 100644
--- a/src/test/java/org/apache/commons/dbcp2/managed/TestManagedDataSource.java
+++ b/src/test/java/org/apache/commons/dbcp2/managed/TestManagedDataSource.java
@@ -124,7 +124,7 @@ public class TestManagedDataSource extends 
TestConnectionPool {
     @Test
     public void testConnectionReturnOnCommit() throws Exception {
         transactionManager.begin();
-        try (final DelegatingConnection<?> connectionA = 
(DelegatingConnection<?>) newConnection()) {
+        try (DelegatingConnection<?> connectionA = (DelegatingConnection<?>) 
newConnection()) {
             // auto close.
         }
         transactionManager.commit();
@@ -136,7 +136,8 @@ public class TestManagedDataSource extends 
TestConnectionPool {
     @Test
     public void testManagedConnectionEqualInnermost() throws Exception {
         ds.setAccessToUnderlyingConnectionAllowed(true);
-        try (final DelegatingConnection<?> con = (DelegatingConnection<?>) 
getConnection()) {
+        try (DelegatingConnection<?> con = (DelegatingConnection<?>) 
getConnection()) {
+            @SuppressWarnings("resource")
             final Connection inner = con.getInnermostDelegate();
             ds.setAccessToUnderlyingConnectionAllowed(false);
             final DelegatingConnection<Connection> con2 = new 
DelegatingConnection<>(inner);
@@ -149,14 +150,14 @@ public class TestManagedDataSource extends 
TestConnectionPool {
 
     @Test
     public void testManagedConnectionEqualsFail() throws Exception {
-        try (final Connection con1 = getConnection(); final Connection con2 = 
getConnection()) {
+        try (Connection con1 = getConnection(); final Connection con2 = 
getConnection()) {
             assertNotEquals(con1, con2);
         }
     }
 
     @Test
     public void testManagedConnectionEqualsNull() throws Exception {
-        try (final Connection con1 = getConnection()) {
+        try (Connection con1 = getConnection()) {
             final Connection con2 = null;
             assertNotEquals(con2, con1);
         }
@@ -167,7 +168,8 @@ public class TestManagedDataSource extends 
TestConnectionPool {
     */
     @Test
     public void testManagedConnectionEqualsReflexive() throws Exception {
-        try (final Connection con = getConnection()) {
+        try (Connection con = getConnection()) {
+            @SuppressWarnings("resource")
             final Connection con2 = con;
             assertEquals(con2, con);
             assertEquals(con, con2);
@@ -186,7 +188,7 @@ public class TestManagedDataSource extends 
TestConnectionPool {
 
         // Grab a new connection - should get c[0]'s closed connection
         // so should be delegate-equivalent
-        try (final Connection con = newConnection()) {
+        try (Connection con = newConnection()) {
             Assertions.assertNotEquals(c[0], con);
             Assertions.assertEquals(((DelegatingConnection<?>) 
c[0]).getInnermostDelegateInternal(),
                 ((DelegatingConnection<?>) 
con).getInnermostDelegateInternal());
@@ -210,7 +212,7 @@ public class TestManagedDataSource extends 
TestConnectionPool {
         ds.setAccessToUnderlyingConnectionAllowed(false);
         // Grab a new connection - should get c[0]'s closed connection
         // so should be delegate-equivalent
-        try (final Connection con = newConnection()) {
+        try (Connection con = newConnection()) {
             Assertions.assertNotEquals(c[0], con);
             Assertions.assertEquals(((DelegatingConnection<?>) 
c[0]).getInnermostDelegateInternal(),
                     ((DelegatingConnection<?>) 
con).getInnermostDelegateInternal());
@@ -223,7 +225,7 @@ public class TestManagedDataSource extends 
TestConnectionPool {
 
     @Test
     public void testManagedConnectionEqualsType() throws Exception {
-        try (final Connection con1 = getConnection()) {
+        try (Connection con1 = getConnection()) {
             final Integer con2 = 0;
             assertNotEquals(con2, con1);
         }
@@ -232,7 +234,7 @@ public class TestManagedDataSource extends 
TestConnectionPool {
     @Test
     public void testNestedConnections() throws Exception {
         transactionManager.begin();
-        try (final Connection c1 = newConnection(); final Connection c2 = 
newConnection()) {
+        try (Connection c1 = newConnection(); final Connection c2 = 
newConnection()) {
             transactionManager.commit();
         }
     }
@@ -262,7 +264,7 @@ public class TestManagedDataSource extends 
TestConnectionPool {
      */
     @Test
     public void testSharedConnection() throws Exception {
-        try (final DelegatingConnection<?> connectionA = 
(DelegatingConnection<?>) newConnection();
+        try (DelegatingConnection<?> connectionA = (DelegatingConnection<?>) 
newConnection();
                 final DelegatingConnection<?> connectionB = 
(DelegatingConnection<?>) newConnection()) {
             assertNotEquals(connectionA, connectionB);
             assertNotEquals(connectionB, connectionA);
diff --git 
a/src/test/java/org/apache/commons/dbcp2/managed/TestManagedDataSourceInTx.java 
b/src/test/java/org/apache/commons/dbcp2/managed/TestManagedDataSourceInTx.java
index d6db5efb..7a91b951 100644
--- 
a/src/test/java/org/apache/commons/dbcp2/managed/TestManagedDataSourceInTx.java
+++ 
b/src/test/java/org/apache/commons/dbcp2/managed/TestManagedDataSourceInTx.java
@@ -55,32 +55,33 @@ public class TestManagedDataSourceInTx extends 
TestManagedDataSource {
         assertSame(conn, statement.getConnection(),
                 "statement.getConnection() should return the exact same 
connection instance that was used to create the statement");
 
-        final ResultSet resultSet = statement.getResultSet();
-        assertFalse(isClosed(resultSet));
-        assertSame(statement, resultSet.getStatement(),
-                "resultSet.getStatement() should return the exact same 
statement instance that was used to create the result set");
-
-        final ResultSet executeResultSet = statement.executeQuery("select * 
from dual");
-        assertFalse(isClosed(executeResultSet));
-        assertSame(statement, executeResultSet.getStatement(),
-                "resultSet.getStatement() should return the exact same 
statement instance that was used to create the result set");
-
-        final ResultSet keysResultSet = statement.getGeneratedKeys();
-        assertFalse(isClosed(keysResultSet));
-        assertSame(statement, keysResultSet.getStatement(),
-                "resultSet.getStatement() should return the exact same 
statement instance that was used to create the result set");
-
-        ResultSet preparedResultSet = null;
-        if (statement instanceof PreparedStatement) {
-            final PreparedStatement preparedStatement = (PreparedStatement) 
statement;
-            preparedResultSet = preparedStatement.executeQuery();
-            assertFalse(isClosed(preparedResultSet));
-            assertSame(statement, preparedResultSet.getStatement(),
+        try (ResultSet resultSet = statement.getResultSet()) {
+            assertFalse(isClosed(resultSet));
+            assertSame(statement, resultSet.getStatement(),
                     "resultSet.getStatement() should return the exact same 
statement instance that was used to create the result set");
-        }
 
+            try (ResultSet executeResultSet = statement.executeQuery("select * 
from dual")) {
+                assertFalse(isClosed(executeResultSet));
+                assertSame(statement, executeResultSet.getStatement(),
+                        "resultSet.getStatement() should return the exact same 
statement instance that was used to create the result set");
+            }
+
+            try (ResultSet keysResultSet = statement.getGeneratedKeys()) {
+                assertFalse(isClosed(keysResultSet));
+                assertSame(statement, keysResultSet.getStatement(),
+                        "resultSet.getStatement() should return the exact same 
statement instance that was used to create the result set");
+            }
+            if (statement instanceof PreparedStatement) {
+                final PreparedStatement preparedStatement = 
(PreparedStatement) statement;
+                try (ResultSet preparedResultSet = 
preparedStatement.executeQuery()) {
+                    assertFalse(isClosed(preparedResultSet));
+                    assertSame(statement, preparedResultSet.getStatement(),
+                            "resultSet.getStatement() should return the exact 
same statement instance that was used to create the result set");
+                }
+            }
 
-        resultSet.getStatement().getConnection().close();
+            resultSet.getStatement().getConnection().close();
+        }
     }
 
     @Override
@@ -148,22 +149,19 @@ public class TestManagedDataSourceInTx extends 
TestManagedDataSource {
         assertNotNull(sharedConnection.getWarnings());
 
         statement.close();
-        connection.close();
         sharedConnection.close();
+        connection.close();
     }
 
     @Test
     public void testCloseInTransaction() throws Exception {
-        final DelegatingConnection<?> connectionA = (DelegatingConnection<?>) 
newConnection();
-        final DelegatingConnection<?> connectionB = (DelegatingConnection<?>) 
newConnection();
-
-        assertNotEquals(connectionA, connectionB);
-        assertNotEquals(connectionB, connectionA);
-        
assertTrue(connectionA.innermostDelegateEquals(connectionB.getInnermostDelegate()));
-        
assertTrue(connectionB.innermostDelegateEquals(connectionA.getInnermostDelegate()));
-
-        connectionA.close();
-        connectionB.close();
+        try (DelegatingConnection<?> connectionA = (DelegatingConnection<?>) 
newConnection();
+                DelegatingConnection<?> connectionB = 
(DelegatingConnection<?>) newConnection()) {
+            assertNotEquals(connectionA, connectionB);
+            assertNotEquals(connectionB, connectionA);
+            
assertTrue(connectionA.innermostDelegateEquals(connectionB.getInnermostDelegate()));
+            
assertTrue(connectionB.innermostDelegateEquals(connectionA.getInnermostDelegate()));
+        }
 
         final Connection connection = newConnection();
 
@@ -176,24 +174,23 @@ public class TestManagedDataSourceInTx extends 
TestManagedDataSource {
 
     @Test
     public void testCommit() throws Exception {
-        final Connection connection = newConnection();
+        try (Connection connection = newConnection()) {
 
-        // connection should be open
-        assertFalse(connection.isClosed(), "Connection should be open");
+            // connection should be open
+            assertFalse(connection.isClosed(), "Connection should be open");
 
-        // attempt commit directly
-        try {
-            connection.commit();
-            fail("commit method should be disabled while enlisted in a 
transaction");
-        } catch (final SQLException e) {
-            // expected
-        }
+            // attempt commit directly
+            try {
+                connection.commit();
+                fail("commit method should be disabled while enlisted in a 
transaction");
+            } catch (final SQLException e) {
+                // expected
+            }
 
-        // make sure it is still open
-        assertFalse(connection.isClosed(), "Connection should be open");
+            // make sure it is still open
+            assertFalse(connection.isClosed(), "Connection should be open");
 
-        // close connection
-        connection.close();
+        }
     }
 
     @Override
@@ -206,9 +203,9 @@ public class TestManagedDataSourceInTx extends 
TestManagedDataSource {
     @Test
     public void testConnectionsAreDistinct() throws Exception {
         final Connection[] conn = new Connection[getMaxTotal()];
-        for(int i=0;i<conn.length;i++) {
+        for (int i = 0; i < conn.length; i++) {
             conn[i] = newConnection();
-            for(int j=0;j<i;j++) {
+            for (int j = 0; j < i; j++) {
                 // two connections should be distinct instances
                 Assertions.assertNotSame(conn[j], conn[i]);
                 // neither should they should be equivalent even though they 
are
diff --git 
a/src/test/java/org/apache/commons/dbcp2/managed/TestSynchronizationOrder.java 
b/src/test/java/org/apache/commons/dbcp2/managed/TestSynchronizationOrder.java
index 70efc9c0..cb38b58c 100644
--- 
a/src/test/java/org/apache/commons/dbcp2/managed/TestSynchronizationOrder.java
+++ 
b/src/test/java/org/apache/commons/dbcp2/managed/TestSynchronizationOrder.java
@@ -184,8 +184,9 @@ public class TestSynchronizationOrder {
                 ds.setAccessToUnderlyingConnectionAllowed(true);
 
                 transactionManager.begin();
-                try (final DelegatingConnection<?> connectionA = 
(DelegatingConnection<?>) ds.getConnection()) {
-                    // Close right away.
+                try (final Connection connectionA = ds.getConnection()) {
+                    // Check and close right away.
+                    assertTrue(connectionA instanceof DelegatingConnection);
                 }
                 transactionManager.commit();
                 assertFalse(transactionManagerRegistered);
@@ -216,8 +217,9 @@ public class TestSynchronizationOrder {
                 ds.setAccessToUnderlyingConnectionAllowed(true);
 
                 transactionManager.begin();
-                try (final DelegatingConnection<?> connectionA = 
(DelegatingConnection<?>) ds.getConnection()) {
-                    // close right away.
+                try (final Connection connectionA = ds.getConnection()) {
+                    // Check and close right away.
+                    assertTrue(connectionA instanceof DelegatingConnection);
                 }
                 transactionManager.commit();
                 assertTrue(transactionManagerRegistered);
diff --git 
a/src/test/java/org/apache/commons/dbcp2/managed/TestTransactionContext.java 
b/src/test/java/org/apache/commons/dbcp2/managed/TestTransactionContext.java
index 2e690430..0ef404e1 100644
--- a/src/test/java/org/apache/commons/dbcp2/managed/TestTransactionContext.java
+++ b/src/test/java/org/apache/commons/dbcp2/managed/TestTransactionContext.java
@@ -18,6 +18,7 @@
 package org.apache.commons.dbcp2.managed;
 
 import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import java.sql.Connection;
 import java.sql.SQLException;
@@ -29,7 +30,7 @@ import 
org.apache.geronimo.transaction.manager.TransactionManagerImpl;
 import org.junit.jupiter.api.Test;
 
 /**
- * TestSuite for TransactionContext
+ * TestSuite for {@link TransactionContext}.
  */
 public class TestTransactionContext {
 
@@ -60,7 +61,8 @@ public class TestTransactionContext {
             basicManagedDataSource.setUsername("userName");
             basicManagedDataSource.setPassword("password");
             basicManagedDataSource.setMaxIdle(1);
-            try (final ManagedConnection<?> conn = 
(ManagedConnection<Connection>) basicManagedDataSource.getConnection()) {
+            try (final Connection conn = 
basicManagedDataSource.getConnection()) {
+                assertTrue(conn instanceof ManagedConnection);
                 final UncooperativeTransaction transaction = new 
UncooperativeTransaction();
                 final TransactionContext transactionContext = new 
TransactionContext(basicManagedDataSource.getTransactionRegistry(), 
transaction);
                 assertThrows(SQLException.class, () -> 
transactionContext.setSharedConnection(conn));

Reply via email to