DATACMNS-387 - Introduce Slice abstraction.
Introduced a Slice interface which is effectively a Page without the total page information. This will allow store modules to implement slice-by-slice access without having to calculate a total number of elements. Extracted content and Pageable related functionality into a common Chunk class which serves as base class for the two concrete implementations SliceImpl and PageImpl. Deprecated hasNextPage() etc. on Page in favor of the generic hasNext() methods. QueryMethod now exposes an isSliceQuery() to indicate a query method will need sliced execution.
This commit is contained in:
@@ -1,17 +1,17 @@
|
||||
/*
|
||||
* Copyright 2008-2013 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
|
||||
*
|
||||
* Copyright 2008-2014 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.
|
||||
* 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.domain;
|
||||
|
||||
@@ -71,12 +71,12 @@ public class PageImplUnitTests {
|
||||
|
||||
Page<Object> page = new PageImpl<Object>(Arrays.asList(new Object()), new PageRequest(0, 1), 10);
|
||||
|
||||
assertThat(page.isFirstPage(), is(true));
|
||||
assertThat(page.hasPreviousPage(), is(false));
|
||||
assertThat(page.isFirst(), is(true));
|
||||
assertThat(page.hasPrevious(), is(false));
|
||||
assertThat(page.previousPageable(), is(nullValue()));
|
||||
|
||||
assertThat(page.isLastPage(), is(false));
|
||||
assertThat(page.hasNextPage(), is(true));
|
||||
assertThat(page.isLast(), is(false));
|
||||
assertThat(page.hasNext(), is(true));
|
||||
assertThat(page.nextPageable(), is((Pageable) new PageRequest(1, 1)));
|
||||
}
|
||||
|
||||
@@ -85,12 +85,12 @@ public class PageImplUnitTests {
|
||||
|
||||
Page<Object> page = new PageImpl<Object>(Arrays.asList(new Object()), new PageRequest(1, 1), 2);
|
||||
|
||||
assertThat(page.isFirstPage(), is(false));
|
||||
assertThat(page.hasPreviousPage(), is(true));
|
||||
assertThat(page.isFirst(), is(false));
|
||||
assertThat(page.hasPrevious(), is(true));
|
||||
assertThat(page.previousPageable(), is((Pageable) new PageRequest(0, 1)));
|
||||
|
||||
assertThat(page.isLastPage(), is(true));
|
||||
assertThat(page.hasNextPage(), is(false));
|
||||
assertThat(page.isLast(), is(true));
|
||||
assertThat(page.hasNext(), is(false));
|
||||
assertThat(page.nextPageable(), is(nullValue()));
|
||||
}
|
||||
|
||||
@@ -107,10 +107,10 @@ public class PageImplUnitTests {
|
||||
assertThat(page.getSort(), is((Sort) null));
|
||||
assertThat(page.getTotalElements(), is(0L));
|
||||
assertThat(page.getTotalPages(), is(1));
|
||||
assertThat(page.hasNextPage(), is(false));
|
||||
assertThat(page.hasPreviousPage(), is(false));
|
||||
assertThat(page.isFirstPage(), is(true));
|
||||
assertThat(page.isLastPage(), is(true));
|
||||
assertThat(page.hasNext(), is(false));
|
||||
assertThat(page.hasPrevious(), is(false));
|
||||
assertThat(page.isFirst(), is(true));
|
||||
assertThat(page.isLast(), is(true));
|
||||
assertThat(page.hasContent(), is(false));
|
||||
}
|
||||
|
||||
@@ -123,7 +123,7 @@ public class PageImplUnitTests {
|
||||
Page<String> page = new PageImpl<String>(Arrays.asList("a"));
|
||||
|
||||
assertThat(page.getTotalPages(), is(1));
|
||||
assertThat(page.hasNextPage(), is(false));
|
||||
assertThat(page.hasPreviousPage(), is(false));
|
||||
assertThat(page.hasNext(), is(false));
|
||||
assertThat(page.hasPrevious(), is(false));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2008-2011 the original author or authors.
|
||||
* Copyright 2008-2014 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.
|
||||
@@ -15,32 +15,32 @@
|
||||
*/
|
||||
package org.springframework.data.repository.query;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Slice;
|
||||
import org.springframework.data.repository.Repository;
|
||||
import org.springframework.data.repository.core.RepositoryMetadata;
|
||||
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link QueryMethod}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class QueryMethodUnitTests {
|
||||
|
||||
@Mock
|
||||
RepositoryMetadata metadata;
|
||||
RepositoryMetadata metadata = new DefaultRepositoryMetadata(SampleRepository.class);
|
||||
|
||||
/**
|
||||
* @see DATAJPA-59
|
||||
*/
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void rejectsPagingMethodWithInvalidReturnType() throws Exception {
|
||||
|
||||
@@ -48,18 +48,27 @@ public class QueryMethodUnitTests {
|
||||
new QueryMethod(method, metadata);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAJPA-59
|
||||
*/
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void rejectsPagingMethodWithoutPageable() throws Exception {
|
||||
Method method = SampleRepository.class.getMethod("pagingMethodWithoutPageable");
|
||||
new QueryMethod(method, metadata);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-64
|
||||
*/
|
||||
@Test
|
||||
public void setsUpSimpleQueryMethodCorrectly() throws Exception {
|
||||
Method method = SampleRepository.class.getMethod("findByUsername", String.class);
|
||||
new QueryMethod(method, metadata);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-61
|
||||
*/
|
||||
@Test
|
||||
public void considersIterableMethodForCollectionQuery() throws Exception {
|
||||
Method method = SampleRepository.class.getMethod("sampleMethod");
|
||||
@@ -67,6 +76,9 @@ public class QueryMethodUnitTests {
|
||||
assertThat(queryMethod.isCollectionQuery(), is(true));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-67
|
||||
*/
|
||||
@Test
|
||||
public void doesNotConsiderPageMethodCollectionQuery() throws Exception {
|
||||
Method method = SampleRepository.class.getMethod("anotherSampleMethod", Pageable.class);
|
||||
@@ -75,33 +87,47 @@ public class QueryMethodUnitTests {
|
||||
assertThat(queryMethod.isCollectionQuery(), is(false));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-171
|
||||
*/
|
||||
@Test
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
public void detectsAnEntityBeingReturned() throws Exception {
|
||||
|
||||
when(metadata.getDomainType()).thenReturn((Class) User.class);
|
||||
when(metadata.getReturnedDomainClass(Mockito.any(Method.class))).thenReturn((Class) SpecialUser.class);
|
||||
|
||||
Method method = SampleRepository.class.getMethod("returnsEntitySubclass");
|
||||
QueryMethod queryMethod = new QueryMethod(method, metadata);
|
||||
|
||||
assertThat(queryMethod.isQueryForEntity(), is(true));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-171
|
||||
*/
|
||||
@Test
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
public void detectsNonEntityBeingReturned() throws Exception {
|
||||
|
||||
when(metadata.getDomainType()).thenReturn((Class) User.class);
|
||||
when(metadata.getReturnedDomainClass(Mockito.any(Method.class))).thenReturn((Class) Integer.class);
|
||||
|
||||
Method method = SampleRepository.class.getMethod("returnsProjection");
|
||||
QueryMethod queryMethod = new QueryMethod(method, metadata);
|
||||
|
||||
assertThat(queryMethod.isQueryForEntity(), is(false));
|
||||
}
|
||||
|
||||
interface SampleRepository {
|
||||
/**
|
||||
* @see DATACMNS-397
|
||||
*/
|
||||
@Test
|
||||
public void detectsSliceMethod() throws Exception {
|
||||
|
||||
RepositoryMetadata repositoryMetadata = new DefaultRepositoryMetadata(SampleRepository.class);
|
||||
Method method = SampleRepository.class.getMethod("sliceOfUsers");
|
||||
QueryMethod queryMethod = new QueryMethod(method, repositoryMetadata);
|
||||
|
||||
assertThat(queryMethod.isSliceQuery(), is(true));
|
||||
assertThat(queryMethod.isCollectionQuery(), is(false));
|
||||
assertThat(queryMethod.isPageQuery(), is(false));
|
||||
}
|
||||
|
||||
interface SampleRepository extends Repository<User, Serializable> {
|
||||
|
||||
String pagingMethodWithInvalidReturnType(Pageable pageable);
|
||||
|
||||
Page<String> pagingMethodWithoutPageable();
|
||||
@@ -115,6 +141,8 @@ public class QueryMethodUnitTests {
|
||||
SpecialUser returnsEntitySubclass();
|
||||
|
||||
Integer returnsProjection();
|
||||
|
||||
Slice<User> sliceOfUsers();
|
||||
}
|
||||
|
||||
class User {
|
||||
|
||||
Reference in New Issue
Block a user