Skip to content

Commit e725ce6

Browse files
committed
Add support for integration tests through Maven
1 parent 54b7ba6 commit e725ce6

File tree

6 files changed

+241
-32
lines changed

6 files changed

+241
-32
lines changed

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,16 @@ public void afterProjectsRead(MavenSession session) {
135135
}
136136

137137
// Test configuration
138-
withPlugin(build, "maven-surefire-plugin", surefirePlugin -> {
139-
configureJunitListener(surefirePlugin, testIdsDir);
140-
if (agent.isEnabled()) {
141-
List<String> agentOptions = agent.getAgentCommandLine();
142-
configureAgentForSurefire(surefirePlugin, buildAgentArgument(target, Context.test, agentOptions));
143-
}
144-
});
138+
List<String> plugins = List.of("maven-surefire-plugin", "maven-failsafe-plugin");
139+
for (String pluginName : plugins) {
140+
withPlugin(build, pluginName, plugin -> {
141+
configureJunitListener(plugin, testIdsDir);
142+
if (agent.isEnabled()) {
143+
List<String> agentOptions = agent.getAgentCommandLine();
144+
configureAgentForPlugin(plugin, buildAgentArgument(target, Context.test, agentOptions));
145+
}
146+
});
147+
}
145148

146149
// Main configuration
147150
if (agent.isEnabled()) {
@@ -202,8 +205,8 @@ private static void withPlugin(Build build, String artifactId, Consumer<? super
202205
.ifPresent(consumer);
203206
}
204207

205-
private static void configureAgentForSurefire(Plugin surefirePlugin, String agentArgument) {
206-
updatePluginConfiguration(surefirePlugin, (exec, configuration) -> {
208+
private static void configureAgentForPlugin(Plugin plugin, String agentArgument) {
209+
updatePluginConfiguration(plugin, (exec, configuration) -> {
207210
Xpp3Dom systemProperties = findOrAppend(configuration, "systemProperties");
208211
Xpp3Dom agent = findOrAppend(systemProperties, NATIVEIMAGE_IMAGECODE);
209212
agent.setValue("agent");

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

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
import java.util.HashMap;
7575
import java.util.HashSet;
7676
import java.util.List;
77+
import java.util.Map;
7778
import java.util.Set;
7879
import java.util.stream.Collectors;
7980
import java.util.stream.Stream;
@@ -176,32 +177,36 @@ public void execute() throws MojoExecutionException {
176177
}
177178

178179
private void configureEnvironment() {
179-
// inherit from surefire mojo
180-
Plugin plugin = project.getPlugin("org.apache.maven.plugins:maven-surefire-plugin");
181-
if (plugin != null) {
180+
List<Plugin> plugins = new ArrayList<>();
181+
182+
Plugin surefire = project.getPlugin("org.apache.maven.plugins:maven-surefire-plugin");
183+
if (surefire != null) {
184+
plugins.add(surefire);
185+
}
186+
187+
Plugin failsafe = project.getPlugin("org.apache.maven.plugins:maven-failsafe-plugin");
188+
if (failsafe != null) {
189+
plugins.add(failsafe);
190+
}
191+
192+
for (Plugin plugin : plugins) {
182193
Object configuration = plugin.getConfiguration();
183194
if (configuration instanceof Xpp3Dom) {
184195
Xpp3Dom dom = (Xpp3Dom) configuration;
185-
Xpp3Dom environmentVariables = dom.getChild("environmentVariables");
186-
if (environmentVariables != null) {
187-
Xpp3Dom[] children = environmentVariables.getChildren();
188-
if (environment == null) {
189-
environment = new HashMap<>(children.length);
190-
}
191-
for (Xpp3Dom child : children) {
192-
environment.put(child.getName(), child.getValue());
193-
}
194-
}
195-
Xpp3Dom systemProps = dom.getChild("systemPropertyVariables");
196-
if (systemProps != null) {
197-
Xpp3Dom[] children = systemProps.getChildren();
198-
if (systemProperties == null) {
199-
systemProperties = new HashMap<>(children.length);
200-
}
201-
for (Xpp3Dom child : children) {
202-
systemProperties.put(child.getName(), child.getValue());
203-
}
204-
}
196+
applyPluginProperties(dom.getChild("environmentVariables"), environment);
197+
applyPluginProperties(dom.getChild("systemPropertyVariables"), systemProperties);
198+
}
199+
}
200+
}
201+
202+
private void applyPluginProperties(Xpp3Dom pluginProperty, Map<String, String> values) {
203+
if (pluginProperty != null) {
204+
Xpp3Dom[] children = pluginProperty.getChildren();
205+
if (values == null) {
206+
values = new HashMap<>(children.length);
207+
}
208+
for (Xpp3Dom child : children) {
209+
values.put(child.getName(), child.getValue());
205210
}
206211
}
207212
}

samples/integration-test/pom.xml

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
4+
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5+
6+
The Universal Permissive License (UPL), Version 1.0
7+
8+
Subject to the condition set forth below, permission is hereby granted to any
9+
person obtaining a copy of this software, associated documentation and/or
10+
data (collectively the "Software"), free of charge and under any and all
11+
copyright rights in the Software, and any and all patent rights owned or
12+
freely licensable by each licensor hereunder covering either (i) the
13+
unmodified Software as contributed to or provided by such licensor, or (ii)
14+
the Larger Works (as defined below), to deal in both
15+
16+
(a) the Software, and
17+
18+
(b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
19+
one is included with the Software each a "Larger Work" to which the Software
20+
is contributed by such licensors),
21+
22+
without restriction, including without limitation the rights to copy, create
23+
derivative works of, display, perform, and distribute the Software and make,
24+
use, sell, offer for sale, import, export, have made, and have sold the
25+
Software and the Larger Work(s), and to sublicense the foregoing rights on
26+
either these or other terms.
27+
28+
This license is subject to the following condition:
29+
30+
The above copyright notice and either this complete permission notice or at a
31+
minimum a reference to the UPL must be included in all copies or substantial
32+
portions of the Software.
33+
34+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
37+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
38+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
39+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
40+
SOFTWARE.
41+
-->
42+
43+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
44+
http://maven.apache.org/xsd/maven-4.0.0.xsd">
45+
<modelVersion>4.0.0</modelVersion>
46+
47+
<groupId>org.graalvm.buildtools.examples</groupId>
48+
<artifactId>maven</artifactId>
49+
<version>1.0.0-SNAPSHOT</version>
50+
51+
<properties>
52+
<java.version>1.8</java.version>
53+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
54+
<junit.jupiter.version>5.13.0</junit.jupiter.version>
55+
<native.maven.plugin.version>0.11.0-SNAPSHOT</native.maven.plugin.version>
56+
<junit.platform.native.version>0.11.0-SNAPSHOT</junit.platform.native.version>
57+
<imageName>example-app</imageName>
58+
<mainClass>org.graalvm.demo.Application</mainClass>
59+
</properties>
60+
61+
<dependencies>
62+
<dependency>
63+
<groupId>org.junit.jupiter</groupId>
64+
<artifactId>junit-jupiter</artifactId>
65+
<version>${junit.jupiter.version}</version>
66+
<scope>test</scope>
67+
</dependency>
68+
</dependencies>
69+
70+
<profiles>
71+
<profile>
72+
<id>native</id>
73+
<build>
74+
<plugins>
75+
<plugin>
76+
<groupId>org.apache.maven.plugins</groupId>
77+
<artifactId>maven-failsafe-plugin</artifactId>
78+
<version>3.5.3</version>
79+
<executions>
80+
<execution>
81+
<goals>
82+
<goal>integration-test</goal>
83+
<goal>verify</goal>
84+
</goals>
85+
</execution>
86+
</executions>
87+
</plugin>
88+
</plugins>
89+
</build>
90+
</profile>
91+
</profiles>
92+
93+
<build>
94+
<plugins>
95+
<plugin>
96+
<groupId>org.apache.maven.plugins</groupId>
97+
<artifactId>maven-surefire-plugin</artifactId>
98+
<version>3.0.0-M5</version>
99+
</plugin>
100+
<plugin>
101+
<groupId>org.apache.maven.plugins</groupId>
102+
<artifactId>maven-failsafe-plugin</artifactId>
103+
<version>3.5.3</version>
104+
</plugin>
105+
<plugin>
106+
<groupId>org.codehaus.mojo</groupId>
107+
<artifactId>exec-maven-plugin</artifactId>
108+
<version>3.0.0</version>
109+
<executions>
110+
<execution>
111+
<id>java</id>
112+
<goals>
113+
<goal>java</goal>
114+
</goals>
115+
<configuration>
116+
<mainClass>${mainClass}</mainClass>
117+
</configuration>
118+
</execution>
119+
<execution>
120+
<id>native</id>
121+
<goals>
122+
<goal>exec</goal>
123+
</goals>
124+
<configuration>
125+
<executable>${project.build.directory}/${imageName}</executable>
126+
<workingDirectory>${project.build.directory}</workingDirectory>
127+
</configuration>
128+
</execution>
129+
</executions>
130+
</plugin>
131+
</plugins>
132+
</build>
133+
134+
</project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.graalvm.demo;
2+
3+
public class Application {
4+
static String getMessage() {
5+
return "Hello, native!";
6+
}
7+
8+
public static void main(String[] args) {
9+
System.out.println(getMessage());
10+
}
11+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.graalvm.demo;
2+
3+
public class Calculator {
4+
5+
public Calculator() {
6+
if (System.getenv("TEST_ENV") != null) {
7+
System.out.println("TEST_ENV = " + System.getenv("TEST_ENV"));
8+
}
9+
if (System.getProperty("test-property") != null) {
10+
System.out.println("test-property = " + System.getProperty("test-property"));
11+
}
12+
}
13+
14+
public int add(int a, int b) {
15+
return a + b;
16+
}
17+
18+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.graalvm.demo;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.DisplayName;
6+
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.CsvSource;
9+
10+
class CalculatorTest {
11+
12+
@Test
13+
@DisplayName("1 + 1 = 2")
14+
void addsTwoNumbers() {
15+
Calculator calculator = new Calculator();
16+
assertEquals(2, calculator.add(1, 1), "1 + 1 should equal 2");
17+
}
18+
19+
@Test
20+
@DisplayName("1 + 2 = 3")
21+
void addsTwoNumbers2() {
22+
Calculator calculator = new Calculator();
23+
assertEquals(3, calculator.add(1, 2), "1 + 2 should equal 3");
24+
}
25+
26+
@ParameterizedTest(name = "{0} + {1} = {2}")
27+
@CsvSource({
28+
"0, 1, 1",
29+
"1, 2, 3",
30+
"49, 51, 100",
31+
"1, 100, 101"
32+
})
33+
void add(int first, int second, int expectedResult) {
34+
Calculator calculator = new Calculator();
35+
assertEquals(expectedResult, calculator.add(first, second),
36+
() -> first + " + " + second + " should equal " + expectedResult);
37+
}
38+
}

0 commit comments

Comments
 (0)