/
AotTests.java
190 lines (169 loc) · 8.19 KB
/
AotTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.maven;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Stream;
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.contentOf;
/**
* Integration tests for the Maven plugin's AOT support.
*
* @author Stephane Nicoll
* @author Andy Wilkinson
* @author Scott Frederick
*/
@ExtendWith(MavenBuildExtension.class)
public class AotTests {
@TestTemplate
void whenAotRunsSourcesAreGenerated(MavenBuild mavenBuild) {
mavenBuild.project("aot").goals("package").execute((project) -> {
Path aotDirectory = project.toPath().resolve("target/spring-aot/main");
assertThat(collectRelativePaths(aotDirectory.resolve("sources")))
.contains(Path.of("org", "test", "SampleApplication__ApplicationContextInitializer.java"));
});
}
@TestTemplate
void whenAotRunsResourcesAreGeneratedAndCopiedToTargetClasses(MavenBuild mavenBuild) {
mavenBuild.project("aot-resource-generation").goals("package").execute((project) -> {
Path targetClasses = project.toPath().resolve("target/classes");
assertThat(collectRelativePaths(targetClasses)).contains(
Path.of("META-INF", "native-image", "org.springframework.boot.maven.it", "aot-resource-generation",
"reflect-config.json"),
Path.of("META-INF", "native-image", "org.springframework.boot.maven.it", "aot-resource-generation",
"resource-config.json"),
Path.of("META-INF", "native-image", "org.springframework.boot.maven.it", "aot-resource-generation",
"native-image.properties"),
Path.of("generated-resource"), Path.of("nested/generated-resource"));
});
}
@TestTemplate
void whenAotRunsWithJdkProxyResourcesIncludeProxyConfig(MavenBuild mavenBuild) {
mavenBuild.project("aot-jdk-proxy").goals("package").execute((project) -> {
Path aotDirectory = project.toPath().resolve("target/spring-aot/main");
assertThat(collectRelativePaths(aotDirectory.resolve("resources"))).contains(
Path.of("META-INF", "native-image", "org.springframework.boot.maven.it", "aot-jdk-proxy",
"reflect-config.json"),
Path.of("META-INF", "native-image", "org.springframework.boot.maven.it", "aot-jdk-proxy",
"resource-config.json"),
Path.of("META-INF", "native-image", "org.springframework.boot.maven.it", "aot-jdk-proxy",
"proxy-config.json"),
Path.of("META-INF", "native-image", "org.springframework.boot.maven.it", "aot-jdk-proxy",
"native-image.properties"));
});
}
@TestTemplate
void whenAotRunsWithClassProxyClassesAreGenerated(MavenBuild mavenBuild) {
mavenBuild.project("aot-class-proxy").goals("package").execute((project) -> {
Path aotDirectory = project.toPath().resolve("target/spring-aot/main");
assertThat(collectRelativePaths(aotDirectory.resolve("classes")))
.contains(Path.of("org", "test", "SampleRunner$$SpringCGLIB$$0.class"));
});
}
@TestTemplate
void whenAotRunsWithProfilesSourcesAreGenerated(MavenBuild mavenBuild) {
mavenBuild.project("aot-profile").goals("package").execute((project) -> {
Path aotDirectory = project.toPath().resolve("target/spring-aot/main");
assertThat(collectRelativePaths(aotDirectory.resolve("sources")))
.contains(Path.of("org", "test", "TestProfileConfiguration__BeanDefinitions.java"));
});
}
@TestTemplate
void whenAotRunsWithArgumentsSourcesAreGenerated(MavenBuild mavenBuild) {
mavenBuild.project("aot-arguments").goals("package").execute((project) -> {
Path aotDirectory = project.toPath().resolve("target/spring-aot/main");
assertThat(collectRelativePaths(aotDirectory.resolve("sources")))
.contains(Path.of("org", "test", "TestProfileConfiguration__BeanDefinitions.java"));
});
}
@TestTemplate
void whenAotRunsWithJvmArgumentsSourcesAreGenerated(MavenBuild mavenBuild) {
mavenBuild.project("aot-jvm-arguments").goals("package").execute((project) -> {
Path aotDirectory = project.toPath().resolve("target/spring-aot/main");
assertThat(collectRelativePaths(aotDirectory.resolve("sources")))
.contains(Path.of("org", "test", "TestProfileConfiguration__BeanDefinitions.java"));
});
}
@TestTemplate
void whenAotRunsWithReleaseSourcesAreGenerated(MavenBuild mavenBuild) {
mavenBuild.project("aot-release").goals("package").execute((project) -> {
Path aotDirectory = project.toPath().resolve("target/spring-aot/main");
assertThat(collectRelativePaths(aotDirectory.resolve("sources")))
.contains(Path.of("org", "test", "SampleApplication__ApplicationContextInitializer.java"));
});
}
@TestTemplate
void whenAotRunsWithInvalidCompilerArgumentsCompileFails(MavenBuild mavenBuild) {
mavenBuild.project("aot-compiler-arguments").goals("package").executeAndFail(
(project) -> assertThat(buildLog(project)).contains("invalid flag: --invalid-compiler-arg"));
}
@TestTemplate
void whenAotRunsSourcesAreCompiled(MavenBuild mavenBuild) {
mavenBuild.project("aot").goals("package").execute((project) -> {
Path classesDirectory = project.toPath().resolve("target/classes");
assertThat(collectRelativePaths(classesDirectory))
.contains(Path.of("org", "test", "SampleApplication__ApplicationContextInitializer.class"));
});
}
@TestTemplate
void whenAotRunsResourcesAreCopiedToTargetClasses(MavenBuild mavenBuild) {
mavenBuild.project("aot-jdk-proxy").goals("package").execute((project) -> {
Path classesDirectory = project.toPath().resolve("target/classes/META-INF/native-image");
assertThat(collectRelativePaths(classesDirectory)).contains(
Path.of("org.springframework.boot.maven.it", "aot-jdk-proxy", "reflect-config.json"),
Path.of("org.springframework.boot.maven.it", "aot-jdk-proxy", "resource-config.json"),
Path.of("org.springframework.boot.maven.it", "aot-jdk-proxy", "proxy-config.json"),
Path.of("org.springframework.boot.maven.it", "aot-jdk-proxy", "native-image.properties"));
});
}
@TestTemplate
void whenAotRunsWithClassProxyClassesAreCopiedToTargetClasses(MavenBuild mavenBuild) {
mavenBuild.project("aot-class-proxy").goals("package").execute((project) -> {
Path classesDirectory = project.toPath().resolve("target/classes/");
assertThat(collectRelativePaths(classesDirectory))
.contains(Path.of("org", "test", "SampleRunner$$SpringCGLIB$$0.class"));
});
}
@TestTemplate
void whenAotTestRunsSourcesAndResourcesAreGenerated(MavenBuild mavenBuild) {
mavenBuild.project("aot-test").goals("test").execute((project) -> {
Path aotDirectory = project.toPath().resolve("target/spring-aot/test");
assertThat(collectRelativePaths(aotDirectory.resolve("sources"))).contains(Path.of("org", "test",
"SampleApplicationTests__TestContext001_ApplicationContextInitializer.java"));
Path testClassesDirectory = project.toPath().resolve("target/test-classes");
assertThat(collectRelativePaths(testClassesDirectory)).contains(Path.of("META-INF", "native-image",
"org.springframework.boot.maven.it", "aot-test", "reflect-config.json"));
assertThat(collectRelativePaths(testClassesDirectory)).contains(Path.of("org", "test",
"SampleApplicationTests__TestContext001_ApplicationContextInitializer.class"));
});
}
Stream<Path> collectRelativePaths(Path sourceDirectory) {
try {
return Files.walk(sourceDirectory).filter(Files::isRegularFile)
.map((path) -> path.subpath(sourceDirectory.getNameCount(), path.getNameCount()));
}
catch (IOException ex) {
throw new IllegalStateException(ex);
}
}
protected String buildLog(File project) {
return contentOf(new File(project, "target/build.log"));
}
}