Polish "Fix DataSourceUtils inconsistent exception handling"

See gh-28669
This commit is contained in:
Stephane Nicoll
2022-06-23 08:59:42 +02:00
parent edfe5d2f18
commit e98b602d4f
2 changed files with 15 additions and 9 deletions

View File

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

View File

@@ -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 {
@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");
}
}