org.openrewrite.java.testing.assertj.JUnitAssertEqualsToAssertThat Maven / Gradle / Ivy
Show all versions of rewrite-testing-frameworks Show documentation
/*
* Copyright 2024 the original author or authors.
*
* 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
*
* https://docs.moderne.io/licensing/moderne-source-available-license
*
* 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.JavaParser;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.search.UsesMethod;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.TypeUtils;
import java.util.List;
public class JUnitAssertEqualsToAssertThat extends Recipe {
private static final String JUNIT = "org.junit.jupiter.api.Assertions";
private static final String ASSERTJ = "org.assertj.core.api.Assertions";
private static final MethodMatcher ASSERT_EQUALS_MATCHER = new MethodMatcher(JUNIT + " assertEquals(..)", true);
@Override
public String getDisplayName() {
return "JUnit `assertEquals` to AssertJ";
}
@Override
public String getDescription() {
return "Convert JUnit-style `assertEquals()` to AssertJ's `assertThat().isEqualTo()`.";
}
@Override
public TreeVisitor, ExecutionContext> getVisitor() {
return Preconditions.check(new UsesMethod<>(ASSERT_EQUALS_MATCHER), new JavaIsoVisitor() {
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation mi = super.visitMethodInvocation(method, ctx);
if (!ASSERT_EQUALS_MATCHER.matches(mi)) {
return mi;
}
maybeAddImport(ASSERTJ, "assertThat", false);
maybeRemoveImport(JUNIT);
List args = mi.getArguments();
Expression expected = args.get(0);
Expression actual = args.get(1);
if (args.size() == 2) {
return JavaTemplate.builder("assertThat(#{any()}).isEqualTo(#{any()});")
.staticImports(ASSERTJ + ".assertThat")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3.24"))
.build()
.apply(getCursor(), mi.getCoordinates().replace(), actual, expected);
}
if (args.size() == 3 && !isFloatingPointType(args.get(2))) {
Expression message = args.get(2);
return JavaTemplate.builder("assertThat(#{any()}).as(#{any()}).isEqualTo(#{any()});")
.staticImports(ASSERTJ + ".assertThat")
.imports("java.util.function.Supplier")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3.24"))
.build()
.apply(getCursor(), mi.getCoordinates().replace(), actual, message, expected);
}
if (args.size() == 3) {
maybeAddImport(ASSERTJ, "within", false);
return JavaTemplate.builder("assertThat(#{any()}).isCloseTo(#{any()}, within(#{any()}));")
.staticImports(ASSERTJ + ".assertThat", ASSERTJ + ".within")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3.24"))
.build()
.apply(getCursor(), mi.getCoordinates().replace(), actual, expected, args.get(2));
}
maybeAddImport(ASSERTJ, "within", false);
// The assertEquals is using a floating point with a delta argument and a message.
Expression message = args.get(3);
return JavaTemplate.builder("assertThat(#{any()}).as(#{any()}).isCloseTo(#{any()}, within(#{any()}));")
.staticImports(ASSERTJ + ".assertThat", ASSERTJ + ".within")
.imports("java.util.function.Supplier")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3.24"))
.build()
.apply(getCursor(), mi.getCoordinates().replace(), actual, message, expected, args.get(2));
}
private boolean isFloatingPointType(Expression expression) {
JavaType.FullyQualified fullyQualified = TypeUtils.asFullyQualified(expression.getType());
if (fullyQualified != null) {
String typeName = fullyQualified.getFullyQualifiedName();
return "java.lang.Double".equals(typeName) || "java.lang.Float".equals(typeName);
}
JavaType.Primitive parameterType = TypeUtils.asPrimitive(expression.getType());
return parameterType == JavaType.Primitive.Double || parameterType == JavaType.Primitive.Float;
}
});
}
}