pull/8889/head
Stephane Nicoll 8 years ago
parent bcb13bea63
commit 2f2b1a0e8f

@ -75,11 +75,11 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class BatchAutoConfigurationTests {
private AnnotationConfigApplicationContext context;
@Rule
public ExpectedException expected = ExpectedException.none();
private AnnotationConfigApplicationContext context;
@After
public void close() {
if (this.context != null) {
@ -89,12 +89,8 @@ public class BatchAutoConfigurationTests {
@Test
public void testDefaultContext() throws Exception {
this.context = new AnnotationConfigApplicationContext();
this.context.register(TestConfiguration.class,
EmbeddedDataSourceConfiguration.class, BatchAutoConfiguration.class,
TransactionAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
load(new Class<?>[] { TestConfiguration.class,
EmbeddedDataSourceConfiguration.class });
assertThat(this.context.getBean(JobLauncher.class)).isNotNull();
assertThat(this.context.getBean(JobExplorer.class)).isNotNull();
assertThat(
@ -106,11 +102,7 @@ public class BatchAutoConfigurationTests {
@Test
public void testNoDatabase() throws Exception {
this.context = new AnnotationConfigApplicationContext();
this.context.register(TestCustomConfiguration.class, BatchAutoConfiguration.class,
TransactionAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
load(new Class<?>[] { TestCustomConfiguration.class });
assertThat(this.context.getBean(JobLauncher.class)).isNotNull();
JobExplorer explorer = this.context.getBean(JobExplorer.class);
assertThat(explorer).isNotNull();
@ -119,11 +111,8 @@ public class BatchAutoConfigurationTests {
@Test
public void testNoBatchConfiguration() throws Exception {
this.context = new AnnotationConfigApplicationContext();
this.context.register(EmptyConfiguration.class, BatchAutoConfiguration.class,
TransactionAutoConfiguration.class, EmbeddedDataSourceConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
load(new Class<?>[] { EmptyConfiguration.class,
EmbeddedDataSourceConfiguration.class });
assertThat(this.context.getBeanNamesForType(JobLauncher.class).length)
.isEqualTo(0);
assertThat(this.context.getBeanNamesForType(JobRepository.class).length)
@ -132,12 +121,8 @@ public class BatchAutoConfigurationTests {
@Test
public void testDefinesAndLaunchesJob() throws Exception {
this.context = new AnnotationConfigApplicationContext();
this.context.register(JobConfiguration.class,
EmbeddedDataSourceConfiguration.class, BatchAutoConfiguration.class,
TransactionAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
load(new Class<?>[] { JobConfiguration.class,
EmbeddedDataSourceConfiguration.class });
assertThat(this.context.getBean(JobLauncher.class)).isNotNull();
this.context.getBean(JobLauncherCommandLineRunner.class).run();
assertThat(this.context.getBean(JobRepository.class).getLastJobExecution("job",
@ -146,14 +131,9 @@ public class BatchAutoConfigurationTests {
@Test
public void testDefinesAndLaunchesNamedJob() throws Exception {
this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context,
load(new Class<?>[] { NamedJobConfigurationWithRegisteredJob.class,
EmbeddedDataSourceConfiguration.class },
"spring.batch.job.names:discreteRegisteredJob");
this.context.register(NamedJobConfigurationWithRegisteredJob.class,
EmbeddedDataSourceConfiguration.class, BatchAutoConfiguration.class,
TransactionAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
JobRepository repository = this.context.getBean(JobRepository.class);
assertThat(this.context.getBean(JobLauncher.class)).isNotNull();
this.context.getBean(JobLauncherCommandLineRunner.class).run();
@ -163,31 +143,21 @@ public class BatchAutoConfigurationTests {
@Test
public void testDefinesAndLaunchesLocalJob() throws Exception {
this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context,
load(new Class<?>[] { NamedJobConfigurationWithLocalJob.class,
EmbeddedDataSourceConfiguration.class },
"spring.batch.job.names:discreteLocalJob");
this.context.register(NamedJobConfigurationWithLocalJob.class,
EmbeddedDataSourceConfiguration.class, BatchAutoConfiguration.class,
TransactionAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBean(JobLauncher.class)).isNotNull();
this.context.getBean(JobLauncherCommandLineRunner.class).run();
assertThat(this.context.getBean(JobRepository.class)
.getLastJobExecution("discreteLocalJob", new JobParameters()))
.isNotNull();
.isNotNull();
}
@Test
public void testDisableLaunchesJob() throws Exception {
this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context,
load(new Class<?>[] { JobConfiguration.class,
EmbeddedDataSourceConfiguration.class },
"spring.batch.job.enabled:false");
this.context.register(JobConfiguration.class,
EmbeddedDataSourceConfiguration.class, BatchAutoConfiguration.class,
TransactionAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBean(JobLauncher.class)).isNotNull();
assertThat(this.context.getBeanNamesForType(CommandLineRunner.class).length)
.isEqualTo(0);
@ -195,19 +165,14 @@ public class BatchAutoConfigurationTests {
@Test
public void testDisableSchemaLoader() throws Exception {
this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context,
"spring.datasource.name:batchtest",
load(new Class<?>[] { TestConfiguration.class,
EmbeddedDataSourceConfiguration.class },
"spring.datasource.generate-unique-name=true",
"spring.batch.initializer.enabled:false");
this.context.register(TestConfiguration.class,
EmbeddedDataSourceConfiguration.class, BatchAutoConfiguration.class,
TransactionAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBean(JobLauncher.class)).isNotNull();
assertThat(
this.context.getBean(BatchProperties.class).getInitializer().isEnabled())
.isFalse();
.isFalse();
this.expected.expect(BadSqlGrammarException.class);
new JdbcTemplate(this.context.getBean(DataSource.class))
.queryForList("select * from BATCH_JOB_EXECUTION");
@ -215,14 +180,9 @@ public class BatchAutoConfigurationTests {
@Test
public void testUsingJpa() throws Exception {
this.context = new AnnotationConfigApplicationContext();
// The order is very important here: DataSource -> Hibernate -> Batch
this.context.register(TestConfiguration.class,
load(new Class<?>[] { TestConfiguration.class,
EmbeddedDataSourceConfiguration.class,
HibernateJpaAutoConfiguration.class, BatchAutoConfiguration.class,
TransactionAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
HibernateJpaAutoConfiguration.class });
PlatformTransactionManager transactionManager = this.context
.getBean(PlatformTransactionManager.class);
// It's a lazy proxy, but it does render its target if you ask for toString():
@ -236,17 +196,12 @@ public class BatchAutoConfigurationTests {
@Test
public void testRenamePrefix() throws Exception {
this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context,
"spring.datasource.name:batchtest",
load(new Class<?>[] { TestConfiguration.class,
EmbeddedDataSourceConfiguration.class,
HibernateJpaAutoConfiguration.class },
"spring.datasource.generate-unique-name=true",
"spring.batch.schema:classpath:batch/custom-schema-hsql.sql",
"spring.batch.tablePrefix:PREFIX_");
this.context.register(TestConfiguration.class,
EmbeddedDataSourceConfiguration.class,
HibernateJpaAutoConfiguration.class, BatchAutoConfiguration.class,
TransactionAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBean(JobLauncher.class)).isNotNull();
assertThat(
this.context.getBean(BatchProperties.class).getInitializer().isEnabled())
@ -263,19 +218,15 @@ public class BatchAutoConfigurationTests {
@Test
public void testCustomTablePrefixWithDefaultSchemaDisablesInitializer()
throws Exception {
this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context,
"spring.datasource.name:batchtest", "spring.batch.tablePrefix:PREFIX_");
this.context.register(TestConfiguration.class,
EmbeddedDataSourceConfiguration.class,
HibernateJpaAutoConfiguration.class, BatchAutoConfiguration.class,
TransactionAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
load(new Class<?>[] { TestConfiguration.class,
EmbeddedDataSourceConfiguration.class,
HibernateJpaAutoConfiguration.class },
"spring.datasource.generate-unique-name=true",
"spring.batch.tablePrefix:PREFIX_");
assertThat(this.context.getBean(JobLauncher.class)).isNotNull();
assertThat(
this.context.getBean(BatchProperties.class).getInitializer().isEnabled())
.isFalse();
.isFalse();
this.expected.expect(BadSqlGrammarException.class);
new JdbcTemplate(this.context.getBean(DataSource.class))
.queryForList("select * from BATCH_JOB_EXECUTION");
@ -283,16 +234,11 @@ public class BatchAutoConfigurationTests {
@Test
public void testCustomizeJpaTransactionManagerUsingProperties() throws Exception {
this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context,
load(new Class<?>[] { TestConfiguration.class,
EmbeddedDataSourceConfiguration.class,
HibernateJpaAutoConfiguration.class },
"spring.transaction.default-timeout:30",
"spring.transaction.rollback-on-commit-failure:true");
this.context.register(TestConfiguration.class,
EmbeddedDataSourceConfiguration.class,
HibernateJpaAutoConfiguration.class, BatchAutoConfiguration.class,
TransactionAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
this.context.getBean(BatchConfigurer.class);
JpaTransactionManager transactionManager = JpaTransactionManager.class.cast(
this.context.getBean(BatchConfigurer.class).getTransactionManager());
@ -303,15 +249,10 @@ public class BatchAutoConfigurationTests {
@Test
public void testCustomizeDataSourceTransactionManagerUsingProperties()
throws Exception {
this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context,
load(new Class<?>[] { TestConfiguration.class,
EmbeddedDataSourceConfiguration.class },
"spring.transaction.default-timeout:30",
"spring.transaction.rollback-on-commit-failure:true");
this.context.register(TestConfiguration.class,
EmbeddedDataSourceConfiguration.class, BatchAutoConfiguration.class,
TransactionAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
this.context.getBean(BatchConfigurer.class);
DataSourceTransactionManager transactionManager = DataSourceTransactionManager.class
.cast(this.context.getBean(BatchConfigurer.class)
@ -320,6 +261,18 @@ public class BatchAutoConfigurationTests {
assertThat(transactionManager.isRollbackOnCommitFailure()).isTrue();
}
private void load(Class<?>[] configs, String... environment) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(ctx, environment);
if (configs != null) {
ctx.register(configs);
}
ctx.register(BatchAutoConfiguration.class, TransactionAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
ctx.refresh();
this.context = ctx;
}
@Configuration
protected static class EmptyConfiguration {

Loading…
Cancel
Save