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

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