parent
f31b8ec226
commit
0b32215c13
|
|
@ -69,26 +69,24 @@ public class H2ConsoleAutoConfiguration {
|
||||||
if (logger.isInfoEnabled()) {
|
if (logger.isInfoEnabled()) {
|
||||||
logDataSources(dataSource, path);
|
logDataSources(dataSource, path);
|
||||||
}
|
}
|
||||||
|
|
||||||
return registration;
|
return registration;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void logDataSources(ObjectProvider<DataSource> dataSource, String path) {
|
private void logDataSources(ObjectProvider<DataSource> dataSource, String path) {
|
||||||
List<String> urls = dataSource.orderedStream()
|
List<String> urls = dataSource.orderedStream().map((available) -> {
|
||||||
.map((available) -> {
|
|
||||||
String url = null;
|
|
||||||
try (Connection connection = available.getConnection()) {
|
try (Connection connection = available.getConnection()) {
|
||||||
url = connection.getMetaData().getURL();
|
return "'" + connection.getMetaData().getURL() + "'";
|
||||||
} catch (Exception ex) {
|
|
||||||
|
|
||||||
}
|
}
|
||||||
return url;
|
catch (Exception ex) {
|
||||||
}).filter(Objects::nonNull)
|
return null;
|
||||||
.collect(Collectors.toList());
|
}
|
||||||
|
}).filter(Objects::nonNull).collect(Collectors.toList());
|
||||||
if (!urls.isEmpty()) {
|
if (!urls.isEmpty()) {
|
||||||
String log = urls.stream().collect(Collectors.joining("', '",
|
StringBuilder sb = new StringBuilder("H2 console available at '").append(path).append("'. ");
|
||||||
"H2 console available at '" + path + "'. Database(s) available at '", "'."));
|
String tmp = (urls.size() > 1) ? "Databases" : "Database";
|
||||||
logger.info(log);
|
sb.append(tmp).append(" available at ");
|
||||||
|
sb.append(String.join(", ", urls));
|
||||||
|
logger.info(sb.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,7 @@ import org.springframework.boot.test.system.OutputCaptureExtension;
|
||||||
import org.springframework.boot.web.servlet.ServletRegistrationBean;
|
import org.springframework.boot.web.servlet.ServletRegistrationBean;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.annotation.Primary;
|
import org.springframework.core.annotation.Order;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.mockito.BDDMockito.given;
|
import static org.mockito.BDDMockito.given;
|
||||||
|
|
@ -120,28 +120,28 @@ class H2ConsoleAutoConfigurationTests {
|
||||||
this.contextRunner.withConfiguration(AutoConfigurations.of(DataSourceAutoConfiguration.class))
|
this.contextRunner.withConfiguration(AutoConfigurations.of(DataSourceAutoConfiguration.class))
|
||||||
.withPropertyValues("spring.h2.console.enabled=true").run((context) -> {
|
.withPropertyValues("spring.h2.console.enabled=true").run((context) -> {
|
||||||
try (Connection connection = context.getBean(DataSource.class).getConnection()) {
|
try (Connection connection = context.getBean(DataSource.class).getConnection()) {
|
||||||
assertThat(output)
|
assertThat(output).contains("H2 console available at '/h2-console'. Database available at '"
|
||||||
.contains("H2 console available at '/h2-console'. Database(s) available at '" + connection.getMetaData().getURL() + "'");
|
+ 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
|
@Test
|
||||||
@ExtendWith(OutputCaptureExtension.class)
|
@ExtendWith(OutputCaptureExtension.class)
|
||||||
void noDataSourceIsLoggedWhenNoneAvailable(CapturedOutput output) {
|
void noDataSourceIsLoggedWhenNoneAvailable(CapturedOutput output) {
|
||||||
this.contextRunner.withUserConfiguration(FailingDataSourceConfiguration.class)
|
this.contextRunner.withUserConfiguration(FailingDataSourceConfiguration.class)
|
||||||
.withPropertyValues("spring.h2.console.enabled=true")
|
.withPropertyValues("spring.h2.console.enabled=true")
|
||||||
.run((context) -> assertThat(output).isEmpty());
|
.run((context) -> assertThat(output).doesNotContain("H2 console available"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@ExtendWith(OutputCaptureExtension.class)
|
||||||
|
void allDataSourceUrlsAreLoggedWhenMultipleAvailable(CapturedOutput output) {
|
||||||
|
this.contextRunner
|
||||||
|
.withUserConfiguration(FailingDataSourceConfiguration.class, MultiDataSourceConfiguration.class)
|
||||||
|
.withPropertyValues("spring.h2.console.enabled=true").run((context) -> assertThat(output).contains(
|
||||||
|
"H2 console available at '/h2-console'. Databases available at 'someJdbcUrl', 'anotherJdbcUrl'"));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void h2ConsoleShouldNotFailIfDatabaseConnectionFails() {
|
void h2ConsoleShouldNotFailIfDatabaseConnectionFails() {
|
||||||
|
|
@ -163,20 +163,21 @@ class H2ConsoleAutoConfigurationTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Configuration(proxyBeanMethods = false)
|
@Configuration(proxyBeanMethods = false)
|
||||||
static class MultiDataSourceConfiguration extends FailingDataSourceConfiguration {
|
static class MultiDataSourceConfiguration {
|
||||||
|
|
||||||
@Bean
|
|
||||||
@Primary
|
|
||||||
DataSource primaryDataSource() throws SQLException {
|
|
||||||
return getDataSource("primaryUrl");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
@Order(5)
|
||||||
DataSource anotherDataSource() throws SQLException {
|
DataSource anotherDataSource() throws SQLException {
|
||||||
return getDataSource("secondaryUrl");
|
return mockDataSource("anotherJdbcUrl");
|
||||||
}
|
}
|
||||||
|
|
||||||
private DataSource getDataSource(String url) throws SQLException {
|
@Bean
|
||||||
|
@Order(0)
|
||||||
|
DataSource someDataSource() throws SQLException {
|
||||||
|
return mockDataSource("someJdbcUrl");
|
||||||
|
}
|
||||||
|
|
||||||
|
private DataSource mockDataSource(String url) throws SQLException {
|
||||||
DataSource dataSource = mock(DataSource.class);
|
DataSource dataSource = mock(DataSource.class);
|
||||||
given(dataSource.getConnection()).willReturn(mock(Connection.class));
|
given(dataSource.getConnection()).willReturn(mock(Connection.class));
|
||||||
given(dataSource.getConnection().getMetaData()).willReturn(mock(DatabaseMetaData.class));
|
given(dataSource.getConnection().getMetaData()).willReturn(mock(DatabaseMetaData.class));
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue