Best Assertj code snippet using org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.compareSortedMap
Source:RecursiveComparisonDifferenceCalculator.java
...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)...
compareSortedMap
Using AI Code Generation
1import static org.assertj.core.api.Assertions.assertThat;2import java.util.Arrays;3import java.util.HashMap;4import java.util.Map;5import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator;6import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculatorTest;7import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculatorTest.MapEntry;8import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculatorTest.MapKey;9import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculatorTest.MapValue;10import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculatorTest.Person;11import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculatorTest.PersonComparator;12import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculatorTest.PersonWithAddress;13import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculatorTest.PersonWithAddressComparator;14import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculatorTest.PersonWithAge;15import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculatorTest.PersonWithAgeComparator;16import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculatorTest.PersonWithAgeAndAddress;17import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculatorTest.PersonWithAgeAndAddressComparator;18import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculatorTest.PersonWithAgeAndAddressAndName;19import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculatorTest.PersonWithAgeAndAddressAndNameComparator;20import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculatorTest.PersonWithAgeAndName;21import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculatorTest.PersonWithAgeAndNameComparator;22import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculatorTest.PersonWithAgeAndNameComparator2;23import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculatorTest.PersonWithAgeAndNameComparator3;24import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculatorTest.PersonWithAgeAndNameComparator4;25import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculatorTest.PersonWithAgeAndNameComparator5;26import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculatorTest.PersonWithAgeAndNameComparator6;27import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculatorTest.PersonWithAgeAndNameComparator7;28import org.assertj.core.api
compareSortedMap
Using AI Code Generation
1package com.baeldung.assertj.recursive;2import static org.assertj.core.api.Assertions.assertThat;3import java.util.Map;4import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator;5import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifference;6import org.junit.Test;7public class RecursiveComparisonDifferenceCalculatorUnitTest {8 public void givenTwoMaps_whenCompareSortedMap_thenCorrect() {9 Map<String, String> map1 = Map.of("a", "1", "b", "2");10 Map<String, String> map2 = Map.of("b", "2", "a", "1");11 RecursiveComparisonDifference difference = RecursiveComparisonDifferenceCalculator.compareSortedMap(map1, map2);12 assertThat(difference).isNull();13 }14}15Actual :RecursiveComparisonDifference [path=, actualValue={a=1, b=2}, expectedValue={b=2, a=1}]
compareSortedMap
Using AI Code Generation
1import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator;2import java.util.SortedMap;3import java.util.TreeMap;4public class SortedMapCompare {5 public static void main(String[] args) {6 SortedMap<String, Integer> map1 = new TreeMap<>();7 map1.put("a", 1);8 map1.put("b", 2);9 map1.put("c", 3);10 SortedMap<String, Integer> map2 = new TreeMap<>();11 map2.put("a", 1);12 map2.put("b", 2);13 map2.put("c", 3);14 System.out.println(RecursiveComparisonDifferenceCalculator.compareSortedMap(map1, map2));15 }16}
compareSortedMap
Using AI Code Generation
1 def compareSortedMap = { actual, expected ->2 def differences = new RecursiveComparisonDifferenceCalculator().compareSortedMap(actual, expected)3 if (differences.isEmpty()) {4 }5 return differences[0].toString()6 }7 assertThat(actual).usingComparatorForType(compareSortedMap, Map).isEqualTo(expected)8 def compareSortedMap = { actual, expected ->9 def differences = new RecursiveComparisonDifferenceCalculator().compareSortedMap(actual, expected)10 if (differences.isEmpty()) {11 }12 return differences[0].toString()13 }14 assertThat(actual).usingComparatorForType(compareSortedMap, Map).isEqualTo(expected)15 <{a=1, b=2, c=3}>16 <{a=1, b=2, c=3}>17when recursively comparing field by field, but found the following difference(s):18 at org.assertj.core.api.AbstractAssert.isEqualTo(AbstractAssert.java:81)19 at org.assertj.core.api.AssertionsForClassTypes.isEqualTo(AssertionsForClassTypes.java:69)20 at org.assertj.core.api.Assertions.isEqualTo(Assertions.java:116)21 at org.assertj.core.api.Assertions.isEqualTo(Assertions.java:108)22 at org.assertj.core.api.Assertions.isEqualTo(Assertions.java:101)23 at org.assertj.core.api.Assertions.isEqualTo(Assertions.java:97)24 at org.assertj.core.api.Assertions.isEqualTo(Assertions.java:93)25 at org.assertj.core.api.Assertions.isEqualTo(Assertions.java:89)26 at org.assertj.core.api.Assertions.isEqualTo(Assertions.java:85)27 at org.assertj.core.api.Assertions.isEqualTo(Assertions.java:81)
compareSortedMap
Using AI Code Generation
1List<RecursiveComparisonDifference> differences = RecursiveComparisonDifferenceCalculator.compareSortedMap(expected, actual);2List<RecursiveComparisonDifference> differences = RecursiveComparisonDifferenceCalculator.compareSortedMap(expected, actual, comparisonStrategy);3List<RecursiveComparisonDifference> differences = RecursiveComparisonDifferenceCalculator.compareSortedMap(expected, actual, recursiveComparisonConfiguration);4List<RecursiveComparisonDifference> differences = RecursiveComparisonDifferenceCalculator.compareSortedMap(expected, actual, comparisonStrategy, recursiveComparisonConfiguration);5List<RecursiveComparisonDifference> differences = RecursiveComparisonDifferenceCalculator.compareSortedMap(expected, actual, ignoredFields);6List<RecursiveComparisonDifference> differences = RecursiveComparisonDifferenceCalculator.compareSortedMap(expected, actual, ignoredFields, recursiveComparisonConfiguration);7List<RecursiveComparisonDifference> differences = RecursiveComparisonDifferenceCalculator.compareSortedMap(expected, actual, ignoredFields, comparisonStrategy, recursiveComparisonConfiguration);8List<RecursiveComparisonDifference> differences = RecursiveComparisonDifferenceCalculator.compareSortedMap(expected, actual, recursiveComparisonConfiguration, ignoredFields);9List<RecursiveComparisonDifference> differences = RecursiveComparisonDifferenceCalculator.compareSortedMap(expected, actual, comparisonStrategy, recursiveComparisonConfiguration, ignoredFields);10List<RecursiveComparisonDifference> differences = RecursiveComparisonDifferenceCalculator.compareSortedMap(expected, actual, comparisonStrategy, ignoredFields, recursiveComparisonConfiguration);
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!