@@ -42,8 +42,6 @@ class JavaCompileProblemsIntegrationTest extends AbstractIntegrationSpec impleme
42
42
/**
43
43
* A map of all possible file locations, and the number of occurrences we expect to find in the problems.
44
44
*/
45
- public static final String REDUNDANT_CAST_JAVA_8_MESSAGE = ' redundant cast to java.lang.String'
46
- public static final String REDUNDANT_CAST_JAVA_9_MESSAGE = ' redundant cast to String'
47
45
private final Map<String , Integer > possibleFileLocations = [:]
48
46
49
47
/**
@@ -75,6 +73,75 @@ class JavaCompileProblemsIntegrationTest extends AbstractIntegrationSpec impleme
75
73
}
76
74
}
77
75
76
+ /**
77
+ * Verifies common properties of an error problem.
78
+ *
79
+ * @param problem The problem to verify
80
+ * @param expectLineLocation Whether to expect a line location (defaults to true)
81
+ * @param checkSolutions Whether to check for non-empty solutions (defaults to true)
82
+ */
83
+ void verifyErrorProblem (ReceivedProblem problem , boolean expectLineLocation = true , boolean checkSolutions = true ) {
84
+ assertLabel(problem)
85
+ assertLocations(problem, expectLineLocation)
86
+ assert problem. severity == Severity . ERROR
87
+ assert problem. fqid == ' compilation:java:compiler-err-expected'
88
+ assert problem. contextualLabel == ' \' ;\' expected'
89
+ if (checkSolutions) {
90
+ assert ! problem. solutions. empty
91
+ }
92
+ }
93
+
94
+ /**
95
+ * Verifies common properties of a warning problem.
96
+ *
97
+ * @param problem The problem to verify
98
+ * @param expectLineLocation Whether to expect a line location (defaults to true)
99
+ * @param fileLocation Optional file location for additional verification
100
+ */
101
+ void verifyWarningProblem (ReceivedProblem problem , boolean expectLineLocation = true , String fileLocation = null ) {
102
+ assertLabel(problem)
103
+ assertLocations(problem, expectLineLocation)
104
+ assert problem. severity == Severity . WARNING
105
+ assert problem. fqid == ' compilation:java:compiler-warn-redundant-cast'
106
+ assertRedundantCastInContextualLabel(problem. contextualLabel)
107
+
108
+ // Optional verification for details if file location is provided
109
+ if (fileLocation) {
110
+ assertRedundantCastInDetails(problem. details, fileLocation)
111
+ assert problem. solutions. empty
112
+ }
113
+ }
114
+
115
+ /**
116
+ * Verifies common properties of a -Werror specific error problem.
117
+ *
118
+ * @param problem The problem to verify
119
+ */
120
+ void verifyWerrorProblem (ReceivedProblem problem ) {
121
+ assertLocations(problem, false , false )
122
+ assert problem. severity == Severity . ERROR
123
+ assert problem. fqid == ' compilation:java:compiler-err-warnings-and-werror'
124
+ assert problem. contextualLabel == ' warnings found and -Werror specified'
125
+ assert ! problem. solutions. empty
126
+ assert problem. details == " error: warnings found and -Werror specified"
127
+ }
128
+
129
+ /**
130
+ * Verifies a JDK-specific warning problem with the appropriate message format.
131
+ *
132
+ * @param problem The problem to verify
133
+ * @param isJava9Compatible Whether the JDK is Java 9 compatible
134
+ */
135
+ void verifyJdkSpecificWarningProblem (ReceivedProblem problem , boolean isJava9Compatible ) {
136
+ assertLocations(problem, true )
137
+ assert problem. severity == Severity . WARNING
138
+ assert problem. fqid == ' compilation:java:compiler-warn-redundant-cast'
139
+
140
+ def message = getRedundantMessage(isJava9Compatible)
141
+ assert problem. contextualLabel == message
142
+ assert problem. details. contains(message)
143
+ }
144
+
78
145
def " problem is received when a single-file compilation failure happens" () {
79
146
given :
80
147
possibleFileLocations. put(writeJavaCausingTwoCompilationErrors(" Foo" ). absolutePath, 2 )
@@ -83,21 +150,8 @@ class JavaCompileProblemsIntegrationTest extends AbstractIntegrationSpec impleme
83
150
fails(" compileJava" )
84
151
85
152
then :
86
- verifyAll(receivedProblem(0 )) {
87
- assertLabel(it)
88
- assertLocations(it, true )
89
- severity == Severity . ERROR
90
- fqid == ' compilation:java:compiler-err-expected'
91
- contextualLabel == ' \' ;\' expected'
92
- }
93
- verifyAll(receivedProblem(1 )) {
94
- assertLabel(it)
95
- assertLocations(it, true )
96
- severity == Severity . ERROR
97
- fqid == ' compilation:java:compiler-err-expected'
98
- contextualLabel == ' \' ;\' expected'
99
- }
100
-
153
+ verifyErrorProblem(receivedProblem(0 ))
154
+ verifyErrorProblem(receivedProblem(1 ))
101
155
result. error. contains(" 2 errors\n " )
102
156
}
103
157
@@ -110,35 +164,7 @@ class JavaCompileProblemsIntegrationTest extends AbstractIntegrationSpec impleme
110
164
fails(" compileJava" )
111
165
112
166
then :
113
- verifyAll(receivedProblem(0 )) {
114
- assertLocations(it, true )
115
- severity == Severity . ERROR
116
- fqid == ' compilation:java:compiler-err-expected'
117
- contextualLabel == ' \' ;\' expected'
118
- ! solutions. empty
119
- }
120
- verifyAll(receivedProblem(1 )) {
121
- assertLocations(it, true )
122
- severity == Severity . ERROR
123
- fqid == ' compilation:java:compiler-err-expected'
124
- contextualLabel == ' \' ;\' expected'
125
- ! solutions. empty
126
- }
127
- verifyAll(receivedProblem(2 )) {
128
- assertLocations(it, true )
129
- severity == Severity . ERROR
130
- fqid == ' compilation:java:compiler-err-expected'
131
- contextualLabel == ' \' ;\' expected'
132
- ! solutions. empty
133
- }
134
- verifyAll(receivedProblem(3 )) {
135
- assertLocations(it, true )
136
- severity == Severity . ERROR
137
- fqid == ' compilation:java:compiler-err-expected'
138
- contextualLabel == ' \' ;\' expected'
139
- ! solutions. empty
140
- }
141
-
167
+ (0 .. 3 ). each { verifyErrorProblem(receivedProblem(it)) }
142
168
result. error. contains(" 4 errors\n " )
143
169
}
144
170
@@ -150,30 +176,12 @@ class JavaCompileProblemsIntegrationTest extends AbstractIntegrationSpec impleme
150
176
def result = run(" compileJava" )
151
177
152
178
then :
153
- verifyAll(receivedProblem(0 )) {
154
- assertLocations(it, true )
155
- severity == Severity . WARNING
156
- fqid == ' compilation:java:compiler-warn-redundant-cast'
157
- assertRedundantCastInContextualLabel(contextualLabel)
158
- }
159
- verifyAll(receivedProblem(1 )) {
160
- assertLocations(it, true )
161
- severity == Severity . WARNING
162
- fqid == ' compilation:java:compiler-warn-redundant-cast'
163
- assertRedundantCastInContextualLabel(contextualLabel)
164
- }
165
-
179
+ (0 .. 1 ). each { verifyWarningProblem(receivedProblem(it)) }
166
180
result. error. contains(" 2 warnings\n " )
167
181
}
168
182
169
183
void assertRedundantCastInContextualLabel (String label ) {
170
- if (JavaVersion . current(). java9Compatible) {
171
- // Above Java 9, we can use the RichDiagnosticFormatter
172
- assert label == REDUNDANT_CAST_JAVA_9_MESSAGE
173
- } else {
174
- // Below Java 9, we have to use the BasicDiagnosticFormatter
175
- assert label == REDUNDANT_CAST_JAVA_8_MESSAGE
176
- }
184
+ assert label == getRedundantMessage()
177
185
}
178
186
179
187
def " problem is received when a single-file note happens" () {
@@ -208,33 +216,7 @@ class JavaCompileProblemsIntegrationTest extends AbstractIntegrationSpec impleme
208
216
def result = run(" compileJava" )
209
217
210
218
then :
211
- verifyAll(receivedProblem(0 )) {
212
- assertLocations(it, true )
213
- severity == Severity . WARNING
214
- fqid == ' compilation:java:compiler-warn-redundant-cast'
215
- assertRedundantCastInContextualLabel(contextualLabel)
216
-
217
- }
218
- verifyAll(receivedProblem(1 )) {
219
- assertLocations(it, true )
220
- severity == Severity . WARNING
221
- fqid == ' compilation:java:compiler-warn-redundant-cast'
222
- assertRedundantCastInContextualLabel(contextualLabel)
223
- }
224
- verifyAll(receivedProblem(2 )) {
225
- assertLocations(it, true )
226
- severity == Severity . WARNING
227
- fqid == ' compilation:java:compiler-warn-redundant-cast'
228
- assertRedundantCastInContextualLabel(contextualLabel)
229
-
230
- }
231
- verifyAll(receivedProblem(3 )) {
232
- assertLocations(it, true )
233
- severity == Severity . WARNING
234
- fqid == ' compilation:java:compiler-warn-redundant-cast'
235
- assertRedundantCastInContextualLabel(contextualLabel)
236
- }
237
-
219
+ (0 .. 3 ). each { verifyWarningProblem(receivedProblem(it)) }
238
220
result. error. contains(" 4 warnings\n " )
239
221
}
240
222
@@ -247,31 +229,7 @@ class JavaCompileProblemsIntegrationTest extends AbstractIntegrationSpec impleme
247
229
def result = fails(" compileJava" )
248
230
249
231
then :
250
- verifyAll(receivedProblem(0 )) {
251
- assertLocations(it, true )
252
- severity == Severity . ERROR
253
- fqid == ' compilation:java:compiler-err-expected'
254
- contextualLabel == ' \' ;\' expected'
255
- }
256
- verifyAll(receivedProblem(1 )) {
257
- assertLocations(it, true )
258
- severity == Severity . ERROR
259
- fqid == ' compilation:java:compiler-err-expected'
260
- contextualLabel == ' \' ;\' expected'
261
- }
262
- verifyAll(receivedProblem(2 )) {
263
- assertLocations(it, true )
264
- severity == Severity . ERROR
265
- fqid == ' compilation:java:compiler-err-expected'
266
- contextualLabel == ' \' ;\' expected'
267
- }
268
- verifyAll(receivedProblem(3 )) {
269
- assertLocations(it, true )
270
- severity == Severity . ERROR
271
- fqid == ' compilation:java:compiler-err-expected'
272
- contextualLabel == ' \' ;\' expected'
273
- }
274
-
232
+ (0 .. 3 ). each { verifyErrorProblem(receivedProblem(it)) }
275
233
result. error. contains(" 4 errors\n " )
276
234
}
277
235
@@ -287,30 +245,13 @@ class JavaCompileProblemsIntegrationTest extends AbstractIntegrationSpec impleme
287
245
fails(" compileTestJava" )
288
246
289
247
then :
290
- verifyAll(receivedProblem(0 )) {
291
- assertLocations(it, true )
292
- severity == Severity . ERROR
293
- fqid == ' compilation:java:compiler-err-expected'
294
- contextualLabel == ' \' ;\' expected'
295
- }
296
- verifyAll(receivedProblem(1 )) {
297
- assertLocations(it, true )
298
- severity == Severity . ERROR
299
- fqid == ' compilation:java:compiler-err-expected'
300
- contextualLabel == ' \' ;\' expected'
301
- }
302
- verifyAll(receivedProblem(2 )) {
303
- assertLocations(it, true )
304
- severity == Severity . WARNING
305
- fqid == ' compilation:java:compiler-warn-redundant-cast'
306
- assertRedundantCastInContextualLabel(contextualLabel)
307
- }
308
- verifyAll(receivedProblem(3 )) {
309
- assertLocations(it, true )
310
- severity == Severity . WARNING
311
- fqid == ' compilation:java:compiler-warn-redundant-cast'
312
- assertRedundantCastInContextualLabel(contextualLabel)
313
- }
248
+ // Errors from test source set
249
+ verifyErrorProblem(receivedProblem(0 ))
250
+ verifyErrorProblem(receivedProblem(1 ))
251
+
252
+ // Warnings from main source set
253
+ verifyWarningProblem(receivedProblem(2 ))
254
+ verifyWarningProblem(receivedProblem(3 ))
314
255
315
256
result. error. contains(" 2 errors\n " )
316
257
result. error. contains(" 2 warnings\n " )
@@ -327,38 +268,12 @@ class JavaCompileProblemsIntegrationTest extends AbstractIntegrationSpec impleme
327
268
fails(" compileJava" )
328
269
329
270
then :
330
- // 2 warnings + 1 special error
331
- // The compiler will report a single error, implying that the warnings were treated as errors
332
- verifyAll(receivedProblem(0 )) {
333
- assertLocations(it, false , false )
334
- severity == Severity . ERROR
335
- fqid == ' compilation:java:compiler-err-warnings-and-werror'
336
- contextualLabel == ' warnings found and -Werror specified'
337
- ! solutions. empty
338
- details == " error: warnings found and -Werror specified"
339
- }
271
+ // Special -Werror error
272
+ verifyWerrorProblem(receivedProblem(0 ))
340
273
341
- // The two expected warnings are still reported as warnings
342
- verifyAll(receivedProblem(1 )) {
343
- assertLocations(it, true )
344
- severity == Severity . WARNING
345
- fqid == ' compilation:java:compiler-warn-redundant-cast'
346
- assertRedundantCastInContextualLabel(contextualLabel)
347
- assertRedundantCastInDetails(details, " $fooFileLocation :5" )
348
- solutions. empty
349
- verifyAll(getSingleOriginLocation(FileLocation )) {
350
- it. path == fooFileLocation. absolutePath
351
- }
352
-
353
- }
354
- verifyAll(receivedProblem(2 )) {
355
- assertLocations(it, true )
356
- severity == Severity . WARNING
357
- fqid == ' compilation:java:compiler-warn-redundant-cast'
358
- assertRedundantCastInContextualLabel(contextualLabel)
359
- assertRedundantCastInDetails(details, " $fooFileLocation :9" )
360
- solutions. empty
361
- }
274
+ // The two expected warnings
275
+ verifyWarningProblem(receivedProblem(1 ), true , " $fooFileLocation :11" )
276
+ verifyWarningProblem(receivedProblem(2 ), true , " $fooFileLocation :7" )
362
277
363
278
result. error. contains(" 1 error\n " )
364
279
result. error. contains(" 2 warnings\n " )
@@ -454,14 +369,7 @@ class JavaCompileProblemsIntegrationTest extends AbstractIntegrationSpec impleme
454
369
455
370
then :
456
371
outputContains(DiagnosticToProblemListener . FORMATTER_FALLBACK_MESSAGE )
457
- verifyAll(receivedProblem(0 )) {
458
- assertLocations(it, true )
459
- severity == Severity . WARNING
460
- fqid == ' compilation:java:compiler-warn-redundant-cast'
461
- contextualLabel == REDUNDANT_CAST_JAVA_8_MESSAGE
462
- // In JDK8, the compiler will not simplify the type to just "String"
463
- details. contains(REDUNDANT_CAST_JAVA_8_MESSAGE )
464
- }
372
+ verifyJdkSpecificWarningProblem(receivedProblem(0 ), false ) // JDK8 is not Java 9 compatible
465
373
}
466
374
467
375
@Issue (" https://github.com/gradle/gradle/pull/29141" )
@@ -481,13 +389,7 @@ class JavaCompileProblemsIntegrationTest extends AbstractIntegrationSpec impleme
481
389
482
390
then :
483
391
! result. error. contains(DiagnosticToProblemListener . FORMATTER_FALLBACK_MESSAGE )
484
- verifyAll(receivedProblem(0 )) {
485
- assertLocations(it, true )
486
- severity == Severity . WARNING
487
- fqid == ' compilation:java:compiler-warn-redundant-cast'
488
- contextualLabel == REDUNDANT_CAST_JAVA_9_MESSAGE
489
- details. contains(REDUNDANT_CAST_JAVA_9_MESSAGE )
490
- }
392
+ verifyJdkSpecificWarningProblem(receivedProblem(0 ), true ) // These JDKs are Java 9 compatible
491
393
492
394
where :
493
395
jdk << AvailableJavaHomes . getAvailableJdks {
@@ -693,19 +595,16 @@ class JavaCompileProblemsIntegrationTest extends AbstractIntegrationSpec impleme
693
595
"""
694
596
}
695
597
696
- def void assertRedundantCastInDetails (String details , String fooFileLocation ) {
697
- if ( JavaVersion . current() . java9Compatible) {
698
- // Above Java 9, we can use the RichDiagnosticFormatter
699
- assert details == """ \
700
- ${ fooFileLocation} : warning: [cast] $R EDUNDANT_CAST_JAVA_9_MESSAGE
598
+ void assertRedundantCastInDetails (String details , String fooFileLocation ) {
599
+ // Determine which message format to use based on Java version
600
+ // Define the expected details format
601
+ assert details == """ \
602
+ ${ fooFileLocation} : warning: [cast] ${ getRedundantMessage() }
701
603
String s = (String)"Hello World";
702
604
^"""
703
- } else {
704
- // Below Java 9, we have to use the BasicDiagnosticFormatter
705
- assert details == """ \
706
- ${ fooFileLocation} : warning: [cast] $REDUNDANT_CAST_JAVA_8_MESSAGE
707
- String s = (String)"Hello World";
708
- ^"""
709
- }
605
+ }
606
+
607
+ String getRedundantMessage (boolean isJava9Compatible = JavaVersion . current(). java9Compatible) {
608
+ " redundant cast to ${ isJava9Compatible ? "" : "java.lang."} String"
710
609
}
711
610
}
0 commit comments