From c23e38717d67851d45a4af2f159a4812b08815be Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Sun, 14 Aug 2022 14:36:15 +0200 Subject: [PATCH] Build test code using JDK 17 This enables usage of text blocks, which in combination with IntelliJ IDEA's _language injection_ functionality greatly simplify writing `BugChecker` tests. --- error-prone-contrib/README.md | 17 +- .../bugpatterns/AmbiguousJsonCreatorTest.java | 231 +++++++++--------- pom.xml | 32 ++- 3 files changed, 155 insertions(+), 125 deletions(-) diff --git a/error-prone-contrib/README.md b/error-prone-contrib/README.md index 976abb050b3..c6df947ad19 100644 --- a/error-prone-contrib/README.md +++ b/error-prone-contrib/README.md @@ -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 @@ -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 diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/AmbiguousJsonCreatorTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/AmbiguousJsonCreatorTest.java index a59bb2e7f02..c3edb5dcf43 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/AmbiguousJsonCreatorTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/AmbiguousJsonCreatorTest.java @@ -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(); } @@ -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); } } diff --git a/pom.xml b/pom.xml index 18c519a12f7..5deaab37405 100644 --- a/pom.xml +++ b/pom.xml @@ -150,6 +150,7 @@ 3.0.0 1.0 11 + 17 3.6.3 4.6.1 1.0.1 @@ -854,6 +855,8 @@ true ${version.jdk} ${version.jdk} + ${version.jdk.test} + ${version.jdk.test} false @@ -897,7 +900,7 @@ src/main/resources/**/*.properties,src/test/resources/**/*.properties - ${version.jdk} + ${version.jdk.test} ${version.maven} @@ -1740,5 +1743,32 @@ + + + idea + + + idea.maven.embedder.version + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + ${version.jdk.test} + + + + + +