From 87c5626db1299b4e0be79354d301c2dd9903e337 Mon Sep 17 00:00:00 2001 From: lucasward Date: Mon, 28 Jul 2008 19:40:33 +0000 Subject: [PATCH] BATCH-750: An exception thrown from SkipListener#onSkipInRead will now be swalled and logged, rather than causing a rollback. --- ...ractBufferedItemReaderItemStreamTests.java | 125 ++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/AbstractBufferedItemReaderItemStreamTests.java diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/AbstractBufferedItemReaderItemStreamTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/AbstractBufferedItemReaderItemStreamTests.java new file mode 100644 index 000000000..2c77e83f9 --- /dev/null +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/AbstractBufferedItemReaderItemStreamTests.java @@ -0,0 +1,125 @@ +/** + * + */ +package org.springframework.batch.item.support; + +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; + +import junit.framework.TestCase; + +/** + * @author Lucas Ward + * + */ +public class AbstractBufferedItemReaderItemStreamTests extends TestCase { + + StringReader reader; + + /* (non-Javadoc) + * @see junit.framework.TestCase#setUp() + */ + protected void setUp() throws Exception { + super.setUp(); + + reader = new StringReader(Arrays.asList(new String[]{"a", "b", "c", "d", "e"})); + } + + public void testNormalUsage() throws Exception{ + + assertEquals("a", reader.read()); + assertEquals("b", reader.read()); + assertEquals("c", reader.read()); + } + + public void testMarkAndReset() throws Exception{ + + reader.mark(); + assertEquals("a", reader.read()); + assertEquals("b", reader.read()); + reader.reset(); + assertEquals("a", reader.read()); + } + + public void testReaderExceptionWithoutRollback() throws Exception{ + + reader.mark(); + assertEquals("a", reader.read()); + assertEquals("b", reader.read()); + reader.setThrowException(true); + try{ + reader.read(); + fail(); + } + catch(RuntimeException ex){ + //expected + } + reader.setThrowException(false); + reader.mark(); + assertEquals("c", reader.read()); + + assertEquals("d", reader.read()); + } + + public void testResetWithoutException() throws Exception{ + + reader.mark(); + assertEquals("a", reader.read()); + assertEquals("b", reader.read()); + reader.reset(); + assertEquals("a", reader.read()); + } + + public void testResetWithException() throws Exception{ + + reader.mark(); + assertEquals("a", reader.read()); + assertEquals("b", reader.read()); + reader.setThrowException(true); + try{ + reader.read(); + fail(); + } + catch(RuntimeException ex){ + //expected + } + reader.setThrowException(false); + reader.reset(); + assertEquals("a", reader.read()); + } + + + private class StringReader extends AbstractBufferedItemReaderItemStream{ + + final Iterator iterator; + boolean throwException = false; + + public StringReader(List strings) { + iterator = strings.iterator(); + } + + protected void doClose() throws Exception { + + } + + protected void doOpen() throws Exception { + // TODO Auto-generated method stub + + } + + protected Object doRead() throws Exception { + if(throwException){ + throw new RuntimeException(); + } + else{ + return iterator.next(); + } + } + + public void setThrowException(boolean throwException) { + this.throwException = throwException; + } + + } +}