Skip to content

Allow the test classes directory to be configured for the NativeTestMojo #757

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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 @@ -11,6 +11,7 @@ class IntegrationTest extends AbstractGraalVMMavenFunctionalTest {
buildSucceeded
file("target/failsafe-reports").exists()
file("target/failsafe-reports/org.graalvm.demo.CalculatorTestIT.txt").readLines().any(line -> line.contains("Tests run: 6, Failures: 0, Errors: 0, Skipped: 0"))
outputContains "Using configuration from maven-failsafe-plugin"
outputContains "[junit-platform-native] Running in 'test listener' mode"
outputContains """
[ 3 containers found ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,59 @@ class JavaApplicationWithTestsFunctionalTest extends AbstractGraalVMMavenFunctio

then:
buildSucceeded
outputContains "Using configuration from maven-surefire-plugin"
outputContains "[junit-platform-native] Running in 'test listener' mode"
outputContains """
[ 3 containers found ]
[ 0 containers skipped ]
[ 3 containers started ]
[ 0 containers aborted ]
[ 3 containers successful ]
[ 0 containers failed ]
[ 6 tests found ]
[ 0 tests skipped ]
[ 6 tests started ]
[ 0 tests aborted ]
[ 6 tests successful ]
[ 0 tests failed ]
""".trim()
}

def "can run integration tests in a native image with the Maven plugin"() {
withSample("java-application-with-tests")

when:
mvn '-Pnative', '-DquickBuild', 'verify'

then:
buildSucceeded
outputContains "Using configuration from maven-failsafe-plugin"
outputContains "[junit-platform-native] Running in 'test listener' mode"
outputContains """
[ 2 containers found ]
[ 0 containers skipped ]
[ 2 containers started ]
[ 0 containers aborted ]
[ 2 containers successful ]
[ 0 containers failed ]
[ 1 tests found ]
[ 0 tests skipped ]
[ 1 tests started ]
[ 0 tests aborted ]
[ 1 tests successful ]
[ 0 tests failed ]
""".trim()
}

def "can run tests in src/main/java in a native image with the Maven plugin"() {
withSample("java-application-with-tests-in-src-main")

when:
mvn '-Pnative', '-DquickBuild', 'test'

then:
buildSucceeded
outputContains "Using configuration from maven-surefire-plugin"
outputContains "[junit-platform-native] Running in 'test listener' mode"
outputContains """
[ 3 containers found ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ public void enableLogging(Logger logger) {
*/
enum Context {main, test}

static String testIdsDirectory(String baseDir) {
return baseDir + File.separator + "test-ids";
static String testIdsDirectory(String baseDir, String pluginName) {
return baseDir + File.separator + pluginName + "-test-ids";
}

static String buildAgentArgument(String baseDir, Context context, List<String> agentOptions) {
Expand Down Expand Up @@ -124,7 +124,6 @@ public void afterProjectsRead(MavenSession session) {
Build build = project.getBuild();
withPlugin(build, "native-maven-plugin", nativePlugin -> {
String target = build.getDirectory();
String testIdsDir = testIdsDirectory(target);

Xpp3Dom configurationRoot = (Xpp3Dom) nativePlugin.getConfiguration();
AgentConfiguration agent;
Expand All @@ -138,6 +137,7 @@ public void afterProjectsRead(MavenSession session) {
List<String> plugins = List.of("maven-surefire-plugin", "maven-failsafe-plugin");
for (String pluginName : plugins) {
withPlugin(build, pluginName, plugin -> {
String testIdsDir = testIdsDirectory(target, plugin.getArtifactId());
configureJunitListener(plugin, testIdsDir);
if (agent.isEnabled()) {
List<String> agentOptions = agent.getAgentCommandLine();
Expand Down Expand Up @@ -217,8 +217,8 @@ private static void configureAgentForPlugin(Plugin plugin, String agentArgument)
});
}

private static void configureJunitListener(Plugin surefirePlugin, String testIdsDir) {
updatePluginConfiguration(surefirePlugin, (exec, configuration) -> {
private static void configureJunitListener(Plugin plugin, String testIdsDir) {
updatePluginConfiguration(plugin, (exec, configuration) -> {
Xpp3Dom systemProperties = findOrAppend(configuration, "systemProperties");
Xpp3Dom junitTracking = findOrAppend(systemProperties, JUNIT_PLATFORM_LISTENERS_UID_TRACKING_ENABLED);
Xpp3Dom testIdsProperty = findOrAppend(systemProperties, JUNIT_PLATFORM_LISTENERS_UID_TRACKING_OUTPUT_DIR);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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.buildtools.maven;

import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.ResolutionScope;

/**
* This goal builds and runs native integration tests.
*
* @author Jonathan Knight
*/
@Mojo(name = NativeIntegrationTestMojo.INTEGRATION_TEST_GOAL,
defaultPhase = LifecyclePhase.INTEGRATION_TEST, threadSafe = true,
requiresDependencyResolution = ResolutionScope.TEST,
requiresDependencyCollection = ResolutionScope.TEST)
public class NativeIntegrationTestMojo extends NativeTestMojo {
Copy link
Member

Choose a reason for hiding this comment

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

@dnestoro do we need to document this? Also, could we execute only native integration tests (once the JUnit issues are fixed)? What would be the command for that?

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.apache.maven.artifact.Artifact;
import org.apache.maven.model.FileSet;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.PluginExecution;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
Expand Down Expand Up @@ -88,21 +89,39 @@
*
* @author Sebastien Deleuze
*/
@Mojo(name = "test", defaultPhase = LifecyclePhase.TEST, threadSafe = true,
@Mojo(name = NativeTestMojo.TEST_GOAL, defaultPhase = LifecyclePhase.TEST, threadSafe = true,
requiresDependencyResolution = ResolutionScope.TEST,
requiresDependencyCollection = ResolutionScope.TEST)
public class NativeTestMojo extends AbstractNativeImageMojo {

/**
* The test goal for this plugin.
*/
public static final String TEST_GOAL = "test";

/**
* The integration-test goal for this plugin.
*/
public static final String INTEGRATION_TEST_GOAL = "integration-test";

@Parameter(property = "skipTests", defaultValue = "false")
private boolean skipTests;

@Parameter(property = "skipNativeTests", defaultValue = "false")
private boolean skipNativeTests;

/**
* The location of the test classes.
* <p>
* This field will be set at execution time from either the Surefire or
* Failsafe plugin configurations.
*/
private String testClassesDirectory;

@Override
protected void populateApplicationClasspath() throws MojoExecutionException {
super.populateApplicationClasspath();
imageClasspath.add(Paths.get(project.getBuild().getTestOutputDirectory()));
imageClasspath.add(Path.of(testClassesDirectory));
Copy link
Member

@vjovanov vjovanov Aug 21, 2025

Choose a reason for hiding this comment

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

@melix are we handling this case in Gradle properly, i.e. the testClassesDirs?

project.getBuild()
.getTestResources()
.stream()
Expand Down Expand Up @@ -146,6 +165,9 @@ public void execute() throws MojoExecutionException {
logger.info("Skipping native-image tests (parameter 'skipTests' or 'skipNativeTests' is true).");
return;
}

configureEnvironment();

if (!hasTests()) {
logger.info("Skipped native-image tests since there are no test classes.");
return;
Expand All @@ -159,7 +181,6 @@ public void execute() throws MojoExecutionException {
logger.info("Initializing project: " + project.getName());
logger.info("====================");

configureEnvironment();
buildArgs.add("--features=org.graalvm.junit.platform.JUnitPlatformFeature");

/* in version 5.12.0 JUnit added initialize-at-build-time properties files which we need to exclude */
Expand All @@ -168,8 +189,7 @@ public void execute() throws MojoExecutionException {
if (systemProperties == null) {
systemProperties = new HashMap<>();
}
systemProperties.put("junit.platform.listeners.uid.tracking.output.dir",
NativeExtension.testIdsDirectory(outputDirectory.getAbsolutePath()));

if (runtimeArgs == null) {
runtimeArgs = new ArrayList<>();
}
Expand All @@ -181,25 +201,112 @@ public void execute() throws MojoExecutionException {
runNativeTests(outputDirectory.toPath().resolve(NATIVE_TESTS_EXE));
}

private void configureEnvironment() {
List<Plugin> plugins = new ArrayList<>();
protected void configureEnvironment() {
// set the default test classes location
testClassesDirectory = project.getBuild().getTestOutputDirectory();

Plugin surefire = project.getPlugin("org.apache.maven.plugins:maven-surefire-plugin");
Plugin failsafe = project.getPlugin("org.apache.maven.plugins:maven-failsafe-plugin");
String currentGoal = mojoExecution.getGoal();
boolean testPluginProcessed = false;

if (surefire != null) {
plugins.add(surefire);
testPluginProcessed = processTestPluginConfig(surefire, currentGoal);
}

Plugin failsafe = project.getPlugin("org.apache.maven.plugins:maven-failsafe-plugin");
if (failsafe != null) {
plugins.add(failsafe);
if (!testPluginProcessed && failsafe != null) {
// Surefire was not configured with an execution for the current goal, so try Failsafe
testPluginProcessed = processTestPluginConfig(failsafe, currentGoal);
}

if (!testPluginProcessed) {
// neither Surefire nor Failsafe has an execution for the current goal,
// so use the configuration for whichever plugin's default goal matches
// the current goal
if (mojoExecution.getGoal().equals(TEST_GOAL) && surefire != null) {
// the current goal is "test" which is Surefire's default goal
processTestPluginConfig(surefire, null);
getLog().info("Using configuration from " + surefire.getArtifactId());
} else if (mojoExecution.getGoal().equals(INTEGRATION_TEST_GOAL) && failsafe != null) {
// the current goal is "integration-test" which is Failsafe's default goal
processTestPluginConfig(failsafe, null);
getLog().info("Using configuration from " + failsafe.getArtifactId());
}
}
}

/**
* Process the configuration from the Surefire or Failsafe plugins.
* <p>
* This method will check to see whether the plugin has an execution
* matching the goa for this plugin. If it does the plugin's configuration
* and the configuration for the matching execution will be processed.
* If the plugin has no executions, then just its global configuration
* will be processed.
*
* @param plugin the Surefire or Failsafe plugin
* @param currentGoal the current goal being executed
*/
private boolean processTestPluginConfig(Plugin plugin, String currentGoal)
{
List<PluginExecution> executions = plugin.getExecutions();
boolean found = false;
if (!executions.isEmpty()) {
for (PluginExecution execution : executions) {
if (execution.getGoals().contains(currentGoal)) {
processTestPlugin(plugin, execution);
found = true;
break;
}
}
}
return found;
}

/**
* Process the configuration from the Surefire or Failsafe plugins.
*
* @param plugin the Surefire or Failsafe plugin configuration
* @param execution the plugin execution to use for configuration
*/
private void processTestPlugin(Plugin plugin, PluginExecution execution) {
processTestPluginConfig(plugin.getConfiguration());
if (execution != null) {
processTestPluginConfig(execution.getConfiguration());
}
systemProperties.put("junit.platform.listeners.uid.tracking.output.dir",
NativeExtension.testIdsDirectory(outputDirectory.getAbsolutePath(), plugin.getArtifactId()));
getLog().info("Using configuration from " + plugin.getArtifactId()
+ (execution != null ? ", execution id " + execution.getId() : ""));
}

/**
* Process the configuration from the Surefire or Failsafe plugins.
*
* @param configuration the Surefire or Failsafe plugin configuration
*/
private void processTestPluginConfig(Object configuration) {
if (configuration instanceof Xpp3Dom) {
Xpp3Dom dom = (Xpp3Dom) configuration;
applyPluginProperties(dom.getChild("environmentVariables"), environment);
applyPluginProperties(dom.getChild("systemPropertyVariables"), systemProperties);
setTestClassesDirectory(dom.getChild("testClassesDirectory"));
}
}

for (Plugin plugin : plugins) {
Object configuration = plugin.getConfiguration();
if (configuration instanceof Xpp3Dom) {
Xpp3Dom dom = (Xpp3Dom) configuration;
applyPluginProperties(dom.getChild("environmentVariables"), environment);
applyPluginProperties(dom.getChild("systemPropertyVariables"), systemProperties);
/**
* Set the test classes directory from the testClassesDirectory configuration {@link Xpp3Dom}.
* <p>
* The {@link #testClassesDirectory} field is only set if the dom is not {@code null}
* and it contains is non-blank value.
*
* @param dom the testClassesDirectory configuration {@link Xpp3Dom}.
*/
protected void setTestClassesDirectory(Xpp3Dom dom) {
if (dom != null) {
String value = dom.getValue();
if (value != null && !value.isBlank()) {
testClassesDirectory = value;
}
}
}
Expand All @@ -217,7 +324,7 @@ private void applyPluginProperties(Xpp3Dom pluginProperty, Map<String, String> v
}

private boolean hasTests() {
Path testOutputPath = Paths.get(project.getBuild().getTestOutputDirectory());
Path testOutputPath = Path.of(testClassesDirectory);
if (Files.exists(testOutputPath) && Files.isDirectory(testOutputPath)) {
try (Stream<Path> testClasses = Files.walk(testOutputPath)) {
return testClasses.anyMatch(p -> p.getFileName().toString().endsWith(".class"));
Expand Down
2 changes: 1 addition & 1 deletion samples/integration-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
<execution>
<id>test-native</id>
<goals>
<goal>test</goal>
<goal>integration-test</goal>
</goals>
<phase>verify</phase>
</execution>
Expand Down
Loading