Skip to content

Commit

Permalink
Polish "Detect JobRepository as depending on DB init"
Browse files Browse the repository at this point in the history
  • Loading branch information
wilkinsona committed Jul 9, 2021
1 parent cf69785 commit f008228
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 17 deletions.
@@ -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.
Expand Down
Expand Up @@ -39,12 +39,14 @@
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;
Expand Down Expand Up @@ -154,21 +156,6 @@ void testDefinesAndLaunchesNamedJob() {
});
}

@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
Expand Down Expand Up @@ -315,6 +302,50 @@ void testBatchDataSource() {
});
}

@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 {

Expand Down

0 comments on commit f008228

Please sign in to comment.