diff --git a/CHANGES.txt b/CHANGES.txt index a0ce8b008d..b3377aea74 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,8 +1,9 @@ Current 7.6.0 +Fixed: assertEqualsNoOrder for Collection and Iterators size check was missing (Adam Kaczmarek) Fixed: GITHUB-2709: Testnames not working together with suites in suite (Martin Aldrin) Fixed: GITHUB-2704: IHookable and IConfigurable callback discrepancy (Krishnan Mahadevan) -Fixed: GITHUB-2637: Upgrade to JDK11 as the minimum JDK requirements(Krishnan Mahadevan) +Fixed: GITHUB-2637: Upgrade to JDK11 as the minimum JDK requirements (Krishnan Mahadevan) 7.5 Fixed: GITHUB-2701: Bump gradle version to 7.3.3 to support java17 build (ZhangJian He) diff --git a/testng-asserts/src/main/java/org/testng/Assert.java b/testng-asserts/src/main/java/org/testng/Assert.java index 315e5b7a42..f52a0df63f 100644 --- a/testng-asserts/src/main/java/org/testng/Assert.java +++ b/testng-asserts/src/main/java/org/testng/Assert.java @@ -1669,8 +1669,22 @@ public static void assertEqualsNoOrder(Object[] actual, Object[] expected, Strin } } + /** + * Asserts that two collection contain the same elements in no particular order. If they do not, + * an {@code AssertionError}, with the given message, is thrown. + * + * @param actual the actual value + * @param expected the expected value + * @param message the assertion error message + */ public static void assertEqualsNoOrder( Collection actual, Collection expected, String message) { + if (actual.size() != expected.size()) { + failAssertNoEqual( + "Collections do not have the same size: " + actual.size() + " != " + expected.size(), + message); + } + List actualCollection = Lists.newArrayList(actual); actualCollection.removeAll(expected); if (!actualCollection.isEmpty()) { @@ -1679,9 +1693,28 @@ public static void assertEqualsNoOrder( } } + /** + * Asserts that two iterators contain the same elements in no particular order. If they do not, an + * {@code AssertionError}, with the given message, is thrown. + * + * @param actual the actual value + * @param expected the expected value + * @param message the assertion error message + */ public static void assertEqualsNoOrder(Iterator actual, Iterator expected, String message) { List actualCollection = Lists.newArrayList(actual); - actualCollection.removeAll(Lists.newArrayList(expected)); + List expectedCollection = Lists.newArrayList(expected); + + if (actualCollection.size() != expectedCollection.size()) { + failAssertNoEqual( + "Iterators do not have the same size: " + + +actualCollection.size() + + " != " + + expectedCollection.size(), + message); + } + + actualCollection.removeAll(expectedCollection); if (!actualCollection.isEmpty()) { failAssertNoEqual( "Iterators not equal: expected: " @@ -1734,10 +1767,24 @@ public static void assertEqualsNoOrder(Object[] actual, Object[] expected) { assertEqualsNoOrder(actual, expected, null); } + /** + * Asserts that two collection contain the same elements in no particular order. If they do not, + * an {@code AssertionError} is thrown. + * + * @param actual the actual value + * @param expected the expected value + */ public static void assertEqualsNoOrder(Collection actual, Collection expected) { assertEqualsNoOrder(actual, expected, null); } + /** + * Asserts that two iterators contain the same elements in no particular order. If they do not, an + * {@code AssertionError} is thrown. + * + * @param actual the actual value + * @param expected the expected value + */ public static void assertEqualsNoOrder(Iterator actual, Iterator expected) { assertEqualsNoOrder(actual, expected, null); } diff --git a/testng-asserts/src/test/java/test/asserttests/AssertTest.java b/testng-asserts/src/test/java/test/asserttests/AssertTest.java index bc4fc3aaff..070346f7bd 100644 --- a/testng-asserts/src/test/java/test/asserttests/AssertTest.java +++ b/testng-asserts/src/test/java/test/asserttests/AssertTest.java @@ -301,6 +301,24 @@ public void checkCollectionEqualsNoOrder() { assertEqualsNoOrder(collection1, collection2); } + @Test(expectedExceptions = AssertionError.class) + public void checkCollectionEqualsNoOrderCollection1SizeGreater() { + + Collection collection1 = new LinkedHashSet<>(Arrays.asList("a", "b", "c", "d")); + Collection collection2 = new LinkedHashSet<>(Arrays.asList("a", "c", "b")); + + assertEqualsNoOrder(collection1, collection2); + } + + @Test(expectedExceptions = AssertionError.class) + public void checkCollectionEqualsNoOrderCollection2SizeGreater() { + + Collection collection1 = new LinkedHashSet<>(Arrays.asList("a", "b", "c")); + Collection collection2 = new LinkedHashSet<>(Arrays.asList("a", "c", "b", "d")); + + assertEqualsNoOrder(collection1, collection2); + } + @Test(description = "GITHUB-2540") public void checkCollectionEquals() { @@ -346,6 +364,26 @@ public void checkIteratorEqualsNoOrder() { assertEqualsNoOrder(iterator1, iterator2); } + @Test(expectedExceptions = AssertionError.class) + public void checkIteratorEqualsNoOrderIterator1SizeGreater() { + + Iterator iterator1 = + (new LinkedHashSet<>(Arrays.asList("a", "b", "c", "d"))).iterator(); + Iterator iterator2 = (new LinkedHashSet<>(Arrays.asList("a", "c", "b"))).iterator(); + + assertEqualsNoOrder(iterator1, iterator2); + } + + @Test(expectedExceptions = AssertionError.class) + public void checkIteratorEqualsNoOrderIterator2SizeGreater() { + + Iterator iterator1 = (new LinkedHashSet<>(Arrays.asList("a", "b", "c"))).iterator(); + Iterator iterator2 = + (new LinkedHashSet<>(Arrays.asList("a", "c", "b", "d"))).iterator(); + + assertEqualsNoOrder(iterator1, iterator2); + } + @Test(description = "GITHUB-2540") public void checkIteratorEquals() { @@ -391,6 +429,24 @@ public void checkSetEqualsNoOrder() { assertEqualsNoOrder(set1, set2); } + @Test(expectedExceptions = AssertionError.class) + public void checkSetEqualsNoOrderSet1SizeGreater() { + + Set set1 = new LinkedHashSet<>(Arrays.asList("a", "b", "c", "d")); + Set set2 = new LinkedHashSet<>(Arrays.asList("a", "c", "b")); + + assertEqualsNoOrder(set1, set2); + } + + @Test(expectedExceptions = AssertionError.class) + public void checkSetEqualsNoOrderSet2SizeGreater() { + + Set set1 = new LinkedHashSet<>(Arrays.asList("a", "b", "c")); + Set set2 = new LinkedHashSet<>(Arrays.asList("a", "c", "b", "d")); + + assertEqualsNoOrder(set1, set2); + } + @Test(description = "GITHUB-2540") public void checkSetEquals() {