From 2740d79331334b3d3c1529efdc9f226ec26fad84 Mon Sep 17 00:00:00 2001 From: robokaso Date: Wed, 23 Jul 2008 08:46:50 +0000 Subject: [PATCH] IN PROGRESS - BATCH-711: Upgrade ItemWriter and implementations to use parameterized types separated implementation of PassThroughFieldSetMapper and ...Creator --- .../mapping/PassThroughFieldSetCreator.java | 22 ++++++++ .../mapping/PassThroughFieldSetMapper.java | 25 ++-------- .../item/file/FlatFileItemWriterTests.java | 50 ++++++------------- .../PassThroughFieldSetCreatorTests.java | 34 +++++++++++++ .../PassThroughFieldSetMapperTests.java | 26 +--------- 5 files changed, 77 insertions(+), 80 deletions(-) create mode 100644 spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/mapping/PassThroughFieldSetCreator.java create mode 100644 spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/mapping/PassThroughFieldSetCreatorTests.java diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/mapping/PassThroughFieldSetCreator.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/mapping/PassThroughFieldSetCreator.java new file mode 100644 index 000000000..949476646 --- /dev/null +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/mapping/PassThroughFieldSetCreator.java @@ -0,0 +1,22 @@ +package org.springframework.batch.item.file.mapping; + +public class PassThroughFieldSetCreator implements FieldSetCreator { + + /** + * If the input is a {@link FieldSet} pass it to the caller. Otherwise + * convert to a String with toString() and convert it to a single field + * {@link FieldSet}. + * + * @see org.springframework.batch.item.file.mapping.FieldSetCreator#mapItem(java.lang.Object) + */ + public FieldSet mapItem(T item) { + if (item instanceof FieldSet) { + return (FieldSet) item; + } + + String stringItem = item.toString(); + + return new DefaultFieldSet(new String[] { stringItem }); + } + +} diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/mapping/PassThroughFieldSetMapper.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/mapping/PassThroughFieldSetMapper.java index 6c798d05d..e26de83fe 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/mapping/PassThroughFieldSetMapper.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/mapping/PassThroughFieldSetMapper.java @@ -15,7 +15,6 @@ */ package org.springframework.batch.item.file.mapping; - /** * Pass through {@link FieldSetMapper} useful for passing a {@link FieldSet} * back directly rather than a mapped object. @@ -23,31 +22,17 @@ package org.springframework.batch.item.file.mapping; * @author Lucas Ward * */ -public class PassThroughFieldSetMapper implements FieldSetMapper
, FieldSetCreator { +public class PassThroughFieldSetMapper implements FieldSetMapper
{ /* * (non-Javadoc) - * @see org.springframework.batch.io.file.FieldSetMapper#mapLine(org.springframework.batch.io.file.FieldSet) + * + * @see + * org.springframework.batch.io.file.FieldSetMapper#mapLine(org.springframework + * .batch.io.file.FieldSet) */ public FieldSet mapLine(FieldSet fs, int lineNum) { return fs; } - /** - * If the input is a {@link FieldSet} pass it to the caller. Otherwise - * convert to a String with toString() and convert it to a single field - * {@link FieldSet}. - * - * @see org.springframework.batch.item.file.mapping.FieldSetCreator#mapItem(java.lang.Object) - */ - public FieldSet mapItem(Object data) { - if (data instanceof FieldSet) { - return (FieldSet) data; - } - if (!(data instanceof String)) { - data = "" + data; - } - return new DefaultFieldSet(new String[] { (String) data }); - } - } diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/FlatFileItemWriterTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/FlatFileItemWriterTests.java index 64f92c2a7..623f2cd07 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/FlatFileItemWriterTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/FlatFileItemWriterTests.java @@ -29,7 +29,7 @@ import org.springframework.batch.item.ItemStreamException; import org.springframework.batch.item.file.mapping.DefaultFieldSet; import org.springframework.batch.item.file.mapping.FieldSet; import org.springframework.batch.item.file.mapping.FieldSetCreator; -import org.springframework.batch.item.file.mapping.PassThroughFieldSetMapper; +import org.springframework.batch.item.file.mapping.PassThroughFieldSetCreator; import org.springframework.core.io.FileSystemResource; import org.springframework.transaction.support.TransactionSynchronizationManager; import org.springframework.util.ClassUtils; @@ -39,14 +39,14 @@ import org.springframework.util.ClassUtils; * in separate TestCase classes with different setUp and * tearDown methods * - * @author robert.kasanicky + * @author Robert Kasanicky * @author Dave Syer * */ public class FlatFileItemWriterTests extends TestCase { // object under test - private FlatFileItemWriter writer = new FlatFileItemWriter(); + private FlatFileItemWriter writer = new FlatFileItemWriter(); // String to be written into file by the FlatFileInputTemplate private static final String TEST_STRING = "FlatFileOutputTemplateTest-OutputData"; @@ -73,7 +73,7 @@ public class FlatFileItemWriterTests extends TestCase { outputFile = File.createTempFile("flatfile-output-", ".tmp"); writer.setResource(new FileSystemResource(outputFile)); - writer.setFieldSetCreator(new PassThroughFieldSetMapper()); + writer.setFieldSetCreator(new PassThroughFieldSetCreator()); writer.afterPropertiesSet(); writer.setSaveState(true); executionContext = new ExecutionContext(); @@ -143,38 +143,18 @@ public class FlatFileItemWriterTests extends TestCase { * @throws Exception */ public void testWriteWithConverter() throws Exception { - writer.setFieldSetCreator(new FieldSetCreator() { - public FieldSet mapItem(Object data) { + writer.setFieldSetCreator(new FieldSetCreator() { + public FieldSet mapItem(String data) { return new DefaultFieldSet(new String[] { "FOO:" + data }); } }); - Object data = new Object(); + String data = "string"; writer.open(executionContext); writer.write(data); writer.flush(); String lineFromFile = readLine(); // converter not used if input is String - assertEquals("FOO:" + data.toString(), lineFromFile); - } - - /** - * Regular usage of write(String) method - * - * @throws Exception - */ - public void testWriteWithConverterAndInfiniteLoop() throws Exception { - writer.setFieldSetCreator(new FieldSetCreator() { - public FieldSet mapItem(Object data) { - return new DefaultFieldSet(new String[] { "FOO:" + data }); - } - }); - Object data = new Object(); - writer.open(executionContext); - writer.write(data); - writer.flush(); - String lineFromFile = readLine(); - // converter not used if input is String - assertEquals("FOO:" + data.toString(), lineFromFile); + assertEquals("FOO:" + data, lineFromFile); } /** @@ -183,8 +163,8 @@ public class FlatFileItemWriterTests extends TestCase { * @throws Exception */ public void testWriteWithConverterAndString() throws Exception { - writer.setFieldSetCreator(new FieldSetCreator() { - public FieldSet mapItem(Object data) { + writer.setFieldSetCreator(new FieldSetCreator() { + public FieldSet mapItem(String data) { return new DefaultFieldSet(new String[] { "FOO:" + data }); } }); @@ -295,8 +275,8 @@ public class FlatFileItemWriterTests extends TestCase { } public void testOpenWithNonWritableFile() throws Exception { - writer = new FlatFileItemWriter(); - writer.setFieldSetCreator(new PassThroughFieldSetMapper()); + writer = new FlatFileItemWriter(); + writer.setFieldSetCreator(new PassThroughFieldSetCreator()); FileSystemResource file = new FileSystemResource("target/no-such-file.foo"); writer.setResource(file); new File(file.getFile().getParent()).mkdirs(); @@ -313,7 +293,7 @@ public class FlatFileItemWriterTests extends TestCase { } public void testAfterPropertiesSetChecksMandatory() throws Exception { - writer = new FlatFileItemWriter(); + writer = new FlatFileItemWriter(); try { writer.afterPropertiesSet(); fail("Expected IllegalArgumentException"); @@ -324,9 +304,9 @@ public class FlatFileItemWriterTests extends TestCase { } public void testDefaultStreamContext() throws Exception { - writer = new FlatFileItemWriter(); + writer = new FlatFileItemWriter(); writer.setResource(new FileSystemResource(outputFile)); - writer.setFieldSetCreator(new PassThroughFieldSetMapper()); + writer.setFieldSetCreator(new PassThroughFieldSetCreator()); writer.afterPropertiesSet(); writer.setSaveState(true); writer.open(executionContext); diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/mapping/PassThroughFieldSetCreatorTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/mapping/PassThroughFieldSetCreatorTests.java new file mode 100644 index 000000000..863bceb69 --- /dev/null +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/mapping/PassThroughFieldSetCreatorTests.java @@ -0,0 +1,34 @@ +package org.springframework.batch.item.file.mapping; + +import junit.framework.TestCase; + +public class PassThroughFieldSetCreatorTests extends TestCase { + + private PassThroughFieldSetCreator mapper = new PassThroughFieldSetCreator(); + + /** + * Test method for + * {@link org.springframework.batch.item.file.mapping.PassThroughFieldSetCreator#mapItem(Object)}. + */ + public void testUnmapItemAsFieldSet() { + FieldSet fieldSet = new DefaultFieldSet(new String[] { "foo", "bar" }); + assertEquals(fieldSet, mapper.mapItem(fieldSet)); + } + + /** + * Test method for + * {@link org.springframework.batch.item.file.mapping.PassThroughFieldSetCreator#mapItem(java.lang.Object)}. + */ + public void testUnmapItemAsString() { + assertEquals(new DefaultFieldSet(new String[] { "foo" }), mapper.mapItem("foo")); + } + + /** + * Test method for + * {@link org.springframework.batch.item.file.mapping.PassThroughFieldSetCreator#mapItem(java.lang.Object)}. + */ + public void testUnmapItemAsNonString() { + Object object = new Object(); + assertEquals(new DefaultFieldSet(new String[] { "" + object }), mapper.mapItem(object)); + } +} diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/mapping/PassThroughFieldSetMapperTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/mapping/PassThroughFieldSetMapperTests.java index 3cc58b838..884cf2caf 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/mapping/PassThroughFieldSetMapperTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/mapping/PassThroughFieldSetMapperTests.java @@ -34,29 +34,5 @@ public class PassThroughFieldSetMapperTests extends TestCase { assertEquals(fieldSet, mapper.mapLine(fieldSet, -1)); } - /** - * Test method for - * {@link org.springframework.batch.item.file.mapping.PassThroughFieldSetMapper#mapItem(java.lang.Object)}. - */ - public void testUnmapItemAsFieldSet() { - FieldSet fieldSet = new DefaultFieldSet(new String[] { "foo", "bar" }); - assertEquals(fieldSet, mapper.mapItem(fieldSet)); - } - - /** - * Test method for - * {@link org.springframework.batch.item.file.mapping.PassThroughFieldSetMapper#mapItem(java.lang.Object)}. - */ - public void testUnmapItemAsString() { - assertEquals(new DefaultFieldSet(new String[] { "foo" }), mapper.mapItem("foo")); - } - - /** - * Test method for - * {@link org.springframework.batch.item.file.mapping.PassThroughFieldSetMapper#mapItem(java.lang.Object)}. - */ - public void testUnmapItemAsNonString() { - Object object = new Object(); - assertEquals(new DefaultFieldSet(new String[] { "" + object }), mapper.mapItem(object)); - } + }