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,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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user