How to use describeOrderedCollectionTypes method of org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator class

Best Assertj code snippet using org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.describeOrderedCollectionTypes

Source:RecursiveComparisonDifferenceCalculator.java Github

copy

Full Screen

...49 */50public class RecursiveComparisonDifferenceCalculator {51 private static final String DIFFERENT_ACTUAL_AND_EXPECTED_FIELD_TYPES = "expected field is %s but actual field is not (%s)";52 private static final String ACTUAL_NOT_ORDERED_COLLECTION = "expected field is an ordered collection but actual field is not (%s), ordered collections are: "53 + describeOrderedCollectionTypes();54 private static final String VALUE_FIELD_NAME = "value";55 private static final String STRICT_TYPE_ERROR = "the fields are considered different since the comparison enforces strict type check and %s is not a subtype of %s";56 private static final String DIFFERENT_SIZE_ERROR = "actual and expected values are %s of different size, actual size=%s when expected size=%s";57 private static final String MISSING_FIELDS = "%s can't be compared to %s as %s does not declare all %s fields, it lacks these: %s";58 private static final Map<Class<?>, Boolean> customEquals = new ConcurrentHashMap<>();59 private static final Map<Class<?>, Boolean> customHash = new ConcurrentHashMap<>();60 private static class ComparisonState {61 // Not using a Set as we want to precisely track visited values, a set would remove duplicates62 List<DualValue> visitedDualValues;63 List<ComparisonDifference> differences = new ArrayList<>();64 DualValueDeque dualValuesToCompare;65 RecursiveComparisonConfiguration recursiveComparisonConfiguration;66 public ComparisonState(List<DualValue> visited, RecursiveComparisonConfiguration recursiveComparisonConfiguration) {67 this.visitedDualValues = visited;68 this.dualValuesToCompare = new DualValueDeque(recursiveComparisonConfiguration);69 this.recursiveComparisonConfiguration = recursiveComparisonConfiguration;70 }71 void addDifference(DualValue dualValue) {72 differences.add(new ComparisonDifference(dualValue.getPath(), dualValue.actual, dualValue.expected));73 }74 void addDifference(DualValue dualValue, String description, Object... args) {75 differences.add(new ComparisonDifference(dualValue.getPath(), dualValue.actual, dualValue.expected,76 format(description, args)));77 }78 public List<ComparisonDifference> getDifferences() {79 Collections.sort(differences);80 return differences;81 }82 public boolean hasDualValuesToCompare() {83 return !dualValuesToCompare.isEmpty();84 }85 public DualValue pickDualValueToCompare() {86 final DualValue dualValue = dualValuesToCompare.removeFirst();87 if (dualValue.hasPotentialCyclingValues()) {88 // visited dual values are here to avoid cycle, java types don't have cycle, there is no need to track them.89 // moreover this would make should_fix_1854_minimal_test to fail (see the test for a detailed explanation)90 visitedDualValues.add(dualValue);91 }92 return dualValue;93 }94 private void registerForComparison(DualValue dualValue) {95 if (!visitedDualValues.contains(dualValue)) dualValuesToCompare.addFirst(dualValue);96 }97 private void initDualValuesToCompare(Object actual, Object expected, List<String> parentPath, boolean isRootObject) {98 DualValue dualValue = new DualValue(parentPath, actual, expected);99 boolean mustCompareFieldsRecursively = mustCompareFieldsRecursively(isRootObject, dualValue);100 if (dualValue.hasNoNullValues() && dualValue.hasNoContainerValues() && mustCompareFieldsRecursively) {101 // disregard the equals method and start comparing fields102 Set<String> nonIgnoredActualFieldsNames = recursiveComparisonConfiguration.getNonIgnoredActualFieldNames(dualValue);103 if (!nonIgnoredActualFieldsNames.isEmpty()) {104 // fields to ignore are evaluated when adding their corresponding dualValues to dualValuesToCompare which filters105 // ignored fields according to recursiveComparisonConfiguration106 Set<String> expectedFieldsNames = getFieldsNames(expected.getClass());107 if (expectedFieldsNames.containsAll(nonIgnoredActualFieldsNames)) {108 // we compare actual fields vs expected, ignoring expected additional fields109 for (String nonIgnoredActualFieldName : nonIgnoredActualFieldsNames) {110 DualValue fieldDualValue = new DualValue(parentPath, nonIgnoredActualFieldName,111 COMPARISON.getSimpleValue(nonIgnoredActualFieldName, actual),112 COMPARISON.getSimpleValue(nonIgnoredActualFieldName, expected));113 dualValuesToCompare.addFirst(fieldDualValue);114 }115 } else {116 dualValuesToCompare.addFirst(dualValue);117 }118 } else {119 dualValuesToCompare.addFirst(dualValue);120 }121 } else {122 dualValuesToCompare.addFirst(dualValue);123 }124 // We need to remove already visited fields pair to avoid infinite recursion in case125 // parent -> set{child} with child having a reference back to parent126 // it occurs to unordered collection where we compare all possible combination of the collection elements recursively127 // --128 // remove visited values one by one, DualValue.equals correctly compare respective actual and expected fields by reference129 visitedDualValues.forEach(visitedDualValue -> dualValuesToCompare.stream()130 .filter(dualValueToCompare -> dualValueToCompare.equals(visitedDualValue))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 compareXXX198 if (dualValue.isExpectedFieldAnArray()) {199 compareArrays(dualValue, comparisonState);200 continue;201 }202 // we compare ordered collections specifically as to be matching, each pair of elements at a given index must match.203 // concretely we compare: (col1[0] vs col2[0]), (col1[1] vs col2[1])...(col1[n] vs col2[n])204 if (dualValue.isExpectedFieldAnOrderedCollection()205 && !recursiveComparisonConfiguration.shouldIgnoreCollectionOrder(dualValue)) {206 compareOrderedCollections(dualValue, comparisonState);207 continue;208 }209 if (dualValue.isExpectedFieldAnIterable()) {210 compareUnorderedIterables(dualValue, comparisonState);211 continue;212 }213 if (dualValue.isExpectedFieldAnOptional()) {214 compareOptional(dualValue, comparisonState);215 continue;216 }217 // Compare two SortedMaps taking advantage of the fact that these Maps can be compared in O(N) time due to their ordering218 if (dualValue.isExpectedFieldASortedMap()) {219 compareSortedMap(dualValue, comparisonState);220 continue;221 }222 // Compare two Unordered Maps. This is a slightly more expensive comparison because order cannot be assumed, therefore a223 // temporary Map must be created, however the comparison still runs in O(N) time.224 if (dualValue.isExpectedFieldAMap()) {225 compareUnorderedMap(dualValue, comparisonState);226 continue;227 }228 if (shouldCompareDualValue(recursiveComparisonConfiguration, dualValue)) {229 if (!actualFieldValue.equals(expectedFieldValue)) comparisonState.addDifference(dualValue);230 continue;231 }232 Class<?> actualFieldValueClass = actualFieldValue.getClass();233 Class<?> expectedFieldClass = expectedFieldValue.getClass();234 if (recursiveComparisonConfiguration.isInStrictTypeCheckingMode() && expectedTypeIsNotSubtypeOfActualType(dualValue)) {235 comparisonState.addDifference(dualValue, STRICT_TYPE_ERROR, expectedFieldClass.getName(),236 actualFieldValueClass.getName());237 continue;238 }239 Set<String> actualNonIgnoredFieldsNames = recursiveComparisonConfiguration.getNonIgnoredActualFieldNames(dualValue);240 Set<String> expectedFieldsNames = getFieldsNames(expectedFieldClass);241 // Check if expected has more fields than actual, in that case the additional fields are reported as difference242 if (!expectedFieldsNames.containsAll(actualNonIgnoredFieldsNames)) {243 // report missing fields in actual244 Set<String> actualFieldsNamesNotInExpected = newHashSet(actualNonIgnoredFieldsNames);245 actualFieldsNamesNotInExpected.removeAll(expectedFieldsNames);246 String missingFields = actualFieldsNamesNotInExpected.toString();247 String expectedClassName = expectedFieldClass.getName();248 String actualClassName = actualFieldValueClass.getName();249 String missingFieldsDescription = format(MISSING_FIELDS, actualClassName, expectedClassName,250 expectedFieldClass.getSimpleName(), actualFieldValueClass.getSimpleName(),251 missingFields);252 comparisonState.addDifference(dualValue, missingFieldsDescription);253 } else { // TODO remove else to report more diff254 // compare actual's fields against expected :255 // - if actual has more fields than expected, the additional fields are ignored as expected is the reference256 for (String actualFieldName : actualNonIgnoredFieldsNames) {257 if (expectedFieldsNames.contains(actualFieldName)) {258 DualValue newDualValue = new DualValue(currentPath, actualFieldName,259 COMPARISON.getSimpleValue(actualFieldName, actualFieldValue),260 COMPARISON.getSimpleValue(actualFieldName, expectedFieldValue));261 comparisonState.registerForComparison(newDualValue);262 }263 }264 }265 }266 return comparisonState.getDifferences();267 }268 private static boolean shouldCompareDualValue(RecursiveComparisonConfiguration recursiveComparisonConfiguration,269 final DualValue dualValue) {270 return !recursiveComparisonConfiguration.shouldIgnoreOverriddenEqualsOf(dualValue)271 && hasOverriddenEquals(dualValue.actual.getClass());272 }273 // avoid comparing enum recursively since they contain static fields which are ignored in recursive comparison274 // this would make different field enum value to be considered the same!275 private static void compareAsEnums(final DualValue dualValue,276 ComparisonState comparisonState,277 RecursiveComparisonConfiguration recursiveComparisonConfiguration) {278 if (recursiveComparisonConfiguration.isInStrictTypeCheckingMode()) {279 // we can use == for comparison which checks both actual and expected values and types are the same280 if (dualValue.actual != dualValue.expected) comparisonState.addDifference(dualValue);281 return;282 }283 if (!dualValue.isActualAnEnum()) {284 comparisonState.addDifference(dualValue, differentTypeErrorMessage(dualValue, "an enum"));285 return;286 }287 // both actual and expected are enums288 Enum<?> actualEnum = (Enum<?>) dualValue.actual;289 Enum<?> expectedEnum = (Enum<?>) dualValue.expected;290 // we must only compare actual and expected enum by value but not by type291 if (!actualEnum.name().equals(expectedEnum.name())) comparisonState.addDifference(dualValue);292 }293 private static boolean shouldHonorOverriddenEquals(DualValue dualValue,294 RecursiveComparisonConfiguration recursiveComparisonConfiguration) {295 boolean shouldNotIgnoreOverriddenEqualsIfAny = !recursiveComparisonConfiguration.shouldIgnoreOverriddenEqualsOf(dualValue);296 return shouldNotIgnoreOverriddenEqualsIfAny && dualValue.actual != null && hasOverriddenEquals(dualValue.actual.getClass());297 }298 private static void compareArrays(DualValue dualValue, ComparisonState comparisonState) {299 if (!dualValue.isActualFieldAnArray()) {300 // at the moment we only allow comparing arrays with arrays but we might allow comparing to collections later on301 // but only if we are not in strict type mode.302 comparisonState.addDifference(dualValue, differentTypeErrorMessage(dualValue, "an array"));303 return;304 }305 // both values in dualValue are arrays306 int actualArrayLength = Array.getLength(dualValue.actual);307 int expectedArrayLength = Array.getLength(dualValue.expected);308 if (actualArrayLength != expectedArrayLength) {309 comparisonState.addDifference(dualValue, DIFFERENT_SIZE_ERROR, "arrays", actualArrayLength, expectedArrayLength);310 // no need to inspect elements, arrays are not equal as they don't have the same size311 return;312 }313 // register each pair of actual/expected elements for recursive comparison314 List<String> arrayFieldPath = dualValue.getPath();315 for (int i = 0; i < actualArrayLength; i++) {316 Object actualElement = Array.get(dualValue.actual, i);317 Object expectedElement = Array.get(dualValue.expected, i);318 // TODO add [i] to the path ?319 comparisonState.registerForComparison(new DualValue(arrayFieldPath, actualElement, expectedElement));320 }321 }322 /*323 * Deeply compare two Collections that must be same length and in same order.324 */325 private static void compareOrderedCollections(DualValue dualValue, ComparisonState comparisonState) {326 if (!dualValue.isActualFieldAnOrderedCollection()) {327 // at the moment if expected is an ordered collection then actual should also be one328 comparisonState.addDifference(dualValue, ACTUAL_NOT_ORDERED_COLLECTION, dualValue.actual.getClass().getCanonicalName());329 return;330 }331 Collection<?> actualCollection = (Collection<?>) dualValue.actual;332 Collection<?> expectedCollection = (Collection<?>) dualValue.expected;333 if (actualCollection.size() != expectedCollection.size()) {334 comparisonState.addDifference(dualValue, DIFFERENT_SIZE_ERROR,335 "collections", actualCollection.size(), expectedCollection.size());336 // no need to inspect elements, arrays are not equal as they don't have the same size337 return;338 }339 // register pair of elements with same index for later comparison as we compare elements in order340 Iterator<?> expectedIterator = expectedCollection.iterator();341 List<String> path = dualValue.getPath();342 actualCollection.stream()343 .map(element -> new DualValue(path, element, expectedIterator.next()))344 .forEach(comparisonState::registerForComparison);345 }346 private static String differentTypeErrorMessage(DualValue dualValue, String actualTypeDescription) {347 return format(DIFFERENT_ACTUAL_AND_EXPECTED_FIELD_TYPES,348 actualTypeDescription, dualValue.actual.getClass().getCanonicalName());349 }350 private static void compareUnorderedIterables(DualValue dualValue, ComparisonState comparisonState) {351 if (!dualValue.isActualFieldAnIterable()) {352 // at the moment we only compare iterable with iterables (but we might allow arrays too)353 comparisonState.addDifference(dualValue, differentTypeErrorMessage(dualValue, "an iterable"));354 return;355 }356 Iterable<?> actual = (Iterable<?>) dualValue.actual;357 Iterable<?> expected = (Iterable<?>) dualValue.expected;358 int actualSize = sizeOf(actual);359 int expectedSize = sizeOf(expected);360 if (actualSize != expectedSize) {361 comparisonState.addDifference(dualValue, DIFFERENT_SIZE_ERROR, "collections", actualSize, expectedSize);362 // no need to inspect elements, iterables are not equal as they don't have the same size363 return;364 // TODO instead we could register the diff between expected and actual that is:365 // - unexpected actual elements (the ones not matching any expected)366 // - expected elements not found in actual.367 }368 List<String> path = dualValue.getPath();369 // copy expected as we will remove elements found in actual370 Collection<?> expectedCopy = new LinkedList<>(toCollection(expected));371 for (Object actualElement : actual) {372 // compare recursively actualElement to all remaining expected elements373 Iterator<?> expectedIterator = expectedCopy.iterator();374 while (expectedIterator.hasNext()) {375 Object expectedElement = expectedIterator.next();376 // we need to get the currently visited dual values otherwise a cycle would cause an infinite recursion.377 List<ComparisonDifference> differences = determineDifferences(actualElement, expectedElement, path, false,378 comparisonState.visitedDualValues,379 comparisonState.recursiveComparisonConfiguration);380 if (differences.isEmpty()) {381 // we found an element in expected matching actualElement, we must remove it as if actual matches expected382 // it means for each actual element there is one and only matching expected element.383 expectedIterator.remove();384 // jump to next actual element check385 break;386 }387 }388 }389 // expectedCopy not empty = there was at least one actual element not matching any expected elements.390 if (!expectedCopy.isEmpty()) comparisonState.addDifference(dualValue);391 // TODO instead we could register the diff between expected and actual that is:392 // - unexpected actual elements (the ones not matching any expected)393 // - expected elements not found in actual.394 }395 private static <K, V> void compareSortedMap(DualValue dualValue, ComparisonState comparisonState) {396 if (!dualValue.isActualFieldASortedMap()) {397 // at the moment we only compare iterable with iterables (but we might allow arrays too)398 comparisonState.addDifference(dualValue, differentTypeErrorMessage(dualValue, "a sorted map"));399 return;400 }401 Map<?, ?> actualMap = (Map<?, ?>) dualValue.actual;402 @SuppressWarnings("unchecked")403 Map<K, V> expectedMap = (Map<K, V>) dualValue.expected;404 if (actualMap.size() != expectedMap.size()) {405 comparisonState.addDifference(dualValue, DIFFERENT_SIZE_ERROR, "sorted maps", actualMap.size(), expectedMap.size());406 // no need to inspect entries, maps are not equal as they don't have the same size407 return;408 // TODO instead we could register the diff between expected and actual that is:409 // - unexpected actual entries (the ones not matching any expected)410 // - expected entries not found in actual.411 }412 List<String> path = dualValue.getPath();413 Iterator<Map.Entry<K, V>> expectedMapEntries = expectedMap.entrySet().iterator();414 for (Map.Entry<?, ?> actualEntry : actualMap.entrySet()) {415 Map.Entry<?, ?> expectedEntry = expectedMapEntries.next();416 // Must split the Key and Value so that Map.Entry's equals() method is not used.417 comparisonState.registerForComparison(new DualValue(path, actualEntry.getKey(), expectedEntry.getKey()));418 comparisonState.registerForComparison(new DualValue(path, actualEntry.getValue(), expectedEntry.getValue()));419 }420 }421 private static void compareUnorderedMap(DualValue dualValue, ComparisonState comparisonState) {422 if (!dualValue.isActualFieldAMap()) {423 comparisonState.addDifference(dualValue, differentTypeErrorMessage(dualValue, "a map"));424 return;425 }426 Map<?, ?> actualMap = (Map<?, ?>) dualValue.actual;427 Map<?, ?> expectedMap = (Map<?, ?>) dualValue.expected;428 if (actualMap.size() != expectedMap.size()) {429 comparisonState.addDifference(dualValue, DIFFERENT_SIZE_ERROR, "maps", actualMap.size(), expectedMap.size());430 // no need to inspect entries, maps are not equal as they don't have the same size431 return;432 // TODO instead we could register the diff between expected and actual that is:433 // - unexpected actual entries (the ones not matching any expected)434 // - expected entries not found in actual.435 }436 Map<Integer, Map.Entry<?, ?>> fastLookup = expectedMap.entrySet().stream()437 .collect(toMap(entry -> deepHashCode(entry.getKey()), entry -> entry));438 List<String> path = dualValue.getPath();439 for (Map.Entry<?, ?> actualEntry : actualMap.entrySet()) {440 int deepHashCode = deepHashCode(actualEntry.getKey());441 if (!fastLookup.containsKey(deepHashCode)) {442 // TODO add description of the entry in actual not found in expected.443 comparisonState.addDifference(dualValue);444 return;445 }446 Map.Entry<?, ?> expectedEntry = fastLookup.get(deepHashCode);447 // Must split the Key and Value so that Map.Entry's equals() method is not used.448 comparisonState.registerForComparison(new DualValue(path, actualEntry.getKey(), expectedEntry.getKey()));449 comparisonState.registerForComparison(new DualValue(path, actualEntry.getValue(), expectedEntry.getValue()));450 }451 }452 private static void compareOptional(DualValue dualValue, ComparisonState comparisonState) {453 if (!dualValue.isActualFieldAnOptional()) {454 comparisonState.addDifference(dualValue, differentTypeErrorMessage(dualValue, "an Optional"));455 return;456 }457 Optional<?> actual = (Optional<?>) dualValue.actual;458 Optional<?> expected = (Optional<?>) dualValue.expected;459 if (actual.isPresent() != expected.isPresent()) {460 comparisonState.addDifference(dualValue);461 return;462 }463 // either both are empty or present464 if (!actual.isPresent()) return; // both optional are empty => end of the comparison465 // both are present, we have to compare their values recursively466 Object value1 = actual.get();467 Object value2 = expected.get();468 // we add VALUE_FIELD_NAME to the path since we register Optional.value fields.469 comparisonState.registerForComparison(new DualValue(dualValue.getPath(), VALUE_FIELD_NAME, value1, value2));470 }471 /**472 * Determine if the passed in class has a non-Object.equals() method. This473 * method caches its results in static ConcurrentHashMap to benefit474 * execution performance.475 *476 * @param c Class to check.477 * @return true, if the passed in Class has a .equals() method somewhere478 * between itself and just below Object in it's inheritance.479 */480 static boolean hasOverriddenEquals(Class<?> c) {481 if (customEquals.containsKey(c)) {482 return customEquals.get(c);483 }484 Class<?> origClass = c;485 while (!Object.class.equals(c)) {486 try {487 c.getDeclaredMethod("equals", Object.class);488 customEquals.put(origClass, true);489 return true;490 } catch (Exception ignored) {}491 c = c.getSuperclass();492 }493 customEquals.put(origClass, false);494 return false;495 }496 /**497 * Get a deterministic hashCode (int) value for an Object, regardless of498 * when it was created or where it was loaded into memory. The problem with499 * java.lang.Object.hashCode() is that it essentially relies on memory500 * location of an object (what identity it was assigned), whereas this501 * method will produce the same hashCode for any object graph, regardless of502 * how many times it is created.<br>503 * <br>504 *505 * This method will handle cycles correctly (A-&gt;B-&gt;C-&gt;A). In this506 * case, Starting with object A, B, or C would yield the same hashCode. If507 * an object encountered (root, subobject, etc.) has a hashCode() method on508 * it (that is not Object.hashCode()), that hashCode() method will be called509 * and it will stop traversal on that branch.510 *511 * @param obj Object who hashCode is desired.512 * @return the 'deep' hashCode value for the passed in object.513 */514 static int deepHashCode(Object obj) {515 Set<Object> visited = new HashSet<>();516 LinkedList<Object> stack = new LinkedList<>();517 stack.addFirst(obj);518 int hash = 0;519 while (!stack.isEmpty()) {520 obj = stack.removeFirst();521 if (obj == null || visited.contains(obj)) {522 continue;523 }524 visited.add(obj);525 if (obj.getClass().isArray()) {526 int len = Array.getLength(obj);527 for (int i = 0; i < len; i++) {528 stack.addFirst(Array.get(obj, i));529 }530 continue;531 }532 if (obj instanceof Collection) {533 stack.addAll(0, (Collection<?>) obj);534 continue;535 }536 if (obj instanceof Map) {537 stack.addAll(0, ((Map<?, ?>) obj).keySet());538 stack.addAll(0, ((Map<?, ?>) obj).values());539 continue;540 }541 if (obj instanceof Double || obj instanceof Float) {542 // just take the integral value for hashcode543 // equality tests things more comprehensively544 stack.add(Math.round(((Number) obj).doubleValue()));545 continue;546 }547 if (hasCustomHashCode(obj.getClass())) {548 // A real hashCode() method exists, call it.549 hash += obj.hashCode();550 continue;551 }552 Collection<Field> fields = getDeclaredFieldsIncludingInherited(obj.getClass());553 for (Field field : fields) {554 stack.addFirst(COMPARISON.getSimpleValue(field.getName(), obj));555 }556 }557 return hash;558 }559 /**560 * Determine if the passed in class has a non-Object.hashCode() method. This561 * method caches its results in static ConcurrentHashMap to benefit562 * execution performance.563 *564 * @param c Class to check.565 * @return true, if the passed in Class has a .hashCode() method somewhere566 * between itself and just below Object in it's inheritance.567 */568 static boolean hasCustomHashCode(Class<?> c) {569 Class<?> origClass = c;570 if (customHash.containsKey(c)) {571 return customHash.get(c);572 }573 while (!Object.class.equals(c)) {574 try {575 c.getDeclaredMethod("hashCode");576 customHash.put(origClass, true);577 return true;578 } catch (Exception ignored) {}579 c = c.getSuperclass();580 }581 customHash.put(origClass, false);582 return false;583 }584 @SuppressWarnings({ "unchecked", "rawtypes" })585 private static boolean propertyOrFieldValuesAreEqual(DualValue dualValue,586 RecursiveComparisonConfiguration recursiveComparisonConfiguration) {587 final String fieldName = dualValue.getConcatenatedPath();588 final Object actualFieldValue = dualValue.actual;589 final Object expectedFieldValue = dualValue.expected;590 // no need to look into comparators if objects are the same591 if (actualFieldValue == expectedFieldValue) return true;592 // check field comparators as they take precedence over type comparators593 Comparator fieldComparator = recursiveComparisonConfiguration.getComparatorForField(fieldName);594 if (fieldComparator != null) return fieldComparator.compare(actualFieldValue, expectedFieldValue) == 0;595 // check if a type comparators exist for the field type596 Class fieldType = actualFieldValue != null ? actualFieldValue.getClass() : expectedFieldValue.getClass();597 Comparator typeComparator = recursiveComparisonConfiguration.getComparatorForType(fieldType);598 if (typeComparator != null) return typeComparator.compare(actualFieldValue, expectedFieldValue) == 0;599 // default comparison using equals600 return Objects.areEqual(actualFieldValue, expectedFieldValue);601 }602 private static ComparisonDifference expectedAndActualTypeDifference(Object actual, Object expected) {603 String additionalInformation = format("actual and expected are considered different since the comparison enforces strict type check and expected type %s is not a subtype of actual type %s",604 expected.getClass().getName(), actual.getClass().getName());605 return rootComparisonDifference(actual, expected, additionalInformation);606 }607 // TODO should be checking actual!608 private static boolean expectedTypeIsNotSubtypeOfActualType(DualValue dualField) {609 return expectedTypeIsNotSubtypeOfActualType(dualField.actual, dualField.expected);610 }611 private static boolean expectedTypeIsNotSubtypeOfActualType(Object actual, Object expected) {612 return !actual.getClass().isAssignableFrom(expected.getClass());613 }614 private static String describeOrderedCollectionTypes() {615 return Stream.of(DEFAULT_ORDERED_COLLECTION_TYPES)616 .map(Class::getName)617 .collect(joining(", ", "[", "]"));618 }619}...

Full Screen

Full Screen

describeOrderedCollectionTypes

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator;2import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifference;3import org.assertj.core.api.recursive.comparison.Difference;4import org.assertj.core.api.recursive.comparison.DifferenceEvaluator;5import java.util.List;6import java.util.Map;7import java.util.Set;8List<RecursiveComparisonDifference> differences = new RecursiveComparisonDifferenceCalculator().calculateDifferences(actual, expected);9List<Difference> differences = differences.get(0).getDifferences();10List<Difference> differences = differences.get(1).getDifferences();11List<Difference> differences = differences.get(2).getDifferences();12List<Difference> differences = differences.get(3).getDifferences();13List<Difference> differences = differences.get(0).getDifferences();14List<Difference> differences = differences.get(1).getDifferences();15List<Difference> differences = differences.get(2).getDifferences();16List<Difference> differences = differences.get(0).getDifferences();17List<Difference> differences = differences.get(1).getDifferences();18List<Difference> differences = differences.get(2).getDifferences();19List<Difference> differences = differences.get(3).getDifferences();20List<Difference> differences = differences.get(0).getDifferences();21List<Difference> differences = differences.get(1).getDifferences();22List<Difference> differences = differences.get(2).getDifferences();23List<Difference> differences = differences.get(3).getDifferences();24List<Difference> differences = differences.get(0).getDifferences();25List<Difference> differences = differences.get(1).getDifferences();26List<Difference> differences = differences.get(2).getDifferences();27List<Difference> differences = differences.get(3).getDifferences();28List<Difference> differences = differences.get(0).getDifferences();29List<Difference> differences = differences.get(1).getDifferences();30List<Difference> differences = differences.get(2).getDifferences();31List<Difference> differences = differences.get(3).getDifferences();32List<Difference> differences = differences.get(0).getDifferences();33List<Difference> differences = differences.get(1).getDifferences();34List<Difference> differences = differences.get(2).getDifferences();35List<Difference> differences = differences.get(0).getDifferences();

Full Screen

Full Screen

describeOrderedCollectionTypes

Using AI Code Generation

copy

Full Screen

1import static org.assertj.core.api.Assertions.*;2import static org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.*;3import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifference;4import java.util.ArrayList;5import java.util.List;6public class Example {7 public static void main(String[] args) {8 List<String> actual = new ArrayList<>();9 actual.add("John");10 actual.add("Doe");11 List<String> expected = new ArrayList<>();12 expected.add("John");13 expected.add("Doe");14 RecursiveComparisonDifference difference = describeOrderedCollectionTypes(actual, expected);15 System.out.println(difference);16 }17}18package org.assertj.core.api.recursive.comparison;19import java.util.ArrayList;20import java.util.List;21public class RecursiveComparisonDifference {22 private final String description;23 private final List<RecursiveComparisonDifference> children = new ArrayList<>();24 public RecursiveComparisonDifference(String description) {25 this.description = description;26 }27 public String getDescription() {28 return description;29 }30 public List<RecursiveComparisonDifference> getChildren() {31 return children;32 }33 public void addChild(RecursiveComparisonDifference child) {34 children.add(child);35 }36 public String toString() {37 return "RecursiveComparisonDifference{" +38 '}';39 }40}

Full Screen

Full Screen

describeOrderedCollectionTypes

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifference;2import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator;3import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.RecursiveComparisonDifferenceCalculatorBuilder;4import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.RecursiveComparisonDifferenceCalculatorBuilder.RecursiveComparisonDifferenceCalculatorBuilderWithDifferences;5import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.RecursiveComparisonDifferenceCalculatorBuilder.RecursiveComparisonDifferenceCalculatorBuilderWithDifferences.RecursiveComparisonDifferenceCalculatorBuilderWithDifferencesAndDifferences;6import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.RecursiveComparisonDifferenceCalculatorBuilder.RecursiveComparisonDifferenceCalculatorBuilderWithDifferences.RecursiveComparisonDifferenceCalculatorBuilderWithDifferencesAndDifferences.RecursiveComparisonDifferenceCalculatorBuilderWithDifferencesAndDifferencesAndDifferences;7import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.RecursiveComparisonDifferenceCalculatorBuilder.RecursiveComparisonDifferenceCalculatorBuilderWithDifferences.RecursiveComparisonDifferenceCalculatorBuilderWithDifferencesAndDifferences.RecursiveComparisonDifferenceCalculatorBuilderWithDifferencesAndDifferencesAndDifferences.RecursiveComparisonDifferenceCalculatorBuilderWithDifferencesAndDifferencesAndDifferencesAndDifferences;8import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.RecursiveComparisonDifferenceCalculatorBuilder.RecursiveComparisonDifferenceCalculatorBuilderWithDifferences.RecursiveComparisonDifferenceCalculatorBuilderWithDifferencesAndDifferences.RecursiveComparisonDifferenceCalculatorBuilderWithDifferencesAndDifferencesAndDifferences.RecursiveComparisonDifferenceCalculatorBuilderWithDifferencesAndDifferencesAndDifferencesAndDifferences.RecursiveComparisonDifferenceCalculatorBuilderWithDifferencesAndDifferencesAndDifferencesAndDifferencesAndDifferences;9import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.RecursiveComparisonDifferenceCalculatorBuilder.RecursiveComparisonDifferenceCalculatorBuilderWithDifferences.RecursiveComparisonDifferenceCalculatorBuilderWithDifferencesAndDifferences.RecursiveComparisonDifferenceCalculatorBuilderWithDifferencesAndDifferencesAndDifferences.RecursiveComparisonDifferenceCalculatorBuilderWithDifferencesAndDifferencesAndDifferencesAndDifferences.RecursiveComparisonDifferenceCalculatorBuilderWithDifferencesAndDifferencesAndDifferencesAndDifferencesAndDifferences.RecursiveComparisonDifferenceCalculatorBuilderWithDifferencesAndDifferencesAndDifferencesAndDifferencesAndDifferencesAndDifferences;

Full Screen

Full Screen

describeOrderedCollectionTypes

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifference2import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator3def diff = new RecursiveComparisonDifferenceCalculator().describeOrderedCollectionTypes(collection1, collection2)4def diff2 = new RecursiveComparisonDifferenceCalculator().describeOrderedCollectionTypes(collection1, collection2)5def diff3 = new RecursiveComparisonDifferenceCalculator().describeOrderedCollectionTypes(collection1, collection2)6println diff3.toString()

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