BATCH-1972: StaxEventItemReader fails when restarted at end of file

This commit is contained in:
jpraet
2013-03-01 21:14:43 +01:00
committed by Michael Minella
parent 02b4a66d45
commit f7a88902ed
2 changed files with 33 additions and 2 deletions

View File

@@ -17,6 +17,7 @@
package org.springframework.batch.item.xml;
import java.io.InputStream;
import java.util.NoSuchElementException;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLEventReader;
@@ -247,8 +248,18 @@ public class StaxEventItemReader<T> extends AbstractItemCountingItemStreamItemRe
@Override
protected void jumpToItem(int itemIndex) throws Exception {
for (int i = 0; i < itemIndex; i++) {
readToStartFragment();
readToEndFragment();
try {
readToStartFragment();
readToEndFragment();
} catch (NoSuchElementException e) {
if (itemIndex == (i + 1)) {
// we can presume a NoSuchElementException on the last item means the EOF was reached on the last run
return;
} else {
// if NoSuchElementException occurs on an item other than the last one, this indicates a problem
throw e;
}
}
}
}

View File

@@ -223,6 +223,26 @@ public class StaxEventItemReaderTests {
}
/**
* Test restart at end of file.
*/
@Test
public void testRestartAtEndOfFile() throws Exception {
source.open(executionContext);
assertNotNull(source.read());
assertNotNull(source.read());
assertNull(source.read());
source.update(executionContext);
source.close();
assertEquals(3, executionContext.getInt(ClassUtils.getShortName(StaxEventItemReader.class) + ".read.count"));
source = createNewInputSouce();
source.open(executionContext);
assertNull(source.read());
}
@Test
public void testRestoreWorksFromClosedStream() throws Exception {
source.close();