Merge pull request #28204 from StitzL

* pr/28204:
  Polish "Log URLs for all DataSources"
  Log URLs for all DataSources

Closes gh-28204
pull/28344/head
Stephane Nicoll 3 years ago
commit 05ee8c68de

@ -17,6 +17,9 @@
package org.springframework.boot.autoconfigure.h2; package org.springframework.boot.autoconfigure.h2;
import java.sql.Connection; import java.sql.Connection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.sql.DataSource; import javax.sql.DataSource;
@ -63,16 +66,28 @@ public class H2ConsoleAutoConfiguration {
String urlMapping = path + (path.endsWith("/") ? "*" : "/*"); String urlMapping = path + (path.endsWith("/") ? "*" : "/*");
ServletRegistrationBean<WebServlet> registration = new ServletRegistrationBean<>(new WebServlet(), urlMapping); ServletRegistrationBean<WebServlet> registration = new ServletRegistrationBean<>(new WebServlet(), urlMapping);
configureH2ConsoleSettings(registration, properties.getSettings()); configureH2ConsoleSettings(registration, properties.getSettings());
dataSource.ifAvailable((available) -> { if (logger.isInfoEnabled()) {
logDataSources(dataSource, path);
}
return registration;
}
private void logDataSources(ObjectProvider<DataSource> dataSource, String path) {
List<String> urls = dataSource.orderedStream().map((available) -> {
try (Connection connection = available.getConnection()) { try (Connection connection = available.getConnection()) {
logger.info("H2 console available at '" + path + "'. Database available at '" return "'" + connection.getMetaData().getURL() + "'";
+ connection.getMetaData().getURL() + "'");
} }
catch (Exception ex) { catch (Exception ex) {
// Continue return null;
} }
}); }).filter(Objects::nonNull).collect(Collectors.toList());
return registration; if (!urls.isEmpty()) {
StringBuilder sb = new StringBuilder("H2 console available at '").append(path).append("'. ");
String tmp = (urls.size() > 1) ? "Databases" : "Database";
sb.append(tmp).append(" available at ");
sb.append(String.join(", ", urls));
logger.info(sb.toString());
}
} }
private void configureH2ConsoleSettings(ServletRegistrationBean<WebServlet> registration, Settings settings) { private void configureH2ConsoleSettings(ServletRegistrationBean<WebServlet> registration, Settings settings) {

@ -17,6 +17,7 @@
package org.springframework.boot.autoconfigure.h2; package org.springframework.boot.autoconfigure.h2;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException; import java.sql.SQLException;
import javax.sql.DataSource; import javax.sql.DataSource;
@ -33,6 +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.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;
@ -114,25 +116,42 @@ class H2ConsoleAutoConfigurationTests {
@Test @Test
@ExtendWith(OutputCaptureExtension.class) @ExtendWith(OutputCaptureExtension.class)
void dataSourceUrlIsLoggedWhenAvailable(CapturedOutput output) { void singleDataSourceUrlIsLoggedWhenOnlyOneAvailable(CapturedOutput output) {
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("Database available at '" + connection.getMetaData().getURL() + "'"); + connection.getMetaData().getURL() + "'");
} }
}); });
} }
@Test
@ExtendWith(OutputCaptureExtension.class)
void noDataSourceIsLoggedWhenNoneAvailable(CapturedOutput output) {
this.contextRunner.withUserConfiguration(FailingDataSourceConfiguration.class)
.withPropertyValues("spring.h2.console.enabled=true")
.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() {
this.contextRunner.withUserConfiguration(CustomDataSourceConfiguration.class) this.contextRunner.withUserConfiguration(FailingDataSourceConfiguration.class)
.withPropertyValues("spring.h2.console.enabled=true") .withPropertyValues("spring.h2.console.enabled=true")
.run((context) -> assertThat(context.isRunning()).isTrue()); .run((context) -> assertThat(context.isRunning()).isTrue());
} }
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
static class CustomDataSourceConfiguration { static class FailingDataSourceConfiguration {
@Bean @Bean
DataSource dataSource() throws SQLException { DataSource dataSource() throws SQLException {
@ -143,4 +162,29 @@ class H2ConsoleAutoConfigurationTests {
} }
@Configuration(proxyBeanMethods = false)
static class MultiDataSourceConfiguration {
@Bean
@Order(5)
DataSource anotherDataSource() throws SQLException {
return mockDataSource("anotherJdbcUrl");
}
@Bean
@Order(0)
DataSource someDataSource() throws SQLException {
return mockDataSource("someJdbcUrl");
}
private DataSource mockDataSource(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;
}
}
} }

Loading…
Cancel
Save