/
AbstractCrossTaskIncrementalJavaCompilationIntegrationTest.groovy
665 lines (523 loc) · 23.4 KB
/
AbstractCrossTaskIncrementalJavaCompilationIntegrationTest.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
/*
* Copyright 2016 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
*
* http://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.gradle.java.compile.incremental
import groovy.transform.NotYetImplemented
import org.gradle.integtests.fixtures.AbstractIntegrationSpec
import org.gradle.integtests.fixtures.CompilationOutputsFixture
import spock.lang.Issue
import spock.lang.Unroll
abstract class AbstractCrossTaskIncrementalJavaCompilationIntegrationTest extends AbstractIntegrationSpec {
CompilationOutputsFixture impl
def setup() {
executer.requireOwnGradleUserHomeDir()
impl = new CompilationOutputsFixture(file("impl/build/classes"))
buildFile << """
subprojects {
apply plugin: 'java'
tasks.withType(JavaCompile) {
options.incremental = true
options.fork = true
}
repositories { mavenCentral() }
}
$projectDependencyBlock
"""
settingsFile << "include 'api', 'impl'"
}
protected abstract String getProjectDependencyBlock()
private File java(Map projectToClassBodies) {
File out
projectToClassBodies.each { project, bodies ->
bodies.each { body ->
def className = (body =~ /(?s).*?(?:class|interface) (\w+) .*/)[0][1]
assert className: "unable to find class name"
def f = file("$project/src/main/java/${className}.java")
f.createFile()
f.text = body
out = f
}
}
out
}
def "detects changed class in an upstream project"() {
java api: ["class A {}", "class B {}"], impl: ["class ImplA extends A {}", "class ImplB extends B {}"]
impl.snapshot { run "compileJava" }
when:
java api: ["class A { String change; }"]
run "impl:compileJava"
then:
impl.recompiledClasses("ImplA")
}
def "detects change to transitive dependency in an upstream project"() {
java api: ["class A {}", "class B extends A {}"]
java impl: ["class SomeImpl {}", "class ImplB extends B {}", "class ImplB2 extends ImplB {}"]
impl.snapshot { run "compileJava" }
when:
java api: ["class A { String change; }"]
run "impl:compileJava"
then:
impl.recompiledClasses("ImplB", "ImplB2")
}
def "deletion of jar without dependents does not recompile any classes"() {
java api: ["class A {}"], impl: ["class SomeImpl {}"]
impl.snapshot { run "compileJava" }
when:
buildFile << """
project(':impl') {
configurations.compile.dependencies.clear() //so that api jar is no longer on classpath
}
"""
run "impl:compileJava"
then:
impl.noneRecompiled()
}
def "deletion of jar with dependents causes compilation failure"() {
java api: ["class A {}"], impl: ["class ImplA extends A {}"]
impl.snapshot { run "compileJava" }
when:
buildFile << """
project(':impl') {
configurations.compile.dependencies.clear() //so that api jar is no longer on classpath
}
"""
fails "impl:compileJava"
then:
impl.noneRecompiled()
}
def "detects change to dependency and ensures class dependency info refreshed"() {
java api: ["class A {}", "class B extends A {}"]
java impl: ["class SomeImpl {}", "class ImplB extends B {}", "class ImplB2 extends ImplB {}"]
impl.snapshot { run "compileJava" }
when:
java api: ["class B { /* remove extends */ }"]
run "impl:compileJava"
then:
impl.recompiledClasses("ImplB", "ImplB2")
when:
impl.snapshot()
java api: ["class A { /* change */ }"]
run "impl:compileJava"
then:
impl.noneRecompiled() //because after earlier change to B, class A is no longer a dependency
}
def "detects deleted class in an upstream project and fails compilation"() {
def b = java(api: ["class A {}", "class B {}"])
java impl: ["class ImplA extends A {}", "class ImplB extends B {}"]
impl.snapshot { run "compileJava" }
when:
assert b.delete()
fails "impl:compileJava"
then:
impl.noneRecompiled()
}
def "recompilation not necessary when upstream does not change any of the actual dependencies"() {
java api: ["class A {}", "class B {}"], impl: ["class ImplA extends A {}"]
impl.snapshot { run "compileJava" }
when:
java api: ["class B { String change; }"]
run "impl:compileJava"
then:
impl.noneRecompiled()
}
def "deletion of jar with non-private constant causes rebuild if constant is used"() {
java api: ["class A { public final static int x = 1; }"], impl: ["class X { int x() { return 1;} }", "class Y {}"]
impl.snapshot { run "compileJava" }
when:
buildFile << """
project(':impl') {
configurations.compile.dependencies.clear() //so that api jar is no longer on classpath
}
"""
run "impl:compileJava"
then:
impl.recompiledClasses("X")
}
def "change in an upstream class with non-private constant doesn't cause full rebuild if constant is not used"() {
java api: ["class A {}", "class B { final static int x = 1; }"], impl: ["class ImplA extends A {}", "class ImplB extends B {}"]
impl.snapshot { run "compileJava" }
when:
java api: ["class B { /* change */ }"]
run "impl:compileJava"
then:
impl.recompiledClasses('ImplB')
}
@Unroll
def "change in an upstream class with non-private constant causes rebuild if same constant is used (#constantType)"() {
java api: ["class A {}", "class B { final static $constantType x = $constantValue; }"], impl: ["class ImplA extends A { $constantType foo() { return $constantValue; }}", "class ImplB {int foo() { return 2; }}"]
impl.snapshot { run "compileJava" }
when:
java api: ["class B { /* change */ }"]
run "impl:compileJava"
then:
impl.recompiledClasses('ImplA')
where:
constantType | constantValue
'boolean' | 'false'
'byte' | '(byte) 125'
'short' | '(short) 666'
'int' | '55542'
'long' | '5L'
'float' | '6f'
'double' | '7d'
'String' | '"foo"'
'String' | '"foo" + "bar"'
}
@Unroll
def "change in an upstream class with non-private constant causes rebuild only if same constant is used and no direct dependency (#constantType)"() {
java api: ["class A {}", "class B { final static $constantType x = $constantValue; }"], impl: ["class X { $constantType foo() { return $constantValue; }}", "class Y {int foo() { return -2; }}"]
impl.snapshot { run "compileJava" }
when:
java api: ["class B { /* change */ }"]
run "impl:compileJava"
then:
impl.recompiledClasses('X')
where:
constantType | constantValue
'boolean' | 'false'
'byte' | '(byte) 125'
'short' | '(short) 666'
'int' | '55542'
'long' | '5L'
'float' | '6f'
'double' | '7d'
'String' | '"foo"'
'String' | '"foo" + "bar"'
}
@Unroll
def "constant value change in an upstream class causes rebuild if previous constant value was used in previous build (#constantType)"() {
java api: ["class A {}", "class B { final static $constantType x = $constantValue; }"], impl: ["class X { $constantType foo() { return $constantValue; }}", "class Y {int foo() { return -2; }}"]
impl.snapshot { run "compileJava" }
when:
java api: ["class B { final static $constantType x = $newValue; /* change value */ ; void blah() { /* avoid flakiness by changing compiled file length*/ } }"]
run "impl:compileJava"
then:
impl.recompiledClasses('X')
where:
constantType | constantValue | newValue
'boolean' | 'false' | 'true'
'byte' | '(byte) 125' | '(byte) 126'
'short' | '(short) 666' | '(short) 555'
'int' | '55542' | '444'
'long' | '5L' | '689L'
'float' | '6f' | '6.5f'
'double' | '7d' | '7.2d'
'String' | '"foo"' | '"bar"'
'String' | '"foo" + "bar"' | '"bar"'
}
def "ignores irrelevant changes to constant values"() {
java api: ["class A {}", "class B { final static int x = 3; final static int y = -2; }"],
impl: ["class X { int foo() { return 3; }}", "class Y {int foo() { return -2; }}"]
impl.snapshot { run "compileJava" }
when:
java api: ["class B { final static int x = 3 ; final static int y = -3; void blah() { /* change irrelevant to constant value x */ } }"]
run "impl:compileJava"
then:
impl.recompiledClasses('Y')
}
def "recompiles in case of conflicting changing constant values"() {
java api: ["class A { final static int x = 3; }", "class B { final static int x = 3; final static int y = -2; }"],
impl: ["class X { int foo() { return 3; }}", "class Y {int foo() { return -2; }}"]
impl.snapshot { run "compileJava" }
when:
java api: ["class B { final static int x = 3 ; final static int y = -3; void blah() { /* change irrelevant to constant value x */ } }"]
java api: ["class A { final static int x = 2 ; final static int y = -2; void blah() { /* change irrelevant to constant value y */ } }"]
run "impl:compileJava"
then:
impl.recompiledClasses('X', 'Y')
}
def "change in an upstream transitive class with non-private constant does not cause full rebuild"() {
java api: ["class A { final static int x = 1; }", "class B extends A {}"], impl: ["class ImplA extends A {}", "class ImplB extends B {}"]
impl.snapshot { run "compileJava" }
when:
java api: ["class B { /* change */ }"]
run "impl:compileJava"
then:
impl.recompiledClasses('ImplB')
}
def "private constant in upstream project does not trigger full rebuild"() {
java api: ["class A {}", "class B { private final static int x = 1; }"], impl: ["class ImplA extends A {}", "class ImplB extends B {}"]
impl.snapshot { run "compileJava" }
when:
java api: ["class B { /* change */ }"]
run "impl:compileJava"
then:
impl.noneRecompiled()
}
def "addition of unused class in upstream project does not rebuild"() {
java api: ["class A {}", "class B { private final static int x = 1; }"], impl: ["class ImplA extends A {}", "class ImplB extends B {}"]
impl.snapshot { run "compileJava" }
when:
java api: ["class C { }"]
run "impl:compileJava"
then:
impl.noneRecompiled()
}
def "removal of unused class in upstream project does not rebuild"() {
java api: ["class A {}", "class B { private final static int x = 1; }"], impl: ["class ImplA extends A {}", "class ImplB extends B {}"]
def c = java api: ["class C { }"]
impl.snapshot { run "compileJava" }
when:
c.delete()
run "impl:compileJava"
then:
impl.noneRecompiled()
}
@NotYetImplemented
def "doesn't recompile if external dependency has ABI incompatible change but not on class we use"() {
given:
buildFile << '''
project(':impl') {
repositories { jcenter() }
dependencies { compile 'org.apache.commons:commons-lang3:3.3' }
}
'''
java api: ["class A {}", "class B { }"], impl: ["class ImplA extends A {}", """import org.apache.commons.lang3.StringUtils;
class ImplB extends B {
public static String HELLO = StringUtils.capitalize("hello");
}"""]
impl.snapshot { run "compileJava" }
when:
buildFile.text = buildFile.text.replace('3.3', '3.3.1')
run 'impl:compileJava'
then:
impl.noneRecompiled()
}
def "detects changed classes when upstream project was built in isolation"() {
java api: ["class A {}", "class B {}"], impl: ["class ImplA extends A {}", "class ImplB extends B {}"]
impl.snapshot { run "compileJava" }
when:
java api: ["class A { String change; }"]
run "api:compileJava"
run "impl:compileJava"
then:
impl.recompiledClasses("ImplA")
}
def "detects class changes in subsequent runs ensuring the jar snapshots are refreshed"() {
java api: ["class A {}", "class B {}"], impl: ["class ImplA extends A {}", "class ImplB extends B {}"]
impl.snapshot { run "compileJava" }
when:
java api: ["class A { String change; }"]
run "api:compileJava"
run "impl:compileJava" //different build invocation
then:
impl.recompiledClasses("ImplA")
when:
impl.snapshot()
java api: ["class B { String change; }"]
run "compileJava"
then:
impl.recompiledClasses("ImplB")
}
def "changes to resources in jar do not incur recompilation"() {
java impl: ["class A {}"]
impl.snapshot { run "impl:compileJava" }
when:
file("api/src/main/resources/some-resource.txt") << "xxx"
java api: ["class A { String change; }"]
run "impl:compileJava"
then:
impl.noneRecompiled()
}
def "handles multiple compile tasks in the same project"() {
settingsFile << "\n include 'other'" //add an extra project
java impl: ["class ImplA extends A {}"], api: ["class A {}"], other: ["class Other {}"]
//new separate compile task (integTestCompile) depends on class from the extra project
file("impl/build.gradle") << """
sourceSets { integTest.java.srcDir 'src/integTest/java' }
dependencies { integTestCompile project(":other") }
"""
file("impl/src/integTest/java/SomeIntegTest.java") << "class SomeIntegTest extends Other {}"
impl.snapshot { run "compileIntegTestJava", "compileJava" }
when: //when api class is changed
java api: ["class A { String change; }"]
run "compileIntegTestJava", "compileJava"
then: //only impl class is recompiled
impl.recompiledClasses("ImplA")
when: //when other class is changed
impl.snapshot()
java other: ["class Other { String change; }"]
run "compileIntegTestJava", "compileJava"
then: //only integTest class is recompiled
impl.recompiledClasses("SomeIntegTest")
}
def "the order of classpath items is unchanged"() {
java api: ["class A {}"], impl: ["class B {}"]
file("impl/build.gradle") << """
dependencies { compile "org.mockito:mockito-core:1.9.5", "junit:junit:4.12" }
compileJava.doFirst {
file("classpath.txt").createNewFile(); file("classpath.txt").text = classpath.files*.name.collect { it.replace('main','api.jar') }.join(', ')
}
"""
when:
run("impl:compileJava") //initial run
then:
file("impl/classpath.txt").text == "api.jar, mockito-core-1.9.5.jar, junit-4.12.jar, objenesis-1.0.jar, hamcrest-core-1.3.jar"
when: //project dependency changes
java api: ["class A { String change; }"]
run("impl:compileJava")
then:
file("impl/classpath.txt").text == "api.jar, mockito-core-1.9.5.jar, junit-4.12.jar, objenesis-1.0.jar, hamcrest-core-1.3.jar"
when: //transitive dependency is excluded
file("impl/build.gradle") << "configurations.compile.exclude module: 'hamcrest-core' \n"
run("impl:compileJava")
then:
file("impl/classpath.txt").text == "api.jar, mockito-core-1.9.5.jar, junit-4.12.jar, objenesis-1.0.jar"
when: //direct dependency is excluded
file("impl/build.gradle") << "configurations.compile.exclude module: 'junit' \n"
run("impl:compileJava")
then:
file("impl/classpath.txt").text == "api.jar, mockito-core-1.9.5.jar, objenesis-1.0.jar"
when: //new dependency is added
file("impl/build.gradle") << "dependencies { compile 'org.testng:testng:6.8.7' } \n"
run("impl:compileJava")
then:
file("impl/classpath.txt").text == "api.jar, mockito-core-1.9.5.jar, testng-6.8.7.jar, objenesis-1.0.jar, bsh-2.0b4.jar, jcommander-1.27.jar, snakeyaml-1.12.jar"
}
def "handles duplicate class found in jar"() {
java api: ["class A extends B {}", "class B {}"], impl: ["class A extends C {}", "class C {}"]
impl.snapshot { run("impl:compileJava") }
when:
//change to source dependency duplicate triggers recompilation
java impl: ["class C { String change; }"]
run("impl:compileJava")
then:
impl.recompiledClasses("A", "C")
when:
//change to jar dependency duplicate is ignored because source duplicate wins
impl.snapshot()
java api: ["class B { String change; } "]
run("impl:compileJava")
then:
impl.noneRecompiled()
}
def "new jar with duplicate class appearing earlier on classpath must trigger compilation"() {
java impl: ["class A extends org.junit.Assert {}"]
file("impl/build.gradle") << """
configurations.compile.dependencies.clear()
dependencies { compile 'junit:junit:4.12' }
"""
impl.snapshot { run("impl:compileJava") }
when:
//add new jar with duplicate class that will be earlier on the classpath (project dependencies are earlier on classpath)
file("api/src/main/java/org/junit/Assert.java") << "package org.junit; public class Assert {}"
file("impl/build.gradle") << "dependencies { compile project(':api') }"
run("impl:compileJava")
then:
impl.recompiledClasses("A")
}
def "new jar without duplicate class does not trigger compilation"() {
java impl: ["class A {}"]
impl.snapshot { run("impl:compileJava") }
when:
file("impl/build.gradle") << "dependencies { compile 'junit:junit:4.12' }"
run("impl:compileJava")
then:
impl.noneRecompiled()
}
def "changed jar with duplicate class appearing earlier on classpath must trigger compilation"() {
java impl: ["class A extends org.junit.Assert {}"]
file("impl/build.gradle") << """
dependencies { compile 'junit:junit:4.12' }
"""
impl.snapshot { run("impl:compileJava") }
when:
//update existing jar with duplicate class that will be earlier on the classpath (project dependencies are earlier on classpath)
file("api/src/main/java/org/junit/Assert.java") << "package org.junit; public class Assert {}"
run("impl:compileJava")
then:
impl.recompiledClasses("A")
}
def "deletion of a jar with duplicate class causes recompilation"() {
file("api/src/main/java/org/junit/Assert.java") << "package org.junit; public class Assert {}"
java impl: ["class A extends org.junit.Assert {}"]
file("impl/build.gradle") << "dependencies { compile 'junit:junit:4.12' }"
impl.snapshot { run("impl:compileJava") }
when:
file("impl/build.gradle").text = """
configurations.compile.dependencies.clear() //kill project dependency
dependencies { compile 'junit:junit:4.11' } //leave only junit
"""
run("impl:compileJava")
then:
impl.recompiledClasses("A")
}
@Unroll
def "recompiles outermost class when #visibility inner class constains constant reference"() {
java api: [
"class A { public static final int EVIL = 666; }",
], impl: [
"class B {}",
"class C { $visibility static class Inner { int foo() { return A.EVIL; } } }",
"class D { $visibility class Inner { int foo() { return A.EVIL; } } }",
"class E { void foo() { Runnable r = new Runnable() { public void run() { int x = A.EVIL; } }; } }",
"class F { int foo() { return A.EVIL; } $visibility static class Inner { } }",
]
impl.snapshot { run("impl:compileJava") }
when:
java api: ["class A { public static final int EVIL = 0; void blah() { /* avoid flakiness by changing compiled file length*/ } }"]
run("impl:compileJava")
then:
impl.recompiledClasses('C', 'C$Inner', 'D', 'D$Inner', 'E', 'E$1', 'F', 'F$Inner')
where:
visibility << ['public', 'private' , '']
}
def "recognizes change of constant value in annotation, even if we know it's a bad practice"() {
java api: [
"class A { public static final int CST = 0; }",
"""import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
@interface B { int value(); }"""
], impl: [
// cases where it's relevant, ABI-wise
"@B(A.CST) class OnClass {}",
"class OnMethod { @B(A.CST) void foo() {} }",
"class OnParameter { void foo(@B(A.CST) int x) {} }"
]
impl.snapshot { run("impl:compileJava") }
when:
java api: ["class A { public static final int CST = 1234; void blah() { /* avoid flakiness by changing compiled file length*/ } }"]
run("impl:compileJava")
then:
impl.recompiledClasses("OnClass", "OnMethod", "OnParameter")
}
@NotYetImplemented
@Issue("gradle/performance#335")
def "doesn't destroy class analysis when a compile error occurs"() {
java api: ["class A {}"], impl: ["class ImplA extends A {}"]
impl.snapshot { run "compileJava" }
when:
java api: ["class A { compile error }"]
then:
impl.snapshot { fails "impl:compileJava" }
when:
java api: ["class A { String foo; }"]
run 'impl:compileJava'
then:
!output.contains(':api:compileJava - is not incremental (e.g. outputs have changed, no previous execution, etc.).')
impl.recompiledClasses("ImplA")
}
@Issue("gradle/gradle#1474")
def "recompiles dependent class in case a constant is computed from another constant"() {
java api: ["class A { public static final int FOO = 10; }"], impl: ['class B { public static final int BAR = 2 + A.FOO; } ']
impl.snapshot { run 'compileJava' }
when:
java api: ['class A { public static final int FOO = 100; }']
run 'impl:compileJava'
then:
impl.recompiledClasses 'B'
}
}