IN PROGRESS - issue BATCH-145: Move CollectionItemProvider to infrastructure from samples
http://opensource.atlassian.com/projects/spring/browse/BATCH-145
This commit is contained in:
@@ -1 +1 @@
|
||||
<?xml version='1.0' encoding='UTF-8'?><trades><trade><isin>XYZ0001</isin><quantity>5</quantity><price>11.39</price><customer>Customer1</customer></trade><trade><isin>XYZ0002</isin><quantity>2</quantity><price>72.99</price><customer>Customer2c</customer></trade><trade><isin>XYZ0003</isin><quantity>9</quantity><price>99.99</price><customer>Customer3</customer></trade><trade><isin>XYZ0001</isin><quantity>5</quantity><price>11.39</price><customer>Customer1</customer></trade><trade><isin>XYZ0002</isin><quantity>2</quantity><price>72.99</price><customer>Customer2c</customer></trade><trade><isin>XYZ0003</isin><quantity>9</quantity><price>99.99</price><customer>Customer3</customer></trade><trade><isin>XYZ0001</isin><quantity>5</quantity><price>11.39</price><customer>Customer1</customer></trade><trade><isin>XYZ0002</isin><quantity>2</quantity><price>72.99</price><customer>Customer2c</customer></trade><trade><isin>XYZ0003</isin><quantity>9</quantity><price>99.99</price><customer>Customer3</customer></trade></trades>
|
||||
<?xml version="1.0" encoding="UTF-8"?><trades><trade><isin>XYZ0001</isin><quantity>5</quantity><price>11.39</price><customer>Customer1</customer></trade><trade><isin>XYZ0002</isin><quantity>2</quantity><price>72.99</price><customer>Customer2c</customer></trade><trade><isin>XYZ0003</isin><quantity>9</quantity><price>99.99</price><customer>Customer3</customer></trade><trade><isin>XYZ0001</isin><quantity>5</quantity><price>11.39</price><customer>Customer1</customer></trade><trade><isin>XYZ0002</isin><quantity>2</quantity><price>72.99</price><customer>Customer2c</customer></trade><trade><isin>XYZ0003</isin><quantity>9</quantity><price>99.99</price><customer>Customer3</customer></trade><trade><isin>XYZ0001</isin><quantity>5</quantity><price>11.39</price><customer>Customer1</customer></trade><trade><isin>XYZ0002</isin><quantity>2</quantity><price>72.99</price><customer>Customer2c</customer></trade><trade><isin>XYZ0003</isin><quantity>9</quantity><price>99.99</price><customer>Customer3</customer></trade></trades>
|
||||
@@ -22,7 +22,6 @@ import java.util.Collection;
|
||||
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.FieldSetMapper;
|
||||
import org.springframework.batch.item.ItemProvider;
|
||||
import org.springframework.batch.item.provider.AbstractItemProvider;
|
||||
@@ -45,9 +44,6 @@ public class CollectionItemProvider extends AbstractItemProvider {
|
||||
|
||||
private InputSource inputSource;
|
||||
|
||||
// maps a single line to a simple record
|
||||
private FieldSetMapper fieldSetMapper;
|
||||
|
||||
/**
|
||||
* Get the next list of records.
|
||||
*
|
||||
@@ -56,7 +52,7 @@ public class CollectionItemProvider extends AbstractItemProvider {
|
||||
public Object next() {
|
||||
ResultHolder holder = new ResultHolder();
|
||||
|
||||
while (process((FieldSet)inputSource.read(), holder)) {
|
||||
while (process(inputSource.read(), holder)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -67,29 +63,29 @@ public class CollectionItemProvider extends AbstractItemProvider {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean process(FieldSet fieldSet, ResultHolder holder) {
|
||||
private boolean process(Object value, ResultHolder holder) {
|
||||
// finish processing if we hit the end of file
|
||||
if (fieldSet == null) {
|
||||
if (value == null) {
|
||||
log.debug("Exhausted InputSource");
|
||||
holder.exhausted = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
// start a new collection
|
||||
if (fieldSet.readString(0).equals("BEGIN")) {
|
||||
if (value == FieldSetMapper.BEGIN_RECORD) {
|
||||
log.debug("Start of new record detected");
|
||||
return true;
|
||||
}
|
||||
|
||||
// mark we are finished with current collection
|
||||
if (fieldSet.readString(0).equals("END")) {
|
||||
if (value == FieldSetMapper.END_RECORD) {
|
||||
log.debug("End of record detected");
|
||||
return false;
|
||||
}
|
||||
|
||||
// add a simple record to the current collection
|
||||
log.debug("Mapping: " + fieldSet);
|
||||
holder.records.add(fieldSetMapper.mapLine(fieldSet));
|
||||
log.debug("Mapping: " + value);
|
||||
holder.records.add(value);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -101,10 +97,6 @@ public class CollectionItemProvider extends AbstractItemProvider {
|
||||
this.inputSource = inputSource;
|
||||
}
|
||||
|
||||
public void setFieldSetMapper(FieldSetMapper mapper) {
|
||||
this.fieldSetMapper = mapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* Private class for temporary state management while item is being
|
||||
* collected.
|
||||
|
||||
@@ -31,6 +31,14 @@ public class TradeFieldSetMapper implements FieldSetMapper {
|
||||
|
||||
public Object mapLine(FieldSet fieldSet) {
|
||||
|
||||
if ("BEGIN".equals(fieldSet.readString(0))) {
|
||||
return FieldSetMapper.BEGIN_RECORD;
|
||||
}
|
||||
|
||||
if ("END".equals(fieldSet.readString(0))) {
|
||||
return FieldSetMapper.END_RECORD;
|
||||
}
|
||||
|
||||
Trade trade = new Trade();
|
||||
trade.setIsin(fieldSet.readString(0));
|
||||
trade.setQuantity(fieldSet.readLong(1));
|
||||
|
||||
@@ -1,13 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
<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
|
||||
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">
|
||||
|
||||
<bean class="org.springframework.batch.execution.configuration.JobConfigurationRegistryBeanPostProcessor">
|
||||
<property name="jobConfigurationRegistry" ref="jobConfigurationRegistry"/>
|
||||
<bean
|
||||
class="org.springframework.batch.execution.configuration.JobConfigurationRegistryBeanPostProcessor">
|
||||
<property name="jobConfigurationRegistry"
|
||||
ref="jobConfigurationRegistry" />
|
||||
</bean>
|
||||
|
||||
<bean id="jobConfiguration" parent="simpleJob">
|
||||
@@ -17,18 +20,22 @@
|
||||
<bean
|
||||
class="org.springframework.batch.execution.tasklet.RestartableItemProviderTasklet">
|
||||
<property name="itemProvider">
|
||||
<bean class="org.springframework.batch.sample.item.provider.CollectionItemProvider">
|
||||
<property name="inputSource" ref="fileInputSource" />
|
||||
<property name="fieldSetMapper" ref="tradeLineMapper" />
|
||||
<bean
|
||||
class="org.springframework.batch.sample.item.provider.CollectionItemProvider">
|
||||
<property name="inputSource"
|
||||
ref="fileInputSource" />
|
||||
</bean>
|
||||
</property>
|
||||
<property name="itemProcessor">
|
||||
<bean class="org.springframework.batch.sample.item.processor.DefaultFlatFileProcessor">
|
||||
<bean
|
||||
class="org.springframework.batch.sample.item.processor.DefaultFlatFileProcessor">
|
||||
<property name="flatFileOutputSource">
|
||||
<bean class="org.springframework.batch.io.file.support.FlatFileOutputSource"
|
||||
scope="step" >
|
||||
<bean
|
||||
class="org.springframework.batch.io.file.support.FlatFileOutputSource"
|
||||
scope="step">
|
||||
<aop:scoped-proxy />
|
||||
<property name="resource" value="file:20070122.testStream.multilineStep.txt" />
|
||||
<property name="resource"
|
||||
value="file:20070122.testStream.multilineStep.txt" />
|
||||
</bean>
|
||||
</property>
|
||||
</bean>
|
||||
@@ -39,18 +46,19 @@
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="fileInputSource" 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="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="fieldSetMapper" ref="tradeLineMapper" />
|
||||
<!-- <property name="validator" ref="fixedValidator" /> -->
|
||||
</bean>
|
||||
|
||||
<bean id="tradeLineMapper" class="org.springframework.batch.sample.mapping.TradeFieldSetMapper" />
|
||||
<bean id="tradeLineMapper"
|
||||
class="org.springframework.batch.sample.mapping.TradeFieldSetMapper" />
|
||||
|
||||
<bean id="fixedFileDescriptor"
|
||||
class="org.springframework.batch.io.file.support.transform.PrefixMatchingCompositeLineTokenizer">
|
||||
@@ -79,7 +87,7 @@
|
||||
<property name="columns" value="1-12, 13-15, 16-20, 21-29" />
|
||||
</bean>
|
||||
|
||||
<bean parent="customEditorConfigurer"/>
|
||||
<bean parent="customEditorConfigurer" />
|
||||
|
||||
<!-- register the step scope with the application context -->
|
||||
<bean class="org.springframework.batch.execution.scope.StepScope" />
|
||||
|
||||
@@ -7,15 +7,12 @@ 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.FieldSetMapper;
|
||||
import org.springframework.batch.sample.item.provider.CollectionItemProvider;
|
||||
|
||||
public class CollectionItemProviderTests extends TestCase {
|
||||
|
||||
private MockControl inputControl;
|
||||
private InputSource input;
|
||||
private FieldSetMapper mapper;
|
||||
private CollectionItemProvider provider;
|
||||
|
||||
public void setUp() {
|
||||
@@ -24,26 +21,20 @@ public class CollectionItemProviderTests extends TestCase {
|
||||
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.read();
|
||||
inputControl.setReturnValue(new FieldSet(new String[] {"BEGIN"}));
|
||||
inputControl.setReturnValue(FieldSetMapper.BEGIN_RECORD);
|
||||
input.read();
|
||||
inputControl.setReturnValue(new FieldSet(new String[] {"line"}),3);
|
||||
inputControl.setReturnValue("line",3);
|
||||
input.read();
|
||||
inputControl.setReturnValue(new FieldSet(new String[] {"END"}));
|
||||
inputControl.setReturnValue(FieldSetMapper.END_RECORD);
|
||||
input.read();
|
||||
inputControl.setReturnValue(null);
|
||||
inputControl.replay();
|
||||
|
||||
@@ -36,5 +36,13 @@ public class TradeFieldSetMapperTests extends AbstractFieldSetMapperTests{
|
||||
protected FieldSetMapper fieldSetMapper() {
|
||||
return new TradeFieldSetMapper();
|
||||
}
|
||||
|
||||
public void testBeginRecord() throws Exception {
|
||||
assertEquals(FieldSetMapper.BEGIN_RECORD, fieldSetMapper().mapLine(new FieldSet(new String[] {"BEGIN"})));
|
||||
}
|
||||
|
||||
public void testEndRecord() throws Exception {
|
||||
assertEquals(FieldSetMapper.END_RECORD, fieldSetMapper().mapLine(new FieldSet(new String[] {"END"})));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user