Skip to content

Commit ceaf88c

Browse files
committed
Polish "Include non-default DataSource candidates"
See gh-44293
1 parent 8d27f4e commit ceaf88c

File tree

2 files changed

+44
-18
lines changed

2 files changed

+44
-18
lines changed

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -47,7 +47,6 @@
4747
* @author Marten Deinum
4848
* @author Stephane Nicoll
4949
* @author Phillip Webb
50-
* @author Yanming Zhou
5150
* @since 1.3.0
5251
*/
5352
@AutoConfiguration(after = DataSourceAutoConfiguration.class)

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

+43-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -57,7 +57,6 @@
5757
* @author Stephane Nicoll
5858
* @author Shraddha Yeole
5959
* @author Phillip Webb
60-
* @author Yanming Zhou
6160
*/
6261
class H2ConsoleAutoConfigurationTests {
6362

@@ -163,6 +162,17 @@ void allDataSourceUrlsAreLoggedWhenMultipleAvailable(CapturedOutput output) {
163162
"H2 console available at '/h2-console'. Databases available at 'someJdbcUrl', 'anotherJdbcUrl'"));
164163
}
165164

165+
@Test
166+
@ExtendWith(OutputCaptureExtension.class)
167+
void allDataSourceUrlsAreLoggedWhenNonCandidate(CapturedOutput output) {
168+
ClassLoader webAppClassLoader = new URLClassLoader(new URL[0]);
169+
this.contextRunner.withClassLoader(webAppClassLoader)
170+
.withUserConfiguration(FailingDataSourceConfiguration.class, MultiDataSourceNonCandidateConfiguration.class)
171+
.withPropertyValues("spring.h2.console.enabled=true")
172+
.run((context) -> assertThat(output).contains(
173+
"H2 console available at '/h2-console'. Databases available at 'someJdbcUrl', 'anotherJdbcUrl'"));
174+
}
175+
166176
@Test
167177
void h2ConsoleShouldNotFailIfDatabaseConnectionFails() {
168178
this.contextRunner.withUserConfiguration(FailingDataSourceConfiguration.class)
@@ -186,6 +196,20 @@ void dataSourceIsNotInitializedEarly(CapturedOutput output) {
186196
});
187197
}
188198

199+
private static DataSource mockDataSource(String url, ClassLoader classLoader) throws SQLException {
200+
DataSource dataSource = mock(DataSource.class);
201+
given(dataSource.getConnection()).will((invocation) -> {
202+
assertThat(Thread.currentThread().getContextClassLoader()).isEqualTo(classLoader);
203+
Connection connection = mock(Connection.class);
204+
DatabaseMetaData metadata = mock(DatabaseMetaData.class);
205+
given(connection.getMetaData()).willReturn(metadata);
206+
given(metadata.getURL()).willReturn(url);
207+
return connection;
208+
});
209+
210+
return dataSource;
211+
}
212+
189213
@Configuration(proxyBeanMethods = false)
190214
static class FailingDataSourceConfiguration {
191215

@@ -204,27 +228,30 @@ static class MultiDataSourceConfiguration {
204228
@Bean
205229
@Order(5)
206230
DataSource anotherDataSource() throws SQLException {
207-
return mockDataSource("anotherJdbcUrl");
231+
return mockDataSource("anotherJdbcUrl", getClass().getClassLoader());
208232
}
209233

210-
@Bean(defaultCandidate = false)
234+
@Bean
211235
@Order(0)
212236
DataSource someDataSource() throws SQLException {
213-
return mockDataSource("someJdbcUrl");
237+
return mockDataSource("someJdbcUrl", getClass().getClassLoader());
214238
}
215239

216-
private DataSource mockDataSource(String url) throws SQLException {
217-
DataSource dataSource = mock(DataSource.class);
218-
given(dataSource.getConnection()).will((invocation) -> {
219-
assertThat(Thread.currentThread().getContextClassLoader()).isEqualTo(getClass().getClassLoader());
220-
Connection connection = mock(Connection.class);
221-
DatabaseMetaData metadata = mock(DatabaseMetaData.class);
222-
given(connection.getMetaData()).willReturn(metadata);
223-
given(metadata.getURL()).willReturn(url);
224-
return connection;
225-
});
240+
}
226241

227-
return dataSource;
242+
@Configuration(proxyBeanMethods = false)
243+
static class MultiDataSourceNonCandidateConfiguration {
244+
245+
@Bean
246+
@Order(5)
247+
DataSource anotherDataSource() throws SQLException {
248+
return mockDataSource("anotherJdbcUrl", getClass().getClassLoader());
249+
}
250+
251+
@Bean(defaultCandidate = false)
252+
@Order(0)
253+
DataSource nonDefaultDataSource() throws SQLException {
254+
return mockDataSource("someJdbcUrl", getClass().getClassLoader());
228255
}
229256

230257
}

0 commit comments

Comments
 (0)