diff --git a/src/main/java/org/springframework/data/domain/ExampleMatcher.java b/src/main/java/org/springframework/data/domain/ExampleMatcher.java index ecced45ee..a9b4f691c 100644 --- a/src/main/java/org/springframework/data/domain/ExampleMatcher.java +++ b/src/main/java/org/springframework/data/domain/ExampleMatcher.java @@ -337,6 +337,7 @@ public class ExampleMatcher { * * @author Mark Paluch */ + @EqualsAndHashCode public static class GenericPropertyMatcher { StringMatcher stringMatcher = null; @@ -634,10 +635,12 @@ public class ExampleMatcher { * Define specific property handling for a Dot-Path. * * @author Christoph Strobl + * @author Mark Paluch * @since 1.12 */ @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) @RequiredArgsConstructor(access = AccessLevel.PRIVATE) + @EqualsAndHashCode public static class PropertySpecifier { String path; @@ -746,8 +749,10 @@ public class ExampleMatcher { * Define specific property handling for Dot-Paths. * * @author Christoph Strobl + * @author Mark Paluch * @since 1.12 */ + @EqualsAndHashCode public static class PropertySpecifiers { private final Map propertySpecifiers = new LinkedHashMap(); diff --git a/src/test/java/org/springframework/data/domain/ExampleMatcherUnitTests.java b/src/test/java/org/springframework/data/domain/ExampleMatcherUnitTests.java index 62cbefbf8..2626f0247 100644 --- a/src/test/java/org/springframework/data/domain/ExampleMatcherUnitTests.java +++ b/src/test/java/org/springframework/data/domain/ExampleMatcherUnitTests.java @@ -22,8 +22,7 @@ import static org.springframework.data.domain.ExampleMatcher.*; import org.junit.Before; import org.junit.Test; -import org.springframework.data.domain.ExampleMatcher.NullHandler; -import org.springframework.data.domain.ExampleMatcher.StringMatcher; +import org.springframework.data.domain.ExampleMatcher.*; /** * Unit test for {@link ExampleMatcher}. @@ -176,6 +175,47 @@ public class ExampleMatcherUnitTests { assertThat(configuredExampleSpec.isIgnoreCaseEnabled(), is(true)); } + /** + * @see DATACMNS-900 + */ + @Test + public void shouldCompareUsingHashCodeAndEquals() throws Exception { + + matcher = matching() // + .withIgnorePaths("foo", "bar", "baz") // + .withNullHandler(NullHandler.IGNORE) // + .withIgnoreCase("ignored-case") // + .withMatcher("hello", GenericPropertyMatchers.contains().caseSensitive()) // + .withMatcher("world", new MatcherConfigurer() { + @Override + public void configureMatcher(GenericPropertyMatcher matcher) { + matcher.endsWith(); + } + }); + + ExampleMatcher sameAsMatcher = matching() // + .withIgnorePaths("foo", "bar", "baz") // + .withNullHandler(NullHandler.IGNORE) // + .withIgnoreCase("ignored-case") // + .withMatcher("hello", GenericPropertyMatchers.contains().caseSensitive()) // + .withMatcher("world", new MatcherConfigurer() { + @Override + public void configureMatcher(GenericPropertyMatcher matcher) { + matcher.endsWith(); + } + }); + + ExampleMatcher different = matching() // + .withIgnorePaths("foo", "bar", "baz") // + .withNullHandler(NullHandler.IGNORE) // + .withMatcher("hello", GenericPropertyMatchers.contains().ignoreCase()); + + assertThat(matcher.hashCode(), is(sameAsMatcher.hashCode())); + assertThat(matcher.hashCode(), is(not(different.hashCode()))); + assertThat(matcher, is(equalTo(sameAsMatcher))); + assertThat(matcher, is(not(equalTo(different)))); + } + static class Person { String firstname; diff --git a/src/test/java/org/springframework/data/domain/ExampleUnitTests.java b/src/test/java/org/springframework/data/domain/ExampleUnitTests.java index acee1adfb..94c0ad58c 100644 --- a/src/test/java/org/springframework/data/domain/ExampleUnitTests.java +++ b/src/test/java/org/springframework/data/domain/ExampleUnitTests.java @@ -17,9 +17,11 @@ package org.springframework.data.domain; import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.*; +import static org.springframework.data.domain.ExampleMatcher.*; import org.junit.Before; import org.junit.Test; +import org.springframework.data.domain.ExampleMatcher.*; /** * Test for {@link Example}. @@ -54,10 +56,28 @@ public class ExampleUnitTests { * @see DATACMNS-810 */ @Test - public void retunsSampleObjectsClassAsProbeType() { + public void returnsSampleObjectsClassAsProbeType() { assertThat(example.getProbeType(), is(equalTo(Person.class))); } + /** + * @see DATACMNS-900 + */ + @Test + public void shouldCompareUsingHashCodeAndEquals() throws Exception { + + Example example = Example.of(person, matching().withIgnoreCase("firstname")); + Example sameAsExample = Example.of(person, matching().withIgnoreCase("firstname")); + + Example different = Example.of(person, + matching().withMatcher("firstname", GenericPropertyMatchers.contains())); + + assertThat(example.hashCode(), is(sameAsExample.hashCode())); + assertThat(example.hashCode(), is(not(different.hashCode()))); + assertThat(example, is(equalTo(sameAsExample))); + assertThat(example, is(not(equalTo(different)))); + } + static class Person { String firstname; }