package test.asserttests; import org.testng.annotations.Test; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNotEquals; /** * Tests different equality cases for nested collections * and arrays. */ public class ArrayEqualityAssertTest { @Test public void arrayAssertEquals() { assertEquals(new int[]{ 42 }, new int[] { 42 }, "arrays of primitives are compared by value in assertEquals"); } @Test(expectedExceptions = AssertionError.class) public void arrayAssertNotEquals() { assertNotEquals(new int[]{ 42 }, new int[] { 42 }, "arrays of primitives are compared by value in assertNotEquals"); } @Test public void boxedArrayAssertEquals() { assertEquals(new Integer[]{ 42 }, new Integer[] { 42 }, "arrays of wrapped values are compared by value in assertEquals"); } @Test(expectedExceptions = AssertionError.class) public void boxedArrayAssertNotEquals() { assertNotEquals(new Integer[]{ 42 }, new Integer[] { 42 }, "arrays of wrapped values are compared by value in assertNotEquals"); } @Test public void mixedArraysAssertEquals() { assertEquals(new int[]{ 42 }, new Integer[] { 42 }, "arrays of wrapped values are compared by value in assertEquals"); assertEquals(new Integer[]{ 42 }, new int[] { 42 }, "arrays of wrapped values are compared by value in assertEquals"); } @Test(expectedExceptions = AssertionError.class) public void mixedArraysAssertNotEquals() { assertNotEquals(new int[]{ 42 }, new Integer[] { 42 }, "arrays of wrapped values are compared by value in assertNotEquals"); assertNotEquals(new Integer[]{ 42 }, new int[] { 42 }, "arrays of wrapped values are compared by value in assertNotEquals"); } @Test(expectedExceptions = AssertionError.class) public void arrayInsideListAssertEquals() { List list = Arrays.asList( new int[]{ 42 } ); List listCopy = Arrays.asList( new int[]{ 42 } ); assertEquals(list, listCopy, "arrays inside lists are compared by reference in assertEquals"); } @Test public void arrayInsideListAssertNotEquals() { List list = Arrays.asList( new int[]{ 42 } ); List listCopy = Arrays.asList( new int[]{ 42 } ); assertNotEquals(list, listCopy, "arrays inside lists are compared by reference in assertNotEquals"); } @Test(expectedExceptions = AssertionError.class) public void arrayInsideMapAssertEquals() { Map map = new HashMap<>(); map.put("array", new int[]{ 42 }); Map mapCopy = new HashMap<>(); mapCopy.put("array", new int[]{ 42 }); // arrays inside maps are compared by reference in assertEquals(Map,Map) assertEquals(map, mapCopy); } @Test(expectedExceptions = AssertionError.class) public void arrayInsideMapAssertEqualsWithMessage() { Map map = new HashMap<>(); map.put("array", new int[]{ 42 }); Map mapCopy = new HashMap<>(); mapCopy.put("array", new int[]{ 42 }); assertEquals(map, mapCopy, "arrays inside maps are compared by reference in assertEquals(Map,Map,String)"); } @Test public void arrayInsideMapAssertNotEquals() { Map map = new HashMap<>(); map.put("array", new int[]{ 42 }); Map mapCopy = new HashMap<>(); mapCopy.put("array", new int[]{ 42 }); assertNotEquals(map, mapCopy, "arrays inside maps are compared by reference in assertNotEquals"); } @Test(expectedExceptions = AssertionError.class) public void arrayInsideSetAssertEquals() { Set set = new HashSet<>(); set.add(new int[]{ 42 }); Set setCopy = new HashSet<>(); setCopy.add(new int[]{ 42 }); assertEquals(set, setCopy, "arrays inside sets are compared by reference in assertNotEquals"); } @Test public void arrayInsideSetAssertNotEquals() { Set set = new HashSet<>(); set.add(new int[]{ 42 }); Set setCopy = new HashSet<>(); setCopy.add(new int[]{ 42 }); assertNotEquals(set, setCopy, "arrays inside sets are compared by reference in assertNotEquals"); } @Test(expectedExceptions = AssertionError.class) public void arrayDeepInListsAssertEquals() { List> list = Collections.singletonList(Arrays.asList(new int[]{ 42 })); List> listCopy = Collections.singletonList(Arrays.asList(new int[]{ 42 })); assertEquals(list, listCopy, "arrays inside lists which are inside lists themselves are compared by reference in assertEquals"); } @Test(expectedExceptions = AssertionError.class) public void arrayDeepInMapsAssertEquals() { Map> map = new HashMap<>(); Map innerMap = new HashMap<>(); innerMap.put("array", new int[]{ 42 }); map.put("map", innerMap); Map> mapCopy = new HashMap<>(); Map innerMapCopy = new HashMap<>(); innerMapCopy.put("array", new int[]{ 42 }); mapCopy.put("map", innerMapCopy); assertEquals(map, mapCopy, "arrays inside maps which are inside maps themselves are compared by reference in assertEquals"); } @Test(expectedExceptions = AssertionError.class) public void arrayDeepInListAndMapAssertEquals() { List> list = new ArrayList<>(); Map innerMap = new HashMap<>(); innerMap.put("array", new int[]{ 42 }); list.add(innerMap); List> listCopy = new ArrayList<>(); Map innerMapCopy = new HashMap<>(); innerMapCopy.put("array", new int[]{ 42 }); list.add(innerMapCopy); assertEquals(list, listCopy, "arrays inside maps which are inside lists themselves are compared by reference in assertEquals"); } @Test(expectedExceptions = AssertionError.class) public void arrayDeepInMapAndListAssertEquals() { Map> map = new HashMap<>(); map.put("list", Arrays.asList(new int[]{ 42 })); Map> mapCopy = new HashMap<>(); mapCopy.put("list", Arrays.asList(new int[]{ 42 })); assertEquals(map, mapCopy, "arrays inside maps which are inside lists themselves are compared by reference in assertEquals"); } @Test(expectedExceptions = AssertionError.class) public void arrayInsideIterableAssertEquals() { Iterable iterable = Arrays.asList( new int[]{ 42 } ); Iterable iterableCopy = Arrays.asList( new int[]{ 42 } ); assertEquals(iterable, iterableCopy, "arrays inside Iterables are compared by reference in assertEquals"); } @Test(expectedExceptions = AssertionError.class) public void arrayDeepInIterablesAssertEquals() { List> iterable = Collections.singletonList(Arrays.asList(new int[]{ 42 })); List> iterableCopy = Collections.singletonList(Arrays.asList(new int[]{ 42 })); assertEquals(iterable, iterableCopy, "arrays inside Iterables which are inside Iterables themselves are compared by reference in assertEquals"); } @Test(expectedExceptions = AssertionError.class) public void arrayInsideArrayAssertEquals() { int[][] array = new int[][] { new int[]{ 42 } }; int[][] arrayCopy = new int[][] { new int[]{ 42 } }; assertEquals(array, arrayCopy, "arrays inside arrays are compared by reference in assertEquals"); } @Test(expectedExceptions = AssertionError.class) public void arrayDeepInArraysAssertEquals() { int[][][] array = new int[][][] { new int[][] { new int[]{ 42 } } }; int[][][] arrayCopy = new int[][][] { new int[][] { new int[]{ 42 } } }; assertEquals(array, arrayCopy, "arrays inside arrays which are inside arrays themselves are compared by reference in assertEquals"); } @SuppressWarnings("unchecked") @Test(expectedExceptions = AssertionError.class) public void arrayDeepInArrayAndListAssertEquals() { List[] array = new List[] { Arrays.asList(new int[]{ 42 }) }; List[] arrayCopy = new List[] { Arrays.asList(new int[]{ 42 }) }; assertEquals(array, arrayCopy, "arrays inside arrays which are inside arrays themselves are compared by reference in assertEquals"); } }