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:08:07 UTC

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

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();
         }