diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/CompositeItemProcessor.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/CompositeItemProcessor.java index 13dee854a..82920f4c4 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/CompositeItemProcessor.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/CompositeItemProcessor.java @@ -19,27 +19,27 @@ import org.springframework.util.Assert; @SuppressWarnings("unchecked") public class CompositeItemProcessor implements ItemProcessor, InitializingBean { - private List itemTransformers; + private List itemProcessors; public O process(I item) throws Exception { Object result = item; - for(ItemProcessor transformer: itemTransformers){ + for(ItemProcessor transformer: itemProcessors){ result = transformer.process(result); } return (O) result; } public void afterPropertiesSet() throws Exception { - Assert.notEmpty(itemTransformers); + Assert.notEmpty(itemProcessors); } /** - * @param itemTransformers will be chained to produce a composite + * @param itemProcessors will be chained to produce a composite * transformation. */ - public void setItemTransformers(List itemTransformers) { - this.itemTransformers = itemTransformers; + public void setItemProcessors(List itemProcessors) { + this.itemProcessors = itemProcessors; } } diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/validator/ValidatingItemProcessor.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/validator/ValidatingItemProcessor.java new file mode 100644 index 000000000..cea6e86a9 --- /dev/null +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/validator/ValidatingItemProcessor.java @@ -0,0 +1,42 @@ +package org.springframework.batch.item.validator; + +import org.springframework.batch.item.ItemProcessor; +import org.springframework.util.Assert; + +/** + * Simple implementation of {@link ItemProcessor} validates and input and + * returns it without modifications. + * + * @author Robert Kasanicky + * + */ +public class ValidatingItemProcessor implements ItemProcessor { + + private Validator validator; + + public ValidatingItemProcessor(Validator validator){ + Assert.notNull(validator, "Validator must not be null."); + this.validator = validator; + } + + /** + * Set the validator used to validate each item. + * + * @param validator + */ + public void setValidator(Validator validator) { + this.validator = validator; + } + + /** + * Validate the item and return it unmodified + * + * @return the input item + * @throws ValidationException if validation fails + */ + public T process(T item) throws ValidationException { + validator.validate(item); + return item; + } + +} diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/validator/ValidatingItemReader.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/validator/ValidatingItemReader.java deleted file mode 100644 index 818b5565d..000000000 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/validator/ValidatingItemReader.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 2006-2007 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.batch.item.validator; - -import org.springframework.batch.item.support.DelegatingItemReader; -import org.springframework.util.Assert; - -/** - * Simple extension of {@link DelegatingItemReader} that provides for - * validation before returning input. - * - * @author Lucas Ward - * - */ -public class ValidatingItemReader extends DelegatingItemReader { - - private Validator validator; - - /* (non-Javadoc) - * @see org.springframework.batch.item.reader.DelegatingItemReader#afterPropertiesSet() - */ - public void afterPropertiesSet() throws Exception { - super.afterPropertiesSet(); - Assert.notNull(validator, "Validator must not be null."); - } - - /* (non-Javadoc) - * @see org.springframework.batch.item.reader.DelegatingItemReader#read() - */ - public T read() throws Exception { - T input = super.read(); - if(input != null){ - validator.validate(input); - } - return input; - } - - /** - * Set the validator used to validate each item. - * - * @param validator - */ - public void setValidator(Validator validator) { - this.validator = validator; - } -} diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/CompositeItemProcessorTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/CompositeItemProcessorTests.java index 401ce9d35..0105daad3 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/CompositeItemProcessorTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/CompositeItemProcessorTests.java @@ -32,7 +32,7 @@ public class CompositeItemProcessorTests { transformer1 = createMock(ItemProcessor.class); transformer2 = createMock(ItemProcessor.class); - composite.setItemTransformers(new ArrayList() {{ + composite.setItemProcessors(new ArrayList() {{ add(transformer1); add(transformer2); }}); @@ -71,7 +71,7 @@ public class CompositeItemProcessorTests { public void testAfterPropertiesSet() throws Exception { // value not set - composite.setItemTransformers(null); + composite.setItemProcessors(null); try { composite.afterPropertiesSet(); fail(); @@ -81,7 +81,7 @@ public class CompositeItemProcessorTests { } // empty list - composite.setItemTransformers(new ArrayList()); + composite.setItemProcessors(new ArrayList()); try { composite.afterPropertiesSet(); fail(); diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/validator/ValidatingItemProcessorTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/validator/ValidatingItemProcessorTests.java new file mode 100644 index 000000000..473436d9d --- /dev/null +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/validator/ValidatingItemProcessorTests.java @@ -0,0 +1,39 @@ +package org.springframework.batch.item.validator; + +import org.junit.Test; +import static org.junit.Assert.*; +import static org.easymock.EasyMock.*; + +/** + * Tests for {@link ValidatingItemProcessor}. + */ +public class ValidatingItemProcessorTests { + + private Validator validator = createMock(Validator.class); + + private ValidatingItemProcessor tested = new ValidatingItemProcessor(validator); + + private String item = "item"; + + @Test + public void testSuccessfulValidation() throws Exception { + + validator.validate(item); + expectLastCall(); + replay(validator); + + assertSame(item, tested.process(item)); + + verify(validator); + } + + @Test(expected=ValidationException.class) + public void testFailedValidation() throws Exception { + + validator.validate(item); + expectLastCall().andThrow(new ValidationException("invalid item")); + replay(validator); + + tested.process(item); + } +} diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/validator/ValidatingItemReaderTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/validator/ValidatingItemReaderTests.java deleted file mode 100644 index 80ded6c01..000000000 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/validator/ValidatingItemReaderTests.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Copyright 2006-2007 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.batch.item.validator; - -import static org.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.expectLastCall; -import static org.easymock.EasyMock.replay; -import static org.easymock.EasyMock.verify; -import junit.framework.TestCase; - -import org.springframework.batch.item.ItemReader; - -/** - * @author Lucas Ward - * - */ -public class ValidatingItemReaderTests extends TestCase { - - ItemReader inputSource; - ValidatingItemReader itemProvider; - Validator validator; - - /* (non-Javadoc) - * @see junit.framework.TestCase#setUp() - */ - protected void setUp() throws Exception { - super.setUp(); - - inputSource = new MockItemReader(this); - validator = createMock(Validator.class); - itemProvider = new ValidatingItemReader(); - itemProvider.setItemReader(inputSource); - itemProvider.setValidator(validator); - } - - /* - * Super class' afterPropertieSet should be called to - * ensure ItemReader is set. - */ - public void testItemReaderPropertiesSet(){ - try{ - itemProvider.setItemReader(null); - itemProvider.afterPropertiesSet(); - fail(); - }catch(Exception ex){ - assertTrue(ex instanceof IllegalArgumentException); - } - } - - public void testValidatorPropertesSet(){ - try{ - itemProvider.setValidator(null); - itemProvider.afterPropertiesSet(); - fail(); - }catch(Exception ex){ - assertTrue(ex instanceof IllegalArgumentException); - } - } - - public void testValidation() throws Exception{ - - validator.validate(this); - expectLastCall().once(); - replay(validator); - assertEquals(itemProvider.read(), this); - verify(validator); - } - - public void testValidationException() throws Exception{ - - validator.validate(this); - expectLastCall().andThrow(new ValidationException("")); - replay(validator); - try{ - itemProvider.read(); - fail(); - }catch(ValidationException ex){ - //expected - } - } - - public void testNullInput() throws Exception{ - replay(validator); - itemProvider.setItemReader(new MockItemReader(null)); - assertNull(itemProvider.read()); - //assert validator wasn't called. - verify(validator); - } - - private static class MockItemReader implements ItemReader { - - Object value; - - public MockItemReader(Object value){ - this.value = value; - } - - public Object read() { - return value; - } - } -} diff --git a/spring-batch-samples/src/main/resources/jobs/beanWrapperMapperSampleJob.xml b/spring-batch-samples/src/main/resources/jobs/beanWrapperMapperSampleJob.xml index c8a615a61..a92d27f4f 100644 --- a/spring-batch-samples/src/main/resources/jobs/beanWrapperMapperSampleJob.xml +++ b/spring-batch-samples/src/main/resources/jobs/beanWrapperMapperSampleJob.xml @@ -11,13 +11,12 @@ - - - - - + + + + - + diff --git a/spring-batch-samples/src/main/resources/jobs/compositeItemWriterSampleJob.xml b/spring-batch-samples/src/main/resources/jobs/compositeItemWriterSampleJob.xml index cc626773c..4e5ba7b11 100644 --- a/spring-batch-samples/src/main/resources/jobs/compositeItemWriterSampleJob.xml +++ b/spring-batch-samples/src/main/resources/jobs/compositeItemWriterSampleJob.xml @@ -18,14 +18,12 @@ - - - - + + + + - + diff --git a/spring-batch-samples/src/main/resources/jobs/fixedLengthImportJob.xml b/spring-batch-samples/src/main/resources/jobs/fixedLengthImportJob.xml index 95713cfb2..948471f78 100644 --- a/spring-batch-samples/src/main/resources/jobs/fixedLengthImportJob.xml +++ b/spring-batch-samples/src/main/resources/jobs/fixedLengthImportJob.xml @@ -10,11 +10,10 @@ - - - - - + + + + diff --git a/spring-batch-samples/src/main/resources/jobs/multiResourceJob.xml b/spring-batch-samples/src/main/resources/jobs/multiResourceJob.xml index 9f97cb8d2..5b29923ad 100644 --- a/spring-batch-samples/src/main/resources/jobs/multiResourceJob.xml +++ b/spring-batch-samples/src/main/resources/jobs/multiResourceJob.xml @@ -12,13 +12,10 @@ - - - - - + + + + diff --git a/spring-batch-samples/src/main/resources/jobs/multilineOrderJob.xml b/spring-batch-samples/src/main/resources/jobs/multilineOrderJob.xml index c9fc93dc5..d0481fb25 100644 --- a/spring-batch-samples/src/main/resources/jobs/multilineOrderJob.xml +++ b/spring-batch-samples/src/main/resources/jobs/multilineOrderJob.xml @@ -20,25 +20,29 @@ - - - - - - - - - - - - - + + + + + + + + - - + + + + + + + + + + + @@ -134,7 +138,8 @@ - + - - - - - + + + + - + diff --git a/spring-batch-samples/src/main/resources/jobs/restartSample.xml b/spring-batch-samples/src/main/resources/jobs/restartSample.xml index 3370bf133..0dd704e3d 100644 --- a/spring-batch-samples/src/main/resources/jobs/restartSample.xml +++ b/spring-batch-samples/src/main/resources/jobs/restartSample.xml @@ -13,13 +13,12 @@ - - - - - - + + + + + + diff --git a/spring-batch-samples/src/main/resources/jobs/skipSampleJob.xml b/spring-batch-samples/src/main/resources/jobs/skipSampleJob.xml index 9082d2901..9485aa30c 100644 --- a/spring-batch-samples/src/main/resources/jobs/skipSampleJob.xml +++ b/spring-batch-samples/src/main/resources/jobs/skipSampleJob.xml @@ -15,16 +15,7 @@ - - - - - - - + - - - - + + + +