DATACMNS-1820 - Moves PageabeExecutionUtils to o.s.data.support.

This avoids module cycles when it is used from a template.
The old version is still there, deprecated and delegates to the new version.

Original pull request: #472.
This commit is contained in:
Jens Schauder
2020-10-28 07:28:07 +01:00
parent 0902fe8f8b
commit 6572880412
3 changed files with 192 additions and 21 deletions

View File

@@ -0,0 +1,117 @@
/*
* Copyright 2020 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.support;
import static java.util.Collections.*;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import java.util.Arrays;
import java.util.function.LongSupplier;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
/**
* Unit tests for {@link PageableExecutionUtils}.
*
* @author Mark Paluch
* @author Oliver Gierke
* @author Jens Schauder
*/
@ExtendWith(MockitoExtension.class)
class PageableExecutionUtilsUnitTests {
@Mock LongSupplier totalSupplierMock;
@Test // DATAMCNS-884
void firstPageRequestIsLessThanOneFullPageDoesNotRequireTotal() {
Page<Integer> page = PageableExecutionUtils.getPage(Arrays.asList(1, 2, 3), PageRequest.of(0, 10),
totalSupplierMock);
assertThat(page).contains(1, 2, 3);
assertThat(page.getTotalElements()).isEqualTo(3L);
verifyNoInteractions(totalSupplierMock);
}
@Test // DATAMCNS-884
void noPageableRequestDoesNotRequireTotal() {
Page<Integer> page = PageableExecutionUtils.getPage(Arrays.asList(1, 2, 3), Pageable.unpaged(), totalSupplierMock);
assertThat(page).contains(1, 2, 3);
assertThat(page.getTotalElements()).isEqualTo(3L);
verifyNoInteractions(totalSupplierMock);
}
@Test // DATAMCNS-884
void subsequentPageRequestIsLessThanOneFullPageDoesNotRequireTotal() {
Page<Integer> page = PageableExecutionUtils.getPage(Arrays.asList(1, 2, 3), PageRequest.of(5, 10),
totalSupplierMock);
assertThat(page).contains(1, 2, 3);
assertThat(page.getTotalElements()).isEqualTo(53L);
verifyNoInteractions(totalSupplierMock);
}
@Test // DATAMCNS-884
void firstPageRequestHitsUpperBoundRequiresTotal() {
doReturn(4L).when(totalSupplierMock).getAsLong();
Page<Integer> page = PageableExecutionUtils.getPage(Arrays.asList(1, 2, 3), PageRequest.of(0, 3),
totalSupplierMock);
assertThat(page).contains(1, 2, 3);
assertThat(page.getTotalElements()).isEqualTo(4L);
verify(totalSupplierMock).getAsLong();
}
@Test // DATAMCNS-884
void subsequentPageRequestHitsUpperBoundRequiresTotal() {
doReturn(7L).when(totalSupplierMock).getAsLong();
Page<Integer> page = PageableExecutionUtils.getPage(Arrays.asList(1, 2, 3), PageRequest.of(1, 3),
totalSupplierMock);
assertThat(page).contains(1, 2, 3);
assertThat(page.getTotalElements()).isEqualTo(7L);
verify(totalSupplierMock).getAsLong();
}
@Test // DATAMCNS-884
void subsequentPageRequestWithoutResultRequiresRequireTotal() {
doReturn(7L).when(totalSupplierMock).getAsLong();
Page<Integer> page = PageableExecutionUtils.getPage(emptyList(), PageRequest.of(5, 10), totalSupplierMock);
assertThat(page.getTotalElements()).isEqualTo(7L);
verify(totalSupplierMock).getAsLong();
}
}