Skip to content

Commit 0b32215

Browse files
committed
Polish "Log URLs for all DataSources"
See gh-28204
1 parent f31b8ec commit 0b32215

File tree

2 files changed

+34
-35
lines changed

2 files changed

+34
-35
lines changed

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

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,26 +69,24 @@ public ServletRegistrationBean<WebServlet> h2Console(H2ConsoleProperties propert
6969
if (logger.isInfoEnabled()) {
7070
logDataSources(dataSource, path);
7171
}
72-
7372
return registration;
7473
}
7574

7675
private void logDataSources(ObjectProvider<DataSource> dataSource, String path) {
77-
List<String> urls = dataSource.orderedStream()
78-
.map((available) -> {
79-
String url = null;
80-
try (Connection connection = available.getConnection()) {
81-
url = connection.getMetaData().getURL();
82-
} catch (Exception ex) {
83-
84-
}
85-
return url;
86-
}).filter(Objects::nonNull)
87-
.collect(Collectors.toList());
76+
List<String> urls = dataSource.orderedStream().map((available) -> {
77+
try (Connection connection = available.getConnection()) {
78+
return "'" + connection.getMetaData().getURL() + "'";
79+
}
80+
catch (Exception ex) {
81+
return null;
82+
}
83+
}).filter(Objects::nonNull).collect(Collectors.toList());
8884
if (!urls.isEmpty()) {
89-
String log = urls.stream().collect(Collectors.joining("', '",
90-
"H2 console available at '" + path + "'. Database(s) available at '", "'."));
91-
logger.info(log);
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());
9290
}
9391
}
9492

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

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import org.springframework.boot.web.servlet.ServletRegistrationBean;
3535
import org.springframework.context.annotation.Bean;
3636
import org.springframework.context.annotation.Configuration;
37-
import org.springframework.context.annotation.Primary;
37+
import org.springframework.core.annotation.Order;
3838

3939
import static org.assertj.core.api.Assertions.assertThat;
4040
import static org.mockito.BDDMockito.given;
@@ -120,30 +120,30 @@ void singleDataSourceUrlIsLoggedWhenOnlyOneAvailable(CapturedOutput output) {
120120
this.contextRunner.withConfiguration(AutoConfigurations.of(DataSourceAutoConfiguration.class))
121121
.withPropertyValues("spring.h2.console.enabled=true").run((context) -> {
122122
try (Connection connection = context.getBean(DataSource.class).getConnection()) {
123-
assertThat(output)
124-
.contains("H2 console available at '/h2-console'. Database(s) available at '" + connection.getMetaData().getURL() + "'");
123+
assertThat(output).contains("H2 console available at '/h2-console'. Database available at '"
124+
+ connection.getMetaData().getURL() + "'");
125125
}
126126
});
127127
}
128128

129-
@Test
130-
@ExtendWith(OutputCaptureExtension.class)
131-
void allDataSourceUrlsAreLoggedWhenMultipleAvailable(CapturedOutput output) {
132-
this.contextRunner.withUserConfiguration(MultiDataSourceConfiguration.class)
133-
.withPropertyValues("spring.h2.console.enabled=true").run((context) ->
134-
assertThat(output).contains("H2 console available at '/h2-console'. Database(s) available at 'primaryUrl', 'secondaryUrl'"));
135-
}
136-
137129
@Test
138130
@ExtendWith(OutputCaptureExtension.class)
139131
void noDataSourceIsLoggedWhenNoneAvailable(CapturedOutput output) {
140132
this.contextRunner.withUserConfiguration(FailingDataSourceConfiguration.class)
141133
.withPropertyValues("spring.h2.console.enabled=true")
142-
.run((context) -> assertThat(output).isEmpty());
134+
.run((context) -> assertThat(output).doesNotContain("H2 console available"));
143135
}
144136

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+
}
145145

146-
@Test
146+
@Test
147147
void h2ConsoleShouldNotFailIfDatabaseConnectionFails() {
148148
this.contextRunner.withUserConfiguration(FailingDataSourceConfiguration.class)
149149
.withPropertyValues("spring.h2.console.enabled=true")
@@ -163,20 +163,21 @@ DataSource dataSource() throws SQLException {
163163
}
164164

165165
@Configuration(proxyBeanMethods = false)
166-
static class MultiDataSourceConfiguration extends FailingDataSourceConfiguration {
166+
static class MultiDataSourceConfiguration {
167167

168168
@Bean
169-
@Primary
170-
DataSource primaryDataSource() throws SQLException {
171-
return getDataSource("primaryUrl");
169+
@Order(5)
170+
DataSource anotherDataSource() throws SQLException {
171+
return mockDataSource("anotherJdbcUrl");
172172
}
173173

174174
@Bean
175-
DataSource anotherDataSource() throws SQLException {
176-
return getDataSource("secondaryUrl");
175+
@Order(0)
176+
DataSource someDataSource() throws SQLException {
177+
return mockDataSource("someJdbcUrl");
177178
}
178179

179-
private DataSource getDataSource(String url) throws SQLException {
180+
private DataSource mockDataSource(String url) throws SQLException {
180181
DataSource dataSource = mock(DataSource.class);
181182
given(dataSource.getConnection()).willReturn(mock(Connection.class));
182183
given(dataSource.getConnection().getMetaData()).willReturn(mock(DatabaseMetaData.class));

0 commit comments

Comments
 (0)