Skip to content

Commit 05ee8c6

Browse files
committed
Merge pull request #28204 from StitzL
* pr/28204: Polish "Log URLs for all DataSources" Log URLs for all DataSources Closes gh-28204
2 parents 14f47bd + 0b32215 commit 05ee8c6

File tree

2 files changed

+70
-11
lines changed

2 files changed

+70
-11
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfiguration.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
package org.springframework.boot.autoconfigure.h2;
1818

1919
import java.sql.Connection;
20+
import java.util.List;
21+
import java.util.Objects;
22+
import java.util.stream.Collectors;
2023

2124
import javax.sql.DataSource;
2225

@@ -63,16 +66,28 @@ public ServletRegistrationBean<WebServlet> h2Console(H2ConsoleProperties propert
6366
String urlMapping = path + (path.endsWith("/") ? "*" : "/*");
6467
ServletRegistrationBean<WebServlet> registration = new ServletRegistrationBean<>(new WebServlet(), urlMapping);
6568
configureH2ConsoleSettings(registration, properties.getSettings());
66-
dataSource.ifAvailable((available) -> {
69+
if (logger.isInfoEnabled()) {
70+
logDataSources(dataSource, path);
71+
}
72+
return registration;
73+
}
74+
75+
private void logDataSources(ObjectProvider<DataSource> dataSource, String path) {
76+
List<String> urls = dataSource.orderedStream().map((available) -> {
6777
try (Connection connection = available.getConnection()) {
68-
logger.info("H2 console available at '" + path + "'. Database available at '"
69-
+ connection.getMetaData().getURL() + "'");
78+
return "'" + connection.getMetaData().getURL() + "'";
7079
}
7180
catch (Exception ex) {
72-
// Continue
81+
return null;
7382
}
74-
});
75-
return registration;
83+
}).filter(Objects::nonNull).collect(Collectors.toList());
84+
if (!urls.isEmpty()) {
85+
StringBuilder sb = new StringBuilder("H2 console available at '").append(path).append("'. ");
86+
String tmp = (urls.size() > 1) ? "Databases" : "Database";
87+
sb.append(tmp).append(" available at ");
88+
sb.append(String.join(", ", urls));
89+
logger.info(sb.toString());
90+
}
7691
}
7792

7893
private void configureH2ConsoleSettings(ServletRegistrationBean<WebServlet> registration, Settings settings) {

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.boot.autoconfigure.h2;
1818

1919
import java.sql.Connection;
20+
import java.sql.DatabaseMetaData;
2021
import java.sql.SQLException;
2122

2223
import javax.sql.DataSource;
@@ -33,6 +34,7 @@
3334
import org.springframework.boot.web.servlet.ServletRegistrationBean;
3435
import org.springframework.context.annotation.Bean;
3536
import org.springframework.context.annotation.Configuration;
37+
import org.springframework.core.annotation.Order;
3638

3739
import static org.assertj.core.api.Assertions.assertThat;
3840
import static org.mockito.BDDMockito.given;
@@ -114,25 +116,42 @@ void customInitParameters() {
114116

115117
@Test
116118
@ExtendWith(OutputCaptureExtension.class)
117-
void dataSourceUrlIsLoggedWhenAvailable(CapturedOutput output) {
119+
void singleDataSourceUrlIsLoggedWhenOnlyOneAvailable(CapturedOutput output) {
118120
this.contextRunner.withConfiguration(AutoConfigurations.of(DataSourceAutoConfiguration.class))
119121
.withPropertyValues("spring.h2.console.enabled=true").run((context) -> {
120122
try (Connection connection = context.getBean(DataSource.class).getConnection()) {
121-
assertThat(output)
122-
.contains("Database available at '" + connection.getMetaData().getURL() + "'");
123+
assertThat(output).contains("H2 console available at '/h2-console'. Database available at '"
124+
+ connection.getMetaData().getURL() + "'");
123125
}
124126
});
125127
}
126128

129+
@Test
130+
@ExtendWith(OutputCaptureExtension.class)
131+
void noDataSourceIsLoggedWhenNoneAvailable(CapturedOutput output) {
132+
this.contextRunner.withUserConfiguration(FailingDataSourceConfiguration.class)
133+
.withPropertyValues("spring.h2.console.enabled=true")
134+
.run((context) -> assertThat(output).doesNotContain("H2 console available"));
135+
}
136+
137+
@Test
138+
@ExtendWith(OutputCaptureExtension.class)
139+
void allDataSourceUrlsAreLoggedWhenMultipleAvailable(CapturedOutput output) {
140+
this.contextRunner
141+
.withUserConfiguration(FailingDataSourceConfiguration.class, MultiDataSourceConfiguration.class)
142+
.withPropertyValues("spring.h2.console.enabled=true").run((context) -> assertThat(output).contains(
143+
"H2 console available at '/h2-console'. Databases available at 'someJdbcUrl', 'anotherJdbcUrl'"));
144+
}
145+
127146
@Test
128147
void h2ConsoleShouldNotFailIfDatabaseConnectionFails() {
129-
this.contextRunner.withUserConfiguration(CustomDataSourceConfiguration.class)
148+
this.contextRunner.withUserConfiguration(FailingDataSourceConfiguration.class)
130149
.withPropertyValues("spring.h2.console.enabled=true")
131150
.run((context) -> assertThat(context.isRunning()).isTrue());
132151
}
133152

134153
@Configuration(proxyBeanMethods = false)
135-
static class CustomDataSourceConfiguration {
154+
static class FailingDataSourceConfiguration {
136155

137156
@Bean
138157
DataSource dataSource() throws SQLException {
@@ -143,4 +162,29 @@ DataSource dataSource() throws SQLException {
143162

144163
}
145164

165+
@Configuration(proxyBeanMethods = false)
166+
static class MultiDataSourceConfiguration {
167+
168+
@Bean
169+
@Order(5)
170+
DataSource anotherDataSource() throws SQLException {
171+
return mockDataSource("anotherJdbcUrl");
172+
}
173+
174+
@Bean
175+
@Order(0)
176+
DataSource someDataSource() throws SQLException {
177+
return mockDataSource("someJdbcUrl");
178+
}
179+
180+
private DataSource mockDataSource(String url) throws SQLException {
181+
DataSource dataSource = mock(DataSource.class);
182+
given(dataSource.getConnection()).willReturn(mock(Connection.class));
183+
given(dataSource.getConnection().getMetaData()).willReturn(mock(DatabaseMetaData.class));
184+
given(dataSource.getConnection().getMetaData().getURL()).willReturn(url);
185+
return dataSource;
186+
}
187+
188+
}
189+
146190
}

0 commit comments

Comments
 (0)