Merge pull request #6651 from vpavic:improve-batch-autoconfig

* pr/6651:
  Polish contribution
  Validate Spring Batch database initializer configuration
This commit is contained in:
Stephane Nicoll 2016-09-12 14:53:27 +02:00
commit 5b0d916910
3 changed files with 41 additions and 6 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 Stephane Nicoll
* @author Eddú Meléndez * @author Eddú Meléndez
* @author Vedran Pavic
* @since 1.2.0 * @since 1.2.0
*/ */
@ConfigurationProperties("spring.batch") @ConfigurationProperties("spring.batch")
@ -69,16 +70,24 @@ public class BatchProperties {
return this.tablePrefix; return this.tablePrefix;
} }
public static class Initializer { public class Initializer {
/** /**
* Create the required batch tables on startup if necessary. * Create the required batch tables on startup if necessary. Enabled
* automatically if no custom table prefix is set or if a custom schema is
* configured.
*/ */
private boolean enabled = true; private Boolean enabled;
public boolean isEnabled() { public boolean isEnabled() {
if (this.enabled != null) {
return this.enabled; return this.enabled;
} }
boolean defaultTablePrefix = BatchProperties.this.getTablePrefix() == null;
boolean customSchema = !DEFAULT_SCHEMA_LOCATION.equals(
BatchProperties.this.getSchema());
return (defaultTablePrefix || customSchema);
}
public void setEnabled(boolean enabled) { public void setEnabled(boolean enabled) {
this.enabled = enabled; this.enabled = enabled;

View File

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

View File

@ -847,7 +847,7 @@ content into your application; rather pick only the properties that you need.
spring.artemis.user= # Login user of the broker. spring.artemis.user= # Login user of the broker.
# SPRING BATCH ({sc-spring-boot-autoconfigure}/batch/BatchProperties.{sc-ext}[BatchProperties]) # SPRING BATCH ({sc-spring-boot-autoconfigure}/batch/BatchProperties.{sc-ext}[BatchProperties])
spring.batch.initializer.enabled=true # Create the required batch tables on startup if necessary. spring.batch.initializer.enabled= # Create the required batch tables on startup if necessary. Enabled automatically if no custom table prefix is set or if a custom schema is configured.
spring.batch.job.enabled=true # Execute all Spring Batch jobs in the context on startup. spring.batch.job.enabled=true # Execute all Spring Batch jobs in the context on startup.
spring.batch.job.names= # Comma-separated list of job names to execute on startup (For instance `job1,job2`). By default, all Jobs found in the context are executed. spring.batch.job.names= # Comma-separated list of job names to execute on startup (For instance `job1,job2`). By default, all Jobs found in the context are executed.
spring.batch.schema=classpath:org/springframework/batch/core/schema-@@platform@@.sql # Path to the SQL file to use to initialize the database schema. spring.batch.schema=classpath:org/springframework/batch/core/schema-@@platform@@.sql # Path to the SQL file to use to initialize the database schema.