Verify custom finders can handle PageRequest as method parameter.

Spring Data Commons patched handling subclasses of special parameter types through https://github.com/spring-projects/spring-data-commons/issues/2626. This commit adds test cases ensuring things work properly with Spring Data JPA.

See #2013.
This commit is contained in:
Greg L. Turnquist
2022-05-12 11:47:34 -05:00
parent 307e8fce11
commit a2262a7718
2 changed files with 35 additions and 0 deletions

View File

@@ -27,6 +27,7 @@ import static org.springframework.data.jpa.domain.sample.UserSpecifications.*;
import lombok.Data;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
@@ -1679,6 +1680,32 @@ public class UserRepositoryTests {
assertThat(users).hasSize(0);
}
@Test // GH-2013
void findByCollectionWithPageable() {
flushTestUsers();
Page<User> userPage = repository.findByAgeIn(Arrays.asList(28, 35), (Pageable) PageRequest.of(0, 2));
assertThat(userPage).hasSize(2);
assertThat(userPage.getTotalElements()).isEqualTo(2);
assertThat(userPage.getTotalPages()).isEqualTo(1);
assertThat(userPage.getContent()).containsExactlyInAnyOrder(firstUser, secondUser);
}
@Test // GH-2013
void findByCollectionWithPageRequest() {
flushTestUsers();
Page<User> userPage = repository.findByAgeIn(Arrays.asList(28, 35), (PageRequest) PageRequest.of(0, 2));
assertThat(userPage).hasSize(2);
assertThat(userPage.getTotalElements()).isEqualTo(2);
assertThat(userPage.getTotalPages()).isEqualTo(1);
assertThat(userPage.getContent()).containsExactlyInAnyOrder(firstUser, secondUser);
}
@Test // DATAJPA-606
void findByEmptyArrayOfIntegers() throws Exception {

View File

@@ -27,6 +27,7 @@ import javax.persistence.EntityManager;
import javax.persistence.QueryHint;
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;
@@ -52,6 +53,7 @@ import org.springframework.transaction.annotation.Transactional;
* @author Jeff Sheets
* @author Andrey Kovalev
* @author JyotirmoyVS
* @author Greg Turnquist
*/
public interface UserRepository
extends JpaRepository<User, Integer>, JpaSpecificationExecutor<User>, UserRepositoryCustom {
@@ -504,6 +506,12 @@ public interface UserRepository
// DATAJPA-606
List<User> findByAgeIn(Collection<Integer> ages);
// GH-2013
Page<User> findByAgeIn(Collection<Integer> ages, Pageable pageable);
// GH-2013
Page<User> findByAgeIn(Collection<Integer> ages, PageRequest pageable);
// DATAJPA-606
List<User> queryByAgeIn(Integer[] ages);