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,144 @@
|
||||
/*
|
||||
* 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 java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Abstract Java Bean implementation of {@code Pageable}.
|
||||
*
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
public abstract class AbstractPageRequest implements Pageable, Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1232825578694716871L;
|
||||
|
||||
private final int page;
|
||||
private final int size;
|
||||
|
||||
/**
|
||||
* Creates a new {@link AbstractPageRequest}. Pages are zero indexed, thus providing 0 for {@code page} will return
|
||||
* the first page.
|
||||
*
|
||||
* @param page
|
||||
* @param size
|
||||
*/
|
||||
public AbstractPageRequest(int page, int size) {
|
||||
|
||||
if (page < 0) {
|
||||
throw new IllegalArgumentException("Page index must not be less than zero!");
|
||||
}
|
||||
|
||||
if (size < 1) {
|
||||
throw new IllegalArgumentException("Page size must not be less than one!");
|
||||
}
|
||||
|
||||
this.page = page;
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.domain.Pageable#getPageSize()
|
||||
*/
|
||||
public int getPageSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.domain.Pageable#getPageNumber()
|
||||
*/
|
||||
public int getPageNumber() {
|
||||
return page;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.domain.Pageable#getOffset()
|
||||
*/
|
||||
public int getOffset() {
|
||||
return page * size;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.domain.Pageable#hasPrevious()
|
||||
*/
|
||||
public boolean hasPrevious() {
|
||||
return page > 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.domain.Pageable#next()
|
||||
*/
|
||||
public abstract Pageable next();
|
||||
|
||||
/**
|
||||
* Returns the {@link Pageable} requesting the previous {@link Page}.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public abstract Pageable previous();
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.domain.Pageable#first()
|
||||
*/
|
||||
public abstract Pageable first();
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.domain.Pageable#previousOrFirst()
|
||||
*/
|
||||
public Pageable previousOrFirst() {
|
||||
return hasPrevious() ? previous() : first();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#hashCode()
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + page;
|
||||
result = prime * result + size;
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
AbstractPageRequest other = (AbstractPageRequest) obj;
|
||||
if (page != other.page)
|
||||
return false;
|
||||
if (size != other.size)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -15,21 +15,18 @@
|
||||
*/
|
||||
package org.springframework.data.domain;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.springframework.data.domain.Sort.Direction;
|
||||
|
||||
/**
|
||||
* Basic Java Bean implementation of {@code Pageable}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
public class PageRequest implements Pageable, Serializable {
|
||||
public class PageRequest extends AbstractPageRequest {
|
||||
|
||||
private static final long serialVersionUID = 8280485938848398236L;
|
||||
private static final long serialVersionUID = -4541509938956089562L;
|
||||
|
||||
private final int page;
|
||||
private final int size;
|
||||
private final Sort sort;
|
||||
|
||||
/**
|
||||
@@ -63,45 +60,10 @@ public class PageRequest implements Pageable, Serializable {
|
||||
* @param sort can be {@literal null}.
|
||||
*/
|
||||
public PageRequest(int page, int size, Sort sort) {
|
||||
|
||||
if (page < 0) {
|
||||
throw new IllegalArgumentException("Page index must not be less than zero!");
|
||||
}
|
||||
|
||||
if (size < 1) {
|
||||
throw new IllegalArgumentException("Page size must not be less than one!");
|
||||
}
|
||||
|
||||
this.page = page;
|
||||
this.size = size;
|
||||
super(page, size);
|
||||
this.sort = sort;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.domain.Pageable#getPageSize()
|
||||
*/
|
||||
public int getPageSize() {
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.domain.Pageable#getPageNumber()
|
||||
*/
|
||||
public int getPageNumber() {
|
||||
return page;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.domain.Pageable#getOffset()
|
||||
*/
|
||||
public int getOffset() {
|
||||
return page * size;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.domain.Pageable#getSort()
|
||||
@@ -110,28 +72,20 @@ public class PageRequest implements Pageable, Serializable {
|
||||
return sort;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.domain.Pageable#hasPrevious()
|
||||
*/
|
||||
public boolean hasPrevious() {
|
||||
return page > 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.domain.Pageable#next()
|
||||
*/
|
||||
public Pageable next() {
|
||||
return new PageRequest(page + 1, size, sort);
|
||||
return new PageRequest(getPageNumber() + 1, getPageSize(), getSort());
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.domain.Pageable#previousOrFirst()
|
||||
* @see org.springframework.data.domain.AbstractPageRequest#previous()
|
||||
*/
|
||||
public Pageable previousOrFirst() {
|
||||
return hasPrevious() ? new PageRequest(page - 1, size, sort) : this;
|
||||
public PageRequest previous() {
|
||||
return getPageNumber() == 0 ? this : new PageRequest(getPageNumber() - 1, getPageSize(), getSort());
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -139,7 +93,7 @@ public class PageRequest implements Pageable, Serializable {
|
||||
* @see org.springframework.data.domain.Pageable#first()
|
||||
*/
|
||||
public Pageable first() {
|
||||
return new PageRequest(0, size, sort);
|
||||
return new PageRequest(0, getPageSize(), getSort());
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -159,12 +113,9 @@ public class PageRequest implements Pageable, Serializable {
|
||||
|
||||
PageRequest that = (PageRequest) obj;
|
||||
|
||||
boolean pageEqual = this.page == that.page;
|
||||
boolean sizeEqual = this.size == that.size;
|
||||
|
||||
boolean sortEqual = this.sort == null ? that.sort == null : this.sort.equals(that.sort);
|
||||
|
||||
return pageEqual && sizeEqual && sortEqual;
|
||||
return super.equals(that) && sortEqual;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -173,14 +124,7 @@ public class PageRequest implements Pageable, Serializable {
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
|
||||
int result = 17;
|
||||
|
||||
result = 31 * result + page;
|
||||
result = 31 * result + size;
|
||||
result = 31 * result + (null == sort ? 0 : sort.hashCode());
|
||||
|
||||
return result;
|
||||
return 31 * super.hashCode() + (null == sort ? 0 : sort.hashCode());
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -189,7 +133,7 @@ public class PageRequest implements Pageable, Serializable {
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("Page request [number: %d, size %d, sort: %s]", page, size,
|
||||
return String.format("Page request [number: %d, size %d, sort: %s]", getPageNumber(), getPageSize(),
|
||||
sort == null ? null : sort.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
* 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 org.springframework.data.domain.AbstractPageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
|
||||
import com.mysema.query.types.OrderSpecifier;
|
||||
|
||||
/**
|
||||
* Basic Java Bean implementation of {@link Pageable} with support for QueryDSL.
|
||||
*
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
public class QPageRequest extends AbstractPageRequest {
|
||||
|
||||
private static final long serialVersionUID = 7529171950267879273L;
|
||||
|
||||
private final QSort sort;
|
||||
|
||||
/**
|
||||
* Creates a new {@link QPageRequest}. Pages are zero indexed, thus providing 0 for {@code page} will return the first
|
||||
* page.
|
||||
*
|
||||
* @param page
|
||||
* @param size
|
||||
*/
|
||||
public QPageRequest(int page, int size) {
|
||||
this(page, size, (QSort) null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link QPageRequest} with the given {@link OrderSpecifier}s applied.
|
||||
*
|
||||
* @param page
|
||||
* @param size
|
||||
* @param orderSpecifiers must not be {@literal null} or empty;
|
||||
*/
|
||||
public QPageRequest(int page, int size, OrderSpecifier<?>... orderSpecifiers) {
|
||||
this(page, size, new QSort(orderSpecifiers));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link QPageRequest} with sort parameters applied.
|
||||
*
|
||||
* @param page
|
||||
* @param size
|
||||
* @param sort
|
||||
*/
|
||||
public QPageRequest(int page, int size, QSort sort) {
|
||||
super(page, size);
|
||||
|
||||
this.sort = sort;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.domain.Pageable#getSort()
|
||||
*/
|
||||
@Override
|
||||
public QSort getSort() {
|
||||
return sort;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.domain.AbstractPageRequest#next()
|
||||
*/
|
||||
@Override
|
||||
public Pageable next() {
|
||||
return new QPageRequest(getPageNumber() + 1, getPageSize(), getSort());
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.domain.AbstractPageRequest#previous()
|
||||
*/
|
||||
@Override
|
||||
public Pageable previous() {
|
||||
return new QPageRequest(getPageNumber() - 1, getPageSize(), getSort());
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.domain.AbstractPageRequest#first()
|
||||
*/
|
||||
@Override
|
||||
public Pageable first() {
|
||||
return new QPageRequest(0, getPageSize(), getSort());
|
||||
}
|
||||
}
|
||||
153
src/main/java/org/springframework/data/querydsl/QSort.java
Normal file
153
src/main/java/org/springframework/data/querydsl/QSort.java
Normal file
@@ -0,0 +1,153 @@
|
||||
/*
|
||||
* 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 java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import com.mysema.query.types.Expression;
|
||||
import com.mysema.query.types.OrderSpecifier;
|
||||
|
||||
/**
|
||||
* Sort option for queries that wraps a querydsl {@link OrderSpecifier}.
|
||||
*
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
public class QSort extends Sort implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = -6701117396842171930L;
|
||||
|
||||
private final List<OrderSpecifier<?>> orderSpecifiers;
|
||||
|
||||
/**
|
||||
* Creates a new {@link QSort} instance with the given {@link OrderSpecifier}s.
|
||||
*
|
||||
* @param orderSpecifiers must not be {@literal null} or empty;
|
||||
*/
|
||||
public QSort(OrderSpecifier<?>... orderSpecifiers) {
|
||||
this(Arrays.asList(orderSpecifiers));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link QSort} instance with the given {@link OrderSpecifier}s.
|
||||
*
|
||||
* @param orderSpecifiers must not be {@literal null} or empty;
|
||||
*/
|
||||
public QSort(List<OrderSpecifier<?>> orderSpecifiers) {
|
||||
super(toOrders(orderSpecifiers));
|
||||
Assert.notEmpty(orderSpecifiers, "Order specifiers must not be null or empty!");
|
||||
this.orderSpecifiers = orderSpecifiers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the given {@link OrderSpecifier}s into a list of {@link Order}s.
|
||||
*
|
||||
* @param orderSpecifiers must not be {@literal null} or empty.
|
||||
* @return
|
||||
*/
|
||||
private static List<Order> toOrders(List<OrderSpecifier<?>> orderSpecifiers) {
|
||||
|
||||
Assert.notEmpty(orderSpecifiers, "Order specifiers must not be null or empty!");
|
||||
|
||||
List<Order> orders = new ArrayList<Sort.Order>();
|
||||
for (OrderSpecifier<?> orderSpecifier : orderSpecifiers) {
|
||||
orders.add(toOrder(orderSpecifier));
|
||||
}
|
||||
|
||||
return orders;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the given {@link OrderSpecifier} into an {@link Order}.
|
||||
*
|
||||
* @param orderSpecifier must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
private static Order toOrder(OrderSpecifier<?> orderSpecifier) {
|
||||
|
||||
Assert.notNull(orderSpecifier, "Order specifier must not be null!");
|
||||
|
||||
Expression<?> target = orderSpecifier.getTarget();
|
||||
Object targetElement = ((com.mysema.query.types.Path) target).getMetadata().getElement();
|
||||
|
||||
Assert.notNull(targetElement, "Target element must not be null!");
|
||||
|
||||
return new Order(targetElement.toString()).with(orderSpecifier.isAscending() ? Direction.ASC : Direction.DESC);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the orderSpecifier
|
||||
*/
|
||||
public List<OrderSpecifier<?>> getOrderSpecifiers() {
|
||||
return orderSpecifiers;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.domain.Sort#and(org.springframework.data.domain.Sort)
|
||||
*/
|
||||
@Override
|
||||
public Sort and(Sort sort) {
|
||||
throw new UnsupportedOperationException("You cannot combine a querydsl based QSort with an arbitrary Sort!");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new {@link QSort} consisting of the {@link OrderSpecifier}s of the current {@code QSort} combined with
|
||||
* the ones from the given {@code QSort}.
|
||||
*
|
||||
* @param sort can be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
public QSort and(QSort sort) {
|
||||
return sort == null ? this : and(sort.getOrderSpecifiers());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new {@link QSort} consisting of the {@link OrderSpecifier}s of the current {@link QSort} combined with
|
||||
* the given ones.
|
||||
*
|
||||
* @param orderSpecifiers must not be {@literal null} or empty.
|
||||
* @return
|
||||
*/
|
||||
public QSort and(List<OrderSpecifier<?>> orderSpecifiers) {
|
||||
|
||||
Assert.notEmpty(orderSpecifiers, "OrderSpecifiers must not be null or empty!");
|
||||
|
||||
List<OrderSpecifier<?>> newOrderSpecifiers = new ArrayList<OrderSpecifier<?>>(this.orderSpecifiers);
|
||||
newOrderSpecifiers.addAll(orderSpecifiers);
|
||||
|
||||
return new QSort(newOrderSpecifiers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new {@link QSort} consisting of the {@link OrderSpecifier}s of the current {@link QSort} combined with
|
||||
* the given ones.
|
||||
*
|
||||
* @param orderSpecifiers must not be {@literal null} or empty.
|
||||
* @return
|
||||
*/
|
||||
public QSort and(OrderSpecifier<?>... orderSpecifiers) {
|
||||
|
||||
Assert.notEmpty(orderSpecifiers, "OrderSpecifiers must not be null or empty!");
|
||||
|
||||
return and(Arrays.asList(orderSpecifiers));
|
||||
}
|
||||
}
|
||||
@@ -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