DATACMNS-867 - Moved to new factory methods for Sort.
Codebase now uses Sort.by(…) where possible instead of the deprecated new Sort(…).
This commit is contained in:
@@ -55,7 +55,7 @@ public class PageRequest extends AbstractPageRequest {
|
||||
*/
|
||||
@Deprecated
|
||||
public PageRequest(int page, int size, Direction direction, String... properties) {
|
||||
this(page, size, new Sort(direction, properties));
|
||||
this(page, size, Sort.by(direction, properties));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -83,7 +83,7 @@ public class PageRequest extends AbstractPageRequest {
|
||||
}
|
||||
|
||||
public static PageRequest of(int page, int size, Direction direction, String... properties) {
|
||||
return of(page, size, new Sort(direction, properties));
|
||||
return of(page, size, Sort.by(direction, properties));
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -39,7 +39,7 @@ public class Sort implements Iterable<org.springframework.data.domain.Sort.Order
|
||||
|
||||
private static final long serialVersionUID = 5737186511678863905L;
|
||||
|
||||
private static final Sort UNSORTED = new Sort(new Order[0]);
|
||||
private static final Sort UNSORTED = Sort.by(new Order[0]);
|
||||
|
||||
public static final Direction DEFAULT_DIRECTION = Direction.ASC;
|
||||
|
||||
@@ -93,7 +93,7 @@ public class Sort implements Iterable<org.springframework.data.domain.Sort.Order
|
||||
/**
|
||||
* Creates a new {@link Sort} instance.
|
||||
*
|
||||
* @param direction defaults to {@linke Sort#DEFAULT_DIRECTION} (for {@literal null} cases, too)
|
||||
* @param direction defaults to {@link Sort#DEFAULT_DIRECTION} (for {@literal null} cases, too)
|
||||
* @param properties must not be {@literal null} or contain {@literal null} or empty strings.
|
||||
*/
|
||||
public Sort(Direction direction, List<String> properties) {
|
||||
@@ -148,6 +148,24 @@ public class Sort implements Iterable<org.springframework.data.domain.Sort.Order
|
||||
return new Sort(orders);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link Sort} for the given {@link Order}s.
|
||||
*
|
||||
* @param direction must not be {@literal null}.
|
||||
* @param properties must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
public static Sort by(Direction direction, String... properties) {
|
||||
|
||||
Assert.notNull(direction, "Direction must not be null!");
|
||||
Assert.notNull(properties, "Properties must not be null!");
|
||||
Assert.isTrue(properties.length > 0, "At least one property must be given!");
|
||||
|
||||
return Sort.by(Arrays.stream(properties)//
|
||||
.map(it -> new Order(direction, it))//
|
||||
.collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a {@link Sort} instances representing no sorting setup at all.
|
||||
*
|
||||
@@ -419,6 +437,10 @@ public class Sort implements Iterable<org.springframework.data.domain.Sort.Order
|
||||
this(DEFAULT_DIRECTION, property);
|
||||
}
|
||||
|
||||
public static Order by(String property) {
|
||||
return new Order(property);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link Order} instance. if order is {@literal null} then order defaults to
|
||||
* {@link Sort#DEFAULT_DIRECTION}
|
||||
@@ -515,7 +537,7 @@ public class Sort implements Iterable<org.springframework.data.domain.Sort.Order
|
||||
* @return
|
||||
*/
|
||||
public Sort withProperties(String... properties) {
|
||||
return new Sort(this.direction, properties);
|
||||
return Sort.by(this.direction, properties);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -178,7 +178,7 @@ public class SortHandlerMethodArgumentResolver implements SortArgumentResolver {
|
||||
return Sort.unsorted();
|
||||
}
|
||||
|
||||
return sortOrNull.and(new Sort(sortDefault.direction(), fields));
|
||||
return sortOrNull.and(Sort.by(sortDefault.direction(), fields));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -43,7 +43,7 @@ public class PageRequestUnitTests extends AbstractPageRequestUnitTests {
|
||||
@Test
|
||||
public void equalsRegardsSortCorrectly() {
|
||||
|
||||
Sort sort = new Sort(Direction.DESC, "foo");
|
||||
Sort sort = Sort.by(Direction.DESC, "foo");
|
||||
AbstractPageRequest request = PageRequest.of(0, 10, sort);
|
||||
|
||||
// Equals itself
|
||||
|
||||
@@ -38,9 +38,7 @@ public class SortUnitTests {
|
||||
*/
|
||||
@Test
|
||||
public void appliesDefaultForOrder() throws Exception {
|
||||
|
||||
assertThat(Sort.by("foo").iterator().next().getDirection()).isEqualTo(Sort.DEFAULT_DIRECTION);
|
||||
assertThat(new Sort((Direction) null, "foo").iterator().next().getDirection()).isEqualTo(Sort.DEFAULT_DIRECTION);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -51,7 +49,7 @@ public class SortUnitTests {
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void preventsNullProperties() throws Exception {
|
||||
|
||||
new Sort(Direction.ASC, (String[]) null);
|
||||
Sort.by(Direction.ASC, (String[]) null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -62,7 +60,7 @@ public class SortUnitTests {
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void preventsNullProperty() throws Exception {
|
||||
|
||||
new Sort(Direction.ASC, (String) null);
|
||||
Sort.by(Direction.ASC, (String) null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -73,7 +71,7 @@ public class SortUnitTests {
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void preventsEmptyProperty() throws Exception {
|
||||
|
||||
new Sort(Direction.ASC, "");
|
||||
Sort.by(Direction.ASC, "");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -84,7 +82,7 @@ public class SortUnitTests {
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void preventsNoProperties() throws Exception {
|
||||
|
||||
new Sort(Direction.ASC);
|
||||
Sort.by(Direction.ASC);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -152,4 +150,12 @@ public class SortUnitTests {
|
||||
assertThat(result.getNullHandling()).isEqualTo(source.getNullHandling());
|
||||
assertThat(result.isIgnoreCase()).isEqualTo(source.isIgnoreCase());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void preventsNullDirection() {
|
||||
|
||||
assertThatExceptionOfType(IllegalArgumentException.class)//
|
||||
.isThrownBy(() -> Sort.by((Direction) null, "foo"))//
|
||||
.withMessageContaining("Direction");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ public class SpringDataJaxbUnitTests {
|
||||
Marshaller marshaller;
|
||||
Unmarshaller unmarshaller;
|
||||
|
||||
Sort sort = new Sort(Direction.ASC, "firstname", "lastname");
|
||||
Sort sort = Sort.by(Direction.ASC, "firstname", "lastname");
|
||||
Pageable pageable = PageRequest.of(2, 15, sort);
|
||||
Resource resource = new ClassPathResource("pageable.xml", this.getClass());
|
||||
Resource schemaFile = new ClassPathResource("spring-data-jaxb.xsd", this.getClass());
|
||||
|
||||
@@ -114,7 +114,7 @@ public class QSortUnitTests {
|
||||
QUser user = QUser.user;
|
||||
QSort sort = new QSort(user.firstname.asc());
|
||||
|
||||
Sort result = sort.and(new Sort(Direction.ASC, "lastname"));
|
||||
Sort result = sort.and(Sort.by(Direction.ASC, "lastname"));
|
||||
assertThat(result).hasSize(2);
|
||||
assertThat(result).contains(new Order(Direction.ASC, "lastname"), new Order(Direction.ASC, "firstname"));
|
||||
}
|
||||
@@ -125,7 +125,7 @@ public class QSortUnitTests {
|
||||
QUser user = QUser.user;
|
||||
QSort sort = new QSort(user.dateOfBirth.yearMonth().asc());
|
||||
|
||||
Sort result = sort.and(new Sort(Direction.ASC, "lastname"));
|
||||
Sort result = sort.and(Sort.by(Direction.ASC, "lastname"));
|
||||
assertThat(result).hasSize(2);
|
||||
assertThat(result).contains(new Order(Direction.ASC, "lastname"),
|
||||
new Order(Direction.ASC, user.dateOfBirth.yearMonth().toString()));
|
||||
|
||||
@@ -121,7 +121,7 @@ public class ExtensionAwareEvaluationContextProviderUnitTests {
|
||||
public void exposesPageableParameter() throws Exception {
|
||||
|
||||
this.method = SampleRepo.class.getMethod("findByFirstname", String.class, Pageable.class);
|
||||
PageRequest pageable = PageRequest.of(2, 3, new Sort(Direction.DESC, "lastname"));
|
||||
PageRequest pageable = PageRequest.of(2, 3, Sort.by(Direction.DESC, "lastname"));
|
||||
|
||||
assertThat(evaluateExpression("#pageable.offset", new Object[] { "test", pageable })).isEqualTo(6L);
|
||||
assertThat(evaluateExpression("#pageable.pageSize", new Object[] { "test", pageable })).isEqualTo(3);
|
||||
@@ -133,7 +133,7 @@ public class ExtensionAwareEvaluationContextProviderUnitTests {
|
||||
public void exposesSortParameter() throws Exception {
|
||||
|
||||
this.method = SampleRepo.class.getMethod("findByFirstname", String.class, Sort.class);
|
||||
Sort sort = new Sort(Direction.DESC, "lastname");
|
||||
Sort sort = Sort.by(Direction.DESC, "lastname");
|
||||
|
||||
assertThat(evaluateExpression("#sort.toString()", new Object[] { "test", sort })).isEqualTo("lastname: DESC");
|
||||
}
|
||||
|
||||
@@ -44,7 +44,6 @@ public class OrderBySourceUnitTests {
|
||||
|
||||
OrderBySource orderBySource = new OrderBySource("LastnameAscUsernameDesc");
|
||||
assertThat(orderBySource.toSort()).isEqualTo(Sort.by("lastname").ascending().and(Sort.by("username").descending()));
|
||||
// assertThat(orderBySource.toSort()).hasValue(new Sort(new Order(ASC, "lastname"), new Order(DESC, "username")));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
|
||||
@@ -37,9 +37,9 @@ public class HateoasSortHandlerMethodArgumentResolverUnitTests extends SortHandl
|
||||
public void buildsUpRequestParameters() throws Exception {
|
||||
|
||||
assertUriStringFor(SORT, "sort=firstname,lastname,desc");
|
||||
assertUriStringFor(new Sort(ASC, "foo").and(new Sort(DESC, "bar").and(new Sort(ASC, "foobar"))),
|
||||
assertUriStringFor(Sort.by(ASC, "foo").and(Sort.by(DESC, "bar").and(Sort.by(ASC, "foobar"))),
|
||||
"sort=foo,asc&sort=bar,desc&sort=foobar,asc");
|
||||
assertUriStringFor(new Sort(ASC, "foo").and(new Sort(ASC, "bar").and(new Sort(DESC, "foobar"))),
|
||||
assertUriStringFor(Sort.by(ASC, "foo").and(Sort.by(ASC, "bar").and(Sort.by(DESC, "foobar"))),
|
||||
"sort=foo,bar,asc&sort=foobar,desc");
|
||||
}
|
||||
|
||||
|
||||
@@ -44,7 +44,7 @@ public abstract class SortDefaultUnitTests {
|
||||
static final String[] SORT_FIELDS = new String[] { "firstname", "lastname" };
|
||||
static final Direction SORT_DIRECTION = Direction.DESC;
|
||||
|
||||
static final Sort SORT = new Sort(SORT_DIRECTION, SORT_FIELDS);
|
||||
static final Sort SORT = Sort.by(SORT_DIRECTION, SORT_FIELDS);
|
||||
|
||||
@Rule public ExpectedException exception = ExpectedException.none();
|
||||
|
||||
|
||||
@@ -55,7 +55,7 @@ public class SortHandlerMethodArgumentResolverUnitTests extends SortDefaultUnitT
|
||||
|
||||
MethodParameter parameter = TestUtils.getParameterOfMethod(getControllerClass(), "unsupportedMethod", String.class);
|
||||
SortHandlerMethodArgumentResolver resolver = new SortHandlerMethodArgumentResolver();
|
||||
Sort fallbackSort = new Sort(Direction.ASC, "ID");
|
||||
Sort fallbackSort = Sort.by(Direction.ASC, "ID");
|
||||
resolver.setFallbackSort(fallbackSort);
|
||||
|
||||
Sort sort = resolver.resolveArgument(parameter, null, new ServletWebRequest(new MockHttpServletRequest()), null);
|
||||
@@ -123,7 +123,7 @@ public class SortHandlerMethodArgumentResolverUnitTests extends SortDefaultUnitT
|
||||
|
||||
SortHandlerMethodArgumentResolver resolver = new SortHandlerMethodArgumentResolver();
|
||||
Sort result = resolver.resolveArgument(parameter, null, new ServletWebRequest(request), null);
|
||||
assertThat(result).isEqualTo(new Sort(Direction.ASC, "firstname", "lastname"));
|
||||
assertThat(result).isEqualTo(Sort.by(Direction.ASC, "firstname", "lastname"));
|
||||
}
|
||||
|
||||
@Test // DATACMNS-408
|
||||
@@ -150,7 +150,7 @@ public class SortHandlerMethodArgumentResolverUnitTests extends SortDefaultUnitT
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.addParameter("sort", "property1,,DESC");
|
||||
|
||||
assertThat(resolveSort(request, PARAMETER)).isEqualTo(new Sort(DESC, "property1"));
|
||||
assertThat(resolveSort(request, PARAMETER)).isEqualTo(Sort.by(DESC, "property1"));
|
||||
}
|
||||
|
||||
@Test // DATACMNS-408
|
||||
@@ -160,7 +160,7 @@ public class SortHandlerMethodArgumentResolverUnitTests extends SortDefaultUnitT
|
||||
request.addParameter("sort", "property,DESC");
|
||||
request.addParameter("sort", "");
|
||||
|
||||
assertThat(resolveSort(request, PARAMETER)).isEqualTo(new Sort(DESC, "property"));
|
||||
assertThat(resolveSort(request, PARAMETER)).isEqualTo(Sort.by(DESC, "property"));
|
||||
}
|
||||
|
||||
@Test // DATACMNS-379
|
||||
|
||||
Reference in New Issue
Block a user