RESOLVED - issue BATCH-640: FieldSetMapper.mapLine() should contain the line number
Added rownum parameter to field set mapper (with default value -1 defined as constant in FieldSetMapper)
This commit is contained in:
@@ -179,13 +179,13 @@ public class FlatFileItemReader extends ExecutionContextUserSupport implements R
|
||||
String line = readLine();
|
||||
|
||||
if (line != null) {
|
||||
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
|
||||
int lineCount = getReader().getPosition();
|
||||
throw new FlatFileParseException("Parsing error at line: " + lineCount + " in resource="
|
||||
+ resource.getDescription() + ", input=[" + line + "]", ex, line, lineCount);
|
||||
}
|
||||
|
||||
@@ -99,7 +99,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.
|
||||
*
|
||||
@@ -115,7 +115,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.
|
||||
@@ -150,9 +150,9 @@ 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)
|
||||
*/
|
||||
public Object mapLine(FieldSet fs) {
|
||||
public Object mapLine(FieldSet fs, int rownum) {
|
||||
Object copy = getBean();
|
||||
DataBinder binder = createBinder(copy);
|
||||
binder.bind(new MutablePropertyValues(getBeanProperties(copy, fs.getProperties())));
|
||||
|
||||
@@ -16,18 +16,31 @@
|
||||
|
||||
package org.springframework.batch.item.file.mapping;
|
||||
|
||||
|
||||
/**
|
||||
* Interface that is used to map data obtained from a file into an object.
|
||||
* Interface that is used to map data obtained from a {@link FieldSet} into an
|
||||
* object.
|
||||
*
|
||||
* @author tomas.slanina
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public interface FieldSetMapper {
|
||||
|
||||
|
||||
/**
|
||||
* Method used to map data obtained from a file into an object.
|
||||
* Constant (negative) value indicating an unknown row number.
|
||||
*/
|
||||
public Object mapLine(FieldSet fs);
|
||||
public final static int ROW_NUMBER_UNKNOWN = -1;
|
||||
|
||||
/**
|
||||
* Method used to map data obtained from a {@link FieldSet} into an object.
|
||||
* Implementations can do whatever they need to to convert the input into an
|
||||
* object of the desired type. Often the row number is not used, and
|
||||
* sometimes it is not available anyway, in which case its value will be
|
||||
* {@link #ROW_NUMBER_UNKNOWN}.
|
||||
*
|
||||
* @param fs the {@link FieldSet} to map
|
||||
* @param rownum the row number for the field set in the input source (if
|
||||
* known)
|
||||
*/
|
||||
public Object mapLine(FieldSet fs, int rownum);
|
||||
}
|
||||
|
||||
@@ -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 rownum) {
|
||||
return fs;
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ public class MultiResourceItemReaderFlatFileTests extends CommonItemStreamItemRe
|
||||
FlatFileItemReader fileReader = new FlatFileItemReader();
|
||||
|
||||
fileReader.setFieldSetMapper(new FieldSetMapper() {
|
||||
public Object mapLine(FieldSet fs) {
|
||||
public Object mapLine(FieldSet fs, int rownum) {
|
||||
Foo foo = new Foo();
|
||||
foo.setValue(fs.readInt(0));
|
||||
return foo;
|
||||
|
||||
@@ -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 rownum) {
|
||||
return fs;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -59,7 +59,7 @@ public class FlatFileItemReaderBasicTests extends TestCase {
|
||||
};
|
||||
|
||||
private FieldSetMapper fieldSetMapper = new FieldSetMapper() {
|
||||
public Object mapLine(FieldSet fs) {
|
||||
public Object mapLine(FieldSet fs, int rownum) {
|
||||
return fs;
|
||||
}
|
||||
};
|
||||
@@ -126,7 +126,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 rownum) {
|
||||
throw new RuntimeException("foo");
|
||||
}
|
||||
});
|
||||
|
||||
@@ -17,7 +17,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 rownum) {
|
||||
Foo foo = new Foo();
|
||||
foo.setValue(fs.readInt(0));
|
||||
return foo;
|
||||
|
||||
@@ -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, FieldSetMapper.ROW_NUMBER_UNKNOWN);
|
||||
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, FieldSetMapper.ROW_NUMBER_UNKNOWN);
|
||||
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, FieldSetMapper.ROW_NUMBER_UNKNOWN);
|
||||
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, FieldSetMapper.ROW_NUMBER_UNKNOWN);
|
||||
assertEquals("This is some dummy string", result.getVarString());
|
||||
assertEquals(true, result.isVarBoolean());
|
||||
assertEquals('C', result.getVarChar());
|
||||
@@ -128,7 +128,7 @@ public class BeanWrapperFieldSetMapperTests extends TestCase {
|
||||
FieldSet fieldSet = new DefaultFieldSet(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, FieldSetMapper.ROW_NUMBER_UNKNOWN);
|
||||
|
||||
assertEquals("This is some dummy string", result.getValueA());
|
||||
assertEquals(1, result.getValueB());
|
||||
@@ -148,7 +148,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, FieldSetMapper.ROW_NUMBER_UNKNOWN);
|
||||
|
||||
assertEquals("This is some dummy string", result.getValueA());
|
||||
assertEquals(1, result.getValueB());
|
||||
@@ -165,7 +165,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, FieldSetMapper.ROW_NUMBER_UNKNOWN);
|
||||
|
||||
// "foo" is similar enough to "value" that it matches - but only because
|
||||
// nothing else does...
|
||||
@@ -187,7 +187,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, FieldSetMapper.ROW_NUMBER_UNKNOWN);
|
||||
|
||||
assertEquals("Another dummy", result.getTestObjectB().getValueA());
|
||||
assertEquals(2, result.getTestObjectB().getTestObjectC().getValue());
|
||||
@@ -207,7 +207,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, FieldSetMapper.ROW_NUMBER_UNKNOWN);
|
||||
fail("Expected NotWritablePropertyException");
|
||||
}
|
||||
catch (NotWritablePropertyException e) {
|
||||
@@ -229,7 +229,7 @@ public class BeanWrapperFieldSetMapperTests extends TestCase {
|
||||
FieldSet fieldSet = new DefaultFieldSet(new String[] { "2" }, new String[] { "TestObjectA.garbage" });
|
||||
|
||||
try {
|
||||
mapper.mapLine(fieldSet);
|
||||
mapper.mapLine(fieldSet, FieldSetMapper.ROW_NUMBER_UNKNOWN);
|
||||
fail("Expected NotWritablePropertyException");
|
||||
}
|
||||
catch (NotWritablePropertyException e) {
|
||||
@@ -269,7 +269,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, FieldSetMapper.ROW_NUMBER_UNKNOWN);
|
||||
|
||||
assertEquals(1, ((TestNestedC) nestedList.getNestedC().get(0)).getValue());
|
||||
assertEquals(2, ((TestNestedC) nestedList.getNestedC().get(1)).getValue());
|
||||
@@ -285,7 +285,7 @@ public class BeanWrapperFieldSetMapperTests extends TestCase {
|
||||
FieldSet fieldSet = new DefaultFieldSet(new String[] { "00009" }, new String[] { "varLong" });
|
||||
|
||||
try {
|
||||
mapper.mapLine(fieldSet);
|
||||
mapper.mapLine(fieldSet, FieldSetMapper.ROW_NUMBER_UNKNOWN);
|
||||
fail("Expected BindingException");
|
||||
}
|
||||
catch (BindingException e) {
|
||||
@@ -302,7 +302,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, FieldSetMapper.ROW_NUMBER_UNKNOWN);
|
||||
|
||||
assertEquals(9, bean.getVarLong());
|
||||
}
|
||||
@@ -316,7 +316,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, FieldSetMapper.ROW_NUMBER_UNKNOWN);
|
||||
|
||||
assertEquals(9, bean.getVarLong());
|
||||
assertEquals(78, bean.getVarInt());
|
||||
|
||||
@@ -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, FieldSetMapper.ROW_NUMBER_UNKNOWN));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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 rownum) {
|
||||
return new Trade(fs);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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, FieldSetMapper.ROW_NUMBER_UNKNOWN);
|
||||
|
||||
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, FieldSetMapper.ROW_NUMBER_UNKNOWN));
|
||||
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, FieldSetMapper.ROW_NUMBER_UNKNOWN));
|
||||
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, FieldSetMapper.ROW_NUMBER_UNKNOWN));
|
||||
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, FieldSetMapper.ROW_NUMBER_UNKNOWN));
|
||||
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, FieldSetMapper.ROW_NUMBER_UNKNOWN));
|
||||
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, FieldSetMapper.ROW_NUMBER_UNKNOWN));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -165,7 +165,7 @@ public class OrderItemReader extends DelegatingItemReader {
|
||||
order.setLineItems(new ArrayList());
|
||||
}
|
||||
|
||||
order.getLineItems().add(itemMapper.mapLine(fieldSet));
|
||||
order.getLineItems().add(itemMapper.mapLine(fieldSet, FieldSetMapper.ROW_NUMBER_UNKNOWN));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -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 rownum) {
|
||||
Address address = new Address();
|
||||
|
||||
address.setAddressee(fieldSet.readString(ADDRESSEE_COLUMN));
|
||||
|
||||
@@ -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 rownum) {
|
||||
BillingInfo info = new BillingInfo();
|
||||
|
||||
info.setPaymentId(fieldSet.readString(PAYMENT_TYPE_ID_COLUMN));
|
||||
|
||||
@@ -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 rownum) {
|
||||
Customer customer = new Customer();
|
||||
|
||||
if (Customer.LINE_ID_BUSINESS_CUST.equals(fieldSet.readString(LINE_ID_COLUMN))) {
|
||||
|
||||
@@ -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 rownum) {
|
||||
|
||||
if(fs == null){
|
||||
return null;
|
||||
|
||||
@@ -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 rownum) {
|
||||
Order order = new Order();
|
||||
order.setOrderId(fieldSet.readLong(ORDER_ID_COLUMN));
|
||||
order.setOrderDate(fieldSet.readDate(ORDER_DATE_COLUMN));
|
||||
|
||||
@@ -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 rownum) {
|
||||
LineItem item = new LineItem();
|
||||
|
||||
item.setItemId(fieldSet.readLong(ITEM_ID_COLUMN));
|
||||
|
||||
@@ -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 rownum) {
|
||||
|
||||
if(fs == null){
|
||||
return null;
|
||||
|
||||
@@ -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 rownum) {
|
||||
ShippingInfo info = new ShippingInfo();
|
||||
|
||||
info.setShipperId(fieldSet.readString(SHIPPER_ID_COLUMN));
|
||||
|
||||
@@ -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 rownum) {
|
||||
|
||||
if ("BEGIN".equals(fieldSet.readString(0))) {
|
||||
return AggregateItemReader.BEGIN_RECORD;
|
||||
|
||||
@@ -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, FieldSetMapper.ROW_NUMBER_UNKNOWN);
|
||||
mapperControl.setReturnValue(order);
|
||||
mapper.mapLine(customerFS);
|
||||
mapper.mapLine(customerFS, FieldSetMapper.ROW_NUMBER_UNKNOWN);
|
||||
mapperControl.setReturnValue(customer);
|
||||
mapper.mapLine(billingFS);
|
||||
mapper.mapLine(billingFS, FieldSetMapper.ROW_NUMBER_UNKNOWN);
|
||||
mapperControl.setReturnValue(billing);
|
||||
mapper.mapLine(shippingFS);
|
||||
mapper.mapLine(shippingFS, FieldSetMapper.ROW_NUMBER_UNKNOWN);
|
||||
mapperControl.setReturnValue(shipping);
|
||||
mapper.mapLine(billingInfoFS);
|
||||
mapper.mapLine(billingInfoFS, FieldSetMapper.ROW_NUMBER_UNKNOWN);
|
||||
mapperControl.setReturnValue(billingInfo);
|
||||
mapper.mapLine(shippingInfoFS);
|
||||
mapper.mapLine(shippingInfoFS, FieldSetMapper.ROW_NUMBER_UNKNOWN);
|
||||
mapperControl.setReturnValue(shippingInfo);
|
||||
mapper.mapLine(itemFS);
|
||||
mapper.mapLine(itemFS, FieldSetMapper.ROW_NUMBER_UNKNOWN);
|
||||
mapperControl.setReturnValue(item,3);
|
||||
mapperControl.replay();
|
||||
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package org.springframework.batch.sample.mapping;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.batch.item.file.mapping.FieldSet;
|
||||
import org.springframework.batch.item.file.mapping.FieldSetMapper;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* Encapsulates basic logic for testing custom {@link FieldSetMapper} implementations.
|
||||
*
|
||||
@@ -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(), FieldSetMapper.ROW_NUMBER_UNKNOWN));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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"}), FieldSetMapper.ROW_NUMBER_UNKNOWN));
|
||||
}
|
||||
|
||||
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"}), FieldSetMapper.ROW_NUMBER_UNKNOWN));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user