|
| 1 | +/* |
| 2 | + * Copyright 2019-2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.jpa.repository; |
| 17 | + |
| 18 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 19 | +import org.junit.jupiter.params.ParameterizedTest; |
| 20 | +import org.junit.jupiter.params.provider.Arguments; |
| 21 | +import org.junit.jupiter.params.provider.MethodSource; |
| 22 | +import org.springframework.beans.factory.annotation.Autowired; |
| 23 | +import org.springframework.data.domain.KeysetScrollPosition; |
| 24 | +import org.springframework.data.domain.ScrollPosition; |
| 25 | +import org.springframework.data.domain.Sort; |
| 26 | +import org.springframework.data.domain.Window; |
| 27 | +import org.springframework.data.jpa.domain.sample.EntityWithoutScrollableId; |
| 28 | +import org.springframework.data.jpa.repository.sample.EntityWithoutScrollableIdRepository; |
| 29 | +import org.springframework.test.context.ContextConfiguration; |
| 30 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 31 | +import org.springframework.transaction.annotation.Transactional; |
| 32 | + |
| 33 | +import java.util.ArrayList; |
| 34 | +import java.util.List; |
| 35 | +import java.util.Map; |
| 36 | +import java.util.stream.Stream; |
| 37 | + |
| 38 | +import static org.assertj.core.api.Assertions.assertThat; |
| 39 | + |
| 40 | +/** |
| 41 | + * @author Yanming Zhou |
| 42 | + */ |
| 43 | +@ExtendWith(SpringExtension.class) |
| 44 | +@ContextConfiguration("classpath:config/namespace-application-context.xml") |
| 45 | +@Transactional |
| 46 | +class ScrollingWithoutIdIntegrationTests { |
| 47 | + |
| 48 | + private final static int pageSize = 10; |
| 49 | + |
| 50 | + private final static Integer[] totals = new Integer[] { 0, 5, 10, 15, 20, 25 }; |
| 51 | + |
| 52 | + @Autowired |
| 53 | + EntityWithoutScrollableIdRepository repository; |
| 54 | + |
| 55 | + void prepare(int total) { |
| 56 | + for (int i = 0; i < total; i++) { |
| 57 | + EntityWithoutScrollableId entity = new EntityWithoutScrollableId(); |
| 58 | + entity.setSeqNo(i); |
| 59 | + this.repository.save(entity); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @ParameterizedTest |
| 64 | + @MethodSource("cartesian") |
| 65 | + void scroll(Sort.Direction sortDirection, ScrollPosition.Direction scrollDirection, int total) { |
| 66 | + |
| 67 | + prepare(total); |
| 68 | + |
| 69 | + List<List<EntityWithoutScrollableId>> contents = new ArrayList<>(); |
| 70 | + |
| 71 | + String propertyName = "seqNo"; |
| 72 | + KeysetScrollPosition position = ScrollPosition.of(Map.of(), scrollDirection); |
| 73 | + if (scrollDirection == ScrollPosition.Direction.BACKWARD && position.getDirection() == ScrollPosition.Direction.FORWARD) { |
| 74 | + // remove this workaround if https://github.com/spring-projects/spring-data-commons/pull/2841 merged |
| 75 | + position = position.backward(); |
| 76 | + } |
| 77 | + while (true) { |
| 78 | + ScrollPosition positionToUse = position; |
| 79 | + Window<EntityWithoutScrollableId> window = this.repository.findBy((root, query, cb) -> null, |
| 80 | + q -> q.limit(pageSize).sortBy(Sort.by(sortDirection, propertyName)).scroll(positionToUse)); |
| 81 | + if (!window.isEmpty()) { |
| 82 | + contents.add(window.getContent()); |
| 83 | + } |
| 84 | + if (!window.hasNext()) { |
| 85 | + break; |
| 86 | + } |
| 87 | + int indexForNext = position.scrollsForward() ? window.size() - 1 : 0; |
| 88 | + position = ScrollPosition.of( |
| 89 | + Map.of(propertyName,window.getContent().get(indexForNext).getSeqNo()), |
| 90 | + scrollDirection); |
| 91 | + // position = window.positionAt(indexForNext); https://github.com/spring-projects/spring-data-jpa/pull/3000 |
| 92 | + // position = window.positionForNext(); https://github.com/spring-projects/spring-data-commons/pull/2843 |
| 93 | + } |
| 94 | + |
| 95 | + if (total == 0) { |
| 96 | + assertThat(contents).hasSize(0); |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + boolean divisible = total % pageSize == 0; |
| 101 | + |
| 102 | + assertThat(contents).hasSize(divisible ? total / pageSize : total / pageSize + 1); |
| 103 | + for (int i = 0; i < contents.size() - 1; i++) { |
| 104 | + assertThat(contents.get(i)).hasSize(pageSize); |
| 105 | + } |
| 106 | + if (divisible) { |
| 107 | + assertThat(contents.get(contents.size() - 1)).hasSize(pageSize); |
| 108 | + } |
| 109 | + else { |
| 110 | + assertThat(contents.get(contents.size() - 1)).hasSize(total % pageSize); |
| 111 | + } |
| 112 | + |
| 113 | + List<EntityWithoutScrollableId> first = contents.get(0); |
| 114 | + List<EntityWithoutScrollableId> last = contents.get(contents.size() - 1); |
| 115 | + |
| 116 | + if (sortDirection == Sort.Direction.ASC) { |
| 117 | + if (scrollDirection == ScrollPosition.Direction.FORWARD) { |
| 118 | + assertThat(first.get(0).getSeqNo()).isEqualTo(0); |
| 119 | + assertThat(last.get(last.size() - 1).getSeqNo()).isEqualTo(total - 1); |
| 120 | + } |
| 121 | + else { |
| 122 | + assertThat(first.get(first.size() - 1).getSeqNo()).isEqualTo(total - 1); |
| 123 | + assertThat(last.get(0).getSeqNo()).isEqualTo(0); |
| 124 | + } |
| 125 | + } |
| 126 | + else { |
| 127 | + if (scrollDirection == ScrollPosition.Direction.FORWARD) { |
| 128 | + assertThat(first.get(0).getSeqNo()).isEqualTo(total - 1); |
| 129 | + assertThat(last.get(last.size() - 1).getSeqNo()).isEqualTo(0); |
| 130 | + } |
| 131 | + else { |
| 132 | + assertThat(first.get(first.size() - 1).getSeqNo()).isEqualTo(0); |
| 133 | + assertThat(last.get(0).getSeqNo()).isEqualTo(total - 1); |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + private static Stream<Arguments> cartesian() { |
| 139 | + return Stream.of(Sort.Direction.class.getEnumConstants()) |
| 140 | + .flatMap(a -> Stream.of(ScrollPosition.Direction.class.getEnumConstants()) |
| 141 | + .flatMap(b -> Stream.of(totals).map(total -> Arguments.of(a, b, total)))); |
| 142 | + } |
| 143 | +} |
0 commit comments