Skip to content

Commit 832838f

Browse files
committed
Deduplicate verification code
1 parent 9e42805 commit 832838f

File tree

1 file changed

+99
-200
lines changed

1 file changed

+99
-200
lines changed

platforms/jvm/language-java/src/integTest/groovy/org/gradle/api/tasks/compile/JavaCompileProblemsIntegrationTest.groovy

Lines changed: 99 additions & 200 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ class JavaCompileProblemsIntegrationTest extends AbstractIntegrationSpec impleme
4242
/**
4343
* A map of all possible file locations, and the number of occurrences we expect to find in the problems.
4444
*/
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'
4745
private final Map<String, Integer> possibleFileLocations = [:]
4846

4947
/**
@@ -75,6 +73,75 @@ class JavaCompileProblemsIntegrationTest extends AbstractIntegrationSpec impleme
7573
}
7674
}
7775

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+
78145
def "problem is received when a single-file compilation failure happens"() {
79146
given:
80147
possibleFileLocations.put(writeJavaCausingTwoCompilationErrors("Foo").absolutePath, 2)
@@ -83,21 +150,8 @@ class JavaCompileProblemsIntegrationTest extends AbstractIntegrationSpec impleme
83150
fails("compileJava")
84151

85152
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))
101155
result.error.contains("2 errors\n")
102156
}
103157

@@ -110,35 +164,7 @@ class JavaCompileProblemsIntegrationTest extends AbstractIntegrationSpec impleme
110164
fails("compileJava")
111165

112166
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)) }
142168
result.error.contains("4 errors\n")
143169
}
144170

@@ -150,30 +176,12 @@ class JavaCompileProblemsIntegrationTest extends AbstractIntegrationSpec impleme
150176
def result = run("compileJava")
151177

152178
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)) }
166180
result.error.contains("2 warnings\n")
167181
}
168182

169183
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()
177185
}
178186

179187
def "problem is received when a single-file note happens"() {
@@ -208,33 +216,7 @@ class JavaCompileProblemsIntegrationTest extends AbstractIntegrationSpec impleme
208216
def result = run("compileJava")
209217

210218
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)) }
238220
result.error.contains("4 warnings\n")
239221
}
240222

@@ -247,31 +229,7 @@ class JavaCompileProblemsIntegrationTest extends AbstractIntegrationSpec impleme
247229
def result = fails("compileJava")
248230

249231
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)) }
275233
result.error.contains("4 errors\n")
276234
}
277235

@@ -287,30 +245,13 @@ class JavaCompileProblemsIntegrationTest extends AbstractIntegrationSpec impleme
287245
fails("compileTestJava")
288246

289247
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))
314255

315256
result.error.contains("2 errors\n")
316257
result.error.contains("2 warnings\n")
@@ -327,38 +268,12 @@ class JavaCompileProblemsIntegrationTest extends AbstractIntegrationSpec impleme
327268
fails("compileJava")
328269

329270
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))
340273

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")
362277

363278
result.error.contains("1 error\n")
364279
result.error.contains("2 warnings\n")
@@ -454,14 +369,7 @@ class JavaCompileProblemsIntegrationTest extends AbstractIntegrationSpec impleme
454369

455370
then:
456371
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
465373
}
466374

467375
@Issue("https://github.com/gradle/gradle/pull/29141")
@@ -481,13 +389,7 @@ class JavaCompileProblemsIntegrationTest extends AbstractIntegrationSpec impleme
481389

482390
then:
483391
!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
491393

492394
where:
493395
jdk << AvailableJavaHomes.getAvailableJdks {
@@ -693,19 +595,16 @@ class JavaCompileProblemsIntegrationTest extends AbstractIntegrationSpec impleme
693595
"""
694596
}
695597

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] $REDUNDANT_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()}
701603
String s = (String)"Hello World";
702604
^"""
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"
710609
}
711610
}

0 commit comments

Comments
 (0)