From 93760bd298b0197df474abffca008b6cd56b6151 Mon Sep 17 00:00:00 2001 From: lucasward Date: Mon, 3 Mar 2008 20:23:14 +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 --- .../DataFieldMaxValueIncrementerFactory.java | 47 ++++++++ ...ltDataFieldMaxValueIncrementerFactory.java | 96 ++++++++++++++++ ...aFieldMaxValueIncrementerFactoryTests.java | 106 ++++++++++++++++++ 3 files changed, 249 insertions(+) create mode 100644 spring-batch-infrastructure/src/main/java/org/springframework/batch/support/DataFieldMaxValueIncrementerFactory.java create mode 100644 spring-batch-infrastructure/src/main/java/org/springframework/batch/support/DefaultDataFieldMaxValueIncrementerFactory.java create mode 100644 spring-batch-infrastructure/src/test/java/org/springframework/batch/support/DefaultDataFieldMaxValueIncrementerFactoryTests.java diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/support/DataFieldMaxValueIncrementerFactory.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/support/DataFieldMaxValueIncrementerFactory.java new file mode 100644 index 000000000..d43bb3623 --- /dev/null +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/support/DataFieldMaxValueIncrementerFactory.java @@ -0,0 +1,47 @@ +/* + * 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.support; + +import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer; + +/** + * Factory for creating {@link DataFieldMaxValueIncrementer} implementations + * based upon a provided string. + * + * @author Lucas Ward + * + */ +public interface DataFieldMaxValueIncrementerFactory { + + /** + * Return the {@link DataFieldMaxValueIncrementer} for the provided database type. + * + * @param databaseType string represented database type + * @param incrementerName incrementer name to create. In many cases this may be the + * sequence name + * @return incrementer + * @throws IllegalArgumentException if databaseType is invalid type, or incrementerName + * is null. + */ + public DataFieldMaxValueIncrementer getIncrementer(String databaseType, String incrementerName); + + /** + * Returns boolean indicated whether or not the provided string is supported by this + * factory. + * + */ + public boolean isSupportedIncrementerType(String databaseType); +} diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/support/DefaultDataFieldMaxValueIncrementerFactory.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/support/DefaultDataFieldMaxValueIncrementerFactory.java new file mode 100644 index 000000000..879ea7baf --- /dev/null +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/support/DefaultDataFieldMaxValueIncrementerFactory.java @@ -0,0 +1,96 @@ +/* + * 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.support; + +import javax.sql.DataSource; + +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; + +/** + * Default implementation of the {@link DataFieldMaxValueIncrementerFactory} + * interface. Valid types are: + * + * Valid values are: + * + * + * + * @author Lucas Ward + * + */ +public class DefaultDataFieldMaxValueIncrementerFactory implements + DataFieldMaxValueIncrementerFactory { + + 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"; + + private DataSource dataSource; + + public DefaultDataFieldMaxValueIncrementerFactory(DataSource dataSource) { + this.dataSource = dataSource; + } + + public DataFieldMaxValueIncrementer getIncrementer(String incrementerType, String incrementerName) { + if (DB_TYPE_DB2.equals(incrementerType)) { + return new DB2SequenceMaxValueIncrementer(dataSource, incrementerName); + } else if (DB_TYPE_DERBY.equals(incrementerType)) { + return new DerbyMaxValueIncrementer(dataSource, incrementerName, "id"); + } else if (DB_TYPE_HSQL.equals(incrementerType)) { + return new HsqlMaxValueIncrementer(dataSource, incrementerName, "id"); + } else if (DB_TYPE_MYSQL.equals(incrementerType)) { + return new MySQLMaxValueIncrementer(dataSource, incrementerName, "id"); + } else if (DB_TYPE_ORACLE.equals(incrementerType)) { + return new OracleSequenceMaxValueIncrementer(dataSource, incrementerName); + } else if (DB_TYPE_POSTGRES.equals(incrementerType)) { + return new PostgreSQLSequenceMaxValueIncrementer(dataSource, incrementerName); + } + throw new IllegalArgumentException("databaseType argument was not on the approved list"); + + } + + public boolean isSupportedIncrementerType(String incrementerType) { + if (!DB_TYPE_DB2.equals(incrementerType) && !DB_TYPE_DERBY.equals(incrementerType) + && !DB_TYPE_HSQL.equals(incrementerType) && !DB_TYPE_MYSQL.equals(incrementerType) + && !DB_TYPE_ORACLE.equals(incrementerType) && !DB_TYPE_POSTGRES.equals(incrementerType)) { + + return false; + } + else{ + return true; + } + } +} diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/support/DefaultDataFieldMaxValueIncrementerFactoryTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/support/DefaultDataFieldMaxValueIncrementerFactoryTests.java new file mode 100644 index 000000000..07c5f898e --- /dev/null +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/support/DefaultDataFieldMaxValueIncrementerFactoryTests.java @@ -0,0 +1,106 @@ +/* + * 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.support; + +import javax.sql.DataSource; + +import junit.framework.TestCase; + +import org.easymock.MockControl; +import org.springframework.jdbc.support.incrementer.DB2SequenceMaxValueIncrementer; +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; + +/** + * @author Lucas Ward + * + */ +public class DefaultDataFieldMaxValueIncrementerFactoryTests extends TestCase { + + DefaultDataFieldMaxValueIncrementerFactory factory; + + /* (non-Javadoc) + * @see junit.framework.TestCase#setUp() + */ + protected void setUp() throws Exception { + super.setUp(); + + DataSource dataSource = (DataSource)MockControl.createControl(DataSource.class).getMock(); + factory = new DefaultDataFieldMaxValueIncrementerFactory(dataSource); + } + + public void testSupportedDatabaseType(){ + assertTrue(factory.isSupportedIncrementerType("db2")); + assertTrue(factory.isSupportedIncrementerType("mysql")); + assertTrue(factory.isSupportedIncrementerType("derby")); + assertTrue(factory.isSupportedIncrementerType("oracle")); + assertTrue(factory.isSupportedIncrementerType("postgres")); + assertTrue(factory.isSupportedIncrementerType("hsql")); + } + + public void testUnsupportedDatabaseType(){ + assertFalse(factory.isSupportedIncrementerType("invalidtype")); + } + + public void testInvalidDatabaseType(){ + try{ + factory.getIncrementer("invalidtype", "NAME"); + fail(); + } + catch(IllegalArgumentException ex){ + //expected + } + } + + public void testNullIncrementerName(){ + try{ + factory.getIncrementer("db2", null); + fail(); + } + catch(IllegalArgumentException ex){ + //expected + } + } + + public void testDb2(){ + assertTrue(factory.getIncrementer("db2", "NAME") instanceof DB2SequenceMaxValueIncrementer); + } + + public void testMysql(){ + assertTrue(factory.getIncrementer("mysql", "NAME") instanceof MySQLMaxValueIncrementer); + } + + public void testOracle(){ + assertTrue(factory.getIncrementer("oracle", "NAME") instanceof OracleSequenceMaxValueIncrementer); + } + + public void testDerby(){ + assertTrue(factory.getIncrementer("derby", "NAME") instanceof DerbyMaxValueIncrementer); + } + + public void testHsql(){ + assertTrue(factory.getIncrementer("hsql", "NAME") instanceof HsqlMaxValueIncrementer); + } + + public void testPostgres(){ + assertTrue(factory.getIncrementer("postgres", "NAME") instanceof PostgreSQLSequenceMaxValueIncrementer); + } + + +}