DATACMNS-402 - Add support for sorting by a QueryDSL OrderSpecifier.
Previously we did only support ordering by a string or property path through Sort. In order to be able to express more type safe ordering expressions we introduce QSort as a subclass of Sort that is able to wrap QueryDSL OrderSpecifiers. To be able to separate the concerns of sorting via QueryDSL expressions better we introduce the AbstractPageRequest base class and QPageRequest as a special implementation that accepts a QSort or OrderSpecifiers for ordering. Original pull request: #59.
This commit is contained in:
committed by
Oliver Gierke
parent
9f65b57867
commit
f2ff84fb63
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* Copyright 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
|
||||
*
|
||||
* 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.domain;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.data.domain.UnitTestUtils.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
public abstract class AbstractPageRequestUnitTests {
|
||||
|
||||
public abstract AbstractPageRequest newPageRequest(int page, int size);
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void preventsNegativePage() {
|
||||
newPageRequest(-1, 10);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void preventsNegativeSize() {
|
||||
newPageRequest(0, -1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void navigatesPageablesCorrectly() {
|
||||
|
||||
Pageable request = newPageRequest(1, 10);
|
||||
|
||||
assertThat(request.hasPrevious(), is(true));
|
||||
assertThat(request.next(), is((Pageable) newPageRequest(2, 10)));
|
||||
|
||||
Pageable first = request.previousOrFirst();
|
||||
|
||||
assertThat(first.hasPrevious(), is(false));
|
||||
assertThat(first, is((Pageable) newPageRequest(0, 10)));
|
||||
assertThat(first, is(request.first()));
|
||||
assertThat(first.previousOrFirst(), is(first));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equalsHonoursPageAndSize() {
|
||||
|
||||
AbstractPageRequest request = newPageRequest(0, 10);
|
||||
|
||||
// Equals itself
|
||||
assertEqualsAndHashcode(request, request);
|
||||
|
||||
// Equals same setup
|
||||
assertEqualsAndHashcode(request, newPageRequest(0, 10));
|
||||
|
||||
// Does not equal on different page
|
||||
assertNotEqualsAndHashcode(request, newPageRequest(1, 10));
|
||||
|
||||
// Does not equal on different size
|
||||
assertNotEqualsAndHashcode(request, newPageRequest(0, 11));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-377
|
||||
*/
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void preventsPageSizeLessThanOne() {
|
||||
newPageRequest(0, 0);
|
||||
}
|
||||
}
|
||||
@@ -15,8 +15,6 @@
|
||||
*/
|
||||
package org.springframework.data.domain;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.data.domain.UnitTestUtils.*;
|
||||
|
||||
import org.junit.Test;
|
||||
@@ -27,39 +25,26 @@ import org.springframework.data.domain.Sort.Direction;
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class PageRequestUnitTests {
|
||||
public class PageRequestUnitTests extends AbstractPageRequestUnitTests {
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void preventsNegativePage() {
|
||||
new PageRequest(-1, 10);
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.domain.AbstractPageRequestUnitTests#newPageRequest(int, int)
|
||||
*/
|
||||
@Override
|
||||
public AbstractPageRequest newPageRequest(int page, int size) {
|
||||
return this.newPageRequest(page, size, null);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void preventsNegativeSize() {
|
||||
new PageRequest(0, -1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void navigatesPageablesCorrectly() {
|
||||
|
||||
Pageable request = new PageRequest(1, 10);
|
||||
|
||||
assertThat(request.hasPrevious(), is(true));
|
||||
assertThat(request.next(), is((Pageable) new PageRequest(2, 10)));
|
||||
|
||||
Pageable first = request.previousOrFirst();
|
||||
|
||||
assertThat(first.hasPrevious(), is(false));
|
||||
assertThat(first, is((Pageable) new PageRequest(0, 10)));
|
||||
assertThat(first, is(request.first()));
|
||||
assertThat(first.previousOrFirst(), is(first));
|
||||
public AbstractPageRequest newPageRequest(int page, int size, Sort sort) {
|
||||
return new PageRequest(page, size, sort);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equalsRegardsSortCorrectly() {
|
||||
|
||||
Sort sort = new Sort(Direction.DESC, "foo");
|
||||
PageRequest request = new PageRequest(0, 10, sort);
|
||||
AbstractPageRequest request = new PageRequest(0, 10, sort);
|
||||
|
||||
// Equals itself
|
||||
assertEqualsAndHashcode(request, request);
|
||||
@@ -76,30 +61,4 @@ public class PageRequestUnitTests {
|
||||
// Is not equal to instance with another sort
|
||||
assertNotEqualsAndHashcode(request, new PageRequest(0, 10, Direction.ASC, "foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equalsHonoursPageAndSize() {
|
||||
|
||||
PageRequest request = new PageRequest(0, 10);
|
||||
|
||||
// Equals itself
|
||||
assertEqualsAndHashcode(request, request);
|
||||
|
||||
// Equals same setup
|
||||
assertEqualsAndHashcode(request, new PageRequest(0, 10));
|
||||
|
||||
// Does not equal on different page
|
||||
assertNotEqualsAndHashcode(request, new PageRequest(1, 10));
|
||||
|
||||
// Does not equal on different size
|
||||
assertNotEqualsAndHashcode(request, new PageRequest(0, 11));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-377
|
||||
*/
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void preventsPageSizeLessThanOne() {
|
||||
new PageRequest(0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright 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
|
||||
*
|
||||
* 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.querydsl;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.domain.AbstractPageRequest;
|
||||
import org.springframework.data.domain.AbstractPageRequestUnitTests;
|
||||
|
||||
/**
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
public class QPageRequestUnitTests extends AbstractPageRequestUnitTests {
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.domain.AbstractPageRequestUnitTests#newPageRequest(int, int)
|
||||
*/
|
||||
@Override
|
||||
public AbstractPageRequest newPageRequest(int page, int size) {
|
||||
return new QPageRequest(page, size);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void constructsQPageRequestWithOrderSpecifiers() {
|
||||
|
||||
QUser user = QUser.user;
|
||||
QPageRequest pageRequest = new QPageRequest(0, 10, user.firstname.asc());
|
||||
|
||||
assertThat(pageRequest.getSort(), is(new QSort(user.firstname.asc())));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void constructsQPageRequestWithQSort() {
|
||||
|
||||
QUser user = QUser.user;
|
||||
QPageRequest pageRequest = new QPageRequest(0, 10, new QSort(user.firstname.asc()));
|
||||
|
||||
assertThat(pageRequest.getSort(), is(new QSort(user.firstname.asc())));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
* Copyright 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
|
||||
*
|
||||
* 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.querydsl;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.domain.Sort;
|
||||
|
||||
import com.mysema.query.types.OrderSpecifier;
|
||||
|
||||
/**
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
public class QSortUnitTests {
|
||||
|
||||
/**
|
||||
* @see DATACMNS-402
|
||||
*/
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void shouldThrowIfNoOrderSpecifiersAreGiven() {
|
||||
new QSort();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-402
|
||||
*/
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void shouldThrowIfNullIsGiven() {
|
||||
new QSort((List<OrderSpecifier<?>>) null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-402
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void sortBySingleProperty() {
|
||||
|
||||
QUser user = QUser.user;
|
||||
QSort qsort = new QSort(user.firstname.asc());
|
||||
|
||||
assertThat(qsort.getOrderSpecifiers().size(), is(1));
|
||||
assertThat((OrderSpecifier<String>) qsort.getOrderSpecifiers().get(0), is(user.firstname.asc()));
|
||||
assertThat(qsort.getOrderFor("firstname"), is(new Sort.Order(Sort.Direction.ASC, "firstname")));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-402
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void sortByMultiplyProperties() {
|
||||
|
||||
QUser user = QUser.user;
|
||||
QSort qsort = new QSort(user.firstname.asc(), user.lastname.desc());
|
||||
|
||||
assertThat(qsort.getOrderSpecifiers().size(), is(2));
|
||||
assertThat((OrderSpecifier<String>) qsort.getOrderSpecifiers().get(0), is(user.firstname.asc()));
|
||||
assertThat((OrderSpecifier<String>) qsort.getOrderSpecifiers().get(1), is(user.lastname.desc()));
|
||||
assertThat(qsort.getOrderFor("firstname"), is(new Sort.Order(Sort.Direction.ASC, "firstname")));
|
||||
assertThat(qsort.getOrderFor("lastname"), is(new Sort.Order(Sort.Direction.DESC, "lastname")));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-402
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void sortByMultiplyPropertiesWithAnd() {
|
||||
|
||||
QUser user = QUser.user;
|
||||
QSort qsort = new QSort(user.firstname.asc()).and(new QSort(user.lastname.desc()));
|
||||
|
||||
assertThat(qsort.getOrderSpecifiers().size(), is(2));
|
||||
assertThat((OrderSpecifier<String>) qsort.getOrderSpecifiers().get(0), is(user.firstname.asc()));
|
||||
assertThat((OrderSpecifier<String>) qsort.getOrderSpecifiers().get(1), is(user.lastname.desc()));
|
||||
assertThat(qsort.getOrderFor("firstname"), is(new Sort.Order(Sort.Direction.ASC, "firstname")));
|
||||
assertThat(qsort.getOrderFor("lastname"), is(new Sort.Order(Sort.Direction.DESC, "lastname")));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-402
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void sortByMultiplyPropertiesWithAndAndVarArgs() {
|
||||
|
||||
QUser user = QUser.user;
|
||||
QSort qsort = new QSort(user.firstname.asc()).and(user.lastname.desc());
|
||||
|
||||
assertThat(qsort.getOrderSpecifiers().size(), is(2));
|
||||
assertThat((OrderSpecifier<String>) qsort.getOrderSpecifiers().get(0), is(user.firstname.asc()));
|
||||
assertThat((OrderSpecifier<String>) qsort.getOrderSpecifiers().get(1), is(user.lastname.desc()));
|
||||
assertThat(qsort.getOrderFor("firstname"), is(new Sort.Order(Sort.Direction.ASC, "firstname")));
|
||||
assertThat(qsort.getOrderFor("lastname"), is(new Sort.Order(Sort.Direction.DESC, "lastname")));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-402
|
||||
*/
|
||||
@Test
|
||||
public void ensureInteroperabilityWithSort() {
|
||||
|
||||
QUser user = QUser.user;
|
||||
QSort qsort = new QSort(user.firstname.asc(), user.lastname.desc());
|
||||
|
||||
Sort sort = qsort;
|
||||
|
||||
assertThat(sort.getOrderFor("firstname"), is(new Sort.Order(Sort.Direction.ASC, "firstname")));
|
||||
assertThat(sort.getOrderFor("lastname"), is(new Sort.Order(Sort.Direction.DESC, "lastname")));
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011 the original author or authors.
|
||||
* Copyright 2011-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.
|
||||
@@ -19,8 +19,10 @@ import com.mysema.query.annotations.QueryEntity;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
@QueryEntity
|
||||
public class User {
|
||||
|
||||
String firstname;
|
||||
String lastname;
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.data.domain.AbstractPageRequest;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
@@ -48,9 +49,9 @@ public abstract class PageableDefaultUnitTests {
|
||||
static final int PAGE_SIZE = 47;
|
||||
static final int PAGE_NUMBER = 23;
|
||||
|
||||
static final PageRequest REFERENCE_WITHOUT_SORT = new PageRequest(PAGE_NUMBER, PAGE_SIZE);
|
||||
static final PageRequest REFERENCE_WITH_SORT = new PageRequest(PAGE_NUMBER, PAGE_SIZE, SORT);
|
||||
static final PageRequest REFERENCE_WITH_SORT_FIELDS = new PageRequest(PAGE_NUMBER, PAGE_SIZE, new Sort(SORT_FIELDS));
|
||||
static final AbstractPageRequest REFERENCE_WITHOUT_SORT = new PageRequest(PAGE_NUMBER, PAGE_SIZE);
|
||||
static final AbstractPageRequest REFERENCE_WITH_SORT = new PageRequest(PAGE_NUMBER, PAGE_SIZE, SORT);
|
||||
static final AbstractPageRequest REFERENCE_WITH_SORT_FIELDS = new PageRequest(PAGE_NUMBER, PAGE_SIZE, new Sort(SORT_FIELDS));
|
||||
|
||||
@Rule public ExpectedException exception = ExpectedException.none();
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@ import java.util.Collections;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.domain.AbstractPageRequest;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageImpl;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
@@ -110,7 +111,7 @@ public class PagedResourcesAssemblerUnitTests {
|
||||
resolver.setOneIndexedParameters(true);
|
||||
PagedResourcesAssembler<Person> assembler = new PagedResourcesAssembler<Person>(resolver, null);
|
||||
|
||||
PageRequest request = new PageRequest(0, 1);
|
||||
AbstractPageRequest request = new PageRequest(0, 1);
|
||||
Page<Person> page = new PageImpl<Person>(Collections.<Person> emptyList(), request, 0);
|
||||
|
||||
assembler.toResource(page);
|
||||
@@ -118,7 +119,7 @@ public class PagedResourcesAssemblerUnitTests {
|
||||
|
||||
private static Page<Person> createPage(int index) {
|
||||
|
||||
PageRequest request = new PageRequest(index, 1);
|
||||
AbstractPageRequest request = new PageRequest(index, 1);
|
||||
return new PageImpl<Person>(Arrays.asList(new Person()), request, 3);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user