Skip to content

Commit caad474

Browse files
authored
Idiomatic AssertJ assertions for empty Strings (#525)
* Idiomatic AssertJ assertions for empty Strings Fixes #513 * Add recipe to AssertJ best practices * Resolve automated findings * Reuse the method matcher for precondition too
1 parent 822149a commit caad474

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.java.testing.assertj;
17+
18+
import org.openrewrite.ExecutionContext;
19+
import org.openrewrite.Preconditions;
20+
import org.openrewrite.Recipe;
21+
import org.openrewrite.TreeVisitor;
22+
import org.openrewrite.java.JavaIsoVisitor;
23+
import org.openrewrite.java.MethodMatcher;
24+
import org.openrewrite.java.search.UsesMethod;
25+
import org.openrewrite.java.tree.J;
26+
import org.openrewrite.java.tree.JavaType;
27+
28+
import java.util.Collections;
29+
30+
public class IsEqualToEmptyString extends Recipe {
31+
32+
private static final MethodMatcher IS_EQUAL_TO = new MethodMatcher("org.assertj.core.api.AbstractStringAssert isEqualTo(java.lang.String)");
33+
34+
@Override
35+
public String getDisplayName() {
36+
return "Convert `assertThat(String).isEqualTo(\"\")` to `isEmpty()`";
37+
}
38+
39+
@Override
40+
public String getDescription() {
41+
return "Adopt idiomatic AssertJ assertion for empty Strings.";
42+
}
43+
44+
@Override
45+
public TreeVisitor<?, ExecutionContext> getVisitor() {
46+
return Preconditions.check(
47+
new UsesMethod<>(IS_EQUAL_TO),
48+
new JavaIsoVisitor<ExecutionContext>() {
49+
@Override
50+
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
51+
J.MethodInvocation mi = super.visitMethodInvocation(method, ctx);
52+
if (IS_EQUAL_TO.matches(mi) && J.Literal.isLiteralValue(mi.getArguments().get(0), "")) {
53+
JavaType.Method isEmptyMethodType = mi.getMethodType().withName("isEmpty");
54+
return mi
55+
.withName(mi.getName().withSimpleName("isEmpty").withType(isEmptyMethodType))
56+
.withMethodType(isEmptyMethodType)
57+
.withArguments(Collections.emptyList());
58+
}
59+
return mi;
60+
}
61+
}
62+
);
63+
}
64+
}

src/main/resources/META-INF/rewrite/assertj.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ recipeList:
2626
- org.openrewrite.java.testing.assertj.JUnitToAssertj
2727
- org.openrewrite.java.testing.assertj.StaticImports
2828
- org.openrewrite.java.testing.assertj.SimplifyChainedAssertJAssertions
29+
- org.openrewrite.java.testing.assertj.IsEqualToEmptyString
2930

3031
---
3132
type: specs.openrewrite.org/v1beta/recipe
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.java.testing.assertj;
17+
18+
import org.junit.jupiter.api.Test;
19+
import org.openrewrite.DocumentExample;
20+
import org.openrewrite.InMemoryExecutionContext;
21+
import org.openrewrite.java.JavaParser;
22+
import org.openrewrite.test.RecipeSpec;
23+
import org.openrewrite.test.RewriteTest;
24+
25+
import static org.openrewrite.java.Assertions.java;
26+
27+
class IsEqualToEmptyStringTest implements RewriteTest {
28+
29+
@Override
30+
public void defaults(RecipeSpec spec) {
31+
spec
32+
.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "assertj-core-3.24"))
33+
.recipe(new IsEqualToEmptyString());
34+
}
35+
36+
@DocumentExample
37+
@Test
38+
void convertsIsEqualToEmptyString() {
39+
rewriteRun(
40+
//language=java
41+
java(
42+
"""
43+
import static org.assertj.core.api.Assertions.assertThat;
44+
class Test {
45+
void test() {
46+
assertThat("test").isEqualTo("");
47+
}
48+
}
49+
""",
50+
"""
51+
import static org.assertj.core.api.Assertions.assertThat;
52+
class Test {
53+
void test() {
54+
assertThat("test").isEmpty();
55+
}
56+
}
57+
"""
58+
)
59+
);
60+
}
61+
}

0 commit comments

Comments
 (0)