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 2024/12/14 14:07:49 UTC

(commons-dbcp) branch master updated (fb8426fe -> d62fd3c2)

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 fb8426fe Use assertThrows()
     new 2b0bf8fa Normalize instance variable names
     new 185deebd Normalize instance parameter names
     new 0ad566aa Use JUnit 5's assertThrows()
     new e86d0b51 Use JUnit 5's assertThrows()
     new 282d4214 Use JUnit 5's assertThrows()
     new 9a4d0259 Use JUnit 5's assertThrows()
     new 8b0598a7 Use JUnit 5's assertThrows()
     new badcb78c Use JUnit 5's assertThrows()
     new 1c668cd9 Use JUnit 5's assertThrows()
     new 02cc073c Remove unused imports
     new a6fa8683 Use JUnit 5's assertThrows() and friends
     new 91545f55 Use JUnit 5's assertThrows() and friends
     new 206bb01b Use JUnit 5's assertThrows() and friends
     new 28a36995 Use assertNotNull() instead of test and fail()
     new 4eb708a1 Use JUnit 5's assertThrows() and friends
     new 4beab5db Use JUnit 5's assertThrows() and friends
     new b269c5da Remove unused import
     new 406189af Use JUnit 5's assertThrows() and friends
     new d62fd3c2 Use assertFalse() instead of test and fail()

The 19 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:
 .../datasources/KeyedCPDSConnectionFactory.java    |   1 +
 .../dbcp2/datasources/SharedPoolDataSource.java    |   1 +
 .../apache/commons/dbcp2/TestBasicDataSource.java  |  54 +++++-----
 .../commons/dbcp2/TestDelegatingConnection.java    | 106 ++++++++-----------
 .../commons/dbcp2/TestDelegatingStatement.java     |  36 +++----
 .../dbcp2/TestDriverManagerConnectionFactory.java  |  82 +++++++--------
 .../java/org/apache/commons/dbcp2/TestJndi.java    |   7 +-
 .../org/apache/commons/dbcp2/TestPStmtPooling.java |   9 +-
 .../dbcp2/TestPStmtPoolingBasicDataSource.java     |   6 +-
 .../commons/dbcp2/TestPoolableConnection.java      |  22 ++--
 .../apache/commons/dbcp2/TestPoolingDriver.java    |   9 +-
 .../org/apache/commons/dbcp2/TesterResultSet.java  |  84 +++++++--------
 .../org/apache/commons/dbcp2/TesterStatement.java  |   4 +-
 .../datasources/TestPerUserPoolDataSource.java     |  70 +++++-------
 .../datasources/TestSharedPoolDataSource.java      |  52 +++------
 .../dbcp2/managed/TestBasicManagedDataSource.java  |  63 +++++------
 .../dbcp2/managed/TestConnectionWithNarayana.java  | 117 +++++++++++----------
 .../dbcp2/managed/TestManagedDataSourceInTx.java   |  55 ++++------
 18 files changed, 336 insertions(+), 442 deletions(-)


(commons-dbcp) 07/19: Use JUnit 5's 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 8b0598a75f029dc3fa05a498b9b1b616e3fda0a0
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Sat Dec 14 08:28:56 2024 -0500

    Use JUnit 5's assertThrows()
    
    Remove unused imports
---
 .../org/apache/commons/dbcp2/datasources/TestPerUserPoolDataSource.java  | 1 -
 1 file changed, 1 deletion(-)

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 c7a1539d..ac9f71e1 100644
--- a/src/test/java/org/apache/commons/dbcp2/datasources/TestPerUserPoolDataSource.java
+++ b/src/test/java/org/apache/commons/dbcp2/datasources/TestPerUserPoolDataSource.java
@@ -33,7 +33,6 @@ import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
-import java.sql.Statement;
 import java.time.Duration;
 import java.util.HashMap;
 import java.util.Map;


(commons-dbcp) 01/19: Normalize instance variable names

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 2b0bf8fa0f4eaeda0df5fa9637a2d58d90ef13fc
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Sat Dec 14 08:07:12 2024 -0500

    Normalize instance variable names
---
 .../org/apache/commons/dbcp2/TesterResultSet.java  | 84 +++++++++++-----------
 1 file changed, 42 insertions(+), 42 deletions(-)

diff --git a/src/test/java/org/apache/commons/dbcp2/TesterResultSet.java b/src/test/java/org/apache/commons/dbcp2/TesterResultSet.java
index 0150e263..f642e333 100644
--- a/src/test/java/org/apache/commons/dbcp2/TesterResultSet.java
+++ b/src/test/java/org/apache/commons/dbcp2/TesterResultSet.java
@@ -42,29 +42,29 @@ import java.util.Map;
  */
 public class TesterResultSet extends AbandonedTrace implements ResultSet {
 
-    protected int _type = ResultSet.TYPE_FORWARD_ONLY;
-    protected int _concurrency = ResultSet.CONCUR_READ_ONLY;
-    protected Object[][] _data;
-    protected int _currentRow = -1;
-    protected Statement _statement;
-    protected int _rowsLeft = 2;
-    protected boolean _open = true;
-    protected boolean _sqlExceptionOnClose;
+    protected int type = ResultSet.TYPE_FORWARD_ONLY;
+    protected int concurrency = ResultSet.CONCUR_READ_ONLY;
+    protected Object[][] data;
+    protected int currentRow = -1;
+    protected Statement statement;
+    protected int rowsLeft = 2;
+    protected boolean open = true;
+    protected boolean sqlExceptionOnClose;
 
-    public TesterResultSet(final Statement stmt) {
-        _statement = stmt;
+    public TesterResultSet(final Statement statement) {
+        this.statement = statement;
     }
 
-    public TesterResultSet(final Statement stmt, final int type, final int concurrency) {
-        _statement = stmt;
-        _data = null;
-        _type = type;
-        _concurrency = concurrency;
+    public TesterResultSet(final Statement statement, final int type, final int concurrency) {
+    	this.statement = statement;
+    	this.data = null;
+    	this.type = type;
+    	this.concurrency = concurrency;
     }
 
-    public TesterResultSet(final Statement stmt, final Object[][] data) {
-        _statement = stmt;
-        _data = data;
+    public TesterResultSet(final Statement statement, final Object[][] data) {
+    	this.statement = statement;
+    	this.data = data;
     }
 
     @Override
@@ -89,7 +89,7 @@ public class TesterResultSet extends AbandonedTrace implements ResultSet {
     }
 
     protected void checkOpen() throws SQLException {
-        if (!_open) {
+        if (!open) {
             throw new SQLException("ResultSet is closed.");
         }
     }
@@ -101,20 +101,20 @@ public class TesterResultSet extends AbandonedTrace implements ResultSet {
 
     @Override
     public void close() throws SQLException {
-        if (_sqlExceptionOnClose) {
+        if (sqlExceptionOnClose) {
             throw new SQLException("TestSQLExceptionOnClose");
         }
 
-        if (!_open) {
+        if (!open) {
             return;
         }
 
         // Not all result sets are generated from statements eg DatabaseMetaData
-        if (_statement != null) {
-            ((TesterStatement) _statement).resultSet = null;
+        if (statement != null) {
+            ((TesterStatement) statement).resultSet = null;
         }
 
-        _open = false;
+        open = false;
     }
 
     @Override
@@ -272,7 +272,7 @@ public class TesterResultSet extends AbandonedTrace implements ResultSet {
 
     @Override
     public int getConcurrency() throws SQLException {
-        return this._concurrency;
+        return this.concurrency;
     }
 
     @Override
@@ -409,8 +409,8 @@ public class TesterResultSet extends AbandonedTrace implements ResultSet {
     @Override
     public Object getObject(final int columnIndex) throws SQLException {
         checkOpen();
-        if (_data != null) {
-            return _data[_currentRow][columnIndex - 1];
+        if (data != null) {
+            return data[currentRow][columnIndex - 1];
         }
         return new Object();
     }
@@ -458,7 +458,7 @@ public class TesterResultSet extends AbandonedTrace implements ResultSet {
     @Override
     public int getRow() throws SQLException {
         checkOpen();
-        return 3 - _rowsLeft;
+        return 3 - rowsLeft;
     }
 
     @Override
@@ -496,7 +496,7 @@ public class TesterResultSet extends AbandonedTrace implements ResultSet {
     @Override
     public Statement getStatement() throws SQLException {
         checkOpen();
-        return _statement;
+        return statement;
     }
 
     @Override
@@ -505,7 +505,7 @@ public class TesterResultSet extends AbandonedTrace implements ResultSet {
         if (columnIndex == -1) {
             throw new SQLException("broken connection");
         }
-        if (_data != null) {
+        if (data != null) {
             return (String) getObject(columnIndex);
         }
         return "String" + columnIndex;
@@ -567,7 +567,7 @@ public class TesterResultSet extends AbandonedTrace implements ResultSet {
 
     @Override
     public int getType() throws SQLException {
-        return this._type;
+        return this.type;
     }
 
     /** @deprecated */
@@ -610,34 +610,34 @@ public class TesterResultSet extends AbandonedTrace implements ResultSet {
     @Override
     public boolean isAfterLast() throws SQLException {
         checkOpen();
-        return _rowsLeft < 0;
+        return rowsLeft < 0;
     }
 
     @Override
     public boolean isBeforeFirst() throws SQLException {
         checkOpen();
-        return _rowsLeft == 2;
+        return rowsLeft == 2;
     }
 
     @Override
     public boolean isClosed() throws SQLException {
-        return !_open;
+        return !open;
     }
 
     @Override
     public boolean isFirst() throws SQLException {
         checkOpen();
-        return _rowsLeft == 1;
+        return rowsLeft == 1;
     }
 
     @Override
     public boolean isLast() throws SQLException {
         checkOpen();
-        return _rowsLeft == 0;
+        return rowsLeft == 0;
     }
 
     public boolean isSqlExceptionOnClose() {
-        return _sqlExceptionOnClose;
+        return sqlExceptionOnClose;
     }
 
     @Override
@@ -664,11 +664,11 @@ public class TesterResultSet extends AbandonedTrace implements ResultSet {
     @Override
     public boolean next() throws SQLException {
         checkOpen();
-        if (_data != null) {
-            _currentRow++;
-            return _currentRow < _data.length;
+        if (data != null) {
+            currentRow++;
+            return currentRow < data.length;
         }
-        return --_rowsLeft > 0;
+        return --rowsLeft > 0;
     }
 
     @Override
@@ -717,7 +717,7 @@ public class TesterResultSet extends AbandonedTrace implements ResultSet {
     }
 
     public void setSqlExceptionOnClose(final boolean sqlExceptionOnClose) {
-        this._sqlExceptionOnClose = sqlExceptionOnClose;
+        this.sqlExceptionOnClose = sqlExceptionOnClose;
     }
 
     @Override


(commons-dbcp) 08/19: Use JUnit 5's 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 badcb78c8843d27bedc114ebd44201ec3f723c30
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Sat Dec 14 08:29:01 2024 -0500

    Use JUnit 5's assertThrows()
    
    Remove unused imports
---
 .../org/apache/commons/dbcp2/TestPoolableConnection.java     | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)

diff --git a/src/test/java/org/apache/commons/dbcp2/TestPoolableConnection.java b/src/test/java/org/apache/commons/dbcp2/TestPoolableConnection.java
index a49d10cd..112c0fbd 100644
--- a/src/test/java/org/apache/commons/dbcp2/TestPoolableConnection.java
+++ b/src/test/java/org/apache/commons/dbcp2/TestPoolableConnection.java
@@ -20,7 +20,6 @@ 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.fail;
 
 import java.sql.Connection;
 import java.sql.SQLException;
@@ -130,14 +129,9 @@ public class TestPoolableConnection {
 
         // now set up fatal failure
         nativeConnection.setFailure(new SQLException("Fatal connection error.", "01002"));
-
-        try {
-            conn.createStatement();
-            fail("Should throw SQL exception.");
-        } catch (final SQLException ignored) {
-            // cleanup failure
-            nativeConnection.setFailure(null);
-        }
+        assertThrows(SQLException.class, conn::createStatement);
+        // cleanup failure
+        nativeConnection.setFailure(null);
 
         // validate should now fail because of previous fatal error, despite cleanup
 		assertThrows(SQLException.class, () -> conn.validate("SELECT 1", 1000),


(commons-dbcp) 12/19: Use JUnit 5's assertThrows() and friends

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 91545f55970b9f8a8b000f1cda3a2b4a2ff71e82
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Sat Dec 14 09:01:14 2024 -0500

    Use JUnit 5's assertThrows() and friends
---
 .../dbcp2/managed/TestConnectionWithNarayana.java  | 117 +++++++++++----------
 1 file changed, 59 insertions(+), 58 deletions(-)

diff --git a/src/test/java/org/apache/commons/dbcp2/managed/TestConnectionWithNarayana.java b/src/test/java/org/apache/commons/dbcp2/managed/TestConnectionWithNarayana.java
index 441b089d..f36aa671 100644
--- a/src/test/java/org/apache/commons/dbcp2/managed/TestConnectionWithNarayana.java
+++ b/src/test/java/org/apache/commons/dbcp2/managed/TestConnectionWithNarayana.java
@@ -125,64 +125,64 @@ public class TestConnectionWithNarayana {
 	}
 
     @Test
-    public void testConnectionInTimeout() throws Exception {
-        Connection conn = null;
-        PreparedStatement ps = null;
-        for (int i = 0; i < 5; i++) {
-            try {
-                mds.getTransactionManager().setTransactionTimeout(1);
-                mds.getTransactionManager().begin();
-
-                conn = mds.getConnection();
-                ps = conn.prepareStatement(INSERT_STMT);
-                ps.setString(1, Thread.currentThread().getName());
-                ps.setLong(2, i);
-                ps.setDouble(3, new java.util.Random().nextDouble());
-                ps.setString(4, PAYLOAD);
-                ps.setTimestamp(5, new Timestamp(System.currentTimeMillis()));
-                ps.execute();
-
-                int n = 0;
-                do {
-                    if (mds.getTransactionManager().getTransaction().getStatus() != Status.STATUS_ACTIVE) {
-                        n++;
-                    }
-                    try (Connection c = mds.getConnection(); PreparedStatement ps2 = c.prepareStatement(SELECT_STMT); ResultSet rs = ps2.executeQuery()) {
-                        // nothing here, all auto-close.
-                    }
-                } while (n < 2);
-
-                ps.close();
-                ps = null;
-                conn.close();
-                conn = null;
-
-                try {
-                    mds.getTransactionManager().commit();
-                    fail("Should not have been able to commit");
-                } catch (final RollbackException e) {
-                    // this is expected
-                    if (mds.getTransactionManager().getTransaction() != null) {
-                        // Need to pop it off the thread if a background thread rolled the transaction back
-                        mds.getTransactionManager().rollback();
-                    }
-                }
-            } catch (final Exception e) {
-                if (mds.getTransactionManager().getTransaction() != null) {
-                    // Need to pop it off the thread if a background thread rolled the transaction back
-                    mds.getTransactionManager().rollback();
-                }
-            } finally {
-                if (ps != null) {
-                    ps.close();
-                }
-                if (conn != null) {
-                    conn.close();
-                }
-            }
-            Assertions.assertEquals(0, mds.getNumActive());
-        }
-    }
+	public void testConnectionInTimeout() throws Exception {
+		Connection conn = null;
+		PreparedStatement ps = null;
+		for (int i = 0; i < 5; i++) {
+			try {
+				mds.getTransactionManager().setTransactionTimeout(1);
+				mds.getTransactionManager().begin();
+
+				conn = mds.getConnection();
+				ps = conn.prepareStatement(INSERT_STMT);
+				ps.setString(1, Thread.currentThread().getName());
+				ps.setLong(2, i);
+				ps.setDouble(3, new java.util.Random().nextDouble());
+				ps.setString(4, PAYLOAD);
+				ps.setTimestamp(5, new Timestamp(System.currentTimeMillis()));
+				ps.execute();
+
+				int n = 0;
+				do {
+					if (mds.getTransactionManager().getTransaction().getStatus() != Status.STATUS_ACTIVE) {
+						n++;
+					}
+					try (Connection c = mds.getConnection();
+							PreparedStatement ps2 = c.prepareStatement(SELECT_STMT);
+							ResultSet rs = ps2.executeQuery()) {
+						// nothing here, all auto-close.
+					}
+				} while (n < 2);
+
+				ps.close();
+				ps = null;
+				conn.close();
+				conn = null;
+
+				assertThrows(RollbackException.class, () -> mds.getTransactionManager().commit());
+				// this is expected
+				if (mds.getTransactionManager().getTransaction() != null) {
+					// Need to pop it off the thread if a background thread rolled the transaction
+					// back
+					mds.getTransactionManager().rollback();
+				}
+			} catch (final Exception e) {
+				if (mds.getTransactionManager().getTransaction() != null) {
+					// Need to pop it off the thread if a background thread rolled the transaction
+					// back
+					mds.getTransactionManager().rollback();
+				}
+			} finally {
+				if (ps != null) {
+					ps.close();
+				}
+				if (conn != null) {
+					conn.close();
+				}
+			}
+			Assertions.assertEquals(0, mds.getNumActive());
+		}
+	}
 
     @Test
     public void testRepeatedGetConnectionInTimeout() throws Exception {
@@ -195,6 +195,7 @@ public class TestConnectionWithNarayana {
             } while (mds.getTransactionManager().getTransaction().getStatus() != Status.STATUS_ROLLEDBACK);
             // Let the reaper do it's thing
             Thread.sleep(1000);
+            
             try (Connection conn = mds.getConnection()) {
                 fail("Should not get the connection 1");
             } catch (final SQLException e) {


(commons-dbcp) 11/19: Use JUnit 5's assertThrows() and friends

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 a6fa8683c696c430fdb3797b96d8f1a9f0566424
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Sat Dec 14 09:00:58 2024 -0500

    Use JUnit 5's assertThrows() and friends
---
 .../dbcp2/managed/TestBasicManagedDataSource.java  | 63 ++++++++++------------
 1 file changed, 28 insertions(+), 35 deletions(-)

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 9383fec6..26aaa2b4 100644
--- a/src/test/java/org/apache/commons/dbcp2/managed/TestBasicManagedDataSource.java
+++ b/src/test/java/org/apache/commons/dbcp2/managed/TestBasicManagedDataSource.java
@@ -17,6 +17,7 @@
  */
 package org.apache.commons.dbcp2.managed;
 
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertNull;
@@ -37,6 +38,7 @@ import org.apache.geronimo.transaction.manager.TransactionManagerImpl;
 import org.h2.Driver;
 import org.h2.jdbcx.JdbcDataSource;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.function.ThrowingSupplier;
 
 import com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionManagerImple;
 import com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionSynchronizationRegistryImple;
@@ -182,41 +184,32 @@ public class TestBasicManagedDataSource extends TestBasicDataSource {
     }
 
     @Test
-    public void testTransactionSynchronizationRegistry() throws Exception {
-        try (final BasicManagedDataSource basicManagedDataSource = new BasicManagedDataSource()) {
-            basicManagedDataSource.setTransactionManager(new TransactionManagerImple());
-            final TransactionSynchronizationRegistry tsr = new TransactionSynchronizationRegistryImple();
-            basicManagedDataSource.setTransactionSynchronizationRegistry(tsr);
-            final JdbcDataSource xaDataSource = new JdbcDataSource();
-            xaDataSource.setUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1");
-            basicManagedDataSource.setXaDataSourceInstance(xaDataSource);
-            basicManagedDataSource.setMaxIdle(1);
-
-            final TransactionManager tm = basicManagedDataSource.getTransactionManager();
-            tm.begin();
-            tsr.registerInterposedSynchronization(new SynchronizationAdapter() {
-                @Override
-                public void beforeCompletion() {
-                    Connection connection = null;
-                    try {
-                        connection = basicManagedDataSource.getConnection();
-                        assertNotNull(connection);
-                    } catch (final SQLException e) {
-                        fail(e.getMessage());
-                    } finally {
-                        if (connection != null) {
-                            try {
-                                connection.close();
-                            } catch (final SQLException e) {
-                                fail(e.getMessage());
-                            }
-                        }
-                    }
-                }
-            });
-            tm.commit();
-        }
-    }
+	public void testTransactionSynchronizationRegistry() throws Exception {
+		try (final BasicManagedDataSource basicManagedDataSource = new BasicManagedDataSource()) {
+			basicManagedDataSource.setTransactionManager(new TransactionManagerImple());
+			final TransactionSynchronizationRegistry tsr = new TransactionSynchronizationRegistryImple();
+			basicManagedDataSource.setTransactionSynchronizationRegistry(tsr);
+			final JdbcDataSource xaDataSource = new JdbcDataSource();
+			xaDataSource.setUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1");
+			basicManagedDataSource.setXaDataSourceInstance(xaDataSource);
+			basicManagedDataSource.setMaxIdle(1);
+
+			final TransactionManager tm = basicManagedDataSource.getTransactionManager();
+			tm.begin();
+			tsr.registerInterposedSynchronization(new SynchronizationAdapter() {
+				@Override
+				public void beforeCompletion() {
+					try (Connection connection = assertDoesNotThrow(
+							(ThrowingSupplier<Connection>) basicManagedDataSource::getConnection)) {
+						assertNotNull(connection);
+					} catch (SQLException e) {
+						fail(e);
+					}
+				}
+			});
+			tm.commit();
+		}
+	}
 
     @Test
     public void testXADataSource() throws SQLException {


(commons-dbcp) 04/19: Use JUnit 5's 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 e86d0b519f97d444bb603c50fd226594495688bf
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Sat Dec 14 08:21:01 2024 -0500

    Use JUnit 5's assertThrows()
    
    Refactor test cleanup
---
 .../commons/dbcp2/TestDelegatingStatement.java     | 36 +++++++++-------------
 1 file changed, 15 insertions(+), 21 deletions(-)

diff --git a/src/test/java/org/apache/commons/dbcp2/TestDelegatingStatement.java b/src/test/java/org/apache/commons/dbcp2/TestDelegatingStatement.java
index a640c9b0..7218a0e2 100644
--- a/src/test/java/org/apache/commons/dbcp2/TestDelegatingStatement.java
+++ b/src/test/java/org/apache/commons/dbcp2/TestDelegatingStatement.java
@@ -19,10 +19,10 @@ 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.assertInstanceOf;
 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;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
@@ -32,7 +32,7 @@ import java.sql.Connection;
 import java.sql.SQLException;
 import java.sql.Statement;
 
-import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
@@ -58,6 +58,12 @@ public class TestDelegatingStatement {
     private TesterResultSet testerResultSet;
     private TesterStatement testerStatement;
 
+    @AfterEach
+    public void afterEach() {
+    	testerResultSet.setSqlExceptionOnClose(false);
+        testerStatement.setSqlExceptionOnClose(false);
+    }
+
     @BeforeEach
     public void setUp() throws Exception {
         testerConnection = new TesterConnection("test", "test");
@@ -132,30 +138,18 @@ public class TestDelegatingStatement {
 
     @Test
     public void testCloseWithResultSetCloseException() throws Exception {
-        try {
-            testerResultSet.setSqlExceptionOnClose(true);
-            delegatingStatement.addTrace(testerResultSet);
-            delegatingStatement.close();
-            Assertions.fail("Excpected a SQLExceptionList");
-        } catch (final SQLException e) {
-            Assertions.assertInstanceOf(SQLExceptionList.class, e);
-        } finally {
-            testerResultSet.setSqlExceptionOnClose(false);
-        }
+        testerResultSet.setSqlExceptionOnClose(true);
+        delegatingStatement.addTrace(testerResultSet);
+        final SQLException e = assertThrows(SQLException.class, delegatingStatement::close);
+        assertInstanceOf(SQLExceptionList.class, e);
         verify(mockedStatement, times(1)).close();
     }
 
     @Test
     public void testCloseWithStatementCloseException() throws Exception {
-        try {
-            testerStatement.setSqlExceptionOnClose(true);
-            delegatingTesterStatement.close();
-            Assertions.fail("Excpected a SQLExceptionList");
-        } catch (final SQLException e) {
-            Assertions.assertInstanceOf(SQLExceptionList.class, e);
-        } finally {
-            testerStatement.setSqlExceptionOnClose(false);
-        }
+        testerStatement.setSqlExceptionOnClose(true);
+        final SQLException e = assertThrows(SQLException.class, delegatingTesterStatement::close);
+        assertInstanceOf(SQLExceptionList.class, e);
     }
 
     @Test


(commons-dbcp) 16/19: Use JUnit 5's assertThrows() and friends

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 4beab5db19b467c8037ebb451df88ac4ac01ada6
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Sat Dec 14 09:02:56 2024 -0500

    Use JUnit 5's assertThrows() and friends
---
 .../datasources/TestPerUserPoolDataSource.java     | 69 +++++++++-------------
 1 file changed, 29 insertions(+), 40 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 ac9f71e1..57856a2d 100644
--- a/src/test/java/org/apache/commons/dbcp2/datasources/TestPerUserPoolDataSource.java
+++ b/src/test/java/org/apache/commons/dbcp2/datasources/TestPerUserPoolDataSource.java
@@ -23,7 +23,6 @@ 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;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
@@ -271,50 +270,40 @@ public class TestPerUserPoolDataSource extends TestConnectionPool {
      * https://issues.apache.org/bugzilla/show_bug.cgi?id=18905
      */
     @Test
-    public void testIncorrectPassword() throws SQLException {
-        // Use bad password
-        assertThrows(SQLException.class, () -> ds.getConnection("u1", "zlsafjk"));
-
-        // Use good password
-        ds.getConnection("u1", "p1").close();
-        try (Connection c = ds.getConnection("u1", "x")){
-            fail("Able to retrieve connection with incorrect password");
-        } catch (final SQLException e) {
-            if (!e.getMessage().startsWith("Given password did not match")) {
-                throw e;
-            }
-            // else the exception was expected
-        }
+	public void testIncorrectPassword() throws SQLException {
+		// Use bad password
+		assertThrows(SQLException.class, () -> ds.getConnection("u1", "zlsafjk"));
 
-        // Make sure we can still use our good password.
-        ds.getConnection("u1", "p1").close();
+		// Use good password
+		ds.getConnection("u1", "p1").close();
+		final SQLException e = assertThrows(SQLException.class, () -> ds.getConnection("u1", "x"),
+				"Able to retrieve connection with incorrect password");
+		assertTrue(e.getMessage().startsWith("Given password did not match"));
 
-        // Try related users and passwords
-        ds.getConnection(user, "bar").close();
-        assertThrows(SQLException.class, () -> ds.getConnection("foob", "ar"));
-        assertThrows(SQLException.class, () -> ds.getConnection(user, "baz"));
-    }
+		// Make sure we can still use our good password.
+		ds.getConnection("u1", "p1").close();
+
+		// Try related users and passwords
+		ds.getConnection(user, "bar").close();
+		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++) {
-            c[i] = ds.getConnection();
-            assertNotNull(c[i]);
-        }
-
-        try (Connection conn = ds.getConnection()) {
-            fail("Allowed to open more than DefaultMaxTotal connections.");
-        } catch (final SQLException e) {
-            // should only be able to open 10 connections, so this test should
-            // throw an exception
-        }
-
-        for (final Connection element : c) {
-            element.close();
-        }
-    }
+	public void testMaxTotal() throws Exception {
+		final Connection[] c = new Connection[getMaxTotal()];
+		for (int i = 0; i < c.length; i++) {
+			c[i] = ds.getConnection();
+			assertNotNull(c[i]);
+		}
+		// should only be able to open 10 connections, so this test should throw an
+		// exception
+		assertThrows(SQLException.class, ds::getConnection, "Allowed to open more than DefaultMaxTotal connections.");
+		for (final Connection element : c) {
+			element.close();
+		}
+	}
 
     /**
      * Verify that defaultMaxWaitMillis = 0 means immediate failure when


(commons-dbcp) 19/19: Use assertFalse() instead of test and fail()

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 d62fd3c28cdd1af0aae56444db6411e40f7b60d1
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Sat Dec 14 09:04:39 2024 -0500

    Use assertFalse() instead of test and fail()
---
 .../apache/commons/dbcp2/TestBasicDataSource.java  | 54 +++++++++++-----------
 1 file changed, 27 insertions(+), 27 deletions(-)

diff --git a/src/test/java/org/apache/commons/dbcp2/TestBasicDataSource.java b/src/test/java/org/apache/commons/dbcp2/TestBasicDataSource.java
index bffe8387..da904945 100644
--- a/src/test/java/org/apache/commons/dbcp2/TestBasicDataSource.java
+++ b/src/test/java/org/apache/commons/dbcp2/TestBasicDataSource.java
@@ -517,33 +517,33 @@ public class TestBasicDataSource extends TestConnectionPool {
 
     @Test
     @Disabled
-    public void testEvict() throws Exception {
-        final long delay = 1000;
-
-        ds.setInitialSize(10);
-        ds.setMaxIdle(10);
-        ds.setMaxTotal(10);
-        ds.setMinIdle(5);
-        ds.setNumTestsPerEvictionRun(3);
-        ds.setMinEvictableIdle(Duration.ofMillis(100));
-        ds.setDurationBetweenEvictionRuns(Duration.ofMillis(delay));
-        ds.setPoolPreparedStatements(true);
-
-        try (Connection conn = ds.getConnection()) {
-            // empty
-        }
-
-        final ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
-        while (Stream.of(threadBean.getThreadInfo(threadBean.getAllThreadIds())).anyMatch(t -> t.getThreadName().equals("commons-pool-evictor-thread"))) {
-            if (ds.getNumIdle() <= ds.getMinIdle()) {
-                break;
-            }
-            Thread.sleep(delay);
-        }
-        if (ds.getNumIdle() > ds.getMinIdle()) {
-            fail("EvictionTimer thread was destroyed with numIdle=" + ds.getNumIdle() + "(expected: less or equal than " + ds.getMinIdle() + ")");
-        }
-    }
+	public void testEvict() throws Exception {
+		final long delay = 1000;
+
+		ds.setInitialSize(10);
+		ds.setMaxIdle(10);
+		ds.setMaxTotal(10);
+		ds.setMinIdle(5);
+		ds.setNumTestsPerEvictionRun(3);
+		ds.setMinEvictableIdle(Duration.ofMillis(100));
+		ds.setDurationBetweenEvictionRuns(Duration.ofMillis(delay));
+		ds.setPoolPreparedStatements(true);
+
+		try (Connection conn = ds.getConnection()) {
+			// empty
+		}
+
+		final ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
+		while (Stream.of(threadBean.getThreadInfo(threadBean.getAllThreadIds()))
+				.anyMatch(t -> t.getThreadName().equals("commons-pool-evictor-thread"))) {
+			if (ds.getNumIdle() <= ds.getMinIdle()) {
+				break;
+			}
+			Thread.sleep(delay);
+		}
+		assertFalse(ds.getNumIdle() > ds.getMinIdle(), () -> "EvictionTimer thread was destroyed with numIdle="
+				+ ds.getNumIdle() + "(expected: less or equal than " + ds.getMinIdle() + ")");
+	}
 
     @Test
     public void testInitialSize() throws Exception {


(commons-dbcp) 14/19: Use assertNotNull() instead of test and fail()

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 28a3699566e885d1b67c0d7349e18d3e39c62c48
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Sat Dec 14 09:02:32 2024 -0500

    Use assertNotNull() instead of test and fail()
---
 src/test/java/org/apache/commons/dbcp2/TestJndi.java | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/src/test/java/org/apache/commons/dbcp2/TestJndi.java b/src/test/java/org/apache/commons/dbcp2/TestJndi.java
index c01616b8..7ba37c16 100644
--- a/src/test/java/org/apache/commons/dbcp2/TestJndi.java
+++ b/src/test/java/org/apache/commons/dbcp2/TestJndi.java
@@ -17,7 +17,7 @@
 
 package org.apache.commons.dbcp2;
 
-import static org.junit.jupiter.api.Assertions.fail;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
 
 import java.util.Hashtable;
 
@@ -93,10 +93,7 @@ public class TestJndi {
     protected DataSource retrieveDataSource() throws Exception {
         final Context ctx = getInitialContext();
         final DataSource dataSource = (DataSource) ctx.lookup(JNDI_PATH);
-
-        if (dataSource == null) {
-            fail("DataSource should not be null");
-        }
+        assertNotNull(dataSource, "DataSource should not be null");
         return dataSource;
     }
 


(commons-dbcp) 13/19: Use JUnit 5's assertThrows() and friends

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 206bb01bb66ba702fa0964a9db8950215b2ad3b1
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Sat Dec 14 09:01:54 2024 -0500

    Use JUnit 5's assertThrows() and friends
---
 .../dbcp2/TestDriverManagerConnectionFactory.java  | 82 +++++++++++-----------
 1 file changed, 41 insertions(+), 41 deletions(-)

diff --git a/src/test/java/org/apache/commons/dbcp2/TestDriverManagerConnectionFactory.java b/src/test/java/org/apache/commons/dbcp2/TestDriverManagerConnectionFactory.java
index ce6b368c..d94065a1 100644
--- a/src/test/java/org/apache/commons/dbcp2/TestDriverManagerConnectionFactory.java
+++ b/src/test/java/org/apache/commons/dbcp2/TestDriverManagerConnectionFactory.java
@@ -81,47 +81,47 @@ public class TestDriverManagerConnectionFactory extends AbstractDriverTest {
         cf.createConnection();
     }
 
-    public void testDriverManagerInit(final boolean withProperties) throws Exception {
-        final GenericObjectPoolConfig<PoolableConnection> config = new GenericObjectPoolConfig<>();
-        config.setMaxTotal(10);
-        config.setMaxIdle(0);
-        final Properties properties = new Properties();
-        // The names "user" and "password" are specified in java.sql.DriverManager.getConnection(String, String, String)
-        properties.setProperty(Constants.KEY_USER, "foo");
-        properties.setProperty(Constants.KEY_PASSWORD, "bar");
-        final ConnectionFactory connectionFactory = withProperties ?
-                new DriverManagerConnectionFactory("jdbc:apache:commons:testdriver", properties) :
-                new DriverManagerConnectionFactory("jdbc:apache:commons:testdriver", "foo", "bar");
-        final PoolableConnectionFactory poolableConnectionFactory =
-            new PoolableConnectionFactory(connectionFactory, null);
-        poolableConnectionFactory.setDefaultReadOnly(Boolean.FALSE);
-        poolableConnectionFactory.setDefaultAutoCommit(Boolean.TRUE);
-
-        final GenericObjectPool<PoolableConnection> connectionPool =
-                new GenericObjectPool<>(poolableConnectionFactory, config);
-        poolableConnectionFactory.setPool(connectionPool);
-        final PoolingDataSource<PoolableConnection> dataSource =
-                new PoolingDataSource<>(connectionPool);
-
-        final ConnectionThread[] connectionThreads = new ConnectionThread[10];
-        final Thread[] threads = new Thread[10];
-
-        for (int i = 0; i < 10; i++) {
-            connectionThreads[i] = new ConnectionThread(dataSource);
-            threads[i] = new Thread(connectionThreads[i]);
-        }
-        for (int i = 0; i < 10; i++) {
-            threads[i].start();
-        }
-        for (int i = 0; i < 10; i++) {
-            while (threads[i].isAlive()){//JDK1.5: getState() != Thread.State.TERMINATED) {
-                Thread.sleep(100);
-            }
-            if (!connectionThreads[i].getResult()) {
-                fail("Exception during getConnection(): " + connectionThreads[i]);
-            }
-        }
-    }
+	public void testDriverManagerInit(final boolean withProperties) throws Exception {
+		final GenericObjectPoolConfig<PoolableConnection> config = new GenericObjectPoolConfig<>();
+		config.setMaxTotal(10);
+		config.setMaxIdle(0);
+		final Properties properties = new Properties();
+		// The names "user" and "password" are specified in
+		// java.sql.DriverManager.getConnection(String, String, String)
+		properties.setProperty(Constants.KEY_USER, "foo");
+		properties.setProperty(Constants.KEY_PASSWORD, "bar");
+		final ConnectionFactory connectionFactory = withProperties
+				? new DriverManagerConnectionFactory("jdbc:apache:commons:testdriver", properties)
+				: new DriverManagerConnectionFactory("jdbc:apache:commons:testdriver", "foo", "bar");
+		final PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
+				null);
+		poolableConnectionFactory.setDefaultReadOnly(Boolean.FALSE);
+		poolableConnectionFactory.setDefaultAutoCommit(Boolean.TRUE);
+
+		final GenericObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory,
+				config);
+		poolableConnectionFactory.setPool(connectionPool);
+		final PoolingDataSource<PoolableConnection> dataSource = new PoolingDataSource<>(connectionPool);
+
+		final ConnectionThread[] connectionThreads = new ConnectionThread[10];
+		final Thread[] threads = new Thread[10];
+
+		for (int i = 0; i < 10; i++) {
+			connectionThreads[i] = new ConnectionThread(dataSource);
+			threads[i] = new Thread(connectionThreads[i]);
+		}
+		for (int i = 0; i < 10; i++) {
+			threads[i].start();
+		}
+		for (int i = 0; i < 10; i++) {
+			while (threads[i].isAlive()) { // JDK1.5: getState() != Thread.State.TERMINATED) {
+				Thread.sleep(100);
+			}
+			if (!connectionThreads[i].getResult()) {
+				fail("Exception during getConnection(): " + connectionThreads[i]);
+			}
+		}
+	}
 
     @Test
     public void testDriverManagerInitWithCredentials() throws Exception {


(commons-dbcp) 06/19: Use JUnit 5's 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 9a4d0259de21bce4c37f23ff904d0c27c6a20d67
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Sat Dec 14 08:28:45 2024 -0500

    Use JUnit 5's assertThrows()
    
    Remove unused imports
---
 src/test/java/org/apache/commons/dbcp2/TestPoolingDriver.java | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)

diff --git a/src/test/java/org/apache/commons/dbcp2/TestPoolingDriver.java b/src/test/java/org/apache/commons/dbcp2/TestPoolingDriver.java
index 28f68e21..8db94ab0 100644
--- a/src/test/java/org/apache/commons/dbcp2/TestPoolingDriver.java
+++ b/src/test/java/org/apache/commons/dbcp2/TestPoolingDriver.java
@@ -22,8 +22,8 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertNotEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertNotSame;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.jupiter.api.Assertions.fail;
 
 import java.io.ByteArrayOutputStream;
 import java.io.OutputStreamWriter;
@@ -129,12 +129,7 @@ public class TestPoolingDriver extends TestConnectionPool {
         final PoolingDriver driver2 = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
         driver2.closePool("test");
 
-        try (Connection c = DriverManager.getConnection("jdbc:apache:commons:dbcp:test")) {
-            fail("expected SQLException");
-        }
-        catch (final SQLException e) {
-            // OK
-        }
+        assertThrows(SQLException.class, () -> DriverManager.getConnection("jdbc:apache:commons:dbcp:test"));
     }
 
     @Test


(commons-dbcp) 05/19: Use JUnit 5's 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 282d4214d9b983b96daac2d955d0be8c5f3c8f61
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Sat Dec 14 08:25:38 2024 -0500

    Use JUnit 5's assertThrows()
---
 .../java/org/apache/commons/dbcp2/TestPoolableConnection.java  | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)

diff --git a/src/test/java/org/apache/commons/dbcp2/TestPoolableConnection.java b/src/test/java/org/apache/commons/dbcp2/TestPoolableConnection.java
index 164cb25a..a49d10cd 100644
--- a/src/test/java/org/apache/commons/dbcp2/TestPoolableConnection.java
+++ b/src/test/java/org/apache/commons/dbcp2/TestPoolableConnection.java
@@ -121,13 +121,9 @@ public class TestPoolableConnection {
 
         // Set up non-fatal exception
         nativeConnection.setFailure(new SQLException("Not fatal error.", "Invalid syntax."));
-        try {
-            conn.createStatement();
-            fail("Should throw SQL exception.");
-        } catch (final SQLException ignored) {
-            // cleanup failure
-            nativeConnection.setFailure(null);
-        }
+        assertThrows(SQLException.class, conn::createStatement);
+        // cleanup failure
+        nativeConnection.setFailure(null);
 
         // validate should not fail - error was not fatal and condition was cleaned up
         conn.validate("SELECT 1", 1000);


(commons-dbcp) 03/19: Use JUnit 5's 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 0ad566aa4536c092258be7b29648ac25b7eed796
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Sat Dec 14 08:14:59 2024 -0500

    Use JUnit 5's assertThrows()
    
    Refactor test cleanup
---
 .../commons/dbcp2/TestDelegatingConnection.java    | 106 ++++++++-------------
 1 file changed, 41 insertions(+), 65 deletions(-)

diff --git a/src/test/java/org/apache/commons/dbcp2/TestDelegatingConnection.java b/src/test/java/org/apache/commons/dbcp2/TestDelegatingConnection.java
index b8ac354e..e4c3b881 100644
--- a/src/test/java/org/apache/commons/dbcp2/TestDelegatingConnection.java
+++ b/src/test/java/org/apache/commons/dbcp2/TestDelegatingConnection.java
@@ -23,11 +23,9 @@ 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;
 
 import java.sql.Connection;
 import java.sql.DriverManager;
-import java.sql.PreparedStatement;
 import java.sql.SQLException;
 import java.sql.Savepoint;
 
@@ -100,6 +98,8 @@ public class TestDelegatingConnection {
 
     @AfterEach
     public void afterEach() throws SQLException {
+        testerStatement.setSqlExceptionOnClose(false);
+        testerResultSet.setSqlExceptionOnClose(false);
         h2DConnection.close();
     }
 
@@ -141,34 +141,29 @@ public class TestDelegatingConnection {
      * Verify fix for DBCP-241
      */
     @Test
-    public void testCheckOpenNull() throws Exception {
-        delegatingConnection.close();
-        SQLException e = assertThrows(SQLException.class, delegatingConnection::checkOpen);
-        assertTrue(e.getMessage().endsWith("is closed."));
-
-        delegatingConnection = new DelegatingConnection<>(null);
-        delegatingConnection.setClosedInternal(true);
-        e = assertThrows(SQLException.class, delegatingConnection::checkOpen);
-        assertTrue(e.getMessage().endsWith("is null."));
-
-        final PoolingConnection pc = new PoolingConnection(connection2);
-        pc.setStatementPool(new GenericKeyedObjectPool<>(pc));
-        delegatingConnection = new DelegatingConnection<>(pc);
-        pc.close();
-        delegatingConnection.close();
-        try {
-            try (PreparedStatement ps = delegatingConnection.prepareStatement("")) {
-            }
-            fail("Expecting SQLException");
-        } catch (final SQLException ex) {
-            assertTrue(ex.getMessage().endsWith("is closed."));
-        }
+	public void testCheckOpenNull() throws Exception {
+		delegatingConnection.close();
+		SQLException e = assertThrows(SQLException.class, delegatingConnection::checkOpen);
+		assertTrue(e.getMessage().endsWith("is closed."));
 
-        delegatingConnection = new DelegatingConnection<>(new RTEGeneratingConnection());
-        delegatingConnection.close();
-        e = assertThrows(SQLException.class, delegatingConnection::checkOpen);
-        assertTrue(e.getMessage().endsWith("is closed."));
-    }
+		delegatingConnection = new DelegatingConnection<>(null);
+		delegatingConnection.setClosedInternal(true);
+		e = assertThrows(SQLException.class, delegatingConnection::checkOpen);
+		assertTrue(e.getMessage().endsWith("is null."));
+
+		final PoolingConnection pc = new PoolingConnection(connection2);
+		pc.setStatementPool(new GenericKeyedObjectPool<>(pc));
+		delegatingConnection = new DelegatingConnection<>(pc);
+		pc.close();
+		delegatingConnection.close();
+		e = assertThrows(SQLException.class, () -> delegatingConnection.prepareStatement(""));
+		assertTrue(e.getMessage().endsWith("is closed."));
+
+		delegatingConnection = new DelegatingConnection<>(new RTEGeneratingConnection());
+		delegatingConnection.close();
+		e = assertThrows(SQLException.class, delegatingConnection::checkOpen);
+		assertTrue(e.getMessage().endsWith("is closed."));
+	}
 
     @Test
     public void testCommit() throws Exception {
@@ -289,50 +284,31 @@ public class TestDelegatingConnection {
 
     @Test
     public void testPassivateWithResultSetCloseException() {
-        try {
-            testerResultSet.setSqlExceptionOnClose(true);
-            delegatingConnection.addTrace(testerResultSet);
-            delegatingConnection.passivate();
-            Assertions.fail("Expected SQLExceptionList");
-        } catch (final SQLException e) {
-            Assertions.assertInstanceOf(SQLExceptionList.class, e);
-            Assertions.assertEquals(1, ((SQLExceptionList) e).getCauseList().size());
-        } finally {
-            testerResultSet.setSqlExceptionOnClose(false);
-        }
+        testerResultSet.setSqlExceptionOnClose(true);
+        delegatingConnection.addTrace(testerResultSet);
+        final SQLException e = assertThrows(SQLException.class, delegatingConnection::passivate);
+        Assertions.assertInstanceOf(SQLExceptionList.class, e);
+        Assertions.assertEquals(1, ((SQLExceptionList) e).getCauseList().size());
     }
 
     @Test
     public void testPassivateWithResultSetCloseExceptionAndStatementCloseException() {
-        try {
-            testerStatement.setSqlExceptionOnClose(true);
-            testerResultSet.setSqlExceptionOnClose(true);
-            delegatingConnection.addTrace(testerStatement);
-            delegatingConnection.addTrace(testerResultSet);
-            delegatingConnection.passivate();
-            Assertions.fail("Expected SQLExceptionList");
-        } catch (final SQLException e) {
-            Assertions.assertInstanceOf(SQLExceptionList.class, e);
-            Assertions.assertEquals(2, ((SQLExceptionList) e).getCauseList().size());
-        } finally {
-            testerStatement.setSqlExceptionOnClose(false);
-            testerResultSet.setSqlExceptionOnClose(false);
-        }
+        testerStatement.setSqlExceptionOnClose(true);
+        testerResultSet.setSqlExceptionOnClose(true);
+        delegatingConnection.addTrace(testerStatement);
+        delegatingConnection.addTrace(testerResultSet);
+        final SQLException e = assertThrows(SQLException.class, delegatingConnection::passivate);
+        Assertions.assertInstanceOf(SQLExceptionList.class, e);
+        Assertions.assertEquals(2, ((SQLExceptionList) e).getCauseList().size());
     }
 
     @Test
     public void testPassivateWithStatementCloseException() {
-        try {
-            testerStatement.setSqlExceptionOnClose(true);
-            delegatingConnection.addTrace(testerStatement);
-            delegatingConnection.passivate();
-            Assertions.fail("Expected SQLExceptionList");
-        } catch (final SQLException e) {
-            Assertions.assertInstanceOf(SQLExceptionList.class, e);
-            Assertions.assertEquals(1, ((SQLExceptionList) e).getCauseList().size());
-        } finally {
-            testerStatement.setSqlExceptionOnClose(false);
-        }
+        testerStatement.setSqlExceptionOnClose(true);
+        delegatingConnection.addTrace(testerStatement);
+        final SQLException e = assertThrows(SQLException.class, delegatingConnection::passivate);
+        Assertions.assertInstanceOf(SQLExceptionList.class, e);
+        Assertions.assertEquals(1, ((SQLExceptionList) e).getCauseList().size());
     }
 
     @Test


(commons-dbcp) 10/19: Remove unused imports

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 02cc073ce0c16f864737a9f4b75ee91bd3860837
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Sat Dec 14 08:29:24 2024 -0500

    Remove unused imports
---
 .../apache/commons/dbcp2/datasources/KeyedCPDSConnectionFactory.java | 1 +
 .../org/apache/commons/dbcp2/datasources/SharedPoolDataSource.java   | 1 +
 .../org/apache/commons/dbcp2/TestPStmtPoolingBasicDataSource.java    | 5 +----
 3 files changed, 3 insertions(+), 4 deletions(-)

diff --git a/src/main/java/org/apache/commons/dbcp2/datasources/KeyedCPDSConnectionFactory.java b/src/main/java/org/apache/commons/dbcp2/datasources/KeyedCPDSConnectionFactory.java
index db7fee5c..e31631f8 100644
--- a/src/main/java/org/apache/commons/dbcp2/datasources/KeyedCPDSConnectionFactory.java
+++ b/src/main/java/org/apache/commons/dbcp2/datasources/KeyedCPDSConnectionFactory.java
@@ -31,6 +31,7 @@ import javax.sql.ConnectionEventListener;
 import javax.sql.ConnectionPoolDataSource;
 import javax.sql.PooledConnection;
 
+import org.apache.commons.dbcp2.PoolableConnection;
 import org.apache.commons.dbcp2.Utils;
 import org.apache.commons.pool2.KeyedObjectPool;
 import org.apache.commons.pool2.KeyedPooledObjectFactory;
diff --git a/src/main/java/org/apache/commons/dbcp2/datasources/SharedPoolDataSource.java b/src/main/java/org/apache/commons/dbcp2/datasources/SharedPoolDataSource.java
index 1de7ccff..cdfcab77 100644
--- a/src/main/java/org/apache/commons/dbcp2/datasources/SharedPoolDataSource.java
+++ b/src/main/java/org/apache/commons/dbcp2/datasources/SharedPoolDataSource.java
@@ -26,6 +26,7 @@ import javax.naming.Reference;
 import javax.naming.StringRefAddr;
 import javax.sql.ConnectionPoolDataSource;
 
+import org.apache.commons.dbcp2.PoolableConnection;
 import org.apache.commons.pool2.KeyedObjectPool;
 import org.apache.commons.pool2.KeyedPooledObjectFactory;
 import org.apache.commons.pool2.impl.GenericKeyedObjectPool;
diff --git a/src/test/java/org/apache/commons/dbcp2/TestPStmtPoolingBasicDataSource.java b/src/test/java/org/apache/commons/dbcp2/TestPStmtPoolingBasicDataSource.java
index 11deb0dd..e24501a8 100644
--- a/src/test/java/org/apache/commons/dbcp2/TestPStmtPoolingBasicDataSource.java
+++ b/src/test/java/org/apache/commons/dbcp2/TestPStmtPoolingBasicDataSource.java
@@ -122,10 +122,7 @@ public class TestPStmtPoolingBasicDataSource extends TestBasicDataSource {
         assertNotSame(stmt1, stmt2);
 
         // go over the maxOpen limit
-        try (PreparedStatement ps = conn.prepareStatement("select 'c' from dual")) {
-            fail("expected SQLException");
-        }
-        catch (final SQLException e) {}
+        assertThrows(SQLException.class, () -> conn.prepareStatement("select 'c' from dual"));
 
         // make idle
         stmt2.close();


(commons-dbcp) 02/19: Normalize instance parameter names

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 185deebd61c3ed7c54a0549835f4be3527b77bb5
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Sat Dec 14 08:07:24 2024 -0500

    Normalize instance parameter names
---
 src/test/java/org/apache/commons/dbcp2/TesterStatement.java | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/test/java/org/apache/commons/dbcp2/TesterStatement.java b/src/test/java/org/apache/commons/dbcp2/TesterStatement.java
index 76437269..705df9bf 100644
--- a/src/test/java/org/apache/commons/dbcp2/TesterStatement.java
+++ b/src/test/java/org/apache/commons/dbcp2/TesterStatement.java
@@ -398,8 +398,8 @@ public class TesterStatement extends AbandonedTrace implements Statement {
         this.queryTimeout = seconds;
     }
 
-    public void setSqlExceptionOnClose(final boolean _sqlExceptionOnClose) {
-        this.sqlExceptionOnClose = _sqlExceptionOnClose;
+    public void setSqlExceptionOnClose(final boolean sqlExceptionOnClose) {
+        this.sqlExceptionOnClose = sqlExceptionOnClose;
     }
 
     @Override


(commons-dbcp) 15/19: Use JUnit 5's assertThrows() and friends

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 4eb708a16b504060bafe4ad4aa9142938042e7a6
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Sat Dec 14 09:02:46 2024 -0500

    Use JUnit 5's assertThrows() and friends
---
 .../dbcp2/managed/TestManagedDataSourceInTx.java   | 55 ++++++++--------------
 1 file changed, 20 insertions(+), 35 deletions(-)

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 70aa10b5..018d1867 100644
--- a/src/test/java/org/apache/commons/dbcp2/managed/TestManagedDataSourceInTx.java
+++ b/src/test/java/org/apache/commons/dbcp2/managed/TestManagedDataSourceInTx.java
@@ -17,6 +17,7 @@
  */
 package org.apache.commons.dbcp2.managed;
 
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertNotEquals;
@@ -41,6 +42,7 @@ import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.function.ThrowingSupplier;
 
 /**
  * Tests ManagedDataSource with an active transaction in progress.
@@ -225,50 +227,33 @@ public class TestManagedDataSourceInTx extends TestManagedDataSource {
                     // Ignore
                 }
                 assertEquals(numActive, pool.getNumActive());
-                try {
-                    conn.close();
-                } catch (final Exception e) {
-                    fail("Should have been able to close the connection");
-                }
+                assertDoesNotThrow(conn::close, "Should have been able to close the connection");
                 // TODO Requires DBCP-515 assertTrue(numActive -1 == pool.getNumActive());
             }
 
             @Override
-            public void beforeCompletion() {
-                try {
-                    conn = (ManagedConnection<?>) ds.getConnection();
-                    assertNotNull(conn);
-                } catch (final SQLException e) {
-                    fail("Could not get connection");
-                }
-            }
+			public void beforeCompletion() {
+				assertDoesNotThrow(() -> conn = (ManagedConnection<?>) ds.getConnection(), "Could not get connection");
+			}
         });
         transactionManager.commit();
     }
 
     @Test
-    public void testGetConnectionInAfterCompletion() throws Exception {
-        try (DelegatingConnection<?> connection = (DelegatingConnection<?>) newConnection()) {
-            // Don't close so we can check it for warnings in afterCompletion
-            transactionManager.getTransaction().registerSynchronization(new SynchronizationAdapter() {
-                @Override
-                public void afterCompletion(final int i) {
-                    try {
-                        final Connection connection1 = ds.getConnection();
-                        try {
-                            connection1.getWarnings();
-                            fail("Could operate on closed connection");
-                        } catch (final SQLException e) {
-                            // This is expected
-                        }
-                    } catch (final SQLException e) {
-                        fail("Should have been able to get connection");
-                    }
-                }
-            });
-        }
-        transactionManager.commit();
-    }
+	public void testGetConnectionInAfterCompletion() throws Exception {
+		try (DelegatingConnection<?> connection = (DelegatingConnection<?>) newConnection()) {
+			// Don't close so we can check it for warnings in afterCompletion
+			transactionManager.getTransaction().registerSynchronization(new SynchronizationAdapter() {
+				@Override
+				public void afterCompletion(final int i) {
+					final Connection connection1 = assertDoesNotThrow((ThrowingSupplier<Connection>) ds::getConnection);
+					assertThrows(SQLException.class, () -> connection1.getWarnings(),
+							"Could operate on closed connection");
+				}
+			});
+		}
+		transactionManager.commit();
+	}
 
     @Override
     @Test


(commons-dbcp) 18/19: Use JUnit 5's assertThrows() and friends

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 406189afbc7896b6e6aba8b6340157be906f408e
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Sat Dec 14 09:03:24 2024 -0500

    Use JUnit 5's assertThrows() and friends
---
 .../datasources/TestSharedPoolDataSource.java      | 52 +++++++---------------
 1 file changed, 16 insertions(+), 36 deletions(-)

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 f3f6dfd3..c8bba318 100644
--- a/src/test/java/org/apache/commons/dbcp2/datasources/TestSharedPoolDataSource.java
+++ b/src/test/java/org/apache/commons/dbcp2/datasources/TestSharedPoolDataSource.java
@@ -22,7 +22,6 @@ 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;
 
 import java.sql.CallableStatement;
 import java.sql.Connection;
@@ -453,39 +452,22 @@ public class TestSharedPoolDataSource extends TestConnectionPool {
      * JIRA: DBCP-245
      */
     @Test
-    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");
-        } catch (final SQLException e1) {
-            // should fail
-        }
-
-        // Use good password
-        ds.getConnection("u1", "p1").close();
-        try (Connection c = ds.getConnection("u1", "x")) {
-            fail("Able to retrieve connection with incorrect password");
-        } catch (final SQLException e) {
-            if (!e.getMessage().startsWith("Given password did not match")) {
-                throw e;
-            }
-            // else the exception was expected
-        }
-
-        // Make sure we can still use our good password.
-        ds.getConnection("u1", "p1").close();
-
-        // Try related users and passwords
-        ds.getConnection("foo", "bar").close();
-        try (Connection c = ds.getConnection("u1", "ar")) {
-            fail("Should have caused an SQLException");
-        } catch (final SQLException expected) {
-        }
-        try (Connection c = ds.getConnection("u1", "baz")) {
-            fail("Should have generated SQLException");
-        } catch (final SQLException expected) {
-        }
-    }
+	public void testIncorrectPassword() throws SQLException {
+		ds.getConnection("u2", "p2").close();
+		assertThrows(SQLException.class, () -> ds.getConnection("u1", "zlsafjk"),
+				"Able to retrieve connection with incorrect password");
+		// Use good password
+		ds.getConnection("u1", "p1").close();
+		final SQLException e = assertThrows(SQLException.class, () -> ds.getConnection("u1", "x"),
+				"Able to retrieve connection with incorrect password");
+		assertTrue(e.getMessage().startsWith("Given password did not match"));
+		// Make sure we can still use our good password.
+		ds.getConnection("u1", "p1").close();
+		// Try related users and passwords
+		ds.getConnection("foo", "bar").close();
+		assertThrows(SQLException.class, () -> ds.getConnection("u1", "ar"));
+		assertThrows(SQLException.class, () -> ds.getConnection("u1", "baz"));
+	}
 
     @Override
     @Test
@@ -495,9 +477,7 @@ public class TestSharedPoolDataSource extends TestConnectionPool {
             c[i] = ds.getConnection();
             assertNotNull(c[i]);
         }
-
         assertThrows(SQLException.class, ds::getConnection, "Allowed to open more than DefaultMaxTotal connections.");
-
         for (final Connection element : c) {
             element.close();
         }


(commons-dbcp) 09/19: Use JUnit 5's 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 1c668cd9d97af30864cbde465a19b64aa908aca8
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Sat Dec 14 08:29:11 2024 -0500

    Use JUnit 5's assertThrows()
    
    Remove unused imports
---
 src/test/java/org/apache/commons/dbcp2/TestPStmtPooling.java | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/src/test/java/org/apache/commons/dbcp2/TestPStmtPooling.java b/src/test/java/org/apache/commons/dbcp2/TestPStmtPooling.java
index ab141289..674f6520 100644
--- a/src/test/java/org/apache/commons/dbcp2/TestPStmtPooling.java
+++ b/src/test/java/org/apache/commons/dbcp2/TestPStmtPooling.java
@@ -20,8 +20,8 @@ package org.apache.commons.dbcp2;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertNotSame;
 import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.jupiter.api.Assertions.fail;
 
 import java.sql.Connection;
 import java.sql.DriverManager;
@@ -172,11 +172,8 @@ public class TestPStmtPooling {
         final Connection poolingConnection =
             ((DelegatingConnection<?>) poolableConnection).getDelegate();
         poolingConnection.close();
-        try (PreparedStatement ps = conn.prepareStatement("select 1 from dual")) {
-            fail("Expecting SQLException");
-        } catch (final SQLException ex) {
-            assertTrue(ex.getMessage().endsWith("invalid PoolingConnection."));
-        }
+        final SQLException ex = assertThrows(SQLException.class, () -> conn.prepareStatement("select 1 from dual"));
+        assertTrue(ex.getMessage().endsWith("invalid PoolingConnection."));
         ds.close();
     }
 


(commons-dbcp) 17/19: Remove unused import

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 b269c5da5acb25d44b99e9f0b93d8f393af6f7c0
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Sat Dec 14 09:03:14 2024 -0500

    Remove unused import
---
 .../java/org/apache/commons/dbcp2/TestPStmtPoolingBasicDataSource.java   | 1 -
 1 file changed, 1 deletion(-)

diff --git a/src/test/java/org/apache/commons/dbcp2/TestPStmtPoolingBasicDataSource.java b/src/test/java/org/apache/commons/dbcp2/TestPStmtPoolingBasicDataSource.java
index e24501a8..227253b7 100644
--- a/src/test/java/org/apache/commons/dbcp2/TestPStmtPoolingBasicDataSource.java
+++ b/src/test/java/org/apache/commons/dbcp2/TestPStmtPoolingBasicDataSource.java
@@ -23,7 +23,6 @@ import static org.junit.jupiter.api.Assertions.assertNotSame;
 import static org.junit.jupiter.api.Assertions.assertSame;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.jupiter.api.Assertions.fail;
 
 import java.sql.Connection;
 import java.sql.PreparedStatement;