Merge pull request #44293 from quaff

* pr/44293:
  Polish "Include non-default DataSource candidates"
  Include non-default DataSource candidates

Closes gh-44293
This commit is contained in:
Stéphane Nicoll 2025-02-18 11:18:39 +01:00
commit 8d55f24f45
2 changed files with 47 additions and 16 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -113,7 +113,10 @@ public class H2ConsoleAutoConfiguration {
}
private List<String> getConnectionUrls(ObjectProvider<DataSource> dataSources) {
return dataSources.orderedStream().map(this::getConnectionUrl).filter(Objects::nonNull).toList();
return dataSources.orderedStream(ObjectProvider.UNFILTERED)
.map(this::getConnectionUrl)
.filter(Objects::nonNull)
.toList();
}
private String getConnectionUrl(DataSource dataSource) {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -162,6 +162,17 @@ class H2ConsoleAutoConfigurationTests {
"H2 console available at '/h2-console'. Databases available at 'someJdbcUrl', 'anotherJdbcUrl'"));
}
@Test
@ExtendWith(OutputCaptureExtension.class)
void allDataSourceUrlsAreLoggedWhenNonCandidate(CapturedOutput output) {
ClassLoader webAppClassLoader = new URLClassLoader(new URL[0]);
this.contextRunner.withClassLoader(webAppClassLoader)
.withUserConfiguration(FailingDataSourceConfiguration.class, MultiDataSourceNonCandidateConfiguration.class)
.withPropertyValues("spring.h2.console.enabled=true")
.run((context) -> assertThat(output).contains(
"H2 console available at '/h2-console'. Databases available at 'someJdbcUrl', 'anotherJdbcUrl'"));
}
@Test
void h2ConsoleShouldNotFailIfDatabaseConnectionFails() {
this.contextRunner.withUserConfiguration(FailingDataSourceConfiguration.class)
@ -185,6 +196,20 @@ class H2ConsoleAutoConfigurationTests {
});
}
private static DataSource mockDataSource(String url, ClassLoader classLoader) throws SQLException {
DataSource dataSource = mock(DataSource.class);
given(dataSource.getConnection()).will((invocation) -> {
assertThat(Thread.currentThread().getContextClassLoader()).isEqualTo(classLoader);
Connection connection = mock(Connection.class);
DatabaseMetaData metadata = mock(DatabaseMetaData.class);
given(connection.getMetaData()).willReturn(metadata);
given(metadata.getURL()).willReturn(url);
return connection;
});
return dataSource;
}
@Configuration(proxyBeanMethods = false)
static class FailingDataSourceConfiguration {
@ -203,27 +228,30 @@ class H2ConsoleAutoConfigurationTests {
@Bean
@Order(5)
DataSource anotherDataSource() throws SQLException {
return mockDataSource("anotherJdbcUrl");
return mockDataSource("anotherJdbcUrl", getClass().getClassLoader());
}
@Bean
@Order(0)
DataSource someDataSource() throws SQLException {
return mockDataSource("someJdbcUrl");
return mockDataSource("someJdbcUrl", getClass().getClassLoader());
}
private DataSource mockDataSource(String url) throws SQLException {
DataSource dataSource = mock(DataSource.class);
given(dataSource.getConnection()).will((invocation) -> {
assertThat(Thread.currentThread().getContextClassLoader()).isEqualTo(getClass().getClassLoader());
Connection connection = mock(Connection.class);
DatabaseMetaData metadata = mock(DatabaseMetaData.class);
given(connection.getMetaData()).willReturn(metadata);
given(metadata.getURL()).willReturn(url);
return connection;
});
}
return dataSource;
@Configuration(proxyBeanMethods = false)
static class MultiDataSourceNonCandidateConfiguration {
@Bean
@Order(5)
DataSource anotherDataSource() throws SQLException {
return mockDataSource("anotherJdbcUrl", getClass().getClassLoader());
}
@Bean(defaultCandidate = false)
@Order(0)
DataSource nonDefaultDataSource() throws SQLException {
return mockDataSource("someJdbcUrl", getClass().getClassLoader());
}
}