How to use hasCustomComparator method of org.assertj.core.internal.DeepDifference class

Best Assertj code snippet using org.assertj.core.internal.DeepDifference.hasCustomComparator

Source:RecursiveComparisonDifferenceCalculator.java Github

copy

Full Screen

...131 .findFirst()132 .ifPresent(dualValuesToCompare::remove));133 }134 private boolean mustCompareFieldsRecursively(boolean isRootObject, DualValue dualValue) {135 boolean noCustomComparisonForDualValue = !recursiveComparisonConfiguration.hasCustomComparator(dualValue)136 && !shouldHonorOverriddenEquals(dualValue, recursiveComparisonConfiguration);137 return isRootObject || noCustomComparisonForDualValue;138 }139 }140 /**141 * Compare two objects for differences by doing a 'deep' comparison. This will traverse the142 * Object graph and perform either a field-by-field comparison on each143 * object (if not .equals() method has been overridden from Object), or it144 * will call the customized .equals() method if it exists.145 * <p>146 *147 * This method handles cycles correctly, for example A-&gt;B-&gt;C-&gt;A.148 * Suppose a and a' are two separate instances of the A with the same values149 * for all fields on A, B, and C. Then a.deepEquals(a') will return an empty list. It150 * uses cycle detection storing visited objects in a Set to prevent endless151 * loops.152 *153 * @param actual Object one to compare154 * @param expected Object two to compare155 * @param recursiveComparisonConfiguration the recursive comparison configuration156 * @return the list of differences found or an empty list if objects are equivalent.157 * Equivalent means that all field values of both subgraphs are the same,158 * either at the field level or via the respectively encountered overridden159 * .equals() methods during traversal.160 */161 public List<ComparisonDifference> determineDifferences(Object actual, Object expected,162 RecursiveComparisonConfiguration recursiveComparisonConfiguration) {163 if (recursiveComparisonConfiguration.isInStrictTypeCheckingMode() && expectedTypeIsNotSubtypeOfActualType(actual, expected)) {164 return list(expectedAndActualTypeDifference(actual, expected));165 }166 List<String> rootPath = list();167 List<DualValue> visited = list();168 return determineDifferences(actual, expected, rootPath, true, visited, recursiveComparisonConfiguration);169 }170 // TODO keep track of ignored fields in an RecursiveComparisonExecution class ?171 private static List<ComparisonDifference> determineDifferences(Object actual, Object expected, List<String> parentPath,172 boolean isRootObject, List<DualValue> visited,173 RecursiveComparisonConfiguration recursiveComparisonConfiguration) {174 ComparisonState comparisonState = new ComparisonState(visited, recursiveComparisonConfiguration);175 comparisonState.initDualValuesToCompare(actual, expected, parentPath, isRootObject);176 while (comparisonState.hasDualValuesToCompare()) {177 final DualValue dualValue = comparisonState.pickDualValueToCompare();178 final List<String> currentPath = dualValue.getPath();179 final Object actualFieldValue = dualValue.actual;180 final Object expectedFieldValue = dualValue.expected;181 if (actualFieldValue == expectedFieldValue) continue;182 // Custom comparators take precedence over all other types of comparison183 if (recursiveComparisonConfiguration.hasCustomComparator(dualValue)) {184 if (!propertyOrFieldValuesAreEqual(dualValue, recursiveComparisonConfiguration)) comparisonState.addDifference(dualValue);185 // since we used a custom comparator we don't need to inspect the nested fields any further186 continue;187 }188 if (actualFieldValue == null || expectedFieldValue == null) {189 // one of the value is null while the other is not as we already know that actualFieldValue != expectedFieldValue190 comparisonState.addDifference(dualValue);191 continue;192 }193 if (dualValue.isExpectedAnEnum()) {194 compareAsEnums(dualValue, comparisonState, recursiveComparisonConfiguration);195 continue;196 }197 // TODO move hasFieldTypesDifference check into each compareXXX...

Full Screen

Full Screen

Source:DeepDifference.java Github

copy

Full Screen

...148 final Object key2 = dualKey.key2;149 if (key1 == key2) {150 continue;151 }152 if (hasCustomComparator(dualKey, comparatorByPropertyOrField, comparatorByType)) {153 if (propertyOrFieldValuesAreEqual(key1, key2, dualKey.getConcatenatedPath(),154 comparatorByPropertyOrField, comparatorByType))155 continue;156 }157 if (key1 == null || key2 == null) {158 differences.add(new Difference(currentPath, key1, key2));159 continue;160 }161 if (key1 instanceof Collection) {162 if (!(key2 instanceof Collection)) {163 differences.add(new Difference(currentPath, key1, key2));164 continue;165 }166 } else if (key2 instanceof Collection) {167 differences.add(new Difference(currentPath, key1, key2));168 continue;169 }170 if (key1 instanceof SortedSet) {171 if (!(key2 instanceof SortedSet)) {172 differences.add(new Difference(currentPath, key1, key2));173 continue;174 }175 } else if (key2 instanceof SortedSet) {176 differences.add(new Difference(currentPath, key1, key2));177 continue;178 }179 if (key1 instanceof SortedMap) {180 if (!(key2 instanceof SortedMap)) {181 differences.add(new Difference(currentPath, key1, key2));182 continue;183 }184 } else if (key2 instanceof SortedMap) {185 differences.add(new Difference(currentPath, key1, key2));186 continue;187 }188 if (key1 instanceof Map) {189 if (!(key2 instanceof Map)) {190 differences.add(new Difference(currentPath, key1, key2));191 continue;192 }193 } else if (key2 instanceof Map) {194 differences.add(new Difference(currentPath, key1, key2));195 continue;196 }197 // Handle all [] types. In order to be equal, the arrays must be the198 // same length, be of the same type, be in the same order, and all199 // elements within the array must be deeply equivalent.200 if (key1.getClass().isArray()) {201 if (!compareArrays(key1, key2, currentPath, toCompare, visited)) {202 differences.add(new Difference(currentPath, key1, key2));203 continue;204 }205 continue;206 }207 // Special handle SortedSets because they are fast to compare208 // because their elements must be in the same order to be equivalent Sets.209 if (key1 instanceof SortedSet) {210 if (!compareOrderedCollection((Collection<?>) key1, (Collection<?>) key2, currentPath, toCompare, visited)) {211 differences.add(new Difference(currentPath, key1, key2));212 continue;213 }214 continue;215 }216 // Check List, as element order matters this comparison is faster than using unordered comparison.217 if (key1 instanceof List) {218 if (!compareOrderedCollection((Collection<?>) key1, (Collection<?>) key2, currentPath, toCompare, visited)) {219 differences.add(new Difference(currentPath, key1, key2));220 continue;221 }222 continue;223 }224 // Handle unordered Collection.225 if (key1 instanceof Collection) {226 if (!compareUnorderedCollection((Collection<?>) key1, (Collection<?>) key2, currentPath, toCompare,227 visited, comparatorByPropertyOrField, comparatorByType)) {228 differences.add(new Difference(currentPath, key1, key2));229 continue;230 }231 continue;232 }233 // Compare two SortedMaps. This takes advantage of the fact that these234 // Maps can be compared in O(N) time due to their ordering.235 if (key1 instanceof SortedMap) {236 if (!compareSortedMap((SortedMap<?, ?>) key1, (SortedMap<?, ?>) key2, currentPath, toCompare, visited)) {237 differences.add(new Difference(currentPath, key1, key2));238 continue;239 }240 continue;241 }242 // Compare two Unordered Maps. This is a slightly more expensive comparison because243 // order cannot be assumed, therefore a temporary Map must be created, however the244 // comparison still runs in O(N) time.245 if (key1 instanceof Map) {246 if (!compareUnorderedMap((Map<?, ?>) key1, (Map<?, ?>) key2, currentPath, toCompare, visited)) {247 differences.add(new Difference(currentPath, key1, key2));248 continue;249 }250 continue;251 }252 if (hasCustomEquals(key1.getClass())) {253 if (!key1.equals(key2)) {254 differences.add(new Difference(currentPath, key1, key2));255 continue;256 }257 continue;258 }259 Set<String> key1FieldsNames = getFieldsNames(getDeclaredFieldsIncludingInherited(key1.getClass()));260 Set<String> key2FieldsNames = getFieldsNames(getDeclaredFieldsIncludingInherited(key2.getClass()));261 if (!key2FieldsNames.containsAll(key1FieldsNames)) {262 differences.add(new Difference(currentPath, key1, key2));263 } else {264 for (String fieldName : key1FieldsNames) {265 List<String> path = new ArrayList<>(currentPath);266 path.add(fieldName);267 DualKey dk = new DualKey(path,268 COMPARISON.getSimpleValue(fieldName, key1),269 COMPARISON.getSimpleValue(fieldName, key2));270 if (!visited.contains(dk)) {271 toCompare.addFirst(dk);272 }273 }274 }275 }276 return differences;277 }278 private static boolean hasCustomComparator(DualKey dualKey, Map<String, Comparator<?>> comparatorByPropertyOrField,279 TypeComparators comparatorByType) {280 String fieldName = dualKey.getConcatenatedPath();281 if (comparatorByPropertyOrField.containsKey(fieldName)) return true;282 // we know that dualKey.key1 != dualKey.key2 at this point, so one the key is not null283 Class<?> keyType = dualKey.key1 != null ? dualKey.key1.getClass() : dualKey.key2.getClass();284 return comparatorByType.get(keyType) != null;285 }286 private static Deque<DualKey> initStack(Object a, Object b, List<String> parentPath,287 Map<String, Comparator<?>> comparatorByPropertyOrField,288 TypeComparators comparatorByType) {289 Deque<DualKey> stack = new LinkedList<>();290 boolean isRootObject = parentPath == null;291 List<String> currentPath = isRootObject ? new ArrayList<String>() : parentPath;292 DualKey basicDualKey = new DualKey(currentPath, a, b);293 if (a != null && b != null && !isContainerType(a) && !isContainerType(b)294 && (isRootObject || !hasCustomComparator(basicDualKey, comparatorByPropertyOrField, comparatorByType))) {295 // disregard the equals method and start comparing fields296 Set<String> aFieldsNames = getFieldsNames(getDeclaredFieldsIncludingInherited(a.getClass()));297 if (!aFieldsNames.isEmpty()) {298 Set<String> bFieldsNames = getFieldsNames(getDeclaredFieldsIncludingInherited(b.getClass()));299 if (!bFieldsNames.containsAll(aFieldsNames)) {300 stack.addFirst(basicDualKey);301 } else {302 for (String fieldName : aFieldsNames) {303 List<String> fieldPath = new ArrayList<>(currentPath);304 fieldPath.add(fieldName);305 DualKey dk = new DualKey(fieldPath,306 COMPARISON.getSimpleValue(fieldName, a),307 COMPARISON.getSimpleValue(fieldName, b));308 stack.addFirst(dk);...

Full Screen

Full Screen

hasCustomComparator

Using AI Code Generation

copy

Full Screen

1imort org.assertj.core.internal.DeepDifference;2pDeepDifferenceExample {3 public static void main(String[] args) {4 DeepDifference deepDifference = new DeepDifference();5 boolean result = deepDifference.hasCustomComparator(String.class);6 System.out.println("Has Custom Comparator for String class: " + result);7 }8}9Difference between equals() and == operator in Java

Full Screen

Full Screen

hasCustomComparator

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.internal.DeepDifference;2public class DeepDifferenceExample {3 public static void main(String[] args) {4 DeepDifference deepDifference = new DeepDifference();5 boolean result = deepDifference.hasCustomComparator(String.class);6 System.out.println("Has Custom Comparator for String class: " + result);7 }8}9Difference between equals() and == operator in Java

Full Screen

Full Screen

hasCustomComparator

Using AI Code Generation

copy

Full Screen

1public class Main {2 public static void main(String[] args) {3 DeepDifference deepDifference = new DeepDifference();4 System.out.println(deepDifference.hasCustomComparator(new Object(), new Object()));5 }6}7public class Main {8 public static void main(String[] args) {9 DeepDifference deepDifference = new DeepDifference();10 System.out.println(deepDifference.hasCustomComparator(new Object(), new Object()));11 }12}13public class Main {14 public static void main(String[] args) {15 DeepDifference deepDifference = new DeepDifference();16 System.out.println(deepDifference.hasCustomComparator(new Object(), new Object()));17 }18}19public class Main {20 public static void main(String[] args) {21 DeepDifference deepDifference = new DeepDifference();22 System.out.println(deepDifference.hasCustomComparator(new Object(), new Object()));23 }24}25public class Main {26 public static void main(String[] args) {27 DeepDifference deepDifference = new DeepDifference();28 System.out.println(deepDifference.hasCustomComparator(new Object(), new Object()));29 }30}31public class Main {32 public static void main(String[] args) {33 DeepDifference deepDifference = new DeepDifference();34 System.out.println(deepDifference.hasCustomComparator(new Object(), new Object()));35 }36}37public class Main {38 public static void main(String[] args) {39 DeepDifference deepDifference = new DeepDifference();40 System.out.println(deepDifference.hasCustomComparator(new Object(), new Object()));41 }42}43public class Main {44 public static void main(String[] args) {45 DeepDifference deepDifference = new DeepDifference();46 System.out.println(de

Full Screen

Full Screen

hasCustomComparator

Using AI Code Generation

copy

Full Screen

1public class Test {2 public static void main(String[] args) {3 DeepDifference deepDifference = new DeepDifference();4 boolean result = deepDifference.hasCustomComparator(String.class);5 System.out.println(result);6 }7}8public class Test {9 public static void main(String[] args) {10 DeepDifference deepDifference = new DeepDifference();11 Comparator<String> result = deepDifference.getCustomComparator(String.class);12 System.out.println(result);13 }14}

Full Screen

Full Screen

hasCustomComparator

Using AI Code Generation

copy

Full Screen

1public class Test {2 public static void main(String[] args) {3 DeepDifference deepDifference = new DeepDifference();4 deepDifference.hasCustomComparator(new Object(), new Object());5 }6}7public class Test {8 public static void main(String[] args) {9 DeepDifference deepDifference = new DeepDifference();10 deepDifference.hasCustomComparator(new Object(), new Object());11 }12}13import org.assertj.core.api.AbstractAssert;14import org.assertj.core.internal.DeepDifference;15import org.assertj.core.internal.DeepDifference.Difference;16public class Test extends AbstractAssert<Test, Object> {17 public Test(Object actual) {18 super(actual, Test.class);19 }20 public static Test assertThat(Object actual) {21 return new Test(actual);22 }23 public Test isEqualTo(Object expected) {24 DeepDifference deepDifference = new DeepDifference();25 Difference difference = deepDifference.determineDifferences(actual, expected);26 if (difference != null) {27 failWithMessage("expecting:%n <%s>%n but was:%n <%s>%n because:%n <%s>", actual, expected, difference);28 }29 return this;30 }31}32package org.assertj.core.internal;33public class DeepDifference {34 public Difference determineDifferences(Object actual, Object expected) {35 return null;36 }37 public boolean hasCustomComparator(Object actual, Object expected) {38 return false;39 }40 public static class Difference {41 }42}43import org.assertj.core.api.AbstractAssert;44import org.assertj.core.internal.DeepDifference;45import org.assertj.core.internal.DeepDifference.Difference;46public class Test extends AbstractAssert<Test, Object> {47 public Test(Object actual) {48 super(actual, Test.class);49 }50 public static Test assertThat(Object actual) {51 return new Test(actual);52 }53 public Test isEqualTo(Object expected) {54 DeepDifference deepDifference = new DeepDifference();55 Difference difference = deepDifference.determineDifferences(actual, expected);56 if (difference != null) {57 failWithMessage("expecting:%n <%s>%n but was:%n <%s>%n because:%n <%s>", actual, expected, difference);58 }59 return this;60 }61}62import org.assertj.core.api.AbstractAssert;63import org.assertj.core.internal.DeepDifference;64import org.assertj.core.internal.DeepDifference.Difference;

Full Screen

Full Screen

hasCustomComparator

Using AI Code Generation

copy

Full Screen

1import static org.assertj.core.internal.DeepDifference.*;2import org.assertj.core.internal.DeepDifference;3import java.util.Comparator;4public class 1 {5 public static void main(String[] args) {6 DeepDifference deepDifference = new DeepDifference();7 Comparator comparator = new Comparator() {8 public int compare(Object o1, Object o2) {9 return 0;10 }11 };12 System.out.println(deepDifference.hasCustomComparator(comparator));13 }14}15import static org.assertj.core.internal.DeepDifference.*;16import org.assertj.core.internal.DeepDifference;17import java.util.Comparator;

Full Screen

Full Screen

hasCustomComparator

Using AI Code Generation

copy

Full Screen

1package org.assertj.core.internal;2import org.assertj.core.util.diff.Delta;3import org.assertj.core.util.diff.DeltaVisitor;4import org.assertj.core.util.diff.DiffUtils;5import org.assertj.core.util.diff.Patch;6import org.assertj.core.util.introspection.IntrospectionError;7import java.util.ArrayList;8import java.util.Comparator;9import java.util.List;10import static java.util.Collections.emptyList;11import static org.assertj.core.error.ShouldBeEqual.shouldBeEqual;12import static org.assertj.core.error.ShouldHaveSameSizeAs.shouldHaveSameSizeAs;13import static org.assertj.core.error.ShouldNotBeNull.shouldNotBeNull;14import static org.assertj.core.internal.CommonValidations.checkIsNotNull;15import static org.assertj.core.util.Arrays.isArray;16import static org.assertj.core.util.Arrays.isNullOrEmpty;17import static org.assertj.core.util.Iterables.sizeOf;18import static org.assertj.core.util.Objects.areEqual;19public class DeepDifference {20 private static final Comparator<Object> DEFAULT_COMPARATOR = new DefaultComparator();21 private final ComparisonStrategy comparisonStrategy;22 private Comparator<Object> customComparator;23 public DeepDifference(ComparisonStrategy comparisonStrategy) {24 this.comparisonStrategy = comparisonStrategy;25 }26 public boolean hasCustomComparator() {27 return customComparator != null;28 }29 public void setCustomComparator(Comparator<Object> customComparator) {30 this.customComparator = customComparator;31 }32 public void clearCustomComparator() {33 this.customComparator = null;34 }35 public void assertEquals(Object actual, Object expected) {36 if (areEqual(actual, expected)) {37 return;38 }39 if (actual == null || expected == null) {40 throw failures.failure(info, shouldNotBeNull());41 }42 if (isArray(actual) && isArray(expected)) {43 assertArrayEquals(actual, expected);44 } else if (isIterable(actual) && isIterable(expected)) {45 assertIterableEquals(actual, expected);46 } else if (isNullOrEmpty(actual) || isNullOrEmpty(expected)) {47 throw failures.failure(info, shouldHaveSameSizeAs(actual, sizeOf(actual), sizeOf(expected)));48 } else if (isMap(actual) && isMap(expected)) {49 assertMapsEqual(actual, expected);50 } else if (isMultiDimensionalArray(actual) && isMultiDimensionalArray(expected)) {51 assertMultiDimensionalArrayEquals(actual, expected);52 } else {53 assertObjectsAreEqual(info, actual, expected

Full Screen

Full Screen

hasCustomComparator

Using AI Code Generation

copy

Full Screen

1public class AssertjCustomComparator {2 public static void main(String[] args) {3 Comparator<Comparable> comparator = (o1, o2) -> {4 return o1.compareTo(o2);5 };6 DeepDifference difference = new DeepDifference(comparator);7 Person p1 = new Person("John", 20);8 Person p2 = new Person("John", 20);9 boolean hasCustomComparator = difference.hasCustomComparator(p1);10 System.out.println("hasCustomComparator: " + hasCustomComparator);11 }12}13public class 2 {14 public static void main(String[] args) {15 DeepDifference deepDifference = new DeepDifference();16 Comparator comparator = new Comparator() {17 public int compare(Object o1, Object o2) {18 return 0;19 }20 };21 System.out.println(hasCustomComparator(deepDifference, comparator));22 }23}

Full Screen

Full Screen

hasCustomComparator

Using AI Code Generation

copy

Full Screen

1package org.assertj.core.internal;2import java.util.Comparator;3import org.assertj.core.internal.DeepDifference;4public class MyDeepDifference extends DeepDifference {5 public MyDeepDifference(Comparator<?> customComparator) {6 super(customComparator);7 }8 public boolean hasCustomComparator() {9 return super.hasCustomComparator();10 }11}12public class Test {13 public static void main(String[] args) {14 MyDeepDifference myDeepDifference = new MyDeepDifference(null);15 System.out.println(myDeepDifference.hasCustomComparator());16 }17}

Full Screen

Full Screen

hasCustomComparator

Using AI Code Generation

copy

Full Screen

1public class AssertjCustomComparator {2 public static void main(String[] args) {3 Comparator<Comparable> comparator = (o1, o2) -> {4 return o1.compareTo(o2);5 };6 DeepDifference difference = new DeepDifference(comparator);7 Person p1 = new Person("John", 20);8 Person p2 = new Person("John", 20);9 boolean hasCustomComparator = difference.hasCustomComparator(p1);10 System.out.println("hasCustomComparator: " + hasCustomComparator);11 }12}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful