From 60cff4b0bfdd8c426be9815d552f938b6644aea9 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Mon, 17 Jun 2024 19:20:19 +0200 Subject: [PATCH] Add Picnic AssertJ rules to AssertJ best practices (#527) * Add Picnic AssertJ rules to AssertJ best practices * Include Picnic's JUnitToAssertJRulesRecipes in migration * Exclude `jakarta.xml.bind-api` from TimeFold * Move the exclude to rewrite-third-party --- build.gradle.kts | 1 + .../testing/assertj/IsEqualToEmptyString.java | 7 +++++++ src/main/resources/META-INF/rewrite/assertj.yml | 17 +++++++++++++++++ ...gTest.java => AssertJBestPracticesTest.java} | 4 ++-- 4 files changed, 27 insertions(+), 2 deletions(-) rename src/test/java/org/openrewrite/java/testing/assertj/{IsEqualToEmptyStringTest.java => AssertJBestPracticesTest.java} (93%) diff --git a/build.gradle.kts b/build.gradle.kts index 4f3662b82..4efd2fa0e 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -34,6 +34,7 @@ dependencies { implementation("org.openrewrite:rewrite-maven") implementation("org.openrewrite.recipe:rewrite-java-dependencies:$rewriteVersion") implementation("org.openrewrite.recipe:rewrite-static-analysis:$rewriteVersion") + implementation("org.openrewrite.recipe:rewrite-third-party:$rewriteVersion") runtimeOnly("org.openrewrite:rewrite-java-17") compileOnly("org.projectlombok:lombok:latest.release") diff --git a/src/main/java/org/openrewrite/java/testing/assertj/IsEqualToEmptyString.java b/src/main/java/org/openrewrite/java/testing/assertj/IsEqualToEmptyString.java index 168c14324..184ef6dee 100644 --- a/src/main/java/org/openrewrite/java/testing/assertj/IsEqualToEmptyString.java +++ b/src/main/java/org/openrewrite/java/testing/assertj/IsEqualToEmptyString.java @@ -27,6 +27,13 @@ import java.util.Collections; +/** + * AssertJ has a more idiomatic way of asserting that a String is empty. + * This recipe will find instances of `assertThat(String).isEqualTo("")` and replace them with `isEmpty()`. + * + * @deprecated Use {@link tech.picnic.errorprone.refasterrules.AssertJStringRulesRecipes.AbstractStringAssertStringIsEmptyRecipe} instead. + */ +@Deprecated public class IsEqualToEmptyString extends Recipe { private static final MethodMatcher IS_EQUAL_TO = new MethodMatcher("org.assertj.core.api.AbstractStringAssert isEqualTo(java.lang.String)"); diff --git a/src/main/resources/META-INF/rewrite/assertj.yml b/src/main/resources/META-INF/rewrite/assertj.yml index 2695a5dd0..f928d283a 100644 --- a/src/main/resources/META-INF/rewrite/assertj.yml +++ b/src/main/resources/META-INF/rewrite/assertj.yml @@ -28,6 +28,22 @@ recipeList: - org.openrewrite.java.testing.assertj.SimplifyChainedAssertJAssertions - org.openrewrite.java.testing.assertj.IsEqualToEmptyString + - tech.picnic.errorprone.refasterrules.AssertJBigDecimalRulesRecipes + - tech.picnic.errorprone.refasterrules.AssertJBigIntegerRulesRecipes + - tech.picnic.errorprone.refasterrules.AssertJBooleanRulesRecipes + - tech.picnic.errorprone.refasterrules.AssertJByteRulesRecipes + - tech.picnic.errorprone.refasterrules.AssertJCharSequenceRulesRecipes + - tech.picnic.errorprone.refasterrules.AssertJDoubleRulesRecipes + - tech.picnic.errorprone.refasterrules.AssertJFloatRulesRecipes + - tech.picnic.errorprone.refasterrules.AssertJIntegerRulesRecipes + - tech.picnic.errorprone.refasterrules.AssertJLongRulesRecipes + - tech.picnic.errorprone.refasterrules.AssertJNumberRulesRecipes + - tech.picnic.errorprone.refasterrules.AssertJPrimitiveRulesRecipes + - tech.picnic.errorprone.refasterrules.AssertJRulesRecipes + - tech.picnic.errorprone.refasterrules.AssertJShortRulesRecipes + - tech.picnic.errorprone.refasterrules.AssertJStringRulesRecipes + - tech.picnic.errorprone.refasterrules.AssertJThrowingCallableRulesRecipes + --- type: specs.openrewrite.org/v1beta/recipe name: org.openrewrite.java.testing.assertj.StaticImports @@ -365,3 +381,4 @@ recipeList: version: 3.x onlyIfUsing: org.assertj.core.api.Assertions acceptTransitive: true + - tech.picnic.errorprone.refasterrules.JUnitToAssertJRulesRecipes diff --git a/src/test/java/org/openrewrite/java/testing/assertj/IsEqualToEmptyStringTest.java b/src/test/java/org/openrewrite/java/testing/assertj/AssertJBestPracticesTest.java similarity index 93% rename from src/test/java/org/openrewrite/java/testing/assertj/IsEqualToEmptyStringTest.java rename to src/test/java/org/openrewrite/java/testing/assertj/AssertJBestPracticesTest.java index 9edf572d6..4252b33ff 100644 --- a/src/test/java/org/openrewrite/java/testing/assertj/IsEqualToEmptyStringTest.java +++ b/src/test/java/org/openrewrite/java/testing/assertj/AssertJBestPracticesTest.java @@ -24,13 +24,13 @@ import static org.openrewrite.java.Assertions.java; -class IsEqualToEmptyStringTest implements RewriteTest { +class AssertJBestPracticesTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { spec .parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "assertj-core-3.24")) - .recipe(new IsEqualToEmptyString()); + .recipeFromResources("org.openrewrite.java.testing.assertj.Assertj"); } @DocumentExample