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

@@ -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;
}
}