How to use getConcatenatedPath method of org.assertj.core.api.recursive.comparison.DualValue class

Best Assertj code snippet using org.assertj.core.api.recursive.comparison.DualValue.getConcatenatedPath

Source:RecursiveComparisonDifferenceCalculator.java Github

copy

Full Screen

...145 && !shouldHonorEquals(dualValue, recursiveComparisonConfiguration)146 && dualValue.hasNoContainerValues();147 }148 private String getCustomErrorMessage(DualValue dualValue) {149 String fieldName = dualValue.getConcatenatedPath();150 // field custome messages take precedence over type messages151 if (recursiveComparisonConfiguration.hasCustomMessageForField(fieldName)) {152 return recursiveComparisonConfiguration.getMessageForField(fieldName);153 }154 Class<?> fieldType = dualValue.actual != null ? dualValue.actual.getClass() : dualValue.expected.getClass();155 if (recursiveComparisonConfiguration.hasCustomMessageForType(fieldType)) {156 return recursiveComparisonConfiguration.getMessageForType(fieldType);157 }158 return null;159 }160 }161 /**162 * Compare two objects for differences by doing a 'deep' comparison. This will traverse the163 * Object graph and perform either a field-by-field comparison on each164 * object (if not .equals() method has been overridden from Object), or it165 * will call the customized .equals() method if it exists.166 * <p>167 *168 * This method handles cycles correctly, for example A-&gt;B-&gt;C-&gt;A.169 * Suppose a and a' are two separate instances of the A with the same values170 * for all fields on A, B, and C. Then a.deepEquals(a') will return an empty list. It171 * uses cycle detection storing visited objects in a Set to prevent endless172 * loops.173 *174 * @param actual Object one to compare175 * @param expected Object two to compare176 * @param recursiveComparisonConfiguration the recursive comparison configuration177 * @return the list of differences found or an empty list if objects are equivalent.178 * Equivalent means that all field values of both subgraphs are the same,179 * either at the field level or via the respectively encountered overridden180 * .equals() methods during traversal.181 */182 public List<ComparisonDifference> determineDifferences(Object actual, Object expected,183 RecursiveComparisonConfiguration recursiveComparisonConfiguration) {184 if (recursiveComparisonConfiguration.isInStrictTypeCheckingMode() && expectedTypeIsNotSubtypeOfActualType(actual, expected)) {185 return list(expectedAndActualTypeDifference(actual, expected));186 }187 List<DualValue> visited = list();188 return determineDifferences(actual, expected, rootFieldLocation(), visited, recursiveComparisonConfiguration);189 }190 // TODO keep track of ignored fields in an RecursiveComparisonExecution class ?191 private static List<ComparisonDifference> determineDifferences(Object actual, Object expected, FieldLocation fieldLocation,192 List<DualValue> visited,193 RecursiveComparisonConfiguration recursiveComparisonConfiguration) {194 ComparisonState comparisonState = new ComparisonState(visited, recursiveComparisonConfiguration);195 comparisonState.initDualValuesToCompare(actual, expected, fieldLocation);196 while (comparisonState.hasDualValuesToCompare()) {197 final DualValue dualValue = comparisonState.pickDualValueToCompare();198 final Object actualFieldValue = dualValue.actual;199 final Object expectedFieldValue = dualValue.expected;200 // Custom comparators take precedence over all other types of comparison201 if (recursiveComparisonConfiguration.hasCustomComparator(dualValue)) {202 if (!areDualValueEqual(dualValue, recursiveComparisonConfiguration)) comparisonState.addDifference(dualValue);203 // since we used a custom comparator we don't need to inspect the nested fields any further204 continue;205 }206 if (actualFieldValue == expectedFieldValue) continue;207 if (actualFieldValue == null || expectedFieldValue == null) {208 // one of the value is null while the other is not as we already know that actualFieldValue != expectedFieldValue209 comparisonState.addDifference(dualValue);210 continue;211 }212 if (dualValue.isExpectedAnEnum()) {213 compareAsEnums(dualValue, comparisonState, recursiveComparisonConfiguration);214 continue;215 }216 // TODO move hasFieldTypesDifference check into each compareXXX217 if (dualValue.isExpectedFieldAnArray()) {218 compareArrays(dualValue, comparisonState);219 continue;220 }221 // we compare ordered collections specifically as to be matching, each pair of elements at a given index must match.222 // concretely we compare: (col1[0] vs col2[0]), (col1[1] vs col2[1])...(col1[n] vs col2[n])223 if (dualValue.isExpectedFieldAnOrderedCollection()224 && !recursiveComparisonConfiguration.shouldIgnoreCollectionOrder(dualValue.fieldLocation)) {225 compareOrderedCollections(dualValue, comparisonState);226 continue;227 }228 if (dualValue.isExpectedFieldAnIterable()) {229 compareUnorderedIterables(dualValue, comparisonState);230 continue;231 }232 if (dualValue.isExpectedFieldAnOptional()) {233 compareOptional(dualValue, comparisonState);234 continue;235 }236 // Compare two SortedMaps taking advantage of the fact that these Maps can be compared in O(N) time due to their ordering237 if (dualValue.isExpectedFieldASortedMap()) {238 compareSortedMap(dualValue, comparisonState);239 continue;240 }241 // Compare two Unordered Maps. This is a slightly more expensive comparison because order cannot be assumed, therefore a242 // temporary Map must be created, however the comparison still runs in O(N) time.243 if (dualValue.isExpectedFieldAMap()) {244 compareUnorderedMap(dualValue, comparisonState);245 continue;246 }247 // compare Atomic types by value manually as they are container type and we can't use introspection in java 17+248 if (dualValue.isExpectedFieldAnAtomicBoolean()) {249 compareAtomicBoolean(dualValue, comparisonState);250 continue;251 }252 if (dualValue.isExpectedFieldAnAtomicInteger()) {253 compareAtomicInteger(dualValue, comparisonState);254 continue;255 }256 if (dualValue.isExpectedFieldAnAtomicIntegerArray()) {257 compareAtomicIntegerArray(dualValue, comparisonState);258 continue;259 }260 if (dualValue.isExpectedFieldAnAtomicLong()) {261 compareAtomicLong(dualValue, comparisonState);262 continue;263 }264 if (dualValue.isExpectedFieldAnAtomicLongArray()) {265 compareAtomicLongArray(dualValue, comparisonState);266 continue;267 }268 if (dualValue.isExpectedFieldAnAtomicReference()) {269 compareAtomicReference(dualValue, comparisonState);270 continue;271 }272 if (dualValue.isExpectedFieldAnAtomicReferenceArray()) {273 compareAtomicReferenceArray(dualValue, comparisonState);274 continue;275 }276 if (shouldHonorEquals(dualValue, recursiveComparisonConfiguration)) {277 if (!actualFieldValue.equals(expectedFieldValue)) comparisonState.addDifference(dualValue);278 continue;279 }280 Class<?> actualFieldValueClass = actualFieldValue.getClass();281 Class<?> expectedFieldClass = expectedFieldValue.getClass();282 if (recursiveComparisonConfiguration.isInStrictTypeCheckingMode() && expectedTypeIsNotSubtypeOfActualType(dualValue)) {283 comparisonState.addDifference(dualValue,284 format(STRICT_TYPE_ERROR, expectedFieldClass.getName(), actualFieldValueClass.getName()));285 continue;286 }287 Set<String> actualNonIgnoredFieldsNames = recursiveComparisonConfiguration.getActualFieldNamesToCompare(dualValue);288 Set<String> expectedFieldsNames = getFieldsNames(expectedFieldClass);289 // Check if expected has more fields than actual, in that case the additional fields are reported as difference290 if (!expectedFieldsNames.containsAll(actualNonIgnoredFieldsNames)) {291 // report missing fields in actual292 Set<String> actualFieldsNamesNotInExpected = newHashSet(actualNonIgnoredFieldsNames);293 actualFieldsNamesNotInExpected.removeAll(expectedFieldsNames);294 String missingFields = actualFieldsNamesNotInExpected.toString();295 String expectedClassName = expectedFieldClass.getName();296 String actualClassName = actualFieldValueClass.getName();297 String missingFieldsDescription = format(MISSING_FIELDS, actualClassName, expectedClassName,298 expectedFieldClass.getSimpleName(), actualFieldValueClass.getSimpleName(),299 missingFields);300 comparisonState.addDifference(dualValue, missingFieldsDescription);301 } else { // TODO remove else to report more diff302 // compare actual's fields against expected :303 // - if actual has more fields than expected, the additional fields are ignored as expected is the reference304 for (String actualFieldName : actualNonIgnoredFieldsNames) {305 if (expectedFieldsNames.contains(actualFieldName)) {306 DualValue newDualValue = new DualValue(dualValue.fieldLocation.field(actualFieldName),307 COMPARISON.getSimpleValue(actualFieldName, actualFieldValue),308 COMPARISON.getSimpleValue(actualFieldName, expectedFieldValue));309 comparisonState.registerForComparison(newDualValue);310 }311 }312 }313 }314 return comparisonState.getDifferences();315 }316 // avoid comparing enum recursively since they contain static fields which are ignored in recursive comparison317 // this would make different field enum value to be considered the same!318 private static void compareAsEnums(final DualValue dualValue,319 ComparisonState comparisonState,320 RecursiveComparisonConfiguration recursiveComparisonConfiguration) {321 if (recursiveComparisonConfiguration.isInStrictTypeCheckingMode()) {322 // we can use == for comparison which checks both actual and expected values and types are the same323 if (dualValue.actual != dualValue.expected) comparisonState.addDifference(dualValue);324 return;325 }326 if (!dualValue.isActualAnEnum()) {327 comparisonState.addDifference(dualValue, differentTypeErrorMessage(dualValue, "an enum"));328 return;329 }330 // both actual and expected are enums331 Enum<?> actualEnum = (Enum<?>) dualValue.actual;332 Enum<?> expectedEnum = (Enum<?>) dualValue.expected;333 // we must only compare actual and expected enum by value but not by type334 if (!actualEnum.name().equals(expectedEnum.name())) comparisonState.addDifference(dualValue);335 }336 private static boolean shouldHonorEquals(DualValue dualValue,337 RecursiveComparisonConfiguration recursiveComparisonConfiguration) {338 // since java 17 we can't introspect java types and get their fields so by default we compare them with equals339 // unless for some container like java types: iterables, array, optional, atomic values where we take the contained values340 // through accessors and register them in the recursive comparison.341 boolean shouldHonorJavaTypeEquals = dualValue.hasSomeJavaTypeValue() && !dualValue.isExpectedAContainer();342 return shouldHonorJavaTypeEquals || shouldHonorOverriddenEquals(dualValue, recursiveComparisonConfiguration);343 }344 private static boolean shouldHonorOverriddenEquals(DualValue dualValue,345 RecursiveComparisonConfiguration recursiveComparisonConfiguration) {346 boolean shouldNotIgnoreOverriddenEqualsIfAny = !recursiveComparisonConfiguration.shouldIgnoreOverriddenEqualsOf(dualValue);347 return shouldNotIgnoreOverriddenEqualsIfAny && dualValue.actual != null && hasOverriddenEquals(dualValue.actual.getClass());348 }349 private static void compareArrays(DualValue dualValue, ComparisonState comparisonState) {350 if (!dualValue.isActualFieldAnArray()) {351 // at the moment we only allow comparing arrays with arrays but we might allow comparing to collections later on352 // but only if we are not in strict type mode.353 comparisonState.addDifference(dualValue, differentTypeErrorMessage(dualValue, "an array"));354 return;355 }356 // both values in dualValue are arrays357 int actualArrayLength = Array.getLength(dualValue.actual);358 int expectedArrayLength = Array.getLength(dualValue.expected);359 if (actualArrayLength != expectedArrayLength) {360 comparisonState.addDifference(dualValue, format(DIFFERENT_SIZE_ERROR, "arrays", actualArrayLength, expectedArrayLength));361 // no need to inspect elements, arrays are not equal as they don't have the same size362 return;363 }364 // register each pair of actual/expected elements for recursive comparison365 FieldLocation arrayFieldLocation = dualValue.fieldLocation;366 for (int i = 0; i < actualArrayLength; i++) {367 Object actualElement = Array.get(dualValue.actual, i);368 Object expectedElement = Array.get(dualValue.expected, i);369 FieldLocation elementFieldLocation = arrayFieldLocation.field(format("[%d]", i));370 comparisonState.registerForComparison(new DualValue(elementFieldLocation, actualElement, expectedElement));371 }372 }373 /*374 * Deeply compare two Collections that must be same length and in same order.375 */376 private static void compareOrderedCollections(DualValue dualValue, ComparisonState comparisonState) {377 if (!dualValue.isActualFieldAnOrderedCollection()) {378 // at the moment if expected is an ordered collection then actual should also be one379 comparisonState.addDifference(dualValue,380 format(ACTUAL_NOT_ORDERED_COLLECTION, dualValue.actual.getClass().getCanonicalName()));381 return;382 }383 Collection<?> actualCollection = (Collection<?>) dualValue.actual;384 Collection<?> expectedCollection = (Collection<?>) dualValue.expected;385 if (actualCollection.size() != expectedCollection.size()) {386 comparisonState.addDifference(dualValue, format(DIFFERENT_SIZE_ERROR, "collections", actualCollection.size(),387 expectedCollection.size()));388 // no need to inspect elements, arrays are not equal as they don't have the same size389 return;390 }391 // register pair of elements with same index for later comparison as we compare elements in order392 Iterator<?> expectedIterator = expectedCollection.iterator();393 int i = 0;394 for (Object element : actualCollection) {395 FieldLocation elementFielLocation = dualValue.fieldLocation.field(format("[%d]", i));396 DualValue elementDualValue = new DualValue(elementFielLocation, element, expectedIterator.next());397 comparisonState.registerForComparison(elementDualValue);398 i++;399 }400 }401 private static String differentTypeErrorMessage(DualValue dualValue, String actualTypeDescription) {402 return format(DIFFERENT_ACTUAL_AND_EXPECTED_FIELD_TYPES,403 actualTypeDescription, dualValue.actual.getClass().getCanonicalName());404 }405 private static void compareUnorderedIterables(DualValue dualValue, ComparisonState comparisonState) {406 if (!dualValue.isActualFieldAnIterable()) {407 // at the moment we only compare iterable with iterables (but we might allow arrays too)408 comparisonState.addDifference(dualValue, differentTypeErrorMessage(dualValue, "an iterable"));409 return;410 }411 Iterable<?> actual = (Iterable<?>) dualValue.actual;412 Iterable<?> expected = (Iterable<?>) dualValue.expected;413 int actualSize = sizeOf(actual);414 int expectedSize = sizeOf(expected);415 if (actualSize != expectedSize) {416 comparisonState.addDifference(dualValue, format(DIFFERENT_SIZE_ERROR, "collections", actualSize, expectedSize));417 // no need to inspect elements, iterables are not equal as they don't have the same size418 return;419 }420 // copy actual as we will remove elements found in expected421 Collection<?> actualCopy = new LinkedList<>(toCollection(actual));422 List<Object> expectedElementsNotFound = list();423 for (Object expectedElement : expected) {424 boolean expectedElementMatched = false;425 // compare recursively expectedElement to all remaining actual elements426 Iterator<?> actualIterator = actualCopy.iterator();427 while (actualIterator.hasNext()) {428 Object actualElement = actualIterator.next();429 // we need to get the currently visited dual values otherwise a cycle would cause an infinite recursion.430 List<ComparisonDifference> differences = determineDifferences(actualElement, expectedElement, dualValue.fieldLocation,431 comparisonState.visitedDualValues,432 comparisonState.recursiveComparisonConfiguration);433 if (differences.isEmpty()) {434 // found an element in actual matching expectedElement, remove it as it can't be used to match other expected elements435 actualIterator.remove();436 expectedElementMatched = true;437 // jump to next actual element check438 break;439 }440 }441 if (!expectedElementMatched) {442 expectedElementsNotFound.add(expectedElement);443 }444 }445 if (!expectedElementsNotFound.isEmpty()) {446 String unmatched = format("The following expected elements were not matched in the actual %s:%n %s",447 actual.getClass().getSimpleName(), expectedElementsNotFound);448 comparisonState.addDifference(dualValue, unmatched);449 // TODO could improve the error by listing the actual elements not in expected but that would need450 // another double loop inverting actual and expected to find the actual elements not matched in expected451 }452 }453 // TODO replace by ordered map454 private static <K, V> void compareSortedMap(DualValue dualValue, ComparisonState comparisonState) {455 if (!dualValue.isActualFieldASortedMap()) {456 // at the moment we only compare iterable with iterables (but we might allow arrays too)457 comparisonState.addDifference(dualValue, differentTypeErrorMessage(dualValue, "a sorted map"));458 return;459 }460 Map<?, ?> actualMap = (Map<?, ?>) dualValue.actual;461 @SuppressWarnings("unchecked")462 Map<K, V> expectedMap = (Map<K, V>) dualValue.expected;463 if (actualMap.size() != expectedMap.size()) {464 comparisonState.addDifference(dualValue, format(DIFFERENT_SIZE_ERROR, "sorted maps", actualMap.size(), expectedMap.size()));465 // no need to inspect entries, maps are not equal as they don't have the same size466 return;467 }468 Iterator<Map.Entry<K, V>> expectedMapEntries = expectedMap.entrySet().iterator();469 for (Map.Entry<?, ?> actualEntry : actualMap.entrySet()) {470 Map.Entry<?, ?> expectedEntry = expectedMapEntries.next();471 // check keys are matched before comparing values as keys represents a field472 if (!java.util.Objects.equals(actualEntry.getKey(), expectedEntry.getKey())) {473 // report a missing key/field.474 comparisonState.addKeyDifference(dualValue, actualEntry.getKey(), expectedEntry.getKey());475 } else {476 // as the key/field match we can simply compare field/key values477 FieldLocation keyFieldLocation = keyFieldLocation(dualValue.fieldLocation, actualEntry.getKey());478 comparisonState.registerForComparison(new DualValue(keyFieldLocation, actualEntry.getValue(), expectedEntry.getValue()));479 }480 }481 }482 private static void compareUnorderedMap(DualValue dualValue, ComparisonState comparisonState) {483 if (!dualValue.isActualFieldAMap()) {484 comparisonState.addDifference(dualValue, differentTypeErrorMessage(dualValue, "a map"));485 return;486 }487 Map<?, ?> actualMap = (Map<?, ?>) dualValue.actual;488 Map<?, ?> expectedMap = (Map<?, ?>) dualValue.expected;489 if (actualMap.size() != expectedMap.size()) {490 comparisonState.addDifference(dualValue, format(DIFFERENT_SIZE_ERROR, "maps", actualMap.size(), expectedMap.size()));491 // no need to inspect entries, maps are not equal as they don't have the same size492 return;493 }494 // actual and expected maps same size but do they have the same keys?495 Set<?> expectedKeysNotFound = new LinkedHashSet<>(expectedMap.keySet());496 expectedKeysNotFound.removeAll(actualMap.keySet());497 if (!expectedKeysNotFound.isEmpty()) {498 comparisonState.addDifference(dualValue, format("The following keys were not found in the actual map value:%n %s",499 expectedKeysNotFound));500 return;501 }502 // actual and expected maps have the same keys, we need now to compare their values503 for (Object key : expectedMap.keySet()) {504 FieldLocation keyFieldLocation = keyFieldLocation(dualValue.fieldLocation, key);505 comparisonState.registerForComparison(new DualValue(keyFieldLocation, actualMap.get(key), expectedMap.get(key)));506 }507 }508 private static FieldLocation keyFieldLocation(FieldLocation parentFieldLocation, Object key) {509 return key == null ? parentFieldLocation : parentFieldLocation.field(key.toString());510 }511 private static void compareOptional(DualValue dualValue, ComparisonState comparisonState) {512 if (!dualValue.isActualFieldAnOptional()) {513 comparisonState.addDifference(dualValue, differentTypeErrorMessage(dualValue, "an Optional"));514 return;515 }516 Optional<?> actual = (Optional<?>) dualValue.actual;517 Optional<?> expected = (Optional<?>) dualValue.expected;518 if (actual.isPresent() != expected.isPresent()) {519 comparisonState.addDifference(dualValue);520 return;521 }522 // either both are empty or present523 if (!actual.isPresent()) return; // both optional are empty => end of the comparison524 // both are present, we have to compare their values recursively525 Object value1 = actual.get();526 Object value2 = expected.get();527 // we add VALUE_FIELD_NAME to the path since we register Optional.value fields.528 comparisonState.registerForComparison(new DualValue(dualValue.fieldLocation.field(VALUE_FIELD_NAME), value1, value2));529 }530 private static void compareAtomicBoolean(DualValue dualValue, ComparisonState comparisonState) {531 if (!dualValue.isActualFieldAnAtomicBoolean()) {532 comparisonState.addDifference(dualValue, differentTypeErrorMessage(dualValue, "an AtomicBoolean"));533 return;534 }535 AtomicBoolean actual = (AtomicBoolean) dualValue.actual;536 AtomicBoolean expected = (AtomicBoolean) dualValue.expected;537 Object value1 = actual.get();538 Object value2 = expected.get();539 // we add VALUE_FIELD_NAME to the path since we register AtomicBoolean.value fields.540 comparisonState.registerForComparison(new DualValue(dualValue.fieldLocation.field(VALUE_FIELD_NAME), value1, value2));541 }542 private static void compareAtomicInteger(DualValue dualValue, ComparisonState comparisonState) {543 if (!dualValue.isActualFieldAnAtomicInteger()) {544 comparisonState.addDifference(dualValue, differentTypeErrorMessage(dualValue, "an AtomicInteger"));545 return;546 }547 AtomicInteger actual = (AtomicInteger) dualValue.actual;548 AtomicInteger expected = (AtomicInteger) dualValue.expected;549 Object value1 = actual.get();550 Object value2 = expected.get();551 // we add VALUE_FIELD_NAME to the path since we register AtomicInteger.value fields.552 comparisonState.registerForComparison(new DualValue(dualValue.fieldLocation.field(VALUE_FIELD_NAME), value1, value2));553 }554 private static void compareAtomicIntegerArray(DualValue dualValue, ComparisonState comparisonState) {555 if (!dualValue.isActualFieldAnAtomicIntegerArray()) {556 comparisonState.addDifference(dualValue, differentTypeErrorMessage(dualValue, "an AtomicIntegerArray"));557 return;558 }559 AtomicIntegerArray actual = (AtomicIntegerArray) dualValue.actual;560 AtomicIntegerArray expected = (AtomicIntegerArray) dualValue.expected;561 // both values in dualValue are arrays562 int actualArrayLength = actual.length();563 int expectedArrayLength = expected.length();564 if (actualArrayLength != expectedArrayLength) {565 comparisonState.addDifference(dualValue,566 format(DIFFERENT_SIZE_ERROR, "AtomicIntegerArrays", actualArrayLength, expectedArrayLength));567 // no need to inspect elements, arrays are not equal as they don't have the same size568 return;569 }570 // register each pair of actual/expected elements for recursive comparison571 FieldLocation arrayFieldLocation = dualValue.fieldLocation;572 for (int i = 0; i < actualArrayLength; i++) {573 Object actualElement = actual.get(i);574 Object expectedElement = expected.get(i);575 FieldLocation elementFieldLocation = arrayFieldLocation.field(format(ARRAY_FIELD_NAME + "[%d]", i));576 comparisonState.registerForComparison(new DualValue(elementFieldLocation, actualElement, expectedElement));577 }578 }579 private static void compareAtomicLong(DualValue dualValue, ComparisonState comparisonState) {580 if (!dualValue.isActualFieldAnAtomicLong()) {581 comparisonState.addDifference(dualValue, differentTypeErrorMessage(dualValue, "an AtomicLong"));582 return;583 }584 AtomicLong actual = (AtomicLong) dualValue.actual;585 AtomicLong expected = (AtomicLong) dualValue.expected;586 Object value1 = actual.get();587 Object value2 = expected.get();588 // we add VALUE_FIELD_NAME to the path since we register AtomicLong.value fields.589 comparisonState.registerForComparison(new DualValue(dualValue.fieldLocation.field(VALUE_FIELD_NAME), value1, value2));590 }591 private static void compareAtomicLongArray(DualValue dualValue, ComparisonState comparisonState) {592 if (!dualValue.isActualFieldAnAtomicLongArray()) {593 comparisonState.addDifference(dualValue, differentTypeErrorMessage(dualValue, "an AtomicLongArray"));594 return;595 }596 AtomicLongArray actual = (AtomicLongArray) dualValue.actual;597 AtomicLongArray expected = (AtomicLongArray) dualValue.expected;598 // both values in dualValue are arrays599 int actualArrayLength = actual.length();600 int expectedArrayLength = expected.length();601 if (actualArrayLength != expectedArrayLength) {602 comparisonState.addDifference(dualValue,603 format(DIFFERENT_SIZE_ERROR, "AtomicLongArrays", actualArrayLength, expectedArrayLength));604 // no need to inspect elements, arrays are not equal as they don't have the same size605 return;606 }607 // register each pair of actual/expected elements for recursive comparison608 FieldLocation arrayFieldLocation = dualValue.fieldLocation;609 for (int i = 0; i < actualArrayLength; i++) {610 Object actualElement = actual.get(i);611 Object expectedElement = expected.get(i);612 FieldLocation elementFieldLocation = arrayFieldLocation.field(format(ARRAY_FIELD_NAME + "[%d]", i));613 comparisonState.registerForComparison(new DualValue(elementFieldLocation, actualElement, expectedElement));614 }615 }616 private static void compareAtomicReferenceArray(DualValue dualValue, ComparisonState comparisonState) {617 if (!dualValue.isActualFieldAnAtomicReferenceArray()) {618 comparisonState.addDifference(dualValue, differentTypeErrorMessage(dualValue, "an AtomicReferenceArray"));619 return;620 }621 AtomicReferenceArray<?> actual = (AtomicReferenceArray<?>) dualValue.actual;622 AtomicReferenceArray<?> expected = (AtomicReferenceArray<?>) dualValue.expected;623 // both values in dualValue are arrays624 int actualArrayLength = actual.length();625 int expectedArrayLength = expected.length();626 if (actualArrayLength != expectedArrayLength) {627 comparisonState.addDifference(dualValue,628 format(DIFFERENT_SIZE_ERROR, "AtomicReferenceArrays", actualArrayLength,629 expectedArrayLength));630 // no need to inspect elements, arrays are not equal as they don't have the same size631 return;632 }633 // register each pair of actual/expected elements for recursive comparison634 FieldLocation arrayFieldLocation = dualValue.fieldLocation;635 for (int i = 0; i < actualArrayLength; i++) {636 Object actualElement = actual.get(i);637 Object expectedElement = expected.get(i);638 FieldLocation elementFieldLocation = arrayFieldLocation.field(format(ARRAY_FIELD_NAME + "[%d]", i));639 comparisonState.registerForComparison(new DualValue(elementFieldLocation, actualElement, expectedElement));640 }641 }642 private static void compareAtomicReference(DualValue dualValue, ComparisonState comparisonState) {643 if (!dualValue.isActualFieldAnAtomicReference()) {644 comparisonState.addDifference(dualValue, differentTypeErrorMessage(dualValue, "an AtomicReference"));645 return;646 }647 AtomicReference<?> actual = (AtomicReference<?>) dualValue.actual;648 AtomicReference<?> expected = (AtomicReference<?>) dualValue.expected;649 Object value1 = actual.get();650 Object value2 = expected.get();651 // we add VALUE_FIELD_NAME to the path since we register AtomicReference.value fields.652 comparisonState.registerForComparison(new DualValue(dualValue.fieldLocation.field(VALUE_FIELD_NAME), value1, value2));653 }654 /**655 * Determine if the passed in class has a non-Object.equals() method. This656 * method caches its results in static ConcurrentHashMap to benefit657 * execution performance.658 *659 * @param c Class to check.660 * @return true, if the passed in Class has a .equals() method somewhere661 * between itself and just below Object in it's inheritance.662 */663 static boolean hasOverriddenEquals(Class<?> c) {664 if (customEquals.containsKey(c)) {665 return customEquals.get(c);666 }667 Class<?> origClass = c;668 while (!Object.class.equals(c)) {669 try {670 c.getDeclaredMethod("equals", Object.class);671 customEquals.put(origClass, true);672 return true;673 } catch (Exception ignored) {}674 c = c.getSuperclass();675 }676 customEquals.put(origClass, false);677 return false;678 }679 @SuppressWarnings({ "rawtypes", "unchecked" })680 private static boolean areDualValueEqual(DualValue dualValue,681 RecursiveComparisonConfiguration recursiveComparisonConfiguration) {682 final String fieldName = dualValue.getConcatenatedPath();683 final Object actualFieldValue = dualValue.actual;684 final Object expectedFieldValue = dualValue.expected;685 // check field comparators as they take precedence over type comparators686 Comparator fieldComparator = recursiveComparisonConfiguration.getComparatorForField(fieldName);687 if (fieldComparator != null) return areEqualUsingComparator(actualFieldValue, expectedFieldValue, fieldComparator);688 // check if a type comparators exist for the field type689 Class fieldType = actualFieldValue != null ? actualFieldValue.getClass() : expectedFieldValue.getClass();690 Comparator typeComparator = recursiveComparisonConfiguration.getComparatorForType(fieldType);691 if (typeComparator != null) return areEqualUsingComparator(actualFieldValue, expectedFieldValue, typeComparator);692 // default comparison using equals693 return deepEquals(actualFieldValue, expectedFieldValue);694 }695 private static boolean areEqualUsingComparator(final Object actual, final Object expected, Comparator<Object> comparator) {696 try {...

Full Screen

Full Screen

Source:DualValue_constructor_Test.java Github

copy

Full Screen

...22 void should_build_field_path() {23 // GIVEN24 DualValue dualValue = new DualValue(list("foo"), "bar", "", "");25 // WHEN26 String expectedFieldConcatenatedPath = dualValue.getConcatenatedPath();27 List<String> expectedFieldPath = dualValue.getPath();28 // THEN29 assertThat(expectedFieldConcatenatedPath).isEqualTo("foo.bar");30 assertThat(expectedFieldPath).isEqualTo(list("foo", "bar"));31 }32}...

Full Screen

Full Screen

getConcatenatedPath

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.recursive.comparison.DualValue;2import org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration;3import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifference;4import java.util.List;5public class DualValueDemo {6 public static void main(String[] args) {7 RecursiveComparisonConfiguration recursiveComparisonConfiguration = new RecursiveComparisonConfiguration();8 DualValue dualValue = new DualValue("a", "b", recursiveComparisonConfiguration);9 List<RecursiveComparisonDifference> recursiveComparisonDifferences = dualValue.getRecursiveComparisonDifferences();10 for (RecursiveComparisonDifference recursiveComparisonDifference : recursiveComparisonDifferences) {11 System.out.println(recursiveComparisonDifference.getConcatenatedPath());12 }13 }14}15import org.assertj.core.api.recursive.comparison.DualValue;16import org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration;17import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifference;18import java.util.List;19public class RecursiveComparisonDifferenceDemo {20 public static void main(String[] args) {21 RecursiveComparisonConfiguration recursiveComparisonConfiguration = new RecursiveComparisonConfiguration();22 DualValue dualValue = new DualValue("a", "b", recursiveComparisonConfiguration);23 List<RecursiveComparisonDifference> recursiveComparisonDifferences = dualValue.getRecursiveComparisonDifferences();24 for (RecursiveComparisonDifference recursiveComparisonDifference : recursiveComparisonDifferences) {25 System.out.println(recursiveComparisonDifference.getConcatenatedPath());26 }27 }28}29import org.assertj.core.api.recursive.comparison.DualValue;30import org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration;31import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifference;32import java.util.List;33public class FieldLocationDemo {34 public static void main(String[] args) {35 RecursiveComparisonConfiguration recursiveComparisonConfiguration = new RecursiveComparisonConfiguration();36 DualValue dualValue = new DualValue("a", "b", recursiveComparisonConfiguration);37 List<RecursiveComparisonDifference> recursiveComparisonDifferences = dualValue.getRecursiveComparisonDifferences();38 for (RecursiveComparisonDifference recursiveComparisonDifference : recursiveComparisonD

Full Screen

Full Screen

getConcatenatedPath

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.recursive.comparison.DualValue;2import org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration;3import org.assertj.core.util.introspection.IntrospectionError;4public class DualValueTest {5 public static void main(String[] args) {6 DualValue dualValue = new DualValue("a", "b", new RecursiveComparisonConfiguration());7 System.out.println(dualValue.getConcatenatedPath());8 }9}10import org.assertj.core.api.recursive.comparison.DualValue;11import org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration;12import org.assertj.core.util.introspection.IntrospectionError;13public class DualValueTest {14 public static void main(String[] args) {15 DualValue dualValue = new DualValue("a", "b", new RecursiveComparisonConfiguration());16 dualValue.getPath();17 System.out.println(dualValue.getConcatenatedPath());18 }19}

Full Screen

Full Screen

getConcatenatedPath

Using AI Code Generation

copy

Full Screen

1package org.assertj.core.api.recursive.comparison;2import org.assertj.core.api.recursive.comparison.DualValue;3public class RecursiveComparisonAssert_getConcatenatedPath_1 {4 public static void main(String[] args) {5 DualValue dualValue = new DualValue("actual", "expected");6 String concatenatedPath = dualValue.getConcatenatedPath();7 System.out.println(concatenatedPath);8 }9}

Full Screen

Full Screen

getConcatenatedPath

Using AI Code Generation

copy

Full Screen

1package org.assertj.core.api.recursive.comparison;2import java.util.Arrays;3import java.util.List;4public class GetConcatenatedPath {5 public static void main(String[] args) {6 DualValue dualValue = new DualValue("1", "2");7 List<String> path = Arrays.asList("a", "b", "c");8 System.out.println(dualValue.getConcatenatedPath(path));9 }10}

Full Screen

Full Screen

getConcatenatedPath

Using AI Code Generation

copy

Full Screen

1package org.assertj.core.api.recursive.comparison;2import java.util.ArrayList;3import java.util.List;4import org.assertj.core.api.recursive.comparison.DualValue;5public class getConcatenatedPath {6 public static void main(String[] args) {7 List<String> path = new ArrayList<String>();8 path.add("a");9 path.add("b");10 DualValue dualValue = new DualValue("a", "b", path);11 System.out.println(dualValue.getConcatenatedPath());12 }13}14package org.assertj.core.api.recursive.comparison;15import java.util.ArrayList;16import java.util.List;17import org.assertj.core.api.recursive.comparison.DualValue;18public class getConcatenatedPath {19 public static void main(String[] args) {20 List<String> path = new ArrayList<String>();21 DualValue dualValue = new DualValue("a", "b", path);22 System.out.println(dualValue.getConcatenatedPath());23 }24}25package org.assertj.core.api.recursive.comparison;26import java.util.ArrayList;27import java.util.List;28import org.assertj.core.api.recursive.comparison.DualValue;29public class getConcatenatedPath {30 public static void main(String[] args) {31 List<String> path = new ArrayList<String>();32 path.add("a");33 path.add("b");34 path.add("c");35 DualValue dualValue = new DualValue("a", "b", path);36 System.out.println(dualValue.getConcatenatedPath());37 }38}39package org.assertj.core.api.recursive.comparison;40import java.util.ArrayList;41import java.util.List;42import org.assertj.core.api.recursive.comparison.DualValue;43public class getConcatenatedPath {44 public static void main(String[] args) {45 List<String> path = new ArrayList<String>();46 path.add("a");47 path.add("b");48 path.add("c");

Full Screen

Full Screen

getConcatenatedPath

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.recursive.comparison.DualValue;2class A {3 String a;4 String b;5 public A(String a, String b) {6 this.a = a;7 this.b = b;8 }9}10public class 1 {11 public static void main(String[] args) {12 A a = new A("a", "b");13 A b = new A("a", "b");14 DualValue dualValue = new DualValue(a, b);15 System.out.println(dualValue.getConcatenatedPath());16 }17}

Full Screen

Full Screen

getConcatenatedPath

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.recursive.comparison.DualValue;2import java.util.ArrayList;3import java.util.List;4import java.util.stream.Collectors;5import java.util.stream.Stream;6public class ConcatenatedPath {7 public static void main(String[] args) {8 List<String> list = new ArrayList<>();9 list.add("a");10 list.add("b");11 list.add("c");12 Stream<String> stream = list.stream();13 List<String> list1 = new ArrayList<>();14 list1.add("x");15 list1.add("y");16 list1.add("z");17 Stream<String> stream1 = list1.stream();18 DualValue dualValue = new DualValue(stream, stream1);19 System.out.println(dualValue.getConcatenatedPath());

Full Screen

Full Screen

getConcatenatedPath

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.recursive.comparison.DualValue;2import org.assertj.core.api.recursive.comparison.DualValue;3import org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration;4import org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration;5import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifference;6import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifference;7import org.junit.jupiter.api.Test;8import org.junit.jupiter.api.Test;9import static org.assertj.core.api.Assertions.*;10import static org.assertj.core.api.Assertions.*;11import static org.assertj.core.api.Assertions.assertThat;12import static org.asser

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.

Run Assertj automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful