Browse Source

Polish "Fix DataSourceUtils inconsistent exception handling"

See gh-28669
pull/32614/head
Stephane Nicoll 4 years ago
parent
commit
e98b602d4f
  1. 7
      spring-jdbc/src/main/java/org/springframework/jdbc/CannotGetJdbcConnectionException.java
  2. 17
      spring-jdbc/src/test/java/org/springframework/jdbc/datasource/DataSourceUtilsTests.java

7
spring-jdbc/src/main/java/org/springframework/jdbc/CannotGetJdbcConnectionException.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -48,6 +48,11 @@ public class CannotGetJdbcConnectionException extends DataAccessResourceFailureE @@ -48,6 +48,11 @@ public class CannotGetJdbcConnectionException extends DataAccessResourceFailureE
super(msg, ex);
}
/**
* Constructor for CannotGetJdbcConnectionException.
* @param msg the detail message
* @param ex the root cause IllegalStateException
*/
public CannotGetJdbcConnectionException(String msg, IllegalStateException ex) {
super(msg, ex);
}

17
spring-jdbc/src/test/java/org/springframework/jdbc/datasource/DataSourceUtilsTests.java

@ -29,18 +29,17 @@ import static org.mockito.BDDMockito.when; @@ -29,18 +29,17 @@ import static org.mockito.BDDMockito.when;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link DataSourceUtils}.
*
* @author Kevin Schoenfeld
* @see org.springframework.jdbc.datasource.DataSourceUtilsTests
* @since 21.06.2022
* @author Stephane Nicoll
*/
class DataSourceUtilsTests {
@Test
void testConnectionNotAcquiredExceptionIsPropagated() throws SQLException {
final DataSource dataSource = mock(DataSource.class);
DataSource dataSource = mock(DataSource.class);
when(dataSource.getConnection()).thenReturn(null);
assertThatThrownBy(() -> DataSourceUtils.getConnection(dataSource))
.isInstanceOf(CannotGetJdbcConnectionException.class)
.hasMessageStartingWith("Failed to obtain JDBC Connection")
@ -49,12 +48,14 @@ class DataSourceUtilsTests { @@ -49,12 +48,14 @@ class DataSourceUtilsTests {
@Test
void testConnectionSQLExceptionIsPropagated() throws SQLException {
final DataSource dataSource = mock(DataSource.class);
DataSource dataSource = mock(DataSource.class);
when(dataSource.getConnection()).thenThrow(new SQLException("my dummy exception"));
assertThatThrownBy(() -> DataSourceUtils.getConnection(dataSource))
.isInstanceOf(CannotGetJdbcConnectionException.class)
.hasMessageStartingWith("Failed to obtain JDBC Connection")
.hasCauseInstanceOf(SQLException.class);
.cause().isInstanceOf(SQLException.class)
.hasMessage("my dummy exception");
}
}

Loading…
Cancel
Save