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.
This commit is contained in:
Mark Paluch
2016-08-29 15:56:01 +02:00
committed by Oliver Gierke
parent b7f4709b7c
commit ac51b1671d
3 changed files with 68 additions and 3 deletions

View File

@@ -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}.
@@ -207,6 +206,47 @@ public class ExampleMatcherUnitTests {
assertThat(matchingAny().isAllMatching(), is(false));
}
/**
* @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<GenericPropertyMatcher>() {
@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<GenericPropertyMatcher>() {
@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;

View File

@@ -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<Person> example = Example.of(person, matching().withIgnoreCase("firstname"));
Example<Person> sameAsExample = Example.of(person, matching().withIgnoreCase("firstname"));
Example<Person> 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;
}