Polish "Detect JobRepository as depending on DB init"

See gh-27221
pull/27310/head
Andy Wilkinson 3 years ago
parent cf69785393
commit f008228f1a

@ -1,5 +1,5 @@
/*
* Copyright 2021 the original author or authors.
* Copyright 2012-2021 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.

@ -39,12 +39,14 @@ import org.springframework.batch.core.job.AbstractJob;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.DefaultApplicationArguments;
import org.springframework.boot.LazyInitializationBeanFactoryPostProcessor;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage;
import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.test.City;
import org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration;
@ -154,21 +156,6 @@ class BatchAutoConfigurationTests {
});
}
@Test
void testDefinesAndLaunchesNamedJobWithLazyInitialization() {
this.contextRunner
.withUserConfiguration(NamedJobConfigurationWithRegisteredJob.class,
EmbeddedDataSourceConfiguration.class)
.withInitializer((context) -> context
.addBeanFactoryPostProcessor(new LazyInitializationBeanFactoryPostProcessor()))
.withPropertyValues("spring.batch.job.names:discreteRegisteredJob").run((context) -> {
assertThat(context).hasSingleBean(JobLauncher.class);
context.getBean(JobLauncherApplicationRunner.class).run();
assertThat(context.getBean(JobRepository.class).getLastJobExecution("discreteRegisteredJob",
new JobParameters())).isNotNull();
});
}
@Test
void testDefinesAndLaunchesLocalJob() {
this.contextRunner
@ -315,6 +302,50 @@ class BatchAutoConfigurationTests {
});
}
@Test
void jobRepositoryBeansDependOnBatchDataSourceInitializer() {
this.contextRunner.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class)
.run((context) -> {
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
String[] jobRepositoryNames = beanFactory.getBeanNamesForType(JobRepository.class);
assertThat(jobRepositoryNames).isNotEmpty();
for (String jobRepositoryName : jobRepositoryNames) {
assertThat(beanFactory.getBeanDefinition(jobRepositoryName).getDependsOn())
.contains("batchDataSourceInitializer");
}
});
}
@Test
void jobRepositoryBeansDependOnFlyway() {
this.contextRunner.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class)
.withUserConfiguration(FlywayAutoConfiguration.class)
.withPropertyValues("spring.batch.initialize-schema=never").run((context) -> {
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
String[] jobRepositoryNames = beanFactory.getBeanNamesForType(JobRepository.class);
assertThat(jobRepositoryNames).isNotEmpty();
for (String jobRepositoryName : jobRepositoryNames) {
assertThat(beanFactory.getBeanDefinition(jobRepositoryName).getDependsOn()).contains("flyway",
"flywayInitializer");
}
});
}
@Test
void jobRepositoryBeansDependOnLiquibase() {
this.contextRunner.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class)
.withUserConfiguration(LiquibaseAutoConfiguration.class)
.withPropertyValues("spring.batch.initialize-schema=never").run((context) -> {
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
String[] jobRepositoryNames = beanFactory.getBeanNamesForType(JobRepository.class);
assertThat(jobRepositoryNames).isNotEmpty();
for (String jobRepositoryName : jobRepositoryNames) {
assertThat(beanFactory.getBeanDefinition(jobRepositoryName).getDependsOn())
.contains("liquibase");
}
});
}
@Configuration(proxyBeanMethods = false)
protected static class BatchDataSourceConfiguration {

Loading…
Cancel
Save