Polish "Instrument user-created DataSource for checkpoint-restore"

See gh-37630
pull/37808/head
Andy Wilkinson 1 year ago
parent 4b5e50ba94
commit 31008def76

@ -24,7 +24,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnCheckpointRestore; import org.springframework.boot.autoconfigure.condition.ConditionalOnCheckpointRestore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.jdbc.HikariCheckpointRestoreLifecycle; import org.springframework.boot.jdbc.HikariCheckpointRestoreLifecycle;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -33,17 +32,14 @@ import org.springframework.context.annotation.Configuration;
* Checkpoint-restore specific configuration. * Checkpoint-restore specific configuration.
* *
* @author Olga Maciaszek-Sharma * @author Olga Maciaszek-Sharma
* @since 3.2.0
*/ */
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
@ConditionalOnCheckpointRestore @ConditionalOnCheckpointRestore
@ConditionalOnBean(DataSource.class) @ConditionalOnBean(DataSource.class)
public class DataSourceCheckpointRestoreConfiguration { class DataSourceCheckpointRestoreConfiguration {
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
@ConditionalOnClass(HikariDataSource.class) @ConditionalOnClass(HikariDataSource.class)
@ConditionalOnProperty(name = "spring.datasource.type", havingValue = "com.zaxxer.hikari.HikariDataSource",
matchIfMissing = true)
static class Hikari { static class Hikari {
@Bean @Bean

@ -140,21 +140,19 @@ class HikariDataSourceConfigurationTests {
@ClassPathOverrides("org.crac:crac:1.3.0") @ClassPathOverrides("org.crac:crac:1.3.0")
void whenCheckpointRestoreIsAvailableAndDataSourceHasBeenWrappedHikariAutoConfigRegistersLifecycleBean() { void whenCheckpointRestoreIsAvailableAndDataSourceHasBeenWrappedHikariAutoConfigRegistersLifecycleBean() {
this.contextRunner.withUserConfiguration(DataSourceWrapperConfiguration.class) this.contextRunner.withUserConfiguration(DataSourceWrapperConfiguration.class)
.withPropertyValues("spring.datasource.type=" + HikariDataSource.class.getName())
.run((context) -> assertThat(context).hasSingleBean(HikariCheckpointRestoreLifecycle.class)); .run((context) -> assertThat(context).hasSingleBean(HikariCheckpointRestoreLifecycle.class));
} }
@Test @Test
void whenCheckpointRestoreIsNotAvailableHikariAutoConfigDoesNotRegisterLifecycleBean() { void whenCheckpointRestoreIsNotAvailableHikariAutoConfigDoesNotRegisterLifecycleBean() {
this.contextRunner.withPropertyValues("spring.datasource.type=" + HikariDataSource.class.getName()) this.contextRunner
.run((context) -> assertThat(context).doesNotHaveBean(HikariCheckpointRestoreLifecycle.class)); .run((context) -> assertThat(context).doesNotHaveBean(HikariCheckpointRestoreLifecycle.class));
} }
@Test @Test
@ClassPathOverrides("org.crac:crac:1.3.0") @ClassPathOverrides("org.crac:crac:1.3.0")
void whenCheckpointRestoreIsAvailableAndDataSourceInstantiatedByUserHikariAutoConfigRegistersLifecycleBean() { void whenCheckpointRestoreIsAvailableAndDataSourceIsFromUserConfigurationHikariAutoConfigRegistersLifecycleBean() {
this.contextRunner.withUserConfiguration(UserDataSourceConfiguration.class) this.contextRunner.withUserConfiguration(UserDataSourceConfiguration.class)
.withPropertyValues("spring.datasource.type=" + HikariDataSource.class.getName())
.run((context) -> assertThat(context).hasSingleBean(HikariCheckpointRestoreLifecycle.class)); .run((context) -> assertThat(context).hasSingleBean(HikariCheckpointRestoreLifecycle.class));
} }

Loading…
Cancel
Save