Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -27,7 +28,6 @@
* @since 5.0
*/
class AssertionUtils {

private AssertionUtils() {
/* no-op */
}
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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
Expand Down