diff --git a/.gitignore b/.gitignore index d213cf5c9..d622dc1d1 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ out/ bin/ *.log .vscode/ +src/main/generated/ \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index e7ccd9904..c1a91eafc 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -37,6 +37,8 @@ dependencies { implementation("org.openrewrite.recipe:rewrite-static-analysis:$rewriteVersion") runtimeOnly("org.openrewrite:rewrite-java-17") + compileOnly("org.junit.jupiter:junit-jupiter-engine:latest.release") + compileOnly("org.projectlombok:lombok:latest.release") annotationProcessor("org.projectlombok:lombok:latest.release") @@ -47,6 +49,12 @@ dependencies { testImplementation("org.openrewrite:rewrite-kotlin:$rewriteVersion") testImplementation("org.openrewrite.gradle.tooling:model:$rewriteVersion") + annotationProcessor("org.openrewrite:rewrite-templating:${rewriteVersion}") + implementation("org.openrewrite:rewrite-templating:${rewriteVersion}") + compileOnly("com.google.errorprone:error_prone_core:2.19.1:with-dependencies") { + exclude("com.google.auto.service", "auto-service-annotations") + } + testRuntimeOnly("org.gradle:gradle-tooling-api:latest.release") testRuntimeOnly("com.tngtech.archunit:archunit:0.23.1") diff --git a/src/main/java/org/openrewrite/java/testing/cleanup/AssertLiteralBooleanToFail.java b/src/main/java/org/openrewrite/java/testing/cleanup/AssertLiteralBooleanToFail.java new file mode 100644 index 000000000..e1e53aea7 --- /dev/null +++ b/src/main/java/org/openrewrite/java/testing/cleanup/AssertLiteralBooleanToFail.java @@ -0,0 +1,49 @@ +/* + * Copyright 2021 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.java.testing.cleanup; + +import com.google.errorprone.refaster.annotation.AfterTemplate; +import com.google.errorprone.refaster.annotation.BeforeTemplate; +import com.google.errorprone.refaster.annotation.UseImportPolicy; +import org.openrewrite.java.template.RecipeDescriptor; + +import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS; +import static org.junit.jupiter.api.Assertions.*; + +@RecipeDescriptor( + name = "Replace JUnit `assertTrue(false, \"reason\")` and `assertFalse(true, \"reason\")` with `fail(\"reason\")`", + description = "Using fail is more direct and clear." +) +public class AssertLiteralBooleanToFail { + + @BeforeTemplate + void assertFalseBefore(String message) { + assertFalse(true, message); + } + + @BeforeTemplate + void assertTrueBefore(String message) { + assertTrue(false, message); + } + + @AfterTemplate + // This annotation does not get taken into account + // resulting in Assertions.fail(message) being outputted + @UseImportPolicy(value = STATIC_IMPORT_ALWAYS) + void after(String message) { + fail(message); + } +} diff --git a/src/test/java/org/openrewrite/java/testing/cleanup/AssertLiteralBooleanToFailTest.java b/src/test/java/org/openrewrite/java/testing/cleanup/AssertLiteralBooleanToFailTest.java new file mode 100644 index 000000000..da773f4b1 --- /dev/null +++ b/src/test/java/org/openrewrite/java/testing/cleanup/AssertLiteralBooleanToFailTest.java @@ -0,0 +1,117 @@ +/* + * Copyright 2024 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.java.testing.cleanup; + +import org.junit.jupiter.api.Test; +import org.openrewrite.DocumentExample; +import org.openrewrite.InMemoryExecutionContext; +import org.openrewrite.java.JavaParser; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.java.Assertions.java; + +class AssertLiteralBooleanToFailTest implements RewriteTest { + + @Override + public void defaults(RecipeSpec spec) { + spec + .parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "junit-jupiter-api-5.9")) + .recipe(new AssertLiteralBooleanToFailRecipe()); + } + + @Test + @DocumentExample + @SuppressWarnings("SimplifiableAssertion") + void assertWithStaticImports() { + //language=java + rewriteRun( + java( + """ + import static org.junit.jupiter.api.Assertions.assertFalse; + import static org.junit.jupiter.api.Assertions.assertTrue; + + public class Test { + void test() { + assertFalse(true, "assert false true"); + assertTrue(false, "assert true false"); + } + } + """, + """ + import org.junit.jupiter.api.Assertions; + + public class Test { + void test() { + Assertions.fail("assert false true"); + Assertions.fail("assert true false"); + } + } + """ + ) + ); + } + + @Test + @SuppressWarnings("SimplifiableAssertion") + void assertWithAssertionsImport() { + //language=java + rewriteRun( + java( + """ + import org.junit.jupiter.api.Assertions; + + public class Test { + void test() { + Assertions.assertFalse(true, "assert false true"); + Assertions.assertTrue(false, "assert true false"); + } + } + """, + """ + import org.junit.jupiter.api.Assertions; + + public class Test { + void test() { + Assertions.fail("assert false true"); + Assertions.fail("assert true false"); + } + } + """ + ) + ); + } + + @Test + void noChangeWhenNotLiteral() { + //language=java + rewriteRun( + java( + """ + import static org.junit.jupiter.api.Assertions.assertFalse; + import static org.junit.jupiter.api.Assertions.assertTrue; + + public class Test { + void test(boolean a, Object b) { + assertTrue(a, "message"); + assertFalse(b.equals("foo"), "message"); + } + } + """ + ) + ); + } +}