Skip to content

Commit

Permalink
Idiomatic AssertJ assertions for empty Strings (#525)
Browse files Browse the repository at this point in the history
* Idiomatic AssertJ assertions for empty Strings

Fixes #513

* Add recipe to AssertJ best practices

* Resolve automated findings

* Reuse the method matcher for precondition too
  • Loading branch information
timtebeek authored Jun 13, 2024
1 parent 822149a commit caad474
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <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.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 {

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()`";
}

@Override
public String getDescription() {
return "Adopt idiomatic AssertJ assertion for empty Strings.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(
new UsesMethod<>(IS_EQUAL_TO),
new JavaIsoVisitor<ExecutionContext>() {
@Override
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
.withName(mi.getName().withSimpleName("isEmpty").withType(isEmptyMethodType))
.withMethodType(isEmptyMethodType)
.withArguments(Collections.emptyList());
}
return mi;
}
}
);
}
}
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.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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <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.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();
}
}
"""
)
);
}
}

0 comments on commit caad474

Please sign in to comment.