|
|
|
|
@ -17,6 +17,7 @@
@@ -17,6 +17,7 @@
|
|
|
|
|
package org.springframework.boot.autoconfigure.h2; |
|
|
|
|
|
|
|
|
|
import java.sql.Connection; |
|
|
|
|
import java.sql.DatabaseMetaData; |
|
|
|
|
import java.sql.SQLException; |
|
|
|
|
|
|
|
|
|
import javax.sql.DataSource; |
|
|
|
|
@ -33,6 +34,7 @@ import org.springframework.boot.test.system.OutputCaptureExtension;
@@ -33,6 +34,7 @@ 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 org.springframework.context.annotation.Primary; |
|
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
|
|
import static org.mockito.BDDMockito.given; |
|
|
|
|
@ -114,25 +116,42 @@ class H2ConsoleAutoConfigurationTests {
@@ -114,25 +116,42 @@ class H2ConsoleAutoConfigurationTests {
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
@ExtendWith(OutputCaptureExtension.class) |
|
|
|
|
void dataSourceUrlIsLoggedWhenAvailable(CapturedOutput output) { |
|
|
|
|
void singleDataSourceUrlIsLoggedWhenOnlyOneAvailable(CapturedOutput output) { |
|
|
|
|
this.contextRunner.withConfiguration(AutoConfigurations.of(DataSourceAutoConfiguration.class)) |
|
|
|
|
.withPropertyValues("spring.h2.console.enabled=true").run((context) -> { |
|
|
|
|
try (Connection connection = context.getBean(DataSource.class).getConnection()) { |
|
|
|
|
assertThat(output) |
|
|
|
|
.contains("Database available at '" + connection.getMetaData().getURL() + "'"); |
|
|
|
|
.contains("H2 console available at '/h2-console'. Database(s) available at '" + connection.getMetaData().getURL() + "'"); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
@ExtendWith(OutputCaptureExtension.class) |
|
|
|
|
void allDataSourceUrlsAreLoggedWhenMultipleAvailable(CapturedOutput output) { |
|
|
|
|
this.contextRunner.withUserConfiguration(MultiDataSourceConfiguration.class) |
|
|
|
|
.withPropertyValues("spring.h2.console.enabled=true").run((context) -> |
|
|
|
|
assertThat(output).contains("H2 console available at '/h2-console'. Database(s) available at 'primaryUrl', 'secondaryUrl'")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
@ExtendWith(OutputCaptureExtension.class) |
|
|
|
|
void noDataSourceIsLoggedWhenNoneAvailable(CapturedOutput output) { |
|
|
|
|
this.contextRunner.withUserConfiguration(FailingDataSourceConfiguration.class) |
|
|
|
|
.withPropertyValues("spring.h2.console.enabled=true") |
|
|
|
|
.run((context) -> assertThat(output).isEmpty()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void h2ConsoleShouldNotFailIfDatabaseConnectionFails() { |
|
|
|
|
this.contextRunner.withUserConfiguration(CustomDataSourceConfiguration.class) |
|
|
|
|
this.contextRunner.withUserConfiguration(FailingDataSourceConfiguration.class) |
|
|
|
|
.withPropertyValues("spring.h2.console.enabled=true") |
|
|
|
|
.run((context) -> assertThat(context.isRunning()).isTrue()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Configuration(proxyBeanMethods = false) |
|
|
|
|
static class CustomDataSourceConfiguration { |
|
|
|
|
static class FailingDataSourceConfiguration { |
|
|
|
|
|
|
|
|
|
@Bean |
|
|
|
|
DataSource dataSource() throws SQLException { |
|
|
|
|
@ -143,4 +162,28 @@ class H2ConsoleAutoConfigurationTests {
@@ -143,4 +162,28 @@ class H2ConsoleAutoConfigurationTests {
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Configuration(proxyBeanMethods = false) |
|
|
|
|
static class MultiDataSourceConfiguration extends FailingDataSourceConfiguration { |
|
|
|
|
|
|
|
|
|
@Bean |
|
|
|
|
@Primary |
|
|
|
|
DataSource primaryDataSource() throws SQLException { |
|
|
|
|
return getDataSource("primaryUrl"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Bean |
|
|
|
|
DataSource anotherDataSource() throws SQLException { |
|
|
|
|
return getDataSource("secondaryUrl"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private DataSource getDataSource(String url) throws SQLException { |
|
|
|
|
DataSource dataSource = mock(DataSource.class); |
|
|
|
|
given(dataSource.getConnection()).willReturn(mock(Connection.class)); |
|
|
|
|
given(dataSource.getConnection().getMetaData()).willReturn(mock(DatabaseMetaData.class)); |
|
|
|
|
given(dataSource.getConnection().getMetaData().getURL()).willReturn(url); |
|
|
|
|
return dataSource; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|