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:22:05 UTC

(commons-dbcp) 02/04: 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 dc5c62faff07404fcfecef4c7f8c700d8403b37d
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Sat Dec 14 09:21:16 2024 -0500

    Use JUnit 5's assertThrows() and friends
---
 .../dbcp2/managed/TestConnectionWithNarayana.java  | 49 +++++++++-------------
 1 file changed, 19 insertions(+), 30 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 f36aa671..7ae6a454 100644
--- a/src/test/java/org/apache/commons/dbcp2/managed/TestConnectionWithNarayana.java
+++ b/src/test/java/org/apache/commons/dbcp2/managed/TestConnectionWithNarayana.java
@@ -19,6 +19,7 @@ package org.apache.commons.dbcp2.managed;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 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;
@@ -185,34 +186,22 @@ public class TestConnectionWithNarayana {
 	}
 
     @Test
-    public void testRepeatedGetConnectionInTimeout() throws Exception {
-        mds.getTransactionManager().setTransactionTimeout(1);
-        mds.getTransactionManager().begin();
-
-        try {
-            do {
-                Thread.sleep(1000);
-            } 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) {
-                if (!e.getCause().getClass().equals(IllegalStateException.class)) {
-                    throw e;
-                }
-                try (Connection conn = mds.getConnection()) {
-                    fail("Should not get connection 2");
-                } catch (final SQLException e2) {
-                    if (!e2.getCause().getClass().equals(IllegalStateException.class)) {
-                        throw e2;
-                    }
-                }
-            }
-        } finally {
-            mds.getTransactionManager().rollback();
-        }
-        Assertions.assertEquals(0, mds.getNumActive());
-    }
+	public void testRepeatedGetConnectionInTimeout() throws Exception {
+		mds.getTransactionManager().setTransactionTimeout(1);
+		mds.getTransactionManager().begin();
+		try {
+			do {
+				Thread.sleep(1000);
+			} while (mds.getTransactionManager().getTransaction().getStatus() != Status.STATUS_ROLLEDBACK);
+			// Let the reaper do it's thing
+			Thread.sleep(1000);
+			final SQLException e = assertThrows(SQLException.class, mds::getConnection);
+			assertTrue(e.getCause().getClass().equals(IllegalStateException.class));
+			final SQLException e2 = assertThrows(SQLException.class, mds::getConnection);
+			assertTrue(e2.getCause().getClass().equals(IllegalStateException.class));
+		} finally {
+			mds.getTransactionManager().rollback();
+		}
+		Assertions.assertEquals(0, mds.getNumActive());
+	}
 }
\ No newline at end of file