From 89dc5845fcb46c26af187e50ea907a7382d06e72 Mon Sep 17 00:00:00 2001 From: Krishnan Mahadevan Date: Sun, 18 Dec 2022 21:02:15 +0530 Subject: [PATCH] Streamline overloaded assertion methods for Groovy Closes #2854 --- CHANGES.txt | 1 + .../src/main/java/org/testng/Assert.java | 170 +++++++++++++++++- .../java/test/asserttests/AssertTest.java | 2 +- .../test/groovy/test/groovy/GroovyTest.groovy | 8 +- .../issue2854/AssertionsTestSample.groovy | 57 ++++++ 5 files changed, 233 insertions(+), 5 deletions(-) create mode 100644 testng-core/src/test/groovy/test/groovy/issue2854/AssertionsTestSample.groovy diff --git a/CHANGES.txt b/CHANGES.txt index babbcf7ee1..2c9cfda544 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,4 +1,5 @@ Current +Fixed: GITHUB-2854: overloaded assertEquals methods do not work from Groovy (Krishnan Mahadevan) 7.7.0 Fixed: GITHUB-2852: [SECURITY] Fix Zip Slip Vulnerability (Jonathan Leitschuh) diff --git a/testng-asserts/src/main/java/org/testng/Assert.java b/testng-asserts/src/main/java/org/testng/Assert.java index 239df244d3..6fc9da5cff 100644 --- a/testng-asserts/src/main/java/org/testng/Assert.java +++ b/testng-asserts/src/main/java/org/testng/Assert.java @@ -752,6 +752,18 @@ public static void assertEquals(double actual, Double expected, String message) assertEquals(Double.valueOf(actual), expected, message); } + /** + * Asserts that two doubles are equal. If they are not, an 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 assertEquals(Double actual, Double expected, String message) { + assertEquals(actual, (Object) expected, message); + } + /** * Asserts that two doubles are equal. If they are not, an AssertionError is thrown. * @@ -782,8 +794,18 @@ public static void assertEquals(double actual, Double expected) { assertEquals(Double.valueOf(actual), expected, null); } + /** + * Asserts that two doubles are equal. If they are not, an AssertionError is thrown. + * + * @param actual the actual value + * @param expected the expected value + */ + public static void assertEquals(Double actual, Double expected) { + assertEquals(actual, expected, null); + } + private static boolean areEqual(float actual, float expected, float delta) { - // handle infinity specially since subtracting to infinite values gives NaN and the + // handle infinity specially since subtracting to infinite values gives NaN and // the following test fails if (Float.isInfinite(expected)) { if (!(expected == actual)) { @@ -869,6 +891,18 @@ public static void assertEquals(float actual, Float expected, String message) { assertEquals(Float.valueOf(actual), expected, message); } + /** + * Asserts that two floats are equal. If they are not, an 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 assertEquals(Float actual, Float expected, String message) { + assertEquals(actual, (Object) expected, message); + } + /** * Asserts that two floats are equal. If they are not, an AssertionError is thrown. * @@ -899,6 +933,16 @@ public static void assertEquals(float actual, Float expected) { assertEquals(Float.valueOf(actual), expected, null); } + /** + * Asserts that two floats are equal. If they are not, an AssertionError is thrown. + * + * @param actual the actual value + * @param expected the expected value + */ + public static void assertEquals(Float actual, Float expected) { + assertEquals(actual, expected, null); + } + /** * Asserts that two longs are equal. If they are not, an AssertionError, with the given message, * is thrown. @@ -931,8 +975,8 @@ public static void assertEquals(Long actual, long expected, String message) { * @param expected the expected value * @param message the assertion error message */ - public static void assertEquals(long actual, Long expected, String message) { - assertEquals(Long.valueOf(actual), expected, message); + public static void assertEquals(Long actual, Long expected, String message) { + assertEquals(actual, (Object) expected, message); } /** @@ -964,6 +1008,16 @@ public static void assertEquals(long actual, Long expected) { assertEquals(Long.valueOf(actual), expected, null); } + /** + * Asserts that two longs are equal. If they are not, an AssertionError is thrown. + * + * @param actual the actual value + * @param expected the expected value + */ + public static void assertEquals(Long actual, Long expected) { + assertEquals(actual, expected, null); + } + /** * Asserts that two booleans are equal. If they are not, an AssertionError, with the given * message, is thrown. @@ -1000,6 +1054,18 @@ public static void assertEquals(boolean actual, Boolean expected, String message assertEquals(Boolean.valueOf(actual), expected, message); } + /** + * Asserts that two booleans are equal. If they are not, an 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 assertEquals(Boolean actual, Boolean expected, String message) { + assertEquals(actual, (Object) expected, message); + } + /** * Asserts that two booleans are equal. If they are not, an AssertionError is thrown. * @@ -1020,6 +1086,16 @@ public static void assertEquals(Boolean actual, boolean expected) { assertEquals(actual, Boolean.valueOf(expected), null); } + /** + * Asserts that two booleans are equal. If they are not, an AssertionError is thrown. + * + * @param actual the actual value + * @param expected the expected value + */ + public static void assertEquals(Boolean actual, Boolean expected) { + assertEquals(actual, expected, null); + } + /** * Asserts that two booleans are equal. If they are not, an AssertionError is thrown. * @@ -1066,6 +1142,18 @@ public static void assertEquals(byte actual, Byte expected, String message) { assertEquals(Byte.valueOf(actual), expected, message); } + /** + * Asserts that two bytes are equal. If they are not, an 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 assertEquals(Byte actual, Byte expected, String message) { + assertEquals(actual, (Object) expected, message); + } + /** * Asserts that two bytes are equal. If they are not, an AssertionError is thrown. * @@ -1086,6 +1174,16 @@ public static void assertEquals(Byte actual, byte expected) { assertEquals(actual, Byte.valueOf(expected), null); } + /** + * Asserts that two bytes are equal. If they are not, an AssertionError is thrown. + * + * @param actual the actual value + * @param expected the expected value + */ + public static void assertEquals(Byte actual, Byte expected) { + assertEquals(actual, expected, null); + } + /** * Asserts that two bytes are equal. If they are not, an AssertionError is thrown. * @@ -1132,6 +1230,18 @@ public static void assertEquals(char actual, Character expected, String message) assertEquals(Character.valueOf(actual), expected, message); } + /** + * Asserts that two chars are equal. If they are not, an 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 assertEquals(Character actual, Character expected, String message) { + assertEquals(actual, (Object) expected, message); + } + /** * Asserts that two chars are equal. If they are not, an AssertionError is thrown. * @@ -1162,6 +1272,16 @@ public static void assertEquals(char actual, Character expected) { assertEquals(Character.valueOf(actual), expected, null); } + /** + * Asserts that two chars are equal. If they are not, an AssertionError is thrown. + * + * @param actual the actual value + * @param expected the expected value + */ + public static void assertEquals(Character actual, Character expected) { + assertEquals(actual, expected, null); + } + /** * Asserts that two shorts are equal. If they are not, an AssertionError, with the given message, * is thrown. @@ -1198,6 +1318,18 @@ public static void assertEquals(short actual, Short expected, String message) { assertEquals(Short.valueOf(actual), expected, message); } + /** + * Asserts that two shorts are equal. If they are not, an 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 assertEquals(Short actual, Short expected, String message) { + assertEquals(actual, (Object) expected, message); + } + /** * Asserts that two shorts are equal. If they are not, an AssertionError is thrown. * @@ -1228,6 +1360,16 @@ public static void assertEquals(short actual, Short expected) { assertEquals(Short.valueOf(actual), expected, null); } + /** + * Asserts that two shorts are equal. If they are not, an AssertionError is thrown. + * + * @param actual the actual value + * @param expected the expected value + */ + public static void assertEquals(Short actual, Short expected) { + assertEquals(actual, expected, null); + } + /** * Asserts that two ints are equal. If they are not, an AssertionError, with the given message, is * thrown. @@ -1264,6 +1406,18 @@ public static void assertEquals(int actual, Integer expected, String message) { assertEquals(Integer.valueOf(actual), expected, message); } + /** + * Asserts that two ints are equal. If they are not, an 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 assertEquals(Integer actual, Integer expected, String message) { + assertEquals(actual, (Object) expected, message); + } + /** * Asserts that two ints are equal. If they are not, an AssertionError is thrown. * @@ -1294,6 +1448,16 @@ public static void assertEquals(int actual, Integer expected) { assertEquals(Integer.valueOf(actual), expected, null); } + /** + * Asserts that two ints are equal. If they are not, an AssertionError is thrown. + * + * @param actual the actual value + * @param expected the expected value + */ + public static void assertEquals(Integer actual, Integer expected) { + assertEquals(actual, expected, null); + } + /** * Asserts that an object isn't null. If it is, an AssertionError is thrown. * diff --git a/testng-asserts/src/test/java/test/asserttests/AssertTest.java b/testng-asserts/src/test/java/test/asserttests/AssertTest.java index 62ee70927d..8eacebf6df 100644 --- a/testng-asserts/src/test/java/test/asserttests/AssertTest.java +++ b/testng-asserts/src/test/java/test/asserttests/AssertTest.java @@ -51,7 +51,7 @@ public void assertEqualsBoxedUnboxedLong() { assertEquals(a, b); assertEquals(a, b, ""); assertEquals(b, a); - assertEquals(b, a, ""); + assertEquals(Long.valueOf(b), a, ""); } @Test(description = "GITHUB-2652") diff --git a/testng-core/src/test/groovy/test/groovy/GroovyTest.groovy b/testng-core/src/test/groovy/test/groovy/GroovyTest.groovy index 9f8b166b81..e567b0da2d 100644 --- a/testng-core/src/test/groovy/test/groovy/GroovyTest.groovy +++ b/testng-core/src/test/groovy/test/groovy/GroovyTest.groovy @@ -6,8 +6,8 @@ import org.testng.ITestContext import org.testng.ITestNGListener import org.testng.TestListenerAdapter import org.testng.annotations.Test -import org.testng.reporters.JUnitXMLReporter import test.SimpleBaseTest +import test.groovy.issue2854.AssertionsTestSample import static org.assertj.core.api.Assertions.assertThat @@ -53,4 +53,10 @@ class GroovyTest extends SimpleBaseTest { assertThat testMethodNames containsExactly "test1", "test2" } + @Test(description = "GITHUB-2854") + void ensureAssertionsWork() { + def testng = create AssertionsTestSample + testng.run() + assertThat testng.status isEqualTo(0) + } } diff --git a/testng-core/src/test/groovy/test/groovy/issue2854/AssertionsTestSample.groovy b/testng-core/src/test/groovy/test/groovy/issue2854/AssertionsTestSample.groovy new file mode 100644 index 0000000000..befdf752dd --- /dev/null +++ b/testng-core/src/test/groovy/test/groovy/issue2854/AssertionsTestSample.groovy @@ -0,0 +1,57 @@ +package test.groovy.issue2854 + +import org.testng.annotations.Test + +import static org.testng.Assert.assertEquals + +class AssertionsTestSample { + + @Test + void testAssertEqualsWorksWithBooleans() { + assertEquals(true, true) + assertEquals(true, true, "Sample Message") + } + + @Test + void testAssertEqualsWorksWithBytes() { + assertEquals(Byte.valueOf((byte) 10), Byte.valueOf((byte) 10)) + assertEquals(Byte.valueOf((byte) 10), Byte.valueOf((byte) 10), "Sample Message") + } + + @Test + void testAssertEqualsWorksWithChars() { + assertEquals(Character.valueOf((char) 10), Character.valueOf((char) 10)) + assertEquals(Character.valueOf((char) 10), Character.valueOf((char) 10), "Sample Message") + } + + @Test + void testAssertEqualsWorksWithShorts() { + assertEquals(Short.valueOf((short) 10), Short.valueOf((short) 10)) + assertEquals(Short.valueOf((short) 10), Short.valueOf((short) 10), "Sample Message") + } + + @Test + void testAssertEqualsWorksWithInts() { + assertEquals(10, 10) + assertEquals(10, 10, "Sample Message") + } + + @Test + void testAssertEqualsWorksWithLongs() { + assertEquals(Long.valueOf((long) 10), Long.valueOf((long) 10)) + assertEquals(Long.valueOf((long) 10), Long.valueOf((long) 10), "Sample Message") + } + + @Test + void testAssertEqualsWorksWithFloats() { + assertEquals(Float.valueOf((float) 10), Float.valueOf((float) 10)) + assertEquals(Float.valueOf((float) 10), Float.valueOf((float) 10), "Sample Message") + } + + @Test + void testAssertEqualsWorksWithDoubles() { + assertEquals(Double.valueOf((double) 10), Double.valueOf((double) 10)) + assertEquals(Double.valueOf((double) 10), Double.valueOf((double) 10), "Sample Message") + } + +}