DATACMNS-1450 - Introduced type-safe Sort API.

We now expose a TypedSort that can use method handles to define properties to sort by.

Sort.sort(Person.class).by(Person::getName).ascending();

Related tickets: DATACMNS-1449.
This commit is contained in:
Oliver Drotbohm
2018-12-13 16:47:21 +01:00
parent 34d95fb7e4
commit 34507ec000
2 changed files with 121 additions and 0 deletions

View File

@@ -18,6 +18,10 @@ package org.springframework.data.domain;
import static org.assertj.core.api.Assertions.*;
import static org.springframework.data.domain.Sort.NullHandling.*;
import lombok.Getter;
import java.util.Collection;
import org.junit.Test;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
@@ -172,4 +176,28 @@ public class SortUnitTests {
.isThrownBy(() -> Sort.by((Direction) null, "foo"))//
.withMessageContaining("Direction");
}
@Test // DATACMNS-1450
public void translatesTypedSortCorrectly() {
assertThat(Sort.sort(Sample.class).by(Sample::getNested).by(Nested::getFirstname)) //
.containsExactly(Order.by("nested.firstname"));
assertThat(Sort.sort(Sample.class).by((Sample it) -> it.getNested().getFirstname())) //
.containsExactly(Order.by("nested.firstname"));
assertThat(Sort.sort(Sample.class).by(Sample::getNesteds).by(Nested::getFirstname)) //
.containsExactly(Order.by("nesteds.firstname"));
}
@Getter
static class Sample {
Nested nested;
Collection<Nested> nesteds;
}
@Getter
static class Nested {
String firstname;
}
}