From fc9d8b46f40b336d4ab0149078861f7743758a2b Mon Sep 17 00:00:00 2001 From: Karsten Thoms Date: Sun, 22 Dec 2024 12:26:56 +0100 Subject: [PATCH] Add recipe for replacing deprecated method AbstractDateAssert (#655) * Add recipe for replacing deprecated method AbstractDateAssert This adds a recipe to replace org.assertj.core.api.AbstractDateAssert.isEqualToIgnoringMillis(java.util.Date) by the proposed method isCloseTo() * Include IsEqualToIgnoringMillisToIsCloseTo just before StaticImports * Reference generated recipe, not the test --------- Co-authored-by: Tim te Beek --- build.gradle.kts | 1 + .../IsEqualToIgnoringMillisToIsCloseTo.java | 42 ++++++++++++++ .../resources/META-INF/rewrite/assertj.yml | 1 + ...sEqualToIgnoringMillisToIsCloseToTest.java | 57 +++++++++++++++++++ 4 files changed, 101 insertions(+) create mode 100644 src/main/java/org/openrewrite/java/testing/assertj/IsEqualToIgnoringMillisToIsCloseTo.java create mode 100644 src/test/java/org/openrewrite/java/testing/assertj/IsEqualToIgnoringMillisToIsCloseToTest.java diff --git a/build.gradle.kts b/build.gradle.kts index cff34a87e..a5334ec16 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -42,6 +42,7 @@ dependencies { runtimeOnly("tech.picnic.error-prone-support:error-prone-contrib:latest.release:recipes") compileOnly("org.junit.jupiter:junit-jupiter-engine:latest.release") + compileOnly("org.assertj:assertj-core:3.+") compileOnly("org.projectlombok:lombok:latest.release") annotationProcessor("org.projectlombok:lombok:latest.release") diff --git a/src/main/java/org/openrewrite/java/testing/assertj/IsEqualToIgnoringMillisToIsCloseTo.java b/src/main/java/org/openrewrite/java/testing/assertj/IsEqualToIgnoringMillisToIsCloseTo.java new file mode 100644 index 000000000..23a283649 --- /dev/null +++ b/src/main/java/org/openrewrite/java/testing/assertj/IsEqualToIgnoringMillisToIsCloseTo.java @@ -0,0 +1,42 @@ +/* + * Copyright 2024 the original author or authors. + *

+ * Licensed under the Moderne Source Available License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://docs.moderne.io/licensing/moderne-source-available-license + *

+ * 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.RecipeDescriptor; + +import java.util.Date; + +import static org.assertj.core.api.Assertions.assertThat; + +@RecipeDescriptor( + name = "Replace `AbstractDateAssert#isEqualToIgnoringMillis(java.util.Date)` by `by isCloseTo(Date, long)`", + description = "`isEqualToIgnoringMillis()` is deprecated in favor of `isCloseTo()`." +) +@SuppressWarnings({"java:S1874", "deprecation"}) +public class IsEqualToIgnoringMillisToIsCloseTo { + + @BeforeTemplate + void isEqualToIgnoringMillisBefore(Date date1, Date date2) { + assertThat(date1).isEqualToIgnoringMillis(date2); + } + + @AfterTemplate + void isCloseToAfter(Date date1, Date date2) { + assertThat(date1).isCloseTo(date2, 1000L); + } +} diff --git a/src/main/resources/META-INF/rewrite/assertj.yml b/src/main/resources/META-INF/rewrite/assertj.yml index b571cfe86..de17b201c 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.hamcrest.MigrateHamcrestToAssertJ - org.openrewrite.java.testing.assertj.JUnitToAssertj - org.openrewrite.java.testing.testng.TestNgToAssertj + - org.openrewrite.java.testing.assertj.IsEqualToIgnoringMillisToIsCloseToRecipe - org.openrewrite.java.testing.assertj.StaticImports - org.openrewrite.java.testing.assertj.SimplifyChainedAssertJAssertions - org.openrewrite.java.testing.assertj.SimplifyAssertJAssertions diff --git a/src/test/java/org/openrewrite/java/testing/assertj/IsEqualToIgnoringMillisToIsCloseToTest.java b/src/test/java/org/openrewrite/java/testing/assertj/IsEqualToIgnoringMillisToIsCloseToTest.java new file mode 100644 index 000000000..02c8c2d8f --- /dev/null +++ b/src/test/java/org/openrewrite/java/testing/assertj/IsEqualToIgnoringMillisToIsCloseToTest.java @@ -0,0 +1,57 @@ +/* + * Copyright 2024 the original author or authors. + *

+ * Licensed under the Moderne Source Available License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://docs.moderne.io/licensing/moderne-source-available-license + *

+ * 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.test.RewriteTest; + +import static org.openrewrite.java.Assertions.java; + +class IsEqualToIgnoringMillisToIsCloseToTest implements RewriteTest { + + @Test + @DocumentExample + void replaceDeprecation() { + rewriteRun(spec -> spec.recipe(new IsEqualToIgnoringMillisToIsCloseToRecipe()), + //language=java + java( + """ + import org.assertj.core.api.Assertions; + + import java.util.Date; + + class A { + public void foo(Date date1, Date date2) { + Assertions.assertThat(date1).isEqualToIgnoringMillis(date2); + } + } + """, + """ + import org.assertj.core.api.Assertions; + + import java.util.Date; + + class A { + public void foo(Date date1, Date date2) { + Assertions.assertThat(date1).isCloseTo(date2, 1000L); + } + } + """ + ) + ); + } +}