diff --git a/build.gradle.kts b/build.gradle.kts index 42276ebba..4cc34e29d 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -39,6 +39,7 @@ dependencies { compileOnly("org.projectlombok:lombok:latest.release") annotationProcessor("org.projectlombok:lombok:latest.release") + implementation("org.assertj:assertj-core:latest.release") implementation("org.testcontainers:testcontainers:latest.release") testImplementation("org.openrewrite:rewrite-java-17") @@ -60,5 +61,13 @@ dependencies { testRuntimeOnly("org.testcontainers:nginx:latest.release") // testImplementation("org.hamcrest:hamcrest:latest.release") -// testImplementation("org.assertj:assertj-core:latest.release") + + // Refaster style recipes need the rewrite-templating annotation processor and dependency for generated recipes + // https://github.com/openrewrite/rewrite-templating/releases + annotationProcessor("org.openrewrite:rewrite-templating:$rewriteVersion") + implementation("org.openrewrite:rewrite-templating:$rewriteVersion") + // The `@BeforeTemplate` and `@AfterTemplate` annotations are needed for refaster style recipes + compileOnly("com.google.errorprone:error_prone_core:2.19.1") { + exclude("com.google.auto.service", "auto-service-annotations") + } } diff --git a/src/main/java/org/openrewrite/java/testing/assertj/SimplifyAssertExpectations.java b/src/main/java/org/openrewrite/java/testing/assertj/SimplifyAssertExpectations.java new file mode 100644 index 000000000..12ce7e9c1 --- /dev/null +++ b/src/main/java/org/openrewrite/java/testing/assertj/SimplifyAssertExpectations.java @@ -0,0 +1,79 @@ +/* + * 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.assertj; + +import com.google.errorprone.refaster.annotation.AfterTemplate; +import com.google.errorprone.refaster.annotation.BeforeTemplate; +import org.openrewrite.java.template.Primitive; +import org.openrewrite.java.template.RecipeDescriptor; + +import static org.assertj.core.api.Assertions.assertThat; + +@RecipeDescriptor( + name = "Simplify the expectations of assertions", + description = "Simplifies expectations on various assertions." +) +public class SimplifyAssertExpectations { + + @RecipeDescriptor( + name = "Simplify `assertThat(int).isEqualTo(0)`", + description = "Simplify `assertThat(int).isEqualTo(0)` to `assertThat(int).isZero()`." + ) + public static class SimplifyToIsZero { + @BeforeTemplate + void before(@Primitive Integer i) { + assertThat(i).isEqualTo(0); + } + + @AfterTemplate + void after(@Primitive int i) { + assertThat(i).isZero(); + } + } + + @RecipeDescriptor( + name = "Simplify `assertThat(o).isEqualTo(null)`", + description = "Simplify `assertThat(o).isEqualTo(null)` to `assertThat(o).isNull()`." + ) + public static class SimplifyToIsNull { + @BeforeTemplate + void before(Object o) { + assertThat(o).isEqualTo(null); + } + + @AfterTemplate + void after(Object o) { + assertThat(o).isNull(); + } + } + + @RecipeDescriptor( + name = "Simplify `assertThat(i >= j).isTrue()`", + description = "Simplify `assertThat(i >= j).isTrue()` to `assertThat(i).isGreaterThanOrEqualTo(j)`." + ) + public static class SimplifyGreaterEqualComparison { + + @BeforeTemplate + void before(int i, int j) { + assertThat(i >= j).isTrue(); + } + + @AfterTemplate + void after(int i, int j) { + assertThat(i).isGreaterThanOrEqualTo(j); + } + } +} diff --git a/src/main/resources/META-INF/rewrite/assertj.yml b/src/main/resources/META-INF/rewrite/assertj.yml index 2695a5dd0..c6b8ab0b5 100644 --- a/src/main/resources/META-INF/rewrite/assertj.yml +++ b/src/main/resources/META-INF/rewrite/assertj.yml @@ -26,6 +26,7 @@ recipeList: - org.openrewrite.java.testing.assertj.JUnitToAssertj - org.openrewrite.java.testing.assertj.StaticImports - org.openrewrite.java.testing.assertj.SimplifyChainedAssertJAssertions + - org.openrewrite.java.testing.assertj.SimplifyAssertExpectationsRecipes - org.openrewrite.java.testing.assertj.IsEqualToEmptyString --- diff --git a/src/test/java/org/openrewrite/java/testing/assertj/SimplifyAssertExpectationsTest.java b/src/test/java/org/openrewrite/java/testing/assertj/SimplifyAssertExpectationsTest.java new file mode 100644 index 000000000..2c67879fe --- /dev/null +++ b/src/test/java/org/openrewrite/java/testing/assertj/SimplifyAssertExpectationsTest.java @@ -0,0 +1,146 @@ +/* + * 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.assertj; + +import org.junit.jupiter.api.Test; +import org.openrewrite.DocumentExample; +import org.openrewrite.java.JavaParser; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.java.Assertions.java; + +class SimplifyAssertExpectationsTest implements RewriteTest { + + @Override + public void defaults(RecipeSpec spec) { + spec.parser(JavaParser.fromJavaVersion().classpath("assertj-core")) + .recipe(new SimplifyAssertExpectationsRecipes()); + } + + @DocumentExample + @Test + void simplifyIsZero() { + rewriteRun( + //language=java + java( + """ + import static org.assertj.core.api.Assertions.assertThat; + import java.util.List; + import org.junit.jupiter.api.Test; + + class Test { + + @Test + void simpleTest() { + List list = List.of(); + assertThat(list.size()).isEqualTo(0); + assertThat(0).isEqualTo(0); + assertThat(list.size()).isNotEqualTo(0); + } + } + """, + """ + import static org.assertj.core.api.Assertions.assertThat; + import java.util.List; + import org.junit.jupiter.api.Test; + + class Test { + + @Test + void simpleTest() { + List list = List.of(); + assertThat(list.size()).isZero(); + assertThat(0).isZero(); + assertThat(list.size()).isNotZero(); + } + } + """ + ) + ); + } + + @Test + void simplifyIsNull() { + rewriteRun( + //language=java + java( + """ + import static org.assertj.core.api.Assertions.assertThat; + import org.junit.jupiter.api.Test; + + class Test { + + @Test + void simpleTest() { + String s = null; + assertThat(s).isEqualTo(null); + assertThat(s).isNotEqualTo(null); + } + } + """, + """ + import static org.assertj.core.api.Assertions.assertThat; + import org.junit.jupiter.api.Test; + + class Test { + + @Test + void simpleTest() { + String s = null; + assertThat(s).isNull(); + assertThat(s).isNull(); + } + } + """ + ) + ); + } + + @Test + void simplifyIsGreaterThanOrEqual() { + rewriteRun( + //language=java + java( + """ + import static org.assertj.core.api.Assertions.assertThat; + import org.junit.jupiter.api.Test; + + class Test { + + @Test + void simpleTest() { + assertThat(2 >= 1).isTrue(); + } + } + """, + """ + import static org.assertj.core.api.Assertions.assertThat; + import org.junit.jupiter.api.Test; + + class Test { + + @Test + void simpleTest() { + assertThat(2).isGreaterThanOrEqualTo(1); + } + } + """ + ) + ); + } + +}