diff --git a/junit-jupiter-api/src/main/java/org/junit/jupiter/api/AssertionUtils.java b/junit-jupiter-api/src/main/java/org/junit/jupiter/api/AssertionUtils.java index 6ae0c48536a7..05165b58e83c 100644 --- a/junit-jupiter-api/src/main/java/org/junit/jupiter/api/AssertionUtils.java +++ b/junit-jupiter-api/src/main/java/org/junit/jupiter/api/AssertionUtils.java @@ -17,6 +17,7 @@ import java.util.function.Supplier; import org.jspecify.annotations.Nullable; +import org.junit.platform.commons.JUnitException; import org.junit.platform.commons.annotation.Contract; import org.junit.platform.commons.util.UnrecoverableExceptions; @@ -27,7 +28,6 @@ * @since 5.0 */ class AssertionUtils { - private AssertionUtils() { /* no-op */ } @@ -129,6 +129,15 @@ static boolean objectsAreEqual(@Nullable Object obj1, @Nullable Object obj2) { if (obj1 == null) { return (obj2 == null); } + if (obj2 == null) { + return false; + } + if (Boolean.getBoolean("junit.jupiter.disallow.arrays.in.equals.checks")) { + if (obj1.getClass().isArray() && obj2.getClass().isArray()) { + throw new JUnitException( + "Detected array arguments:" + " " + obj1.getClass() + " and " + obj2.getClass()); + } + } return obj1.equals(obj2); } diff --git a/jupiter-tests/src/test/java/org/junit/jupiter/api/AssertEqualsAssertionsTests.java b/jupiter-tests/src/test/java/org/junit/jupiter/api/AssertEqualsAssertionsTests.java index ec30168e7605..884952353cd0 100644 --- a/jupiter-tests/src/test/java/org/junit/jupiter/api/AssertEqualsAssertionsTests.java +++ b/jupiter-tests/src/test/java/org/junit/jupiter/api/AssertEqualsAssertionsTests.java @@ -19,6 +19,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import org.junit.jupiter.api.function.Executable; +import org.junit.platform.commons.JUnitException; import org.opentest4j.AssertionFailedError; /** @@ -750,6 +751,25 @@ void chars() { } + @Nested + class ArraysAsArguments { + @Test + void objects() { + Object object = new Object(); + Object array1 = new Object[] { object }; + Object array2 = new Object[] { object }; + try { + System.setProperty("junit.jupiter.disallow.arrays.in.equals.checks", "true"); + var exception = assertThrows(JUnitException.class, () -> assertEquals(array1, array2)); + assertEquals("Detected array arguments: class [Ljava.lang.Object; and class [Ljava.lang.Object;", + exception.getMessage()); + } + finally { + System.clearProperty("junit.jupiter.disallow.arrays.in.equals.checks"); + } + } + } + // ------------------------------------------------------------------------- @SuppressWarnings("overrides") diff --git a/jupiter-tests/src/test/java/org/junit/jupiter/api/AssertNotEqualsAssertionsTests.java b/jupiter-tests/src/test/java/org/junit/jupiter/api/AssertNotEqualsAssertionsTests.java index 842e7f463a8f..15ab15226f14 100644 --- a/jupiter-tests/src/test/java/org/junit/jupiter/api/AssertNotEqualsAssertionsTests.java +++ b/jupiter-tests/src/test/java/org/junit/jupiter/api/AssertNotEqualsAssertionsTests.java @@ -14,9 +14,16 @@ import static org.junit.jupiter.api.AssertionTestUtils.assertMessageEquals; import static org.junit.jupiter.api.AssertionTestUtils.assertMessageStartsWith; import static org.junit.jupiter.api.AssertionTestUtils.expectAssertionFailedError; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import java.util.Arrays; + +import org.junit.platform.commons.JUnitException; import org.opentest4j.AssertionFailedError; /** @@ -624,6 +631,30 @@ void assertNotEqualsInvokesEqualsMethodForIdenticalObjects() { } + @Nested + class AssertNotEqualsArrays { + @Test + void objects() { + Object object = new Object(); + Object array1 = new Object[] { object }; + Object array2 = new Object[] { object }; + assertThrows(AssertionFailedError.class, () -> assertSame(array1, array2)); + assertThrows(AssertionFailedError.class, () -> assertEquals(array1, array2)); + assertNotEquals(array1, array2); // succeeds + assertTrue(Arrays.deepEquals((Object[]) array1, (Object[]) array2)); + assertArrayEquals((Object[]) array1, (Object[]) array2); + try { + System.setProperty("junit.jupiter.disallow.arrays.in.equals.checks", "true"); + var exception = assertThrows(JUnitException.class, () -> assertNotEquals(array1, array2)); + assertEquals("Detected array arguments: class [Ljava.lang.Object; and class [Ljava.lang.Object;", + exception.getMessage()); + } + finally { + System.clearProperty("junit.jupiter.disallow.arrays.in.equals.checks"); + } + } + } + // ------------------------------------------------------------------------- @Nested