Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make recursive comparison able to ignore specific fields
- Loading branch information
1 parent
3691b28
commit bee3357
Showing
10 changed files
with
349 additions
and
32 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/main/java/org/assertj/core/api/recursive/comparison/FieldLocation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package org.assertj.core.api.recursive.comparison; | ||
|
||
import java.util.Comparator; | ||
import java.util.Objects; | ||
|
||
// TODO should understand Map keys as field | ||
public class FieldLocation implements Comparable<FieldLocation> { | ||
|
||
// TODO test | ||
private static final Comparator<FieldLocation> COMPARATOR = Comparator.comparing(FieldLocation::getFieldPath); | ||
|
||
private String fieldPath; | ||
// private boolean whereverInGraph = false; | ||
// private boolean matches(Field field, Field parent); ? | ||
|
||
@Override | ||
public int compareTo(final FieldLocation other) { | ||
return COMPARATOR.compare(this, other); | ||
} | ||
|
||
@Override | ||
public boolean equals(final Object other) { | ||
if (!(other instanceof FieldLocation)) return false; | ||
FieldLocation castOther = (FieldLocation) other; | ||
return Objects.equals(fieldPath, castOther.fieldPath); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(fieldPath); | ||
} | ||
|
||
public FieldLocation(String fieldPath) { | ||
Objects.requireNonNull(fieldPath, "a field path can't be null"); | ||
this.fieldPath = fieldPath; | ||
} | ||
|
||
public String getFieldPath() { | ||
return fieldPath; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("FieldLocation [fieldPath=%s]", fieldPath); | ||
} | ||
|
||
public boolean matches(String concatenatedPath) { | ||
return fieldPath.equals(concatenatedPath); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/test/java/org/assertj/core/api/recursive/comparison/FieldLocation_matches_Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.assertj.core.api.recursive.comparison; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.CsvSource; | ||
|
||
public class FieldLocation_matches_Test { | ||
|
||
@ParameterizedTest(name = "{0} matches {1}") | ||
@CsvSource(value = { | ||
"name, name", | ||
"foo.bar, foo.bar", | ||
}) | ||
public void should_match_fields(String location, String matchingFieldPath) { | ||
// GIVEN | ||
FieldLocation fieldLocation = new FieldLocation(location); | ||
// WHEN | ||
boolean match = fieldLocation.matches(matchingFieldPath); | ||
// THEN | ||
assertThat(match).as("%s matches %s", fieldLocation, matchingFieldPath).isTrue(); | ||
} | ||
|
||
} |
93 changes: 93 additions & 0 deletions
93
...tj/core/api/recursive/comparison/RecursiveComparisonConfiguration_ignoresFields_Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package org.assertj.core.api.recursive.comparison; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.util.Lists.list; | ||
|
||
import java.util.Date; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Stream; | ||
|
||
import org.apache.commons.lang3.RandomStringUtils; | ||
import org.apache.commons.lang3.RandomUtils; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
public class RecursiveComparisonConfiguration_ignoresFields_Test { | ||
|
||
private RecursiveComparisonConfiguration recursiveComparisonConfiguration; | ||
|
||
@BeforeEach | ||
public void setup() { | ||
recursiveComparisonConfiguration = new RecursiveComparisonConfiguration(); | ||
} | ||
|
||
@Test | ||
public void should_register_fields_path_to_ignore_without_duplicates() { | ||
// GIVEN | ||
recursiveComparisonConfiguration.ignoreFields("foo", "bar", "foo.bar", "bar"); | ||
// WHEN | ||
Set<FieldLocation> fields = recursiveComparisonConfiguration.getIgnoredFields(); | ||
// THEN | ||
assertThat(fields).containsExactlyInAnyOrder(new FieldLocation("foo"), | ||
new FieldLocation("bar"), | ||
new FieldLocation("foo.bar")); | ||
} | ||
|
||
@ParameterizedTest(name = "{0} should be ignored") | ||
@MethodSource("ignoringNullFieldsSource") | ||
public void should_ignore_actual_null_fields(DualKey dualKey) { | ||
// GIVEN | ||
recursiveComparisonConfiguration.setIgnoreAllActualNullFields(true); | ||
// WHEN | ||
boolean ignored = recursiveComparisonConfiguration.shouldIgnore(dualKey); | ||
// THEN | ||
assertThat(ignored).as("%s should be ignored", dualKey).isTrue(); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
private static Stream<Arguments> ignoringNullFieldsSource() { | ||
return Stream.of(Arguments.of(dualKey(null, "John")), | ||
Arguments.of(dualKey(null, 123)), | ||
Arguments.of(dualKey(null, (Object) null)), | ||
Arguments.of(dualKey(null, new Date()))); | ||
|
||
} | ||
|
||
@ParameterizedTest(name = "{0} should be ignored with these ignored fields {1}") | ||
@MethodSource("ignoringSpecifiedFieldsSource") | ||
public void should_ignore_specified_fields(DualKey dualKey, List<String> ignoredFields) { | ||
// GIVEN | ||
recursiveComparisonConfiguration.ignoreFields(ignoredFields.toArray(new String[0])); | ||
// WHEN | ||
boolean ignored = recursiveComparisonConfiguration.shouldIgnore(dualKey); | ||
// THEN | ||
assertThat(ignored).as("%s should be ignored with these ignored fields %s", dualKey, ignoredFields).isTrue(); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
private static Stream<Arguments> ignoringSpecifiedFieldsSource() { | ||
return Stream.of(Arguments.of(randomDualKeyWithPath("name"), list("name")), | ||
Arguments.of(randomDualKeyWithPath("name"), list("foo", "name", "foo")), | ||
Arguments.of(randomDualKeyWithPath("name", "first"), list("name.first")), | ||
Arguments.of(randomDualKeyWithPath("father", "name", "first"), | ||
list("father", "name.first", "father.name.first"))); | ||
|
||
} | ||
|
||
private static DualKey randomDualKeyWithPath(String... pathElements) { | ||
return new DualKey(list(pathElements), new Object(), new Object()); | ||
} | ||
|
||
private static DualKey dualKey(Object key1, Object key2) { | ||
return new DualKey(randomPath(), key1, key2); | ||
} | ||
|
||
private static List<String> randomPath() { | ||
return list(RandomStringUtils.random(RandomUtils.nextInt(0, 10))); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.