OPEN - issue BATCH-911: Consolidate Samples

http://jira.springframework.org/browse/BATCH-911

There is now a new sample job called ioSample, which contains simple examples of all the major item reader and writer combinations, by substituting out implementations in the same job, rather than having a separate job per implementation.
This commit is contained in:
lucasward
2008-11-24 18:53:01 +00:00
parent b0a785a304
commit 9c3b62f6b7
34 changed files with 1423 additions and 17 deletions

View File

@@ -18,7 +18,6 @@
<config>src/main/resources/jobs/adhocLoopJob.xml</config>
<config>src/main/resources/jobs/infiniteLoopJob.xml</config>
<config>src/main/resources/data-source-context-init.xml</config>
<config>src/main/resources/jobs/xmlStaxJob.xml</config>
<config>src/main/resources/jobs/hibernateJob.xml</config>
<config>src/main/resources/jobs/ibatisJob.xml</config>
<config>src/main/resources/jobs/footballJob.xml</config>
@@ -34,7 +33,6 @@
<config>src/main/resources/jobs/multilineOrderInputTokenizers.xml</config>
<config>src/main/resources/jobs/multilineOrderOutputAggregators.xml</config>
<config>src/main/resources/jobs/compositeItemWriterSampleJob.xml</config>
<config>src/main/resources/jobs/multiResourceJob.xml</config>
<config>src/main/resources/hibernate-context.xml</config>
<config>src/main/resources/staging-test-context.xml</config>
<config>src/main/resources/org/springframework/batch/sample/config/common-context.xml</config>
@@ -186,7 +184,6 @@
<configs>
<config>src/main/resources/data-source-context.xml</config>
<config>src/main/resources/data-source-context-init.xml</config>
<config>src/main/resources/jobs/xmlStaxJob.xml</config>
<config>src/main/resources/simple-job-launcher-context.xml</config>
<config>src/main/resources/org/springframework/batch/sample/config/common-context.xml</config>
</configs>
@@ -261,7 +258,6 @@
<configs>
<config>src/main/resources/data-source-context.xml</config>
<config>src/main/resources/data-source-context-init.xml</config>
<config>src/main/resources/jobs/multiResourceJob.xml</config>
<config>src/main/resources/simple-job-launcher-context.xml</config>
<config>src/main/resources/org/springframework/batch/sample/config/common-context.xml</config>
</configs>

View File

@@ -23,8 +23,17 @@ public class CustomerCredit {
private int id;
private String name;
private BigDecimal credit;
public CustomerCredit(){
}
public CustomerCredit(int id, String name, BigDecimal credit){
this.id = id;
this.name = name;
this.credit = credit;
}
public String toString() {
public String toString() {
return "CustomerCredit [id=" + id +",name=" + name + ", credit=" + credit + "]";
}

View File

@@ -0,0 +1,42 @@
/*
* 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.trade.internal;
import org.springframework.batch.item.file.mapping.FieldSet;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.sample.domain.trade.CustomerCredit;
/**
* @author Dan Garrette
* @since 2.0
*/
public class CustomerCreditFieldSetMapper implements FieldSetMapper<CustomerCredit> {
public static final int ID_COLUMN = 0;
public static final int NAME_COLUMN = 1;
public static final int CREDIT_COLUMN = 2;
public CustomerCredit mapFieldSet(FieldSet fieldSet) {
CustomerCredit trade = new CustomerCredit();
trade.setId(fieldSet.readInt(ID_COLUMN));
trade.setName(fieldSet.readString(NAME_COLUMN));
trade.setCredit(fieldSet.readBigDecimal(CREDIT_COLUMN));
return trade;
}
}

View File

@@ -0,0 +1,6 @@
UK21341EAH45,978,98.34,customer1
UK21341EAH46,112,18.12,customer2
UK21341EAH47,245,12.78,customer3
UK21341EAH48,108,109.25,customer4
UK21341EAH49,854,123.39,customer5
UK21341EAH50,234,32.45,customer6
1 UK21341EAH45 978 98.34 customer1
2 UK21341EAH46 112 18.12 customer2
3 UK21341EAH47 245 12.78 customer3
4 UK21341EAH48 108 109.25 customer4
5 UK21341EAH49 854 123.39 customer5
6 UK21341EAH50 234 32.45 customer6

View File

@@ -0,0 +1,2 @@
UK21341EAH51,643,594.23,customer7
UK21341EAH52,393,65.21,customer8
1 UK21341EAH51 643 594.23 customer7
2 UK21341EAH52 393 65.21 customer8

View File

@@ -0,0 +1,6 @@
UK21341EAH45978 98.34customer1
UK21341EAH46112 18.12customer2
UK21341EAH47245 12.78customer3
UK21341EAH48108109.25customer4
UK21341EAH49854123.39customer5
UK21341EAH50234 32.45customer6

View File

@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<trades>
<trade>
<isin>UK21341EAH45</isin>
<quantity>978</quantity>
<price>98.34</price>
<customer>customer1</customer>
</trade>
<trade>
<isin>UK21341EAH46</isin>
<quantity>112</quantity>
<price>18.12</price>
<customer>customer2</customer>
</trade>
<trade>
<isin>UK21341EAH47</isin>
<quantity>245</quantity>
<price>12.78</price>
<customer>customer3</customer>
</trade>
<trade>
<isin>UK21341EAH48</isin>
<quantity>108</quantity>
<price>109.25</price>
<customer>customer4</customer>
</trade>
<trade>
<isin>UK21341EAH49</isin>
<quantity>854</quantity>
<price>123.39</price>
<customer>customer5</customer>
</trade>
<trade>
<isin>UK21341EAH50</isin>
<quantity>234</quantity>
<price>32.45</price>
<customer>customer6</customer>
</trade>
</trades>

View File

@@ -0,0 +1,24 @@
BEGIN
INFO,UK21341EAH45,customer1
AMNT,978,98.34
END
BEGIN
INFO,UK21341EAH46,customer2
AMNT,112,18.12
END
BEGIN
INFO,UK21341EAH47,customer3
AMNT,245,12.78
END
BEGIN
INFO,UK21341EAH48,customer4
AMNT,108,109.25
END
BEGIN
INFO,UK21341EAH49,customer5
AMNT,854,123.39
END
BEGIN
INFO,UK21341EAH50,customer6
AMNT,234,32.45
END

View File

@@ -0,0 +1,12 @@
CUST42001customer100012000
CUST42002customer200022000
CUST42003customer300032000
TRADUK21341EAH45978 98.34customer1
TRADUK21341EAH46112 18.12customer2
CUST42004customer400042000
CUST42005customer500052000
TRADUK21341EAH47245 12.78customer3
TRADUK21341EAH48108109.25customer4
TRADUK21341EAH49854123.39customer5
CUST42006customer600062000
TRADUK21341EAH50234 32.45customer6

View File

@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
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
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">
<bean id="ioSampleJob" parent="simpleJob">
<property name="steps">
<bean id="step1" parent="simpleStep">
<property name="commitInterval" value="2"/>
<property name="itemReader" ref="itemReader"/>
<property name="itemWriter" ref="itemWriter" />
</bean>
</property>
</bean>
</beans>

View File

@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
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
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">
<bean id="itemReader" class="org.springframework.batch.item.file.FlatFileItemReader">
<property name="resource" value="data/iosample/input/delimited.csv" />
<property name="lineMapper">
<bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<property name="lineTokenizer">
<bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
<property name="delimiter" value=","/>
</bean>
</property>
<property name="fieldSetMapper">
<bean class="org.springframework.batch.sample.domain.trade.internal.TradeFieldSetMapper"/>
</property>
</bean>
</property>
</bean>
<bean id="itemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter">
<property name="resource" value="file:target/test-outputs/delimitedOutput.csv" />
<property name="lineAggregator">
<bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
<property name="delimiter" value=","/>
<property name="fieldExtractor">
<bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
<property name="names" value="isin,quantity,price,customer"/>
</bean>
</property>
</bean>
</property>
</bean>
</beans>

View File

@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
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
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">
<bean id="itemReader" class="org.springframework.batch.item.file.FlatFileItemReader">
<property name="resource" value="data/iosample/input/fixedLength.txt" />
<property name="lineMapper">
<bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<property name="lineTokenizer">
<bean class="org.springframework.batch.item.file.transform.FixedLengthTokenizer">
<property name="columns" value="1-12,13-15,16-21,22-30"/>
</bean>
</property>
<property name="fieldSetMapper">
<bean class="org.springframework.batch.sample.domain.trade.internal.TradeFieldSetMapper"/>
</property>
</bean>
</property>
</bean>
<bean id="itemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter">
<property name="resource" value="file:target/test-outputs/fixedLengthOutput.txt" />
<property name="lineAggregator">
<bean class="org.springframework.batch.item.file.transform.FormatterLineAggregator">
<property name="fieldExtractor">
<bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
<property name="names" value="isin,quantity,price,customer"/>
</bean>
</property>
<property name="format" value="%-12s%-3d%6s%-9s"/>
</bean>
</property>
</bean>
</beans>

View File

@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
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
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">
<bean id="itemReader" class="org.springframework.batch.item.database.JdbcCursorItemReader">
<property name="dataSource" ref="dataSource"/>
<property name="sql" value="select ID, NAME, CREDIT from CUSTOMER"/>
<property name="mapper">
<bean class="org.springframework.batch.sample.domain.trade.internal.CustomerCreditRowMapper"/>
</property>
</bean>
<bean id="itemWriter" class="org.springframework.batch.sample.iosample.internal.TradeCustomerItemWriter">
<property name="dao">
<bean class="org.springframework.batch.sample.domain.trade.internal.JdbcTradeDao" p:dataSource-ref="dataSource">
<property name="incrementer">
<bean parent="incrementerParent">
<property name="incrementerName" value="TRADE_SEQ" />
</bean>
</property>
</bean>
</property>
</bean>
</beans>

View File

@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
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
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">
<bean id="itemReader" class="org.springframework.batch.sample.iosample.internal.MultiLineTradeItemReader">
<property name="delegate">
<bean class="org.springframework.batch.item.file.FlatFileItemReader">
<property name="resource" value="data/iosample/input/multiLine.txt" />
<property name="lineMapper">
<bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<property name="lineTokenizer">
<bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer"/>
</property>
<property name="fieldSetMapper">
<bean class="org.springframework.batch.item.file.mapping.PassThroughFieldSetMapper"/>
</property>
</bean>
</property>
</bean>
</property>
</bean>
<bean id="itemWriter" class="org.springframework.batch.sample.iosample.internal.MultiLineTradeItemWriter">
<property name="delegate">
<bean class="org.springframework.batch.item.file.FlatFileItemWriter">
<property name="resource" value="file:target/test-outputs/multiLineOutput.txt" />
<property name="lineAggregator">
<bean class="org.springframework.batch.item.file.transform.PassThroughLineAggregator"/>
</property>
</bean>
</property>
</bean>
</beans>

View File

@@ -0,0 +1,83 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
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
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">
<bean id="itemReader" class="org.springframework.batch.item.file.FlatFileItemReader">
<property name="resource" value="data/iosample/input/multiRecordType.txt" />
<property name="lineMapper">
<bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<property name="lineTokenizer" ref="compositeLineTokenizer"/>
<property name="fieldSetMapper" ref="compositeFieldSetMapper"/>
</bean>
</property>
</bean>
<bean id="compositeLineTokenizer" class="org.springframework.batch.item.file.transform.PrefixMatchingCompositeLineTokenizer">
<property name="tokenizers">
<map>
<entry key="TRAD" value-ref="tradeLineTokenizer" />
<entry key="CUST" value-ref="customerLineTokenizer" />
</map>
</property>
</bean>
<bean id="tradeLineTokenizer" class="org.springframework.batch.item.file.transform.FixedLengthTokenizer">
<property name="names" value="isin,quantity,price,customer,prefix"/>
<property name="columns" value="5-16,17-19,20-25,26-34,1-4"/>
</bean>
<bean id="customerLineTokenizer" class="org.springframework.batch.item.file.transform.FixedLengthTokenizer">
<property name="names" value="id,name,credit,prefix"/>
<property name="columns" value="5-9,10-18,19-26,1-4"/>
</bean>
<bean id="compositeFieldSetMapper" class="org.springframework.batch.sample.iosample.internal.PrefixMatchingCompositeFieldSetMapper">
<property name="mappers">
<map>
<entry key="TRAD" value-ref="tradeFieldSetMapper" />
<entry key="CUST" value-ref="customerFieldSetMapper" />
</map>
</property>
</bean>
<bean id="tradeFieldSetMapper" class="org.springframework.batch.sample.domain.trade.internal.TradeFieldSetMapper"/>
<bean id="customerFieldSetMapper" class="org.springframework.batch.sample.domain.trade.internal.CustomerCreditFieldSetMapper"/>
<bean id="itemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter">
<property name="resource" value="file:target/test-outputs/multiRecordTypeOutput.txt" />
<property name="lineAggregator">
<bean class="org.springframework.batch.sample.iosample.internal.DelegatingTradeLineAggregator">
<property name="tradeLineAggregator" ref="tradeLineAggregator"/>
<property name="customerLineAggregator" ref="customerLineAggregator"/>
</bean>
</property>
</bean>
<bean id="tradeLineAggregator" class="org.springframework.batch.item.file.transform.FormatterLineAggregator">
<property name="fieldExtractor">
<bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
<property name="names" value="isin,quantity,price,customer"/>
</bean>
</property>
<property name="format" value="TRAD%-12s%-3d%6s%-9s"/>
</bean>
<bean id="customerLineAggregator" class="org.springframework.batch.item.file.transform.FormatterLineAggregator">
<property name="fieldExtractor">
<bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
<property name="names" value="id,name,credit"/>
</bean>
</property>
<property name="format" value="CUST%05d%-9s%08.0f"/>
</bean>
</beans>

View File

@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
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
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">
<bean id="itemReader" class="org.springframework.batch.item.file.MultiResourceItemReader">
<property name="resources" value="classpath:data/iosample/input/delimited*.csv" />
<property name="delegate" ref="delegateReader" />
</bean>
<bean id="delegateReader" class="org.springframework.batch.item.file.FlatFileItemReader">
<property name="lineMapper">
<bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<property name="lineTokenizer">
<bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
<property name="delimiter" value=","/>
</bean>
</property>
<property name="fieldSetMapper">
<bean class="org.springframework.batch.sample.domain.trade.internal.TradeFieldSetMapper"/>
</property>
</bean>
</property>
</bean>
<bean id="itemWriter" class="org.springframework.batch.item.file.MultiResourceItemWriter">
<property name="resource" value="file:target/test-outputs/multiResourceOutput.csv" />
<property name="itemCountLimitPerResource" value="6" />
<property name="delegate" ref="delegateWriter"/>
</bean>
<bean id="delegateWriter" class="org.springframework.batch.item.file.FlatFileItemWriter">
<property name="lineAggregator">
<bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
<property name="delimiter" value=","/>
<property name="fieldExtractor">
<bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
<property name="names" value="isin,quantity,price,customer"/>
</bean>
</property>
</bean>
</property>
</bean>
</beans>

View File

@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
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
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">
<bean id="itemReader" class="org.springframework.batch.item.xml.StaxEventItemReader">
<property name="fragmentRootElementName" value="trade" />
<property name="resource" value="data/iosample/input/input.xml" />
<property name="unmarshaller" ref="tradeMarshaller"/>
</bean>
<bean id="itemWriter" class="org.springframework.batch.item.xml.StaxEventItemWriter">
<property name="resource" value="file:target/test-outputs/output.xml" />
<property name="marshaller" ref="tradeMarshaller" />
<property name="rootTagName" value="trades" />
<property name="overwriteOutput" value="true" />
</bean>
<bean id="tradeMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller">
<property name="aliases">
<util:map id="aliases">
<entry key="trade" value="org.springframework.batch.sample.domain.trade.Trade" />
<entry key="isin" value="java.lang.String" />
<entry key="quantity" value="long" />
<entry key="price" value="java.math.BigDecimal" />
<entry key="customer" value="java.lang.String" />
</util:map>
</property>
</bean>
</beans>

View File

@@ -153,9 +153,6 @@ public class SkipSampleFunctionalTests {
assertTrue(!execution1.get("JOB_INSTANCE_ID").equals(execution2.get("JOB_INSTANCE_ID")));
}
/**
*
*/
private void validateLaunchWithSkips() {
// Step1: 5 input records, 1 skipped => 4 written to output
assertEquals(4, SimpleJdbcTestUtils.countRowsInTable(simpleJdbcTemplate, "TRADE"));
@@ -163,12 +160,6 @@ public class SkipSampleFunctionalTests {
// Step2: 4 input records, 1 skipped => 3 written to output
assertEquals(3, itemTrackingWriter.getItems().size());
for(Object o : simpleJdbcTemplate.queryForList(
"SELECT * from ERROR_LOG"))
{
System.err.println("DHG > "+o);
}
// Both steps contained skips
assertEquals(2, SimpleJdbcTestUtils.countRowsInTable(simpleJdbcTemplate, "ERROR_LOG"));
assertEquals(1, simpleJdbcTemplate.queryForInt(
@@ -177,9 +168,6 @@ public class SkipSampleFunctionalTests {
"SELECT Count(*) from ERROR_LOG where JOB_NAME = ? and STEP_NAME = ?", "skipJob", "step2"));
}
/**
*
*/
private void validateLaunchWithoutSkips() {
// Step1: 5 input records => 5 written to output
assertEquals(5, SimpleJdbcTestUtils.countRowsInTable(simpleJdbcTemplate, "TRADE"));

View File

@@ -0,0 +1,47 @@
/*
* 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.iosample;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.test.AbstractJobTests;
import org.springframework.batch.test.AssertFile;
import org.springframework.core.io.FileSystemResource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Dan Garrette
* @since 2.0
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/jobs/ioSampleJob.xml",
"/jobs/ioSample/delimited.xml" })
public class DelimitedFunctionalTests extends AbstractJobTests {
private static final String OUTPUT_FILE = "target/test-outputs/delimitedOutput.csv";
private static final String INPUT_FILE = "src/main/resources/data/ioSample/input/delimited.csv";
/**
* Output should be the same as input
*/
@Test
public void testJob() throws Exception {
this.launchJob();
AssertFile.assertFileEquals(new FileSystemResource(OUTPUT_FILE), new FileSystemResource(INPUT_FILE));
}
}

View File

@@ -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.iosample;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.test.AbstractJobTests;
import org.springframework.batch.test.AssertFile;
import org.springframework.core.io.FileSystemResource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/jobs/ioSampleJob.xml",
"/jobs/ioSample/fixedLength.xml" })
public class FixedLengthFunctionalTests extends AbstractJobTests {
private static final String OUTPUT_FILE = "target/test-outputs/fixedLengthOutput.txt";
private static final String INPUT_FILE = "src/main/resources/data/ioSample/input/fixedLength.txt";
/**
* Output should be the same as input
*/
@Test
public void testJob() throws Exception {
this.launchJob();
AssertFile.assertFileEquals(new FileSystemResource(OUTPUT_FILE), new FileSystemResource(INPUT_FILE));
}
}

View File

@@ -0,0 +1,63 @@
/*
* 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.iosample;
import static org.junit.Assert.assertEquals;
import javax.sql.DataSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.test.AbstractJobTests;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.jdbc.SimpleJdbcTestUtils;
/**
* @author Dan Garrette
* @since 2.0
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/jobs/ioSampleJob.xml",
"/jobs/ioSample/jdbcCursor.xml" })
public class JdbcCursorFunctionalTests extends AbstractJobTests {
private SimpleJdbcTemplate simpleJdbcTemplate;
@Autowired
public void setDataSource(DataSource dataSource) {
this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
}
/**
* Output should be the same as input
*/
@Test
public void testJob() throws Exception {
this.simpleJdbcTemplate.update("delete from TRADE");
this.launchJob();
assertEquals(4, SimpleJdbcTestUtils.countRowsInTable(simpleJdbcTemplate, "TRADE"));
for (int i = 1; i <= 4; i++) {
String sql = "select count(*) from TRADE where CUSTOMER = ?";
assertEquals(1, simpleJdbcTemplate.queryForInt(sql, "customer" + i));
}
}
}

View File

@@ -0,0 +1,47 @@
/*
* 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.iosample;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.test.AbstractJobTests;
import org.springframework.batch.test.AssertFile;
import org.springframework.core.io.FileSystemResource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Dan Garrette
* @since 2.0
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/jobs/ioSampleJob.xml",
"/jobs/ioSample/multiLine.xml" })
public class MultiLineFunctionalTests extends AbstractJobTests {
private static final String OUTPUT_FILE = "target/test-outputs/multiLineOutput.txt";
private static final String INPUT_FILE = "src/main/resources/data/ioSample/input/multiLine.txt";
/**
* Output should be the same as input
*/
@Test
public void testJob() throws Exception {
this.launchJob();
AssertFile.assertFileEquals(new FileSystemResource(OUTPUT_FILE), new FileSystemResource(INPUT_FILE));
}
}

View File

@@ -0,0 +1,47 @@
/*
* 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.iosample;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.test.AbstractJobTests;
import org.springframework.batch.test.AssertFile;
import org.springframework.core.io.FileSystemResource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Dan Garrette
* @since 2.0
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/jobs/ioSampleJob.xml",
"/jobs/ioSample/multiRecordType.xml" })
public class MultiRecordTypeFunctionalTests extends AbstractJobTests {
private static final String OUTPUT_FILE = "target/test-outputs/multiRecordTypeOutput.txt";
private static final String INPUT_FILE = "src/main/resources/data/ioSample/input/multiRecordType.txt";
/**
* Output should be the same as input
*/
@Test
public void testJob() throws Exception {
this.launchJob();
AssertFile.assertFileEquals(new FileSystemResource(OUTPUT_FILE), new FileSystemResource(INPUT_FILE));
}
}

View File

@@ -0,0 +1,48 @@
/*
* 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.iosample;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.test.AbstractJobTests;
import org.springframework.batch.test.AssertFile;
import org.springframework.core.io.FileSystemResource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Dan Garrette
* @since 2.0
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/jobs/ioSampleJob.xml",
"/jobs/ioSample/multiResource.xml" })
public class MultiResourceFunctionalTests extends AbstractJobTests {
/**
* Output should be the same as input
*/
@Test
public void testJob() throws Exception {
this.launchJob();
AssertFile.assertFileEquals(new FileSystemResource("target/test-outputs/multiResourceOutput.csv.1"),
new FileSystemResource("src/main/resources/data/ioSample/input/delimited.csv"));
AssertFile.assertFileEquals(new FileSystemResource("target/test-outputs/multiResourceOutput.csv.2"),
new FileSystemResource("src/main/resources/data/ioSample/input/delimited2.csv"));
}
}

View File

@@ -0,0 +1,50 @@
/*
* 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.iosample;
import java.io.FileReader;
import org.custommonkey.xmlunit.XMLAssert;
import org.custommonkey.xmlunit.XMLUnit;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.test.AbstractJobTests;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Dan Garrette
* @since 2.0
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/jobs/ioSampleJob.xml",
"/jobs/ioSample/xml.xml" })
public class XmlFunctionalTests extends AbstractJobTests {
private static final String OUTPUT_FILE = "target/test-outputs/output.xml";
private static final String INPUT_FILE = "src/main/resources/data/ioSample/input/input.xml";
/**
* Output should be the same as input
*/
@Test
public void testJob() throws Exception {
this.launchJob();
XMLUnit.setIgnoreWhitespace(true);
XMLAssert.assertXMLEqual(new FileReader(INPUT_FILE), new FileReader(OUTPUT_FILE));
}
}

View File

@@ -0,0 +1,50 @@
/*
* 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.iosample.internal;
import org.springframework.batch.item.file.transform.LineAggregator;
import org.springframework.batch.sample.domain.trade.CustomerCredit;
import org.springframework.batch.sample.domain.trade.Trade;
/**
* @author Dan Garrette
* @since 2.0
*/
public class DelegatingTradeLineAggregator implements LineAggregator<Object> {
private LineAggregator<Trade> tradeLineAggregator;
private LineAggregator<CustomerCredit> customerLineAggregator;
public String aggregate(Object item) {
if (item instanceof Trade) {
return this.tradeLineAggregator.aggregate((Trade) item);
}
else if (item instanceof CustomerCredit) {
return this.customerLineAggregator.aggregate((CustomerCredit) item);
}
else {
throw new RuntimeException();
}
}
public void setTradeLineAggregator(LineAggregator<Trade> tradeLineAggregator) {
this.tradeLineAggregator = tradeLineAggregator;
}
public void setCustomerLineAggregator(LineAggregator<CustomerCredit> customerLineAggregator) {
this.customerLineAggregator = customerLineAggregator;
}
}

View File

@@ -0,0 +1,59 @@
/*
* 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.iosample.internal;
import static junit.framework.Assert.assertEquals;
import java.math.BigDecimal;
import org.junit.Test;
import org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor;
import org.springframework.batch.item.file.transform.DelimitedLineAggregator;
import org.springframework.batch.sample.domain.trade.CustomerCredit;
import org.springframework.batch.sample.domain.trade.Trade;
/**
* @author Dan Garrette
* @since 2.0
*/
public class DelegatingTradeLineAggregatorTests {
@Test
public void testAggregate() {
BeanWrapperFieldExtractor<Trade> tradeExtractor = new BeanWrapperFieldExtractor<Trade>();
tradeExtractor.setNames(new String[] { "isin", "quantity", "price", "customer" });
DelimitedLineAggregator<Trade> tradeAggregator = new DelimitedLineAggregator<Trade>();
tradeAggregator.setFieldExtractor(tradeExtractor);
BeanWrapperFieldExtractor<CustomerCredit> customerExtractor = new BeanWrapperFieldExtractor<CustomerCredit>();
tradeExtractor.setNames(new String[] { "id", "name", "credit" });
DelimitedLineAggregator<CustomerCredit> customerAggregator = new DelimitedLineAggregator<CustomerCredit>();
customerAggregator.setFieldExtractor(customerExtractor);
DelegatingTradeLineAggregator aggregator = new DelegatingTradeLineAggregator();
aggregator.setTradeLineAggregator(tradeAggregator);
aggregator.setCustomerLineAggregator(customerAggregator);
Trade t = new Trade("ISIN001", 500, new BigDecimal("4.50"), "Customer1");
assertEquals("ISIN001,500,4.50,Customer1", aggregator.aggregate(t));
CustomerCredit c = new CustomerCredit(256, "customer1", new BigDecimal("3200.00"));
assertEquals("256,customer1,3200.00", aggregator.aggregate(c));
}
}

View File

@@ -0,0 +1,80 @@
/*
* 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.iosample.internal;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.ItemStreamException;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.FieldSet;
import org.springframework.batch.sample.domain.trade.Trade;
import org.springframework.util.Assert;
/**
* @author Dan Garrette
* @since 2.0
*/
public class MultiLineTradeItemReader implements ItemReader<Trade>, ItemStream {
private FlatFileItemReader<FieldSet> delegate;
/**
* @see org.springframework.batch.item.ItemReader#read()
*/
public Trade read() throws Exception {
Trade t = null;
for (FieldSet line = null; (line = this.delegate.read()) != null;) {
String prefix = line.readString(0);
if (prefix.equals("BEGIN")) {
t = new Trade(); // Record must start with 'BEGIN'
}
else if (prefix.equals("INFO")) {
Assert.notNull(t, "No 'BEGIN' was found.");
t.setIsin(line.readString(1));
t.setCustomer(line.readString(2));
}
else if (prefix.equals("AMNT")) {
Assert.notNull(t, "No 'BEGIN' was found.");
t.setQuantity(line.readInt(1));
t.setPrice(line.readBigDecimal(2));
}
else if (prefix.equals("END")) {
return t; // Record must end with 'END'
}
}
Assert.isNull(t, "No 'END' was found.");
return null;
}
public void setDelegate(FlatFileItemReader<FieldSet> delegate) {
this.delegate = delegate;
}
public void close(ExecutionContext executionContext) throws ItemStreamException {
this.delegate.close(executionContext);
}
public void open(ExecutionContext executionContext) throws ItemStreamException {
this.delegate.open(executionContext);
}
public void update(ExecutionContext executionContext) throws ItemStreamException {
this.delegate.update(executionContext);
}
}

View File

@@ -0,0 +1,110 @@
/*
* 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.iosample.internal;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.fail;
import java.math.BigDecimal;
import org.junit.Test;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.DefaultFieldSet;
import org.springframework.batch.item.file.mapping.FieldSet;
import org.springframework.batch.sample.domain.trade.Trade;
/**
* @author Dan Garrette
* @since 2.0
*/
public class MultiLineTradeItemReaderTests {
private MultiLineTradeItemReader reader = new MultiLineTradeItemReader();
@Test
public void testRead_complete() throws Exception {
String isin = "ISIN001";
String customer = "customer1";
long quantity = (long) 300;
BigDecimal price = new BigDecimal("4.50");
final FieldSet[] data = { new DefaultFieldSet(new String[] { "BEGIN" }),
new DefaultFieldSet(new String[] { "INFO", isin, customer }),
new DefaultFieldSet(new String[] { "AMNT", "" + quantity, price.toString() }),
new DefaultFieldSet(new String[] { "END" }) };
Trade t = this.readData(data);
assertEquals(isin, t.getIsin());
assertEquals(customer, t.getCustomer());
assertEquals(quantity, t.getQuantity());
assertEquals(price, t.getPrice());
}
@Test
public void testRead_noBegin() throws Exception {
String isin = "ISIN001";
String customer = "customer1";
long quantity = (long) 300;
BigDecimal price = new BigDecimal("4.50");
final FieldSet[] data = { new DefaultFieldSet(new String[] { "INFO", isin, customer }),
new DefaultFieldSet(new String[] { "AMNT", "" + quantity, price.toString() }),
new DefaultFieldSet(new String[] { "END" }) };
try {
this.readData(data);
fail();
}
catch (IllegalArgumentException e) {
assertEquals("No 'BEGIN' was found.", e.getMessage());
}
}
@Test
public void testRead_noEnd() throws Exception {
String isin = "ISIN001";
String customer = "customer1";
long quantity = (long) 300;
BigDecimal price = new BigDecimal("4.50");
final FieldSet[] data = { new DefaultFieldSet(new String[] { "BEGIN" }),
new DefaultFieldSet(new String[] { "INFO", isin, customer }),
new DefaultFieldSet(new String[] { "AMNT", "" + quantity, price.toString() }) };
try {
this.readData(data);
fail();
}
catch (IllegalArgumentException e) {
assertEquals("No 'END' was found.", e.getMessage());
}
}
private Trade readData(final FieldSet[] data) throws Exception {
this.reader.setDelegate(new FlatFileItemReader<FieldSet>() {
private int i = 0;
public FieldSet read() {
if (i < data.length) {
return data[i++];
}
return null;
}
});
return this.reader.read();
}
}

View File

@@ -0,0 +1,63 @@
/*
* 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.iosample.internal;
import java.util.ArrayList;
import java.util.List;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.ItemStreamException;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.file.FlatFileItemWriter;
import org.springframework.batch.sample.domain.trade.Trade;
/**
* @author Dan Garrette
* @since 2.0
*/
public class MultiLineTradeItemWriter implements ItemWriter<Trade>, ItemStream {
private FlatFileItemWriter<String> delegate;
public void write(List<? extends Trade> items) throws Exception {
List<String> lines = new ArrayList<String>();
for (Trade t : items) {
lines.add("BEGIN");
lines.add("INFO," + t.getIsin() + "," + t.getCustomer());
lines.add("AMNT," + t.getQuantity() + "," + t.getPrice());
lines.add("END");
}
this.delegate.write(lines);
}
public void setDelegate(FlatFileItemWriter<String> delegate) {
this.delegate = delegate;
}
public void close(ExecutionContext executionContext) throws ItemStreamException {
this.delegate.close(executionContext);
}
public void open(ExecutionContext executionContext) throws ItemStreamException {
this.delegate.open(executionContext);
}
public void update(ExecutionContext executionContext) throws ItemStreamException {
this.delegate.update(executionContext);
}
}

View File

@@ -0,0 +1,59 @@
/*
* 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.iosample.internal;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import org.springframework.batch.item.file.FlatFileItemWriter;
import org.springframework.batch.sample.domain.trade.Trade;
/**
* @author Dan Garrette
* @since 2.0
*/
public class MultiLineTradeItemWriterTests {
private MultiLineTradeItemWriter writer;
public MultiLineTradeItemWriterTests() {
FlatFileItemWriter<String> delegate = new FlatFileItemWriter<String>() {
List<String> allItems = new ArrayList<String>();
public void write(List items) throws Exception {
this.allItems.addAll(items);
}
public List<String> getAllItems() {
return this.allItems;
}
};
this.writer = new MultiLineTradeItemWriter();
this.writer.setDelegate(delegate);
}
@Test
public void testWrite() throws Exception {
Trade t1 = new Trade("ISIN001", 400, new BigDecimal("5.75"), "Customer1");
Trade t2 = new Trade("ISIN002", 200, new BigDecimal("6.25"), "Customer2");
this.writer.write(Arrays.asList(t1, t2));
}
}

View File

@@ -0,0 +1,44 @@
/*
* 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.iosample.internal;
import java.util.HashMap;
import java.util.Map;
import org.springframework.batch.item.file.mapping.FieldSet;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
/**
* This class is used to delegate to a {@link FieldSetMapper} based on one field
* in the {@link FieldSet}.
*
* @author Dan Garrette
* @since 2.0
*/
public class PrefixMatchingCompositeFieldSetMapper<T> implements FieldSetMapper<T> {
private Map<String, FieldSetMapper<T>> mappers = new HashMap<String, FieldSetMapper<T>>();
public T mapFieldSet(FieldSet fieldSet) {
String prefix = fieldSet.readString("prefix");
return this.mappers.get(prefix).mapFieldSet(fieldSet);
}
public void setMappers(Map<String, FieldSetMapper<T>> mappers) {
this.mappers = mappers;
}
}

View File

@@ -0,0 +1,63 @@
/*
* 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.iosample.internal;
import static junit.framework.Assert.assertEquals;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import org.springframework.batch.item.file.mapping.DefaultFieldSet;
import org.springframework.batch.item.file.mapping.FieldSet;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.sample.domain.trade.CustomerCredit;
import org.springframework.batch.sample.domain.trade.Trade;
import org.springframework.batch.sample.domain.trade.internal.CustomerCreditFieldSetMapper;
import org.springframework.batch.sample.domain.trade.internal.TradeFieldSetMapper;
/**
* @author Dan Garrette
* @since 2.0
*/
public class PrefixMatchingCompositeFieldSetMapperTests {
@Test
public void testMapFieldSet() {
Map<String, FieldSetMapper> mappers = new HashMap<String, FieldSetMapper>();
mappers.put("TRAD", new TradeFieldSetMapper());
mappers.put("CUST", new CustomerCreditFieldSetMapper());
PrefixMatchingCompositeFieldSetMapper mapper = new PrefixMatchingCompositeFieldSetMapper();
mapper.setMappers(mappers);
String[] tradeNames = new String[] { "isin", "quantity", "price", "customer", "prefix" };
String[] tradeValues = new String[] { "ISIN001", "500", "4.50", "Customer1", "TRAD" };
FieldSet tradeFS = new DefaultFieldSet(tradeValues, tradeNames);
String[] customerNames = new String[] { "id", "name", "credit", "prefix" };
String[] customerValues = new String[] { "256", "customer1", "3200.00", "CUST" };
FieldSet customerFS = new DefaultFieldSet(customerValues, customerNames);
Trade trade = new Trade("ISIN001", 500, new BigDecimal("4.50"), "Customer1");
assertEquals(trade, mapper.mapFieldSet(tradeFS));
CustomerCredit customer = new CustomerCredit(256, "customer1", new BigDecimal("3200.00"));
assertEquals(customer, mapper.mapFieldSet(customerFS));
}
}

View File

@@ -0,0 +1,45 @@
/*
* 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.iosample.internal;
import java.math.BigDecimal;
import java.util.List;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.sample.domain.trade.CustomerCredit;
import org.springframework.batch.sample.domain.trade.Trade;
import org.springframework.batch.sample.domain.trade.TradeDao;
/**
* @author Dan Garrette
* @since 2.0
*/
public class TradeCustomerItemWriter implements ItemWriter<CustomerCredit> {
private TradeDao dao;
private int count;
public void write(List<? extends CustomerCredit> items) throws Exception {
for (CustomerCredit c : items) {
Trade t = new Trade("ISIN" + count++, 100, new BigDecimal("1.50"), c.getName());
this.dao.writeTrade(t);
}
}
public void setDao(TradeDao dao) {
this.dao = dao;
}
}