From ba7490b452cf0838b4650affda1a995037542737 Mon Sep 17 00:00:00 2001 From: lucasward Date: Mon, 15 Oct 2007 05:23:00 +0000 Subject: [PATCH] BATCH 144 and BATCH 150: create Hibernate and iBatis input sources with sample jobs. --- samples/.springBeans | 5 + samples/pom.xml | 322 +++++++------- .../sample/dao/CustomerCreditWriter.java | 7 +- .../dao/FlatFileCustomerCreditWriter.java | 8 +- .../sample/dao/HibernateCreditWriter.java | 43 ++ .../dao/IbatisCustomerCreditWriter.java | 64 +++ .../batch/sample/domain/CustomerCredit.java | 127 +++--- .../CustomerCreditIncreaseProcessor.java | 30 ++ .../src/main/resources/CustomerCredit.hbm.xml | 16 + samples/src/main/resources/ibatis-config.xml | 9 + .../main/resources/ibatis-customer-credit.xml | 24 ++ .../resources/jobs/fixedLengthImportJob.xml | 2 +- .../src/main/resources/jobs/hibernateJob.xml | 75 ++++ samples/src/main/resources/jobs/ibatisJob.xml | 68 +++ .../jobs/multilineOrderInputDescriptors.xml | 2 +- .../jobs/multilineOrderOutputDescriptors.xml | 2 +- .../main/resources/jobs/simpleTaskletJob.xml | 2 +- .../resources/simple-container-definition.xml | 6 +- .../AbstractCustomerCreditIncreaseTests.java | 66 +++ .../sample/HibernateJobFunctionalTests.java | 13 + .../sample/IbatisJobFunctionalTests.java | 15 + .../batch/sample/TradeJobFunctionalTests.java | 393 +++++++++--------- .../CustomerCreditIncreaseProcessorTests.java | 53 +++ 23 files changed, 935 insertions(+), 417 deletions(-) create mode 100644 samples/src/main/java/org/springframework/batch/sample/dao/HibernateCreditWriter.java create mode 100644 samples/src/main/java/org/springframework/batch/sample/dao/IbatisCustomerCreditWriter.java create mode 100644 samples/src/main/java/org/springframework/batch/sample/item/processor/CustomerCreditIncreaseProcessor.java create mode 100644 samples/src/main/resources/CustomerCredit.hbm.xml create mode 100644 samples/src/main/resources/ibatis-config.xml create mode 100644 samples/src/main/resources/ibatis-customer-credit.xml create mode 100644 samples/src/main/resources/jobs/hibernateJob.xml create mode 100644 samples/src/main/resources/jobs/ibatisJob.xml create mode 100644 samples/src/test/java/org/springframework/batch/sample/AbstractCustomerCreditIncreaseTests.java create mode 100644 samples/src/test/java/org/springframework/batch/sample/HibernateJobFunctionalTests.java create mode 100644 samples/src/test/java/org/springframework/batch/sample/IbatisJobFunctionalTests.java create mode 100644 samples/src/test/java/org/springframework/batch/sample/item/processor/CustomerCreditIncreaseProcessorTests.java diff --git a/samples/.springBeans b/samples/.springBeans index 4a84185b2..b50e802f7 100644 --- a/samples/.springBeans +++ b/samples/.springBeans @@ -20,7 +20,12 @@ src/main/resources/jobs/infiniteLoopJob.xml src/main/resources/data-source-context-init.xml src/main/resources/jobs/xmlStaxJob.xml + src/main/resources/jobs/hibernateJob.xml + src/main/resources/jobs/ibatisJob.xml + src/main/resources/jobs/compositeProcessorSampleJob.xml src/main/resources/jobs/nflJob.xml + src/main/resources/jobs/simpleTaskletJob.xml + src/main/resources/beanRefContext.xml diff --git a/samples/pom.xml b/samples/pom.xml index a39e53f1c..76f22f9ad 100644 --- a/samples/pom.xml +++ b/samples/pom.xml @@ -1,153 +1,171 @@ - - 4.0.0 - spring-batch-samples - jar - Samples - - Example batch jobs using Spring Batch Core and Execution. - - - - org.springframework.batch - spring-batch - 1.0-m3-SNAPSHOT - .. - - - - - org.springframework.batch - spring-batch-execution - ${project.version} - - - aspectj - aspectjrt - - - junit - junit - - - org.springmodules - spring-modules-validation - 0.8 - - - rhino - js - - - commons-validator - commons-validator - - - - - org.springframework - spring-test - - - quartz - quartz - 1.5.1 - - - asm - asm-all - 2.2.3 - test - - - aspectj - aspectjweaver - 1.5.3 - test - - - hsqldb - hsqldb - test - - - commons-io - commons-io - test - - - commons-dbcp - commons-dbcp - test - - - org.springframework.ws - spring-oxm - 1.0.0 - true - - - org.springframework - spring-beans - - - org.springframework - spring-core - - - - - geronimo-spec - geronimo-spec-j2ee - 1.4-rc4 - test - - - easymock - easymock - - - - cglib - cglib-nodep - test - - - - com.thoughtworks.xstream - xstream - test - - - - stax - stax - test - - - xmlunit - xmlunit - test - 1.1 - - - - - - - org.apache.maven.plugins - maven-surefire-plugin - 2.3 - - - - **/*Tests.java - - - **/Abstract*.java - **/NflJobFunctionalTests.java - - - - - + + 4.0.0 + spring-batch-samples + jar + Samples + + Example batch jobs using Spring Batch Core and Execution. + + + + org.springframework.batch + spring-batch + 1.0-m3-SNAPSHOT + .. + + + + + org.springframework.batch + spring-batch-execution + ${project.version} + + + aspectj + aspectjrt + + + junit + junit + + + org.springmodules + spring-modules-validation + 0.8 + + + rhino + js + + + commons-validator + commons-validator + + + + + org.springframework + spring-test + + + quartz + quartz + 1.5.1 + + + asm + asm-all + 2.2.3 + test + + + aspectj + aspectjweaver + 1.5.3 + test + + + hsqldb + hsqldb + test + + + commons-io + commons-io + test + + + commons-dbcp + commons-dbcp + test + + + org.springframework.ws + spring-oxm + 1.0.0 + true + + + org.springframework + spring-beans + + + org.springframework + spring-core + + + + + geronimo-spec + geronimo-spec-j2ee + 1.4-rc4 + test + + + easymock + easymock + + + + cglib + cglib-nodep + test + + + + com.thoughtworks.xstream + xstream + test + + + + stax + stax + test + + + xmlunit + xmlunit + test + 1.1 + + + org.hibernate + hibernate + true + + + + org.apache.geronimo.specs + geronimo-jta_1.1_spec + 1.1 + provided + + + + cglib + cglib-nodep + true + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.3 + + + + **/*Tests.java + + + **/Abstract*.java + **/NflJobFunctionalTests.java + + + + + \ No newline at end of file diff --git a/samples/src/main/java/org/springframework/batch/sample/dao/CustomerCreditWriter.java b/samples/src/main/java/org/springframework/batch/sample/dao/CustomerCreditWriter.java index 666a3ff10..53ce99322 100644 --- a/samples/src/main/java/org/springframework/batch/sample/dao/CustomerCreditWriter.java +++ b/samples/src/main/java/org/springframework/batch/sample/dao/CustomerCreditWriter.java @@ -16,16 +16,17 @@ package org.springframework.batch.sample.dao; +import org.springframework.batch.io.OutputSource; import org.springframework.batch.item.ResourceLifecycle; import org.springframework.batch.sample.domain.CustomerCredit; /** * Interface for writing customer's credit information to output. - * + * * @author Robert Kasanicky */ -public interface CustomerCreditWriter extends ResourceLifecycle { +public interface CustomerCreditWriter extends OutputSource { void write(CustomerCredit customerCredit); - + } diff --git a/samples/src/main/java/org/springframework/batch/sample/dao/FlatFileCustomerCreditWriter.java b/samples/src/main/java/org/springframework/batch/sample/dao/FlatFileCustomerCreditWriter.java index b8ef69013..1884e8c4d 100644 --- a/samples/src/main/java/org/springframework/batch/sample/dao/FlatFileCustomerCreditWriter.java +++ b/samples/src/main/java/org/springframework/batch/sample/dao/FlatFileCustomerCreditWriter.java @@ -23,7 +23,7 @@ import org.springframework.beans.factory.DisposableBean; /** * Writes customer's credit information in a file. - * + * * @see CustomerCreditWriter * @author Robert Kasanicky */ @@ -71,10 +71,14 @@ public class FlatFileCustomerCreditWriter implements CustomerCreditWriter, /* * (non-Javadoc) - * + * * @see org.springframework.beans.factory.DisposableBean#destroy() */ public void destroy() throws Exception { close(); } + + public void write(Object output) { + write((CustomerCredit)output); + } } diff --git a/samples/src/main/java/org/springframework/batch/sample/dao/HibernateCreditWriter.java b/samples/src/main/java/org/springframework/batch/sample/dao/HibernateCreditWriter.java new file mode 100644 index 000000000..fee6bef39 --- /dev/null +++ b/samples/src/main/java/org/springframework/batch/sample/dao/HibernateCreditWriter.java @@ -0,0 +1,43 @@ +/* + * 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.sample.dao; + +import org.springframework.batch.io.OutputSource; +import org.springframework.batch.sample.domain.CustomerCredit; +import org.springframework.orm.hibernate3.support.HibernateDaoSupport; + +/** + * @author Lucas Ward + * + */ +public class HibernateCreditWriter extends HibernateDaoSupport implements + CustomerCreditWriter { + + /* (non-Javadoc) + * @see org.springframework.batch.sample.dao.CustomerCreditWriter#write(org.springframework.batch.sample.domain.CustomerCredit) + */ + public void write(CustomerCredit customerCredit) { + getHibernateTemplate().update(customerCredit); + } + + /* (non-Javadoc) + * @see org.springframework.batch.io.OutputSource#write(java.lang.Object) + */ + public void write(Object output) { + write((CustomerCredit)output); + } + +} diff --git a/samples/src/main/java/org/springframework/batch/sample/dao/IbatisCustomerCreditWriter.java b/samples/src/main/java/org/springframework/batch/sample/dao/IbatisCustomerCreditWriter.java new file mode 100644 index 000000000..f6b03b84c --- /dev/null +++ b/samples/src/main/java/org/springframework/batch/sample/dao/IbatisCustomerCreditWriter.java @@ -0,0 +1,64 @@ +/* + * 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.sample.dao; + +import org.springframework.batch.io.InputSource; +import org.springframework.batch.io.OutputSource; +import org.springframework.batch.sample.domain.CustomerCredit; +import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport; + +/** + * @author Lucas Ward + * + */ +public class IbatisCustomerCreditWriter extends SqlMapClientDaoSupport + implements CustomerCreditWriter, OutputSource { + + String statementId; + + /* (non-Javadoc) + * @see org.springframework.batch.sample.dao.CustomerCreditWriter#write(org.springframework.batch.sample.domain.CustomerCredit) + */ + public void write(CustomerCredit customerCredit) { + + getSqlMapClientTemplate().update(statementId, customerCredit); + } + + /* (non-Javadoc) + * @see org.springframework.batch.item.ResourceLifecycle#close() + */ + public void close() { + // TODO Auto-generated method stub + + } + + /* (non-Javadoc) + * @see org.springframework.batch.item.ResourceLifecycle#open() + */ + public void open() { + // TODO Auto-generated method stub + + } + + + public void setStatementId(String statementId) { + this.statementId = statementId; + } + + public void write(Object output) { + write((CustomerCredit)output); + } +} diff --git a/samples/src/main/java/org/springframework/batch/sample/domain/CustomerCredit.java b/samples/src/main/java/org/springframework/batch/sample/domain/CustomerCredit.java index b8a7bca05..799b92fed 100644 --- a/samples/src/main/java/org/springframework/batch/sample/domain/CustomerCredit.java +++ b/samples/src/main/java/org/springframework/batch/sample/domain/CustomerCredit.java @@ -1,57 +1,70 @@ -/* - * 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.sample.domain; - -import java.math.BigDecimal; - -import org.apache.commons.lang.builder.EqualsBuilder; -import org.apache.commons.lang.builder.HashCodeBuilder; - - -public class CustomerCredit { - private String name; - private BigDecimal credit; - - public String toString() { - return "CustomerCredit [name=" + name + ", credit=" + credit + "]"; - } - - public BigDecimal getCredit() { - return credit; - } - - public void setCredit(BigDecimal credit) { - this.credit = credit; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public boolean equals(Object o) { - return EqualsBuilder.reflectionEquals(this, o); - } - - public int hashCode() { - return HashCodeBuilder.reflectionHashCode(this); - } - -} +/* + * 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.sample.domain; + +import java.math.BigDecimal; + +import org.apache.commons.lang.builder.EqualsBuilder; +import org.apache.commons.lang.builder.HashCodeBuilder; + + +public class CustomerCredit { + private int id; + private String name; + private BigDecimal credit; + + public String toString() { + return "CustomerCredit [id=" + id +",name=" + name + ", credit=" + credit + "]"; + } + + public BigDecimal getCredit() { + return credit; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public void setCredit(BigDecimal credit) { + this.credit = credit; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public void increaseCreditBy(BigDecimal sum) { + this.credit = this.credit.add(sum); + } + + public boolean equals(Object o) { + return EqualsBuilder.reflectionEquals(this, o); + } + + public int hashCode() { + return HashCodeBuilder.reflectionHashCode(this); + } + +} diff --git a/samples/src/main/java/org/springframework/batch/sample/item/processor/CustomerCreditIncreaseProcessor.java b/samples/src/main/java/org/springframework/batch/sample/item/processor/CustomerCreditIncreaseProcessor.java new file mode 100644 index 000000000..ca1ebba06 --- /dev/null +++ b/samples/src/main/java/org/springframework/batch/sample/item/processor/CustomerCreditIncreaseProcessor.java @@ -0,0 +1,30 @@ +package org.springframework.batch.sample.item.processor; + +import java.math.BigDecimal; + +import org.springframework.batch.io.OutputSource; +import org.springframework.batch.item.ItemProcessor; +import org.springframework.batch.sample.domain.CustomerCredit; + +/** + * Increases customer's credit by fixed amount. + * + * @author Robert Kasanicky + */ +public class CustomerCreditIncreaseProcessor implements ItemProcessor{ + + public static final BigDecimal FIXED_AMOUNT = new BigDecimal(1000); + + private OutputSource outputSource; + + public void process(Object data) throws Exception { + CustomerCredit customerCredit = (CustomerCredit) data; + customerCredit.increaseCreditBy(FIXED_AMOUNT); + outputSource.write(customerCredit); + } + + public void setOutputSource(OutputSource outputSource) { + this.outputSource = outputSource; + } + +} diff --git a/samples/src/main/resources/CustomerCredit.hbm.xml b/samples/src/main/resources/CustomerCredit.hbm.xml new file mode 100644 index 000000000..02d231884 --- /dev/null +++ b/samples/src/main/resources/CustomerCredit.hbm.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/samples/src/main/resources/ibatis-config.xml b/samples/src/main/resources/ibatis-config.xml new file mode 100644 index 000000000..158fd60bf --- /dev/null +++ b/samples/src/main/resources/ibatis-config.xml @@ -0,0 +1,9 @@ + + + + + + + + \ No newline at end of file diff --git a/samples/src/main/resources/ibatis-customer-credit.xml b/samples/src/main/resources/ibatis-customer-credit.xml new file mode 100644 index 000000000..98f7d26b8 --- /dev/null +++ b/samples/src/main/resources/ibatis-customer-credit.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + update CUSTOMER set CREDIT = #credit# where NAME = #name# + + + \ No newline at end of file diff --git a/samples/src/main/resources/jobs/fixedLengthImportJob.xml b/samples/src/main/resources/jobs/fixedLengthImportJob.xml index 932ba1898..84a01dc20 100644 --- a/samples/src/main/resources/jobs/fixedLengthImportJob.xml +++ b/samples/src/main/resources/jobs/fixedLengthImportJob.xml @@ -52,7 +52,7 @@ - + diff --git a/samples/src/main/resources/jobs/hibernateJob.xml b/samples/src/main/resources/jobs/hibernateJob.xml new file mode 100644 index 000000000..7633d3e3b --- /dev/null +++ b/samples/src/main/resources/jobs/hibernateJob.xml @@ -0,0 +1,75 @@ + + + + + Example for Hibernate integration. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + CustomerCredit.hbm.xml + + + + + hibernate.dialect=org.hibernate.dialect.HSQLDialect + + + + + + + + + + + + + \ No newline at end of file diff --git a/samples/src/main/resources/jobs/ibatisJob.xml b/samples/src/main/resources/jobs/ibatisJob.xml new file mode 100644 index 000000000..31e16d895 --- /dev/null +++ b/samples/src/main/resources/jobs/ibatisJob.xml @@ -0,0 +1,68 @@ + + + + + Example for iBATIS integration. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/samples/src/main/resources/jobs/multilineOrderInputDescriptors.xml b/samples/src/main/resources/jobs/multilineOrderInputDescriptors.xml index a0595cbae..fac0e2dd2 100644 --- a/samples/src/main/resources/jobs/multilineOrderInputDescriptors.xml +++ b/samples/src/main/resources/jobs/multilineOrderInputDescriptors.xml @@ -1,6 +1,6 @@ diff --git a/samples/src/main/resources/jobs/simpleTaskletJob.xml b/samples/src/main/resources/jobs/simpleTaskletJob.xml index a38d6ab3b..c2865e06d 100644 --- a/samples/src/main/resources/jobs/simpleTaskletJob.xml +++ b/samples/src/main/resources/jobs/simpleTaskletJob.xml @@ -89,4 +89,4 @@ - \ No newline at end of file + diff --git a/samples/src/main/resources/simple-container-definition.xml b/samples/src/main/resources/simple-container-definition.xml index 0d86012cf..f14139453 100644 --- a/samples/src/main/resources/simple-container-definition.xml +++ b/samples/src/main/resources/simple-container-definition.xml @@ -23,7 +23,7 @@ - + @@ -73,7 +73,7 @@ - + @@ -119,7 +119,7 @@ - + diff --git a/samples/src/test/java/org/springframework/batch/sample/AbstractCustomerCreditIncreaseTests.java b/samples/src/test/java/org/springframework/batch/sample/AbstractCustomerCreditIncreaseTests.java new file mode 100644 index 000000000..50e4d71e5 --- /dev/null +++ b/samples/src/test/java/org/springframework/batch/sample/AbstractCustomerCreditIncreaseTests.java @@ -0,0 +1,66 @@ +package org.springframework.batch.sample; + +import java.math.BigDecimal; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.List; + +import org.springframework.jdbc.core.JdbcOperations; +import org.springframework.jdbc.core.RowMapper; + +/** + * Test case for jobs that are expected to update customer credit value by fixed amount. + * + * @author Robert Kasanicky + */ +public abstract class AbstractCustomerCreditIncreaseTests extends AbstractLifecycleSpringContextTests { + + private JdbcOperations jdbcTemplate; + + private static final BigDecimal CREDIT_INCREASE = new BigDecimal(1000); + + private static final String ALL_CUSTOMERS = "select * from CUSTOMER"; + + private static final String CREDIT_COLUMN = "CREDIT"; + + private List creditsBeforeUpdate; + + + public void setJdbcTemplate(JdbcOperations jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } + + + /** + * All customers have the same credit + */ + protected void validatePreConditions() throws Exception { + super.validatePreConditions(); + creditsBeforeUpdate = jdbcTemplate.queryForList(ALL_CUSTOMERS); + jdbcTemplate.query(ALL_CUSTOMERS, new RowMapper() { + public Object mapRow(ResultSet rs, int rowNum) throws SQLException { + creditsBeforeUpdate.set(rowNum, rs.getBigDecimal(CREDIT_COLUMN)); + return null; + } + }); + } + + /** + * Credit was increased by REDIT_INCREASE + */ + protected void validatePostConditions() throws Exception { + + + jdbcTemplate.query(ALL_CUSTOMERS, new RowMapper() { + + public Object mapRow(ResultSet rs, int rowNum) throws SQLException { + final BigDecimal CREDIT_BEFORE_UPDATE = (BigDecimal) creditsBeforeUpdate.get(rowNum); + final BigDecimal EXPECTED_CREDIT = CREDIT_BEFORE_UPDATE.add(CREDIT_INCREASE); + assertTrue(EXPECTED_CREDIT.compareTo(rs.getBigDecimal(CREDIT_COLUMN)) == 0); + return null; + } + + }); + } + +} diff --git a/samples/src/test/java/org/springframework/batch/sample/HibernateJobFunctionalTests.java b/samples/src/test/java/org/springframework/batch/sample/HibernateJobFunctionalTests.java new file mode 100644 index 000000000..3be80f6c8 --- /dev/null +++ b/samples/src/test/java/org/springframework/batch/sample/HibernateJobFunctionalTests.java @@ -0,0 +1,13 @@ +package org.springframework.batch.sample; + +/** + * Test for HibernateJob - checks that customer credit has been updated to expected value. + * + * @author Robert Kasanicky + */ +public class HibernateJobFunctionalTests extends AbstractCustomerCreditIncreaseTests { + + protected String[] getConfigLocations() { + return new String[] {"jobs/hibernateJob.xml"}; + } +} diff --git a/samples/src/test/java/org/springframework/batch/sample/IbatisJobFunctionalTests.java b/samples/src/test/java/org/springframework/batch/sample/IbatisJobFunctionalTests.java new file mode 100644 index 000000000..f4653e8ce --- /dev/null +++ b/samples/src/test/java/org/springframework/batch/sample/IbatisJobFunctionalTests.java @@ -0,0 +1,15 @@ +package org.springframework.batch.sample; + + +/** + * Test for IbatisJob - checks that customer credit has been updated to expected value. + * + * @author Robert Kasanicky + */ +public class IbatisJobFunctionalTests extends AbstractCustomerCreditIncreaseTests{ + + protected String[] getConfigLocations() { + return new String[] {"jobs/ibatisJob.xml"}; + } + +} diff --git a/samples/src/test/java/org/springframework/batch/sample/TradeJobFunctionalTests.java b/samples/src/test/java/org/springframework/batch/sample/TradeJobFunctionalTests.java index 8e61534ae..8a4154d84 100644 --- a/samples/src/test/java/org/springframework/batch/sample/TradeJobFunctionalTests.java +++ b/samples/src/test/java/org/springframework/batch/sample/TradeJobFunctionalTests.java @@ -1,196 +1,197 @@ -/* - * 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.sample; - -import java.math.BigDecimal; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.ArrayList; - -import org.springframework.batch.sample.domain.Trade; -import org.springframework.core.io.FileSystemResource; -import org.springframework.core.io.Resource; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.core.RowCallbackHandler; - - - -public class TradeJobFunctionalTests extends AbstractLifecycleSpringContextTests { - - private static final String GET_TRADES = "SELECT isin, quantity, price, customer FROM trade"; - private static final String GET_CUSTOMERS = "SELECT name, credit FROM customer"; - - private ArrayList customers; - private ArrayList trades; - private int activeRow = 0; - - private JdbcTemplate jdbcTemplate; - - /** - * @param jdbcTemplate the jdbcTemplate to set - */ - public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { - this.jdbcTemplate = jdbcTemplate; - } - - protected String[] getConfigLocations() { - return new String[] {"jobs/tradeJob.xml"}; - } - - /* (non-Javadoc) - * @see org.springframework.test.AbstractSingleSpringContextTests#onSetUp() - */ - protected void onSetUp() throws Exception { - super.onSetUp(); - jdbcTemplate.update("delete from TRADE"); - } - - public void testLifecycle() throws Exception{ - super.testLifecycle(); - } - - protected void validatePostConditions() { - - // assertTrue(((Resource)applicationContext.getBean("customerFileLocator")).exists()); - - customers = new ArrayList() {{add(new Customer("customer1", (100000 - 98.34))); - add(new Customer("customer2", (100000 - 18.12 - 12.78))); - add(new Customer("customer3", (100000 - 109.25))); - add(new Customer("customer4", (100000 - 123.39)));}}; - - trades = new ArrayList() {{add(new Trade("UK21341EAH45", 978, new BigDecimal("98.34"), "customer1")); - add(new Trade("UK21341EAH46", 112, new BigDecimal("18.12"), "customer2")); - add(new Trade("UK21341EAH47", 245, new BigDecimal("12.78"), "customer2")); - add(new Trade("UK21341EAH48", 108, new BigDecimal("109.25"), "customer3")); - add(new Trade("UK21341EAH49", 854, new BigDecimal("123.39"), "customer4"));}}; - - // check content of the trade table - jdbcTemplate.query(GET_TRADES, new RowCallbackHandler() { - - public void processRow(ResultSet rs) throws SQLException { - Trade trade = (Trade)trades.get(activeRow++); - - assertTrue(trade.getIsin().equals(rs.getString(1))); - assertTrue(trade.getQuantity() == rs.getLong(2)); - assertTrue(trade.getPrice().equals(rs.getBigDecimal(3))); - assertTrue(trade.getCustomer().equals(rs.getString(4))); - } - }); - - assertTrue(trades.size() == activeRow); - - // check content of the customer table - activeRow = 0; - jdbcTemplate.query(GET_CUSTOMERS, new RowCallbackHandler() { - - public void processRow(ResultSet rs) throws SQLException { - Customer customer = (Customer)customers.get(activeRow++); - - assertTrue(customer.getName().equals(rs.getString(1))); - assertTrue(customer.getCredit() == rs.getDouble(2)); - } - }); - - assertTrue(customers.size() == activeRow); - - // check content of the output file - - -// Clean up - ((FileSystemResource)applicationContext.getBean("customerFileLocator")).getFile().delete(); - } - - protected void validatePreConditions() { - assertTrue(((Resource)applicationContext.getBean("fileLocator")).exists()); - } - - private class Customer { - private String name; - private double credit; - - public Customer(String name, double credit) { - this.name = name; - this.credit = credit; - } - - public Customer(){ - } - - /** - * @return the credit - */ - public double getCredit() { - return credit; - } - /** - * @param credit the credit to set - */ - public void setCredit(double credit) { - this.credit = credit; - } - /** - * @return the name - */ - public String getName() { - return name; - } - /** - * @param name the name to set - */ - public void setName(String name) { - this.name = name; - } - - /* (non-Javadoc) - * @see java.lang.Object#hashCode() - */ - public int hashCode() { - final int PRIME = 31; - int result = 1; - long temp; - temp = Double.doubleToLongBits(credit); - result = PRIME * result + (int) (temp ^ (temp >>> 32)); - result = PRIME * result + ((name == null) ? 0 : name.hashCode()); - return result; - } - - /* (non-Javadoc) - * @see java.lang.Object#equals(java.lang.Object) - */ - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - final Customer other = (Customer) obj; - if (Double.doubleToLongBits(credit) != Double.doubleToLongBits(other.credit)) - return false; - if (name == null) { - if (other.name != null) - return false; - } else if (!name.equals(other.name)) - return false; - return true; - } - - - } - - -} +/* + * 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.sample; + +import java.math.BigDecimal; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +import org.springframework.batch.sample.domain.Trade; +import org.springframework.core.io.FileSystemResource; +import org.springframework.core.io.Resource; +import org.springframework.jdbc.core.JdbcOperations; +import org.springframework.jdbc.core.RowCallbackHandler; + + + +public class TradeJobFunctionalTests extends AbstractLifecycleSpringContextTests { + + private static final String GET_TRADES = "SELECT isin, quantity, price, customer FROM trade"; + private static final String GET_CUSTOMERS = "SELECT name, credit FROM customer"; + + private List customers; + private List trades; + private int activeRow = 0; + + private JdbcOperations jdbcTemplate; + + /** + * @param jdbcTemplate the jdbcTemplate to set + */ + public void setJdbcTemplate(JdbcOperations jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } + + protected String[] getConfigLocations() { + return new String[] {"jobs/tradeJob.xml"}; + } + + /* (non-Javadoc) + * @see org.springframework.test.AbstractSingleSpringContextTests#onSetUp() + */ + protected void onSetUp() throws Exception { + super.onSetUp(); + jdbcTemplate.update("delete from TRADE"); + } + + public void testLifecycle() throws Exception{ + super.testLifecycle(); + } + + protected void validatePostConditions() { + + // assertTrue(((Resource)applicationContext.getBean("customerFileLocator")).exists()); + + customers = new ArrayList() {{add(new Customer("customer1", (100000 - 98.34))); + add(new Customer("customer2", (100000 - 18.12 - 12.78))); + add(new Customer("customer3", (100000 - 109.25))); + add(new Customer("customer4", (100000 - 123.39)));}}; + + trades = new ArrayList() {{add(new Trade("UK21341EAH45", 978, new BigDecimal("98.34"), "customer1")); + add(new Trade("UK21341EAH46", 112, new BigDecimal("18.12"), "customer2")); + add(new Trade("UK21341EAH47", 245, new BigDecimal("12.78"), "customer2")); + add(new Trade("UK21341EAH48", 108, new BigDecimal("109.25"), "customer3")); + add(new Trade("UK21341EAH49", 854, new BigDecimal("123.39"), "customer4"));}}; + + // check content of the trade table + jdbcTemplate.query(GET_TRADES, new RowCallbackHandler() { + + public void processRow(ResultSet rs) throws SQLException { + Trade trade = (Trade)trades.get(activeRow++); + + assertTrue(trade.getIsin().equals(rs.getString(1))); + assertTrue(trade.getQuantity() == rs.getLong(2)); + assertTrue(trade.getPrice().equals(rs.getBigDecimal(3))); + assertTrue(trade.getCustomer().equals(rs.getString(4))); + } + }); + + assertTrue(trades.size() == activeRow); + + // check content of the customer table + activeRow = 0; + jdbcTemplate.query(GET_CUSTOMERS, new RowCallbackHandler() { + + public void processRow(ResultSet rs) throws SQLException { + Customer customer = (Customer)customers.get(activeRow++); + + assertTrue(customer.getName().equals(rs.getString(1))); + assertTrue(customer.getCredit() == rs.getDouble(2)); + } + }); + + assertTrue(customers.size() == activeRow); + + // check content of the output file + + +// Clean up + ((FileSystemResource)applicationContext.getBean("customerFileLocator")).getFile().delete(); + } + + protected void validatePreConditions() { + assertTrue(((Resource)applicationContext.getBean("fileLocator")).exists()); + } + + private class Customer { + private String name; + private double credit; + + public Customer(String name, double credit) { + this.name = name; + this.credit = credit; + } + + public Customer(){ + } + + /** + * @return the credit + */ + public double getCredit() { + return credit; + } + /** + * @param credit the credit to set + */ + public void setCredit(double credit) { + this.credit = credit; + } + /** + * @return the name + */ + public String getName() { + return name; + } + /** + * @param name the name to set + */ + public void setName(String name) { + this.name = name; + } + + /* (non-Javadoc) + * @see java.lang.Object#hashCode() + */ + public int hashCode() { + final int PRIME = 31; + int result = 1; + long temp; + temp = Double.doubleToLongBits(credit); + result = PRIME * result + (int) (temp ^ (temp >>> 32)); + result = PRIME * result + ((name == null) ? 0 : name.hashCode()); + return result; + } + + /* (non-Javadoc) + * @see java.lang.Object#equals(java.lang.Object) + */ + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + final Customer other = (Customer) obj; + if (Double.doubleToLongBits(credit) != Double.doubleToLongBits(other.credit)) + return false; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + return true; + } + + + } + + +} diff --git a/samples/src/test/java/org/springframework/batch/sample/item/processor/CustomerCreditIncreaseProcessorTests.java b/samples/src/test/java/org/springframework/batch/sample/item/processor/CustomerCreditIncreaseProcessorTests.java new file mode 100644 index 000000000..42f335876 --- /dev/null +++ b/samples/src/test/java/org/springframework/batch/sample/item/processor/CustomerCreditIncreaseProcessorTests.java @@ -0,0 +1,53 @@ +package org.springframework.batch.sample.item.processor; + +import java.math.BigDecimal; + +import org.easymock.MockControl; +import org.springframework.batch.io.OutputSource; +import org.springframework.batch.sample.domain.CustomerCredit; + +import junit.framework.TestCase; + +/** + * Tests for {@link CustomerCreditIncreaseProcessor}. + * + * @author Robert Kasanicky + */ +public class CustomerCreditIncreaseProcessorTests extends TestCase{ + + private CustomerCreditIncreaseProcessor processor = new CustomerCreditIncreaseProcessor(); + + private OutputSource outputSource; + private MockControl outputSourceControl = MockControl.createStrictControl(OutputSource.class); + + private CustomerCredit customerCredit = new CustomerCredit(); + + + protected void setUp() throws Exception { + customerCredit.setId(1); + customerCredit.setName("testCustomer"); + + outputSource = (OutputSource) outputSourceControl.getMock(); + processor.setOutputSource(outputSource); + } + + + /** + * Increases customer's credit by fixed value + */ + public void testProcess() throws Exception { + BigDecimal oldCredit = new BigDecimal(10.54); + customerCredit.setCredit(oldCredit); + + outputSource.write(customerCredit); + outputSourceControl.setVoidCallable(); + outputSourceControl.replay(); + + processor.process(customerCredit); + + BigDecimal newCredit = customerCredit.getCredit(); + BigDecimal expectedCredit = oldCredit.add(CustomerCreditIncreaseProcessor.FIXED_AMOUNT); + assertTrue(newCredit.compareTo(expectedCredit) == 0); + outputSourceControl.verify(); + } +}