DATACMNS-884 - Move PageableExecutionSupport from JPA to Spring Data Commons.
PageableExecutionSupport assumes that data queries are cheaper than count queries so we can apply some optimizations. We determine the total from the pageable and the results in which we don't hit the page size bounds (i.e. results are less than a full page without offset or results are greater 0 and less than a full page with offset). In all other cases we issue an additional count query.
This commit is contained in:
committed by
Oliver Gierke
parent
f2a6d63dbb
commit
29c86d74b3
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
* Copyright 2016 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
|
||||
*
|
||||
* http://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.repository.support;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.repository.support.PageableExecutionSupport.TotalSupplier;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link PageableExecutionSupport}.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class PageableExecutionSupportUnitTests {
|
||||
|
||||
@Mock TotalSupplier totalSupplierMock;
|
||||
|
||||
/**
|
||||
* @see DATAMCNS-884
|
||||
*/
|
||||
@Test
|
||||
public void firstPageRequestIsLessThanOneFullPageDoesNotRequireTotal() {
|
||||
|
||||
Page<Integer> page = PageableExecutionSupport.getPage(Arrays.asList(1, 2, 3), new PageRequest(0, 10),
|
||||
totalSupplierMock);
|
||||
|
||||
assertThat(page, hasItems(1, 2, 3));
|
||||
assertThat(page.getTotalElements(), is(3L));
|
||||
verifyZeroInteractions(totalSupplierMock);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMCNS-884
|
||||
*/
|
||||
@Test
|
||||
public void noPageableRequesDoesNotRequireTotal() {
|
||||
|
||||
Page<Integer> page = PageableExecutionSupport.getPage(Arrays.asList(1, 2, 3), null, totalSupplierMock);
|
||||
|
||||
assertThat(page, hasItems(1, 2, 3));
|
||||
assertThat(page.getTotalElements(), is(3L));
|
||||
verifyZeroInteractions(totalSupplierMock);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMCNS-884
|
||||
*/
|
||||
@Test
|
||||
public void subsequentPageRequestIsLessThanOneFullPageDoesNotRequireTotal() {
|
||||
|
||||
Page<Integer> page = PageableExecutionSupport.getPage(Arrays.asList(1, 2, 3), new PageRequest(5, 10),
|
||||
totalSupplierMock);
|
||||
|
||||
assertThat(page, hasItems(1, 2, 3));
|
||||
assertThat(page.getTotalElements(), is(53L));
|
||||
verifyZeroInteractions(totalSupplierMock);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMCNS-884
|
||||
*/
|
||||
@Test
|
||||
public void firstPageRequestHitsUpperBoundRequiresTotal() {
|
||||
|
||||
when(totalSupplierMock.get()).thenReturn(4L);
|
||||
|
||||
Page<Integer> page = PageableExecutionSupport.getPage(Arrays.asList(1, 2, 3), new PageRequest(0, 3),
|
||||
totalSupplierMock);
|
||||
|
||||
assertThat(page, hasItems(1, 2, 3));
|
||||
assertThat(page.getTotalElements(), is(4L));
|
||||
verify(totalSupplierMock).get();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMCNS-884
|
||||
*/
|
||||
@Test
|
||||
public void subsequentPageRequestHitsUpperBoundRequiresTotal() {
|
||||
|
||||
when(totalSupplierMock.get()).thenReturn(7L);
|
||||
|
||||
Page<Integer> page = PageableExecutionSupport.getPage(Arrays.asList(1, 2, 3), new PageRequest(1, 3),
|
||||
totalSupplierMock);
|
||||
|
||||
assertThat(page, hasItems(1, 2, 3));
|
||||
assertThat(page.getTotalElements(), is(7L));
|
||||
verify(totalSupplierMock).get();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMCNS-884
|
||||
*/
|
||||
@Test
|
||||
public void subsequentPageRequestWithoutResultRequiresRequireTotal() {
|
||||
|
||||
when(totalSupplierMock.get()).thenReturn(7L);
|
||||
Page<Integer> page = PageableExecutionSupport.getPage(Collections.<Integer> emptyList(), new PageRequest(5, 10),
|
||||
totalSupplierMock);
|
||||
|
||||
assertThat(page.getTotalElements(), is(7L));
|
||||
verify(totalSupplierMock).get();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user