From e4ca63a927f679140dfc8b12dd738fb843fee565 Mon Sep 17 00:00:00 2001 From: dsyer Date: Tue, 30 Dec 2008 09:23:27 +0000 Subject: [PATCH] RESOLVED - issue BATCH-980: Add SystemPropertyInitializer --- .../support/JobExplorerFactoryBean.java | 5 ++ .../support/JobExplorerFactoryBeanTests.java | 41 +++++++++-- .../support/SystemPropertyInitializer.java | 71 +++++++++++++++++++ .../SystemPropertyInitializerTests.java | 60 ++++++++++++++++ .../resources/adhoc-job-launcher-context.xml | 1 - .../main/resources/data-source-context.xml | 31 ++++---- .../resources/simple-job-launcher-context.xml | 6 +- .../skipSample-job-launcher-context.xml | 3 +- .../test/resources/data-source-context.xml | 23 ++---- 9 files changed, 190 insertions(+), 51 deletions(-) create mode 100644 spring-batch-infrastructure/src/main/java/org/springframework/batch/support/SystemPropertyInitializer.java create mode 100644 spring-batch-infrastructure/src/test/java/org/springframework/batch/support/SystemPropertyInitializerTests.java diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/explore/support/JobExplorerFactoryBean.java b/spring-batch-core/src/main/java/org/springframework/batch/core/explore/support/JobExplorerFactoryBean.java index d578a488f..17c035a86 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/explore/support/JobExplorerFactoryBean.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/explore/support/JobExplorerFactoryBean.java @@ -27,6 +27,7 @@ import org.springframework.batch.core.repository.dao.JobInstanceDao; import org.springframework.batch.core.repository.dao.StepExecutionDao; import org.springframework.batch.item.database.support.DataFieldMaxValueIncrementerFactory; import org.springframework.batch.item.database.support.DefaultDataFieldMaxValueIncrementerFactory; +import org.springframework.batch.support.DatabaseType; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.jdbc.core.simple.SimpleJdbcOperations; @@ -92,6 +93,10 @@ public class JobExplorerFactoryBean extends AbstractJobExplorerFactoryBean imple incrementerFactory = new DefaultDataFieldMaxValueIncrementerFactory(dataSource); } + if (databaseType == null) { + databaseType = DatabaseType.fromMetaData(dataSource).name(); + } + Assert.isTrue(incrementerFactory.isSupportedIncrementerType(databaseType), "'" + databaseType + "' is an unsupported database type. The supported database types are " + StringUtils.arrayToCommaDelimitedString(incrementerFactory.getSupportedIncrementerTypes())); diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/exlore/support/JobExplorerFactoryBeanTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/exlore/support/JobExplorerFactoryBeanTests.java index c67cac6e1..6d70302c9 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/exlore/support/JobExplorerFactoryBeanTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/exlore/support/JobExplorerFactoryBeanTests.java @@ -22,6 +22,9 @@ import static org.easymock.EasyMock.expect; import static org.easymock.EasyMock.replay; import static org.easymock.EasyMock.verify; +import java.sql.Connection; +import java.sql.DatabaseMetaData; + import javax.sql.DataSource; import org.junit.Before; @@ -57,20 +60,45 @@ public class JobExplorerFactoryBeanTests { } + @Test + public void testDetectDatabaseType() throws Exception { + + DatabaseMetaData dmd = createMock(DatabaseMetaData.class); + Connection con = createMock(Connection.class); + expect(dataSource.getConnection()).andReturn(con); + expect(con.getMetaData()).andReturn(dmd); + expect(dmd.getDatabaseProductName()).andReturn("Oracle"); + expect(incrementerFactory.isSupportedIncrementerType("ORACLE")).andReturn(true); + expect(incrementerFactory.getSupportedIncrementerTypes()).andReturn(new String[0]); + expect(incrementerFactory.getIncrementer("ORACLE", tablePrefix + "JOB_SEQ")).andReturn(new StubIncrementer()); + expect(incrementerFactory.getIncrementer("ORACLE", tablePrefix + "JOB_EXECUTION_SEQ")).andReturn( + new StubIncrementer()); + expect(incrementerFactory.getIncrementer("ORACLE", tablePrefix + "STEP_EXECUTION_SEQ")).andReturn( + new StubIncrementer()); + replay(dataSource, con, dmd, incrementerFactory); + factory.afterPropertiesSet(); + + } + @Test public void testNoDatabaseType() throws Exception { + DatabaseMetaData dmd = createMock(DatabaseMetaData.class); + Connection con = createMock(Connection.class); + expect(dataSource.getConnection()).andReturn(con); + expect(con.getMetaData()).andReturn(dmd); + expect(dmd.getDatabaseProductName()).andReturn("foo"); try { expect(incrementerFactory.isSupportedIncrementerType(null)).andReturn(false); expect(incrementerFactory.getSupportedIncrementerTypes()).andReturn(new String[0]); - replay(incrementerFactory); + replay(dataSource, con, dmd, incrementerFactory); factory.afterPropertiesSet(); fail(); } catch (IllegalArgumentException ex) { // expected String message = ex.getMessage(); - assertTrue("Wrong message: " + message, message.indexOf("unsupported database type") >= 0); + assertTrue("Wrong message: " + message, message.indexOf("DatabaseType") >= 0); } } @@ -117,9 +145,12 @@ public class JobExplorerFactoryBeanTests { expect(incrementerFactory.isSupportedIncrementerType("foo")).andReturn(true); expect(incrementerFactory.getSupportedIncrementerTypes()).andReturn(new String[0]); - expect(incrementerFactory.getIncrementer(databaseType, tablePrefix + "JOB_SEQ")).andReturn(new StubIncrementer()); - expect(incrementerFactory.getIncrementer(databaseType, tablePrefix + "JOB_EXECUTION_SEQ")).andReturn(new StubIncrementer()); - expect(incrementerFactory.getIncrementer(databaseType, tablePrefix + "STEP_EXECUTION_SEQ")).andReturn(new StubIncrementer()); + expect(incrementerFactory.getIncrementer(databaseType, tablePrefix + "JOB_SEQ")).andReturn( + new StubIncrementer()); + expect(incrementerFactory.getIncrementer(databaseType, tablePrefix + "JOB_EXECUTION_SEQ")).andReturn( + new StubIncrementer()); + expect(incrementerFactory.getIncrementer(databaseType, tablePrefix + "STEP_EXECUTION_SEQ")).andReturn( + new StubIncrementer()); replay(incrementerFactory); factory.afterPropertiesSet(); diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/support/SystemPropertyInitializer.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/support/SystemPropertyInitializer.java new file mode 100644 index 000000000..9c3c92ec1 --- /dev/null +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/support/SystemPropertyInitializer.java @@ -0,0 +1,71 @@ +/* + * Copyright 2006-2007 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.batch.support; + +import org.springframework.beans.factory.InitializingBean; +import org.springframework.util.Assert; + +/** + * Helper class that sets up a System property with a default value. A System + * property is created with the specified key name, and default value (i.e. if + * the property already exists it is not changed). + * + * @author Dave Syer + * + */ +public class SystemPropertyInitializer implements InitializingBean { + + /** + * Name of system property used by default. + */ + public static final String ENVIRONMENT = "org.springframework.batch.support.SystemPropertyInitializer.ENVIRONMENT"; + + private String keyName = ENVIRONMENT; + + private String defaultValue; + + /** + * Set the key name for the System property that is created. Defaults to + * {@link #ENVIRONMENT}. + * + * @param keyName the key name to set + */ + public void setKeyName(String keyName) { + this.keyName = keyName; + } + + /** + * Mandatory property specifying the default value of the System property. + * + * @param defaultValue the default value to set + */ + public void setDefaultValue(String defaultValue) { + this.defaultValue = defaultValue; + } + + /** + * Sets the System property with the provided name and default value. + * + * @see InitializingBean#afterPropertiesSet() + */ + public void afterPropertiesSet() throws Exception { + Assert.state(defaultValue != null || System.getProperty(keyName) != null, + "Either a default value must be specified or the value should already be set for System property: " + + keyName); + System.setProperty(keyName, System.getProperty(keyName, defaultValue)); + } + +} diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/support/SystemPropertyInitializerTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/support/SystemPropertyInitializerTests.java new file mode 100644 index 000000000..81234545a --- /dev/null +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/support/SystemPropertyInitializerTests.java @@ -0,0 +1,60 @@ +/* + * Copyright 2006-2007 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.batch.support; + +import static org.junit.Assert.assertEquals; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * @author Dave Syer + * + */ +public class SystemPropertyInitializerTests { + + private static final String SIMPLE_NAME = SystemPropertyInitializerTests.class.getSimpleName(); + private SystemPropertyInitializer initializer = new SystemPropertyInitializer(); + + @Before + @After + public void initializeProperty() { + System.clearProperty(SystemPropertyInitializer.ENVIRONMENT); + System.clearProperty(SIMPLE_NAME); + } + + @Test + public void testSetKeyName() throws Exception { + initializer.setKeyName(SIMPLE_NAME); + System.setProperty(SIMPLE_NAME, "foo"); + initializer.afterPropertiesSet(); + assertEquals("foo", System.getProperty(SIMPLE_NAME)); + } + + @Test + public void testSetDefaultValue() throws Exception { + initializer.setDefaultValue("foo"); + initializer.afterPropertiesSet(); + assertEquals("foo", System.getProperty(SystemPropertyInitializer.ENVIRONMENT)); + } + + @Test(expected=IllegalStateException.class) + public void testNoDefaultValue() throws Exception { + initializer.afterPropertiesSet(); + } + +} diff --git a/spring-batch-samples/src/main/resources/adhoc-job-launcher-context.xml b/spring-batch-samples/src/main/resources/adhoc-job-launcher-context.xml index 921151431..53cff59e3 100644 --- a/spring-batch-samples/src/main/resources/adhoc-job-launcher-context.xml +++ b/spring-batch-samples/src/main/resources/adhoc-job-launcher-context.xml @@ -37,7 +37,6 @@ - diff --git a/spring-batch-samples/src/main/resources/data-source-context.xml b/spring-batch-samples/src/main/resources/data-source-context.xml index f4e0730cb..76c9ead29 100644 --- a/spring-batch-samples/src/main/resources/data-source-context.xml +++ b/spring-batch-samples/src/main/resources/data-source-context.xml @@ -6,39 +6,28 @@ + + + - - - - - - environment - - - environment - - - - hsql - - - - + + - + @@ -47,18 +36,22 @@ + - + + + + \ No newline at end of file diff --git a/spring-batch-samples/src/main/resources/simple-job-launcher-context.xml b/spring-batch-samples/src/main/resources/simple-job-launcher-context.xml index 3f1063789..5644c2fa4 100644 --- a/spring-batch-samples/src/main/resources/simple-job-launcher-context.xml +++ b/spring-batch-samples/src/main/resources/simple-job-launcher-context.xml @@ -18,10 +18,6 @@ - @@ -37,7 +33,7 @@ + p:dataSource-ref="dataSource" /> diff --git a/spring-batch-samples/src/main/resources/skipSample-job-launcher-context.xml b/spring-batch-samples/src/main/resources/skipSample-job-launcher-context.xml index c34c4291d..039ef7bbc 100644 --- a/spring-batch-samples/src/main/resources/skipSample-job-launcher-context.xml +++ b/spring-batch-samples/src/main/resources/skipSample-job-launcher-context.xml @@ -15,7 +15,6 @@ - @@ -29,7 +28,7 @@ p:jobRegistry-ref="jobRegistry" /> + p:dataSource-ref="dataSource" /> diff --git a/spring-batch-test/src/test/resources/data-source-context.xml b/spring-batch-test/src/test/resources/data-source-context.xml index 25c7732fd..9a84e7bf3 100755 --- a/spring-batch-test/src/test/resources/data-source-context.xml +++ b/spring-batch-test/src/test/resources/data-source-context.xml @@ -34,30 +34,15 @@ construct a properties file on the classpath. The default is "hsql". --> - - - - - environment - - - environment - - - - hsql - - - - + class="org.springframework.batch.support.SystemPropertyInitializer"> + - + @@ -70,7 +55,7 @@ - +