Skip to content

Commit 2a497e9

Browse files
GH-1388 - Fix test resource lookup for Kotlin in Javadoc processing APT.
Co-authored-by: Dennis Wittkötter <dw@logentis.de>
1 parent e390948 commit 2a497e9

File tree

3 files changed

+82
-1
lines changed

3 files changed

+82
-1
lines changed

spring-modulith-apt/src/main/java/org/springframework/modulith/apt/SpringModulithProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public void init(ProcessingEnvironment environment) {
134134

135135
var path = placeholder.toUri().toString();
136136

137-
if (path.contains(BuildSystemUtils.getTestTarget())) {
137+
if (BuildSystemUtils.pointsToTestTarget(path)) {
138138
this.testExecution = true;
139139
}
140140

spring-modulith-docs/src/main/java/org/springframework/modulith/docs/util/BuildSystemUtils.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
* Utilities to detect the build system used.
2727
*
2828
* @author Oliver Drotbohm
29+
* @author Dennis Wittkötter
2930
* @since 1.3
3031
*/
3132
public class BuildSystemUtils {
@@ -57,11 +58,25 @@ public static Optional<Resource> getTargetResource(String path) {
5758
* Returns the path to the folder containing test classes.
5859
*
5960
* @return will never be {@literal null}.
61+
* @deprecated since 1.4.4, 2.0. Use {@link #pointsToTestTarget(String)} instead.
6062
*/
63+
@Deprecated(since = "1.4.4, 2.0", forRemoval = true)
6164
public static String getTestTarget() {
6265
return isMaven() ? "target/test-classes" : "build/classes/java/test";
6366
}
6467

68+
/**
69+
* Returns whether the given path points to a resource in the test target folder.
70+
*
71+
* @param path must not be {@literal null} or empty.
72+
*/
73+
public static boolean pointsToTestTarget(String path) {
74+
75+
Assert.hasText(path, "Path must not be null or empty!");
76+
77+
return isMaven() ? pointsToMavenTestTarget(path) : pointsToGradleTestTarget(path);
78+
}
79+
6580
/**
6681
* Returns the path to the target folder for resources.
6782
*
@@ -72,6 +87,14 @@ public static String getResourceTarget() {
7287
return isMaven() ? "target/classes" : "build/resources/main";
7388
}
7489

90+
static boolean pointsToMavenTestTarget(String path) {
91+
return path.matches("target\\/test-classes\\/.*");
92+
}
93+
94+
static boolean pointsToGradleTestTarget(String path) {
95+
return path.matches("build(\\/.+)?\\/classes(\\/(java|kotlin))?\\/(test|testFixtures)\\/.*");
96+
}
97+
7598
private static String getTargetFolder() {
7699
return isMaven() ? "target" : "build";
77100
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.modulith.docs.util;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import java.util.stream.Stream;
21+
22+
import org.junit.jupiter.api.DynamicTest;
23+
import org.junit.jupiter.api.TestFactory;
24+
25+
/**
26+
* Unit tests for {@link BuildSystemUtils}.
27+
*
28+
* @author Oliver Drotbohm
29+
*/
30+
class BuildSystemUtilsUnitTests {
31+
32+
@TestFactory // GH-1386
33+
Stream<DynamicTest> detectsMavenTargetResources() {
34+
35+
var values = getSampleResources("target/test-classes");
36+
37+
return DynamicTest.stream(values, it -> it + " is a test resource", it -> {
38+
assertThat(BuildSystemUtils.pointsToTestTarget(it)).isTrue();
39+
});
40+
}
41+
42+
@TestFactory // GH-1386
43+
Stream<DynamicTest> detectsGradleTargetResources() {
44+
45+
var values = getSampleResources(
46+
"build/classes/java/test",
47+
"build/classes/kotlin/test",
48+
"build/tmp/kapt3/classes/testFixtures");
49+
50+
return DynamicTest.stream(values, it -> it + " is a test resource", it -> {
51+
assertThat(BuildSystemUtils.pointsToGradleTestTarget(it)).isTrue();
52+
});
53+
}
54+
55+
private Stream<String> getSampleResources(String... paths) {
56+
return Stream.of(paths).map(it -> it.concat("/some-test-resource.txt"));
57+
}
58+
}

0 commit comments

Comments
 (0)