-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]>
- Loading branch information
Showing
4 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/main/java/org/openrewrite/java/testing/assertj/IsEqualToIgnoringMillisToIsCloseTo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...est/java/org/openrewrite/java/testing/assertj/IsEqualToIgnoringMillisToIsCloseToTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
} |