From 2d76de29ffa3c457ba71f732f9db6280ebad9110 Mon Sep 17 00:00:00 2001 From: Shraddha Yeole Date: Thu, 1 Oct 2020 16:24:43 -0700 Subject: [PATCH 1/2] Do not fail if h2Console bean cannot connect to db See gh-23566 --- .../h2/H2ConsoleAutoConfiguration.java | 2 +- .../h2/H2ConsoleAutoConfigurationTests.java | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfiguration.java index c857d486fc..ca3fb981b7 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfiguration.java @@ -74,7 +74,7 @@ public class H2ConsoleAutoConfiguration { logger.info("H2 console available at '" + path + "'. Database available at '" + connection.getMetaData().getURL() + "'"); } - catch (SQLException ex) { + catch (Exception ex) { // Continue } }); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java index dd8a06feb1..c6568c8182 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java @@ -17,11 +17,13 @@ package org.springframework.boot.autoconfigure.h2; import java.sql.Connection; +import java.sql.SQLException; import javax.sql.DataSource; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.BDDMockito; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanCreationException; @@ -31,8 +33,11 @@ import org.springframework.boot.test.context.runner.WebApplicationContextRunner; import org.springframework.boot.test.system.CapturedOutput; import org.springframework.boot.test.system.OutputCaptureExtension; import org.springframework.boot.web.servlet.ServletRegistrationBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; /** * Tests for {@link H2ConsoleAutoConfiguration} @@ -117,4 +122,25 @@ class H2ConsoleAutoConfigurationTests { }); } + @Test + void testDataSource() { + this.contextRunner.withUserConfiguration(DataSourceAvailable.class) + .withPropertyValues("spring.h2.console.enabled=true").run((context) -> { + assertThat(context.isRunning()).isTrue(); + }); + } + + @Configuration(proxyBeanMethods = false) + static class DataSourceAvailable { + + @Bean + public DataSource dataSource() throws SQLException { + DataSource dataSource = mock(DataSource.class); + BDDMockito.when(dataSource.getConnection()).thenThrow(IllegalStateException.class); + return dataSource; + } + + } + + } From c0f158dffde52f726c0938065114b747afab07a0 Mon Sep 17 00:00:00 2001 From: Madhura Bhave Date: Thu, 1 Oct 2020 18:16:11 -0700 Subject: [PATCH 2/2] Polish "Do not fail if h2Console bean cannot connect to db" See gh-23566 --- .../h2/H2ConsoleAutoConfiguration.java | 1 - .../h2/H2ConsoleAutoConfigurationTests.java | 19 +++++++++---------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfiguration.java index ca3fb981b7..60174667fb 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfiguration.java @@ -17,7 +17,6 @@ package org.springframework.boot.autoconfigure.h2; import java.sql.Connection; -import java.sql.SQLException; import javax.sql.DataSource; diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java index c6568c8182..6d9b2d90c7 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java @@ -23,7 +23,6 @@ import javax.sql.DataSource; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.BDDMockito; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanCreationException; @@ -37,6 +36,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; /** @@ -45,6 +45,7 @@ import static org.mockito.Mockito.mock; * @author Andy Wilkinson * @author Marten Deinum * @author Stephane Nicoll + * @author Shraddha Yeole */ class H2ConsoleAutoConfigurationTests { @@ -123,24 +124,22 @@ class H2ConsoleAutoConfigurationTests { } @Test - void testDataSource() { - this.contextRunner.withUserConfiguration(DataSourceAvailable.class) - .withPropertyValues("spring.h2.console.enabled=true").run((context) -> { - assertThat(context.isRunning()).isTrue(); - }); + void h2ConsoleShouldNotFailIfDatabaseConnectionFails() { + this.contextRunner.withUserConfiguration(CustomDataSourceConfiguration.class) + .withPropertyValues("spring.h2.console.enabled=true") + .run((context) -> assertThat(context.isRunning()).isTrue()); } @Configuration(proxyBeanMethods = false) - static class DataSourceAvailable { + static class CustomDataSourceConfiguration { @Bean - public DataSource dataSource() throws SQLException { + DataSource dataSource() throws SQLException { DataSource dataSource = mock(DataSource.class); - BDDMockito.when(dataSource.getConnection()).thenThrow(IllegalStateException.class); + given(dataSource.getConnection()).willThrow(IllegalStateException.class); return dataSource; } } - }