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

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

Source:DeepDifference.java Github

copy

Full Screen

...222 continue;223 }224 // Handle unordered Collection.225 if (key1 instanceof Collection) {226 if (!compareUnorderedCollection((Collection<?>) key1, (Collection<?>) key2, currentPath, toCompare,227 visited, comparatorByPropertyOrField, comparatorByType)) {228 differences.add(new Difference(currentPath, key1, key2));229 continue;230 }231 continue;232 }233 // Compare two SortedMaps. This takes advantage of the fact that these234 // Maps can be compared in O(N) time due to their ordering.235 if (key1 instanceof SortedMap) {236 if (!compareSortedMap((SortedMap<?, ?>) key1, (SortedMap<?, ?>) key2, currentPath, toCompare, visited)) {237 differences.add(new Difference(currentPath, key1, key2));238 continue;239 }240 continue;241 }242 // Compare two Unordered Maps. This is a slightly more expensive comparison because243 // order cannot be assumed, therefore a temporary Map must be created, however the244 // comparison still runs in O(N) time.245 if (key1 instanceof Map) {246 if (!compareUnorderedMap((Map<?, ?>) key1, (Map<?, ?>) key2, currentPath, toCompare, visited)) {247 differences.add(new Difference(currentPath, key1, key2));248 continue;249 }250 continue;251 }252 if (hasCustomEquals(key1.getClass())) {253 if (!key1.equals(key2)) {254 differences.add(new Difference(currentPath, key1, key2));255 continue;256 }257 continue;258 }259 Set<String> key1FieldsNames = getFieldsNames(getDeclaredFieldsIncludingInherited(key1.getClass()));260 Set<String> key2FieldsNames = getFieldsNames(getDeclaredFieldsIncludingInherited(key2.getClass()));261 if (!key2FieldsNames.containsAll(key1FieldsNames)) {262 differences.add(new Difference(currentPath, key1, key2));263 } else {264 for (String fieldName : key1FieldsNames) {265 List<String> path = new ArrayList<>(currentPath);266 path.add(fieldName);267 DualKey dk = new DualKey(path,268 COMPARISON.getSimpleValue(fieldName, key1),269 COMPARISON.getSimpleValue(fieldName, key2));270 if (!visited.contains(dk)) {271 toCompare.addFirst(dk);272 }273 }274 }275 }276 return differences;277 }278 private static boolean hasCustomComparator(DualKey dualKey, Map<String, Comparator<?>> comparatorByPropertyOrField,279 TypeComparators comparatorByType) {280 String fieldName = dualKey.getConcatenatedPath();281 if (comparatorByPropertyOrField.containsKey(fieldName)) return true;282 // we know that dualKey.key1 != dualKey.key2 at this point, so one the key is not null283 Class<?> keyType = dualKey.key1 != null ? dualKey.key1.getClass() : dualKey.key2.getClass();284 return comparatorByType.get(keyType) != null;285 }286 private static Deque<DualKey> initStack(Object a, Object b, List<String> parentPath,287 Map<String, Comparator<?>> comparatorByPropertyOrField,288 TypeComparators comparatorByType) {289 Deque<DualKey> stack = new LinkedList<>();290 boolean isRootObject = parentPath == null;291 List<String> currentPath = isRootObject ? new ArrayList<String>() : parentPath;292 DualKey basicDualKey = new DualKey(currentPath, a, b);293 if (a != null && b != null && !isContainerType(a) && !isContainerType(b)294 && (isRootObject || !hasCustomComparator(basicDualKey, comparatorByPropertyOrField, comparatorByType))) {295 // disregard the equals method and start comparing fields296 Set<String> aFieldsNames = getFieldsNames(getDeclaredFieldsIncludingInherited(a.getClass()));297 if (!aFieldsNames.isEmpty()) {298 Set<String> bFieldsNames = getFieldsNames(getDeclaredFieldsIncludingInherited(b.getClass()));299 if (!bFieldsNames.containsAll(aFieldsNames)) {300 stack.addFirst(basicDualKey);301 } else {302 for (String fieldName : aFieldsNames) {303 List<String> fieldPath = new ArrayList<>(currentPath);304 fieldPath.add(fieldName);305 DualKey dk = new DualKey(fieldPath,306 COMPARISON.getSimpleValue(fieldName, a),307 COMPARISON.getSimpleValue(fieldName, b));308 stack.addFirst(dk);309 }310 }311 } else {312 stack.addFirst(basicDualKey);313 }314 } else {315 stack.addFirst(basicDualKey);316 }317 return stack;318 }319 private static Set<String> getFieldsNames(Collection<Field> fields) {320 Set<String> fieldNames = new LinkedHashSet<>();321 for (Field field : fields) {322 fieldNames.add(field.getName());323 }324 return fieldNames;325 }326 private static boolean isContainerType(Object o) {327 return o instanceof Collection || o instanceof Map;328 }329 /**330 * Deeply compare to Arrays []. Both arrays must be of the same type, same331 * length, and all elements within the arrays must be deeply equal in order332 * to return true.333 * 334 * @param array1 [] type (Object[], String[], etc.)335 * @param array2 [] type (Object[], String[], etc.)336 * @param path the path to the arrays to compare337 * @param toCompare add items to compare to the Stack (Stack versus recursion)338 * @param visited Set of objects already compared (prevents cycles)339 * @return true if the two arrays are the same length and contain deeply340 * equivalent items.341 */342 private static boolean compareArrays(Object array1, Object array2, List<String> path, Deque<DualKey> toCompare,343 Set<DualKey> visited) {344 int len = Array.getLength(array1);345 if (len != Array.getLength(array2)) {346 return false;347 }348 for (int i = 0; i < len; i++) {349 DualKey dk = new DualKey(path, Array.get(array1, i), Array.get(array2, i));350 if (!visited.contains(dk)) {351 toCompare.addFirst(dk);352 }353 }354 return true;355 }356 /**357 * Deeply compare two Collections that must be same length and in same358 * order.359 * 360 * @param col1 First collection of items to compare361 * @param col2 Second collection of items to compare362 * @param path The path to the collections363 * @param toCompare add items to compare to the Stack (Stack versus recursion)364 * @param visited365 * Set of objects already compared (prevents cycles) value of366 * 'true' indicates that the Collections may be equal, and the367 * sets items will be added to the Stack for further comparison.368 */369 private static <K, V> boolean compareOrderedCollection(Collection<K> col1, Collection<V> col2,370 List<String> path, Deque<DualKey> toCompare,371 Set<DualKey> visited) {372 if (col1.size() != col2.size()) return false;373 Iterator<V> i2 = col2.iterator();374 for (K k : col1) {375 DualKey dk = new DualKey(path, k, i2.next());376 if (!visited.contains(dk)) toCompare.addFirst(dk);377 }378 return true;379 }380 /**381 * It places one collection into a temporary Map by deepHashCode(), so that it382 * can walk the other collection and look for each item in the map, which383 * runs in O(N) time, rather than an O(N^2) lookup that would occur if each384 * item from collection one was scanned for in collection two.385 * 386 * @param col1 First collection of items to compare387 * @param col2 Second collection of items to compare388 * @param path the path to the collections to compare389 * @param toCompare add items to compare to the Stack (Stack versus recursion)390 * @param visited Set containing items that have already been compared, so as to391 * prevent cycles.392 * @return boolean false if the Collections are for certain not equals. A393 * value of 'true' indicates that the Collections may be equal, and394 * the sets items will be added to the Stack for further comparison.395 */396 private static <K, V> boolean compareUnorderedCollectionByHashCodes(Collection<K> col1, Collection<V> col2,397 List<String> path, Deque<DualKey> toCompare,398 Set<DualKey> visited) {399 Map<Integer, Object> fastLookup = new HashMap<>();400 for (Object o : col2) {401 fastLookup.put(deepHashCode(o), o);402 }403 for (Object o : col1) {404 Object other = fastLookup.get(deepHashCode(o));405 if (other == null) {406 // Item not even found in other Collection, no need to continue.407 return false;408 }409 DualKey dk = new DualKey(path, o, other);410 if (!visited.contains(dk)) {411 toCompare.addFirst(dk);412 }413 }414 return true;415 }416 /**417 * Deeply compares two collections referenced by dualKey. This method attempts418 * to quickly determine inequality by length, then if lengths match, in case of419 * collection type is Set and there are passed no custom comparators, there is used420 * comparison on hashcodes basis, otherwise each element from one collection is checked421 * for existence in another one using 'deep' comparison.422 */423 private static <K, V> boolean compareUnorderedCollection(Collection<K> col1, Collection<V> col2,424 List<String> path, Deque<DualKey> toCompare,425 Set<DualKey> visited,426 Map<String, Comparator<?>> comparatorByPropertyOrField,427 TypeComparators comparatorByType) {428 if (col1.size() != col2.size()) return false;429 boolean noCustomComparators = comparatorByPropertyOrField.isEmpty() && comparatorByType.isEmpty();430 if (noCustomComparators && col1 instanceof Set) {431 // this comparison is used for performance optimization reasons432 return compareUnorderedCollectionByHashCodes(col1, col2, path, toCompare, visited);433 }434 Collection<V> col2Copy = new LinkedList<>(col2);435 for (Object o1 : col1) {436 Iterator<V> iterator = col2Copy.iterator();437 while (iterator.hasNext()) {438 Object o2 = iterator.next();439 if (determineDifferences(o1, o2, path, comparatorByPropertyOrField, comparatorByType).isEmpty()) {440 iterator.remove();441 break;442 }443 }444 }445 return col2Copy.isEmpty();446 }...

Full Screen

Full Screen

compareUnorderedCollection

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.Assertions;2import org.assertj.core.internal.DeepDifference;3import java.util.ArrayList;4import java.util.Arrays;5import java.util.List;6public class DeepDifferenceTest {7 public static void main(String[] args) {8 List<String> a = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"));9 List<String> b = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"));10 DeepDifference deepDifference = new DeepDifference();11 Assertions.assertThat(deepDifference.compareUnorderedCollection(a, b)).isNull();12 }13}14import org.assertj.core.api.Assertions;15import org.assertj.core.internal.DeepDifference;16import java.util.ArrayList;17import java.util.Arrays;18import java.util.List;19public class DeepDifferenceTest {20 public static void main(String[] args) {21 List<String> a = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"));22 List<String> b = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "

Full Screen

Full Screen

compareUnorderedCollection

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.internal.DeepDifference;2import org.assertj.core.internal.DeepDifference.Difference;3import java.util.ArrayList;4import java.util.List;5public class DeepDifferenceExample {6 public static void main(String[] args) {7 List<String> actual = new ArrayList<>();8 actual.add("one");9 actual.add("two");10 actual.add("three");11 actual.add("four");12 List<String> expected = new ArrayList<>();13 expected.add("one");14 expected.add("two");15 expected.add("three");16 expected.add("four");17 DeepDifference deepDifference = new DeepDifference();18 Difference difference = deepDifference.compareUnorderedCollection(expected, actual);19 System.out.println("difference: " + difference);20 }21}22import org.assertj.core.internal.DeepDifference;23import org.assertj.core.internal.DeepDifference.Difference;24import java.util.ArrayList;25import java.util.List;26public class DeepDifferenceExample {27 public static void main(String[] args) {28 List<String> actual = new ArrayList<>();29 actual.add("one");30 actual.add("two");31 actual.add("three");32 actual.add("four");33 List<String> expected = new ArrayList<>();34 expected.add("one");35 expected.add("two");36 expected.add("three");37 expected.add("four");38 DeepDifference deepDifference = new DeepDifference();39 Difference difference = deepDifference.compareOrderedCollection(expected, actual);40 System.out.println("difference: " + difference);41 }42}43import org.assertj.core.internal.DeepDifference;44import org.assertj.core.internal.DeepDifference.Difference;45import java.util.ArrayList;46import java.util.List;47public class DeepDifferenceExample {48 public static void main(String[] args) {49 List<String> actual = new ArrayList<>();50 actual.add("one");51 actual.add("two");52 actual.add("three");53 actual.add("four");54 List<String> expected = new ArrayList<>();55 expected.add("four");56 expected.add("two");57 expected.add("three");

Full Screen

Full Screen

compareUnorderedCollection

Using AI Code Generation

copy

Full Screen

1public class CompareUnorderedCollection {2 public static void main(String[] args) {3 List<Integer> list1 = new ArrayList<>();4 list1.add(1);5 list1.add(2);6 list1.add(3);7 list1.add(4);8 list1.add(5);9 list1.add(6);10 List<Integer> list2 = new ArrayList<>();11 list2.add(1);12 list2.add(2);13 list2.add(3);14 list2.add(4);15 list2.add(5);16 list2.add(6);17 List<Integer> list3 = new ArrayList<>();18 list3.add(1);19 list3.add(2);20 list3.add(3);21 list3.add(4);22 list3.add(5);23 list3.add(7);24 List<Integer> list4 = new ArrayList<>();25 list4.add(1);26 list4.add(2);27 list4.add(3);28 list4.add(4);29 list4.add(5);30 System.out.println("list1 and list2 are equal: " + compareUnorderedCollection(list1, list2));31 System.out.println("list1 and list3 are equal: " + compareUnorderedCollection(list1, list3));32 System.out.println("list1 and list4 are equal: " + compareUnorderedCollection(list1, list4));33 }34 private static boolean compareUnorderedCollection(List<Integer> list1, List<Integer> list2) {35 DeepDifference deepDifference = new DeepDifference(list1, list2);36 deepDifference.compareUnorderedCollection();37 return deepDifference.areEqual();38 }39}

Full Screen

Full Screen

compareUnorderedCollection

Using AI Code Generation

copy

Full Screen

1 def compareUnorderedCollection(Collection actual, Collection expected, ComparisonStrategy comparisonStrategy) {2 def deepDifference = new DeepDifference(actual, expected, comparisonStrategy)3 if (difference != null) {4 }5 }6 def "should not throw AssertionError when comparing unordered collections"() {7 def difference = compareUnorderedCollection(actual, expected, StandardComparisonStrategy.instance())8 }9 def "should throw AssertionError when comparing unordered collections"() {10 def difference = compareUnorderedCollection(actual, expected, StandardComparisonStrategy.instance())11 }12}13Your name to display (optional):14Your name to display (optional):

Full Screen

Full Screen

compareUnorderedCollection

Using AI Code Generation

copy

Full Screen

1package com.example;2import static org.assertj.core.api.Assertions.assertThat;3import static org.assertj.core.api.Assertions.assertThatThrownBy;4import static org.assertj.core.api.Assertions.fail;5import static org.assertj.core.internal.DeepDifference.compareUnorderedCollection;6import java.util.ArrayList;7import java.util.Arrays;8import java.util.List;9import org.assertj.core.internal.DeepDifference.Difference;10import org.junit.Test;11public class CollectionDifferenceTest {12 public void test() {13 List<String> list1 = new ArrayList<>(Arrays.asList("a", "b", "c"));14 List<String> list2 = new ArrayList<>(Arrays.asList("a", "b", "c", "d"));15 Difference diff = compareUnorderedCollection(list1, list2, "list1", "list2");16 if (diff != null) {17 System.out.println(diff);18 fail("Lists are not equal");19 }20 }21}

Full Screen

Full Screen

compareUnorderedCollection

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.internal.DeepDifference;2import org.assertj.core.internal.Difference;3import java.util.ArrayList;4import java.util.Arrays;5import java.util.List;6public class TestDeepDifference {7 public static void main(String[] args) {8 List list1 = new ArrayList();9 list1.add("one");10 list1.add("two");11 list1.add("three");12 list1.add("four");13 list1.add("five");14 list1.add("six");15 list1.add("seven");16 list1.add("eight");17 list1.add("nine");18 list1.add("ten");19 List list2 = new ArrayList();20 list2.add(new Object());

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