diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/sample/common/CustomItemReaderTest.java b/spring-batch-samples/src/main/java/org/springframework/batch/sample/common/CustomItemReaderTest.java new file mode 100644 index 000000000..542cb007f --- /dev/null +++ b/spring-batch-samples/src/main/java/org/springframework/batch/sample/common/CustomItemReaderTest.java @@ -0,0 +1,136 @@ +/* + * Copyright 2006-2008 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.sample.common; + +import java.util.ArrayList; +import java.util.List; + +import org.springframework.batch.item.ExecutionContext; +import org.springframework.batch.item.ItemReader; +import org.springframework.batch.item.ItemStream; +import org.springframework.batch.item.ItemStreamException; +import org.springframework.batch.item.MarkFailedException; +import org.springframework.batch.item.NoWorkFoundException; +import org.springframework.batch.item.ParseException; +import org.springframework.batch.item.ResetFailedException; +import org.springframework.batch.item.UnexpectedInputException; + +import junit.framework.TestCase; + +/** + * Unit test class that was used as part of the Reference Documentation. I'm only including it in the + * code to help keep the reference documentation up to date as the code base shifts. + * + * @author Lucas Ward + * + */ +public class CustomItemReaderTest extends TestCase { + + ItemReader itemReader; + + /* (non-Javadoc) + * @see junit.framework.TestCase#setUp() + */ + protected void setUp() throws Exception { + super.setUp(); + + List items = new ArrayList(); + items.add("1"); + items.add("2"); + items.add("3"); + + itemReader = new CustomItemReader(items); + } + + public void testRead() throws Exception{ + + assertEquals("1", itemReader.read()); + assertEquals("2", itemReader.read()); + assertEquals("3", itemReader.read()); + assertNull(itemReader.read()); + } + + public void testRollback() throws Exception{ + + itemReader.mark(); + assertEquals("1", itemReader.read()); + assertEquals("2", itemReader.read()); + itemReader.reset(); + assertEquals("1", itemReader.read()); + } + + public void testRestart() throws Exception{ + + ExecutionContext executionContext = new ExecutionContext(); + ((ItemStream)itemReader).open(executionContext); + assertEquals("1", itemReader.read()); + ((ItemStream)itemReader).update(executionContext); + List items = new ArrayList(); + items.add("1"); + items.add("2"); + items.add("3"); + itemReader = new CustomItemReader(items); + + ((ItemStream)itemReader).open(executionContext); + assertEquals("2", itemReader.read()); + } + + public class CustomItemReader implements ItemReader, ItemStream{ + + List items; + int currentIndex = 0; + int lastMarkedIndex = 0; + private static final String CURRENT_INDEX = "current.index"; + + public CustomItemReader(List items) { + this.items = items; + } + + public T read() throws Exception, UnexpectedInputException, + NoWorkFoundException, ParseException { + + if (currentIndex < items.size()) { + return items.get(currentIndex++); + } + return null; + } + + public void mark() throws MarkFailedException { + lastMarkedIndex = currentIndex; + }; + + public void reset() throws ResetFailedException { + currentIndex = lastMarkedIndex; + } + + public void open(ExecutionContext executionContext) throws ItemStreamException { + if(executionContext.containsKey(CURRENT_INDEX)){ + currentIndex = new Long(executionContext.getLong(CURRENT_INDEX)).intValue(); + } + else{ + currentIndex = 0; + lastMarkedIndex = 0; + } + } + + public void close(ExecutionContext executionContext) throws ItemStreamException {} + + public void update(ExecutionContext executionContext) throws ItemStreamException { + executionContext.putLong(CURRENT_INDEX, new Long(currentIndex).longValue()); + }; + + } +} diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/sample/common/CustomItemWriterTests.java b/spring-batch-samples/src/main/java/org/springframework/batch/sample/common/CustomItemWriterTests.java new file mode 100644 index 000000000..afbfc5b43 --- /dev/null +++ b/spring-batch-samples/src/main/java/org/springframework/batch/sample/common/CustomItemWriterTests.java @@ -0,0 +1,82 @@ +/* + * Copyright 2006-2008 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.sample.common; + +import java.util.ArrayList; +import java.util.List; + +import junit.framework.TestCase; + +import org.springframework.batch.item.ClearFailedException; +import org.springframework.batch.item.FlushFailedException; +import org.springframework.batch.item.ItemWriter; + +/** + * Unit test class that was used as part of the Reference Documentation. I'm + * only including it in the code to help keep the reference documentation up to + * date as the code base shifts. + * + * @author Lucas Ward + * + */ +public class CustomItemWriterTests extends TestCase { + + /* + * (non-Javadoc) + * + * @see junit.framework.TestCase#setUp() + */ + protected void setUp() throws Exception { + super.setUp(); + } + + public void testFlush() throws Exception{ + + CustomItemWriter itemWriter = new CustomItemWriter(); + itemWriter.write("1"); + assertEquals(0, itemWriter.getOutput().size()); + itemWriter.flush(); + assertEquals(1, itemWriter.getOutput().size()); + itemWriter.write("2"); + itemWriter.write("3"); + itemWriter.clear(); + assertEquals(1, itemWriter.getOutput().size()); + } + + public class CustomItemWriter implements ItemWriter{ + + List output = new ArrayList(); + List buffer = new ArrayList(); + + public void write(T item) throws Exception { + buffer.add(item); + } + + public void clear() throws ClearFailedException { + buffer.clear(); + } + + public void flush() throws FlushFailedException { + for(T t:buffer){ + output.add(t); + } + } + + public List getOutput() { + return output; + } + } +}