Skip to content

Commit 2acbe0f

Browse files
authored
mapstruct#1633 Add support for an alternative line in the diagnostics report
* This should be used as a last resort when the compilers report the diagnostic on a wrong line * The NullValuePropertyMappingTest uses repeatable annotations that reports errors on wrong lines in javac JDK-8042710
1 parent 288813f commit 2acbe0f

4 files changed

Lines changed: 30 additions & 7 deletions

File tree

processor/src/test/java/org/mapstruct/ap/test/nullvaluepropertymapping/NullValuePropertyMappingTest.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import java.util.Arrays;
99
import java.util.function.BiConsumer;
1010

11-
import org.junit.Ignore;
1211
import org.junit.Test;
1312
import org.junit.runner.RunWith;
1413
import org.mapstruct.ap.testutil.IssueKey;
@@ -104,14 +103,14 @@ public void testStrategyDefaultAppliedOnForgedMethod() {
104103
}
105104

106105
@Test
107-
@Ignore // test gives different results for JDK and JDT
108106
@WithClasses(ErroneousCustomerMapper1.class)
109107
@ExpectedCompilationOutcome(
110108
value = CompilationResult.FAILED,
111109
diagnostics = {
112110
@Diagnostic(type = ErroneousCustomerMapper1.class,
113111
kind = javax.tools.Diagnostic.Kind.ERROR,
114112
line = 20,
113+
alternativeLine = 22, // Javac wrong error reporting on repeatable annotations JDK-8042710
115114
messageRegExp = "Default value and nullValuePropertyMappingStrategy are both defined in @Mapping, " +
116115
"either define a defaultValue or an nullValuePropertyMappingStrategy.")
117116
}
@@ -120,14 +119,14 @@ public void testBothDefaultValueAndNvpmsDefined() {
120119
}
121120

122121
@Test
123-
@Ignore // test gives different results for JDK and JDT
124122
@WithClasses(ErroneousCustomerMapper2.class)
125123
@ExpectedCompilationOutcome(
126124
value = CompilationResult.FAILED,
127125
diagnostics = {
128126
@Diagnostic(type = ErroneousCustomerMapper2.class,
129127
kind = javax.tools.Diagnostic.Kind.ERROR,
130128
line = 20,
129+
alternativeLine = 22, // Javac wrong error reporting on repeatable annotations JDK-8042710
131130
messageRegExp = "Expression and nullValuePropertyMappingStrategy are both defined in @Mapping, " +
132131
"either define an expression or an nullValuePropertyMappingStrategy.")
133132
}
@@ -136,14 +135,14 @@ public void testBothExpressionAndNvpmsDefined() {
136135
}
137136

138137
@Test
139-
@Ignore // test gives different results for JDK and JDT
140138
@WithClasses(ErroneousCustomerMapper3.class)
141139
@ExpectedCompilationOutcome(
142140
value = CompilationResult.FAILED,
143141
diagnostics = {
144142
@Diagnostic(type = ErroneousCustomerMapper3.class,
145143
kind = javax.tools.Diagnostic.Kind.ERROR,
146144
line = 20,
145+
alternativeLine = 22, // Javac wrong error reporting on repeatable annotations JDK-8042710
147146
messageRegExp = "DefaultExpression and nullValuePropertyMappingStrategy are both defined in " +
148147
"@Mapping, either define a defaultExpression or an nullValuePropertyMappingStrategy.")
149148
}
@@ -152,14 +151,14 @@ public void testBothDefaultExpressionAndNvpmsDefined() {
152151
}
153152

154153
@Test
155-
@Ignore // test gives different results for JDK and JDT
156154
@WithClasses(ErroneousCustomerMapper4.class)
157155
@ExpectedCompilationOutcome(
158156
value = CompilationResult.FAILED,
159157
diagnostics = {
160158
@Diagnostic(type = ErroneousCustomerMapper4.class,
161159
kind = javax.tools.Diagnostic.Kind.ERROR,
162160
line = 20,
161+
alternativeLine = 22, // Javac wrong error reporting on repeatable annotations JDK-8042710
163162
messageRegExp = "Constant and nullValuePropertyMappingStrategy are both defined in @Mapping, " +
164163
"either define a constant or an nullValuePropertyMappingStrategy.")
165164
}
@@ -168,14 +167,14 @@ public void testBothConstantAndNvpmsDefined() {
168167
}
169168

170169
@Test
171-
@Ignore // test gives different results for JDK and JDT
172170
@WithClasses(ErroneousCustomerMapper5.class)
173171
@ExpectedCompilationOutcome(
174172
value = CompilationResult.FAILED,
175173
diagnostics = {
176174
@Diagnostic(type = ErroneousCustomerMapper5.class,
177175
kind = javax.tools.Diagnostic.Kind.ERROR,
178176
line = 20,
177+
alternativeLine = 22, // Javac wrong error reporting on repeatable annotations JDK-8042710
179178
messageRegExp = "Ignore and nullValuePropertyMappingStrategy are both defined in @Mapping, " +
180179
"either define ignore or an nullValuePropertyMappingStrategy.")
181180
}

processor/src/test/java/org/mapstruct/ap/testutil/compilation/annotation/Diagnostic.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@
3535
*/
3636
long line() default -1;
3737

38+
/**
39+
* In case compilers report diagnostics on different lines this can be used as the alternative expected line number
40+
* of the diagnostic.
41+
* <p>
42+
* This should be used as a last resort when the compilers report the diagnostic on a wrong line.
43+
*
44+
* @return The alternative line number of the diagnostic.
45+
*/
46+
long alternativeLine() default -1;
47+
3848
/**
3949
* A regular expression matching the expected message of the diagnostic.
4050
* Wild-cards matching any character (".*") will be added to the beginning

processor/src/test/java/org/mapstruct/ap/testutil/compilation/model/DiagnosticDescriptor.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,18 @@ public class DiagnosticDescriptor {
2626
private final String sourceFileName;
2727
private final Kind kind;
2828
private final Long line;
29+
private final Long alternativeLine;
2930
private final String message;
3031

3132
private DiagnosticDescriptor(String sourceFileName, Kind kind, Long line, String message) {
33+
this( sourceFileName, kind, line, null, message );
34+
}
35+
36+
private DiagnosticDescriptor(String sourceFileName, Kind kind, Long line, Long alternativeLine, String message) {
3237
this.sourceFileName = sourceFileName;
3338
this.kind = kind;
3439
this.line = line;
40+
this.alternativeLine = alternativeLine;
3541
this.message = message;
3642
}
3743

@@ -43,6 +49,7 @@ public static DiagnosticDescriptor forDiagnostic(Diagnostic diagnostic) {
4349
soureFileName,
4450
diagnostic.kind(),
4551
diagnostic.line() != -1 ? diagnostic.line() : null,
52+
diagnostic.alternativeLine() != -1 ? diagnostic.alternativeLine() : null,
4653
diagnostic.messageRegExp()
4754
);
4855
}
@@ -135,6 +142,10 @@ public Long getLine() {
135142
return line;
136143
}
137144

145+
public Long getAlternativeLine() {
146+
return alternativeLine;
147+
}
148+
138149
public String getMessage() {
139150
return message;
140151
}

processor/src/test/java/org/mapstruct/ap/testutil/runner/CompilingStatement.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,10 @@ private void assertDiagnostics(List<DiagnosticDescriptor> actualDiagnostics,
269269
if ( expected.getSourceFileName() != null ) {
270270
assertThat( actual.getSourceFileName() ).isEqualTo( expected.getSourceFileName() );
271271
}
272-
if ( expected.getLine() != null ) {
272+
if ( expected.getLine() != null && expected.getAlternativeLine() != null ) {
273+
assertThat( actual.getLine() ).isIn( expected.getLine(), expected.getAlternativeLine() );
274+
}
275+
else if ( expected.getLine() != null ) {
273276
assertThat( actual.getLine() ).isEqualTo( expected.getLine() );
274277
}
275278
assertThat( actual.getKind() ).isEqualTo( expected.getKind() );

0 commit comments

Comments
 (0)