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 @@
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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); super(msg, ex);
} }
/**
* Constructor for CannotGetJdbcConnectionException.
* @param msg the detail message
* @param ex the root cause IllegalStateException
*/
public CannotGetJdbcConnectionException(String msg, IllegalStateException ex) { public CannotGetJdbcConnectionException(String msg, IllegalStateException ex) {
super(msg, 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;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
/** /**
* Tests for {@link DataSourceUtils}.
*
* @author Kevin Schoenfeld * @author Kevin Schoenfeld
* @see org.springframework.jdbc.datasource.DataSourceUtilsTests * @author Stephane Nicoll
* @since 21.06.2022
*/ */
class DataSourceUtilsTests { class DataSourceUtilsTests {
@Test @Test
void testConnectionNotAcquiredExceptionIsPropagated() throws SQLException { void testConnectionNotAcquiredExceptionIsPropagated() throws SQLException {
final DataSource dataSource = mock(DataSource.class); DataSource dataSource = mock(DataSource.class);
when(dataSource.getConnection()).thenReturn(null); when(dataSource.getConnection()).thenReturn(null);
assertThatThrownBy(() -> DataSourceUtils.getConnection(dataSource)) assertThatThrownBy(() -> DataSourceUtils.getConnection(dataSource))
.isInstanceOf(CannotGetJdbcConnectionException.class) .isInstanceOf(CannotGetJdbcConnectionException.class)
.hasMessageStartingWith("Failed to obtain JDBC Connection") .hasMessageStartingWith("Failed to obtain JDBC Connection")
@ -49,12 +48,14 @@ class DataSourceUtilsTests {
@Test @Test
void testConnectionSQLExceptionIsPropagated() throws SQLException { void testConnectionSQLExceptionIsPropagated() throws SQLException {
final DataSource dataSource = mock(DataSource.class); DataSource dataSource = mock(DataSource.class);
when(dataSource.getConnection()).thenThrow(new SQLException("my dummy exception")); when(dataSource.getConnection()).thenThrow(new SQLException("my dummy exception"));
assertThatThrownBy(() -> DataSourceUtils.getConnection(dataSource)) assertThatThrownBy(() -> DataSourceUtils.getConnection(dataSource))
.isInstanceOf(CannotGetJdbcConnectionException.class) .isInstanceOf(CannotGetJdbcConnectionException.class)
.hasMessageStartingWith("Failed to obtain JDBC Connection") .hasMessageStartingWith("Failed to obtain JDBC Connection")
.hasCauseInstanceOf(SQLException.class); .cause().isInstanceOf(SQLException.class)
.hasMessage("my dummy exception");
} }
} }

Loading…
Cancel
Save