Browse Source

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.
pull/347/head
Mark Paluch 10 years ago committed by Oliver Gierke
parent
commit
ff7f599349
  1. 5
      src/main/java/org/springframework/data/domain/ExampleMatcher.java
  2. 44
      src/test/java/org/springframework/data/domain/ExampleMatcherUnitTests.java
  3. 22
      src/test/java/org/springframework/data/domain/ExampleUnitTests.java

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

@ -337,6 +337,7 @@ public class ExampleMatcher { @@ -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 { @@ -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 { @@ -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<String, PropertySpecifier> propertySpecifiers = new LinkedHashMap<String, PropertySpecifier>();

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

@ -22,8 +22,7 @@ import static org.springframework.data.domain.ExampleMatcher.*; @@ -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 { @@ -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<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;

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

@ -17,9 +17,11 @@ package org.springframework.data.domain; @@ -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 { @@ -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;
}

Loading…
Cancel
Save