RESOLVED - issue BATCH-389: Two samples could make use of our ValidatingItemReader instead of DelegatingItemReader.

http://jira.springframework.org/browse/BATCH-389
This commit is contained in:
robokaso
2008-03-05 14:15:13 +00:00
parent 840d30426a
commit 69e3878501
3 changed files with 110 additions and 128 deletions

View File

@@ -24,7 +24,6 @@ import org.springframework.batch.core.domain.StepExecution;
import org.springframework.batch.io.file.mapping.FieldSet;
import org.springframework.batch.io.file.mapping.FieldSetMapper;
import org.springframework.batch.item.reader.DelegatingItemReader;
import org.springframework.batch.item.validator.Validator;
import org.springframework.batch.sample.domain.Address;
import org.springframework.batch.sample.domain.BillingInfo;
import org.springframework.batch.sample.domain.Customer;
@@ -55,8 +54,6 @@ public class OrderItemReader extends DelegatingItemReader {
private FieldSetMapper shippingMapper;
private Validator validator;
/**
* @throws Exception
* @see org.springframework.batch.item.ItemReader#read()
@@ -68,11 +65,8 @@ public class OrderItemReader extends DelegatingItemReader {
process((FieldSet) super.read());
}
if (order != null) {
log.info("Mapped: " + order);
validator.validate(order);
}
log.info("Mapped: " + order);
Object result = order;
order = null;
@@ -204,8 +198,4 @@ public class OrderItemReader extends DelegatingItemReader {
this.shippingMapper = shippingMapper;
}
public void setValidator(Validator validator) {
this.validator = validator;
}
}

View File

@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
@@ -21,67 +21,72 @@
</list>
</property>
<property name="itemReader">
<bean
class="org.springframework.batch.sample.item.reader.OrderItemReader">
<property name="itemReader"
ref="fileInputTemplate" />
<property name="headerMapper"
ref="headerFieldSetMapper" />
<property name="customerMapper"
ref="customerFieldSetMapper" />
<property name="addressMapper"
ref="addressFieldSetMapper" />
<property name="billingMapper"
ref="billingFieldSetMapper" />
<property name="itemMapper"
ref="orderItemFieldSetMapper" />
<property name="shippingMapper"
ref="shippingFieldSetMapper" />
<property name="validator" ref="validator" />
</bean>
</property>
<property name="itemWriter">
<bean
class="org.springframework.batch.sample.item.writer.OrderWriter">
<property name="delegate" ref="orderWriter" />
</bean>
</property>
</bean>
</property>
</bean>
<bean id="orderWriter"
class="org.springframework.batch.sample.dao.FlatFileOrderWriter">
<property name="delegate" ref="flatFileTransformerOutput" />
<property name="transformer">
<bean
class="org.springframework.batch.sample.dao.OrderTransformer">
<property name="aggregators" ref="outputDescriptors" />
</bean>
</property>
</bean>
<bean id="headerFieldSetMapper"
class="org.springframework.batch.sample.mapping.HeaderFieldSetMapper" />
<bean id="customerFieldSetMapper"
class="org.springframework.batch.sample.mapping.CustomerFieldSetMapper" />
<bean id="addressFieldSetMapper"
class="org.springframework.batch.sample.mapping.AddressFieldSetMapper" />
<bean id="billingFieldSetMapper"
class="org.springframework.batch.sample.mapping.BillingFieldSetMapper" />
<bean id="orderItemFieldSetMapper"
class="org.springframework.batch.sample.mapping.OrderItemFieldSetMapper" />
<bean id="shippingFieldSetMapper"
class="org.springframework.batch.sample.mapping.ShippingFieldSetMapper" />
<bean id="validator"
class="org.springframework.batch.item.validator.SpringValidator">
<property name="validator">
<bean id="orderValidator"
class="org.springmodules.validation.valang.ValangValidator">
<property name="valang">
<value>
<bean
class="org.springframework.batch.item.reader.ValidatingItemReader">
<property name="itemReader">
<bean
class="org.springframework.batch.sample.item.reader.OrderItemReader">
<property name="itemReader"
ref="fileInputTemplate" />
<property name="headerMapper"
ref="headerFieldSetMapper" />
<property name="customerMapper"
ref="customerFieldSetMapper" />
<property name="addressMapper"
ref="addressFieldSetMapper" />
<property name="billingMapper"
ref="billingFieldSetMapper" />
<property name="itemMapper"
ref="orderItemFieldSetMapper" />
<property name="shippingMapper"
ref="shippingFieldSetMapper" />
</bean>
</property>
<property name="validator" ref="validator" />
</bean>
</property>
<property name="itemWriter">
<bean
class="org.springframework.batch.sample.item.writer.OrderWriter">
<property name="delegate" ref="orderWriter" />
</bean>
</property>
</bean>
</property>
</bean>
<bean id="orderWriter"
class="org.springframework.batch.sample.dao.FlatFileOrderWriter">
<property name="delegate" ref="flatFileTransformerOutput" />
<property name="transformer">
<bean
class="org.springframework.batch.sample.dao.OrderTransformer">
<property name="aggregators" ref="outputDescriptors" />
</bean>
</property>
</bean>
<bean id="headerFieldSetMapper"
class="org.springframework.batch.sample.mapping.HeaderFieldSetMapper" />
<bean id="customerFieldSetMapper"
class="org.springframework.batch.sample.mapping.CustomerFieldSetMapper" />
<bean id="addressFieldSetMapper"
class="org.springframework.batch.sample.mapping.AddressFieldSetMapper" />
<bean id="billingFieldSetMapper"
class="org.springframework.batch.sample.mapping.BillingFieldSetMapper" />
<bean id="orderItemFieldSetMapper"
class="org.springframework.batch.sample.mapping.OrderItemFieldSetMapper" />
<bean id="shippingFieldSetMapper"
class="org.springframework.batch.sample.mapping.ShippingFieldSetMapper" />
<bean id="validator"
class="org.springframework.batch.item.validator.SpringValidator">
<property name="validator">
<bean id="orderValidator"
class="org.springmodules.validation.valang.ValangValidator">
<property name="valang">
<value>
<![CDATA[
{ orderId : ? > 0 AND ? <= 9999999999 : 'Incorrect order ID' : 'error.order.id' }
{ orderDate : isFutureDate(?) = FALSE : 'Future date is not allowed' : 'error.order.date.future' }
@@ -131,41 +136,41 @@
</property>
<property name="customFunctions">
<map>
<entry key="isFutureDate"
value="org.springframework.batch.sample.validation.valang.custom.FutureDateFunction" />
<entry key="validateTotalItemsCount"
value="org.springframework.batch.sample.validation.valang.custom.TotalOrderItemsFunction" />
<entry key="validateIds"
value="org.springframework.batch.sample.validation.valang.custom.ValidateIdsFunction" />
<entry key="validatePrices"
value="org.springframework.batch.sample.validation.valang.custom.ValidatePricesFunction" />
<entry key="validateDiscounts"
value="org.springframework.batch.sample.validation.valang.custom.ValidateDiscountsFunction" />
<entry key="validateShippingPrices"
value="org.springframework.batch.sample.validation.valang.custom.ValidateShippingPricesFunction" />
<entry key="validateHandlingPrices"
value="org.springframework.batch.sample.validation.valang.custom.ValidateHandlingPricesFunction" />
<entry key="validateQuantities"
value="org.springframework.batch.sample.validation.valang.custom.ValidateQuantitiesFunction" />
<entry key="validateTotalPrices"
value="org.springframework.batch.sample.validation.valang.custom.ValidateTotalPricesFunction" />
</map>
</property>
</bean>
</property>
</bean>
<!-- "{" <key> : <rule> : <message> : [ <error_code> [ : <error_parameters> ] ] "}" -->
<bean id="fileInputLocator"
class="org.springframework.core.io.ClassPathResource">
<constructor-arg type="java.lang.String"
value="data/multilineOrderJob/input/20070122.teststream.multilineOrderStep.txt" />
</bean>
<bean id="fileOutputLocator"
class="org.springframework.core.io.FileSystemResource">
<constructor-arg type="java.lang.String"
value="target/test-outputs/20070122.teststream.multilineOrderStep.TEMP.txt" />
</bean>
<entry key="isFutureDate"
value="org.springframework.batch.sample.validation.valang.custom.FutureDateFunction" />
<entry key="validateTotalItemsCount"
value="org.springframework.batch.sample.validation.valang.custom.TotalOrderItemsFunction" />
<entry key="validateIds"
value="org.springframework.batch.sample.validation.valang.custom.ValidateIdsFunction" />
<entry key="validatePrices"
value="org.springframework.batch.sample.validation.valang.custom.ValidatePricesFunction" />
<entry key="validateDiscounts"
value="org.springframework.batch.sample.validation.valang.custom.ValidateDiscountsFunction" />
<entry key="validateShippingPrices"
value="org.springframework.batch.sample.validation.valang.custom.ValidateShippingPricesFunction" />
<entry key="validateHandlingPrices"
value="org.springframework.batch.sample.validation.valang.custom.ValidateHandlingPricesFunction" />
<entry key="validateQuantities"
value="org.springframework.batch.sample.validation.valang.custom.ValidateQuantitiesFunction" />
<entry key="validateTotalPrices"
value="org.springframework.batch.sample.validation.valang.custom.ValidateTotalPricesFunction" />
</map>
</property>
</bean>
</property>
</bean>
<!-- "{" <key> : <rule> : <message> : [ <error_code> [ : <error_parameters> ] ] "}" -->
<bean id="fileInputLocator"
class="org.springframework.core.io.ClassPathResource">
<constructor-arg type="java.lang.String"
value="data/multilineOrderJob/input/20070122.teststream.multilineOrderStep.txt" />
</bean>
<bean id="fileOutputLocator"
class="org.springframework.core.io.FileSystemResource">
<constructor-arg type="java.lang.String"
value="target/test-outputs/20070122.teststream.multilineOrderStep.TEMP.txt" />
</bean>
</beans>

View File

@@ -9,14 +9,12 @@ import org.springframework.batch.io.file.mapping.DefaultFieldSet;
import org.springframework.batch.io.file.mapping.FieldSet;
import org.springframework.batch.io.file.mapping.FieldSetMapper;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.validator.Validator;
import org.springframework.batch.sample.domain.Address;
import org.springframework.batch.sample.domain.BillingInfo;
import org.springframework.batch.sample.domain.Customer;
import org.springframework.batch.sample.domain.LineItem;
import org.springframework.batch.sample.domain.Order;
import org.springframework.batch.sample.domain.ShippingInfo;
import org.springframework.batch.sample.item.reader.OrderItemReader;
public class OrderItemReaderTests extends TestCase {
@@ -25,8 +23,6 @@ public class OrderItemReaderTests extends TestCase {
private ItemReader input;
private MockControl mapperControl;
private FieldSetMapper mapper;
private MockControl validatorControl;
private Validator validator;
public void setUp() {
@@ -110,16 +106,8 @@ public class OrderItemReaderTests extends TestCase {
mapperControl.setReturnValue(item,3);
mapperControl.replay();
//create mock validator
validatorControl = MockControl.createControl(Validator.class);
validator = (Validator)validatorControl.getMock();
validator.validate(null);
validatorControl.setMatcher(MockControl.ALWAYS_MATCHER);
validatorControl.setVoidCallable(1);
validatorControl.replay();
//set-up provider: set mappers and validator
provider.setValidator(validator);
//set-up provider: set mappers
provider.setAddressMapper(mapper);
provider.setBillingMapper(mapper);
provider.setCustomerMapper(mapper);
@@ -158,7 +146,6 @@ public class OrderItemReaderTests extends TestCase {
//verify method calls on input source, mapper and validator
inputControl.verify();
mapperControl.verify();
validatorControl.verify();
}
}