Prefer Slice over Page in RepositoryItemReader

Resolves #4115
This commit is contained in:
Henning Poettker
2023-02-09 22:25:38 +01:00
committed by Mahmoud Ben Hassine
parent 900b36f604
commit 669768365f
4 changed files with 21 additions and 20 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2023 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.
@@ -28,9 +28,9 @@ import org.springframework.batch.item.adapter.AbstractMethodInvokingDelegator.In
import org.springframework.batch.item.adapter.DynamicMethodInvocationException;
import org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.lang.Nullable;
@@ -168,7 +168,7 @@ public class RepositoryItemReader<T> extends AbstractItemCountingItemStreamItemR
results = doPageRead();
page ++;
if(results.size() <= 0) {
if(results.isEmpty()) {
return null;
}
@@ -220,7 +220,7 @@ public class RepositoryItemReader<T> extends AbstractItemCountingItemStreamItemR
invoker.setArguments(parameters.toArray());
Page<T> curPage = (Page<T>) doInvoke(invoker);
Slice<T> curPage = (Slice<T>) doInvoke(invoker);
return curPage.getContent();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2020 the original author or authors.
* Copyright 2013-2023 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.
@@ -29,10 +29,11 @@ import org.mockito.MockitoAnnotations;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.adapter.DynamicMethodInvocationException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.SliceImpl;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.repository.PagingAndSortingRepository;
@@ -263,7 +264,7 @@ public class RepositoryItemReaderTests {
reader.setMethodName("findFirstNames");
ArgumentCaptor<PageRequest> pageRequestContainer = ArgumentCaptor.forClass(PageRequest.class);
when(differentRepository.findFirstNames(pageRequestContainer.capture())).thenReturn(new PageImpl<>(singletonList(
when(differentRepository.findFirstNames(pageRequestContainer.capture())).thenReturn(new SliceImpl<>(singletonList(
"result"
)));
@@ -365,7 +366,7 @@ public class RepositoryItemReaderTests {
}
public interface TestRepository extends PagingAndSortingRepository<Map, Long> {
Page<String> findFirstNames(Pageable pageable);
Slice<String> findFirstNames(Pageable pageable);
}
// Simple object for readability

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017-2018 the original author or authors.
* Copyright 2017-2023 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.
@@ -28,8 +28,8 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.batch.item.data.RepositoryItemReader;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.PagingAndSortingRepository;
@@ -54,7 +54,7 @@ public class RepositoryItemReaderBuilderTests {
private TestRepository repository;
@Mock
private Page<String> page;
private Slice<String> slice;
private Map<String, Sort.Direction> sorts;
@@ -69,9 +69,9 @@ public class RepositoryItemReaderBuilderTests {
List<String> testResult = new ArrayList<>();
testResult.add(TEST_CONTENT);
when(page.getContent()).thenReturn(testResult);
when(page.getSize()).thenReturn(5);
when(this.repository.foo(this.pageRequestContainer.capture())).thenReturn(this.page);
when(slice.getContent()).thenReturn(testResult);
when(slice.getSize()).thenReturn(5);
when(this.repository.foo(this.pageRequestContainer.capture())).thenReturn(this.slice);
}
@Test
@@ -116,7 +116,7 @@ public class RepositoryItemReaderBuilderTests {
ArgumentCaptor<String> arg2Captor = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<String> arg3Captor = ArgumentCaptor.forClass(String.class);
when(this.repository.foo(arg1Captor.capture(), arg2Captor.capture(), arg3Captor.capture(),
this.pageRequestContainer.capture())).thenReturn(this.page);
this.pageRequestContainer.capture())).thenReturn(this.slice);
String result = (String) reader.read();
assertEquals("Result returned from reader was not expected value.", TEST_CONTENT, result);
@@ -244,7 +244,7 @@ public class RepositoryItemReaderBuilderTests {
ArgumentCaptor<String> arg2Captor = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<String> arg3Captor = ArgumentCaptor.forClass(String.class);
when(this.repository.foo(arg1Captor.capture(), arg2Captor.capture(), arg3Captor.capture(),
this.pageRequestContainer.capture())).thenReturn(this.page);
this.pageRequestContainer.capture())).thenReturn(this.slice);
RepositoryItemReader<Object> reader = new RepositoryItemReaderBuilder<>().repository(this.repository)
.sorts(this.sorts)
@@ -264,7 +264,7 @@ public class RepositoryItemReaderBuilderTests {
ArgumentCaptor<String> arg2Captor = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<String> arg3Captor = ArgumentCaptor.forClass(String.class);
when(this.repository.foo(arg1Captor.capture(), arg2Captor.capture(), arg3Captor.capture(),
this.pageRequestContainer.capture())).thenReturn(this.page);
this.pageRequestContainer.capture())).thenReturn(this.slice);
RepositoryItemReader<Object> reader = new RepositoryItemReaderBuilder<>().repository(this.repository)
.sorts(this.sorts)

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013 the original author or authors.
* Copyright 2013-2023 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.
@@ -18,10 +18,10 @@ package org.springframework.batch.sample.data;
import java.math.BigDecimal;
import org.springframework.batch.sample.domain.trade.CustomerCredit;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.repository.PagingAndSortingRepository;
public interface CustomerCreditRepository extends PagingAndSortingRepository<CustomerCredit, Long>{
Page<CustomerCredit> findByCreditGreaterThan(BigDecimal credit, Pageable request);
Slice<CustomerCredit> findByCreditGreaterThan(BigDecimal credit, Pageable request);
}