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
6 changes: 4 additions & 2 deletions make/jtreg.gmk
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ $(BUILDDIR)/classes.com.sun.javatest.regtest.ok: \
-d $(CLASSDIR) \
-encoding ASCII \
$(TOOL_DEBUG_FLAGS) \
$(JAVAFILES.com.sun.javatest.regtest-tools)
$(JAVAFILES.com.sun.javatest.regtest-tools) \
$(JAVADIR)/org/junit/jupiter/api/AssertionUtils.java
echo "classes built at `date`" > $@

TARGETS.com.sun.javatest.regtest += $(BUILDDIR)/classes.com.sun.javatest.regtest.ok
Expand Down Expand Up @@ -237,7 +238,8 @@ PKGS.JAR.jtreg += \
com.sun.javatest.regtest.report \
com.sun.javatest.regtest.tool \
com.sun.javatest.regtest.util \
java.lang
java.lang \
org.junit.jupiter.api
TARGETS.JAR.jtreg += $(TARGETS.com.sun.javatest.regtest)

FILES.JAR.jtreg=$(CLASSDIR)/META-INF/services/java.util.spi.ToolProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,11 @@ Map<PathKind, SearchPath> getExecutionPaths(
pp.append(locations.absLibClsList(LibLocn.Kind.SYS_MODULE));
}

// javatest.jar and jtreg.jar
if (include_jtreg) {
(testOnBootClassPath ? bcp : cp).append(getJavaTestClassPath());
}

// Frameworks:
if (multiModule) {
// assert !testOnBootClassPath && !useXPatch()
Expand Down Expand Up @@ -972,11 +977,6 @@ Map<PathKind, SearchPath> getExecutionPaths(
cp.append(cpa);
}

// javatest.jar and jtreg.jar
if (include_jtreg) {
(testOnBootClassPath ? bcp : cp).append(getJavaTestClassPath());
}

Map<PathKind, SearchPath> map = new EnumMap<>(PathKind.class);
if (!bcp.isEmpty())
map.put(PathKind.BOOTCLASSPATH_APPEND, bcp);
Expand Down
124 changes: 124 additions & 0 deletions src/share/classes/org/junit/jupiter/api/AssertionUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* Copyright 2015-2025 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v2.0 which
* accompanies this distribution and is available at
*
* https://www.eclipse.org/legal/epl-v20.html
*/

/*
* Based on https://github.com/junit-team/junit-framework/commits/main/junit-jupiter-api/src/main/java/org/junit/jupiter/api/AssertionUtils.java
*/

package org.junit.jupiter.api;

import static java.util.stream.Collectors.joining;

import java.util.Deque;
import java.util.function.Supplier;

import org.junit.platform.commons.util.UnrecoverableExceptions;
import org.opentest4j.AssertionFailedError;

/**
* {@code AssertionUtils} is a collection of utility methods that are common to
* all assertion implementations.
*
* @since 5.0
*/
class AssertionUtils {

private AssertionUtils() {
/* no-op */
}

static void fail() {
throw new AssertionFailedError();
}

static void fail(String message) {
throw new AssertionFailedError(message);
}

static void fail(String message, Throwable cause) {
throw new AssertionFailedError(message, cause);
}

static void fail(Throwable cause) {
throw new AssertionFailedError(null, cause);
}

static void fail(Supplier<String> messageSupplier) {
throw new AssertionFailedError(nullSafeGet(messageSupplier));
}

static String nullSafeGet(Supplier<String> messageSupplier) {
return (messageSupplier != null ? messageSupplier.get() : null);
}

static String getCanonicalName(Class<?> clazz) {
try {
String canonicalName = clazz.getCanonicalName();
return (canonicalName != null ? canonicalName : clazz.getTypeName());
}
catch (Throwable t) {
UnrecoverableExceptions.rethrowIfUnrecoverable(t);
return clazz.getTypeName();
}
}

static String formatIndexes(Deque<Integer> indexes) {
if (indexes == null || indexes.isEmpty()) {
return "";
}
String indexesString = indexes.stream().map(Object::toString).collect(joining("][", "[", "]"));
return " at index " + indexesString;
}

static boolean floatsAreEqual(float value1, float value2, float delta) {
assertValidDelta(delta);
return floatsAreEqual(value1, value2) || Math.abs(value1 - value2) <= delta;
}

static void assertValidDelta(float delta) {
if (Float.isNaN(delta) || delta < 0.0) {
failIllegalDelta(String.valueOf(delta));
}
}

static void assertValidDelta(double delta) {
if (Double.isNaN(delta) || delta < 0.0) {
failIllegalDelta(String.valueOf(delta));
}
}

static boolean floatsAreEqual(float value1, float value2) {
return Float.floatToIntBits(value1) == Float.floatToIntBits(value2);
}

static boolean doublesAreEqual(double value1, double value2, double delta) {
assertValidDelta(delta);
return doublesAreEqual(value1, value2) || Math.abs(value1 - value2) <= delta;
}

static boolean doublesAreEqual(double value1, double value2) {
return Double.doubleToLongBits(value1) == Double.doubleToLongBits(value2);
}

static boolean objectsAreEqual(Object obj1, Object obj2) {
if (obj1 == null) {
return (obj2 == null);
}
if (obj1.getClass().isArray() && (obj2 != null && obj2.getClass().isArray())) {
throw new AssertionError("Should have used `assertArrayEquals()`?!");
}
return obj1.equals(obj2);
}

private static void failIllegalDelta(String delta) {
fail("positive delta expected but was: <" + delta + ">");
}

}
3 changes: 2 additions & 1 deletion test/junitTrace/JUnitTrace.gmk
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ $(BUILDTESTDIR)/JUnitTrace.othervm.ok: \
$(TESTDIR)/junitTrace/ \
> $(@:%.ok=%/jt.log) 2>&1 || \
true "non-zero exit code from JavaTest intentionally ignored"
$(GREP) -s 'Test results: passed: 3; failed: 2' $(@:%.ok=%/jt.log) > /dev/null
$(GREP) -s 'Test results: passed: 3; failed: 3' $(@:%.ok=%/jt.log) > /dev/null
$(GREP) -s "java.lang.NullPointerException: NPE" $(@:%.ok=%/work/NPE.jtr) > /dev/null
$(GREP) -s "Intentionally thrown before all" $(@:%.ok=%/work/JupiterLifecycle.jtr) > /dev/null
$(GREP) -s "Intentionally thrown after all" $(@:%.ok=%/work/JupiterLifecycle.jtr) > /dev/null
$(GREP) -s "Should have used .assertArrayEquals()." $(@:%.ok=%/work/JupiterAssertEqualsWithArraysTests.jtr) > /dev/null
if $(GREP) -s "^\s\s*at " $(@:%.ok=%/work/Pass.jtr) > /dev/null ; then \
echo "unexpected text"; exit 1; \
fi
Expand Down
45 changes: 45 additions & 0 deletions test/junitTrace/JupiterAssertEqualsWithArraysTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

import org.junit.jupiter.api.*;

/*
* @test
* @run junit JupiterAssertEqualsWithArraysTests
*/
class JupiterAssertEqualsWithArraysTests {
@Test
void arraysInAssertEqualsShouldThrow() {
Object o = new Object();
Object a1 = new Object[] {o};
Object a2 = new Object[] {o};
Assertions.assertEquals(a1, a2);
}

@Test
void arraysInAssertNotEqualsShouldThrow() {
Object a1 = new int[] {99};
Object a2 = new int[] {99};
Assertions.assertNotEquals(a1, a2);
}
}