diff --git a/spring-batch-samples/.springBeans b/spring-batch-samples/.springBeans index 49a0d166a..ac5980a3c 100644 --- a/spring-batch-samples/.springBeans +++ b/spring-batch-samples/.springBeans @@ -18,7 +18,6 @@ src/main/resources/jobs/adhocLoopJob.xml src/main/resources/jobs/infiniteLoopJob.xml src/main/resources/data-source-context-init.xml - src/main/resources/jobs/xmlStaxJob.xml src/main/resources/jobs/hibernateJob.xml src/main/resources/jobs/ibatisJob.xml src/main/resources/jobs/footballJob.xml @@ -34,7 +33,6 @@ src/main/resources/jobs/multilineOrderInputTokenizers.xml src/main/resources/jobs/multilineOrderOutputAggregators.xml src/main/resources/jobs/compositeItemWriterSampleJob.xml - src/main/resources/jobs/multiResourceJob.xml src/main/resources/hibernate-context.xml src/main/resources/staging-test-context.xml src/main/resources/org/springframework/batch/sample/config/common-context.xml @@ -186,7 +184,6 @@ src/main/resources/data-source-context.xml src/main/resources/data-source-context-init.xml - src/main/resources/jobs/xmlStaxJob.xml src/main/resources/simple-job-launcher-context.xml src/main/resources/org/springframework/batch/sample/config/common-context.xml @@ -261,7 +258,6 @@ src/main/resources/data-source-context.xml src/main/resources/data-source-context-init.xml - src/main/resources/jobs/multiResourceJob.xml src/main/resources/simple-job-launcher-context.xml src/main/resources/org/springframework/batch/sample/config/common-context.xml diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/sample/domain/trade/CustomerCredit.java b/spring-batch-samples/src/main/java/org/springframework/batch/sample/domain/trade/CustomerCredit.java index 6750cc230..38bd5d166 100644 --- a/spring-batch-samples/src/main/java/org/springframework/batch/sample/domain/trade/CustomerCredit.java +++ b/spring-batch-samples/src/main/java/org/springframework/batch/sample/domain/trade/CustomerCredit.java @@ -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 + "]"; } diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/sample/domain/trade/internal/CustomerCreditFieldSetMapper.java b/spring-batch-samples/src/main/java/org/springframework/batch/sample/domain/trade/internal/CustomerCreditFieldSetMapper.java new file mode 100644 index 000000000..e4a9eec8b --- /dev/null +++ b/spring-batch-samples/src/main/java/org/springframework/batch/sample/domain/trade/internal/CustomerCreditFieldSetMapper.java @@ -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 { + + 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; + } +} diff --git a/spring-batch-samples/src/main/resources/data/iosample/input/delimited.csv b/spring-batch-samples/src/main/resources/data/iosample/input/delimited.csv new file mode 100644 index 000000000..a39b2d4b8 --- /dev/null +++ b/spring-batch-samples/src/main/resources/data/iosample/input/delimited.csv @@ -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 diff --git a/spring-batch-samples/src/main/resources/data/iosample/input/delimited2.csv b/spring-batch-samples/src/main/resources/data/iosample/input/delimited2.csv new file mode 100644 index 000000000..d1d68d574 --- /dev/null +++ b/spring-batch-samples/src/main/resources/data/iosample/input/delimited2.csv @@ -0,0 +1,2 @@ +UK21341EAH51,643,594.23,customer7 +UK21341EAH52,393,65.21,customer8 diff --git a/spring-batch-samples/src/main/resources/data/iosample/input/fixedLength.txt b/spring-batch-samples/src/main/resources/data/iosample/input/fixedLength.txt new file mode 100644 index 000000000..c31971514 --- /dev/null +++ b/spring-batch-samples/src/main/resources/data/iosample/input/fixedLength.txt @@ -0,0 +1,6 @@ +UK21341EAH45978 98.34customer1 +UK21341EAH46112 18.12customer2 +UK21341EAH47245 12.78customer3 +UK21341EAH48108109.25customer4 +UK21341EAH49854123.39customer5 +UK21341EAH50234 32.45customer6 diff --git a/spring-batch-samples/src/main/resources/data/iosample/input/input.xml b/spring-batch-samples/src/main/resources/data/iosample/input/input.xml new file mode 100644 index 000000000..8330402e8 --- /dev/null +++ b/spring-batch-samples/src/main/resources/data/iosample/input/input.xml @@ -0,0 +1,39 @@ + + + + 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 + + diff --git a/spring-batch-samples/src/main/resources/data/iosample/input/multiLine.txt b/spring-batch-samples/src/main/resources/data/iosample/input/multiLine.txt new file mode 100644 index 000000000..96f8db03e --- /dev/null +++ b/spring-batch-samples/src/main/resources/data/iosample/input/multiLine.txt @@ -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 diff --git a/spring-batch-samples/src/main/resources/data/iosample/input/multiRecordType.txt b/spring-batch-samples/src/main/resources/data/iosample/input/multiRecordType.txt new file mode 100644 index 000000000..2ca8d4a76 --- /dev/null +++ b/spring-batch-samples/src/main/resources/data/iosample/input/multiRecordType.txt @@ -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 diff --git a/spring-batch-samples/src/main/resources/jobs/ioSampleJob.xml b/spring-batch-samples/src/main/resources/jobs/ioSampleJob.xml new file mode 100644 index 000000000..39237c544 --- /dev/null +++ b/spring-batch-samples/src/main/resources/jobs/ioSampleJob.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + diff --git a/spring-batch-samples/src/main/resources/jobs/iosample/delimited.xml b/spring-batch-samples/src/main/resources/jobs/iosample/delimited.xml new file mode 100644 index 000000000..a38f98f8b --- /dev/null +++ b/spring-batch-samples/src/main/resources/jobs/iosample/delimited.xml @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-batch-samples/src/main/resources/jobs/iosample/fixedLength.xml b/spring-batch-samples/src/main/resources/jobs/iosample/fixedLength.xml new file mode 100644 index 000000000..815b9f8ac --- /dev/null +++ b/spring-batch-samples/src/main/resources/jobs/iosample/fixedLength.xml @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-batch-samples/src/main/resources/jobs/iosample/jdbcCursor.xml b/spring-batch-samples/src/main/resources/jobs/iosample/jdbcCursor.xml new file mode 100644 index 000000000..e83ce038f --- /dev/null +++ b/spring-batch-samples/src/main/resources/jobs/iosample/jdbcCursor.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-batch-samples/src/main/resources/jobs/iosample/multiLine.xml b/spring-batch-samples/src/main/resources/jobs/iosample/multiLine.xml new file mode 100644 index 000000000..53f3e435a --- /dev/null +++ b/spring-batch-samples/src/main/resources/jobs/iosample/multiLine.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-batch-samples/src/main/resources/jobs/iosample/multiRecordType.xml b/spring-batch-samples/src/main/resources/jobs/iosample/multiRecordType.xml new file mode 100644 index 000000000..da6ad91d3 --- /dev/null +++ b/spring-batch-samples/src/main/resources/jobs/iosample/multiRecordType.xml @@ -0,0 +1,83 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-batch-samples/src/main/resources/jobs/iosample/multiResource.xml b/spring-batch-samples/src/main/resources/jobs/iosample/multiResource.xml new file mode 100644 index 000000000..2cfedd7c8 --- /dev/null +++ b/spring-batch-samples/src/main/resources/jobs/iosample/multiResource.xml @@ -0,0 +1,53 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-batch-samples/src/main/resources/jobs/iosample/xml.xml b/spring-batch-samples/src/main/resources/jobs/iosample/xml.xml new file mode 100644 index 000000000..c3eef37e9 --- /dev/null +++ b/spring-batch-samples/src/main/resources/jobs/iosample/xml.xml @@ -0,0 +1,39 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/SkipSampleFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/SkipSampleFunctionalTests.java index 94373d524..137c21743 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/SkipSampleFunctionalTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/SkipSampleFunctionalTests.java @@ -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")); diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/DelimitedFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/DelimitedFunctionalTests.java new file mode 100644 index 000000000..4832170c5 --- /dev/null +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/DelimitedFunctionalTests.java @@ -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)); + } +} diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/FixedLengthFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/FixedLengthFunctionalTests.java new file mode 100644 index 000000000..c4802abca --- /dev/null +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/FixedLengthFunctionalTests.java @@ -0,0 +1,43 @@ +/* + * Copyright 2006-2007 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.batch.sample.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)); + } +} diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/JdbcCursorFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/JdbcCursorFunctionalTests.java new file mode 100644 index 000000000..d44922c17 --- /dev/null +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/JdbcCursorFunctionalTests.java @@ -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)); + } + } +} diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/MultiLineFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/MultiLineFunctionalTests.java new file mode 100644 index 000000000..d016cf771 --- /dev/null +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/MultiLineFunctionalTests.java @@ -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)); + } +} diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/MultiRecordTypeFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/MultiRecordTypeFunctionalTests.java new file mode 100644 index 000000000..372ebd914 --- /dev/null +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/MultiRecordTypeFunctionalTests.java @@ -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)); + } +} diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/MultiResourceFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/MultiResourceFunctionalTests.java new file mode 100644 index 000000000..5552c1afb --- /dev/null +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/MultiResourceFunctionalTests.java @@ -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")); + } +} diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/XmlFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/XmlFunctionalTests.java new file mode 100644 index 000000000..c4d423980 --- /dev/null +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/XmlFunctionalTests.java @@ -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)); + } +} diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/internal/DelegatingTradeLineAggregator.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/internal/DelegatingTradeLineAggregator.java new file mode 100644 index 000000000..2ee5cd3df --- /dev/null +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/internal/DelegatingTradeLineAggregator.java @@ -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 { + private LineAggregator tradeLineAggregator; + private LineAggregator 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 tradeLineAggregator) { + this.tradeLineAggregator = tradeLineAggregator; + } + + public void setCustomerLineAggregator(LineAggregator customerLineAggregator) { + this.customerLineAggregator = customerLineAggregator; + } +} diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/internal/DelegatingTradeLineAggregatorTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/internal/DelegatingTradeLineAggregatorTests.java new file mode 100644 index 000000000..fbdf160b2 --- /dev/null +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/internal/DelegatingTradeLineAggregatorTests.java @@ -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 tradeExtractor = new BeanWrapperFieldExtractor(); + tradeExtractor.setNames(new String[] { "isin", "quantity", "price", "customer" }); + + DelimitedLineAggregator tradeAggregator = new DelimitedLineAggregator(); + tradeAggregator.setFieldExtractor(tradeExtractor); + + BeanWrapperFieldExtractor customerExtractor = new BeanWrapperFieldExtractor(); + tradeExtractor.setNames(new String[] { "id", "name", "credit" }); + + DelimitedLineAggregator customerAggregator = new DelimitedLineAggregator(); + 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)); + } +} diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/internal/MultiLineTradeItemReader.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/internal/MultiLineTradeItemReader.java new file mode 100644 index 000000000..4af159086 --- /dev/null +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/internal/MultiLineTradeItemReader.java @@ -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, ItemStream { + + private FlatFileItemReader
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
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); + } +} diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/internal/MultiLineTradeItemReaderTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/internal/MultiLineTradeItemReaderTests.java new file mode 100644 index 000000000..bcb8e4653 --- /dev/null +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/internal/MultiLineTradeItemReaderTests.java @@ -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
() { + private int i = 0; + + public FieldSet read() { + if (i < data.length) { + return data[i++]; + } + return null; + } + }); + return this.reader.read(); + } +} diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/internal/MultiLineTradeItemWriter.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/internal/MultiLineTradeItemWriter.java new file mode 100644 index 000000000..c41e0c5b6 --- /dev/null +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/internal/MultiLineTradeItemWriter.java @@ -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, ItemStream { + + private FlatFileItemWriter delegate; + + public void write(List items) throws Exception { + List lines = new ArrayList(); + 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 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); + } +} diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/internal/MultiLineTradeItemWriterTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/internal/MultiLineTradeItemWriterTests.java new file mode 100644 index 000000000..b9e75aa94 --- /dev/null +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/internal/MultiLineTradeItemWriterTests.java @@ -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 delegate = new FlatFileItemWriter() { + List allItems = new ArrayList(); + + public void write(List items) throws Exception { + this.allItems.addAll(items); + } + + public List 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)); + } +} diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/internal/PrefixMatchingCompositeFieldSetMapper.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/internal/PrefixMatchingCompositeFieldSetMapper.java new file mode 100644 index 000000000..e2d27e739 --- /dev/null +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/internal/PrefixMatchingCompositeFieldSetMapper.java @@ -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 implements FieldSetMapper { + + private Map> mappers = new HashMap>(); + + public T mapFieldSet(FieldSet fieldSet) { + String prefix = fieldSet.readString("prefix"); + return this.mappers.get(prefix).mapFieldSet(fieldSet); + } + + public void setMappers(Map> mappers) { + this.mappers = mappers; + } +} diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/internal/PrefixMatchingCompositeFieldSetMapperTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/internal/PrefixMatchingCompositeFieldSetMapperTests.java new file mode 100644 index 000000000..e9e524ff1 --- /dev/null +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/internal/PrefixMatchingCompositeFieldSetMapperTests.java @@ -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 mappers = new HashMap(); + 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)); + } +} diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/internal/TradeCustomerItemWriter.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/internal/TradeCustomerItemWriter.java new file mode 100644 index 000000000..019156e46 --- /dev/null +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/internal/TradeCustomerItemWriter.java @@ -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 { + private TradeDao dao; + private int count; + + public void write(List 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; + } +}