Skip to content
Merged
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 @@ -66,11 +66,9 @@
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -157,6 +155,12 @@ private void registerTestClassesForReflection(List<? extends DiscoverySelector>
.selectors(selectors)
.build();

TestClassRegistrar testClassRegistrar = new TestClassRegistrar(clazz -> {
debug("Registering test class for reflection: %s", clazz.getName());
nativeImageConfigImpl.registerAllClassMembersForReflection(clazz);
forEachProvider(p -> p.onTestClassRegistered(clazz, nativeImageConfigImpl));
});

Launcher launcher = LauncherFactory.create();
TestPlan testPlan = launcher.discover(request);
testPlan.getRoots().stream()
Expand All @@ -167,49 +171,7 @@ private void registerTestClassesForReflection(List<? extends DiscoverySelector>
.filter(ClassSource.class::isInstance)
.map(ClassSource.class::cast)
.map(ClassSource::getJavaClass)
.forEach(this::registerTestClassForReflection);
}

private final Set<Class<?>> registeredClasses = new HashSet<>();

private boolean shouldRegisterClass(Class<?> clazz) {
/* avoid registering java internal classes */
if (ModuleLayer.boot().modules().contains(clazz.getModule())) {
return false;
}

/* avoid loops (possible case: class B is inner class of A, and B extends A) */
if (registeredClasses.contains(clazz)) {
return false;
}
registeredClasses.add(clazz);

return true;
}

private void registerTestClassForReflection(Class<?> clazz) {
if (!shouldRegisterClass(clazz)) {
return;
}

debug("Registering test class for reflection: %s", clazz.getName());
nativeImageConfigImpl.registerAllClassMembersForReflection(clazz);
forEachProvider(p -> p.onTestClassRegistered(clazz, nativeImageConfigImpl));

Class<?>[] declaredClasses = clazz.getDeclaredClasses();
for (Class<?> declaredClass : declaredClasses) {
registerTestClassForReflection(declaredClass);
}

Class<?>[] interfaces = clazz.getInterfaces();
for (Class<?> inter : interfaces) {
registerTestClassForReflection(inter);
}

Class<?> superClass = clazz.getSuperclass();
if (superClass != null && superClass != Object.class) {
registerTestClassForReflection(superClass);
}
.forEach(testClassRegistrar::registerTestClassForReflection);
}

private void forEachProvider(Consumer<PluginConfigProvider> consumer) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or
* data (collectively the "Software"), free of charge and under any and all
* copyright rights in the Software, and any and all patent rights owned or
* freely licensable by each licensor hereunder covering either (i) the
* unmodified Software as contributed to or provided by such licensor, or (ii)
* the Larger Works (as defined below), to deal in both
*
* (a) the Software, and
*
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
*
* The above copyright notice and either this complete permission notice or at a
* minimum a reference to the UPL must be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package org.graalvm.junit.platform;

import java.util.HashSet;
import java.util.Set;
import java.util.function.Consumer;

class TestClassRegistrar {

private final Set<Class<?>> registeredClasses = new HashSet<>();
private final Consumer<Class<?>> registrationCallback;

TestClassRegistrar(Consumer<Class<?>> registrationCallback) {
this.registrationCallback = registrationCallback;
}

void registerTestClassForReflection(Class<?> clazz) {
if (!shouldRegisterClass(clazz)) {
return;
}

registrationCallback.accept(clazz);

Class<?>[] declaredClasses = clazz.getDeclaredClasses();
for (Class<?> declaredClass : declaredClasses) {
registerTestClassForReflection(declaredClass);
}

Class<?>[] interfaces = clazz.getInterfaces();
for (Class<?> inter : interfaces) {
registerTestClassForReflection(inter);
}

Class<?> superClass = clazz.getSuperclass();
if (superClass != null && superClass != Object.class) {
registerTestClassForReflection(superClass);
}
}

private boolean shouldRegisterClass(Class<?> clazz) {
/* avoid registering java internal classes */
if (ModuleLayer.boot().modules().contains(clazz.getModule())) {
return false;
}

/* avoid loops (possible case: class B is inner class of A, and B extends A) */
if (registeredClasses.contains(clazz)) {
return false;
}
registeredClasses.add(clazz);

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand All @@ -53,32 +52,6 @@

public class AbstractParentClassTests {

public static class OuterClass {
@Test
protected void test() {
Assertions.assertTrue(true, "Just a dummy test that should be executed in outer class");
}

/* Since at org.graalvm.junit.platform.JUnitPlatformFeature#registerTestClassForReflection we register all
* declared classes and superclass of the test class, and we do so recursively, we want to avoid infinite loop.
* This inheritance shows that we won't call registration of these classes indefinitely (call registration of
* all declared classes of AbstractParentClassTests, then recursively call superclass of InfiniteLoopTest and
* repeat the process indefinitely) */
private class InfiniteLoopTest extends OuterClass {
@Test
protected void test() {
Assertions.assertTrue(true, "Just a dummy test that should be executed in inner class");
}
}

/* Since enum here is declared class of AbstractParentClassTests, we want to avoid registrations of
* enum's internal superclasses and sub-classes at org.graalvm.junit.platform.JUnitPlatformFeature#registerTestClassForReflection */
private enum EnumTest {
SOME_VALUE,
OTHER_VALUE
}
}

public abstract static class MathPowerTests {
protected static BiFunction<Integer, Integer, Integer> powFunction;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or
* data (collectively the "Software"), free of charge and under any and all
* copyright rights in the Software, and any and all patent rights owned or
* freely licensable by each licensor hereunder covering either (i) the
* unmodified Software as contributed to or provided by such licensor, or (ii)
* the Larger Works (as defined below), to deal in both
*
* (a) the Software, and
*
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
*
* The above copyright notice and either this complete permission notice or at a
* minimum a reference to the UPL must be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package org.graalvm.junit.jupiter;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.params.ParameterizedClass;
import org.junit.jupiter.params.provider.ValueSource;

abstract class BaseTests {
@Test
void test(TestInfo testInfo) {
Assertions.assertEquals(ParameterizedClassTests.InnerTests.class, testInfo.getTestClass().orElseThrow());
}
}

@ParameterizedClass
@ValueSource(ints = { 1, 2 })
class ParameterizedClassTests {

private final int value;

ParameterizedClassTests(int value) {
this.value = value;
}

@Test
void test() {
Assertions.assertTrue(value == 1 || value == 2);
}

@Nested
class InnerTests extends BaseTests {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or
* data (collectively the "Software"), free of charge and under any and all
* copyright rights in the Software, and any and all patent rights owned or
* freely licensable by each licensor hereunder covering either (i) the
* unmodified Software as contributed to or provided by such licensor, or (ii)
* the Larger Works (as defined below), to deal in both
*
* (a) the Software, and
*
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
*
* The above copyright notice and either this complete permission notice or at a
* minimum a reference to the UPL must be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package org.graalvm.junit.platform;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

class TestClassRegistrarTests {

@Test
void avoidsInfiniteLoopsWhenClassDefinitionsAreRecursive() {
List<Class<?>> registeredClasses = new ArrayList<>();
var registrar = new TestClassRegistrar(registeredClasses::add);

registrar.registerTestClassForReflection(OuterClass.class);

assertEquals(List.of(OuterClass.class, OuterClass.InfiniteLoop.class), registeredClasses);
}

@Test
void avoidsRegistrationOfJavaInternalTypes() {
List<Class<?>> registeredClasses = new ArrayList<>();
var registrar = new TestClassRegistrar(registeredClasses::add);

registrar.registerTestClassForReflection(EnumTest.class);

assertEquals(List.of(EnumTest.class), registeredClasses);
}

public static class OuterClass {

/* Since TestClassRegistrar registers all declared classes and superclass of the test class, and we do so
* recursively, we want to avoid infinite loop.
* This inheritance shows that we won't call registration of these classes indefinitely (call registration of
* all declared classes of AbstractParentClassTests, then recursively call superclass of InfiniteLoopTest and
* repeat the process indefinitely) */
@SuppressWarnings({"unused", "InnerClassMayBeStatic"})
class InfiniteLoop extends OuterClass {
}
}

/* Since enum here is declared class of AbstractParentClassTests, we want to avoid registrations of
* enum's internal superclasses and sub-classes */
@SuppressWarnings("unused")
enum EnumTest {
SOME_VALUE,
OTHER_VALUE
}
}
1 change: 1 addition & 0 deletions common/utils/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ dependencies {
implementation(libs.openjson)
testImplementation(platform(libs.test.junit.bom))
testImplementation(libs.test.junit.jupiter.core)
testRuntimeOnly(libs.test.junit.platform.launcher)
}
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ mavenEmbedder = "3.9.9"
mavenResolver = "1.9.22"
graalvm = "23.0.2"
openjson = "1.0.13"
junitPlatform = "1.11.0"
junitJupiter = "5.11.0"
junitPlatform = "1.13.0"
junitJupiter = "5.13.0"
slf4j = "1.7.9"
groovy = "3.0.11"
jetty = "11.0.11"
Expand Down
Loading
Loading