RESOLVED - issue BATCH-640: FieldSetMapper.mapLine() should contain the line number

This commit is contained in:
dsyer
2008-07-17 09:51:34 +00:00
parent 3de4794205
commit 8ffa494527
25 changed files with 77 additions and 58 deletions

View File

@@ -276,7 +276,7 @@ public class FlatFileItemReader extends AbstractBufferedItemReaderItemStream imp
int lineCount = getReader().getPosition();
try {
FieldSet tokenizedLine = tokenizer.tokenize(line);
return fieldSetMapper.mapLine(tokenizedLine);
return fieldSetMapper.mapLine(tokenizedLine, lineCount);
}
catch (RuntimeException ex) {
// add current line count to message and re-throw

View File

@@ -102,7 +102,7 @@ public class BeanWrapperFieldSetMapper extends DefaultPropertyEditorRegistrar im
/**
* The bean name (id) for an object that can be populated from the field set
* that will be passed into {@link #mapLine(FieldSet)}. Typically a
* that will be passed into {@link #mapLine(FieldSet, int)}. Typically a
* prototype scoped bean so that a new instance is returned for each field
* set mapped.
*
@@ -118,7 +118,7 @@ public class BeanWrapperFieldSetMapper extends DefaultPropertyEditorRegistrar im
/**
* Public setter for the type of bean to create instead of using a prototype
* bean. An object of this type will be created from its default constructor
* for every call to {@link #mapLine(FieldSet)}.<br/>
* for every call to {@link #mapLine(FieldSet, int)}.<br/>
*
* Either this property or the prototype bean name must be specified, but
* not both.
@@ -153,10 +153,10 @@ public class BeanWrapperFieldSetMapper extends DefaultPropertyEditorRegistrar im
* the {@link DataBinder} from {@link #createBinder(Object)} has errors
* after binding).
*
* @see org.springframework.batch.item.file.mapping.FieldSetMapper#mapLine(org.springframework.batch.item.file.mapping.FieldSet)
* @see org.springframework.batch.item.file.mapping.FieldSetMapper#mapLine(org.springframework.batch.item.file.mapping.FieldSet, int)
*/
@SuppressWarnings("unchecked")
public Object mapLine(FieldSet fs) {
public Object mapLine(FieldSet fs, int lineNum) {
Object copy = getBean();
DataBinder binder = createBinder(copy);
binder.bind(new MutablePropertyValues(getBeanProperties(copy, fs.getProperties())));

View File

@@ -30,6 +30,7 @@ public interface FieldSetMapper {
* Method used to map data obtained from a {@link FieldSet} into an object.
*
* @param fs the {@link FieldSet} to map
* @param lineNum the current line number (if known), or negative if not
*/
public Object mapLine(FieldSet fs);
public Object mapLine(FieldSet fs, int lineNum);
}

View File

@@ -29,7 +29,7 @@ public class PassThroughFieldSetMapper implements FieldSetMapper, FieldSetCreato
* (non-Javadoc)
* @see org.springframework.batch.io.file.FieldSetMapper#mapLine(org.springframework.batch.io.file.FieldSet)
*/
public Object mapLine(FieldSet fs) {
public Object mapLine(FieldSet fs, int lineNum) {
return fs;
}

View File

@@ -52,7 +52,7 @@ public class FlatFileItemReaderAdvancedTests extends TestCase {
};
private FieldSetMapper fieldSetMapper = new FieldSetMapper() {
public Object mapLine(FieldSet fs) {
public Object mapLine(FieldSet fs, int lineNum) {
return fs;
}
};

View File

@@ -18,6 +18,8 @@ package org.springframework.batch.item.file;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import junit.framework.TestCase;
@@ -59,7 +61,7 @@ public class FlatFileItemReaderBasicTests extends TestCase {
};
private FieldSetMapper fieldSetMapper = new FieldSetMapper() {
public Object mapLine(FieldSet fs) {
public Object mapLine(FieldSet fs, int lineNum) {
return fs;
}
};
@@ -105,6 +107,23 @@ public class FlatFileItemReaderBasicTests extends TestCase {
assertEquals(null, itemReader.read());
}
/**
* Regular usage of <code>read</code> method
*/
public void testReadWithLineNumber() throws Exception {
final List<Integer> list = new ArrayList<Integer>();
itemReader.setFieldSetMapper(new FieldSetMapper() {
public Object mapLine(FieldSet fs, int lineNum) {
list.add(lineNum);
return fs;
}
});
itemReader.open(executionContext);
assertEquals("[FlatFileInputTemplate-TestData]", itemReader.read().toString());
assertEquals(new Integer(1), list.get(0));
assertEquals(null, itemReader.read());
}
/**
* Regular usage of <code>read</code> method
*/
@@ -126,7 +145,7 @@ public class FlatFileItemReaderBasicTests extends TestCase {
public void testReadWithMapperError() throws Exception {
itemReader.setFieldSetMapper(new FieldSetMapper() {
public Object mapLine(FieldSet fs) {
public Object mapLine(FieldSet fs, int lineNum) {
throw new RuntimeException("foo");
}
});

View File

@@ -18,7 +18,7 @@ public class FlatFileItemReaderCommonTests extends CommonItemStreamItemReaderTes
Resource resource = new ByteArrayResource(FOOS.getBytes());
tested.setResource(resource);
tested.setFieldSetMapper(new FieldSetMapper() {
public Object mapLine(FieldSet fs) {
public Object mapLine(FieldSet fs, int lineNum) {
Foo foo = new Foo();
foo.setValue(fs.readInt(0));
return foo;

View File

@@ -20,7 +20,7 @@ public class MultiResourceItemReaderFlatFileTests extends
FlatFileItemReader fileReader = new FlatFileItemReader();
fileReader.setFieldSetMapper(new FieldSetMapper() {
public Object mapLine(FieldSet fs) {
public Object mapLine(FieldSet fs, int lineNum) {
Foo foo = new Foo();
foo.setValue(fs.readInt(0));
return foo;

View File

@@ -63,7 +63,7 @@ public class BeanWrapperFieldSetMapperTests extends TestCase {
FieldSet fieldSet = new DefaultFieldSet(new String[] { "This is some dummy string", "true", "C" },
new String[] { "varString", "varBoolean", "varChar" });
TestObject result = (TestObject) mapper.mapLine(fieldSet);
TestObject result = (TestObject) mapper.mapLine(fieldSet, -1);
assertEquals("This is some dummy string", result.getVarString());
assertEquals(true, result.isVarBoolean());
assertEquals('C', result.getVarChar());
@@ -78,7 +78,7 @@ public class BeanWrapperFieldSetMapperTests extends TestCase {
FieldSet fieldSet = new DefaultFieldSet(new String[] { "This is some dummy string", "true", "C" },
new String[] { "varString", "varBoolean", "varChar" });
TestObject result = (TestObject) mapper.mapLine(fieldSet);
TestObject result = (TestObject) mapper.mapLine(fieldSet, -1);
assertEquals("This is some dummy string", result.getVarString());
assertEquals(true, result.isVarBoolean());
assertEquals('C', result.getVarChar());
@@ -93,7 +93,7 @@ public class BeanWrapperFieldSetMapperTests extends TestCase {
FieldSet fieldSet = new DefaultFieldSet(new String[] { "This is some dummy string", "true", "C" },
new String[] { "VarString", "VAR_BOOLEAN", "VAR_CHAR" });
TestObject result = (TestObject) mapper.mapLine(fieldSet);
TestObject result = (TestObject) mapper.mapLine(fieldSet, -1);
assertEquals("This is some dummy string", result.getVarString());
assertEquals(true, result.isVarBoolean());
assertEquals('C', result.getVarChar());
@@ -106,7 +106,7 @@ public class BeanWrapperFieldSetMapperTests extends TestCase {
FieldSet fieldSet = new DefaultFieldSet(new String[] { "This is some dummy string", "true", "C" },
new String[] { "varString", "varBoolean", "varChar" });
TestObject result = (TestObject) mapper.mapLine(fieldSet);
TestObject result = (TestObject) mapper.mapLine(fieldSet, -1);
assertEquals("This is some dummy string", result.getVarString());
assertEquals(true, result.isVarBoolean());
assertEquals('C', result.getVarChar());
@@ -129,7 +129,7 @@ public class BeanWrapperFieldSetMapperTests extends TestCase {
new String[] { "This is some dummy string", "1", "Another dummy", "2" }, new String[] { "valueA",
"valueB", "testObjectB.valueA", "testObjectB.testObjectC.value" });
TestNestedA result = (TestNestedA) mapper.mapLine(fieldSet);
TestNestedA result = (TestNestedA) mapper.mapLine(fieldSet, -1);
assertEquals("This is some dummy string", result.getValueA());
assertEquals(1, result.getValueB());
@@ -149,7 +149,7 @@ public class BeanWrapperFieldSetMapperTests extends TestCase {
FieldSet fieldSet = new DefaultFieldSet(new String[] { "This is some dummy string", "1" }, new String[] {
"VALUE_A", "VALUE_B" });
TestNestedA result = (TestNestedA) mapper.mapLine(fieldSet);
TestNestedA result = (TestNestedA) mapper.mapLine(fieldSet, -1);
assertEquals("This is some dummy string", result.getValueA());
assertEquals(1, result.getValueB());
@@ -166,7 +166,7 @@ public class BeanWrapperFieldSetMapperTests extends TestCase {
FieldSet fieldSet = new DefaultFieldSet(new String[] { "1" }, new String[] { "foo" });
TestNestedC result = (TestNestedC) mapper.mapLine(fieldSet);
TestNestedC result = (TestNestedC) mapper.mapLine(fieldSet, -1);
// "foo" is similar enough to "value" that it matches - but only because
// nothing else does...
@@ -188,7 +188,7 @@ public class BeanWrapperFieldSetMapperTests extends TestCase {
FieldSet fieldSet = new DefaultFieldSet(new String[] { "Another dummy", "2" }, new String[] {
"TestObjectB.ValueA", "TestObjectB.TestObjectC.Value" });
TestNestedA result = (TestNestedA) mapper.mapLine(fieldSet);
TestNestedA result = (TestNestedA) mapper.mapLine(fieldSet, -1);
assertEquals("Another dummy", result.getTestObjectB().getValueA());
assertEquals(2, result.getTestObjectB().getTestObjectC().getValue());
@@ -208,7 +208,7 @@ public class BeanWrapperFieldSetMapperTests extends TestCase {
FieldSet fieldSet = new DefaultFieldSet(new String[] { "Another dummy" }, new String[] { "TestObjectB.foo" });
try {
mapper.mapLine(fieldSet);
mapper.mapLine(fieldSet, -1);
fail("Expected NotWritablePropertyException");
}
catch (NotWritablePropertyException e) {
@@ -230,7 +230,7 @@ public class BeanWrapperFieldSetMapperTests extends TestCase {
FieldSet fieldSet = new DefaultFieldSet(new String[] { "2" }, new String[] { "TestObjectA.garbage" });
try {
mapper.mapLine(fieldSet);
mapper.mapLine(fieldSet, -1);
fail("Expected NotWritablePropertyException");
}
catch (NotWritablePropertyException e) {
@@ -270,7 +270,7 @@ public class BeanWrapperFieldSetMapperTests extends TestCase {
FieldSet fieldSet = new DefaultFieldSet(new String[] { "1", "2", "3" }, new String[] { "NestedC[0].Value",
"NestedC[1].Value", "NestedC[2].Value" });
mapper.mapLine(fieldSet);
mapper.mapLine(fieldSet, -1);
assertEquals(1, ((TestNestedC) nestedList.getNestedC().get(0)).getValue());
assertEquals(2, ((TestNestedC) nestedList.getNestedC().get(1)).getValue());
@@ -284,7 +284,7 @@ public class BeanWrapperFieldSetMapperTests extends TestCase {
mapper.setTargetType(TestObject.class);
FieldSet fieldSet = new DefaultFieldSet(new String[] { "00009" }, new String[] { "varLong" });
TestObject bean = (TestObject) mapper.mapLine(fieldSet);
TestObject bean = (TestObject) mapper.mapLine(fieldSet, -1);
// since Spring 2.5.5 this is OK (before that BATCH-261)
assertEquals(9, bean.getVarLong());
}
@@ -298,7 +298,7 @@ public class BeanWrapperFieldSetMapperTests extends TestCase {
mapper.setCustomEditors(Collections.singletonMap(Long.TYPE, new CustomNumberEditor(Long.class, NumberFormat
.getNumberInstance(), true)));
TestObject bean = (TestObject) mapper.mapLine(fieldSet);
TestObject bean = (TestObject) mapper.mapLine(fieldSet, -1);
assertEquals(9, bean.getVarLong());
}
@@ -312,7 +312,7 @@ public class BeanWrapperFieldSetMapperTests extends TestCase {
mapper.setCustomEditors(Collections.singletonMap(Long.TYPE, new CustomNumberEditor(Long.class, NumberFormat
.getNumberInstance(), true)));
TestObject bean = (TestObject) mapper.mapLine(fieldSet);
TestObject bean = (TestObject) mapper.mapLine(fieldSet, -1);
assertEquals(9, bean.getVarLong());
assertEquals(78, bean.getVarInt());

View File

@@ -27,11 +27,11 @@ public class PassThroughFieldSetMapperTests extends TestCase {
/**
* Test method for
* {@link org.springframework.batch.item.file.mapping.PassThroughFieldSetMapper#mapLine(org.springframework.batch.item.file.mapping.FieldSet)}.
* {@link org.springframework.batch.item.file.mapping.PassThroughFieldSetMapper#mapLine(org.springframework.batch.item.file.mapping.FieldSet, int)}.
*/
public void testMapLine() {
FieldSet fieldSet = new DefaultFieldSet(new String[] { "foo", "bar" });
assertEquals(fieldSet, mapper.mapLine(fieldSet));
assertEquals(fieldSet, mapper.mapLine(fieldSet, -1));
}
/**

View File

@@ -60,7 +60,7 @@ public abstract class AbstractTradeBatchTests extends TestCase {
}
protected static class TradeMapper implements FieldSetMapper{
public Object mapLine(FieldSet fs) {
public Object mapLine(FieldSet fs, int lineNum) {
return new Trade(fs);
}
}