IN PROGRESS - BATCH-712: Upgrade ItemReaders to use Parameterized types
This commit is contained in:
@@ -21,9 +21,10 @@ import java.util.ArrayList;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.file.mapping.FieldSet;
|
||||
import org.springframework.batch.item.file.mapping.FieldSetMapper;
|
||||
import org.springframework.batch.item.support.DelegatingItemReader;
|
||||
import org.springframework.batch.item.support.AbstractItemReader;
|
||||
import org.springframework.batch.sample.domain.Address;
|
||||
import org.springframework.batch.sample.domain.BillingInfo;
|
||||
import org.springframework.batch.sample.domain.Customer;
|
||||
@@ -35,7 +36,7 @@ import org.springframework.batch.sample.domain.ShippingInfo;
|
||||
* @author peter.zozom
|
||||
*
|
||||
*/
|
||||
public class OrderItemReader extends DelegatingItemReader<Order> {
|
||||
public class OrderItemReader extends AbstractItemReader<Order> {
|
||||
private static Log log = LogFactory.getLog(OrderItemReader.class);
|
||||
|
||||
private Order order;
|
||||
@@ -53,6 +54,8 @@ public class OrderItemReader extends DelegatingItemReader<Order> {
|
||||
private FieldSetMapper<LineItem> itemMapper;
|
||||
|
||||
private FieldSetMapper<ShippingInfo> shippingMapper;
|
||||
|
||||
private ItemReader<FieldSet> fieldSetReader;
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
@@ -62,7 +65,7 @@ public class OrderItemReader extends DelegatingItemReader<Order> {
|
||||
recordFinished = false;
|
||||
|
||||
while (!recordFinished) {
|
||||
process((FieldSet) super.read());
|
||||
process((FieldSet) fieldSetReader.read());
|
||||
}
|
||||
|
||||
log.info("Mapped: " + order);
|
||||
@@ -172,6 +175,13 @@ public class OrderItemReader extends DelegatingItemReader<Order> {
|
||||
log.debug("Could not map LINE_ID=" + lineId);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param fieldSetReader reads lines from the file converting them to {@link FieldSet}.
|
||||
*/
|
||||
public void setFieldSetReader(ItemReader<FieldSet> fieldSetReader) {
|
||||
this.fieldSetReader = fieldSetReader;
|
||||
}
|
||||
|
||||
public void setAddressMapper(FieldSetMapper<Address> addressMapper) {
|
||||
this.addressMapper = addressMapper;
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
<property name="itemReader">
|
||||
<bean
|
||||
class="org.springframework.batch.sample.item.reader.OrderItemReader">
|
||||
<property name="itemReader"
|
||||
<property name="fieldSetReader"
|
||||
ref="fileItemReader" />
|
||||
<property name="headerMapper"
|
||||
ref="headerFieldSetMapper" />
|
||||
|
||||
@@ -19,43 +19,49 @@ import org.springframework.batch.sample.domain.ShippingInfo;
|
||||
public class OrderItemReaderTests extends TestCase {
|
||||
|
||||
private OrderItemReader provider;
|
||||
|
||||
private MockControl inputControl;
|
||||
private ItemReader input;
|
||||
|
||||
private ItemReader<FieldSet> input;
|
||||
|
||||
private MockControl mapperControl;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private FieldSetMapper mapper;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void setUp() {
|
||||
|
||||
inputControl = MockControl.createControl(ItemReader.class);
|
||||
input = (ItemReader)inputControl.getMock();
|
||||
input = (ItemReader<FieldSet>) inputControl.getMock();
|
||||
|
||||
provider = new OrderItemReader();
|
||||
provider.setItemReader(input);
|
||||
provider.setFieldSetReader(input);
|
||||
}
|
||||
|
||||
/*
|
||||
* OrderItemProvider is resposible for retrieving validated value object from input source.
|
||||
* OrderItemProvider.next():
|
||||
* - reads lines from the input source - returned as fieldsets
|
||||
* - pass fieldsets to the mapper - mapper will create value object
|
||||
* - pass value object to validator
|
||||
* - returns validated object
|
||||
*
|
||||
* In testNext method we are going to test these responsibilities. So we need create mock
|
||||
* objects for input source, mapper and validator.
|
||||
* OrderItemProvider is resposible for retrieving validated value object
|
||||
* from input source. OrderItemProvider.next(): - reads lines from the input
|
||||
* source - returned as fieldsets - pass fieldsets to the mapper - mapper
|
||||
* will create value object - pass value object to validator - returns
|
||||
* validated object
|
||||
*
|
||||
* In testNext method we are going to test these responsibilities. So we
|
||||
* need create mock objects for input source, mapper and validator.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testNext() throws Exception {
|
||||
|
||||
//create fieldsets and set return values for input source
|
||||
FieldSet headerFS = new DefaultFieldSet(new String[] {Order.LINE_ID_HEADER});
|
||||
FieldSet customerFS = new DefaultFieldSet(new String[] {Customer.LINE_ID_NON_BUSINESS_CUST});
|
||||
FieldSet billingFS = new DefaultFieldSet(new String[] {Address.LINE_ID_BILLING_ADDR});
|
||||
FieldSet shippingFS = new DefaultFieldSet(new String[] {Address.LINE_ID_SHIPPING_ADDR});
|
||||
FieldSet billingInfoFS = new DefaultFieldSet(new String[] {BillingInfo.LINE_ID_BILLING_INFO});
|
||||
FieldSet shippingInfoFS = new DefaultFieldSet(new String[] {ShippingInfo.LINE_ID_SHIPPING_INFO});
|
||||
FieldSet itemFS = new DefaultFieldSet(new String[] {LineItem.LINE_ID_ITEM});
|
||||
FieldSet footerFS = new DefaultFieldSet(new String[] {Order.LINE_ID_FOOTER, "100","3","3"},
|
||||
new String[] {"ID","TOTAL_PRICE","TOTAL_LINE_ITEMS","TOTAL_ITEMS"});
|
||||
// create fieldsets and set return values for input source
|
||||
FieldSet headerFS = new DefaultFieldSet(new String[] { Order.LINE_ID_HEADER });
|
||||
FieldSet customerFS = new DefaultFieldSet(new String[] { Customer.LINE_ID_NON_BUSINESS_CUST });
|
||||
FieldSet billingFS = new DefaultFieldSet(new String[] { Address.LINE_ID_BILLING_ADDR });
|
||||
FieldSet shippingFS = new DefaultFieldSet(new String[] { Address.LINE_ID_SHIPPING_ADDR });
|
||||
FieldSet billingInfoFS = new DefaultFieldSet(new String[] { BillingInfo.LINE_ID_BILLING_INFO });
|
||||
FieldSet shippingInfoFS = new DefaultFieldSet(new String[] { ShippingInfo.LINE_ID_SHIPPING_INFO });
|
||||
FieldSet itemFS = new DefaultFieldSet(new String[] { LineItem.LINE_ID_ITEM });
|
||||
FieldSet footerFS = new DefaultFieldSet(new String[] { Order.LINE_ID_FOOTER, "100", "3", "3" }, new String[] {
|
||||
"ID", "TOTAL_PRICE", "TOTAL_LINE_ITEMS", "TOTAL_ITEMS" });
|
||||
|
||||
input.read();
|
||||
inputControl.setReturnValue(headerFS);
|
||||
@@ -70,14 +76,14 @@ public class OrderItemReaderTests extends TestCase {
|
||||
input.read();
|
||||
inputControl.setReturnValue(shippingInfoFS);
|
||||
input.read();
|
||||
inputControl.setReturnValue(itemFS,3);
|
||||
inputControl.setReturnValue(itemFS, 3);
|
||||
input.read();
|
||||
inputControl.setReturnValue(footerFS);
|
||||
input.read();
|
||||
inputControl.setReturnValue(null);
|
||||
inputControl.replay();
|
||||
|
||||
//create value objects
|
||||
// create value objects
|
||||
Order order = new Order();
|
||||
Customer customer = new Customer();
|
||||
Address billing = new Address();
|
||||
@@ -86,10 +92,10 @@ public class OrderItemReaderTests extends TestCase {
|
||||
ShippingInfo shippingInfo = new ShippingInfo();
|
||||
LineItem item = new LineItem();
|
||||
|
||||
//create mock mapper
|
||||
// create mock mapper
|
||||
mapperControl = MockControl.createControl(FieldSetMapper.class);
|
||||
mapper = (FieldSetMapper)mapperControl.getMock();
|
||||
//set how mapper should respond - set return values for mapper
|
||||
mapper = (FieldSetMapper) mapperControl.getMock();
|
||||
// set how mapper should respond - set return values for mapper
|
||||
mapper.mapLine(headerFS, -1);
|
||||
mapperControl.setReturnValue(order);
|
||||
mapper.mapLine(customerFS, -1);
|
||||
@@ -103,11 +109,10 @@ public class OrderItemReaderTests extends TestCase {
|
||||
mapper.mapLine(shippingInfoFS, -1);
|
||||
mapperControl.setReturnValue(shippingInfo);
|
||||
mapper.mapLine(itemFS, -1);
|
||||
mapperControl.setReturnValue(item,3);
|
||||
mapperControl.setReturnValue(item, 3);
|
||||
mapperControl.replay();
|
||||
|
||||
|
||||
//set-up provider: set mappers
|
||||
// set-up provider: set mappers
|
||||
provider.setAddressMapper(mapper);
|
||||
provider.setBillingMapper(mapper);
|
||||
provider.setCustomerMapper(mapper);
|
||||
@@ -115,35 +120,35 @@ public class OrderItemReaderTests extends TestCase {
|
||||
provider.setItemMapper(mapper);
|
||||
provider.setShippingMapper(mapper);
|
||||
|
||||
//call tested method
|
||||
// call tested method
|
||||
Object result = provider.read();
|
||||
|
||||
//verify result
|
||||
// verify result
|
||||
assertNotNull(result);
|
||||
//result should be Order
|
||||
// result should be Order
|
||||
assertTrue(result instanceof Order);
|
||||
|
||||
//verify whether order is constructed correctly
|
||||
//Order object should contain same instances as returned by mapper
|
||||
// verify whether order is constructed correctly
|
||||
// Order object should contain same instances as returned by mapper
|
||||
Order o = (Order) result;
|
||||
assertEquals(o,order);
|
||||
assertEquals(o.getCustomer(),customer);
|
||||
//is it non-bussines customer
|
||||
assertEquals(o, order);
|
||||
assertEquals(o.getCustomer(), customer);
|
||||
// is it non-bussines customer
|
||||
assertFalse(o.getCustomer().isBusinessCustomer());
|
||||
assertEquals(o.getBillingAddress(),billing);
|
||||
assertEquals(o.getShippingAddress(),shipping);
|
||||
assertEquals(o.getBilling(),billingInfo);
|
||||
assertEquals(o.getBillingAddress(), billing);
|
||||
assertEquals(o.getShippingAddress(), shipping);
|
||||
assertEquals(o.getBilling(), billingInfo);
|
||||
assertEquals(o.getShipping(), shippingInfo);
|
||||
//there should be 3 line items
|
||||
// there should be 3 line items
|
||||
assertEquals(3, o.getLineItems().size());
|
||||
for (Iterator<?> i = o.getLineItems().iterator(); i.hasNext();) {
|
||||
assertEquals(i.next(),item);
|
||||
assertEquals(i.next(), item);
|
||||
}
|
||||
|
||||
//try to retrieve next object - nothing should be returned
|
||||
// try to retrieve next object - nothing should be returned
|
||||
assertNull(provider.read());
|
||||
|
||||
//verify method calls on input source, mapper and validator
|
||||
// verify method calls on input source, mapper and validator
|
||||
inputControl.verify();
|
||||
mapperControl.verify();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user