From 432e44981e49e7e0f4f72b7f0de316ea8d7c4c49 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Thu, 13 Jun 2024 22:05:38 +0200 Subject: [PATCH 1/4] Idiomatic AssertJ assertions for empty Strings Fixes #513 --- .../testing/assertj/IsEqualToEmptyString.java | 63 +++++++++++++++++++ .../assertj/IsEqualToEmptyStringTest.java | 61 ++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 src/main/java/org/openrewrite/java/testing/assertj/IsEqualToEmptyString.java create mode 100644 src/test/java/org/openrewrite/java/testing/assertj/IsEqualToEmptyStringTest.java diff --git a/src/main/java/org/openrewrite/java/testing/assertj/IsEqualToEmptyString.java b/src/main/java/org/openrewrite/java/testing/assertj/IsEqualToEmptyString.java new file mode 100644 index 000000000..73c6ef5db --- /dev/null +++ b/src/main/java/org/openrewrite/java/testing/assertj/IsEqualToEmptyString.java @@ -0,0 +1,63 @@ +/* + * 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.openrewrite.ExecutionContext; +import org.openrewrite.Preconditions; +import org.openrewrite.Recipe; +import org.openrewrite.TreeVisitor; +import org.openrewrite.java.JavaIsoVisitor; +import org.openrewrite.java.MethodMatcher; +import org.openrewrite.java.search.UsesMethod; +import org.openrewrite.java.tree.J; +import org.openrewrite.java.tree.JavaType; + +import java.util.Collections; + +public class IsEqualToEmptyString extends Recipe { + @Override + public String getDisplayName() { + return "Convert `assertThat(String).isEqualTo(\"\")` to `isEmpty()`"; + } + + @Override + public String getDescription() { + return "Adopt idiomatic AssertJ assertion for empty Strings."; + } + + @Override + public TreeVisitor getVisitor() { + return Preconditions.check( + new UsesMethod<>("org.assertj.core.api.Assertions assertThat(java.lang.String)"), + new JavaIsoVisitor() { + private final MethodMatcher IS_EQUAL_TO = new MethodMatcher("org.assertj.core.api.AbstractStringAssert isEqualTo(java.lang.String)"); + + @Override + public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext executionContext) { + J.MethodInvocation mi = super.visitMethodInvocation(method, executionContext); + if (IS_EQUAL_TO.matches(mi) && J.Literal.isLiteralValue(mi.getArguments().get(0), "")) { + JavaType.Method isEmptyMethodType = mi.getMethodType().withName("isEmpty"); + return mi + .withName(mi.getName().withSimpleName("isEmpty").withType(isEmptyMethodType)) + .withMethodType(isEmptyMethodType) + .withArguments(Collections.emptyList()); + } + return mi; + } + } + ); + } +} diff --git a/src/test/java/org/openrewrite/java/testing/assertj/IsEqualToEmptyStringTest.java b/src/test/java/org/openrewrite/java/testing/assertj/IsEqualToEmptyStringTest.java new file mode 100644 index 000000000..90ed9d4f6 --- /dev/null +++ b/src/test/java/org/openrewrite/java/testing/assertj/IsEqualToEmptyStringTest.java @@ -0,0 +1,61 @@ +/* + * 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.InMemoryExecutionContext; +import org.openrewrite.java.JavaParser; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.java.Assertions.java; + +class IsEqualToEmptyStringTest implements RewriteTest { + + @Override + public void defaults(RecipeSpec spec) { + spec + .parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "assertj-core-3.24")) + .recipe(new IsEqualToEmptyString()); + } + + @DocumentExample + @Test + void convertsIsEqualToEmptyString() { + rewriteRun( + //language=java + java( + """ + import static org.assertj.core.api.Assertions.assertThat; + class Test { + void test() { + assertThat("test").isEqualTo(""); + } + } + """, + """ + import static org.assertj.core.api.Assertions.assertThat; + class Test { + void test() { + assertThat("test").isEmpty(); + } + } + """ + ) + ); + } +} \ No newline at end of file From a789d5c874c35808156ac28c55a127d470a2a7f7 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Thu, 13 Jun 2024 22:06:50 +0200 Subject: [PATCH 2/4] Add recipe to AssertJ best practices --- src/main/resources/META-INF/rewrite/assertj.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/resources/META-INF/rewrite/assertj.yml b/src/main/resources/META-INF/rewrite/assertj.yml index 18cac7ae6..2695a5dd0 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.IsEqualToEmptyString --- type: specs.openrewrite.org/v1beta/recipe From 8b398876a1955518a749b662936d31158b79de47 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Thu, 13 Jun 2024 22:13:25 +0200 Subject: [PATCH 3/4] Resolve automated findings --- .../java/testing/assertj/IsEqualToEmptyString.java | 4 ++-- .../java/testing/assertj/IsEqualToEmptyStringTest.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) 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 73c6ef5db..e1064fc9d 100644 --- a/src/main/java/org/openrewrite/java/testing/assertj/IsEqualToEmptyString.java +++ b/src/main/java/org/openrewrite/java/testing/assertj/IsEqualToEmptyString.java @@ -46,8 +46,8 @@ public TreeVisitor getVisitor() { private final MethodMatcher IS_EQUAL_TO = new MethodMatcher("org.assertj.core.api.AbstractStringAssert isEqualTo(java.lang.String)"); @Override - public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext executionContext) { - J.MethodInvocation mi = super.visitMethodInvocation(method, executionContext); + public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { + J.MethodInvocation mi = super.visitMethodInvocation(method, ctx); if (IS_EQUAL_TO.matches(mi) && J.Literal.isLiteralValue(mi.getArguments().get(0), "")) { JavaType.Method isEmptyMethodType = mi.getMethodType().withName("isEmpty"); return mi diff --git a/src/test/java/org/openrewrite/java/testing/assertj/IsEqualToEmptyStringTest.java b/src/test/java/org/openrewrite/java/testing/assertj/IsEqualToEmptyStringTest.java index 90ed9d4f6..9edf572d6 100644 --- a/src/test/java/org/openrewrite/java/testing/assertj/IsEqualToEmptyStringTest.java +++ b/src/test/java/org/openrewrite/java/testing/assertj/IsEqualToEmptyStringTest.java @@ -58,4 +58,4 @@ void test() { ) ); } -} \ No newline at end of file +} From 7c362176912ae3b97f26bb62f7ccba26a9ce8027 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Thu, 13 Jun 2024 22:46:21 +0200 Subject: [PATCH 4/4] Reuse the method matcher for precondition too --- .../java/testing/assertj/IsEqualToEmptyString.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) 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 e1064fc9d..168c14324 100644 --- a/src/main/java/org/openrewrite/java/testing/assertj/IsEqualToEmptyString.java +++ b/src/main/java/org/openrewrite/java/testing/assertj/IsEqualToEmptyString.java @@ -28,6 +28,9 @@ import java.util.Collections; public class IsEqualToEmptyString extends Recipe { + + private static final MethodMatcher IS_EQUAL_TO = new MethodMatcher("org.assertj.core.api.AbstractStringAssert isEqualTo(java.lang.String)"); + @Override public String getDisplayName() { return "Convert `assertThat(String).isEqualTo(\"\")` to `isEmpty()`"; @@ -41,10 +44,8 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { return Preconditions.check( - new UsesMethod<>("org.assertj.core.api.Assertions assertThat(java.lang.String)"), + new UsesMethod<>(IS_EQUAL_TO), new JavaIsoVisitor() { - private final MethodMatcher IS_EQUAL_TO = new MethodMatcher("org.assertj.core.api.AbstractStringAssert isEqualTo(java.lang.String)"); - @Override public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { J.MethodInvocation mi = super.visitMethodInvocation(method, ctx);