Reset page and item count on RepositoryItemReader#close()

When closing the RepositoryItemReader, this commit now resets the
current item index and page index to be 0 so that it can be reused
imediately (instead of requiring the reader be step scoped for the reset
to occur).

BATCH-2365
This commit is contained in:
Michael Minella
2015-03-31 15:19:34 -05:00
parent adc2f79242
commit 169fe17713
2 changed files with 38 additions and 3 deletions

View File

@@ -216,6 +216,11 @@ public class RepositoryItemReader<T> extends AbstractItemCountingItemStreamItemR
@Override
protected void doClose() throws Exception {
synchronized (lock) {
current = 0;
page = 0;
results = null;
}
}
private Sort convertToSort(Map<String, Sort.Direction> sorts) {

View File

@@ -185,7 +185,7 @@ public class RepositoryItemReaderTests {
public void testJumpToItem() throws Exception {
reader.setPageSize(100);
ArgumentCaptor<PageRequest> pageRequestContainer = ArgumentCaptor.forClass(PageRequest.class);
when(repository.findAll(pageRequestContainer.capture())).thenReturn(new PageImpl<Object>(new ArrayList<Object>(){{
when(repository.findAll(pageRequestContainer.capture())).thenReturn(new PageImpl<Object>(new ArrayList<Object>() {{
add(new Object());
}}));
@@ -241,7 +241,7 @@ public class RepositoryItemReaderTests {
reader.setPageSize(2);
PageRequest request = new PageRequest(1, 2, new Sort(Direction.ASC, "id"));
when(repository.findAll(request)).thenReturn(new PageImpl<Object>(new ArrayList<Object>(){{
when(repository.findAll(request)).thenReturn(new PageImpl<Object>(new ArrayList<Object>() {{
add("3");
add("4");
}}));
@@ -274,7 +274,7 @@ public class RepositoryItemReaderTests {
}}));
request = new PageRequest(2, 2, new Sort(Direction.ASC, "id"));
when(repository.findAll(request)).thenReturn(new PageImpl<Object>(new ArrayList<Object>(){{
when(repository.findAll(request)).thenReturn(new PageImpl<Object>(new ArrayList<Object>() {{
add("5");
add("6");
}}));
@@ -294,6 +294,36 @@ public class RepositoryItemReaderTests {
assertEquals("6", reader.read());
}
@Test
public void testResetOfPage() throws Exception {
reader.setPageSize(2);
PageRequest request = new PageRequest(0, 2, new Sort(Direction.ASC, "id"));
when(repository.findAll(request)).thenReturn(new PageImpl<Object>(new ArrayList<Object>(){{
add("1");
add("2");
}}));
request = new PageRequest(1, 2, new Sort(Direction.ASC, "id"));
when(repository.findAll(request)).thenReturn(new PageImpl<Object>(new ArrayList<Object>() {{
add("3");
add("4");
}}));
ExecutionContext executionContext = new ExecutionContext();
reader.open(executionContext);
Object result = reader.read();
reader.close();
assertEquals("1", result);
reader.open(executionContext);
assertEquals("1", reader.read());
assertEquals("2", reader.read());
assertEquals("3", reader.read());
}
public static interface TestRepository extends PagingAndSortingRepository<Map, Long> {
Page<String> findFirstNames(Pageable pageable);
}