Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
jevanlingen committed Nov 5, 2024
1 parent 44b855f commit 5f36707
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class JUnitAssertArrayEqualsToAssertThat extends Recipe {

@Override
public String getDisplayName() {
return "JUnit `assertArrayEquals` To AssertJ";
return "JUnit `assertArrayEquals` to assertJ";
}

@Override
Expand Down Expand Up @@ -69,17 +69,8 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3.24"))
.build()
.apply(getCursor(), md.getCoordinates().replace(), actual, expected);
} else if (args.size() == 3 && !isFloatingPointType(args.get(2))) {
Expression message = args.get(2);
JavaTemplate.Builder template = TypeUtils.isString(message.getType()) ?
JavaTemplate.builder("assertThat(#{anyArray()}).as(#{any(String)}).containsExactly(#{anyArray()});") :
JavaTemplate.builder("assertThat(#{anyArray()}).as(#{any(java.util.function.Supplier)}).containsExactly(#{anyArray()});");
return template
.staticImports(ASSERTJ + ".assertThat")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3.24"))
.build()
.apply(getCursor(), md.getCoordinates().replace(), actual, message, expected);
} else if (args.size() == 3) {
}
if (args.size() == 3 && isFloatingPointType(args.get(2))) {
maybeAddImport(ASSERTJ, "within", false);
// assert is using floating points with a delta and no message.
return JavaTemplate.builder("assertThat(#{anyArray()}).containsExactly(#{anyArray()}, within(#{any()}));")
Expand All @@ -88,16 +79,20 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
.build()
.apply(getCursor(), md.getCoordinates().replace(), actual, expected, args.get(2));
}

if (args.size() == 3) {
Expression message = args.get(2);
return JavaTemplate.builder("assertThat(#{anyArray()}).as(#{any()}).containsExactly(#{anyArray()});")
.staticImports(ASSERTJ + ".assertThat")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3.24"))
.build()
.apply(getCursor(), md.getCoordinates().replace(), actual, message, expected);
}

maybeAddImport(ASSERTJ, "within", false);

// The assertEquals is using a floating point with a delta argument and a message.
Expression message = args.get(3);
JavaTemplate.Builder template = TypeUtils.isString(message.getType()) ?
JavaTemplate.builder("assertThat(#{anyArray()}).as(#{any(String)}).containsExactly(#{anyArray()}, within(#{any()}));") :
JavaTemplate.builder("assertThat(#{anyArray()}).as(#{any(java.util.function.Supplier)}).containsExactly(#{anyArray()}, within(#{}));");
return template
return JavaTemplate.builder("assertThat(#{anyArray()}).as(#{any()}).containsExactly(#{anyArray()}, within(#{}));")
.staticImports(ASSERTJ + ".assertThat", ASSERTJ + ".within")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3.24"))
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,17 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3.24"))
.build()
.apply(getCursor(), mi.getCoordinates().replace(), actual, expected);
} else if (args.size() == 3 && !isFloatingPointType(args.get(2))) {
}
if (args.size() == 3 && !isFloatingPointType(args.get(2))) {
Expression message = args.get(2);
JavaTemplate.Builder template = TypeUtils.isString(message.getType()) ?
JavaTemplate.builder("assertThat(#{any()}).as(#{any(String)}).isEqualTo(#{any()});") :
JavaTemplate.builder("assertThat(#{any()}).as(#{any(java.util.function.Supplier)}).isEqualTo(#{any()});");
return template
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);
} else if (args.size() == 3) {
}
if (args.size() == 3) {
maybeAddImport(ASSERTJ, "within", false);
return JavaTemplate.builder("assertThat(#{any()}).isCloseTo(#{any()}, within(#{any()}));")
.staticImports(ASSERTJ + ".assertThat", ASSERTJ + ".within")
Expand All @@ -93,10 +92,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu

// The assertEquals is using a floating point with a delta argument and a message.
Expression message = args.get(3);
JavaTemplate.Builder template = TypeUtils.isString(message.getType()) ?
JavaTemplate.builder("assertThat(#{any()}).as(#{any(String)}).isCloseTo(#{any()}, within(#{any()}));") :
JavaTemplate.builder("assertThat(#{any()}).as(#{any(java.util.function.Supplier)}).isCloseTo(#{any()}, within(#{any()}));");
return template
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"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.openrewrite.java.search.UsesMethod;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.TypeUtils;

import java.util.List;

Expand Down Expand Up @@ -54,7 +53,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
return mi;
}

maybeAddImport("org.assertj.core.api.Assertions", "assertThat");
maybeAddImport("org.assertj.core.api.Assertions", "assertThat", false);
maybeRemoveImport("org.junit.jupiter.api.Assertions");

List<Expression> args = mi.getArguments();
Expand All @@ -68,10 +67,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
}

Expression message = args.get(1);
JavaTemplate.Builder template = TypeUtils.isString(message.getType()) ?
JavaTemplate.builder("assertThat(#{any(boolean)}).as(#{any(String)}).isFalse();") :
JavaTemplate.builder("assertThat(#{any(boolean)}).as(#{any(java.util.function.Supplier)}).isFalse();");
return template
return JavaTemplate.builder("assertThat(#{any(boolean)}).as(#{any()}).isFalse();")
.staticImports("org.assertj.core.api.Assertions.assertThat")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3.24"))
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,25 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
return mi;
}

maybeAddImport("org.assertj.core.api.Assertions", "assertThat");
maybeAddImport("org.assertj.core.api.Assertions", "assertThat", false);
maybeRemoveImport("org.junit.jupiter.api.Assertions");

Expression expectedType = mi.getArguments().get(0);
Expression actualValue = mi.getArguments().get(1);
Expression expected = mi.getArguments().get(0);
Expression actual = mi.getArguments().get(1);
if (mi.getArguments().size() == 2) {
return JavaTemplate.builder("assertThat(#{any()}).isInstanceOf(#{any()});")
.staticImports("org.assertj.core.api.Assertions.assertThat")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3.24"))
.build()
.apply(getCursor(), method.getCoordinates().replace(), actualValue, expectedType);
.apply(getCursor(), method.getCoordinates().replace(), actual, expected);
}

Expression messageOrSupplier = mi.getArguments().get(2);
return JavaTemplate.builder("assertThat(#{any()}).as(#{any()}).isInstanceOf(#{any()});")
.staticImports("org.assertj.core.api.Assertions.assertThat")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3.24"))
.build()
.apply(getCursor(), method.getCoordinates().replace(), actualValue, messageOrSupplier, expectedType);
.apply(getCursor(), method.getCoordinates().replace(), actual, messageOrSupplier, expected);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,32 +69,28 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3.24"))
.build()
.apply(getCursor(), mi.getCoordinates().replace(), actual, expected);
} else if (args.size() == 3 && !isFloatingPointType(args.get(2))) {
Expression message = args.get(2);
JavaTemplate.Builder template = TypeUtils.isString(message.getType()) ?
JavaTemplate.builder("assertThat(#{any()}).as(#{any(String)}).isNotEqualTo(#{any()});") :
JavaTemplate.builder("assertThat(#{any()}).as(#{any(java.util.function.Supplier)}).isNotEqualTo(#{any()});");
return template
.staticImports(ASSERTJ + ".assertThat")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3.24"))
.build()
.apply(getCursor(), mi.getCoordinates().replace(), actual, message, expected);
} else if (args.size() == 3) {
}
if (args.size() == 3 && isFloatingPointType(args.get(2))) {
maybeAddImport(ASSERTJ, "within", false);
return JavaTemplate.builder("assertThat(#{any()}).isNotCloseTo(#{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));
}
if (args.size() == 3) {
Expression message = args.get(2);
return JavaTemplate.builder("assertThat(#{any()}).as(#{any()}).isNotEqualTo(#{any()});")
.staticImports(ASSERTJ + ".assertThat")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3.24"))
.build()
.apply(getCursor(), mi.getCoordinates().replace(), actual, message, expected);
}

maybeAddImport(ASSERTJ, "within");
maybeAddImport(ASSERTJ, "within", false);

Expression message = args.get(3);
JavaTemplate.Builder template = TypeUtils.isString(message.getType()) ?
JavaTemplate.builder("assertThat(#{any()}).as(#{any(String)}).isNotCloseTo(#{any()}, within(#{any()}));") :
JavaTemplate.builder("assertThat(#{any()}).as(#{any(java.util.function.Supplier)}).isNotCloseTo(#{any()}, within(#{any()}));");
return template
return JavaTemplate.builder("assertThat(#{any()}).as(#{any()}).isNotCloseTo(#{any()}, within(#{any()}));")
.staticImports(ASSERTJ + ".assertThat", ASSERTJ + ".within")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3.24"))
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.openrewrite.java.search.UsesMethod;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.TypeUtils;

import java.util.List;

Expand Down Expand Up @@ -69,10 +68,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
}

Expression message = args.get(1);
JavaTemplate.Builder template = TypeUtils.isString(message.getType()) ?
JavaTemplate.builder("assertThat(#{any()}).as(#{any(String)}).isNotNull();") :
JavaTemplate.builder("assertThat(#{any()}).as(#{any(java.util.function.Supplier)}).isNotNull();");
return template
return JavaTemplate.builder("assertThat(#{any()}).as(#{any()}).isNotNull();")
.staticImports("org.assertj.core.api.Assertions.assertThat")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3.24"))
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.openrewrite.java.search.UsesMethod;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.TypeUtils;

import java.util.List;

Expand Down Expand Up @@ -65,14 +64,10 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3.24"))
.build()
.apply(getCursor(), mi.getCoordinates().replace(), actual);

}

Expression message = args.get(1);
JavaTemplate.Builder template = TypeUtils.isString(message.getType()) ?
JavaTemplate.builder("assertThat(#{any()}).as(#{any(String)}).isNull();") :
JavaTemplate.builder("assertThat(#{any()}).as(#{any(java.util.function.Supplier)}).isNull();");
return template
return JavaTemplate.builder("assertThat(#{any()}).as(#{any()}).isNull();")
.staticImports("org.assertj.core.api.Assertions.assertThat")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3.24"))
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.openrewrite.java.search.UsesMethod;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.TypeUtils;

import java.util.List;

Expand Down Expand Up @@ -69,10 +68,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
}

Expression message = args.get(2);
JavaTemplate.Builder template = TypeUtils.isString(message.getType()) ?
JavaTemplate.builder("assertThat(#{any()}).as(#{any(String)}).isSameAs(#{any()});") :
JavaTemplate.builder("assertThat(#{any()}).as(#{any(java.util.function.Supplier)}).isSameAs(#{any()});");
return template
return JavaTemplate.builder("assertThat(#{any()}).as(#{any()}).isSameAs(#{any()});")
.staticImports("org.assertj.core.api.Assertions.assertThat")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3.24"))
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.openrewrite.java.search.UsesMethod;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.TypeUtils;

import java.util.List;

Expand Down Expand Up @@ -68,10 +67,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
}

Expression message = args.get(1);
JavaTemplate.Builder template = TypeUtils.isString(message.getType()) ?
JavaTemplate.builder("assertThat(#{any(boolean)}).as(#{any(String)}).isTrue();") :
JavaTemplate.builder("assertThat(#{any(boolean)}).as(#{any(java.util.function.Supplier)}).isTrue();");
return template
return JavaTemplate.builder("assertThat(#{any()}).as(#{any(String)}).isTrue();")
.staticImports("org.assertj.core.api.Assertions.assertThat")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3.24"))
.build()
Expand Down
Loading

0 comments on commit 5f36707

Please sign in to comment.