-
Notifications
You must be signed in to change notification settings - Fork 51
/
JavaApplicationWithAgentFunctionalTest.groovy
295 lines (252 loc) · 12 KB
/
JavaApplicationWithAgentFunctionalTest.groovy
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
* Copyright (c) 2020, 2021 Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or
* data (collectively the "Software"), free of charge and under any and all
* copyright rights in the Software, and any and all patent rights owned or
* freely licensable by each licensor hereunder covering either (i) the
* unmodified Software as contributed to or provided by such licensor, or (ii)
* the Larger Works (as defined below), to deal in both
*
* (a) the Software, and
*
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
*
* The above copyright notice and either this complete permission notice or at a
* minimum a reference to the UPL must be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.graalvm.buildtools.maven
import spock.lang.Issue
import spock.lang.Unroll
class JavaApplicationWithAgentFunctionalTest extends AbstractGraalVMMavenFunctionalTest {
@Issue("https://github.com/graalvm/native-build-tools/issues/179")
def "agent is used for JVM tests when native image tests are skipped via -DskipNativeTests"() {
given:
withSample("java-application-with-reflection")
when:
// Run Maven in debug mode (-X) in order to capture the command line arguments
// used to launch Surefire with the agent.
mvn '-X', '-Pnative', 'test', '-DskipNativeTests'
then:
// Agent is used with Surefire
outputContains '-agentlib:native-image-agent='
and:
// Agent generates files
['jni', 'proxy', 'reflect', 'resource', 'serialization'].each { name ->
assert file("target/native/agent-output/test/${name}-config.json").exists()
}
and:
// Surefire / JVM tests run
buildSucceeded
outputContains "SurefirePlugin - Running org.graalvm.demo.ApplicationTest"
outputContains "SurefirePlugin - Running org.graalvm.demo.CalculatorTest"
and:
// Native tests do not run
outputContains "Skipping native-image tests (parameter 'skipTests' or 'skipNativeTests' is true)."
outputDoesNotContain "containers found"
}
def "agent is used for tests when enabled in POM without custom options"() {
given:
withSample("java-application-with-reflection")
when:
// Run Maven in debug mode (-X) in order to capture the command line arguments
// used to launch Surefire with the agent.
mvn '-X', '-Pnative', 'test'
then:
outputContains """
[ 4 containers found ]
[ 0 containers skipped ]
[ 4 containers started ]
[ 0 containers aborted ]
[ 4 containers successful ]
[ 0 containers failed ]
[ 7 tests found ]
[ 0 tests skipped ]
[ 7 tests started ]
[ 0 tests aborted ]
[ 7 tests successful ]
[ 0 tests failed ]
""".trim()
and:
['jni', 'proxy', 'reflect', 'resource', 'serialization'].each { name ->
assert file("target/native/agent-output/test/${name}-config.json").exists()
}
and:
outputContains '-agentlib:native-image-agent'
// From shared, unnamed config:
outputContains '=experimental-class-loader-support'
// From test config:
outputContains ',access-filter-file='
outputContains '/src/test/resources/access-filter.json'.replace('/', java.io.File.separator)
// Always configured:
outputContains ',config-output-dir='
outputContains '/target/native/agent-output/test'.replace("/", java.io.File.separator)
and:
// If the custom access-filter.json is applied, we should not see any warnings about Surefire types.
// The actual warning would be something like:
// Warning: Could not resolve org.apache.maven.surefire.junitplatform.JUnitPlatformProvider for reflection configuration. Reason: java.lang.ClassNotFoundException: org.apache.maven.surefire.junitplatform.JUnitPlatformProvider.
outputDoesNotContain 'Warning: Could not resolve org.apache.maven.surefire'
// From periodic-config:
outputDoesNotContain ',config-write-period-secs=30,config-write-initial-delay-secs=5'
}
def "agent is not used for tests when enabled in POM but disabled via the command line"() {
given:
withSample("java-application-with-reflection")
when:
mvn '-Pnative', '-Dagent=false', 'test'
then:
outputContains """
[ 4 containers found ]
[ 0 containers skipped ]
[ 4 containers started ]
[ 0 containers aborted ]
[ 4 containers successful ]
[ 0 containers failed ]
[ 7 tests found ]
[ 0 tests skipped ]
[ 7 tests started ]
[ 0 tests aborted ]
[ 6 tests successful ]
[ 1 tests failed ]
""".trim()
and:
outputContains 'expected: <Hello, native!> but was: <null>'
}
def "agent is used for tests when enabled in POM with custom options"() {
given:
withSample("java-application-with-reflection")
when:
// Run Maven in debug mode (-X) in order to capture the command line arguments
// used to launch Surefire with the agent.
mvn '-X', '-Pnative', '-DagentOptions=periodic-config', 'test'
then:
outputContains """
[ 4 containers found ]
[ 0 containers skipped ]
[ 4 containers started ]
[ 0 containers aborted ]
[ 4 containers successful ]
[ 0 containers failed ]
[ 7 tests found ]
[ 0 tests skipped ]
[ 7 tests started ]
[ 0 tests aborted ]
[ 7 tests successful ]
[ 0 tests failed ]
""".trim()
and:
['jni', 'proxy', 'reflect', 'resource', 'serialization'].each { name ->
assert file("target/native/agent-output/test/${name}-config.json").exists()
}
and:
// If custom agent options are processed, the debug output for Surefire
// should include the following segments of the agent command line argument.
outputContains '-agentlib:native-image-agent'
// From shared, unnamed config:
outputContains '=experimental-class-loader-support'
// From test config:
outputContains ',access-filter-file='
outputContains '/src/test/resources/access-filter.json'.replace('/', java.io.File.separator)
// From periodic-config:
outputContains ',config-write-period-secs=30,config-write-initial-delay-secs=5'
// Always configured:
outputContains ',config-output-dir='
outputContains '/target/native/agent-output/test'.replace("/", java.io.File.separator)
and:
// If the custom access-filter.json is applied, we should not see any warnings about Surefire types.
// The actual warning would be something like:
// Warning: Could not resolve org.apache.maven.surefire.junitplatform.JUnitPlatformProvider for reflection configuration. Reason: java.lang.ClassNotFoundException: org.apache.maven.surefire.junitplatform.JUnitPlatformProvider.
outputDoesNotContain 'Warning: Could not resolve org.apache.maven.surefire'
}
@Issue("https://github.com/graalvm/native-build-tools/issues/134")
@Unroll("generated agent files are added when building native image on Maven #version with JUnit Platform #junitVersion")
def "generated agent files are used when building native image"() {
given:
withSample("java-application-with-reflection")
when:
mvn '-X', '-Pnative', '-DskipTests=true', '-DskipNativeBuild=true', 'package', 'exec:exec@java-agent'
then:
['jni', 'proxy', 'reflect', 'resource', 'serialization'].each { name ->
assert file("target/native/agent-output/main/${name}-config.json").exists()
}
and:
// If custom agent options are not used, the Maven debug output should include
// the following segments of the agent command line argument.
outputContains '-agentlib:native-image-agent'
// From shared, unnamed config:
outputContains '=experimental-class-loader-support'
// From main config:
outputContains ',access-filter-file='
outputContains '/src/main/resources/access-filter.json'.replace('/', java.io.File.separator)
// Always configured:
outputContains ',config-output-dir='
outputContains '/target/native/agent-output/main'.replace("/", java.io.File.separator)
when:
mvn '-Pnative', '-DskipTests=true', 'package', 'exec:exec@native'
then:
outputContains "Application message: Hello, native!"
}
def "generated agent files are not used when building native image when agent is enabled in POM but disabled via the command line"() {
given:
withSample("java-application-with-reflection")
when:
mvn '-X', '-Pnative', '-Dagent=false', '-DskipTests=true', '-DskipNativeBuild=true', 'package', 'exec:exec@java-agent'
then:
outputDoesNotContain '-agentlib:native-image-agent'
when:
mvn '-Pnative', '-DskipTests=true', 'package', 'exec:exec@native'
then:
outputContains "Application message: null"
}
def "custom options and generated agent files are used when building native image"() {
given:
withSample("java-application-with-reflection")
when:
mvn '-X', '-Pnative', '-DagentOptions=periodic-config', '-DskipTests=true', '-DskipNativeBuild=true', 'package', 'exec:exec@java-agent'
then:
['jni', 'proxy', 'reflect', 'resource', 'serialization'].each { name ->
assert file("target/native/agent-output/main/${name}-config.json").exists()
}
and:
// If custom agent options are used, the Maven debug output should include
// the following segments of the agent command line argument.
outputContains '-agentlib:native-image-agent'
// From shared, unnamed config:
outputContains '=experimental-class-loader-support'
// From main config:
outputContains ',access-filter-file='
outputContains '/src/main/resources/access-filter.json'.replace('/', java.io.File.separator)
// From periodic-config:
outputContains ',config-write-period-secs=30,config-write-initial-delay-secs=5'
// Always configured:
outputContains ',config-output-dir='
outputContains '/target/native/agent-output/main'.replace("/", java.io.File.separator)
when:
mvn '-Pnative', '-DskipTests=true', 'package', 'exec:exec@native'
then:
outputContains "Application message: Hello, native!"
}
}