Skip to content

DATACMNS-900 - Add equals/hashCode methods for ExampleMatcher utility classes. #175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>1.13.0.BUILD-SNAPSHOT</version>
<version>1.13.0.DATACMNS-900-SNAPSHOT</version>

<name>Spring Data Core</name>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ public static interface MatcherConfigurer<T> {
*
* @author Mark Paluch
*/
@EqualsAndHashCode
public static class GenericPropertyMatcher {

StringMatcher stringMatcher = null;
Expand Down Expand Up @@ -686,10 +687,12 @@ public Object convert(Object source) {
* 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;
Expand Down Expand Up @@ -798,8 +801,10 @@ public Object transformValue(Object source) {
* Define specific property handling for Dot-Paths.
*
* @author Christoph Strobl
* @author Mark Paluch
* @since 1.12
*/
@EqualsAndHashCode
public static class PropertySpecifiers {

private final Map<String, PropertySpecifier> propertySpecifiers = new LinkedHashMap<String, PropertySpecifier>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@

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}.
Expand Down Expand Up @@ -207,6 +206,47 @@ public void anyMatcherYieldsAnyMatching() {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@

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}.
Expand Down Expand Up @@ -54,10 +56,28 @@ public void rejectsNullProbe() {
* @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;
}
Expand Down