diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/support/JobRepositoryFactoryBean.java b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/support/JobRepositoryFactoryBean.java index fc49c375f..555ca0017 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/support/JobRepositoryFactoryBean.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/support/JobRepositoryFactoryBean.java @@ -18,6 +18,8 @@ package org.springframework.batch.core.repository.support; import javax.sql.DataSource; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.springframework.aop.framework.ProxyFactory; import org.springframework.aop.support.DefaultPointcutAdvisor; import org.springframework.aop.support.NameMatchMethodPointcut; @@ -33,6 +35,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.batch.support.PropertiesConverter; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; @@ -54,6 +57,8 @@ import org.springframework.util.StringUtils; */ public class JobRepositoryFactoryBean extends AbstractJobRepositoryFactoryBean implements InitializingBean { + protected static final Log logger = LogFactory.getLog(JobRepositoryFactoryBean.class); + /** * Default value for isolation level in create* method. */ @@ -136,6 +141,11 @@ public class JobRepositoryFactoryBean extends AbstractJobRepositoryFactoryBean i if (incrementerFactory == null) { incrementerFactory = new DefaultDataFieldMaxValueIncrementerFactory(dataSource); } + + if(databaseType == null){ + logger.info("No database type set, using meta data"); + databaseType = DatabaseType.fromMetaData(dataSource).name(); + } Assert.isTrue(incrementerFactory.isSupportedIncrementerType(databaseType), "'" + databaseType + "' is an unsupported database type. The supported database types are " diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/JobRepositoryFactoryBeanTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/JobRepositoryFactoryBeanTests.java index 8517af8c1..fb8d6a8d6 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/JobRepositoryFactoryBeanTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/JobRepositoryFactoryBeanTests.java @@ -17,6 +17,7 @@ package org.springframework.batch.core.repository.support; import static junit.framework.Assert.*; import static org.easymock.EasyMock.*; + import org.springframework.batch.core.JobParameters; import org.springframework.batch.core.repository.JobRepository; import org.springframework.batch.item.database.support.DataFieldMaxValueIncrementerFactory; @@ -29,6 +30,7 @@ import org.junit.Test; import javax.sql.DataSource; import java.sql.Connection; +import java.sql.DatabaseMetaData; /** * @author Lucas Ward @@ -63,19 +65,24 @@ public class JobRepositoryFactoryBeanTests { @Test public void testNoDatabaseType() throws Exception { - try { - expect(incrementerFactory.isSupportedIncrementerType(null)).andReturn(false); - expect(incrementerFactory.getSupportedIncrementerTypes()).andReturn(new String[0]); - replay(incrementerFactory); - factory.afterPropertiesSet(); - fail(); - } - catch (IllegalArgumentException ex) { - // expected - String message = ex.getMessage(); - assertTrue("Wrong message: " + message, message.indexOf("unsupported database type") >= 0); - } + 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(); + factory.getObject(); + verify(incrementerFactory); + } @Test diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/resource/StepExecutionResourceProxyTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/resource/StepExecutionResourceProxyTests.java index 7846c6597..e356b328c 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/resource/StepExecutionResourceProxyTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/resource/StepExecutionResourceProxyTests.java @@ -102,6 +102,33 @@ public class StepExecutionResourceProxyTests extends TestCase { resource.beforeStep(jobExecution.createStepExecution(step.getName())); doTestPathName("spam-foo", "foo" + pathsep + "data" + pathsep); } + +// public void testNonExistentJobParameter() throws Exception{ +// +// resource.setFilePattern("foo/data/%JOB_NAME%/%non.key%-foo"); +// jobInstance = new JobInstance(new Long(0), new JobParametersBuilder().addString("job.key", "spam") +// .toJobParameters(), "testJob"); +// JobExecution jobExecution = new JobExecution(jobInstance); +// Step step = new StepSupport("bar"); +// try{ +// resource.beforeStep(jobExecution.createStepExecution(step.getName())); +// fail(); +// } +// catch(Exception ex){ +// //expected, if there isn't a JobParameter for that key, it should throw an exception +// } +// } + + public void testLongJobParameter() throws Exception { + + resource.setFilePattern("foo/data/%JOB_NAME%/%job.key(long)%-foo"); + jobInstance = new JobInstance(new Long(0), new JobParametersBuilder().addLong("job.key", 123L) + .toJobParameters(), "testJob"); + JobExecution jobExecution = new JobExecution(jobInstance); + Step step = new StepSupport("bar"); + resource.beforeStep(jobExecution.createStepExecution(step.getName())); + doTestPathName("123-foo", "foo" + pathsep + "data" + pathsep); + } public void testResoureLoaderAware() throws Exception { resource = new StepExecutionResourceProxy();