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

View File

@@ -1,4 +1,4 @@
#Wed Jul 16 13:31:46 CEST 2008
#Thu Jul 17 10:38:08 BST 2008
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5

View File

@@ -91,7 +91,7 @@ public class OrderItemReader extends DelegatingItemReader {
// start a new Order
if (Order.LINE_ID_HEADER.equals(lineId)) {
log.debug("STARTING NEW RECORD");
order = (Order) headerMapper.mapLine(fieldSet);
order = (Order) headerMapper.mapLine(fieldSet, -1);
return;
}
@@ -116,7 +116,7 @@ public class OrderItemReader extends DelegatingItemReader {
log.debug("MAPPING CUSTOMER");
if (order.getCustomer() == null) {
order.setCustomer((Customer) customerMapper.mapLine(fieldSet));
order.setCustomer((Customer) customerMapper.mapLine(fieldSet, -1));
order.getCustomer().setBusinessCustomer(true);
}
@@ -127,7 +127,7 @@ public class OrderItemReader extends DelegatingItemReader {
log.debug("MAPPING CUSTOMER");
if (order.getCustomer() == null) {
order.setCustomer((Customer) customerMapper.mapLine(fieldSet));
order.setCustomer((Customer) customerMapper.mapLine(fieldSet, -1));
order.getCustomer().setBusinessCustomer(false);
}
@@ -136,25 +136,25 @@ public class OrderItemReader extends DelegatingItemReader {
if (Address.LINE_ID_BILLING_ADDR.equals(lineId)) {
log.debug("MAPPING BILLING ADDRESS");
order.setBillingAddress((Address) addressMapper.mapLine(fieldSet));
order.setBillingAddress((Address) addressMapper.mapLine(fieldSet, -1));
return;
}
if (Address.LINE_ID_SHIPPING_ADDR.equals(lineId)) {
log.debug("MAPPING SHIPPING ADDRESS");
order.setShippingAddress((Address) addressMapper.mapLine(fieldSet));
order.setShippingAddress((Address) addressMapper.mapLine(fieldSet, -1));
return;
}
if (BillingInfo.LINE_ID_BILLING_INFO.equals(lineId)) {
log.debug("MAPPING BILLING INFO");
order.setBilling((BillingInfo) billingMapper.mapLine(fieldSet));
order.setBilling((BillingInfo) billingMapper.mapLine(fieldSet, -1));
return;
}
if (ShippingInfo.LINE_ID_SHIPPING_INFO.equals(lineId)) {
log.debug("MAPPING SHIPPING INFO");
order.setShipping((ShippingInfo) shippingMapper.mapLine(fieldSet));
order.setShipping((ShippingInfo) shippingMapper.mapLine(fieldSet, -1));
return;
}
@@ -164,8 +164,7 @@ public class OrderItemReader extends DelegatingItemReader {
if (order.getLineItems() == null) {
order.setLineItems(new ArrayList<LineItem>());
}
order.getLineItems().add((LineItem) itemMapper.mapLine(fieldSet));
order.getLineItems().add((LineItem) itemMapper.mapLine(fieldSet, -1));
return;
}

View File

@@ -33,7 +33,7 @@ public class AddressFieldSetMapper implements FieldSetMapper {
public static final String COUNTRY_COLUMN = "COUNTRY";
public Object mapLine(FieldSet fieldSet) {
public Object mapLine(FieldSet fieldSet, int lineNum) {
Address address = new Address();
address.setAddressee(fieldSet.readString(ADDRESSEE_COLUMN));

View File

@@ -27,7 +27,7 @@ public class BillingFieldSetMapper implements FieldSetMapper {
public static final String PAYMENT_TYPE_ID_COLUMN = "PAYMENT_TYPE_ID";
public static final String PAYMENT_DESC_COLUMN = "PAYMENT_DESC";
public Object mapLine(FieldSet fieldSet) {
public Object mapLine(FieldSet fieldSet, int lineNum) {
BillingInfo info = new BillingInfo();
info.setPaymentId(fieldSet.readString(PAYMENT_TYPE_ID_COLUMN));

View File

@@ -34,7 +34,7 @@ public class CustomerFieldSetMapper implements FieldSetMapper {
public static final String REG_ID_COLUMN = "REG_ID";
public static final String VIP_COLUMN = "VIP";
public Object mapLine(FieldSet fieldSet) {
public Object mapLine(FieldSet fieldSet, int lineNum) {
Customer customer = new Customer();
if (Customer.LINE_ID_BUSINESS_CUST.equals(fieldSet.readString(LINE_ID_COLUMN))) {

View File

@@ -6,7 +6,7 @@ import org.springframework.batch.sample.domain.Game;
public class GameFieldSetMapper implements FieldSetMapper {
public Object mapLine(FieldSet fs) {
public Object mapLine(FieldSet fs, int lineNum) {
if(fs == null){
return null;

View File

@@ -27,7 +27,7 @@ public class HeaderFieldSetMapper implements FieldSetMapper {
public static final String ORDER_ID_COLUMN = "ORDER_ID";
public static final String ORDER_DATE_COLUMN = "ORDER_DATE";
public Object mapLine(FieldSet fieldSet) {
public Object mapLine(FieldSet fieldSet, int lineNum) {
Order order = new Order();
order.setOrderId(fieldSet.readLong(ORDER_ID_COLUMN));
order.setOrderDate(fieldSet.readDate(ORDER_DATE_COLUMN));

View File

@@ -33,7 +33,7 @@ public class OrderItemFieldSetMapper implements FieldSetMapper {
public static final String ITEM_ID_COLUMN = "ITEM_ID";
public Object mapLine(FieldSet fieldSet) {
public Object mapLine(FieldSet fieldSet, int lineNum) {
LineItem item = new LineItem();
item.setItemId(fieldSet.readLong(ITEM_ID_COLUMN));

View File

@@ -6,7 +6,7 @@ import org.springframework.batch.sample.domain.Player;
public class PlayerFieldSetMapper implements FieldSetMapper {
public Object mapLine(FieldSet fs) {
public Object mapLine(FieldSet fs, int lineNum) {
if(fs == null){
return null;

View File

@@ -28,7 +28,7 @@ public class ShippingFieldSetMapper implements FieldSetMapper {
public static final String SHIPPING_TYPE_ID_COLUMN = "SHIPPING_TYPE_ID";
public static final String SHIPPER_ID_COLUMN = "SHIPPER_ID";
public Object mapLine(FieldSet fieldSet) {
public Object mapLine(FieldSet fieldSet, int lineNum) {
ShippingInfo info = new ShippingInfo();
info.setShipperId(fieldSet.readString(SHIPPER_ID_COLUMN));

View File

@@ -30,7 +30,7 @@ public class TradeFieldSetMapper implements FieldSetMapper {
public static final int PRICE_COLUMN = 2;
public static final int CUSTOMER_COLUMN = 3;
public Object mapLine(FieldSet fieldSet) {
public Object mapLine(FieldSet fieldSet, int lineNum) {
if ("BEGIN".equals(fieldSet.readString(0))) {
return AggregateItemReader.BEGIN_RECORD;

View File

@@ -90,19 +90,19 @@ public class OrderItemReaderTests extends TestCase {
mapperControl = MockControl.createControl(FieldSetMapper.class);
mapper = (FieldSetMapper)mapperControl.getMock();
//set how mapper should respond - set return values for mapper
mapper.mapLine(headerFS);
mapper.mapLine(headerFS, -1);
mapperControl.setReturnValue(order);
mapper.mapLine(customerFS);
mapper.mapLine(customerFS, -1);
mapperControl.setReturnValue(customer);
mapper.mapLine(billingFS);
mapper.mapLine(billingFS, -1);
mapperControl.setReturnValue(billing);
mapper.mapLine(shippingFS);
mapper.mapLine(shippingFS, -1);
mapperControl.setReturnValue(shipping);
mapper.mapLine(billingInfoFS);
mapper.mapLine(billingInfoFS, -1);
mapperControl.setReturnValue(billingInfo);
mapper.mapLine(shippingInfoFS);
mapper.mapLine(shippingInfoFS, -1);
mapperControl.setReturnValue(shippingInfo);
mapper.mapLine(itemFS);
mapper.mapLine(itemFS, -1);
mapperControl.setReturnValue(item,3);
mapperControl.replay();

View File

@@ -35,7 +35,7 @@ public abstract class AbstractFieldSetMapperTests extends TestCase {
* Assumes the domain object implements sensible <code>equals(Object other)</code>
*/
public void testRegularUse() {
assertEquals(expectedDomainObject(), fieldSetMapper().mapLine(fieldSet()));
assertEquals(expectedDomainObject(), fieldSetMapper().mapLine(fieldSet(), -1));
}
}

View File

@@ -40,11 +40,11 @@ public class TradeFieldSetMapperTests extends AbstractFieldSetMapperTests{
}
public void testBeginRecord() throws Exception {
assertEquals(AggregateItemReader.BEGIN_RECORD, fieldSetMapper().mapLine(new DefaultFieldSet(new String[] {"BEGIN"})));
assertEquals(AggregateItemReader.BEGIN_RECORD, fieldSetMapper().mapLine(new DefaultFieldSet(new String[] {"BEGIN"}), -1));
}
public void testEndRecord() throws Exception {
assertEquals(AggregateItemReader.END_RECORD, fieldSetMapper().mapLine(new DefaultFieldSet(new String[] {"END"})));
assertEquals(AggregateItemReader.END_RECORD, fieldSetMapper().mapLine(new DefaultFieldSet(new String[] {"END"}), -1));
}
}