IN PROGRESS - BATCH-830: DelegatingItemReader should be removed

replaced ValidatingItemReader with ValidatingItemProcessor
This commit is contained in:
robokaso
2008-09-25 13:56:34 +00:00
parent 55651077e4
commit 963e924bc6
15 changed files with 145 additions and 257 deletions

View File

@@ -32,7 +32,7 @@ public class CompositeItemProcessorTests {
transformer1 = createMock(ItemProcessor.class);
transformer2 = createMock(ItemProcessor.class);
composite.setItemTransformers(new ArrayList<ItemProcessor>() {{
composite.setItemProcessors(new ArrayList<ItemProcessor>() {{
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<ItemProcessor>());
composite.setItemProcessors(new ArrayList<ItemProcessor>());
try {
composite.afterPropertiesSet();
fail();

View File

@@ -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<String> tested = new ValidatingItemProcessor<String>(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);
}
}

View File

@@ -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<Object> inputSource;
ValidatingItemReader<Object> 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<Object>();
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> {
Object value;
public MockItemReader(Object value){
this.value = value;
}
public Object read() {
return value;
}
}
}