Skip to content

Commit 5aea5e2

Browse files
authored
[SUREFIRE-2190] Fix module dependencies for compile only dependencies (#668)
* [SUREFIRE-2190] Fix module dependencies for compile only dependencies Include all modules on the module path to the root modules. This fixes test code that uses dependencies that are declared as optional (`require static`) for the main artifact to access these dependencies.
1 parent 8ca7a09 commit 5aea5e2

File tree

8 files changed

+216
-9
lines changed

8 files changed

+216
-9
lines changed

maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ModularClasspathForkConfiguration.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,18 +194,16 @@ File createArgsFile(
194194
.append(NL);
195195
}
196196

197-
args.append("--add-modules").append(NL).append(moduleName).append(NL);
198-
199197
args.append("--add-reads")
200198
.append(NL)
201199
.append(moduleName)
202200
.append('=')
203201
.append("ALL-UNNAMED")
204202
.append(NL);
205-
} else {
206-
args.append("--add-modules").append(NL).append(moduleName).append(NL);
207203
}
208204

205+
args.append("--add-modules").append(NL).append("ALL-MODULE-PATH").append(NL);
206+
209207
for (String[] entries : providerJpmsArguments) {
210208
for (String entry : entries) {
211209
args.append(entry).append(NL);

maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/ForkConfigurationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public void testCliArgs() throws Exception {
192192
assertThat(endOfFileArg).isPositive();
193193
Path argFile = Paths.get(cliAsString.substring(beginOfFileArg + 1, endOfFileArg));
194194
String argFileText = new String(readAllBytes(argFile));
195-
assertThat(argFileText).contains("arg2").contains("arg3").contains("--add-modules" + NL + "test.module");
195+
assertThat(argFileText).contains("arg2").contains("arg3").contains("--add-modules" + NL + "ALL-MODULE-PATH");
196196
}
197197

198198
@Test

maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/ModularClasspathForkConfigurationTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,13 @@ public void shouldCreateModularArgsFile() throws Exception {
127127

128128
assertThat(argsFileLines.get(7)).isEqualTo("abc/org.apache.abc=ALL-UNNAMED");
129129

130-
assertThat(argsFileLines.get(8)).isEqualTo("--add-modules");
130+
assertThat(argsFileLines.get(8)).isEqualTo("--add-reads");
131131

132-
assertThat(argsFileLines.get(9)).isEqualTo("abc");
132+
assertThat(argsFileLines.get(9)).isEqualTo("abc=ALL-UNNAMED");
133133

134-
assertThat(argsFileLines.get(10)).isEqualTo("--add-reads");
134+
assertThat(argsFileLines.get(10)).isEqualTo("--add-modules");
135135

136-
assertThat(argsFileLines.get(11)).isEqualTo("abc=ALL-UNNAMED");
136+
assertThat(argsFileLines.get(11)).isEqualTo("ALL-MODULE-PATH");
137137

138138
assertThat(argsFileLines.get(12)).isEqualTo(ForkedBooter.class.getName());
139139

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.maven.surefire.its.jiras;
20+
21+
import org.apache.maven.surefire.its.fixture.AbstractJava9PlusIT;
22+
import org.junit.Test;
23+
24+
/**
25+
* Integration test for <a href="https://issues.apache.org/jira/browse/SUREFIRE-2190">SUREFIRE-2190</a>.
26+
*/
27+
public class Surefire2190JUnitIT extends AbstractJava9PlusIT {
28+
@Test
29+
public void test() throws Exception {
30+
assumeJava9().debugLogging().executeVerify().assertTestSuiteResults(4, 0, 0, 0);
31+
}
32+
33+
@Override
34+
protected String getProjectDirectoryName() {
35+
return "surefire-2190";
36+
}
37+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed under the Apache License, Version 2.0 (the "License");
4+
~ you may not use this file except in compliance with the License.
5+
~ You may obtain a copy of the License at
6+
~
7+
~ http://www.apache.org/licenses/LICENSE-2.0
8+
~
9+
~ Unless required by applicable law or agreed to in writing, software
10+
~ distributed under the License is distributed on an "AS IS" BASIS,
11+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
~ See the License for the specific language governing permissions and
13+
~ limitations under the License.
14+
-->
15+
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
16+
<modelVersion>4.0.0</modelVersion>
17+
18+
<groupId>org.apache.maven.plugins.surefire</groupId>
19+
<artifactId>surefire-2190-it</artifactId>
20+
<version>1.0</version>
21+
22+
<properties>
23+
<maven.compiler.source>${java.specification.version}</maven.compiler.source>
24+
<maven.compiler.target>${java.specification.version}</maven.compiler.target>
25+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
26+
</properties>
27+
28+
<dependencies>
29+
<dependency>
30+
<groupId>org.junit.jupiter</groupId>
31+
<artifactId>junit-jupiter-api</artifactId>
32+
<version>5.9.1</version>
33+
<scope>test</scope>
34+
</dependency>
35+
36+
<dependency>
37+
<groupId>org.junit.jupiter</groupId>
38+
<artifactId>junit-jupiter-params</artifactId>
39+
<version>5.9.1</version>
40+
<scope>test</scope>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>jakarta.annotation</groupId>
45+
<artifactId>jakarta.annotation-api</artifactId>
46+
<version>2.1.1</version>
47+
<scope>provided</scope>
48+
<optional>true</optional>
49+
</dependency>
50+
</dependencies>
51+
52+
<build>
53+
<plugins>
54+
<plugin>
55+
<artifactId>maven-compiler-plugin</artifactId>
56+
<version>3.8.1</version>
57+
<configuration>
58+
<encoding>UTF-8</encoding>
59+
</configuration>
60+
</plugin>
61+
<plugin>
62+
<groupId>org.apache.maven.plugins</groupId>
63+
<artifactId>maven-surefire-plugin</artifactId>
64+
<version>${surefire.version}</version>
65+
</plugin>
66+
</plugins>
67+
</build>
68+
</project>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module surefire.bug.thing3 {
2+
requires static jakarta.annotation;
3+
4+
exports surefirebug.thing;
5+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package surefirebug.thing;
2+
3+
import java.util.concurrent.Callable;
4+
import java.util.function.Supplier;
5+
6+
public final class Main implements Callable<String> {
7+
8+
private static final Supplier<String> DEFAULT_PROVIDER = () -> "Hello, World";
9+
10+
11+
public static void main(String... args) throws Exception {
12+
Main main = new Main();
13+
String text = main.call();
14+
15+
System.out.println(text);
16+
}
17+
18+
private final Supplier<String> supplier;
19+
20+
public Main(Supplier<String> supplier) {
21+
this.supplier = supplier;
22+
}
23+
24+
public Main() {
25+
this(DEFAULT_PROVIDER);
26+
}
27+
28+
@Override
29+
public String call() {
30+
var value = supplier.get();
31+
32+
if (value == null) {
33+
var annotations = supplier.getClass().getAnnotations();
34+
for (var annotation : annotations) {
35+
if (annotation.annotationType().getSimpleName().equals("Nullable")) {
36+
return "<null value>";
37+
}
38+
}
39+
throw new IllegalStateException("null without @Nullable annotation");
40+
}
41+
42+
return value;
43+
}
44+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package surefirebug.thing;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import jakarta.annotation.Nullable;
6+
import java.util.function.Supplier;
7+
import org.junit.jupiter.api.Assertions;
8+
import org.junit.jupiter.api.Test;
9+
10+
public class MainTest {
11+
12+
@Test
13+
public void testDefault() {
14+
Main main = new Main();
15+
assertEquals("Hello, World", main.call());
16+
}
17+
18+
@Test
19+
public void testNonStandard() {
20+
Main main = new Main(new NonstandardSupplier());
21+
assertEquals("Hello, People", main.call());
22+
}
23+
24+
@Test
25+
public void testNullSupplierWithNullable() {
26+
Main main = new Main(new NullSupplierWithNullable());
27+
assertEquals("<null value>", main.call());
28+
}
29+
30+
@Test
31+
public void testNullSupplierWithoutNullable() {
32+
Main main = new Main(new NullSupplierWithoutNullable());
33+
IllegalStateException e = Assertions.assertThrows(IllegalStateException.class, main::call);
34+
assertEquals("null without @Nullable annotation", e.getMessage());
35+
}
36+
37+
public static class NonstandardSupplier implements Supplier<String> {
38+
public String get() {
39+
return "Hello, People";
40+
}
41+
}
42+
43+
@Nullable
44+
public static class NullSupplierWithNullable implements Supplier<String> {
45+
public String get() {
46+
return null;
47+
}
48+
}
49+
50+
public static class NullSupplierWithoutNullable implements Supplier<String> {
51+
public String get() {
52+
return null;
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)