From e2094d1b2edc1abc949cba4cf70a3dadbae7acfc Mon Sep 17 00:00:00 2001 From: lucasward Date: Mon, 3 Mar 2008 20:23:26 +0000 Subject: [PATCH] BATCH-407:Added repository factory to samples and created a factory for incrementers for that logic to obtain them could be unit tested. Also renamed SimpelJobRepositoryFactoryBean to just JobRepositoryFactoryBean --- .../launch/support/CommandLineJobRunner.java | 2 +- ...ean.java => JobRepositoryFactoryBean.java} | 79 ++++--------- .../JobRepositoryFactoryBeanTests.java | 104 ++++++++++++++++++ 3 files changed, 127 insertions(+), 58 deletions(-) rename spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/{SimpleJobRepositoryFactoryBean.java => JobRepositoryFactoryBean.java} (51%) create mode 100644 spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/JobRepositoryFactoryBeanTests.java diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/launch/support/CommandLineJobRunner.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/launch/support/CommandLineJobRunner.java index f0b2b553f..98f299f63 100644 --- a/spring-batch-execution/src/main/java/org/springframework/batch/execution/launch/support/CommandLineJobRunner.java +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/launch/support/CommandLineJobRunner.java @@ -98,7 +98,7 @@ import org.springframework.util.StringUtils; *

* * java org.springframework.batch.execution.bootstrap.support.CommandLineJobRunner testJob.xml - * testJob standard-job-launcher.xml schedule.date=2008/01/24 vendor.id=3902483920 + * testJob schedule.date=2008/01/24 vendor.id=3902483920 *

* *

Once arguments have been successfully parsed, autowiring will be used to set diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/SimpleJobRepositoryFactoryBean.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/JobRepositoryFactoryBean.java similarity index 51% rename from spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/SimpleJobRepositoryFactoryBean.java rename to spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/JobRepositoryFactoryBean.java index 78def57e2..18dc1a6bb 100644 --- a/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/SimpleJobRepositoryFactoryBean.java +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/JobRepositoryFactoryBean.java @@ -24,50 +24,27 @@ import org.springframework.batch.execution.repository.dao.JdbcStepExecutionDao; import org.springframework.batch.execution.repository.dao.JobExecutionDao; import org.springframework.batch.execution.repository.dao.JobInstanceDao; import org.springframework.batch.execution.repository.dao.StepExecutionDao; -import org.springframework.beans.factory.BeanCreationException; +import org.springframework.batch.support.DataFieldMaxValueIncrementerFactory; +import org.springframework.batch.support.DefaultDataFieldMaxValueIncrementerFactory; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.support.incrementer.DB2SequenceMaxValueIncrementer; -import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer; -import org.springframework.jdbc.support.incrementer.DerbyMaxValueIncrementer; -import org.springframework.jdbc.support.incrementer.HsqlMaxValueIncrementer; -import org.springframework.jdbc.support.incrementer.MySQLMaxValueIncrementer; -import org.springframework.jdbc.support.incrementer.OracleSequenceMaxValueIncrementer; -import org.springframework.jdbc.support.incrementer.PostgreSQLSequenceMaxValueIncrementer; +import org.springframework.util.Assert; /** * A {@link FactoryBean} that automates the creation of a {@link SimpleJobRepository}. Requires the user - * to describe what kind of database they are using. Valid values are: - * - *

    - *
  • db2
  • - *
  • derby
  • - *
  • hsql
  • - *
  • mysql
  • - *
  • oracle
  • - *
  • postgres
  • - *
+ * to describe what kind of database they are using. * * @author Ben Hale + * @author Lucas Ward */ -public class SimpleJobRepositoryFactoryBean implements FactoryBean, InitializingBean { - - private static final String DB_TYPE_DB2 = "db2"; - - private static final String DB_TYPE_DERBY = "derby"; - - private static final String DB_TYPE_HSQL = "hsql"; - - private static final String DB_TYPE_MYSQL = "mysql"; - - private static final String DB_TYPE_ORACLE = "oracle"; - - private static final String DB_TYPE_POSTGRES = "postgres"; +public class JobRepositoryFactoryBean implements FactoryBean, InitializingBean { private DataSource dataSource; private String databaseType; + + private DataFieldMaxValueIncrementerFactory incrementerFactory; public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; @@ -76,14 +53,20 @@ public class SimpleJobRepositoryFactoryBean implements FactoryBean, Initializing public void setDatabaseType(String dbType) { this.databaseType = dbType; } + + public void setIncrementerFactory( + DataFieldMaxValueIncrementerFactory incrementerFactory) { + this.incrementerFactory = incrementerFactory; + } public void afterPropertiesSet() throws Exception { - if (!DB_TYPE_DB2.equals(databaseType) && !DB_TYPE_DERBY.equals(databaseType) - && !DB_TYPE_HSQL.equals(databaseType) && !DB_TYPE_MYSQL.equals(databaseType) - && !DB_TYPE_ORACLE.equals(databaseType) && !DB_TYPE_POSTGRES.equals(databaseType)) { - throw new BeanCreationException( - "Database type must be one of: 'db2', 'derby', 'hsql', 'mysql', 'oracle', 'postgres'"); + Assert.notNull(dataSource, "Datasource must not be null."); + + if(incrementerFactory == null){ + incrementerFactory = new DefaultDataFieldMaxValueIncrementerFactory(dataSource); } + + Assert.isTrue(incrementerFactory.isSupportedIncrementerType(databaseType), "Unsupported database type"); } public Object getObject() throws Exception { @@ -105,7 +88,7 @@ public class SimpleJobRepositoryFactoryBean implements FactoryBean, Initializing private JobInstanceDao createJobInstanceDao(JdbcTemplate jdbcTemplate) throws Exception { JdbcJobInstanceDao dao = new JdbcJobInstanceDao(); dao.setJdbcTemplate(jdbcTemplate); - dao.setJobIncrementer(getIncrementer(dataSource, "BATCH_JOB_SEQ")); + dao.setJobIncrementer(incrementerFactory.getIncrementer(databaseType, "BATCH_JOB_SEQ")); dao.afterPropertiesSet(); return dao; } @@ -113,7 +96,7 @@ public class SimpleJobRepositoryFactoryBean implements FactoryBean, Initializing private JobExecutionDao createJobExecutionDao(JdbcTemplate jdbcTemplate) throws Exception { JdbcJobExecutionDao dao = new JdbcJobExecutionDao(); dao.setJdbcTemplate(jdbcTemplate); - dao.setJobExecutionIncrementer(getIncrementer(dataSource, "BATCH_JOB_EXECUTION_SEQ")); + dao.setJobExecutionIncrementer(incrementerFactory.getIncrementer(databaseType, "BATCH_JOB_EXECUTION_SEQ")); dao.afterPropertiesSet(); return dao; } @@ -121,26 +104,8 @@ public class SimpleJobRepositoryFactoryBean implements FactoryBean, Initializing private StepExecutionDao createStepExecutionDao(JdbcTemplate jdbcTemplate) throws Exception { JdbcStepExecutionDao dao = new JdbcStepExecutionDao(); dao.setJdbcTemplate(jdbcTemplate); - dao.setStepExecutionIncrementer(getIncrementer(dataSource, "BATCH_STEP_EXECUTION_SEQ")); + dao.setStepExecutionIncrementer(incrementerFactory.getIncrementer(databaseType, "BATCH_STEP_EXECUTION_SEQ")); dao.afterPropertiesSet(); return dao; } - - private DataFieldMaxValueIncrementer getIncrementer(DataSource dataSource, String incrementerName) { - if (DB_TYPE_DB2.equals(databaseType)) { - return new DB2SequenceMaxValueIncrementer(dataSource, incrementerName); - } else if (DB_TYPE_DERBY.equals(databaseType)) { - return new DerbyMaxValueIncrementer(dataSource, incrementerName, "id"); - } else if (DB_TYPE_HSQL.equals(databaseType)) { - return new HsqlMaxValueIncrementer(dataSource, incrementerName, "id"); - } else if (DB_TYPE_MYSQL.equals(databaseType)) { - return new MySQLMaxValueIncrementer(dataSource, incrementerName, "id"); - } else if (DB_TYPE_ORACLE.equals(databaseType)) { - return new OracleSequenceMaxValueIncrementer(dataSource, incrementerName); - } else if (DB_TYPE_POSTGRES.equals(databaseType)) { - return new PostgreSQLSequenceMaxValueIncrementer(dataSource, incrementerName); - } - throw new IllegalArgumentException("databaseType argument was not on the approved list"); - } - } diff --git a/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/JobRepositoryFactoryBeanTests.java b/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/JobRepositoryFactoryBeanTests.java new file mode 100644 index 000000000..e7cc561e6 --- /dev/null +++ b/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/JobRepositoryFactoryBeanTests.java @@ -0,0 +1,104 @@ +/* + * Copyright 2006-2008 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.execution.repository; + +import javax.sql.DataSource; + +import junit.framework.TestCase; + +import org.easymock.MockControl; +import org.springframework.batch.support.DataFieldMaxValueIncrementerFactory; +import org.springframework.dao.DataAccessException; +import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer; + +/** + * @author Lucas Ward + * + */ +public class JobRepositoryFactoryBeanTests extends TestCase{ + + JobRepositoryFactoryBean factory; + MockControl incrementerControl = MockControl.createControl(DataFieldMaxValueIncrementerFactory.class); + DataFieldMaxValueIncrementerFactory incrementerFactory; + DataSource dataSource; + + protected void setUp() throws Exception { + super.setUp(); + + factory = new JobRepositoryFactoryBean(); + MockControl dataSourceControl = MockControl.createControl(DataSource.class); + dataSource = (DataSource)dataSourceControl.getMock(); + factory.setDataSource(dataSource); + incrementerFactory = (DataFieldMaxValueIncrementerFactory)incrementerControl.getMock(); + factory.setIncrementerFactory(incrementerFactory); + } + + public void testNoDatabaseType() throws Exception{ + + try{ + factory.afterPropertiesSet(); + fail(); + } + catch(IllegalArgumentException ex){ + //expected + } + } + + public void testInvalidDatabaseType() throws Exception{ + + factory.setDatabaseType("invalid type"); + try{ + factory.afterPropertiesSet(); + fail(); + } + catch(IllegalArgumentException ex){ + //expected + } + } + + public void testCreateRepository() throws Exception{ + String databaseType = "databaseType"; + factory.setDatabaseType(databaseType); + + incrementerFactory.getIncrementer(databaseType, "BATCH_JOB_SEQ"); + incrementerControl.setReturnValue(new StubIncrementer()); + incrementerFactory.getIncrementer(databaseType, "BATCH_JOB_EXECUTION_SEQ"); + incrementerControl.setReturnValue(new StubIncrementer()); + incrementerFactory.getIncrementer(databaseType, "BATCH_STEP_EXECUTION_SEQ"); + incrementerControl.setReturnValue(new StubIncrementer()); + incrementerControl.replay(); + + factory.getObject(); + + incrementerControl.verify(); + } + + private class StubIncrementer implements DataFieldMaxValueIncrementer { + + public int nextIntValue() throws DataAccessException { + return 0; + } + + public long nextLongValue() throws DataAccessException { + return 0; + } + + public String nextStringValue() throws DataAccessException { + return null; + } + + } +}