BATCH-140: Modified file input sources to no longer implemented FieldSet and now accept an injected mapper.

This commit is contained in:
lucasward
2007-10-08 04:28:15 +00:00
parent 354702d6a5
commit 407d032bff
20 changed files with 208 additions and 381 deletions

View File

@@ -23,7 +23,6 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.io.InputSource;
import org.springframework.batch.io.file.FieldSet;
import org.springframework.batch.io.file.FieldSetInputSource;
import org.springframework.batch.io.file.FieldSetMapper;
import org.springframework.batch.item.ItemProvider;
import org.springframework.batch.item.provider.AbstractItemProvider;
@@ -32,32 +31,32 @@ import org.springframework.batch.item.provider.AbstractItemProvider;
* An {@link ItemProvider} that delivers a list as its item, storing up objects
* from the injected {@link InputSource} until they are ready to be packed out
* as a collection.<br/>
*
*
* This class is thread safe (it can be used concurrently by multiple threads) as
* long as the {@link InputSource} is also thread safe.
*
*
* @author Dave Syer
*
*
*/
public class CollectionItemProvider extends AbstractItemProvider {
private static final Log log = LogFactory
.getLog(CollectionItemProvider.class);
private FieldSetInputSource inputSource;
private InputSource inputSource;
// maps a single line to a simple record
private FieldSetMapper fieldSetMapper;
/**
* Get the next list of records.
*
*
* @see org.springframework.batch.item.ItemProvider#next()
*/
public Object next() {
ResultHolder holder = new ResultHolder();
while (process(inputSource.readFieldSet(), holder)) {
while (process((FieldSet)inputSource.read(), holder)) {
continue;
}
@@ -98,7 +97,7 @@ public class CollectionItemProvider extends AbstractItemProvider {
* Injection setter for {@link InputSource}.
* @param inputSource an {@link InputSource}.
*/
public void setInputSource(FieldSetInputSource inputSource) {
public void setInputSource(InputSource inputSource) {
this.inputSource = inputSource;
}
@@ -109,9 +108,9 @@ public class CollectionItemProvider extends AbstractItemProvider {
/**
* Private class for temporary state management while item is being
* collected.
*
*
* @author Dave Syer
*
*
*/
private class ResultHolder {
Collection records = new ArrayList();

View File

@@ -1,55 +0,0 @@
package org.springframework.batch.sample.item.provider;
import org.springframework.batch.io.Skippable;
import org.springframework.batch.io.file.FieldSetMapper;
import org.springframework.batch.io.file.support.DefaultFlatFileInputSource;
import org.springframework.batch.item.ItemProvider;
import org.springframework.batch.restart.RestartData;
import org.springframework.batch.restart.Restartable;
import org.springframework.batch.sample.domain.NflPlayer;
public class NflPlayerItemProvider implements ItemProvider, Restartable, Skippable{
DefaultFlatFileInputSource inputSource = null;
FieldSetMapper fieldSetMapper = null;
public void setFieldSetMapper(FieldSetMapper fieldSetMapper) {
this.fieldSetMapper = fieldSetMapper;
}
public void setInputSource(DefaultFlatFileInputSource inputSource) {
this.inputSource = inputSource;
}
public Object getKey(Object item) {
// TODO Auto-generated method stub
return null;
}
public Object next() throws Exception {
NflPlayer nflPlayer = (NflPlayer)fieldSetMapper.mapLine(inputSource.readFieldSet());
return nflPlayer;
}
public boolean recover(Object data, Throwable cause) {
// TODO Auto-generated method stub
return false;
}
public RestartData getRestartData() {
return inputSource.getRestartData();
}
public void restoreFrom(RestartData data) {
inputSource.restoreFrom(data);
}
public void skip() {
inputSource.skip();
}
}

View File

@@ -22,8 +22,8 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.core.configuration.StepConfiguration;
import org.springframework.batch.core.domain.StepExecution;
import org.springframework.batch.io.InputSource;
import org.springframework.batch.io.file.FieldSet;
import org.springframework.batch.io.file.FieldSetInputSource;
import org.springframework.batch.io.file.FieldSetMapper;
import org.springframework.batch.item.provider.AbstractItemProvider;
import org.springframework.batch.item.validator.Validator;
@@ -42,7 +42,7 @@ import org.springframework.batch.sample.domain.ShippingInfo;
*/
public class OrderItemProvider extends AbstractItemProvider {
private static Log log = LogFactory.getLog(OrderItemProvider.class);
private FieldSetInputSource inputSource;
private InputSource inputSource;
private Order order;
private boolean recordFinished;
private FieldSetMapper headerMapper;
@@ -60,7 +60,7 @@ public class OrderItemProvider extends AbstractItemProvider {
recordFinished = false;
while (!recordFinished) {
process(inputSource.readFieldSet());
process((FieldSet)inputSource.read());
}
if (order!=null) {
@@ -70,7 +70,7 @@ public class OrderItemProvider extends AbstractItemProvider {
Object result = order;
order = null;
return result;
}
@@ -88,7 +88,7 @@ public class OrderItemProvider extends AbstractItemProvider {
}
String lineId = fieldSet.readString(0);
//start a new Order
if (Order.LINE_ID_HEADER.equals(lineId)) {
log.debug("STARTING NEW RECORD");
@@ -119,7 +119,7 @@ public class OrderItemProvider extends AbstractItemProvider {
order.setCustomer((Customer) customerMapper.mapLine(fieldSet));
order.getCustomer().setBusinessCustomer(true);
}
return;
}
@@ -130,7 +130,7 @@ public class OrderItemProvider extends AbstractItemProvider {
order.setCustomer((Customer) customerMapper.mapLine(fieldSet));
order.getCustomer().setBusinessCustomer(false);
}
return;
}
@@ -166,10 +166,10 @@ public class OrderItemProvider extends AbstractItemProvider {
}
order.getLineItems().add(itemMapper.mapLine(fieldSet));
return;
}
log.debug("Could not map LINE_ID="+lineId);
}
@@ -190,8 +190,8 @@ public class OrderItemProvider extends AbstractItemProvider {
this.headerMapper = headerMapper;
}
public void setInputSource(FieldSetInputSource inputTemplate) {
this.inputSource = inputTemplate;
public void setInputSource(InputSource inputSource) {
this.inputSource = inputSource;
}
public void setItemMapper(FieldSetMapper itemMapper) {
@@ -205,5 +205,5 @@ public class OrderItemProvider extends AbstractItemProvider {
public void setValidator(Validator validator) {
this.validator = validator;
}
}

View File

@@ -1,66 +0,0 @@
/*
* 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.item.provider;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.io.exception.TransactionInvalidException;
import org.springframework.batch.io.file.FieldSetInputSource;
import org.springframework.batch.io.file.FieldSetMapper;
import org.springframework.batch.item.provider.AbstractItemProvider;
/**
* @author peter.zozom
*
*/
public class SkipSampleItemProvider extends AbstractItemProvider {
private static Log log = LogFactory.getLog(SkipSampleItemProvider.class);
private int counter = 0;
private int exceptionOnrecordNumber = 14;
private FieldSetInputSource inputSource;
private FieldSetMapper fieldSetMapper;
public Object next() {
counter++;
if (counter == exceptionOnrecordNumber) {
// this causes rollback of current transaction
log.debug("Throwing TransactionInvalidException to cause transaction rollback...");
throw new TransactionInvalidException("Error processing line: " + counter + ". Rollbacking...");
}
return fieldSetMapper.mapLine(inputSource.readFieldSet());
}
public void setInputSource(FieldSetInputSource inputTemplate) {
this.inputSource = inputTemplate;
}
public void setFieldSetMapper(FieldSetMapper fieldSetMapper) {
this.fieldSetMapper = fieldSetMapper;
}
public void setThrowExceptionOnRecordNumber(int exceptionOnrecordNumber) {
this.exceptionOnrecordNumber = exceptionOnrecordNumber;
}
}

View File

@@ -0,0 +1,38 @@
/*
* 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.mapping;
import org.springframework.batch.io.file.FieldSet;
import org.springframework.batch.io.file.FieldSetMapper;
/**
* Pass through {@link FieldSetMapper} useful for
* passing a fieldset back from a FlatFileInputsource rather
* than a mapped object.
*
* @author Lucas Ward
*
*/
public class PassThroughFieldSetMapper implements FieldSetMapper {
/* (non-Javadoc)
* @see org.springframework.batch.io.file.FieldSetMapper#mapLine(org.springframework.batch.io.file.FieldSet)
*/
public Object mapLine(FieldSet fs) {
return fs;
}
}

View File

@@ -34,35 +34,26 @@ import org.springframework.batch.statistics.StatisticsProvider;
* reading and processing logic need not to be reused in different contexts. In
* general it is recommended to separate these two concerns using an
* {@link ItemProviderProcessTasklet}.
*
*
* Note this class is thread-safe, as per the 'standard' module implementations
* provided by the framework.
*
*
* @author Robert Kasanicky
* @author Lucas Ward
* @author Dave Syer
*/
public class SimpleTradeTasklet implements Tasklet, StatisticsProvider {
/**
/*
* reads the data from input file
*/
private DefaultFlatFileInputSource inputSource;
/**
* maps a line to a Trade object
*/
private FieldSetMapper tradeFieldSetMapper = new TradeFieldSetMapper();
/**
/*
* writes a Trade object to output
*/
private TradeWriter tradeWriter;
/**
* domain object being processed
*/
private Trade trade;
/**
* number of trade objects processed
*/
@@ -75,7 +66,7 @@ public class SimpleTradeTasklet implements Tasklet, StatisticsProvider {
* written out without any processing.
*/
public ExitStatus execute() throws Exception {
trade = (Trade) tradeFieldSetMapper.mapLine(inputSource.readFieldSet());
Trade trade = (Trade)inputSource.read();
if (trade == null) {
// no Trade object returned, reading input is finished
@@ -87,30 +78,6 @@ public class SimpleTradeTasklet implements Tasklet, StatisticsProvider {
return ExitStatus.CONTINUABLE;
}
/**
* Inner class which implements the FieldSetMapper interface. It contains
* one method, mapLine, which accepts a FieldSet as a parameter. This method
* will be called by the inputSource when it is passed in.
*
*/
private static class TradeFieldSetMapper implements FieldSetMapper {
public Object mapLine(FieldSet fieldSet) {
if (fieldSet == null) {
return null;
}
Trade trade = new Trade();
trade.setIsin(fieldSet.readString("ISIN"));
trade.setQuantity(fieldSet.readLong(1));
trade.setPrice(fieldSet.readBigDecimal(2));
trade.setCustomer(fieldSet.readString(3));
return trade;
}
}
public void setInputSource(DefaultFlatFileInputSource inputTemplate) {
this.inputSource = inputTemplate;
}

View File

@@ -20,9 +20,8 @@
class="org.springframework.batch.execution.tasklet.RestartableItemProviderTasklet">
<property name="itemProvider">
<bean
class="org.springframework.batch.item.provider.FlatFileItemProvider">
<property name="source" ref="fileInputTemplate" />
<property name="mapper" ref="fieldSetMapper" />
class="org.springframework.batch.item.provider.ValidatingItemProvider">
<property name="inputSource" ref="fileInputTemplate" />
<property name="validator" ref="fixedValidator" />
</bean>
</property>
@@ -40,9 +39,8 @@
class="org.springframework.batch.execution.tasklet.RestartableItemProviderTasklet">
<property name="itemProvider">
<bean
class="org.springframework.batch.item.provider.FlatFileItemProvider">
<property name="source" ref="fileInputTemplate2" />
<property name="mapper" ref="fieldSetMapper2" />
class="org.springframework.batch.item.provider.InputSourceItemProvider">
<property name="inputSource" ref="fileInputTemplate2" />
</bean>
</property>
<property name="itemProcessor">
@@ -63,6 +61,7 @@
<aop:scoped-proxy />
<property name="resource" ref="fileLocator" />
<property name="tokenizer" ref="fixedFileDescriptor" />
<property name="fieldSetMapper" ref="fieldSetMapper" />
</bean>
<bean id="fileInputTemplate2" class="org.springframework.batch.io.file.support.DefaultFlatFileInputSource"
@@ -70,6 +69,7 @@
<aop:scoped-proxy />
<property name="resource" ref="fileLocator2" />
<property name="tokenizer" ref="fixedFileDescriptor2" />
<property name="fieldSetMapper" ref="fieldSetMapper2" />
<!-- <property name="validator" ref="fixedValidator" />-->
</bean>

View File

@@ -14,14 +14,13 @@
<bean id="jobConfiguration" parent="simpleJob">
<property name="steps">
<bean id="step1" parent="simpleStep">
<constructor-arg>
<constructor-arg>
<bean
class="org.springframework.batch.execution.tasklet.RestartableItemProviderTasklet">
<property name="itemProvider">
<bean
class="org.springframework.batch.item.provider.FlatFileItemProvider">
<property name="source" ref="fileInputTemplate" />
<property name="mapper" ref="fieldSetMapper" />
class="org.springframework.batch.item.provider.ValidatingItemProvider">
<property name="inputSource" ref="fileInputTemplate" />
<property name="validator" ref="fixedValidator" />
</bean>
</property>
@@ -32,7 +31,7 @@
<bean class="org.springframework.batch.sample.item.processor.TradeProcessor">
<property name="writer" ref="tradeDao" />
</bean>
<bean class="org.springframework.batch.item.processor.OutputSourceItemProcessor">
<property name="outputSource" ref="flatFileOutputSource" />
</bean>
@@ -53,6 +52,7 @@
<aop:scoped-proxy />
<property name="resource" ref="fileLocator" />
<property name="tokenizer" ref="fixedFileDescriptor" />
<property name="fieldSetMapper" ref="fieldSetMapper" />
</bean>
<bean id="fixedFileDescriptor" class="org.springframework.batch.io.file.support.transform.FixedLengthTokenizer">
@@ -83,11 +83,11 @@
</property>
</bean>
<bean class="org.springframework.batch.io.file.support.FlatFileOutputSource" id="flatFileOutputSource">
<property name="resource" ref="customerFileLocator" />
</bean>
<bean id="customerFileLocator" class="org.springframework.core.io.FileSystemResource">
<constructor-arg type="java.lang.String" value="20070122.testStream.ParallelCustomerReportStep.TEMP.txt" />
</bean>
@@ -103,5 +103,5 @@
<!-- register the step scope with the application context -->
<bean class="org.springframework.batch.execution.scope.StepScope" />
</beans>

View File

@@ -19,9 +19,8 @@
class="org.springframework.batch.execution.tasklet.RestartableItemProviderTasklet">
<property name="itemProvider">
<bean
class="org.springframework.batch.item.provider.FlatFileItemProvider">
<property name="source" ref="fileInputTemplate" />
<property name="mapper" ref="fieldSetMapper" />
class="org.springframework.batch.item.provider.ValidatingItemProvider">
<property name="inputSource" ref="fileInputTemplate" />
<property name="validator" ref="fixedValidator" />
</bean>
</property>
@@ -42,6 +41,7 @@
abstract="true">
<property name="resource" ref="fileLocator" />
<property name="tokenizer" ref="fixedFileDescriptor" />
<property name="fieldSetMapper" ref="fieldSetMapper" />
</bean>
<!-- TODO: refactor this to use aop:scoped-proxy when SPR-3804 is fixed -->
@@ -51,7 +51,7 @@
<property name="targetBeanName" value="scopedInputTemplate"/>
</bean>
<bean id="testInputTemplate" parent="abstractInputTemplate"/>
<bean id="testInputTemplate" parent="abstractInputTemplate" />
<bean id="fixedFileDescriptor" class="org.springframework.batch.io.file.support.transform.FixedLengthTokenizer">
<property name="names" value="ISIN, Quantity, Price, Customer" />

View File

@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
@@ -18,7 +18,7 @@
class="org.springframework.batch.execution.tasklet.RestartableItemProviderTasklet">
<property name="itemProvider">
<bean class="org.springframework.batch.sample.item.provider.CollectionItemProvider">
<property name="inputSource" ref="fileInputTemplate" />
<property name="inputSource" ref="fileInputSource" />
<property name="fieldSetMapper" ref="tradeLineMapper" />
</bean>
</property>
@@ -39,11 +39,14 @@
</property>
</bean>
<bean id="fileInputTemplate" class="org.springframework.batch.io.file.support.DefaultFlatFileInputSource"
<bean id="fileInputSource" class="org.springframework.batch.io.file.support.DefaultFlatFileInputSource"
scope="step">
<aop:scoped-proxy />
<property name="resource" value="classpath:data/multilineJob/input/20070122.teststream.multilineStep.txt" />
<property name="tokenizer" ref="fixedFileDescriptor" />
<property name="fieldSetMapper">
<bean class="org.springframework.batch.sample.mapping.PassThroughFieldSetMapper" />
</property>
<!-- <property name="validator" ref="fixedValidator" /> -->
</bean>
@@ -68,18 +71,18 @@
<bean id="endRecordDescriptor"
class="org.springframework.batch.io.file.support.transform.FixedLengthTokenizer">
<property name="lengths" value="3" />
</bean>
</bean>
<bean id="tradeRecordDescriptor"
class="org.springframework.batch.io.file.support.transform.FixedLengthTokenizer">
<property name="names" value="ISIN,Quantity,Price,Customer" />
<property name="lengths" value="12,3,5,9" />
</bean>
<bean parent="customEditorConfigurer"/>
<!-- register the step scope with the application context -->
<bean class="org.springframework.batch.execution.scope.StepScope" />
</beans>

View File

@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
@@ -13,6 +13,9 @@
<aop:scoped-proxy />
<property name="resource" ref="fileInputLocator" />
<property name="tokenizer" ref="orderFileDescriptor" />
<property name="fieldSetMapper">
<bean class="org.springframework.batch.sample.mapping.PassThroughFieldSetMapper" />
</property>
</bean>
<bean id="flatFileOutputSource" class="org.springframework.batch.io.file.support.FlatFileOutputSource"
@@ -24,5 +27,5 @@
<bean id="delimitedLineAggregator" class="org.springframework.batch.io.file.support.transform.DelimitedLineTokenizer" />
<bean id="fixedLineAggregator" class="org.springframework.batch.io.file.support.transform.FixedLengthLineAggregator" />
</beans>

View File

@@ -18,9 +18,8 @@
<bean class="org.springframework.batch.sample.tasklet.ExceptionRestartableTasklet">
<property name="itemProvider">
<bean
class="org.springframework.batch.item.provider.FlatFileItemProvider">
<property name="source" ref="fileInputTemplate" />
<property name="mapper" ref="fieldSetMapper" />
class="org.springframework.batch.item.provider.ValidatingItemProvider">
<property name="inputSource" ref="fileInputSource" />
<property name="validator" ref="fixedValidator" />
</bean>
</property>
@@ -41,11 +40,12 @@
<!-- INFRASTRUCTURE SETUP -->
<bean id="fileInputTemplate" class="org.springframework.batch.io.file.support.DefaultFlatFileInputSource"
<bean id="fileInputSource" class="org.springframework.batch.io.file.support.DefaultFlatFileInputSource"
scope="step" >
<aop:scoped-proxy />
<property name="resource" ref="fileLocator" />
<property name="tokenizer" ref="fixedFileDescriptor" />
<property name="fieldSetMapper" ref="fieldSetMapper" />
</bean>
<bean id="fixedFileDescriptor" class="org.springframework.batch.io.file.support.transform.FixedLengthTokenizer">

View File

@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
@@ -10,7 +10,7 @@
<property name="jobConfigurationRegistry" ref="jobConfigurationRegistry"/>
</bean>
<bean id="jobConfiguration" parent="simpleJob">
<bean id="simpleTrade" parent="simpleJob">
<property name="steps">
<list>
<bean id="step1" parent="simpleStep">
@@ -38,13 +38,8 @@
<bean id="abstractInputTemplate" class="org.springframework.batch.io.file.support.DefaultFlatFileInputSource"
abstract="true">
<property name="resource" ref="fileLocator" />
<property name="tokenizer" ref="fixedFileDescriptor" />
</bean>
<bean id="fileLocator" class="org.springframework.core.io.ClassPathResource">
<constructor-arg>
<value>/data/simpleTaskletJob/input/20070122.teststream.ImportTradeDataStep.txt</value>
</constructor-arg>
<property name="tokenizer" ref="tradeTokenizer" />
<property name="fieldSetMapper" ref="tradeMapper" />
</bean>
<!-- TODO: refactor this to use aop:scoped-proxy when SPR-3804 is fixed -->
@@ -54,26 +49,33 @@
<property name="targetBeanName" value="scopedInputTemplate"/>
</bean>
<bean id="testInputTemplate" parent="abstractInputTemplate"/>
<bean id="testInputTemplate" parent="abstractInputTemplate" />
<bean id="fixedFileDescriptor" class="org.springframework.batch.io.file.support.transform.FixedLengthTokenizer">
<bean id="tradeMapper" class="org.springframework.batch.sample.mapping.TradeFieldSetMapper" />
<bean id="fileLocator" class="org.springframework.core.io.ClassPathResource">
<constructor-arg type="java.lang.String"
value="/data/simpleTaskletJob/input/20070122.teststream.ImportTradeDataStep.txt" />
</bean>
<bean id="tradeTokenizer" class="org.springframework.batch.io.file.support.transform.FixedLengthTokenizer">
<property name="names" value="ISIN, Quantity, Price, Customer" />
<property name="lengths" value="12, 3, 5, 9" />
</bean>
<bean id="tradeLogAdvice"
class="org.springframework.batch.sample.advice.TradeWriterLogAdvice" />
<bean id="tradeLogAdvice"
class="org.springframework.batch.sample.advice.TradeWriterLogAdvice" />
<aop:config>
<aop:aspect id="tradeWriterLogging" ref="tradeLogAdvice">
<aop:after
pointcut="execution( * org.springframework.batch.sample.dao.TradeWriter+.writeTrade(org.springframework.batch.sample.domain.Trade)) and args(trade)"
method="doBasicLogging" />
</aop:aspect>
</aop:config>
<bean parent="customEditorConfigurer"/>
<!-- register the step scope with the application context -->

View File

@@ -3,7 +3,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
@@ -27,13 +27,9 @@
class="org.springframework.batch.execution.tasklet.RestartableItemProviderTasklet">
<property name="itemProvider">
<bean
class="org.springframework.batch.item.provider.FlatFileItemProvider">
<property name="source"
class="org.springframework.batch.item.provider.ValidatingItemProvider">
<property name="inputSource"
ref="fileInputTemplate" />
<property name="mapper">
<bean
class="org.springframework.batch.sample.mapping.TradeFieldSetMapper" />
</property>
<property name="validator"
ref="tradeValidator" />
</bean>
@@ -127,6 +123,6 @@
<!-- register the step scope with the application context -->
<bean class="org.springframework.batch.execution.scope.StepScope" />
</beans>

View File

@@ -32,6 +32,9 @@
<aop:scoped-proxy />
<property name="resource" ref="fileLocator" />
<property name="tokenizer" ref="tradeFileDescriptor" />
<property name="fieldSetMapper">
<bean class="org.springframework.batch.sample.mapping.TradeFieldSetMapper" />
</property>
</bean>
<bean id="tradeFileDescriptor"

View File

@@ -21,23 +21,25 @@ import java.io.FileReader;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.batch.io.InputSource;
import org.springframework.batch.io.file.FieldSet;
import org.springframework.batch.io.file.FieldSetInputSource;
import org.springframework.batch.io.file.support.DefaultFlatFileInputSource;
import org.springframework.batch.item.ResourceLifecycle;
import org.springframework.batch.sample.domain.Trade;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.RowCallbackHandler;
public class FixedLengthImportJobFunctionalTests extends AbstractLifecycleSpringContextTests {
//expected line length in input file (sum of pattern lengths + 2, because the counter is appended twice)
private static final int LINE_LENGTH = 29;
//auto-injected attributes
private JdbcOperations jdbcTemplate;
private Resource fileLocator;
private FieldSetInputSource inputSource;
private DefaultFlatFileInputSource inputSource;
protected void onSetUp() throws Exception {
super.onSetUp();
jdbcTemplate.update("delete from TRADE");
@@ -51,21 +53,21 @@ public class FixedLengthImportJobFunctionalTests extends AbstractLifecycleSpring
* check that records have been correctly written to database
*/
protected void validatePostConditions() {
((ResourceLifecycle) inputSource).open();
inputSource.open();
jdbcTemplate.query("SELECT ID, ISIN, QUANTITY, PRICE, CUSTOMER FROM trade ORDER BY id", new RowCallbackHandler() {
public void processRow(ResultSet rs) throws SQLException {
FieldSet fieldSet = inputSource.readFieldSet();
assertEquals(fieldSet.readString(0), rs.getString(2));
assertEquals(fieldSet.readLong(1),rs.getLong(3));
assertEquals(fieldSet.readBigDecimal(2), rs.getBigDecimal(4));
assertEquals(fieldSet.readString(3), rs.getString(5));
Trade trade = (Trade)inputSource.read();
assertEquals(trade.getIsin(), rs.getString(2));
assertEquals(trade.getQuantity(),rs.getLong(3));
assertEquals(trade.getPrice(), rs.getBigDecimal(4));
assertEquals(trade.getCustomer(), rs.getString(5));
}
});
assertNull(inputSource.read());
}
@@ -89,8 +91,8 @@ public class FixedLengthImportJobFunctionalTests extends AbstractLifecycleSpring
public void setFileLocator(Resource fileLocator) {
this.fileLocator = fileLocator;
}
public void setFieldSetInputSource(FieldSetInputSource inputSource){
public void setDefaultFlatFileInputSource(DefaultFlatFileInputSource inputSource){
this.inputSource = inputSource;
}
}

View File

@@ -6,63 +6,63 @@ import java.util.Iterator;
import junit.framework.TestCase;
import org.easymock.MockControl;
import org.springframework.batch.io.InputSource;
import org.springframework.batch.io.file.FieldSet;
import org.springframework.batch.io.file.FieldSetInputSource;
import org.springframework.batch.io.file.FieldSetMapper;
import org.springframework.batch.sample.item.provider.CollectionItemProvider;
public class CollectionItemProviderTests extends TestCase {
private MockControl inputControl;
private FieldSetInputSource input;
private InputSource input;
private FieldSetMapper mapper;
private CollectionItemProvider provider;
public void setUp() {
//create mock for input
inputControl = MockControl.createControl(FieldSetInputSource.class);
input = (FieldSetInputSource) inputControl.getMock();
inputControl = MockControl.createControl(InputSource.class);
input = (InputSource) inputControl.getMock();
//create mock for mapper
mapper = new FieldSetMapper() {
public Object mapLine(FieldSet fs) { return fs.readString(0); }
};
//create provider
provider = new CollectionItemProvider();
provider.setInputSource(input);
provider.setFieldSetMapper(mapper);
}
public void testNext() {
//set-up mock input
input.readFieldSet();
input.read();
inputControl.setReturnValue(new FieldSet(new String[] {"BEGIN"}));
input.readFieldSet();
input.read();
inputControl.setReturnValue(new FieldSet(new String[] {"line"}),3);
input.readFieldSet();
input.read();
inputControl.setReturnValue(new FieldSet(new String[] {"END"}));
input.readFieldSet();
input.read();
inputControl.setReturnValue(null);
inputControl.replay();
//read object
Object result = provider.next();
//it should be collection of 3 strings "line"
assertTrue(result instanceof Collection);
Collection lines = (Collection)result;
assertEquals(3, lines.size());
for (Iterator i = lines.iterator(); i.hasNext();) {
assertEquals("line", i.next());
}
//read object again - it should return null
assertNull(provider.next());
//verify method calls
inputControl.verify();
}

View File

@@ -5,8 +5,8 @@ import java.util.Iterator;
import junit.framework.TestCase;
import org.easymock.MockControl;
import org.springframework.batch.io.InputSource;
import org.springframework.batch.io.file.FieldSet;
import org.springframework.batch.io.file.FieldSetInputSource;
import org.springframework.batch.io.file.FieldSetMapper;
import org.springframework.batch.item.validator.Validator;
import org.springframework.batch.sample.domain.Address;
@@ -21,29 +21,29 @@ public class OrderItemProviderTests extends TestCase {
private OrderItemProvider provider;
private MockControl inputControl;
private FieldSetInputSource input;
private InputSource input;
private MockControl mapperControl;
private FieldSetMapper mapper;
private MockControl validatorControl;
private Validator validator;
public void setUp() {
inputControl = MockControl.createControl(FieldSetInputSource.class);
input = (FieldSetInputSource)inputControl.getMock();
inputControl = MockControl.createControl(InputSource.class);
input = (InputSource)inputControl.getMock();
provider = new OrderItemProvider();
provider.setInputSource(input);
}
/*
* OrderItemProvider is resposible for retrieving validated value object from input source.
* OrderItemProvider.next():
* - reads lines from the input source - returned as fieldsets
* - reads lines from the input source - returned as fieldsets
* - pass fieldsets to the mapper - mapper will create value object
* - pass value object to validator
* - returns validated object
*
*
* In testNext method we are going to test these responsibilities. So we need create mock
* objects for input source, mapper and validator.
*/
@@ -60,26 +60,26 @@ public class OrderItemProviderTests extends TestCase {
FieldSet footerFS = new FieldSet(new String[] {Order.LINE_ID_FOOTER, "100","3","3"},
new String[] {"ID","TOTAL_PRICE","TOTAL_LINE_ITEMS","TOTAL_ITEMS"});
input.readFieldSet();
input.read();
inputControl.setReturnValue(headerFS);
input.readFieldSet();
input.read();
inputControl.setReturnValue(customerFS);
input.readFieldSet();
input.read();
inputControl.setReturnValue(billingFS);
input.readFieldSet();
input.read();
inputControl.setReturnValue(shippingFS);
input.readFieldSet();
input.read();
inputControl.setReturnValue(billingInfoFS);
input.readFieldSet();
input.read();
inputControl.setReturnValue(shippingInfoFS);
input.readFieldSet();
input.read();
inputControl.setReturnValue(itemFS,3);
input.readFieldSet();
input.read();
inputControl.setReturnValue(footerFS);
input.readFieldSet();
input.read();
inputControl.setReturnValue(null);
inputControl.replay();
//create value objects
Order order = new Order();
Customer customer = new Customer();
@@ -88,11 +88,11 @@ public class OrderItemProviderTests extends TestCase {
BillingInfo billingInfo = new BillingInfo();
ShippingInfo shippingInfo = new ShippingInfo();
LineItem item = new LineItem();
//create mock mapper
mapperControl = MockControl.createControl(FieldSetMapper.class);
mapper = (FieldSetMapper)mapperControl.getMock();
//set how mapper should respond - set return values for mapper
//set how mapper should respond - set return values for mapper
mapper.mapLine(headerFS);
mapperControl.setReturnValue(order);
mapper.mapLine(customerFS);
@@ -108,7 +108,7 @@ public class OrderItemProviderTests extends TestCase {
mapper.mapLine(itemFS);
mapperControl.setReturnValue(item,3);
mapperControl.replay();
//create mock validator
validatorControl = MockControl.createControl(Validator.class);
validator = (Validator)validatorControl.getMock();
@@ -125,15 +125,15 @@ public class OrderItemProviderTests extends TestCase {
provider.setHeaderMapper(mapper);
provider.setItemMapper(mapper);
provider.setShippingMapper(mapper);
//call tested method
Object result = provider.next();
//verify result
assertNotNull(result);
//result should be Order
assertTrue(result instanceof Order);
//verify whether order is constructed correctly
//Order object should contain same instances as returned by mapper
Order o = (Order) result;
@@ -150,14 +150,14 @@ public class OrderItemProviderTests extends TestCase {
for (Iterator i = o.getLineItems().iterator(); i.hasNext();) {
assertEquals(i.next(),item);
}
//try to retrieve next object - nothing should be returned
assertNull(provider.next());
//verify method calls on input source, mapper and validator
inputControl.verify();
mapperControl.verify();
validatorControl.verify();
}
}

View File

@@ -1,64 +0,0 @@
package org.springframework.batch.sample.item.provider;
import junit.framework.TestCase;
import org.easymock.MockControl;
import org.springframework.batch.io.exception.TransactionInvalidException;
import org.springframework.batch.io.file.FieldSetInputSource;
import org.springframework.batch.io.file.FieldSetMapper;
import org.springframework.batch.sample.item.provider.SkipSampleItemProvider;
public class SkipSampleItemProviderTests extends TestCase {
private MockControl inputControl;
private FieldSetInputSource input;
private MockControl mapperControl;
private FieldSetMapper mapper;
private SkipSampleItemProvider provider;
private static final int ITER_COUNT = 7;
public void setUp() {
inputControl = MockControl.createControl(FieldSetInputSource.class);
input = (FieldSetInputSource)inputControl.getMock();
mapperControl = MockControl.createControl(FieldSetMapper.class);
mapper = (FieldSetMapper)mapperControl.getMock();
provider = new SkipSampleItemProvider();
provider.setInputSource(input);
provider.setFieldSetMapper(mapper);
}
public void testNext() {
//set-up mock input
input.readFieldSet();
inputControl.setReturnValue(null,ITER_COUNT);
inputControl.replay();
//set-up mock mapper
mapper.mapLine(null);
mapperControl.setReturnValue("line",ITER_COUNT);
mapperControl.replay();
//set exception iteration count
provider.setThrowExceptionOnRecordNumber(ITER_COUNT + 1);
//call next() method multiple times and verify whether exception is thrown when expected
for (int i = 0; i <= ITER_COUNT; i++) {
try {
assertEquals("line", provider.next());
assertTrue(i < ITER_COUNT);
} catch (TransactionInvalidException tie) {
assertEquals(ITER_COUNT,i);
}
}
//verify method calls
inputControl.verify();
mapperControl.verify();
}
}

View File

@@ -16,25 +16,24 @@ public class SimpleTradeTaskletTests extends TestCase {
private boolean writerCalled = false;
public void testReadAndProcess() throws Exception {
//create input
DefaultFlatFileInputSource input = new DefaultFlatFileInputSource() {
private boolean done = false;
public FieldSet readFieldSet() {
public Object read() {
if (!done) {
FieldSet fs = new FieldSet(new String[] {"1234","5","100","testName"},
new String[] {"ISIN", "quantity", "price", "customer"});
Trade trade = new Trade("1234", 5, new BigDecimal(100), "testName");
inputCalled = true;
done = true;
return fs;
return trade;
} else {
return null;
}
}
};
//create writer
TradeWriter writer = new TradeWriter() {
public void writeTrade(Trade trade) {
@@ -46,22 +45,22 @@ public class SimpleTradeTaskletTests extends TestCase {
}
public void write(Object output) {}
public void close() {}
public void open() {}
public void open() {}
};
//create module
SimpleTradeTasklet module = new SimpleTradeTasklet();
module.setInputSource(input);
module.setTradeDao(writer);
//call tested methods
//read method should return true, because input returned fieldset
//read method should return true, because input returned fieldset
assertTrue(module.execute().isContinuable());
//verify whether input and writer were called
assertTrue(inputCalled);
assertTrue(writerCalled);
//read should return false, because input returned null
assertFalse(module.execute().isContinuable());
}