Skip to content

Commit

Permalink
Set ignoringAllOverriddenEquals to skip any java types
Browse files Browse the repository at this point in the history
  • Loading branch information
joel-costigliola committed Feb 13, 2019
1 parent aba27a2 commit 2c9fc54
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 50 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/assertj/core/api/AbstractObjectAssert.java
Expand Up @@ -816,8 +816,8 @@ public <T> SELF returns(T expected, Function<ACTUAL, T> from) {
* <li>overridden equals methods were used in the comparison </li>
* <li>these types were compared with the following comparators: </li>
* <ul>
* <li>java.lang.Double -> DoubleComparator[precision=1.0E-15] </li>
* <li>java.lang.Float -> FloatComparator[precision=1.0E-6] </li>
* <li>java.lang.Double -&gt; DoubleComparator[precision=1.0E-15] </li>
* <li>java.lang.Float -&gt; FloatComparator[precision=1.0E-6] </li>
* </ul>
* </ul>
*
Expand Down
Expand Up @@ -61,7 +61,7 @@ public String getConcatenatedPath() {
return concatenatedPath;
}

public boolean isBasicType() {
return key1.getClass().getName().startsWith("java.lang");
public boolean isJavaType() {
return key1.getClass().getName().startsWith("java.");
}
}
Expand Up @@ -317,14 +317,11 @@ public RecursiveComparisonAssert ignoringFieldsMatchingRegexes(String... regexes

/**
* By default the recursive comparison uses overridden {@code equals} methods to compare fields,
* this method allows to force a recursive comparison for all fields at the exception of basic types (i.e. java.lang types)
* - at some point we need to compare something!
* this method allows to compare recursively all fields <b>except fields with java types</b> (at some point we need to compare something!).
* <p>
* For the recursive comparison to use the overridden {@code equals} of a given type anyway (like {@link Date}) you can register
* a type comparator using {@link #withComparatorForType(Comparator, Class)}.
* <p>
* TODO introduce {@code ignoringAllOverriddenEqualsExceptFor(Class<?>... classes)} ?
* <p>
* Example:
* <pre><code class='java'> public class Person {
* String name;
Expand Down
Expand Up @@ -135,6 +135,15 @@ public Set<FieldLocation> getIgnoredFields() {
return ignoredFields;
}

/**
* Force a recursive comparison on all fields (except java types).
* <p>
* See {@link RecursiveComparisonAssert#ignoringAllOverriddenEquals()} for examples.
*/
public void ignoreAllOverriddenEquals() {
ignoreAllOverriddenEquals = true;
}

/**
* Adds the given fields to the list of fields to force a recursive comparison on.
* <p>
Expand Down Expand Up @@ -195,7 +204,7 @@ public <T> void registerComparatorForType(Comparator<? super T> comparator, Clas
* Comparators specified by this method have precedence over comparators added with {@link #registerComparatorForType(Comparator, Class)}.
* <p>
* See {@link RecursiveComparisonAssert#withComparatorForFields(Comparator, String...) RecursiveComparisonAssert#withComparatorForFields(Comparator, String...)} for examples.
*
*
* @param comparator the {@link java.util.Comparator Comparator} to use to compare the given field
* @param fieldLocation the location from the root object of the field the comparator should be used for
*/
Expand Down Expand Up @@ -279,7 +288,7 @@ private static String concatenatedPath(String parentPath, String name) {
}

boolean shouldIgnoreOverriddenEqualsOf(DualKey dualKey) {
if (dualKey.isBasicType()) return false; // we must compare basic types otherwise the recursive comparison loops infinitely!
if (dualKey.isJavaType()) return false; // we must compare basic types otherwise the recursive comparison loops infinitely!
return ignoreAllOverriddenEquals
|| matchesAnIgnoredOverriddenEqualsField(dualKey)
|| shouldIgnoreOverriddenEqualsOf(dualKey.key1.getClass());
Expand All @@ -306,9 +315,13 @@ private void describeIgnoreAllActualNullFields(StringBuilder description) {
}

private void describeOverriddenEqualsMethodsUsage(StringBuilder description, Representation representation) {
description.append(format("- overridden equals methods were used in the comparison"));
if (isConfiguredToIgnoreSomeOverriddenEqualsMethods()) {
description.append(format(", except for:%n"));
boolean isConfiguredToIgnoreSomeOverriddenEqualsMethods = isConfiguredToIgnoreSomeOverriddenEqualsMethods();
String header = ignoreAllOverriddenEquals
? "- no overridden equals methods were used in the comparison except for java types"
: "- overridden equals methods were used in the comparison";
description.append(header);
if (isConfiguredToIgnoreSomeOverriddenEqualsMethods) {
description.append(format(ignoreAllOverriddenEquals ? " and:%n" : ", except for:%n"));
describeIgnoredOverriddenEqualsMethods(description, representation);
} else {
description.append(format("%n"));
Expand Down Expand Up @@ -443,8 +456,4 @@ private void describeTypeCheckingStrictness(StringBuilder description) {
description.append(format(str));
}

public void ignoreAllOverriddenEquals() {
ignoreAllOverriddenEquals = true;
}

}
Expand Up @@ -54,5 +54,4 @@ public void should_build_multiline_description_containing_percent() {
"- expected value : \"%%bar%%%%\"%n" +
"%%additional %%information%%"));
}

}
Expand Up @@ -18,7 +18,6 @@
import static org.assertj.core.presentation.StandardRepresentation.STANDARD_REPRESENTATION;
import static org.assertj.core.test.AlwaysDifferentComparator.alwaysDifferent;
import static org.assertj.core.test.AlwaysEqualComparator.ALWAY_EQUALS_TUPLE;
import static org.assertj.core.util.Lists.list;

import java.util.Comparator;

Expand All @@ -41,61 +40,93 @@ public void setup() {

@Test
public void should_show_that_null_fields_are_ignored() {
// WHEN
// GIVEN
recursiveComparisonConfiguration.setIgnoreAllActualNullFields(true);
// WHEN
String multiLineDescription = recursiveComparisonConfiguration.multiLineDescription(STANDARD_REPRESENTATION);
// THEN
assertThat(multiLineDescription).contains(format("- all actual null fields were ignored in the comparison%n"));
}

@Test
public void should_show_that_some_given_fields_are_ignored() {
// WHEN
// GIVEN
recursiveComparisonConfiguration.ignoreFields("foo", "bar", "foo.bar");
// WHEN
String multiLineDescription = recursiveComparisonConfiguration.multiLineDescription(STANDARD_REPRESENTATION);
// THEN
assertThat(multiLineDescription).contains(format("- the following fields were ignored in the comparison: foo, bar, foo.bar%n"));
}

@Test
public void should_show_the_regexes_used_to_ignore_fields() {
// WHEN
// GIVEN
recursiveComparisonConfiguration.ignoreFieldsMatchingRegexes("foo", "bar", "foo.bar");
// WHEN
String multiLineDescription = recursiveComparisonConfiguration.multiLineDescription(STANDARD_REPRESENTATION);
// THEN
assertThat(multiLineDescription).contains(format("- the fields matching the following regexes were ignored in the comparison: foo, bar, foo.bar%n"));
}

@Test
public void should_show_the_ignored_overridden_equals_methods_regexes() {
public void should_show_the_ignored_all_overridden_equals_methods_flag() {
// GIVEN
recursiveComparisonConfiguration.ignoreAllOverriddenEquals();
// WHEN
String multiLineDescription = recursiveComparisonConfiguration.multiLineDescription(STANDARD_REPRESENTATION);
// THEN
assertThat(multiLineDescription).contains("- no overridden equals methods were used in the comparison except for java types");
}

@Test
public void should_show_the_ignored_all_overridden_equals_methods_flag_and_additional_ones() {
// GIVEN
recursiveComparisonConfiguration.ignoreAllOverriddenEquals();
recursiveComparisonConfiguration.ignoreOverriddenEqualsForFields("foo", "bar", "foo.bar");
recursiveComparisonConfiguration.ignoreOverriddenEqualsForFieldsMatchingRegexes(".*oo", ".*ar");
recursiveComparisonConfiguration.ignoreOverriddenEqualsForTypes(String.class, Multimap.class);
// WHEN
String multiLineDescription = recursiveComparisonConfiguration.multiLineDescription(STANDARD_REPRESENTATION);
// THEN
// @format:off
assertThat(multiLineDescription).contains(format("- no overridden equals methods were used in the comparison except for java types and:%n" +
" - the following fields: foo, bar, foo.bar%n" +
" - the following types: java.lang.String, com.google.common.collect.Multimap%n" +
" - the types matching the following regexes: .*oo, .*ar%n"));
// @format:on
}

@Test
public void should_show_the_ignored_overridden_equals_methods_regexes() {
// GIVEN
recursiveComparisonConfiguration.ignoreOverriddenEqualsForFieldsMatchingRegexes("foo", "bar", "foo.bar");
// WHEN
String multiLineDescription = recursiveComparisonConfiguration.multiLineDescription(STANDARD_REPRESENTATION);
// THEN
// @format:off
assertThat(multiLineDescription).contains(format(
"- overridden equals methods were used in the comparison, except for:%n" +
" - the types matching the following regexes: foo, bar, foo.bar%n"));
assertThat(multiLineDescription).contains(format("- overridden equals methods were used in the comparison, except for:%n" +
" - the types matching the following regexes: foo, bar, foo.bar%n"));
// @format:on
}

@Test
public void should_show_the_ignored_overridden_equals_methods_types() {
// WHEN
// GIVEN
recursiveComparisonConfiguration.ignoreOverriddenEqualsForTypes(String.class, Multimap.class);
// WHEN
String multiLineDescription = recursiveComparisonConfiguration.multiLineDescription(STANDARD_REPRESENTATION);
// THEN
// @format:off
assertThat(multiLineDescription).contains(format(
"- overridden equals methods were used in the comparison, except for:%n" +
" - the following types: java.lang.String, com.google.common.collect.Multimap%n"));
assertThat(multiLineDescription).contains(format("- overridden equals methods were used in the comparison, except for:%n" +
" - the following types: java.lang.String, com.google.common.collect.Multimap%n"));
// @format:on
}

@Test
public void should_show_the_ignored_overridden_equals_methods_fields() {
// WHEN
// GIVEN
recursiveComparisonConfiguration.ignoreOverriddenEqualsForFields("foo", "baz", "foo.baz");
// WHEN
String multiLineDescription = recursiveComparisonConfiguration.multiLineDescription(STANDARD_REPRESENTATION);
// THEN
// @format:off
Expand All @@ -107,9 +138,10 @@ public void should_show_the_ignored_overridden_equals_methods_fields() {

@Test
public void should_show_the_registered_comparator_by_types_and_the_default_ones() {
// WHEN
// GIVEN
recursiveComparisonConfiguration.registerComparatorForType(new AbsValueComparator<>(), Integer.class);
recursiveComparisonConfiguration.registerComparatorForType(AlwaysEqualComparator.ALWAY_EQUALS_TUPLE, Tuple.class);
// WHEN
String multiLineDescription = recursiveComparisonConfiguration.multiLineDescription(STANDARD_REPRESENTATION);
// THEN
// @format:off
Expand All @@ -124,10 +156,11 @@ public void should_show_the_registered_comparator_by_types_and_the_default_ones(

@Test
public void should_show_the_registered_comparator_for_specific_fields_alphabetically() {
// WHEN
// GIVEN
recursiveComparisonConfiguration.registerComparatorForField(ALWAY_EQUALS_TUPLE, fielLocation("foo"));
recursiveComparisonConfiguration.registerComparatorForField(alwaysDifferent(), fielLocation("bar"));
recursiveComparisonConfiguration.registerComparatorForField(new PercentageComparator(), fielLocation("height"));
// WHEN
String multiLineDescription = recursiveComparisonConfiguration.multiLineDescription(STANDARD_REPRESENTATION);
// THEN
// @format:off
Expand All @@ -140,17 +173,19 @@ public void should_show_the_registered_comparator_for_specific_fields_alphabetic

@Test
public void should_show_when_strict_type_checking_is_used() {
// WHEN
// GIVEN
recursiveComparisonConfiguration.strictTypeChecking(true);
// WHEN
String multiLineDescription = recursiveComparisonConfiguration.multiLineDescription(STANDARD_REPRESENTATION);
// THEN
assertThat(multiLineDescription).contains(format("- actual and expected objects and their fields were considered different when of incompatible types (i.e. expected type does not extend actual's type) even if all their fields match, for example a Person instance will never match a PersonDto (call strictTypeChecking(false) to change that behavior).%n"));
}

@Test
public void should_show_when_lenient_type_checking_is_used() {
// WHEN
// GIVEN
recursiveComparisonConfiguration.strictTypeChecking(false);
// WHEN
String multiLineDescription = recursiveComparisonConfiguration.multiLineDescription(STANDARD_REPRESENTATION);
// THEN
assertThat(multiLineDescription).contains(format("- actual and expected objects and their fields were compared field by field recursively even if they were not of the same type, this allows for example to compare a Person to a PersonDto (call strictTypeChecking(true) to change that behavior).%n"));
Expand Down Expand Up @@ -194,18 +229,6 @@ public void should_show_a_complete_multiline_description() {
// @format:on
}

@Test
public void should_build_multiline_description_containing_percent() {
// GIVEN
ComparisonDifference com = new ComparisonDifference(list("a", "b"), "foo%", "%bar%%", "%additional %information%");

// THEN
assertThat(com.multiLineDescription()).isEqualTo(format("field/property 'a.b' differ:%n" +
"- actual value : \"foo%%\"%n" +
"- expected value : \"%%bar%%%%\"%n" +
"%%additional %%information%%"));
}

// just to test the description does not fail when given a comparator with various String.format reserved flags
private class PercentageComparator implements Comparator<Double> {

Expand Down
Expand Up @@ -16,6 +16,7 @@
import static org.assertj.core.util.Lists.list;
import static org.junit.jupiter.params.provider.Arguments.arguments;

import java.util.Date;
import java.util.List;
import java.util.stream.Stream;

Expand All @@ -39,7 +40,7 @@ public void setup() {
}

@Test
public void should_ignore_all_overridden_equals_for_non_basic_types() {
public void should_ignore_all_overridden_equals_for_non_java_types() {
// GIVEN
DualKey dualKey = new DualKey(list("foo"), new Person(), new Person());
recursiveComparisonConfiguration.ignoreAllOverriddenEquals();
Expand All @@ -52,7 +53,7 @@ public void should_ignore_all_overridden_equals_for_non_basic_types() {

@ParameterizedTest
@MethodSource("ignoringAllOverriddenEqualsExceptBasicTypes")
public void should_ignore_all_overridden_equals_except_basic_types(Object value) {
public void should_ignore_all_overridden_equals_except_java_types(Object value) {
// GIVEN
DualKey dualKey = new DualKey(list("foo"), value, value);
recursiveComparisonConfiguration.ignoreAllOverriddenEquals();
Expand All @@ -65,7 +66,7 @@ public void should_ignore_all_overridden_equals_except_basic_types(Object value)

@SuppressWarnings("unused")
private static Stream<Object> ignoringAllOverriddenEqualsExceptBasicTypes() {
return Stream.of("foo", 23, 2.0, 123L, true, Byte.MIN_VALUE, new Object());
return Stream.of("foo", 23, 2.0, 123L, true, Byte.MIN_VALUE, new Object(), new Date());
}

@ParameterizedTest(name = "{0} overridden equals should be ignored with these regexes {1}")
Expand Down

0 comments on commit 2c9fc54

Please sign in to comment.