Browse Source

Polish "Include non-default DataSource candidates"

See gh-44293
pull/44380/head
Stéphane Nicoll 12 months ago
parent
commit
ceaf88c1a4
  1. 3
      spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfiguration.java
  2. 59
      spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java

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

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 Marten Deinum
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Phillip Webb * @author Phillip Webb
* @author Yanming Zhou
* @since 1.3.0 * @since 1.3.0
*/ */
@AutoConfiguration(after = DataSourceAutoConfiguration.class) @AutoConfiguration(after = DataSourceAutoConfiguration.class)

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

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 Stephane Nicoll
* @author Shraddha Yeole * @author Shraddha Yeole
* @author Phillip Webb * @author Phillip Webb
* @author Yanming Zhou
*/ */
class H2ConsoleAutoConfigurationTests { class H2ConsoleAutoConfigurationTests {
@ -163,6 +162,17 @@ class H2ConsoleAutoConfigurationTests {
"H2 console available at '/h2-console'. Databases available at 'someJdbcUrl', 'anotherJdbcUrl'")); "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 @Test
void h2ConsoleShouldNotFailIfDatabaseConnectionFails() { void h2ConsoleShouldNotFailIfDatabaseConnectionFails() {
this.contextRunner.withUserConfiguration(FailingDataSourceConfiguration.class) 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) @Configuration(proxyBeanMethods = false)
static class FailingDataSourceConfiguration { static class FailingDataSourceConfiguration {
@ -204,27 +228,30 @@ class H2ConsoleAutoConfigurationTests {
@Bean @Bean
@Order(5) @Order(5)
DataSource anotherDataSource() throws SQLException { DataSource anotherDataSource() throws SQLException {
return mockDataSource("anotherJdbcUrl"); return mockDataSource("anotherJdbcUrl", getClass().getClassLoader());
} }
@Bean(defaultCandidate = false) @Bean
@Order(0) @Order(0)
DataSource someDataSource() throws SQLException { 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());
} }
} }

Loading…
Cancel
Save