Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce and migrate to a plugin configuration mechanism. #59

Merged
merged 1 commit into from
Jun 29, 2021
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
1 change: 1 addition & 0 deletions common/junit-platform-native/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ dependencies {
implementation group: 'org.junit.jupiter', name: 'junit-jupiter', version: junit_jupiter_version
testImplementation(platform('org.junit:junit-bom:' + junit_jupiter_version))
testImplementation('org.junit.jupiter:junit-jupiter')
testImplementation('org.junit.vintage:junit-vintage-engine')
}

apply from: "gradle/native-image-testing.gradle"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ afterEvaluate {
"-cp", classpath,
"--no-fallback",
"--features=org.graalvm.junit.platform.JUnitPlatformFeature",
"-H:Name=native-image-tests.bin",
"-H:Class=org.graalvm.junit.platform.NativeImageJUnitLauncher"
"-H:Name=native-image-tests",
"-H:Class=org.graalvm.junit.platform.NativeImageJUnitLauncher",
]
if (project.hasProperty("agent")) {
if (!new File("${buildDir}/agentOutput/").exists()) {
throw new GradleException("Agent output missing when -Pagent is set.\n" +
"You need to run `./gradlew -Pagent test` first.")
"You need to run `gradle -Pagent test` first.")
}

args << "-H:ConfigurationFileDirectories=${buildDir}/agentOutput"
Expand All @@ -59,5 +59,5 @@ afterEvaluate {
tasks.register("nativeTest", Exec) {
dependsOn nativeTestBuild
workingDir = "${buildDir}"
executable = "./native-image-tests.bin"
executable = "${buildDir}/native-image-tests"
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@
*/
package org.graalvm.junit.platform;

import org.graalvm.junit.platform.config.core.PluginConfigProvider;
import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.hosted.Feature;
import org.graalvm.nativeimage.hosted.RuntimeClassInitialization;
import org.graalvm.nativeimage.hosted.RuntimeReflection;
import org.junit.platform.engine.DiscoverySelector;
import org.junit.platform.engine.discovery.DiscoverySelectors;
import org.junit.platform.engine.support.descriptor.ClassSource;
Expand All @@ -60,58 +60,29 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.function.Consumer;
import java.util.stream.Collectors;

@SuppressWarnings("unused")
public final class JUnitPlatformFeature implements Feature {

final boolean debug = System.getProperty("debug") != null;
public final boolean debug = System.getProperty("debug") != null;

private static final NativeImageConfigurationImpl nativeImageConfigImpl = new NativeImageConfigurationImpl();
private final ServiceLoader<PluginConfigProvider> extensionConfigProviders = ServiceLoader.load(PluginConfigProvider.class);

@Override
public void duringSetup(DuringSetupAccess access) {
try {
RuntimeReflection.register(org.junit.platform.commons.annotation.Testable.class.getMethods());
RuntimeReflection.register(Class.forName("org.junit.jupiter.params.ParameterizedTestExtension").getDeclaredMethods());
RuntimeReflection.registerForReflectiveInstantiation(Class.forName("org.junit.jupiter.params.ParameterizedTestExtension"));
RuntimeReflection.register(Class.forName("org.junit.jupiter.params.provider.CsvArgumentsProvider").getMethods());
RuntimeReflection.register(Class.forName("org.junit.jupiter.params.provider.CsvArgumentsProvider").getDeclaredMethods());
RuntimeReflection.registerForReflectiveInstantiation(Class.forName("org.junit.jupiter.params.provider.CsvArgumentsProvider"));
} catch (ClassNotFoundException e) {
throw new RuntimeException("Missing some JUnit Platform classes for runtime reflection configuration. \n" +
"Check if JUnit Platform is on your classpath or if that version is supported. \n" +
"Original error: " + e);
}
forEachProvider(p -> p.onLoad(nativeImageConfigImpl));
}

@Override
public void beforeAnalysis(BeforeAnalysisAccess access) {
RuntimeClassInitialization.initializeAtBuildTime("org.junit.vintage.engine.support.UniqueIdReader");
RuntimeClassInitialization.initializeAtBuildTime("org.junit.vintage.engine.support.UniqueIdStringifier");
RuntimeClassInitialization.initializeAtBuildTime("org.junit.platform.launcher.core.InternalTestPlan");
RuntimeClassInitialization.initializeAtBuildTime("org.junit.platform.commons.util.StringUtils");
RuntimeClassInitialization.initializeAtBuildTime("org.junit.platform.launcher.core.TestExecutionListenerRegistry");
RuntimeClassInitialization.initializeAtBuildTime("org.junit.platform.commons.logging.LoggerFactory$DelegatingLogger");
RuntimeClassInitialization.initializeAtBuildTime("org.junit.platform.launcher.core.EngineDiscoveryOrchestrator");
RuntimeClassInitialization.initializeAtBuildTime("org.junit.platform.launcher.core.LauncherConfigurationParameters");
RuntimeClassInitialization.initializeAtBuildTime("org.junit.platform.commons.logging.LoggerFactory");
RuntimeClassInitialization.initializeAtBuildTime("org.junit.jupiter.engine.config.EnumConfigurationParameterConverter");
RuntimeClassInitialization.initializeAtBuildTime("org.junit.jupiter.engine.descriptor.ClassTestDescriptor");
RuntimeClassInitialization.initializeAtBuildTime("org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor");
RuntimeClassInitialization.initializeAtBuildTime("org.junit.jupiter.engine.descriptor.TestFactoryTestDescriptor");
RuntimeClassInitialization.initializeAtBuildTime("org.junit.platform.engine.UniqueIdFormat");
RuntimeClassInitialization.initializeAtBuildTime("org.junit.jupiter.engine.descriptor.JupiterTestDescriptor");
RuntimeClassInitialization.initializeAtBuildTime("org.junit.jupiter.engine.descriptor.MethodBasedTestDescriptor");
RuntimeClassInitialization.initializeAtBuildTime("org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor");
RuntimeClassInitialization.initializeAtBuildTime("org.junit.platform.commons.util.ReflectionUtils");
RuntimeClassInitialization.initializeAtBuildTime("org.junit.jupiter.engine.descriptor.TestTemplateTestDescriptor");
RuntimeClassInitialization.initializeAtBuildTime("org.junit.jupiter.engine.execution.ConditionEvaluator");
RuntimeClassInitialization.initializeAtBuildTime("org.junit.jupiter.engine.execution.ExecutableInvoker");
RuntimeClassInitialization.initializeAtBuildTime(NativeImageJUnitLauncher.class);

Launcher launcher = LauncherFactory.create();
Expand All @@ -136,7 +107,7 @@ private List<? extends DiscoverySelector> getSelectors(List<Path> classpath) {

// Run a a junit launcher to discover tests and register classes for reflection
if (debug) {
classpath.forEach(path -> System.out.println("[Debug] Found classpath: " + path));
classpath.forEach(path -> debug("Found classpath: " + path));
}
return DiscoverySelectors.selectClasspathRoots(new HashSet<>(classpath));

Expand All @@ -163,16 +134,28 @@ private TestPlan registerTestPlan(Launcher launcher, List<Path> classpath) {
}

private void registerTestClassForReflection(Class<?> clazz) {
if (debug) {
System.out.println("[Debug] Registering test class for reflection: " + clazz.getName());
debug("Registering test class for reflection: %s", clazz.getName());
nativeImageConfigImpl.registerAllClassMembersForReflection(clazz);
forEachProvider(p -> p.onTestClassRegistered(clazz, nativeImageConfigImpl));
Class<?> superClass = clazz.getSuperclass();
if (superClass != null && superClass != Object.class) {
registerTestClassForReflection(superClass);
}
RuntimeReflection.register(clazz.getDeclaredConstructors());
}

for (Field field : clazz.getDeclaredFields()) {
RuntimeReflection.register(field);
private void forEachProvider(Consumer<PluginConfigProvider> consumer) {
for (PluginConfigProvider provider : extensionConfigProviders) {
consumer.accept(provider);
}
for (Method method : clazz.getDeclaredMethods()) {
RuntimeReflection.register(method);
}

public static void debug(String format, Object... args) {
if (debug()) {
System.out.printf("[Debug] " + format + "%n", args);
}
}

public static boolean debug() {
return ImageSingletons.lookup(JUnitPlatformFeature.class).debug;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (c) 2021, 2021 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.graalvm.junit.platform.config.core.NativeImageConfiguration;
import org.graalvm.nativeimage.hosted.RuntimeClassInitialization;
import org.graalvm.nativeimage.hosted.RuntimeReflection;

import java.lang.reflect.Executable;
import java.lang.reflect.Field;

class NativeImageConfigurationImpl implements NativeImageConfiguration {

@Override
public void registerForReflection(Class<?>... classes) {
RuntimeReflection.register(classes);
}

@Override
public void registerForReflection(Executable... methods) {
RuntimeReflection.register(methods);
}

@Override
public void registerForReflection(Field... fields) {
RuntimeReflection.register(fields);
}

@Override
public void initializeAtBuildTime(String... classNames) {
for (String className : classNames) {
Class<?> clazz;
try {
clazz = Class.forName(className);
initializeAtBuildTime(clazz);
} catch (ClassNotFoundException e) {
JUnitPlatformFeature.debug("[Native Image Configuration] Failed to register class for build time initialization: %s Reason: %s", className, e);
}
}
}

@Override
public void initializeAtBuildTime(Class<?>... classes) {
RuntimeClassInitialization.initializeAtBuildTime(classes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,19 @@ public void testPlanExecutionStarted(TestPlan testPlan) {
this.testPlan = testPlan;
}

@Override
public void executionSkipped(TestIdentifier testIdentifier, String reason) {
printTest(testIdentifier, "SKIPPED: " + reason);
}

@Override
public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) {
printTest(testIdentifier, testExecutionResult.getStatus().name());
}

private void printTest(TestIdentifier testIdentifier, String status) {
if (testIdentifier.getParentId().isPresent() && !testIdentifier.isContainer()) {
out.println(LegacyReportingUtils.getClassName(testPlan, testIdentifier) + " > " + testIdentifier.getDisplayName() + " " + testExecutionResult.getStatus().name() + "\n");
out.println(LegacyReportingUtils.getClassName(testPlan, testIdentifier) + " > " + testIdentifier.getDisplayName() + " " + status + "\n");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
* that were executed and generates a file (currently hard coded to
* {@code ./build/test_ids.txt} with Gradle or {@code ./target/test_ids.txt} with Maven)
* that contains test IDs which can be passed to custom launcher to select exactly those test classes.
*
* <p>
* This file should be replaced with org.junit.platform.launcher.listeners.UniqueIdTrackingListener,
* once junit-platform-launcher version 1.8 gets released.
*
Expand All @@ -71,8 +71,17 @@ public class UniqueIdTrackingTestExecutionListener implements TestExecutionListe

private final List<String> uniqueIds = new ArrayList<>();

@Override
public void executionSkipped(TestIdentifier testIdentifier, String reason) {
addTest(testIdentifier);
lazar-mitrovic marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) {
addTest(testIdentifier);
}

private void addTest(TestIdentifier testIdentifier) {
if (testIdentifier.isTest()) {
this.uniqueIds.add(testIdentifier.getUniqueId());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2021, 2021 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.config.core;

import org.graalvm.junit.platform.JUnitPlatformFeature;

import java.lang.reflect.Executable;
import java.lang.reflect.Field;

public interface NativeImageConfiguration {

void registerForReflection(Class<?>... classes);

void registerForReflection(Executable... methods);

void registerForReflection(Field... fields);

default void registerAllClassMembersForReflection(Class<?>... classes) {
for (Class<?> clazz : classes) {
JUnitPlatformFeature.debug("[Native Image Configuration] Registering for reflection: %s", clazz.getName());
registerForReflection(clazz);
registerForReflection(clazz.getDeclaredConstructors());
registerForReflection(clazz.getDeclaredMethods());
registerForReflection(clazz.getDeclaredFields());
}
}

void initializeAtBuildTime(String... classNames);

void initializeAtBuildTime(Class<?>... classes);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2021 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
Expand Down Expand Up @@ -38,12 +38,12 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.example.project;
package org.graalvm.junit.platform.config.core;

public class Calculator {
public interface PluginConfigProvider {

public int add(int a, int b) {
return a + b;
}
void onLoad(NativeImageConfiguration config);

void onTestClassRegistered(Class<?> testClass, NativeImageConfiguration registry);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
void onTestClassRegistered(Class<?> testClass, NativeImageConfiguration registry);
void onTestClassRegistered(Class<?> testClass, NativeImageConfiguration config);


}
Loading