|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 3 | + * the License. You may obtain a copy of the License at |
| 4 | + * |
| 5 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + * |
| 7 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 8 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 9 | + * specific language governing permissions and limitations under the License. |
| 10 | + * |
| 11 | + * Copyright 2012-2018 the original author or authors. |
| 12 | + */ |
| 13 | +package org.assertj.core.internal.objects; |
| 14 | + |
| 15 | +import static org.assertj.core.util.AssertionsUtil.expectAssertionError; |
| 16 | + |
| 17 | +import java.util.Date; |
| 18 | + |
| 19 | +import org.assertj.core.api.recursive.comparison.ComparisonDifference; |
| 20 | +import org.assertj.core.internal.objects.data.Giant; |
| 21 | +import org.assertj.core.internal.objects.data.Person; |
| 22 | +import org.assertj.core.internal.objects.data.PersonDto; |
| 23 | +import org.assertj.core.internal.objects.data.PersonDtoWithPersonNeighbour; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +public class Objects_assertIsEqualToUsingRecursiveComparison_strictTypeCheck_Test |
| 27 | + extends Objects_assertIsEqualToUsingRecursiveComparison_BaseTest { |
| 28 | + |
| 29 | + @Test |
| 30 | + public void should_pass_by_default_when_objects_data_are_equals_whatever_their_types_are() { |
| 31 | + // GIVEN |
| 32 | + Person actual = new Person("John"); |
| 33 | + actual.home.address.number = 1; |
| 34 | + actual.dateOfBirth = new Date(123); |
| 35 | + actual.neighbour = new Person("Jack"); |
| 36 | + actual.neighbour.home.address.number = 123; |
| 37 | + actual.neighbour.neighbour = new Person("James"); |
| 38 | + actual.neighbour.neighbour.home.address.number = 124; |
| 39 | + |
| 40 | + PersonDto expected = new PersonDto("John"); |
| 41 | + expected.home.address.number = 1; |
| 42 | + expected.dateOfBirth = new Date(123); |
| 43 | + expected.neighbour = new PersonDto("Jack"); |
| 44 | + expected.neighbour.home.address.number = 123; |
| 45 | + expected.neighbour.neighbour = new PersonDto("James"); |
| 46 | + expected.neighbour.neighbour.home.address.number = 124; |
| 47 | + |
| 48 | + // THEN |
| 49 | + areEqualsByRecursiveComparison(actual, expected, recursiveComparisonConfiguration); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void should_pass_in_strict_type_check_mode_when_objects_data_are_equals_and_expected_type_is_compatible_with_actual_type() { |
| 54 | + // GIVEN |
| 55 | + Person actual = new Person("John"); |
| 56 | + actual.home.address.number = 1; |
| 57 | + actual.dateOfBirth = new Date(123); |
| 58 | + actual.neighbour = new Person("Jack"); |
| 59 | + actual.neighbour.home.address.number = 123; |
| 60 | + actual.neighbour.neighbour = new Person("James"); |
| 61 | + actual.neighbour.neighbour.home.address.number = 124; |
| 62 | + |
| 63 | + Giant expected = new Giant(); |
| 64 | + expected.name = "John"; |
| 65 | + expected.home.address.number = 1; |
| 66 | + expected.dateOfBirth = new Date(123); |
| 67 | + expected.neighbour = new Giant(); |
| 68 | + expected.neighbour.name = "Jack"; |
| 69 | + expected.neighbour.home.address.number = 123; |
| 70 | + expected.neighbour.neighbour = new Person("James"); |
| 71 | + expected.neighbour.neighbour.home.address.number = 124; |
| 72 | + |
| 73 | + Person expected2 = new Person("John"); |
| 74 | + expected2.home.address.number = 1; |
| 75 | + expected2.dateOfBirth = new Date(123); |
| 76 | + expected2.neighbour = new Person("Jack"); |
| 77 | + expected2.neighbour.home.address.number = 123; |
| 78 | + expected2.neighbour.neighbour = new Person("James"); |
| 79 | + expected2.neighbour.neighbour.home.address.number = 124; |
| 80 | + |
| 81 | + // WHEN |
| 82 | + recursiveComparisonConfiguration.strictTypeChecking(true); |
| 83 | + |
| 84 | + // THEN |
| 85 | + areEqualsByRecursiveComparison(actual, expected, recursiveComparisonConfiguration); |
| 86 | + areEqualsByRecursiveComparison(actual, expected2, recursiveComparisonConfiguration); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void should_fail_in_strict_type_checking_mode_when_actual_and_expected_have_the_same_data_but_incompatible_types() { |
| 91 | + // GIVEN |
| 92 | + Person actual = new Person("John"); |
| 93 | + actual.home.address.number = 1; |
| 94 | + actual.dateOfBirth = new Date(123); |
| 95 | + actual.neighbour = new Person("Jack"); |
| 96 | + actual.neighbour.home.address.number = 123; |
| 97 | + |
| 98 | + PersonDtoWithPersonNeighbour expected = new PersonDtoWithPersonNeighbour("John"); |
| 99 | + expected.home.address.number = 1; |
| 100 | + expected.dateOfBirth = new Date(123); |
| 101 | + expected.neighbour = new Person("Jack"); |
| 102 | + expected.neighbour.home.address.number = 123; |
| 103 | + |
| 104 | + recursiveComparisonConfiguration.strictTypeChecking(true); |
| 105 | + |
| 106 | + // WHEN |
| 107 | + expectAssertionError(() -> areEqualsByRecursiveComparison(actual, expected, recursiveComparisonConfiguration)); |
| 108 | + |
| 109 | + // THEN |
| 110 | + // as neighbour comparison fails, the comparison skip any neighbour fields |
| 111 | + ComparisonDifference difference = diff("", actual, expected, |
| 112 | + "actual and expected are considered different since the comparison enforces strict type check and expected type org.assertj.core.internal.objects.data.PersonDtoWithPersonNeighbour is not a subtype of actual type org.assertj.core.internal.objects.data.Person"); |
| 113 | + verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(actual, expected, difference); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void should_fail_in_strict_type_checking_mode_when_actual_and_expected_fields_have_the_same_data_but_incompatible_types() { |
| 118 | + // GIVEN |
| 119 | + Something withA = new Something(new A(10)); |
| 120 | + Something withB = new Something(new B(10)); |
| 121 | + |
| 122 | + recursiveComparisonConfiguration.strictTypeChecking(true); |
| 123 | + |
| 124 | + // WHEN |
| 125 | + expectAssertionError(() -> areEqualsByRecursiveComparison(withA, withB, recursiveComparisonConfiguration)); |
| 126 | + |
| 127 | + // THEN |
| 128 | + // inner comparison fails as the fields have different types |
| 129 | + ComparisonDifference valueDifference = diff("inner", withA.inner, withB.inner, |
| 130 | + "the fields are considered different since the comparison enforces strict type check and org.assertj.core.internal.objects.Objects_assertIsEqualToUsingRecursiveComparison_strictTypeCheck_Test$B is not a subtype of org.assertj.core.internal.objects.Objects_assertIsEqualToUsingRecursiveComparison_strictTypeCheck_Test$A"); |
| 131 | + verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(withA, withB, valueDifference); |
| 132 | + } |
| 133 | + |
| 134 | + private static class Something { |
| 135 | + Inner inner; // can be A or B |
| 136 | + |
| 137 | + public Something(Inner inner) { |
| 138 | + this.inner = inner; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + private static class Inner { |
| 143 | + @SuppressWarnings("unused") |
| 144 | + int value; |
| 145 | + } |
| 146 | + |
| 147 | + private static class A extends Inner { |
| 148 | + |
| 149 | + public A(int value) { |
| 150 | + this.value = value; |
| 151 | + } |
| 152 | + |
| 153 | + } |
| 154 | + |
| 155 | + private static class B extends Inner { |
| 156 | + public B(int value) { |
| 157 | + this.value = value; |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | +} |
0 commit comments