Skip to content

Commit

Permalink
Add recipe for replacing deprecated method AbstractDateAssert (#655)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
kthoms and timtebeek authored Dec 22, 2024
1 parent 6e9e190 commit fc9d8b4
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* 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
* <p>
* https://docs.moderne.io/licensing/moderne-source-available-license
* <p>
* 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);
}
}
1 change: 1 addition & 0 deletions src/main/resources/META-INF/rewrite/assertj.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* 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
* <p>
* https://docs.moderne.io/licensing/moderne-source-available-license
* <p>
* 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);
}
}
"""
)
);
}
}

0 comments on commit fc9d8b4

Please sign in to comment.