Skip to content

Commit ff7f599

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 682e228 commit ff7f599

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
@@ -337,6 +337,7 @@ public static interface MatcherConfigurer<T> {
337337
*
338338
* @author Mark Paluch
339339
*/
340+
@EqualsAndHashCode
340341
public static class GenericPropertyMatcher {
341342

342343
StringMatcher stringMatcher = null;
@@ -634,10 +635,12 @@ public Object convert(Object source) {
634635
* Define specific property handling for a Dot-Path.
635636
*
636637
* @author Christoph Strobl
638+
* @author Mark Paluch
637639
* @since 1.12
638640
*/
639641
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
640642
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
643+
@EqualsAndHashCode
641644
public static class PropertySpecifier {
642645

643646
String path;
@@ -746,8 +749,10 @@ public Object transformValue(Object source) {
746749
* Define specific property handling for Dot-Paths.
747750
*
748751
* @author Christoph Strobl
752+
* @author Mark Paluch
749753
* @since 1.12
750754
*/
755+
@EqualsAndHashCode
751756
public static class PropertySpecifiers {
752757

753758
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}.
@@ -176,6 +175,47 @@ public void withCreatesNewInstance() throws Exception {
176175
assertThat(configuredExampleSpec.isIgnoreCaseEnabled(), is(true));
177176
}
178177

178+
/**
179+
* @see DATACMNS-900
180+
*/
181+
@Test
182+
public void shouldCompareUsingHashCodeAndEquals() throws Exception {
183+
184+
matcher = matching() //
185+
.withIgnorePaths("foo", "bar", "baz") //
186+
.withNullHandler(NullHandler.IGNORE) //
187+
.withIgnoreCase("ignored-case") //
188+
.withMatcher("hello", GenericPropertyMatchers.contains().caseSensitive()) //
189+
.withMatcher("world", new MatcherConfigurer<GenericPropertyMatcher>() {
190+
@Override
191+
public void configureMatcher(GenericPropertyMatcher matcher) {
192+
matcher.endsWith();
193+
}
194+
});
195+
196+
ExampleMatcher sameAsMatcher = matching() //
197+
.withIgnorePaths("foo", "bar", "baz") //
198+
.withNullHandler(NullHandler.IGNORE) //
199+
.withIgnoreCase("ignored-case") //
200+
.withMatcher("hello", GenericPropertyMatchers.contains().caseSensitive()) //
201+
.withMatcher("world", new MatcherConfigurer<GenericPropertyMatcher>() {
202+
@Override
203+
public void configureMatcher(GenericPropertyMatcher matcher) {
204+
matcher.endsWith();
205+
}
206+
});
207+
208+
ExampleMatcher different = matching() //
209+
.withIgnorePaths("foo", "bar", "baz") //
210+
.withNullHandler(NullHandler.IGNORE) //
211+
.withMatcher("hello", GenericPropertyMatchers.contains().ignoreCase());
212+
213+
assertThat(matcher.hashCode(), is(sameAsMatcher.hashCode()));
214+
assertThat(matcher.hashCode(), is(not(different.hashCode())));
215+
assertThat(matcher, is(equalTo(sameAsMatcher)));
216+
assertThat(matcher, is(not(equalTo(different))));
217+
}
218+
179219
static class Person {
180220

181221
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)