Skip to content

Commit 7c2efd6

Browse files
committed
Update Gradle tests to skip JVM tests execution
1 parent 9c253a4 commit 7c2efd6

File tree

5 files changed

+65
-46
lines changed

5 files changed

+65
-46
lines changed

native-maven-plugin/src/main/java/org/graalvm/buildtools/maven/MergeAgentFilesMojo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ private void mergeForGivenDir(String agentOutputDirectory) throws MojoExecutionE
116116
File baseDir = new File(agentOutputDirectory);
117117
if (baseDir.exists()) {
118118
List<File> sessionDirectories = sessionDirectoriesFrom(baseDir.listFiles()).collect(Collectors.toList());
119-
if (sessionDirectories.size() == 0) {
119+
if (sessionDirectories.isEmpty()) {
120120
sessionDirectories = Collections.singletonList(baseDir);
121121
}
122122

native-maven-plugin/src/main/java/org/graalvm/buildtools/maven/NativeExtension.java

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,24 +55,28 @@
5555
import org.graalvm.buildtools.agent.AgentConfiguration;
5656
import org.graalvm.buildtools.utils.AgentUtils;
5757
import org.graalvm.buildtools.utils.SharedConstants;
58+
import org.graalvm.buildtools.utils.Utils;
5859

5960
import java.io.File;
6061
import java.util.ArrayList;
6162
import java.util.Collections;
6263
import java.util.List;
64+
import java.util.Objects;
6365
import java.util.function.BiConsumer;
6466
import java.util.function.Consumer;
6567
import java.util.stream.Collectors;
6668

6769
import static org.graalvm.buildtools.utils.NativeImageConfigurationUtils.getNativeImage;
6870

6971
/**
70-
* This extension is responsible for configuring the Surefire plugin to enable
72+
* This extension is responsible for configuring the Surefire and the Failsafe plugins to enable
7173
* the JUnit Platform test listener and registering the native dependency transparently.
7274
*/
7375
@Component(role = AbstractMavenLifecycleParticipant.class, hint = "native-build-tools")
7476
public class NativeExtension extends AbstractMavenLifecycleParticipant implements LogEnabled {
7577

78+
private static final String SKIP_JVM_TESTS = "skipJVMTests";
79+
7680
private static final String JUNIT_PLATFORM_LISTENERS_UID_TRACKING_ENABLED = "junit.platform.listeners.uid.tracking.enabled";
7781
private static final String JUNIT_PLATFORM_DRY_RUN_ENABLED = "junit.platform.execution.dryRun.enabled";
7882
private static final String JUNIT_PLATFORM_LISTENERS_UID_TRACKING_OUTPUT_DIR = "junit.platform.listeners.uid.tracking.output.dir";
@@ -135,11 +139,16 @@ public void afterProjectsRead(MavenSession session) {
135139
throw new RuntimeException(e);
136140
}
137141

142+
boolean skipJVMTests = shouldSkipJVMTests(configurationRoot, session);
143+
if (skipJVMTests && agent.isEnabled()) {
144+
throw new IllegalStateException("Native Image Agent and skipJVMTests cannot be used at the same time.");
145+
}
146+
138147
// Test configuration
139148
List<String> plugins = List.of("maven-surefire-plugin", "maven-failsafe-plugin");
140149
for (String pluginName : plugins) {
141150
withPlugin(build, pluginName, plugin -> {
142-
configureJunitListener(plugin, testIdsDir);
151+
configureJunitListener(plugin, testIdsDir, skipJVMTests);
143152
if (agent.isEnabled()) {
144153
List<String> agentOptions = agent.getAgentCommandLine();
145154
configureAgentForPlugin(plugin, buildAgentArgument(target, Context.test, agentOptions));
@@ -189,6 +198,16 @@ public void afterProjectsRead(MavenSession session) {
189198
}
190199
}
191200

201+
private static boolean shouldSkipJVMTests(Xpp3Dom configurationRoot, MavenSession session) {
202+
String fromCommandLine = session.getSystemProperties().getProperty(SKIP_JVM_TESTS);
203+
if (fromCommandLine == null) {
204+
Boolean skipNode = Utils.parseBooleanNode(configurationRoot, SKIP_JVM_TESTS);
205+
return Objects.requireNonNullElse(skipNode, false);
206+
}
207+
208+
return Boolean.parseBoolean(fromCommandLine);
209+
}
210+
192211
private static void setupMergeAgentFiles(PluginExecution exec, Xpp3Dom configuration, Context context) {
193212
List<String> goals = new ArrayList<>();
194213
goals.add("merge-agent-files");
@@ -218,15 +237,17 @@ private static void configureAgentForPlugin(Plugin plugin, String agentArgument)
218237
});
219238
}
220239

221-
private static void configureJunitListener(Plugin surefirePlugin, String testIdsDir) {
222-
updatePluginConfiguration(surefirePlugin, (exec, configuration) -> {
240+
private static void configureJunitListener(Plugin plugin, String testIdsDir, boolean skipJVMTests) {
241+
updatePluginConfiguration(plugin, (exec, configuration) -> {
223242
Xpp3Dom systemProperties = findOrAppend(configuration, "systemProperties");
224243

225244
Xpp3Dom junitTracking = findOrAppend(systemProperties, JUNIT_PLATFORM_LISTENERS_UID_TRACKING_ENABLED);
226245
junitTracking.setValue("true");
227246

228-
Xpp3Dom junitDryRun = findOrAppend(systemProperties, JUNIT_PLATFORM_DRY_RUN_ENABLED);
229-
junitDryRun.setValue("true");
247+
if (skipJVMTests) {
248+
Xpp3Dom junitDryRun = findOrAppend(systemProperties, JUNIT_PLATFORM_DRY_RUN_ENABLED);
249+
junitDryRun.setValue("true");
250+
}
230251

231252
Xpp3Dom testIdsProperty = findOrAppend(systemProperties, JUNIT_PLATFORM_LISTENERS_UID_TRACKING_OUTPUT_DIR);
232253
testIdsProperty.setValue(testIdsDir);

native-maven-plugin/src/main/java/org/graalvm/buildtools/maven/NativeTestMojo.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ public class NativeTestMojo extends AbstractNativeImageMojo {
9999
@Parameter(property = "skipNativeTests", defaultValue = "false")
100100
private boolean skipNativeTests;
101101

102+
// this parameter is only used in NativeExtension to configure Surefire and Failsafe plugins to skip executing tests on JVM
103+
@Parameter(property = "skipJVMTests", defaultValue = "false")
104+
private boolean skipJVMTests;
105+
102106
@Override
103107
protected void populateApplicationClasspath() throws MojoExecutionException {
104108
super.populateApplicationClasspath();

native-maven-plugin/src/main/java/org/graalvm/buildtools/utils/AgentUtils.java

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,13 @@
5656
import java.util.List;
5757
import java.util.stream.Collectors;
5858

59-
import static org.graalvm.buildtools.utils.Utils.parseBoolean;
60-
6159
public abstract class AgentUtils {
6260

61+
private static final String STANDARD = "standard";
62+
private static final String CONDITIONAL = "conditional";
63+
private static final String DIRECT = "direct";
64+
private static final String DISABLED = "disabled";
65+
6366
public static AgentMode getAgentMode(Xpp3Dom agent) throws Exception {
6467
Xpp3Dom defaultModeNode = Xpp3DomParser.getTagByName(agent, "defaultMode");
6568
// if default mode is not provided in pom, return Standard mode
@@ -72,13 +75,13 @@ public static AgentMode getAgentMode(Xpp3Dom agent) throws Exception {
7275
AgentMode agentMode;
7376
String mode = defaultModeNode.getValue();
7477
switch (mode.toLowerCase()) {
75-
case "standard":
76-
agentMode = new StandardAgentMode();
77-
break;
78-
case "disabled":
78+
case DISABLED:
7979
agentMode = new DisabledAgentMode();
8080
break;
81-
case "conditional":
81+
case STANDARD:
82+
agentMode = new StandardAgentMode();
83+
break;
84+
case CONDITIONAL:
8285
// conditional mode needs few more options declared in xml
8386
if (agentModes == null) {
8487
throw new RuntimeException("Tag <modes> not provided in agent configuration.");
@@ -91,12 +94,12 @@ public static AgentMode getAgentMode(Xpp3Dom agent) throws Exception {
9194
throw new Exception("UserCodeFilterPath must be provided in agent configuration");
9295
}
9396

94-
Boolean parallel = parseBooleanNode(agentModes, "parallel");
97+
Boolean parallel = Utils.parseBooleanNode(agentModes, "parallel");
9598
agentMode = new ConditionalAgentMode(userCodeFilterPathNode.getValue(),
9699
extraFilterPathNode != null ? extraFilterPathNode.getValue() : "",
97100
parallel == null ? false : parallel);
98101
break;
99-
case "direct":
102+
case DIRECT:
100103
// direct mode is given
101104
if (agentModes == null) {
102105
throw new RuntimeException("Tag <modes> not provided in agent configuration.");
@@ -138,11 +141,11 @@ public static AgentConfiguration collectAgentProperties(MavenSession session, Xp
138141

139142
ArrayList<String> callerFilterFiles = (ArrayList<String>) getFilterFiles(options, "callerFilterFiles");
140143
ArrayList<String> accessFilterFiles = (ArrayList<String>) getFilterFiles(options, "accessFilterFiles");
141-
Boolean builtinCallerFilter = parseBooleanNode(options, "builtinCallerFilter");
142-
Boolean builtinHeuristicFilter = parseBooleanNode(options, "builtinHeuristicFilter");
143-
Boolean enableExperimentalPredefinedClasses = parseBooleanNode(options, "enableExperimentalPredefinedClasses");
144-
Boolean enableExperimentalUnsafeAllocationTracing = parseBooleanNode(options, "enableExperimentalUnsafeAllocationTracing");
145-
Boolean trackReflectionMetadata = parseBooleanNode(options, "trackReflectionMetadata");
144+
Boolean builtinCallerFilter = Utils.parseBooleanNode(options, "builtinCallerFilter");
145+
Boolean builtinHeuristicFilter = Utils.parseBooleanNode(options, "builtinHeuristicFilter");
146+
Boolean enableExperimentalPredefinedClasses = Utils.parseBooleanNode(options, "enableExperimentalPredefinedClasses");
147+
Boolean enableExperimentalUnsafeAllocationTracing = Utils.parseBooleanNode(options, "enableExperimentalUnsafeAllocationTracing");
148+
Boolean trackReflectionMetadata = Utils.parseBooleanNode(options, "trackReflectionMetadata");
146149

147150
AgentMode mode;
148151
try {
@@ -175,7 +178,7 @@ private static Boolean isAgentEnabledInCmd(MavenSession session) {
175178
String systemProperty = session.getSystemProperties().getProperty("agent");
176179
if (systemProperty != null) {
177180
// -Dagent=[true|false] overrides configuration in the POM.
178-
return parseBoolean("agent system property", systemProperty);
181+
return Boolean.parseBoolean(systemProperty);
179182
}
180183

181184
return null;
@@ -187,7 +190,7 @@ private static boolean isAgentEnabled(MavenSession session, Xpp3Dom agent) {
187190
return cmdEnable;
188191
}
189192

190-
Boolean val = parseBooleanNode(agent, "enabled");
193+
Boolean val = Utils.parseBooleanNode(agent, "enabled");
191194
if (val == null) {
192195
return false;
193196
}
@@ -210,18 +213,4 @@ private static List<String> getFilterFiles(Xpp3Dom root, String type) {
210213
.map(Xpp3Dom::getValue)
211214
.collect(Collectors.toCollection(ArrayList::new));
212215
}
213-
214-
private static Boolean parseBooleanNode(Xpp3Dom root, String name) {
215-
if (root == null) {
216-
return null;
217-
}
218-
219-
Xpp3Dom node = Xpp3DomParser.getTagByName(root, name);
220-
if (node == null) {
221-
// if node is not provided, default value is false
222-
return null;
223-
}
224-
225-
return Utils.parseBoolean("<" + name + ">", node.getValue());
226-
}
227216
}

native-maven-plugin/src/main/java/org/graalvm/buildtools/utils/Utils.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,22 @@
4040
*/
4141
package org.graalvm.buildtools.utils;
4242

43+
import org.codehaus.plexus.util.xml.Xpp3Dom;
44+
4345
public abstract class Utils {
44-
public static boolean parseBoolean(String description, String value) {
45-
value = assertNotEmptyAndTrim(value, description + " must have a value").toLowerCase();
46-
switch (value) {
47-
case "true":
48-
return true;
49-
case "false":
50-
return false;
51-
default:
52-
throw new IllegalStateException(description + " must have a value of 'true' or 'false'");
46+
47+
public static Boolean parseBooleanNode(Xpp3Dom root, String name) {
48+
if (root == null) {
49+
return null;
5350
}
51+
52+
Xpp3Dom node = Xpp3DomParser.getTagByName(root, name);
53+
if (node == null) {
54+
// if node is not provided, default value is false
55+
return null;
56+
}
57+
58+
return Boolean.parseBoolean(node.getValue());
5459
}
5560

5661
public static String assertNotEmptyAndTrim(String input, String message) {

0 commit comments

Comments
 (0)