Skip to content

Commit

Permalink
Build test code using JDK 17
Browse files Browse the repository at this point in the history
This enables usage of text blocks, which in combination with IntelliJ
IDEA's _language injection_ functionality greatly simplify writing
`BugChecker` tests.
  • Loading branch information
Stephan202 committed Aug 14, 2022
1 parent 773226a commit c23e387
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 125 deletions.
17 changes: 6 additions & 11 deletions error-prone-contrib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,11 @@ Two other goals that one may find relevant:
`target/pit-reports/index.html` files. For more information check the [PIT
Maven plugin][pitest-maven].

When running the project's tests in IntelliJ IDEA, you might see the following
error:
```
java: exporting a package from system module jdk.compiler is not allowed with --release
```

If this happens, go to _Settings -> Build, Execution, Deployment -> Compiler ->
Java Compiler_ and deselect the option _Use '--release' option for
cross-compilation (Java 9 and later)_. See [IDEA-288052][idea-288052] for
details.
The `BugChecker` implementations provided by this project are tested using
Error Prone's `CompilationTestHelper` and `BugCheckerRefactoringTestHelper`
classes. These utilities accept text blocks containing inline Java source code.
To ease modification of this inline source code, consider using IntelliJ IDEA's
[language injection][idea-language-injection] feature.

### Contribution guidelines

Expand Down Expand Up @@ -342,7 +337,7 @@ Refaster's expressiveness:
[forbidden-apis]: https://github.com/policeman-tools/forbidden-apis
[fossa]: https://fossa.io
[google-java-format]: https://github.com/google/google-java-format
[idea-288052]: https://youtrack.jetbrains.com/issue/IDEA-288052
[idea-language-injection]: https://www.jetbrains.com/help/idea/using-language-injections.html
[maven]: https://maven.apache.org
[modernizer-maven-plugin]: https://github.com/gaul/modernizer-maven-plugin
[pitest]: https://pitest.org
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,99 +20,100 @@ void identification() {
compilationTestHelper
.addSourceLines(
"Container.java",
"import com.fasterxml.jackson.annotation.JsonCreator;",
"import com.fasterxml.jackson.annotation.JsonValue;",
"",
"interface Container {",
" enum A {",
" FOO(1);",
"",
" private final int i;",
"",
" A(int i) {",
" this.i = i;",
" }",
"",
" // BUG: Diagnostic matches: X",
" @JsonCreator",
" public static A of(int i) {",
" return FOO;",
" }",
" }",
"",
" enum B {",
" FOO(1);",
"",
" private final int i;",
"",
" B(int i) {",
" this.i = i;",
" }",
"",
" @JsonCreator(mode = JsonCreator.Mode.DELEGATING)",
" public static B of(int i) {",
" return FOO;",
" }",
" }",
"",
" enum C {",
" FOO(1, \"s\");",
"",
" @JsonValue private final int i;",
" private final String s;",
"",
" C(int i, String s) {",
" this.i = i;",
" this.s = s;",
" }",
"",
" // BUG: Diagnostic matches: X",
" @JsonCreator",
" public static C of(int i) {",
" return FOO;",
" }",
" }",
"",
" enum D {",
" FOO(1, \"s\");",
"",
" private final int i;",
" private final String s;",
"",
" D(int i, String s) {",
" this.i = i;",
" this.s = s;",
" }",
"",
" @JsonCreator",
" public static D of(int i, String s) {",
" return FOO;",
" }",
" }",
"",
" enum E {",
" FOO;",
"",
" // BUG: Diagnostic matches: X",
" @JsonCreator",
" public static E of(String s) {",
" return FOO;",
" }",
" }",
"",
" class F {",
" private final String s;",
"",
" F(String s) {",
" this.s = s;",
" }",
"",
" @JsonCreator",
" public static F of(String s) {",
" return new F(s);",
" }",
" }",
"}")
"""
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
interface Container {
enum A {
FOO(1);
private final int i;
A(int i) {
this.i = i;
}
// BUG: Diagnostic matches: X
@JsonCreator
public static A of(int i) {
return FOO;
}
}
enum B {
FOO(1);
private final int i;
B(int i) {
this.i = i;
}
@JsonCreator(mode = JsonCreator.Mode.DELEGATING)
public static B of(int i) {
return FOO;
}
}
enum C {
FOO(1, \"s\");
@JsonValue private final int i;
private final String s;
C(int i, String s) {
this.i = i;
this.s = s;
}
// BUG: Diagnostic matches: X
@JsonCreator
public static C of(int i) {
return FOO;
}
}
enum D {
FOO(1, \"s\");
private final int i;
private final String s;
D(int i, String s) {
this.i = i;
this.s = s;
}
@JsonCreator
public static D of(int i, String s) {
return FOO;
}
}
enum E {
FOO;
// BUG: Diagnostic matches: X
@JsonCreator
public static E of(String s) {
return FOO;
}
}
class F {
private final String s;
F(String s) {
this.s = s;
}
@JsonCreator
public static F of(String s) {
return new F(s);
}
}
}""")
.doTest();
}

Expand All @@ -121,28 +122,32 @@ void replacement() {
refactoringTestHelper
.addInputLines(
"in/A.java",
"import com.fasterxml.jackson.annotation.JsonCreator;",
"",
"enum A {",
" FOO;",
"",
" @JsonCreator",
" public static A of(String s) {",
" return FOO;",
" }",
"}")
"""
import com.fasterxml.jackson.annotation.JsonCreator;
enum A {
FOO;
@JsonCreator
public static A of(String s) {
return FOO;
}
}
""")
.addOutputLines(
"out/A.java",
"import com.fasterxml.jackson.annotation.JsonCreator;",
"",
"enum A {",
" FOO;",
"",
" @JsonCreator(mode = JsonCreator.Mode.DELEGATING)",
" public static A of(String s) {",
" return FOO;",
" }",
"}")
"""
import com.fasterxml.jackson.annotation.JsonCreator;
enum A {
FOO;
@JsonCreator(mode = JsonCreator.Mode.DELEGATING)
public static A of(String s) {
return FOO;
}
}
""")
.doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH);
}
}
32 changes: 31 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@
<version.findbugs-format-string>3.0.0</version.findbugs-format-string>
<version.guava-beta-checker>1.0</version.guava-beta-checker>
<version.jdk>11</version.jdk>
<version.jdk.test>17</version.jdk.test>
<version.maven>3.6.3</version.maven>
<version.mockito>4.6.1</version.mockito>
<version.nopen-checker>1.0.1</version.nopen-checker>
Expand Down Expand Up @@ -854,6 +855,8 @@
<parameters>true</parameters>
<source>${version.jdk}</source>
<target>${version.jdk}</target>
<testSource>${version.jdk.test}</testSource>
<testTarget>${version.jdk.test}</testTarget>
<!-- Erroneously inverted logic... for details, see
https://issues.apache.org/jira/browse/MCOMPILER-209. -->
<useIncrementalCompilation>false</useIncrementalCompilation>
Expand Down Expand Up @@ -897,7 +900,7 @@
<includes>src/main/resources/**/*.properties,src/test/resources/**/*.properties</includes>
</requireEncoding>
<requireJavaVersion>
<version>${version.jdk}</version>
<version>${version.jdk.test}</version>
</requireJavaVersion>
<requireMavenVersion>
<version>${version.maven}</version>
Expand Down Expand Up @@ -1740,5 +1743,32 @@
</plugins>
</build>
</profile>
<profile>
<!-- This profile is active when the project is loaded into
IntelliJ IDEA. Drop it once IDEA properly supports the `testSource`
and `testTarget` settings of the `maven-compiler-plugin`, such that
specifying different language levels for main and test code works
out of the box. See
https://youtrack.jetbrains.com/issue/IDEA-85478. -->
<id>idea</id>
<activation>
<property>
<name>idea.maven.embedder.version</name>
</property>
</activation>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<release>${version.jdk.test}</release>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
</profiles>
</project>

0 comments on commit c23e387

Please sign in to comment.