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