Skip to content

Commit 5115deb

Browse files
committed
Introduce CLI for triggering AOT test context processing
This commit introduces ProcessTestsAheadOfTimeCommand, a command-line application (CLI) that scans the provided classpath roots for Spring integration test classes and then generates AOT artifacts for those test classes in the provided output directories. This CLI is only intended to be used by build tools. Closes gh-28825
1 parent feb2a30 commit 5115deb

File tree

5 files changed

+174
-1
lines changed

5 files changed

+174
-1
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ configure(allprojects) { project ->
9393
dependency "com.github.librepdf:openpdf:1.3.29"
9494
dependency "com.rometools:rome:1.18.0"
9595
dependency "commons-io:commons-io:2.11.0"
96+
dependency "info.picocli:picocli:4.6.3"
9697
dependency "io.vavr:vavr:0.10.4"
9798
dependency "net.sf.jopt-simple:jopt-simple:5.0.4"
9899
dependencySet(group: 'org.apache.activemq', version: '5.16.2') {

spring-test/spring-test.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ dependencies {
1414
optional(project(":spring-webflux"))
1515
optional(project(":spring-webmvc"))
1616
optional(project(":spring-websocket"))
17+
optional('info.picocli:picocli')
1718
optional("jakarta.activation:jakarta.activation-api")
1819
optional("jakarta.el:jakarta.el-api")
1920
optional("jakarta.inject:jakarta.inject-api")
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright 2002-2022 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+
17+
package org.springframework.test.context.aot;
18+
19+
import java.nio.file.Files;
20+
import java.nio.file.Path;
21+
import java.util.Set;
22+
import java.util.concurrent.Callable;
23+
import java.util.stream.Stream;
24+
25+
import picocli.CommandLine;
26+
import picocli.CommandLine.Command;
27+
import picocli.CommandLine.Option;
28+
import picocli.CommandLine.Parameters;
29+
30+
import org.springframework.aot.generate.FileSystemGeneratedFiles;
31+
import org.springframework.aot.generate.GeneratedFiles;
32+
import org.springframework.aot.nativex.FileNativeConfigurationWriter;
33+
import org.springframework.aot.nativex.NativeConfigurationWriter;
34+
35+
/**
36+
* Command-line application that scans the provided classpath roots for Spring
37+
* integration test classes and then generates AOT artifacts for those test
38+
* classes in the provided output directories.
39+
*
40+
* @author Sam Brannen
41+
* @since 6.0
42+
* @see TestClassScanner
43+
* @see TestContextAotGenerator
44+
* @see FileNativeConfigurationWriter
45+
*/
46+
@Command(mixinStandardHelpOptions = true, description = "Process test classes ahead of time")
47+
public class ProcessTestsAheadOfTimeCommand implements Callable<Integer> {
48+
49+
@Parameters(index = "0", arity = "1..*", description = "Classpath roots for compiled test classes.")
50+
private Path[] testClasspathRoots;
51+
52+
@Option(names = {"--packages"}, required = false, description = "Test packages to scan. This is optional any only intended for testing purposes.")
53+
private String[] packagesToScan = new String[0];
54+
55+
@Option(names = {"--sources-out"}, required = true, description = "Output path for the generated sources.")
56+
private Path sourcesOutputPath;
57+
58+
@Option(names = {"--resources-out"}, required = true, description = "Output path for the generated resources.")
59+
private Path resourcesOutputPath;
60+
61+
62+
@Override
63+
public Integer call() throws Exception {
64+
TestClassScanner testClassScanner = new TestClassScanner(Set.of(this.testClasspathRoots));
65+
Stream<Class<?>> testClasses = testClassScanner.scan(this.packagesToScan);
66+
67+
// TODO Determine if we need to support CLASS output path.
68+
Path tempDir = Files.createTempDirectory("classes");
69+
GeneratedFiles generatedFiles = new FileSystemGeneratedFiles(kind -> switch(kind) {
70+
case SOURCE -> this.sourcesOutputPath;
71+
case RESOURCE -> this.resourcesOutputPath;
72+
case CLASS -> tempDir;
73+
});
74+
TestContextAotGenerator generator = new TestContextAotGenerator(generatedFiles);
75+
generator.processAheadOfTime(testClasses);
76+
77+
NativeConfigurationWriter writer = new FileNativeConfigurationWriter(this.resourcesOutputPath);
78+
writer.write(generator.getRuntimeHints());
79+
80+
return 0;
81+
}
82+
83+
static int execute(String[] args) throws Exception {
84+
return new CommandLine(new ProcessTestsAheadOfTimeCommand()).execute(args);
85+
}
86+
87+
public static void main(String[] args) throws Exception {
88+
System.exit(execute(args));
89+
}
90+
91+
}

spring-test/src/test/java/org/springframework/test/context/aot/AbstractAotTests.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,16 @@ Stream<Class<?>> scan(String... packageNames) {
7272

7373
Set<Path> classpathRoots() {
7474
try {
75-
return Set.of(Paths.get(getClass().getProtectionDomain().getCodeSource().getLocation().toURI()));
75+
return Set.of(classpathRoot());
76+
}
77+
catch (Exception ex) {
78+
throw new RuntimeException(ex);
79+
}
80+
}
81+
82+
Path classpathRoot() {
83+
try {
84+
return Paths.get(getClass().getProtectionDomain().getCodeSource().getLocation().toURI());
7685
}
7786
catch (Exception ex) {
7887
throw new RuntimeException(ex);
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2002-2022 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+
17+
package org.springframework.test.context.aot;
18+
19+
import java.io.IOException;
20+
import java.nio.file.Files;
21+
import java.nio.file.Path;
22+
import java.util.List;
23+
24+
import org.junit.jupiter.api.Test;
25+
import org.junit.jupiter.api.io.CleanupMode;
26+
import org.junit.jupiter.api.io.TempDir;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
30+
/**
31+
* Tests for {@link ProcessTestsAheadOfTimeCommand}.
32+
*
33+
* @author Sam Brannen
34+
* @since 6.0
35+
*/
36+
class ProcessTestsAheadOfTimeCommandTests extends AbstractAotTests {
37+
38+
@Test
39+
void execute(@TempDir(cleanup = CleanupMode.ON_SUCCESS) Path tempDir) throws Exception {
40+
Path sourcesOutputPath = tempDir.resolve("src/test/java").toAbsolutePath();
41+
Path resourcesOutputPath = tempDir.resolve("src/test/resources").toAbsolutePath();
42+
String testPackage = "org.springframework.test.context.aot.samples.basic";
43+
String[] args = {
44+
"--sources-out=" + sourcesOutputPath,
45+
"--resources-out=" + resourcesOutputPath,
46+
"--packages=" + testPackage,
47+
classpathRoot().toString()
48+
};
49+
int exitCode = ProcessTestsAheadOfTimeCommand.execute(args);
50+
assertThat(exitCode).as("exit code").isZero();
51+
52+
assertThat(findFiles(sourcesOutputPath)).containsExactlyInAnyOrder(
53+
expectedSourceFilesForBasicSpringTests);
54+
55+
assertThat(findFiles(resourcesOutputPath)).contains(
56+
"META-INF/native-image/reflect-config.json",
57+
"META-INF/native-image/resource-config.json",
58+
"META-INF/native-image/proxy-config.json");
59+
}
60+
61+
private static List<String> findFiles(Path outputPath) throws IOException {
62+
int lengthOfOutputPath = outputPath.toFile().getAbsolutePath().length() + 1;
63+
return Files.find(outputPath, Integer.MAX_VALUE,
64+
(path, attributes) -> attributes.isRegularFile())
65+
.map(Path::toAbsolutePath)
66+
.map(Path::toString)
67+
.map(path -> path.substring(lengthOfOutputPath))
68+
.toList();
69+
}
70+
71+
}

0 commit comments

Comments
 (0)