Polish "Include non-default DataSource candidates"
See gh-44293
This commit is contained in:
parent
8d27f4ee7c
commit
ceaf88c1a4
|
@ -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.
|
||||
|
@ -47,7 +47,6 @@ import org.springframework.core.log.LogMessage;
|
|||
* @author Marten Deinum
|
||||
* @author Stephane Nicoll
|
||||
* @author Phillip Webb
|
||||
* @author Yanming Zhou
|
||||
* @since 1.3.0
|
||||
*/
|
||||
@AutoConfiguration(after = DataSourceAutoConfiguration.class)
|
||||
|
|
|
@ -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.
|
||||
|
@ -57,7 +57,6 @@ import static org.mockito.Mockito.mock;
|
|||
* @author Stephane Nicoll
|
||||
* @author Shraddha Yeole
|
||||
* @author Phillip Webb
|
||||
* @author Yanming Zhou
|
||||
*/
|
||||
class H2ConsoleAutoConfigurationTests {
|
||||
|
||||
|
@ -163,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)
|
||||
|
@ -186,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 {
|
||||
|
||||
|
@ -204,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", getClass().getClassLoader());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
static class MultiDataSourceNonCandidateConfiguration {
|
||||
|
||||
@Bean
|
||||
@Order(5)
|
||||
DataSource anotherDataSource() throws SQLException {
|
||||
return mockDataSource("anotherJdbcUrl", getClass().getClassLoader());
|
||||
}
|
||||
|
||||
@Bean(defaultCandidate = false)
|
||||
@Order(0)
|
||||
DataSource someDataSource() throws SQLException {
|
||||
return mockDataSource("someJdbcUrl");
|
||||
}
|
||||
|
||||
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;
|
||||
DataSource nonDefaultDataSource() throws SQLException {
|
||||
return mockDataSource("someJdbcUrl", getClass().getClassLoader());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue