From e43c6ed21638105e3624d653770165d476462e11 Mon Sep 17 00:00:00 2001 From: Vedran Pavic Date: Sun, 14 Aug 2016 11:48:47 +0200 Subject: [PATCH] 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. --- .../autoconfigure/batch/BatchProperties.java | 10 ++++--- .../batch/BatchAutoConfigurationTests.java | 26 +++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchProperties.java index 0da9917e364d..17e0e7b38efe 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchProperties.java @@ -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 @@ * * @author Stephane Nicoll * @author EddĂș MelĂ©ndez + * @author Vedran Pavic * @since 1.2.0 */ @ConfigurationProperties("spring.batch") @@ -69,7 +70,7 @@ public String getTablePrefix() { return this.tablePrefix; } - public static class Initializer { + public class Initializer { /** * Create the required batch tables on startup if necessary. @@ -77,7 +78,10 @@ public static class Initializer { 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) { diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java index 75199d71629f..8a91c0c0ec16 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java @@ -67,6 +67,7 @@ * * @author Dave Syer * @author Stephane Nicoll + * @author Vedran Pavic */ public class BatchAutoConfigurationTests { @@ -91,6 +92,8 @@ public void testDefaultContext() throws Exception { 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 void testDisableSchemaLoader() throws Exception { 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 void testRenamePrefix() throws Exception { 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 void testRenamePrefix() throws Exception { .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 { }