You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2023/08/27 19:40:41 UTC
[commons-dbcp] 03/03: Use assertThrows()
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 fc7fef8d78f6ca2d9154434c1af757f500810fcf
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Aug 27 15:40:33 2023 -0400
Use assertThrows()
---
.../apache/commons/dbcp2/TestBasicDataSource.java | 48 ++++------------------
.../apache/commons/dbcp2/TestConnectionPool.java | 11 ++---
.../datasources/TestPerUserPoolDataSource.java | 5 +--
3 files changed, 14 insertions(+), 50 deletions(-)
diff --git a/src/test/java/org/apache/commons/dbcp2/TestBasicDataSource.java b/src/test/java/org/apache/commons/dbcp2/TestBasicDataSource.java
index aa1d18d0..bfa6f71b 100644
--- a/src/test/java/org/apache/commons/dbcp2/TestBasicDataSource.java
+++ b/src/test/java/org/apache/commons/dbcp2/TestBasicDataSource.java
@@ -563,28 +563,16 @@ public class TestBasicDataSource extends TestConnectionPool {
@Test
public void testInvalidConnectionInitSql() {
- try {
- ds.setConnectionInitSqls(Arrays.asList("SELECT 1", "invalid"));
- try (Connection c = ds.getConnection()) {}
- fail("expected SQLException");
- }
- catch (final SQLException e) {
- if (!e.toString().contains("invalid")) {
- fail("expected detailed error message");
- }
- }
+ ds.setConnectionInitSqls(Arrays.asList("SELECT 1", "invalid"));
+ final SQLException e = assertThrows(SQLException.class, ds::getConnection);
+ assertTrue(e.toString().contains("invalid"));
}
@Test
public void testInvalidValidationQuery() {
ds.setValidationQuery("invalid");
- try (Connection c = ds.getConnection()) {
- fail("expected SQLException");
- } catch (final SQLException e) {
- if (!e.toString().contains("invalid")) {
- fail("expected detailed error message");
- }
- }
+ final SQLException e = assertThrows(SQLException.class, ds::getConnection);
+ assertTrue(e.toString().contains("invalid"));
}
// Bugzilla Bug 28251: Returning dead database connections to BasicDataSource
@@ -753,15 +741,7 @@ public class TestBasicDataSource extends TestConnectionPool {
@Test
public void testMaxTotalZero() throws Exception {
ds.setMaxTotal(0);
-
- try {
- final Connection conn = ds.getConnection();
- assertNotNull(conn);
- fail("SQLException expected");
-
- } catch (final SQLException e) {
- // test OK
- }
+ assertThrows(SQLException.class, ds::getConnection);
}
/**
@@ -991,12 +971,7 @@ public class TestBasicDataSource extends TestConnectionPool {
assertEquals("", ds.getConnectionProperties().getProperty("name1"));
// null should throw a NullPointerException
- try {
- ds.setConnectionProperties(null);
- fail("Expected NullPointerException");
- } catch (final NullPointerException e) {
- // expected
- }
+ assertThrows(NullPointerException.class, () -> ds.setConnectionProperties(null));
}
@Test
@@ -1125,13 +1100,8 @@ public class TestBasicDataSource extends TestConnectionPool {
public void testValidationQueryTimoutFail() {
ds.setTestOnBorrow(true);
ds.setValidationQueryTimeout(Duration.ofSeconds(3)); // Too fast for TesterStatement
- try (Connection c = ds.getConnection()) {
- fail("expected SQLException");
- } catch (final SQLException ex) {
- if (!ex.toString().contains("timeout")) {
- fail("expected timeout error message");
- }
- }
+ final SQLException e = assertThrows(SQLException.class, ds::getConnection);
+ assertTrue(e.toString().contains("timeout"));
}
}
diff --git a/src/test/java/org/apache/commons/dbcp2/TestConnectionPool.java b/src/test/java/org/apache/commons/dbcp2/TestConnectionPool.java
index a34fe13c..362f29eb 100644
--- a/src/test/java/org/apache/commons/dbcp2/TestConnectionPool.java
+++ b/src/test/java/org/apache/commons/dbcp2/TestConnectionPool.java
@@ -24,6 +24,7 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertNull;
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;
@@ -758,13 +759,9 @@ public abstract class TestConnectionPool {
assertNotNull(c[i]);
}
- try {
- newConnection();
- fail("Allowed to open more than DefaultMaxTotal connections.");
- } catch (final java.sql.SQLException e) {
- // should only be able to open 10 connections, so this test should
- // throw an exception
- }
+ // should only be able to open 10 connections, so this test should
+ // throw an exception
+ assertThrows(SQLException.class, this::newConnection);
for (final Connection element : c) {
element.close();
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 be414d66..8e67cd15 100644
--- a/src/test/java/org/apache/commons/dbcp2/datasources/TestPerUserPoolDataSource.java
+++ b/src/test/java/org/apache/commons/dbcp2/datasources/TestPerUserPoolDataSource.java
@@ -93,10 +93,7 @@ public class TestPerUserPoolDataSource extends TestConnectionPool {
// See DBCP-8
@Test
public void testChangePassword() throws Exception {
- try (Connection c = ds.getConnection(user, "bay")){
- fail("Should have generated SQLException");
- } catch (final SQLException expected) {
- }
+ assertThrows(SQLException.class, () -> ds.getConnection(user, "bay"));
final Connection con1 = ds.getConnection(user, "bar");
final Connection con2 = ds.getConnection(user, "bar");
final Connection con3 = ds.getConnection(user, "bar");