Skip to content

Commit

Permalink
Simplify config providers with Utils.toClasses().
Browse files Browse the repository at this point in the history
  • Loading branch information
fniephaus committed Sep 11, 2023
1 parent 8b21143 commit 00e45b7
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,6 @@ 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
package org.graalvm.junit.platform.config.core;

import org.graalvm.junit.platform.JUnitPlatformFeature;
import org.graalvm.junit.platform.config.util.Utils;

import java.lang.reflect.Executable;
import java.lang.reflect.Field;
Expand All @@ -54,6 +55,10 @@ public interface NativeImageConfiguration {

void registerForReflection(Field... fields);

default void registerAllClassMembersForReflection(String... classNames) {
registerAllClassMembersForReflection(Utils.toClasses(classNames));
};

default void registerAllClassMembersForReflection(Class<?>... classes) {
for (Class<?> clazz : classes) {
JUnitPlatformFeature.debug("[Native Image Configuration] Registering for reflection: %s", clazz.getName());
Expand All @@ -64,7 +69,9 @@ default void registerAllClassMembersForReflection(Class<?>... classes) {
}
}

void initializeAtBuildTime(String... classNames);
default void initializeAtBuildTime(String... classNames) {
initializeAtBuildTime(Utils.toClasses(classNames));
};

void initializeAtBuildTime(Class<?>... classes);
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public class JupiterConfigProvider implements PluginConfigProvider {

@Override
public void onLoad(NativeImageConfiguration config) {
String[] buildTimeInitializedClasses = new String[]{
config.initializeAtBuildTime(
"org.junit.jupiter.engine.config.EnumConfigurationParameterConverter",
"org.junit.jupiter.engine.descriptor.ClassTestDescriptor",
"org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor",
Expand All @@ -84,23 +84,12 @@ public void onLoad(NativeImageConfiguration config) {
// new in Junit 5.10
"org.junit.platform.launcher.core.LauncherConfig",
"org.junit.jupiter.engine.config.InstantiatingConfigurationParameterConverter"
};
for (String className : buildTimeInitializedClasses) {
config.initializeAtBuildTime(className);
}
);

String[] registeredForReflection = {
config.registerAllClassMembersForReflection(
"org.junit.jupiter.engine.extension.TimeoutExtension$ExecutorResource",
"org.junit.jupiter.engine.extension.TimeoutInvocationFactory$SingleThreadExecutorResource"
};
for (String className : registeredForReflection) {
try {
Class <?> executor = Class.forName(className);
config.registerAllClassMembersForReflection(executor);
} catch (ClassNotFoundException e) {
debug("Failed to register class for reflection. Reason: %s", e);
}
}
);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class PlatformConfigProvider implements PluginConfigProvider {

@Override
public void onLoad(NativeImageConfiguration config) {
String[] buildTimeInitializedClasses = new String[] {
config.initializeAtBuildTime(
"org.junit.platform.launcher.TestIdentifier",
"org.junit.platform.launcher.core.InternalTestPlan",
"org.junit.platform.commons.util.StringUtils",
Expand All @@ -61,10 +61,7 @@ public void onLoad(NativeImageConfiguration config) {
"org.junit.platform.commons.util.ReflectionUtils",
// https://github.com/graalvm/native-build-tools/issues/300
"org.junit.platform.reporting.open.xml.OpenTestReportGeneratingListener"
};
for (String className : buildTimeInitializedClasses) {
config.initializeAtBuildTime(className);
}
);

try {
/* Verify if the core JUnit Platform test class is available on the classpath */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (c) 2023, 2023, 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.util;

import org.graalvm.junit.platform.JUnitPlatformFeature;

import java.util.Arrays;

public class Utils {
public static Class<?>[] toClasses(String... classNames) {
return Arrays.stream(classNames).map(className -> {
Class<?> clazz = null;
try {
clazz = Class.forName(className);
} catch (ClassNotFoundException e) {
JUnitPlatformFeature.debug("[Native Image Configuration] Failed to resolve: %s Reason: %s", className, e);
}
return clazz;
}).filter(c -> c != null).toArray(Class[]::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class VintageConfigProvider implements PluginConfigProvider {

@Override
public void onLoad(NativeImageConfiguration config) {
String[] buildTimeInitializedClasses = new String[]{
config.initializeAtBuildTime(
"org.junit.vintage.engine.descriptor.RunnerTestDescriptor",
"org.junit.vintage.engine.support.UniqueIdReader",
"org.junit.vintage.engine.support.UniqueIdStringifier",
Expand All @@ -57,10 +57,7 @@ public void onLoad(NativeImageConfiguration config) {
"org.junit.runners.JUnit4",
/* Workaround until we can register serializable classes from a native-image feature */
"org.junit.runner.Result"
};
for (String className : buildTimeInitializedClasses) {
config.initializeAtBuildTime(className);
}
);
}

@Override
Expand Down

0 comments on commit 00e45b7

Please sign in to comment.