Skip to content

Commit ac51b16

Browse files
mp911deodrotbohm
authored andcommitted
DATACMNS-900 - Added equals(…) and hashCode() methods for ExampleMatcher utility classes.
We now provide equals(…) and hashCode() methods for GenericPropertyMatcher, PropertySpecifier and PropertySpecifiers so that they can be compared and used in sets. Original pull request: #175.
1 parent b7f4709 commit ac51b16

File tree

3 files changed

+68
-3
lines changed

3 files changed

+68
-3
lines changed

src/main/java/org/springframework/data/domain/ExampleMatcher.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ public static interface MatcherConfigurer<T> {
389389
*
390390
* @author Mark Paluch
391391
*/
392+
@EqualsAndHashCode
392393
public static class GenericPropertyMatcher {
393394

394395
StringMatcher stringMatcher = null;
@@ -686,10 +687,12 @@ public Object convert(Object source) {
686687
* Define specific property handling for a Dot-Path.
687688
*
688689
* @author Christoph Strobl
690+
* @author Mark Paluch
689691
* @since 1.12
690692
*/
691693
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
692694
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
695+
@EqualsAndHashCode
693696
public static class PropertySpecifier {
694697

695698
String path;
@@ -798,8 +801,10 @@ public Object transformValue(Object source) {
798801
* Define specific property handling for Dot-Paths.
799802
*
800803
* @author Christoph Strobl
804+
* @author Mark Paluch
801805
* @since 1.12
802806
*/
807+
@EqualsAndHashCode
803808
public static class PropertySpecifiers {
804809

805810
private final Map<String, PropertySpecifier> propertySpecifiers = new LinkedHashMap<String, PropertySpecifier>();

src/test/java/org/springframework/data/domain/ExampleMatcherUnitTests.java

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222

2323
import org.junit.Before;
2424
import org.junit.Test;
25-
import org.springframework.data.domain.ExampleMatcher.NullHandler;
26-
import org.springframework.data.domain.ExampleMatcher.StringMatcher;
25+
import org.springframework.data.domain.ExampleMatcher.*;
2726

2827
/**
2928
* Unit test for {@link ExampleMatcher}.
@@ -207,6 +206,47 @@ public void anyMatcherYieldsAnyMatching() {
207206
assertThat(matchingAny().isAllMatching(), is(false));
208207
}
209208

209+
/**
210+
* @see DATACMNS-900
211+
*/
212+
@Test
213+
public void shouldCompareUsingHashCodeAndEquals() throws Exception {
214+
215+
matcher = matching() //
216+
.withIgnorePaths("foo", "bar", "baz") //
217+
.withNullHandler(NullHandler.IGNORE) //
218+
.withIgnoreCase("ignored-case") //
219+
.withMatcher("hello", GenericPropertyMatchers.contains().caseSensitive()) //
220+
.withMatcher("world", new MatcherConfigurer<GenericPropertyMatcher>() {
221+
@Override
222+
public void configureMatcher(GenericPropertyMatcher matcher) {
223+
matcher.endsWith();
224+
}
225+
});
226+
227+
ExampleMatcher sameAsMatcher = matching() //
228+
.withIgnorePaths("foo", "bar", "baz") //
229+
.withNullHandler(NullHandler.IGNORE) //
230+
.withIgnoreCase("ignored-case") //
231+
.withMatcher("hello", GenericPropertyMatchers.contains().caseSensitive()) //
232+
.withMatcher("world", new MatcherConfigurer<GenericPropertyMatcher>() {
233+
@Override
234+
public void configureMatcher(GenericPropertyMatcher matcher) {
235+
matcher.endsWith();
236+
}
237+
});
238+
239+
ExampleMatcher different = matching() //
240+
.withIgnorePaths("foo", "bar", "baz") //
241+
.withNullHandler(NullHandler.IGNORE) //
242+
.withMatcher("hello", GenericPropertyMatchers.contains().ignoreCase());
243+
244+
assertThat(matcher.hashCode(), is(sameAsMatcher.hashCode()));
245+
assertThat(matcher.hashCode(), is(not(different.hashCode())));
246+
assertThat(matcher, is(equalTo(sameAsMatcher)));
247+
assertThat(matcher, is(not(equalTo(different))));
248+
}
249+
210250
static class Person {
211251

212252
String firstname;

src/test/java/org/springframework/data/domain/ExampleUnitTests.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717

1818
import static org.hamcrest.CoreMatchers.*;
1919
import static org.junit.Assert.*;
20+
import static org.springframework.data.domain.ExampleMatcher.*;
2021

2122
import org.junit.Before;
2223
import org.junit.Test;
24+
import org.springframework.data.domain.ExampleMatcher.*;
2325

2426
/**
2527
* Test for {@link Example}.
@@ -54,10 +56,28 @@ public void rejectsNullProbe() {
5456
* @see DATACMNS-810
5557
*/
5658
@Test
57-
public void retunsSampleObjectsClassAsProbeType() {
59+
public void returnsSampleObjectsClassAsProbeType() {
5860
assertThat(example.getProbeType(), is(equalTo(Person.class)));
5961
}
6062

63+
/**
64+
* @see DATACMNS-900
65+
*/
66+
@Test
67+
public void shouldCompareUsingHashCodeAndEquals() throws Exception {
68+
69+
Example<Person> example = Example.of(person, matching().withIgnoreCase("firstname"));
70+
Example<Person> sameAsExample = Example.of(person, matching().withIgnoreCase("firstname"));
71+
72+
Example<Person> different = Example.of(person,
73+
matching().withMatcher("firstname", GenericPropertyMatchers.contains()));
74+
75+
assertThat(example.hashCode(), is(sameAsExample.hashCode()));
76+
assertThat(example.hashCode(), is(not(different.hashCode())));
77+
assertThat(example, is(equalTo(sameAsExample)));
78+
assertThat(example, is(not(equalTo(different))));
79+
}
80+
6181
static class Person {
6282
String firstname;
6383
}

0 commit comments

Comments
 (0)