Validate Spring Batch database initializer configuration

This commit adds Spring Batch configuration validation that disables
database initializer in case custom table prefix is configured with
default schema.

See gh-6651
This commit is contained in:
Vedran Pavic 2016-08-14 11:48:47 +02:00 committed by Stephane Nicoll
parent def40bf27f
commit 06a1f44128
2 changed files with 33 additions and 3 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2016 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.
@ -23,6 +23,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
*
* @author Stephane Nicoll
* @author Eddú Meléndez
* @author Vedran Pavic
* @since 1.2.0
*/
@ConfigurationProperties("spring.batch")
@ -69,7 +70,7 @@ public class BatchProperties {
return this.tablePrefix;
}
public static class Initializer {
public class Initializer {
/**
* Create the required batch tables on startup if necessary.
@ -77,7 +78,10 @@ public class BatchProperties {
private boolean enabled = true;
public boolean isEnabled() {
return this.enabled;
boolean isDefaultTablePrefix = BatchProperties.this.getTablePrefix() == null;
boolean isDefaultSchema = DEFAULT_SCHEMA_LOCATION.equals(
BatchProperties.this.getSchema());
return this.enabled && (isDefaultTablePrefix || !isDefaultSchema);
}
public void setEnabled(boolean enabled) {

View File

@ -67,6 +67,7 @@ import static org.assertj.core.api.Assertions.assertThat;
*
* @author Dave Syer
* @author Stephane Nicoll
* @author Vedran Pavic
*/
public class BatchAutoConfigurationTests {
@ -91,6 +92,8 @@ public class BatchAutoConfigurationTests {
this.context.refresh();
assertThat(this.context.getBean(JobLauncher.class)).isNotNull();
assertThat(this.context.getBean(JobExplorer.class)).isNotNull();
assertThat(this.context.getBean(BatchProperties.class)
.getInitializer().isEnabled()).isTrue();
assertThat(new JdbcTemplate(this.context.getBean(DataSource.class))
.queryForList("select * from BATCH_JOB_EXECUTION")).isEmpty();
}
@ -190,6 +193,8 @@ public class BatchAutoConfigurationTests {
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBean(JobLauncher.class)).isNotNull();
assertThat(this.context.getBean(BatchProperties.class)
.getInitializer().isEnabled()).isFalse();
this.expected.expect(BadSqlGrammarException.class);
new JdbcTemplate(this.context.getBean(DataSource.class))
.queryForList("select * from BATCH_JOB_EXECUTION");
@ -228,6 +233,8 @@ public class BatchAutoConfigurationTests {
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBean(JobLauncher.class)).isNotNull();
assertThat(this.context.getBean(BatchProperties.class)
.getInitializer().isEnabled()).isTrue();
assertThat(new JdbcTemplate(this.context.getBean(DataSource.class))
.queryForList("select * from PREFIX_JOB_EXECUTION")).isEmpty();
JobExplorer jobExplorer = this.context.getBean(JobExplorer.class);
@ -237,6 +244,25 @@ public class BatchAutoConfigurationTests {
.isNull();
}
@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,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBean(JobLauncher.class)).isNotNull();
assertThat(this.context.getBean(BatchProperties.class)
.getInitializer().isEnabled()).isFalse();
this.expected.expect(BadSqlGrammarException.class);
new JdbcTemplate(this.context.getBean(DataSource.class))
.queryForList("select * from BATCH_JOB_EXECUTION");
}
@Configuration
protected static class EmptyConfiguration {
}