From 9d67df6aae90a9ecb594b2b28644b6cd34d1b8ab Mon Sep 17 00:00:00 2001 From: Sasha Sheikin Date: Tue, 4 Jun 2024 20:50:29 +0200 Subject: [PATCH 01/12] Copy JUnit classes to TestNG --- .../TestNgAssertEqualsToAssertThat.java | 150 ++++++++++++++++ .../testng/TestNgAssertFalseToAssertThat.java | 110 ++++++++++++ .../TestNgAssertNotEqualsToAssertThat.java | 162 ++++++++++++++++++ .../TestNgAssertNotNullToAssertThat.java | 112 ++++++++++++ .../testng/TestNgAssertNullToAssertThat.java | 111 ++++++++++++ .../testng/TestNgAssertTrueToAssertThat.java | 110 ++++++++++++ .../testng/TestNgFailToAssertJFail.java | 162 ++++++++++++++++++ 7 files changed, 917 insertions(+) create mode 100644 src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertEqualsToAssertThat.java create mode 100644 src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertFalseToAssertThat.java create mode 100644 src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNotEqualsToAssertThat.java create mode 100644 src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNotNullToAssertThat.java create mode 100644 src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNullToAssertThat.java create mode 100644 src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertTrueToAssertThat.java create mode 100644 src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgFailToAssertJFail.java diff --git a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertEqualsToAssertThat.java b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertEqualsToAssertThat.java new file mode 100644 index 000000000..c596cca59 --- /dev/null +++ b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertEqualsToAssertThat.java @@ -0,0 +1,150 @@ +/* + * Copyright 2021 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.testng; + +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.UsesType; +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 TestNgAssertEqualsToAssertThat + extends Recipe { + + @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 getVisitor() { + return Preconditions.check(new UsesType<>("org.junit.jupiter.api.Assertions", false), new AssertEqualsToAssertThatVisitor()); + } + + public static class AssertEqualsToAssertThatVisitor extends JavaIsoVisitor { + private JavaParser.Builder assertionsParser; + + private JavaParser.Builder assertionsParser(ExecutionContext ctx) { + if (assertionsParser == null) { + assertionsParser = JavaParser.fromJavaVersion() + .classpathFromResources(ctx, "assertj-core-3.24"); + } + return assertionsParser; + } + + private static final MethodMatcher JUNIT_ASSERT_EQUALS = new MethodMatcher("org.junit.jupiter.api.Assertions" + " assertEquals(..)"); + + @Override + public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { + if (!JUNIT_ASSERT_EQUALS.matches(method)) { + return method; + } + + List args = method.getArguments(); + Expression expected = args.get(0); + Expression actual = args.get(1); + + //always add the import (even if not referenced) + maybeAddImport("org.assertj.core.api.Assertions", "assertThat", false); + + // Remove import for "org.junit.jupiter.api.Assertions" if no longer used. + maybeRemoveImport("org.junit.jupiter.api.Assertions"); + + if (args.size() == 2) { + return JavaTemplate.builder("assertThat(#{any()}).isEqualTo(#{any()});") + .staticImports("org.assertj.core.api.Assertions.assertThat") + .javaParser(assertionsParser(ctx)) + .build() + .apply(getCursor(), method.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)}).isEqualTo(#{any()});") : + JavaTemplate.builder("assertThat(#{any()}).as(#{any(java.util.function.Supplier)}).isEqualTo(#{any()});"); + return template + .staticImports("org.assertj.core.api.Assertions.assertThat") + .imports("java.util.function.Supplier") + .javaParser(assertionsParser(ctx)) + .build() + .apply( + getCursor(), + method.getCoordinates().replace(), + actual, + message, + expected + ); + } else if (args.size() == 3) { + //always add the import (even if not referenced) + maybeAddImport("org.assertj.core.api.Assertions", "within", false); + return JavaTemplate.builder("assertThat(#{any()}).isCloseTo(#{any()}, within(#{any()}));") + .staticImports("org.assertj.core.api.Assertions.assertThat", "org.assertj.core.api.Assertions.within") + .javaParser(assertionsParser(ctx)) + .build() + .apply(getCursor(), method.getCoordinates().replace(), actual, expected, args.get(2)); + + } + + // The assertEquals is using a floating point with a delta argument and a message. + Expression message = args.get(3); + + //always add the import (even if not referenced) + maybeAddImport("org.assertj.core.api.Assertions", "within", false); + 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 + .staticImports("org.assertj.core.api.Assertions.assertThat", "org.assertj.core.api.Assertions.within") + .imports("java.util.function.Supplier") + .javaParser(assertionsParser(ctx)) + .build() + .apply( + getCursor(), + method.getCoordinates().replace(), + actual, + message, + expected, + args.get(2) + ); + } + + private static 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; + } + } +} diff --git a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertFalseToAssertThat.java b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertFalseToAssertThat.java new file mode 100644 index 000000000..4fa3709b8 --- /dev/null +++ b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertFalseToAssertThat.java @@ -0,0 +1,110 @@ +/* + * Copyright 2021 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.testng; + +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.UsesType; +import org.openrewrite.java.tree.Expression; +import org.openrewrite.java.tree.J; +import org.openrewrite.java.tree.TypeUtils; + +import java.util.List; + +public class TestNgAssertFalseToAssertThat + extends Recipe { + + @Override + public String getDisplayName() { + return "JUnit `assertFalse` to AssertJ"; + } + + @Override + public String getDescription() { + return "Convert JUnit-style `assertFalse()` to AssertJ's `assertThat().isFalse()`."; + } + + @Override + public TreeVisitor getVisitor() { + return Preconditions.check(new UsesType<>("org.junit.jupiter.api.Assertions", false), new AssertFalseToAssertThatVisitor()); + } + + public static class AssertFalseToAssertThatVisitor extends JavaIsoVisitor { + private JavaParser.Builder assertionsParser; + + private JavaParser.Builder assertionsParser(ExecutionContext ctx) { + if (assertionsParser == null) { + assertionsParser = JavaParser.fromJavaVersion() + .classpathFromResources(ctx, "assertj-core-3.24"); + } + return assertionsParser; + } + + private static final MethodMatcher JUNIT_ASSERT_FALSE = new MethodMatcher("org.junit.jupiter.api.Assertions" + " assertFalse(boolean, ..)"); + + @Override + public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { + if (!JUNIT_ASSERT_FALSE.matches(method)) { + return method; + } + + List args = method.getArguments(); + Expression actual = args.get(0); + + if (args.size() == 1) { + method = JavaTemplate.builder("assertThat(#{any(boolean)}).isFalse();") + .staticImports("org.assertj.core.api.Assertions.assertThat") + .javaParser(assertionsParser(ctx)) + .build() + .apply( + getCursor(), + method.getCoordinates().replace(), + actual + ); + } else { + 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();"); + + method = template + .staticImports("org.assertj.core.api.Assertions.assertThat") + .javaParser(assertionsParser(ctx)) + .build() + .apply( + getCursor(), + method.getCoordinates().replace(), + actual, + message + ); + } + + //Make sure there is a static import for "org.assertj.core.api.Assertions.assertThat" (even if not referenced) + maybeAddImport("org.assertj.core.api.Assertions", "assertThat", false); + + // Remove import for "org.junit.jupiter.api.Assertions" if no longer used. + maybeRemoveImport("org.junit.jupiter.api.Assertions"); + + return method; + } + } +} diff --git a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNotEqualsToAssertThat.java b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNotEqualsToAssertThat.java new file mode 100644 index 000000000..5a0537246 --- /dev/null +++ b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNotEqualsToAssertThat.java @@ -0,0 +1,162 @@ +/* + * Copyright 2021 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.testng; + +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.UsesType; +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 TestNgAssertNotEqualsToAssertThat + extends Recipe { + + @Override + public String getDisplayName() { + return "JUnit `assertNotEquals` to AssertJ"; + } + + @Override + public String getDescription() { + return "Convert JUnit-style `assertNotEquals()` to AssertJ's `assertThat().isNotEqualTo()`."; + } + + @Override + public TreeVisitor getVisitor() { + return Preconditions.check(new UsesType<>("org.junit.jupiter.api.Assertions", false), new AssertNotEqualsToAssertThatVisitor()); + } + + public static class AssertNotEqualsToAssertThatVisitor extends JavaIsoVisitor { + private JavaParser.Builder assertionsParser; + + private JavaParser.Builder assertionsParser(ExecutionContext ctx) { + if (assertionsParser == null) { + assertionsParser = JavaParser.fromJavaVersion() + .classpathFromResources(ctx, "assertj-core-3.24"); + } + return assertionsParser; + } + + private static final MethodMatcher JUNIT_ASSERT_EQUALS = new MethodMatcher("org.junit.jupiter.api.Assertions" + " assertNotEquals(..)"); + + @Override + public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { + if (!JUNIT_ASSERT_EQUALS.matches(method)) { + return method; + } + + List args = method.getArguments(); + + Expression expected = args.get(0); + Expression actual = args.get(1); + + if (args.size() == 2) { + method = JavaTemplate.builder("assertThat(#{any()}).isNotEqualTo(#{any()});") + .staticImports("org.assertj.core.api.Assertions.assertThat") + .javaParser(assertionsParser(ctx)) + .build() + .apply( + getCursor(), + method.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()});"); + + + method = template + .staticImports("org.assertj.core.api.Assertions.assertThat") + .javaParser(assertionsParser(ctx)) + .build() + .apply( + getCursor(), + method.getCoordinates().replace(), + actual, + message, + expected + ); + } else if (args.size() == 3) { + method = JavaTemplate.builder("assertThat(#{any()}).isNotCloseTo(#{any()}, within(#{any()}));") + .staticImports("org.assertj.core.api.Assertions.assertThat", "org.assertj.core.api.Assertions.within") + .javaParser(assertionsParser(ctx)) + .build() + .apply( + getCursor(), + method.getCoordinates().replace(), + actual, + expected, + args.get(2) + ); + maybeAddImport("org.assertj.core.api.Assertions", "within", false); + } else { + 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()}));"); + + method = template + .staticImports("org.assertj.core.api.Assertions.assertThat", "org.assertj.core.api.Assertions.within") + .javaParser(assertionsParser(ctx)) + .build() + .apply( + getCursor(), + method.getCoordinates().replace(), + actual, + message, + expected, + args.get(2) + ); + + maybeAddImport("org.assertj.core.api.Assertions", "within", false); + } + + //Make sure there is a static import for "org.assertj.core.api.Assertions.assertThat" (even if not referenced) + maybeAddImport("org.assertj.core.api.Assertions", "assertThat", false); + + // Remove import for "org.junit.jupiter.api.Assertions" if no longer used. + maybeRemoveImport("org.junit.jupiter.api.Assertions"); + + return method; + } + + private static 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; + } + } +} diff --git a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNotNullToAssertThat.java b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNotNullToAssertThat.java new file mode 100644 index 000000000..1e57affff --- /dev/null +++ b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNotNullToAssertThat.java @@ -0,0 +1,112 @@ +/* + * Copyright 2021 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.testng; + +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.UsesType; +import org.openrewrite.java.tree.Expression; +import org.openrewrite.java.tree.J; +import org.openrewrite.java.tree.TypeUtils; + +import java.util.List; + +public class TestNgAssertNotNullToAssertThat + extends Recipe { + + @Override + public String getDisplayName() { + return "JUnit `assertNotNull` to AssertJ"; + } + + @Override + public String getDescription() { + return "Convert JUnit-style `assertNotNull()` to AssertJ's `assertThat().isNotNull()`."; + } + + @Override + public TreeVisitor getVisitor() { + return Preconditions.check(new UsesType<>("org.junit.jupiter.api.Assertions", false), new AssertNotNullToAssertThatVisitor()); + } + + public static class AssertNotNullToAssertThatVisitor extends JavaIsoVisitor { + private JavaParser.Builder assertionsParser; + + private JavaParser.Builder assertionsParser(ExecutionContext ctx) { + if (assertionsParser == null) { + assertionsParser = JavaParser.fromJavaVersion() + .classpathFromResources(ctx, "assertj-core-3.24"); + } + return assertionsParser; + } + + private static final MethodMatcher JUNIT_ASSERT_NOT_NULL_MATCHER = new MethodMatcher("org.junit.jupiter.api.Assertions" + " assertNotNull(..)"); + + @Override + public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { + if (!JUNIT_ASSERT_NOT_NULL_MATCHER.matches(method)) { + return method; + } + + List args = method.getArguments(); + Expression actual = args.get(0); + + if (args.size() == 1) { + method = JavaTemplate.builder("assertThat(#{any()}).isNotNull();") + .staticImports("org.assertj.core.api.Assertions.assertThat") + .javaParser(assertionsParser(ctx)) + .build() + .apply( + getCursor(), + method.getCoordinates().replace(), + actual + ); + + } else { + 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();"); + + method = template + .staticImports("org.assertj.core.api.Assertions.assertThat") + .javaParser(assertionsParser(ctx)) + .build() + .apply( + getCursor(), + method.getCoordinates().replace(), + actual, + message + ); + } + + //Make sure there is a static import for "org.assertj.core.api.Assertions.assertThat" (even if not referenced) + maybeAddImport("org.assertj.core.api.Assertions", "assertThat", false); + + //And if there are no longer references to the JUnit assertions class, we can remove the import. + maybeRemoveImport("org.junit.jupiter.api.Assertions"); + + return method; + } + } +} diff --git a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNullToAssertThat.java b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNullToAssertThat.java new file mode 100644 index 000000000..8b6264a8b --- /dev/null +++ b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNullToAssertThat.java @@ -0,0 +1,111 @@ +/* + * Copyright 2021 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.testng; + +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.UsesType; +import org.openrewrite.java.tree.Expression; +import org.openrewrite.java.tree.J; +import org.openrewrite.java.tree.TypeUtils; + +import java.util.List; + +public class TestNgAssertNullToAssertThat + extends Recipe { + + @Override + public String getDisplayName() { + return "JUnit `assertNull` to AssertJ"; + } + + @Override + public String getDescription() { + return "Convert JUnit-style `assertNull()` to AssertJ's `assertThat().isNull()`."; + } + + @Override + public TreeVisitor getVisitor() { + return Preconditions.check(new UsesType<>("org.junit.jupiter.api.Assertions", false), new AssertNullToAssertThatVisitor()); + } + + public static class AssertNullToAssertThatVisitor extends JavaIsoVisitor { + private JavaParser.Builder assertionsParser; + + private JavaParser.Builder assertionsParser(ExecutionContext ctx) { + if (assertionsParser == null) { + assertionsParser = JavaParser.fromJavaVersion() + .classpathFromResources(ctx, "assertj-core-3.24"); + } + return assertionsParser; + } + + private static final MethodMatcher JUNIT_ASSERT_NULL_MATCHER = new MethodMatcher("org.junit.jupiter.api.Assertions" + " assertNull(..)"); + + @Override + public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { + if (!JUNIT_ASSERT_NULL_MATCHER.matches(method)) { + return method; + } + + List args = method.getArguments(); + Expression actual = args.get(0); + + if (args.size() == 1) { + method = JavaTemplate.builder("assertThat(#{any()}).isNull();") + .staticImports("org.assertj.core.api.Assertions.assertThat") + .javaParser(assertionsParser(ctx)) + .build() + .apply( + getCursor(), + method.getCoordinates().replace(), + actual + ); + } else { + 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();"); + + method = template + .staticImports("org.assertj.core.api.Assertions.assertThat") + .javaParser(assertionsParser(ctx)) + .build() + .apply( + getCursor(), + method.getCoordinates().replace(), + actual, + message + ); + } + + // Make sure there is a static import for "org.assertj.core.api.Assertions.assertThat" (even if not referenced) + maybeAddImport("org.assertj.core.api.Assertions", "assertThat", false); + + // Remove import for "org.junit.jupiter.api.Assertions" if no longer used. + maybeRemoveImport("org.junit.jupiter.api.Assertions"); + + return method; + } + } +} diff --git a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertTrueToAssertThat.java b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertTrueToAssertThat.java new file mode 100644 index 000000000..58b8544be --- /dev/null +++ b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertTrueToAssertThat.java @@ -0,0 +1,110 @@ +/* + * Copyright 2021 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.testng; + +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.UsesType; +import org.openrewrite.java.tree.Expression; +import org.openrewrite.java.tree.J; +import org.openrewrite.java.tree.TypeUtils; + +import java.util.List; + +public class TestNgAssertTrueToAssertThat + extends Recipe { + @Override + public String getDisplayName() { + return "JUnit `assertTrue` to AssertJ"; + } + + @Override + public String getDescription() { + return "Convert JUnit-style `assertTrue()` to AssertJ's `assertThat().isTrue()`."; + } + + @Override + public TreeVisitor getVisitor() { + return Preconditions.check(new UsesType<>("org.junit.jupiter.api.Assertions", false), new AssertTrueToAssertThatVisitor()); + } + + public static class AssertTrueToAssertThatVisitor extends JavaIsoVisitor { + private JavaParser.Builder assertionsParser; + + private JavaParser.Builder assertionsParser(ExecutionContext ctx) { + if (assertionsParser == null) { + assertionsParser = JavaParser.fromJavaVersion() + .classpathFromResources(ctx, "assertj-core-3.24"); + } + return assertionsParser; + } + + private static final MethodMatcher JUNIT_ASSERT_TRUE = new MethodMatcher("org.junit.jupiter.api.Assertions" + " assertTrue(boolean, ..)"); + + @Override + public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { + if (!JUNIT_ASSERT_TRUE.matches(method)) { + return method; + } + + List args = method.getArguments(); + Expression actual = args.get(0); + + if (args.size() == 1) { + method = JavaTemplate.builder("assertThat(#{any(boolean)}).isTrue();") + .staticImports("org.assertj.core.api.Assertions.assertThat") + .javaParser(assertionsParser(ctx)) + .build() + .apply( + getCursor(), + method.getCoordinates().replace(), + actual + ); + } else { + 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();"); + + method = template + .staticImports("org.assertj.core.api.Assertions.assertThat") + .javaParser(assertionsParser(ctx)) + .build() + .apply( + getCursor(), + method.getCoordinates().replace(), + actual, + message + ); + } + + //Make sure there is a static import for "org.assertj.core.api.Assertions.assertThat" (even if not referenced) + maybeAddImport("org.assertj.core.api.Assertions", "assertThat", false); + + // Remove import for "org.junit.jupiter.api.Assertions" if no longer used. + maybeRemoveImport("org.junit.jupiter.api.Assertions"); + + return method; + } + } +} diff --git a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgFailToAssertJFail.java b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgFailToAssertJFail.java new file mode 100644 index 000000000..f1b08758f --- /dev/null +++ b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgFailToAssertJFail.java @@ -0,0 +1,162 @@ +/* + * Copyright 2021 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.testng; + +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.RemoveUnusedImports; +import org.openrewrite.java.search.UsesType; +import org.openrewrite.java.tree.Expression; +import org.openrewrite.java.tree.J; +import org.openrewrite.java.tree.TypeUtils; + +import java.util.List; + +public class TestNgFailToAssertJFail + extends Recipe { + @Override + public String getDisplayName() { + return "JUnit fail to AssertJ"; + } + + @Override + public String getDescription() { + return "Convert JUnit-style `fail()` to AssertJ's `fail()`."; + } + + @Override + public TreeVisitor getVisitor() { + return Preconditions.check(new UsesType<>("org.junit.jupiter.api.Assertions", false), new JUnitFailToAssertJFailVisitor()); + } + + public static class JUnitFailToAssertJFailVisitor extends JavaIsoVisitor { + private JavaParser.Builder assertionsParser; + + private JavaParser.Builder assertionsParser(ExecutionContext ctx) { + if (assertionsParser == null) { + assertionsParser = JavaParser.fromJavaVersion() + .classpathFromResources(ctx, "assertj-core-3.24"); + } + return assertionsParser; + } + + private static final MethodMatcher JUNIT_FAIL_MATCHER = new MethodMatcher("org.junit.jupiter.api.Assertions" + " fail(..)"); + + @Override + public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { + J.MethodInvocation m = method; + + if (!JUNIT_FAIL_MATCHER.matches(m)) { + return m; + } + + List args = m.getArguments(); + + if (args.size() == 1) { + // fail(), fail(String), fail(Supplier), fail(Throwable) + if (args.get(0) instanceof J.Empty) { + m = JavaTemplate.builder("org.assertj.core.api.Assertions.fail(\"\");") + .javaParser(assertionsParser(ctx)) + .build() + .apply(getCursor(), m.getCoordinates().replace()); + } else if (args.get(0) instanceof J.Literal || + TypeUtils.isAssignableTo("java.lang.String", args.get(0).getType())) { + m = JavaTemplate.builder("org.assertj.core.api.Assertions.fail(#{any()});") + .javaParser(assertionsParser(ctx)) + .build() + .apply( + getCursor(), + m.getCoordinates().replace(), + args.get(0) + ); + } else { + m = JavaTemplate.builder("org.assertj.core.api.Assertions.fail(\"\", #{any()});") + .javaParser(assertionsParser(ctx)) + .build() + .apply( + getCursor(), + m.getCoordinates().replace(), + args.get(0) + ); + } + } else { + // fail(String, Throwable) + StringBuilder templateBuilder = new StringBuilder("org.assertj.core.api.Assertions.fail("); + for (int i = 0; i < args.size(); i++) { + templateBuilder.append("#{any()}"); + if (i < args.size() - 1) { + templateBuilder.append(", "); + } + } + templateBuilder.append(");"); + + m = JavaTemplate.builder(templateBuilder.toString()) + .javaParser(assertionsParser(ctx)) + .build() + .apply( + getCursor(), + m.getCoordinates().replace(), + args.toArray() + ); + } + + doAfterVisit(new RemoveUnusedImports().getVisitor()); + doAfterVisit(new UnqualifiedMethodInvocations()); + return m; + } + + private static class UnqualifiedMethodInvocations extends JavaIsoVisitor { + private static final MethodMatcher ASSERTJ_FAIL_MATCHER = new MethodMatcher("org.assertj.core.api.Assertions" + " fail(..)"); + + @Override + public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { + if (!ASSERTJ_FAIL_MATCHER.matches(method)) { + return method; + } + + StringBuilder templateBuilder = new StringBuilder("fail("); + List arguments = method.getArguments(); + for (int i = 0; i < arguments.size(); i++) { + templateBuilder.append("#{any()}"); + if (i < arguments.size() - 1) { + templateBuilder.append(", "); + } + } + templateBuilder.append(");"); + + method = JavaTemplate.builder(templateBuilder.toString()) + .staticImports("org.assertj.core.api.Assertions" + ".fail") + .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3.24")) + .build() + .apply( + getCursor(), + method.getCoordinates().replace(), + arguments.toArray() + ); + //Make sure there is a static import for "org.assertj.core.api.Assertions.assertThat" (even if not referenced) + maybeAddImport("org.assertj.core.api.Assertions", "fail", false); + maybeRemoveImport("org.junit.jupiter.api.Assertions.fail"); + return super.visitMethodInvocation(method, ctx); + } + } + } +} From cf77667638205dd68f5eeeb5bfd023a0e7dc3598 Mon Sep 17 00:00:00 2001 From: Sasha Sheikin Date: Tue, 4 Jun 2024 21:04:46 +0200 Subject: [PATCH 02/12] Adapt copied JUnit classes to TestNG --- .../TestNgAssertEqualsToAssertThat.java | 18 +++++++-------- .../testng/TestNgAssertFalseToAssertThat.java | 14 +++++------ .../TestNgAssertNotEqualsToAssertThat.java | 18 +++++++-------- .../TestNgAssertNotNullToAssertThat.java | 14 +++++------ .../testng/TestNgAssertNullToAssertThat.java | 14 +++++------ .../testng/TestNgAssertTrueToAssertThat.java | 14 +++++------ .../testng/TestNgFailToAssertJFail.java | 14 +++++------ .../resources/META-INF/rewrite/assertj.yml | 23 +++++++++++++++++++ 8 files changed, 76 insertions(+), 53 deletions(-) diff --git a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertEqualsToAssertThat.java b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertEqualsToAssertThat.java index c596cca59..ffc36ec5f 100644 --- a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertEqualsToAssertThat.java +++ b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertEqualsToAssertThat.java @@ -36,17 +36,17 @@ public class TestNgAssertEqualsToAssertThat @Override public String getDisplayName() { - return "JUnit `assertEquals` to AssertJ"; + return "TestNG `assertEquals` to AssertJ"; } @Override public String getDescription() { - return "Convert JUnit-style `assertEquals()` to AssertJ's `assertThat().isEqualTo()`."; + return "Convert TestNG-style `assertEquals()` to AssertJ's `assertThat().isEqualTo()`."; } @Override public TreeVisitor getVisitor() { - return Preconditions.check(new UsesType<>("org.junit.jupiter.api.Assertions", false), new AssertEqualsToAssertThatVisitor()); + return Preconditions.check(new UsesType<>("org.testng.Assert", false), new AssertEqualsToAssertThatVisitor()); } public static class AssertEqualsToAssertThatVisitor extends JavaIsoVisitor { @@ -60,23 +60,23 @@ public static class AssertEqualsToAssertThatVisitor extends JavaIsoVisitor args = method.getArguments(); - Expression expected = args.get(0); - Expression actual = args.get(1); + Expression expected = args.get(1); + Expression actual = args.get(0); //always add the import (even if not referenced) maybeAddImport("org.assertj.core.api.Assertions", "assertThat", false); - // Remove import for "org.junit.jupiter.api.Assertions" if no longer used. - maybeRemoveImport("org.junit.jupiter.api.Assertions"); + // Remove import for "org.testng.Assert" if no longer used. + maybeRemoveImport("org.testng.Assert"); if (args.size() == 2) { return JavaTemplate.builder("assertThat(#{any()}).isEqualTo(#{any()});") diff --git a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertFalseToAssertThat.java b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertFalseToAssertThat.java index 4fa3709b8..c5710403c 100644 --- a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertFalseToAssertThat.java +++ b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertFalseToAssertThat.java @@ -35,17 +35,17 @@ public class TestNgAssertFalseToAssertThat @Override public String getDisplayName() { - return "JUnit `assertFalse` to AssertJ"; + return "TestNG `assertFalse` to AssertJ"; } @Override public String getDescription() { - return "Convert JUnit-style `assertFalse()` to AssertJ's `assertThat().isFalse()`."; + return "Convert TestNG-style `assertFalse()` to AssertJ's `assertThat().isFalse()`."; } @Override public TreeVisitor getVisitor() { - return Preconditions.check(new UsesType<>("org.junit.jupiter.api.Assertions", false), new AssertFalseToAssertThatVisitor()); + return Preconditions.check(new UsesType<>("org.testng.Assert", false), new AssertFalseToAssertThatVisitor()); } public static class AssertFalseToAssertThatVisitor extends JavaIsoVisitor { @@ -59,11 +59,11 @@ public static class AssertFalseToAssertThatVisitor extends JavaIsoVisitor getVisitor() { - return Preconditions.check(new UsesType<>("org.junit.jupiter.api.Assertions", false), new AssertNotEqualsToAssertThatVisitor()); + return Preconditions.check(new UsesType<>("org.testng.Assert", false), new AssertNotEqualsToAssertThatVisitor()); } public static class AssertNotEqualsToAssertThatVisitor extends JavaIsoVisitor { @@ -60,18 +60,18 @@ public static class AssertNotEqualsToAssertThatVisitor extends JavaIsoVisitor args = method.getArguments(); - Expression expected = args.get(0); - Expression actual = args.get(1); + Expression expected = args.get(1); + Expression actual = args.get(0); if (args.size() == 2) { method = JavaTemplate.builder("assertThat(#{any()}).isNotEqualTo(#{any()});") @@ -142,8 +142,8 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu //Make sure there is a static import for "org.assertj.core.api.Assertions.assertThat" (even if not referenced) maybeAddImport("org.assertj.core.api.Assertions", "assertThat", false); - // Remove import for "org.junit.jupiter.api.Assertions" if no longer used. - maybeRemoveImport("org.junit.jupiter.api.Assertions"); + // Remove import for "org.testng.Assert" if no longer used. + maybeRemoveImport("org.testng.Assert"); return method; } diff --git a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNotNullToAssertThat.java b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNotNullToAssertThat.java index 1e57affff..63016ee29 100644 --- a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNotNullToAssertThat.java +++ b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNotNullToAssertThat.java @@ -35,17 +35,17 @@ public class TestNgAssertNotNullToAssertThat @Override public String getDisplayName() { - return "JUnit `assertNotNull` to AssertJ"; + return "TestNG `assertNotNull` to AssertJ"; } @Override public String getDescription() { - return "Convert JUnit-style `assertNotNull()` to AssertJ's `assertThat().isNotNull()`."; + return "Convert TestNG-style `assertNotNull()` to AssertJ's `assertThat().isNotNull()`."; } @Override public TreeVisitor getVisitor() { - return Preconditions.check(new UsesType<>("org.junit.jupiter.api.Assertions", false), new AssertNotNullToAssertThatVisitor()); + return Preconditions.check(new UsesType<>("org.testng.Assert", false), new AssertNotNullToAssertThatVisitor()); } public static class AssertNotNullToAssertThatVisitor extends JavaIsoVisitor { @@ -59,11 +59,11 @@ public static class AssertNotNullToAssertThatVisitor extends JavaIsoVisitor getVisitor() { - return Preconditions.check(new UsesType<>("org.junit.jupiter.api.Assertions", false), new AssertNullToAssertThatVisitor()); + return Preconditions.check(new UsesType<>("org.testng.Assert", false), new AssertNullToAssertThatVisitor()); } public static class AssertNullToAssertThatVisitor extends JavaIsoVisitor { @@ -59,11 +59,11 @@ public static class AssertNullToAssertThatVisitor extends JavaIsoVisitor getVisitor() { - return Preconditions.check(new UsesType<>("org.junit.jupiter.api.Assertions", false), new AssertTrueToAssertThatVisitor()); + return Preconditions.check(new UsesType<>("org.testng.Assert", false), new AssertTrueToAssertThatVisitor()); } public static class AssertTrueToAssertThatVisitor extends JavaIsoVisitor { @@ -58,11 +58,11 @@ public static class AssertTrueToAssertThatVisitor extends JavaIsoVisitor getVisitor() { - return Preconditions.check(new UsesType<>("org.junit.jupiter.api.Assertions", false), new JUnitFailToAssertJFailVisitor()); + return Preconditions.check(new UsesType<>("org.testng.Assert", false), new TestNgFailToAssertJFailVisitor()); } - public static class JUnitFailToAssertJFailVisitor extends JavaIsoVisitor { + public static class TestNgFailToAssertJFailVisitor extends JavaIsoVisitor { private JavaParser.Builder assertionsParser; private JavaParser.Builder assertionsParser(ExecutionContext ctx) { @@ -59,13 +59,13 @@ public static class JUnitFailToAssertJFailVisitor extends JavaIsoVisitor Date: Sat, 15 Jun 2024 20:16:21 +0200 Subject: [PATCH 03/12] Add a first recipe test reusing Picnic's TestNG rules --- build.gradle.kts | 4 ++ .../resources/META-INF/rewrite/assertj.yml | 24 +------ .../resources/META-INF/rewrite/testng.yml | 38 ++++++++++++ .../testing/testng/TestNgToAssertJTest.java | 62 +++++++++++++++++++ 4 files changed, 105 insertions(+), 23 deletions(-) create mode 100644 src/main/resources/META-INF/rewrite/testng.yml create mode 100644 src/test/java/org/openrewrite/java/testing/testng/TestNgToAssertJTest.java diff --git a/build.gradle.kts b/build.gradle.kts index 6e03b6a63..50ed722c6 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -34,6 +34,10 @@ dependencies { implementation("org.openrewrite:rewrite-maven") implementation("org.openrewrite.recipe:rewrite-java-dependencies:$rewriteVersion") implementation("org.openrewrite.recipe:rewrite-static-analysis:$rewriteVersion") + implementation("org.openrewrite.recipe:rewrite-third-party:$rewriteVersion") { + // TODO remove once available from https://github.com/openrewrite/rewrite-third-party/commit/474b3461cdae4bcbd80 + exclude(module = "jakarta.xml.bind-api") + } runtimeOnly("org.openrewrite:rewrite-java-17") compileOnly("org.projectlombok:lombok:latest.release") diff --git a/src/main/resources/META-INF/rewrite/assertj.yml b/src/main/resources/META-INF/rewrite/assertj.yml index 2cdc0f156..46ec6d4b4 100644 --- a/src/main/resources/META-INF/rewrite/assertj.yml +++ b/src/main/resources/META-INF/rewrite/assertj.yml @@ -24,6 +24,7 @@ tags: recipeList: - org.openrewrite.java.testing.hamcrest.MigrateHamcrestToAssertJ - org.openrewrite.java.testing.assertj.JUnitToAssertj + - org.openrewrite.java.testing.testng.TestNgToAssertj - org.openrewrite.java.testing.assertj.StaticImports - org.openrewrite.java.testing.assertj.SimplifyChainedAssertJAssertions @@ -364,26 +365,3 @@ recipeList: version: 3.x onlyIfUsing: org.assertj.core.api.Assertions acceptTransitive: true - ---- -type: specs.openrewrite.org/v1beta/recipe -name: org.openrewrite.java.testing.assertj.TestNgToAssertj -displayName: Migrate TestNG asserts to AssertJ -description: AssertJ provides a rich set of assertions, truly helpful error messages, improves test code readability. Converts assertions from `org.testng.Assert` to `org.assertj.core.api.Assertions`. -tags: - - testing - - assertj -recipeList: - - org.openrewrite.java.testing.assertj.testng.TestNgAssertEqualsToAssertThat - - org.openrewrite.java.testing.assertj.testng.TestNgAssertFalseToAssertThat - - org.openrewrite.java.testing.assertj.testng.TestNgAssertNotEqualsToAssertThat - - org.openrewrite.java.testing.assertj.testng.TestNgAssertNotNullToAssertThat - - org.openrewrite.java.testing.assertj.testng.TestNgAssertNullToAssertThat - - org.openrewrite.java.testing.assertj.testng.TestNgAssertTrueToAssertThat - - org.openrewrite.java.testing.assertj.testng.TestNgFailToAssertJFail - - org.openrewrite.java.dependencies.AddDependency: - groupId: org.assertj - artifactId: assertj-core - version: 3.x - onlyIfUsing: org.assertj.core.api.Assertions - acceptTransitive: true diff --git a/src/main/resources/META-INF/rewrite/testng.yml b/src/main/resources/META-INF/rewrite/testng.yml new file mode 100644 index 000000000..7fc16db45 --- /dev/null +++ b/src/main/resources/META-INF/rewrite/testng.yml @@ -0,0 +1,38 @@ +# +# 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. +# +--- +type: specs.openrewrite.org/v1beta/recipe +name: org.openrewrite.java.testing.testng.TestNgToAssertj +displayName: Migrate TestNG asserts to AssertJ +description: AssertJ provides a rich set of assertions, truly helpful error messages, improves test code readability. Converts assertions from `org.testng.Assert` to `org.assertj.core.api.Assertions`. +tags: + - testing + - assertj +recipeList: + - tech.picnic.errorprone.refasterrules.TestNGToAssertJRulesRecipes +# - org.openrewrite.java.testing.assertj.testng.TestNgAssertEqualsToAssertThat +# - org.openrewrite.java.testing.assertj.testng.TestNgAssertFalseToAssertThat +# - org.openrewrite.java.testing.assertj.testng.TestNgAssertNotEqualsToAssertThat +# - org.openrewrite.java.testing.assertj.testng.TestNgAssertNotNullToAssertThat +# - org.openrewrite.java.testing.assertj.testng.TestNgAssertNullToAssertThat +# - org.openrewrite.java.testing.assertj.testng.TestNgAssertTrueToAssertThat +# - org.openrewrite.java.testing.assertj.testng.TestNgFailToAssertJFail + - org.openrewrite.java.dependencies.AddDependency: + groupId: org.assertj + artifactId: assertj-core + version: 3.x + onlyIfUsing: org.assertj.core.api.Assertions + acceptTransitive: true diff --git a/src/test/java/org/openrewrite/java/testing/testng/TestNgToAssertJTest.java b/src/test/java/org/openrewrite/java/testing/testng/TestNgToAssertJTest.java new file mode 100644 index 000000000..e4a8d6eca --- /dev/null +++ b/src/test/java/org/openrewrite/java/testing/testng/TestNgToAssertJTest.java @@ -0,0 +1,62 @@ +/* + * 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.testng; + +import org.junit.jupiter.api.Test; +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 TestNgToAssertJTest implements RewriteTest { + @Override + public void defaults(RecipeSpec spec) { + spec + .parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(),"testng")) + .recipeFromResources("org.openrewrite.java.testing.testng.TestNgToAssertj"); + } + + @Test + void fail() { + rewriteRun( + //language=java + java( + """ + import static org.testng.Assert.fail; + + class Test { + void testFail() { + fail("foo"); + fail("foo", new IllegalStateException()); + } + } + """, + """ + import org.assertj.core.api.Assertions; + + class Test { + void testFail() { + Assertions.fail("foo"); + Assertions.fail("foo", new IllegalStateException()); + } + } + """ + ) + ); + } +} From 4773a05efe76104a52a57cdf570ab09d4de27ab4 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Sat, 15 Jun 2024 20:27:09 +0200 Subject: [PATCH 04/12] Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- .../testing/assertj/testng/TestNgFailToAssertJFail.java | 6 +----- .../java/testing/testng/TestNgToAssertJTest.java | 2 ++ 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgFailToAssertJFail.java b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgFailToAssertJFail.java index df6243fe8..d3d5fdfdd 100644 --- a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgFailToAssertJFail.java +++ b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgFailToAssertJFail.java @@ -19,11 +19,7 @@ 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.RemoveUnusedImports; +import org.openrewrite.java.*; import org.openrewrite.java.search.UsesType; import org.openrewrite.java.tree.Expression; import org.openrewrite.java.tree.J; diff --git a/src/test/java/org/openrewrite/java/testing/testng/TestNgToAssertJTest.java b/src/test/java/org/openrewrite/java/testing/testng/TestNgToAssertJTest.java index e4a8d6eca..136b8c23c 100644 --- a/src/test/java/org/openrewrite/java/testing/testng/TestNgToAssertJTest.java +++ b/src/test/java/org/openrewrite/java/testing/testng/TestNgToAssertJTest.java @@ -16,6 +16,7 @@ package org.openrewrite.java.testing.testng; import org.junit.jupiter.api.Test; +import org.openrewrite.DocumentExample; import org.openrewrite.InMemoryExecutionContext; import org.openrewrite.java.JavaParser; import org.openrewrite.test.RecipeSpec; @@ -31,6 +32,7 @@ public void defaults(RecipeSpec spec) { .recipeFromResources("org.openrewrite.java.testing.testng.TestNgToAssertj"); } + @DocumentExample @Test void fail() { rewriteRun( From 37bb2e0762c04cb75ff69ac3bad95abb52d7d8b1 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Sat, 15 Jun 2024 22:56:06 +0200 Subject: [PATCH 05/12] Move test and assert static imports inserted --- .../testing/assertj/TestNgToAssertJTest.java | 106 ++++++++++++++++++ .../testing/testng/TestNgToAssertJTest.java | 64 ----------- 2 files changed, 106 insertions(+), 64 deletions(-) create mode 100644 src/test/java/org/openrewrite/java/testing/assertj/TestNgToAssertJTest.java delete mode 100644 src/test/java/org/openrewrite/java/testing/testng/TestNgToAssertJTest.java diff --git a/src/test/java/org/openrewrite/java/testing/assertj/TestNgToAssertJTest.java b/src/test/java/org/openrewrite/java/testing/assertj/TestNgToAssertJTest.java new file mode 100644 index 000000000..0ffbf4365 --- /dev/null +++ b/src/test/java/org/openrewrite/java/testing/assertj/TestNgToAssertJTest.java @@ -0,0 +1,106 @@ +/* + * 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 TestNgToAssertJTest implements RewriteTest { + @Override + public void defaults(RecipeSpec spec) { + spec + .parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "testng")) + .recipeFromResources("org.openrewrite.java.testing.assertj.Assertj"); + } + + /** + * Verify that we can throw new Exception from the JavaTemplate in the generated recipe. + */ + @DocumentExample + @Test + void failWithMessage() { + rewriteRun( + //language=java + java( + """ + import static org.testng.Assert.fail; + + class Test { + void test() { + fail("foo"); + fail("foo", new IllegalStateException()); + fail(); + } + } + """, + """ + import static org.assertj.core.api.Assertions.fail; + + class Test { + void test() { + fail("foo"); + fail("foo", new IllegalStateException()); + throw new AssertionError(); + } + } + """ + ) + ); + } + + /** + * Verify some assertions as implemented through Refaster rules converted to Recipes. No need to test all variants. + */ + @Test + void assertTrueFalse() { + rewriteRun( + //language=java + java( + """ + import static org.testng.Assert.assertFalse; + import static org.testng.Assert.assertTrue; + + class Test { + void test() { + assertTrue(true); + assertTrue(true, "foo"); + assertFalse(false); + assertFalse(false, "foo"); + } + } + """, + """ + import static org.assertj.core.api.Assertions.assertThat; + + class Test { + void test() { + assertThat(true).isTrue(); + assertThat(true).withFailMessage("foo").isTrue(); + assertThat(false).isFalse(); + assertThat(false).withFailMessage("foo").isFalse(); + } + } + """ + ) + ); + } +} diff --git a/src/test/java/org/openrewrite/java/testing/testng/TestNgToAssertJTest.java b/src/test/java/org/openrewrite/java/testing/testng/TestNgToAssertJTest.java deleted file mode 100644 index 136b8c23c..000000000 --- a/src/test/java/org/openrewrite/java/testing/testng/TestNgToAssertJTest.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * 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.testng; - -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 TestNgToAssertJTest implements RewriteTest { - @Override - public void defaults(RecipeSpec spec) { - spec - .parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(),"testng")) - .recipeFromResources("org.openrewrite.java.testing.testng.TestNgToAssertj"); - } - - @DocumentExample - @Test - void fail() { - rewriteRun( - //language=java - java( - """ - import static org.testng.Assert.fail; - - class Test { - void testFail() { - fail("foo"); - fail("foo", new IllegalStateException()); - } - } - """, - """ - import org.assertj.core.api.Assertions; - - class Test { - void testFail() { - Assertions.fail("foo"); - Assertions.fail("foo", new IllegalStateException()); - } - } - """ - ) - ); - } -} From 612c5586073729e569d0310b4f18c1b560f00ba5 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Tue, 10 Dec 2024 22:42:48 +0100 Subject: [PATCH 06/12] Update assertions and enable custom recipes again --- src/main/resources/META-INF/rewrite/testng.yml | 18 +++++++++--------- .../TestNgToAssertJTest.java | 8 ++++---- 2 files changed, 13 insertions(+), 13 deletions(-) rename src/test/java/org/openrewrite/java/testing/{assertj => testng}/TestNgToAssertJTest.java (92%) diff --git a/src/main/resources/META-INF/rewrite/testng.yml b/src/main/resources/META-INF/rewrite/testng.yml index 7fc16db45..d6055efbe 100644 --- a/src/main/resources/META-INF/rewrite/testng.yml +++ b/src/main/resources/META-INF/rewrite/testng.yml @@ -22,17 +22,17 @@ tags: - testing - assertj recipeList: - - tech.picnic.errorprone.refasterrules.TestNGToAssertJRulesRecipes -# - org.openrewrite.java.testing.assertj.testng.TestNgAssertEqualsToAssertThat -# - org.openrewrite.java.testing.assertj.testng.TestNgAssertFalseToAssertThat -# - org.openrewrite.java.testing.assertj.testng.TestNgAssertNotEqualsToAssertThat -# - org.openrewrite.java.testing.assertj.testng.TestNgAssertNotNullToAssertThat -# - org.openrewrite.java.testing.assertj.testng.TestNgAssertNullToAssertThat -# - org.openrewrite.java.testing.assertj.testng.TestNgAssertTrueToAssertThat -# - org.openrewrite.java.testing.assertj.testng.TestNgFailToAssertJFail - org.openrewrite.java.dependencies.AddDependency: groupId: org.assertj artifactId: assertj-core version: 3.x - onlyIfUsing: org.assertj.core.api.Assertions + onlyIfUsing: org.testng.* acceptTransitive: true + - tech.picnic.errorprone.refasterrules.TestNGToAssertJRulesRecipes + - org.openrewrite.java.testing.assertj.testng.TestNgAssertEqualsToAssertThat + - org.openrewrite.java.testing.assertj.testng.TestNgAssertFalseToAssertThat + - org.openrewrite.java.testing.assertj.testng.TestNgAssertNotEqualsToAssertThat + - org.openrewrite.java.testing.assertj.testng.TestNgAssertNotNullToAssertThat + - org.openrewrite.java.testing.assertj.testng.TestNgAssertNullToAssertThat + - org.openrewrite.java.testing.assertj.testng.TestNgAssertTrueToAssertThat + - org.openrewrite.java.testing.assertj.testng.TestNgFailToAssertJFail diff --git a/src/test/java/org/openrewrite/java/testing/assertj/TestNgToAssertJTest.java b/src/test/java/org/openrewrite/java/testing/testng/TestNgToAssertJTest.java similarity index 92% rename from src/test/java/org/openrewrite/java/testing/assertj/TestNgToAssertJTest.java rename to src/test/java/org/openrewrite/java/testing/testng/TestNgToAssertJTest.java index 0ffbf4365..2b5593d09 100644 --- a/src/test/java/org/openrewrite/java/testing/assertj/TestNgToAssertJTest.java +++ b/src/test/java/org/openrewrite/java/testing/testng/TestNgToAssertJTest.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.openrewrite.java.testing.assertj; +package org.openrewrite.java.testing.testng; import org.junit.jupiter.api.Test; import org.openrewrite.DocumentExample; @@ -59,7 +59,7 @@ class Test { void test() { fail("foo"); fail("foo", new IllegalStateException()); - throw new AssertionError(); + fail(""); } } """ @@ -94,9 +94,9 @@ void test() { class Test { void test() { assertThat(true).isTrue(); - assertThat(true).withFailMessage("foo").isTrue(); + assertThat(true).as("foo").isTrue(); assertThat(false).isFalse(); - assertThat(false).withFailMessage("foo").isFalse(); + assertThat(false).as("foo").isFalse(); } } """ From cf8957d5bf08bcaeba839f09f57a4480396c5285 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Tue, 10 Dec 2024 22:49:06 +0100 Subject: [PATCH 07/12] Use `classpath` instead of missing `classpathFromResources` --- build.gradle.kts | 1 + .../resources/META-INF/rewrite/testng.yml | 21 ++++++++++--------- .../testing/testng/TestNgToAssertJTest.java | 8 +++---- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 826ce8dc0..55200b105 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -70,6 +70,7 @@ dependencies { exclude(group = "org.yaml", module = "snakeyaml") } testRuntimeOnly("org.easymock:easymock:latest.release") + testRuntimeOnly("org.testng:testng:latest.release") testRuntimeOnly("org.mockito.kotlin:mockito-kotlin:latest.release") testRuntimeOnly("org.testcontainers:testcontainers:latest.release") testRuntimeOnly("org.testcontainers:nginx:latest.release") diff --git a/src/main/resources/META-INF/rewrite/testng.yml b/src/main/resources/META-INF/rewrite/testng.yml index d6055efbe..b7c9818e4 100644 --- a/src/main/resources/META-INF/rewrite/testng.yml +++ b/src/main/resources/META-INF/rewrite/testng.yml @@ -16,11 +16,12 @@ --- type: specs.openrewrite.org/v1beta/recipe name: org.openrewrite.java.testing.testng.TestNgToAssertj -displayName: Migrate TestNG asserts to AssertJ -description: AssertJ provides a rich set of assertions, truly helpful error messages, improves test code readability. Converts assertions from `org.testng.Assert` to `org.assertj.core.api.Assertions`. +displayName: Migrate TestNG assertions to AssertJ +description: Convert assertions from `org.testng.Assert` to `org.assertj.core.api.Assertions`. tags: - - testing - assertj + - testing + - testng recipeList: - org.openrewrite.java.dependencies.AddDependency: groupId: org.assertj @@ -29,10 +30,10 @@ recipeList: onlyIfUsing: org.testng.* acceptTransitive: true - tech.picnic.errorprone.refasterrules.TestNGToAssertJRulesRecipes - - org.openrewrite.java.testing.assertj.testng.TestNgAssertEqualsToAssertThat - - org.openrewrite.java.testing.assertj.testng.TestNgAssertFalseToAssertThat - - org.openrewrite.java.testing.assertj.testng.TestNgAssertNotEqualsToAssertThat - - org.openrewrite.java.testing.assertj.testng.TestNgAssertNotNullToAssertThat - - org.openrewrite.java.testing.assertj.testng.TestNgAssertNullToAssertThat - - org.openrewrite.java.testing.assertj.testng.TestNgAssertTrueToAssertThat - - org.openrewrite.java.testing.assertj.testng.TestNgFailToAssertJFail +# - org.openrewrite.java.testing.assertj.testng.TestNgAssertEqualsToAssertThat +# - org.openrewrite.java.testing.assertj.testng.TestNgAssertFalseToAssertThat +# - org.openrewrite.java.testing.assertj.testng.TestNgAssertNotEqualsToAssertThat +# - org.openrewrite.java.testing.assertj.testng.TestNgAssertNotNullToAssertThat +# - org.openrewrite.java.testing.assertj.testng.TestNgAssertNullToAssertThat +# - org.openrewrite.java.testing.assertj.testng.TestNgAssertTrueToAssertThat +# - org.openrewrite.java.testing.assertj.testng.TestNgFailToAssertJFail diff --git a/src/test/java/org/openrewrite/java/testing/testng/TestNgToAssertJTest.java b/src/test/java/org/openrewrite/java/testing/testng/TestNgToAssertJTest.java index 2b5593d09..47ef9c5e8 100644 --- a/src/test/java/org/openrewrite/java/testing/testng/TestNgToAssertJTest.java +++ b/src/test/java/org/openrewrite/java/testing/testng/TestNgToAssertJTest.java @@ -28,7 +28,7 @@ class TestNgToAssertJTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { spec - .parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "testng")) + .parser(JavaParser.fromJavaVersion().classpath("testng")) .recipeFromResources("org.openrewrite.java.testing.assertj.Assertj"); } @@ -59,7 +59,7 @@ class Test { void test() { fail("foo"); fail("foo", new IllegalStateException()); - fail(""); + fail(); } } """ @@ -94,9 +94,9 @@ void test() { class Test { void test() { assertThat(true).isTrue(); - assertThat(true).as("foo").isTrue(); + assertThat(true).withFailMessage("foo").isTrue(); assertThat(false).isFalse(); - assertThat(false).as("foo").isFalse(); + assertThat(false).withFailMessage("foo").isFalse(); } } """ From e53927a6a19b4b7c4a3919ea112c55b4edac0910 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Tue, 10 Dec 2024 23:06:51 +0100 Subject: [PATCH 08/12] Inline `JavaParser.Builder` and `Visitor` --- .../TestNgAssertEqualsToAssertThat.java | 164 +++++++------- .../testng/TestNgAssertFalseToAssertThat.java | 103 ++++----- .../TestNgAssertNotEqualsToAssertThat.java | 210 +++++++++--------- .../TestNgAssertNotNullToAssertThat.java | 116 +++++----- .../testng/TestNgAssertNullToAssertThat.java | 114 +++++----- .../testng/TestNgAssertTrueToAssertThat.java | 102 ++++----- .../testng/TestNgFailToAssertJFail.java | 177 +++++++-------- 7 files changed, 444 insertions(+), 542 deletions(-) diff --git a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertEqualsToAssertThat.java b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertEqualsToAssertThat.java index ffc36ec5f..fd891a293 100644 --- a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertEqualsToAssertThat.java +++ b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertEqualsToAssertThat.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 the original author or authors. + * 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. @@ -31,8 +31,7 @@ import java.util.List; -public class TestNgAssertEqualsToAssertThat - extends Recipe { +public class TestNgAssertEqualsToAssertThat extends Recipe { @Override public String getDisplayName() { @@ -46,105 +45,98 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { - return Preconditions.check(new UsesType<>("org.testng.Assert", false), new AssertEqualsToAssertThatVisitor()); - } - - public static class AssertEqualsToAssertThatVisitor extends JavaIsoVisitor { - private JavaParser.Builder assertionsParser; - - private JavaParser.Builder assertionsParser(ExecutionContext ctx) { - if (assertionsParser == null) { - assertionsParser = JavaParser.fromJavaVersion() - .classpathFromResources(ctx, "assertj-core-3.24"); - } - return assertionsParser; - } - - private static final MethodMatcher TESTNG_ASSERT_EQUALS = new MethodMatcher("org.testng.Assert" + " assertEquals(..)"); + return Preconditions.check(new UsesType<>("org.testng.Assert", false), new JavaIsoVisitor() { + private final MethodMatcher TESTNG_ASSERT_EQUALS = new MethodMatcher("org.testng.Assert" + " assertEquals(..)"); - @Override - public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { - if (!TESTNG_ASSERT_EQUALS.matches(method)) { - return method; - } + @Override + public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { + if (!TESTNG_ASSERT_EQUALS.matches(method)) { + return method; + } - List args = method.getArguments(); - Expression expected = args.get(1); - Expression actual = args.get(0); + List args = method.getArguments(); + Expression expected = args.get(1); + Expression actual = args.get(0); - //always add the import (even if not referenced) - maybeAddImport("org.assertj.core.api.Assertions", "assertThat", false); - - // Remove import for "org.testng.Assert" if no longer used. - maybeRemoveImport("org.testng.Assert"); + //always add the import (even if not referenced) + maybeAddImport("org.assertj.core.api.Assertions", "assertThat", false); + + // Remove import for "org.testng.Assert" if no longer used. + maybeRemoveImport("org.testng.Assert"); + + if (args.size() == 2) { + return JavaTemplate.builder("assertThat(#{any()}).isEqualTo(#{any()});") + .staticImports("org.assertj.core.api.Assertions.assertThat") + .javaParser(JavaParser.fromJavaVersion() + .classpathFromResources(ctx, "assertj-core-3.24")) + .build() + .apply(getCursor(), method.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)}).isEqualTo(#{any()});") : + JavaTemplate.builder("assertThat(#{any()}).as(#{any(java.util.function.Supplier)}).isEqualTo(#{any()});"); + return template + .staticImports("org.assertj.core.api.Assertions.assertThat") + .imports("java.util.function.Supplier") + .javaParser(JavaParser.fromJavaVersion() + .classpathFromResources(ctx, "assertj-core-3.24")) + .build() + .apply( + getCursor(), + method.getCoordinates().replace(), + actual, + message, + expected + ); + } else if (args.size() == 3) { + //always add the import (even if not referenced) + maybeAddImport("org.assertj.core.api.Assertions", "within", false); + return JavaTemplate.builder("assertThat(#{any()}).isCloseTo(#{any()}, within(#{any()}));") + .staticImports("org.assertj.core.api.Assertions.assertThat", "org.assertj.core.api.Assertions.within") + .javaParser(JavaParser.fromJavaVersion() + .classpathFromResources(ctx, "assertj-core-3.24")) + .build() + .apply(getCursor(), method.getCoordinates().replace(), actual, expected, args.get(2)); + + } + + // The assertEquals is using a floating point with a delta argument and a message. + Expression message = args.get(3); - if (args.size() == 2) { - return JavaTemplate.builder("assertThat(#{any()}).isEqualTo(#{any()});") - .staticImports("org.assertj.core.api.Assertions.assertThat") - .javaParser(assertionsParser(ctx)) - .build() - .apply(getCursor(), method.getCoordinates().replace(), actual, expected); - } else if (args.size() == 3 && !isFloatingPointType(args.get(2))) { - Expression message = args.get(2); + //always add the import (even if not referenced) + maybeAddImport("org.assertj.core.api.Assertions", "within", false); 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()});"); + 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 - .staticImports("org.assertj.core.api.Assertions.assertThat") + .staticImports("org.assertj.core.api.Assertions.assertThat", "org.assertj.core.api.Assertions.within") .imports("java.util.function.Supplier") - .javaParser(assertionsParser(ctx)) + .javaParser(JavaParser.fromJavaVersion() + .classpathFromResources(ctx, "assertj-core-3.24")) .build() .apply( getCursor(), method.getCoordinates().replace(), actual, message, - expected + expected, + args.get(2) ); - } else if (args.size() == 3) { - //always add the import (even if not referenced) - maybeAddImport("org.assertj.core.api.Assertions", "within", false); - return JavaTemplate.builder("assertThat(#{any()}).isCloseTo(#{any()}, within(#{any()}));") - .staticImports("org.assertj.core.api.Assertions.assertThat", "org.assertj.core.api.Assertions.within") - .javaParser(assertionsParser(ctx)) - .build() - .apply(getCursor(), method.getCoordinates().replace(), actual, expected, args.get(2)); - } - // The assertEquals is using a floating point with a delta argument and a message. - Expression message = args.get(3); - - //always add the import (even if not referenced) - maybeAddImport("org.assertj.core.api.Assertions", "within", false); - 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 - .staticImports("org.assertj.core.api.Assertions.assertThat", "org.assertj.core.api.Assertions.within") - .imports("java.util.function.Supplier") - .javaParser(assertionsParser(ctx)) - .build() - .apply( - getCursor(), - method.getCoordinates().replace(), - actual, - message, - expected, - args.get(2) - ); - } - - private static 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); - } + 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; - } + JavaType.Primitive parameterType = TypeUtils.asPrimitive(expression.getType()); + return parameterType == JavaType.Primitive.Double || parameterType == JavaType.Primitive.Float; + } + }); } + } diff --git a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertFalseToAssertThat.java b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertFalseToAssertThat.java index c5710403c..648a28c03 100644 --- a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertFalseToAssertThat.java +++ b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertFalseToAssertThat.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 the original author or authors. + * 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. @@ -30,8 +30,7 @@ import java.util.List; -public class TestNgAssertFalseToAssertThat - extends Recipe { +public class TestNgAssertFalseToAssertThat extends Recipe { @Override public String getDisplayName() { @@ -45,66 +44,48 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { - return Preconditions.check(new UsesType<>("org.testng.Assert", false), new AssertFalseToAssertThatVisitor()); - } - - public static class AssertFalseToAssertThatVisitor extends JavaIsoVisitor { - private JavaParser.Builder assertionsParser; - - private JavaParser.Builder assertionsParser(ExecutionContext ctx) { - if (assertionsParser == null) { - assertionsParser = JavaParser.fromJavaVersion() - .classpathFromResources(ctx, "assertj-core-3.24"); - } - return assertionsParser; - } + return Preconditions.check(new UsesType<>("org.testng.Assert", false), new JavaIsoVisitor() { + private final MethodMatcher TESTNG_ASSERT_FALSE = new MethodMatcher("org.testng.Assert" + " assertFalse(boolean, ..)"); + + @Override + public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { + if (!TESTNG_ASSERT_FALSE.matches(method)) { + return method; + } + + List args = method.getArguments(); + Expression actual = args.get(0); + + if (args.size() == 1) { + method = JavaTemplate.builder("assertThat(#{any(boolean)}).isFalse();") + .staticImports("org.assertj.core.api.Assertions.assertThat") + .javaParser(JavaParser.fromJavaVersion() + .classpathFromResources(ctx, "assertj-core-3.24")) + .build() + .apply(getCursor(), method.getCoordinates().replace(), actual); + } else { + 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();"); + + method = template + .staticImports("org.assertj.core.api.Assertions.assertThat") + .javaParser(JavaParser.fromJavaVersion() + .classpathFromResources(ctx, "assertj-core-3.24")) + .build() + .apply(getCursor(), method.getCoordinates().replace(), actual, message); + } + + //Make sure there is a static import for "org.assertj.core.api.Assertions.assertThat" (even if not referenced) + maybeAddImport("org.assertj.core.api.Assertions", "assertThat", false); + + // Remove import for "org.testng.Assert" if no longer used. + maybeRemoveImport("org.testng.Assert"); - private static final MethodMatcher TESTNG_ASSERT_FALSE = new MethodMatcher("org.testng.Assert" + " assertFalse(boolean, ..)"); - - @Override - public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { - if (!TESTNG_ASSERT_FALSE.matches(method)) { return method; } - - List args = method.getArguments(); - Expression actual = args.get(0); - - if (args.size() == 1) { - method = JavaTemplate.builder("assertThat(#{any(boolean)}).isFalse();") - .staticImports("org.assertj.core.api.Assertions.assertThat") - .javaParser(assertionsParser(ctx)) - .build() - .apply( - getCursor(), - method.getCoordinates().replace(), - actual - ); - } else { - 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();"); - - method = template - .staticImports("org.assertj.core.api.Assertions.assertThat") - .javaParser(assertionsParser(ctx)) - .build() - .apply( - getCursor(), - method.getCoordinates().replace(), - actual, - message - ); - } - - //Make sure there is a static import for "org.assertj.core.api.Assertions.assertThat" (even if not referenced) - maybeAddImport("org.assertj.core.api.Assertions", "assertThat", false); - - // Remove import for "org.testng.Assert" if no longer used. - maybeRemoveImport("org.testng.Assert"); - - return method; - } + }); } + } diff --git a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNotEqualsToAssertThat.java b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNotEqualsToAssertThat.java index 2d1d1d567..5d351ab9b 100644 --- a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNotEqualsToAssertThat.java +++ b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNotEqualsToAssertThat.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 the original author or authors. + * 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. @@ -31,8 +31,7 @@ import java.util.List; -public class TestNgAssertNotEqualsToAssertThat - extends Recipe { +public class TestNgAssertNotEqualsToAssertThat extends Recipe { @Override public String getDisplayName() { @@ -46,117 +45,110 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { - return Preconditions.check(new UsesType<>("org.testng.Assert", false), new AssertNotEqualsToAssertThatVisitor()); - } - - public static class AssertNotEqualsToAssertThatVisitor extends JavaIsoVisitor { - private JavaParser.Builder assertionsParser; - - private JavaParser.Builder assertionsParser(ExecutionContext ctx) { - if (assertionsParser == null) { - assertionsParser = JavaParser.fromJavaVersion() - .classpathFromResources(ctx, "assertj-core-3.24"); - } - return assertionsParser; - } - - private static final MethodMatcher TESTNG_ASSERT_EQUALS = new MethodMatcher("org.testng.Assert" + " assertNotEquals(..)"); + return Preconditions.check(new UsesType<>("org.testng.Assert", false), new JavaIsoVisitor() { + private final MethodMatcher TESTNG_ASSERT_EQUALS = new MethodMatcher("org.testng.Assert" + " assertNotEquals(..)"); + + @Override + public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { + if (!TESTNG_ASSERT_EQUALS.matches(method)) { + return method; + } + + List args = method.getArguments(); + + Expression expected = args.get(1); + Expression actual = args.get(0); + + if (args.size() == 2) { + method = JavaTemplate.builder("assertThat(#{any()}).isNotEqualTo(#{any()});") + .staticImports("org.assertj.core.api.Assertions.assertThat") + .javaParser(JavaParser.fromJavaVersion() + .classpathFromResources(ctx, "assertj-core-3.24")) + .build() + .apply( + getCursor(), + method.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()});"); + + + method = template + .staticImports("org.assertj.core.api.Assertions.assertThat") + .javaParser(JavaParser.fromJavaVersion() + .classpathFromResources(ctx, "assertj-core-3.24")) + .build() + .apply( + getCursor(), + method.getCoordinates().replace(), + actual, + message, + expected + ); + } else if (args.size() == 3) { + method = JavaTemplate.builder("assertThat(#{any()}).isNotCloseTo(#{any()}, within(#{any()}));") + .staticImports("org.assertj.core.api.Assertions.assertThat", "org.assertj.core.api.Assertions.within") + .javaParser(JavaParser.fromJavaVersion() + .classpathFromResources(ctx, "assertj-core-3.24")) + .build() + .apply( + getCursor(), + method.getCoordinates().replace(), + actual, + expected, + args.get(2) + ); + maybeAddImport("org.assertj.core.api.Assertions", "within", false); + } else { + 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()}));"); + + method = template + .staticImports("org.assertj.core.api.Assertions.assertThat", "org.assertj.core.api.Assertions.within") + .javaParser(JavaParser.fromJavaVersion() + .classpathFromResources(ctx, "assertj-core-3.24")) + .build() + .apply( + getCursor(), + method.getCoordinates().replace(), + actual, + message, + expected, + args.get(2) + ); + + maybeAddImport("org.assertj.core.api.Assertions", "within", false); + } + + //Make sure there is a static import for "org.assertj.core.api.Assertions.assertThat" (even if not referenced) + maybeAddImport("org.assertj.core.api.Assertions", "assertThat", false); + + // Remove import for "org.testng.Assert" if no longer used. + maybeRemoveImport("org.testng.Assert"); - @Override - public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { - if (!TESTNG_ASSERT_EQUALS.matches(method)) { return method; } - List args = method.getArguments(); - - Expression expected = args.get(1); - Expression actual = args.get(0); - - if (args.size() == 2) { - method = JavaTemplate.builder("assertThat(#{any()}).isNotEqualTo(#{any()});") - .staticImports("org.assertj.core.api.Assertions.assertThat") - .javaParser(assertionsParser(ctx)) - .build() - .apply( - getCursor(), - method.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()});"); - - - method = template - .staticImports("org.assertj.core.api.Assertions.assertThat") - .javaParser(assertionsParser(ctx)) - .build() - .apply( - getCursor(), - method.getCoordinates().replace(), - actual, - message, - expected - ); - } else if (args.size() == 3) { - method = JavaTemplate.builder("assertThat(#{any()}).isNotCloseTo(#{any()}, within(#{any()}));") - .staticImports("org.assertj.core.api.Assertions.assertThat", "org.assertj.core.api.Assertions.within") - .javaParser(assertionsParser(ctx)) - .build() - .apply( - getCursor(), - method.getCoordinates().replace(), - actual, - expected, - args.get(2) - ); - maybeAddImport("org.assertj.core.api.Assertions", "within", false); - } else { - 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()}));"); - - method = template - .staticImports("org.assertj.core.api.Assertions.assertThat", "org.assertj.core.api.Assertions.within") - .javaParser(assertionsParser(ctx)) - .build() - .apply( - getCursor(), - method.getCoordinates().replace(), - actual, - message, - expected, - args.get(2) - ); - - maybeAddImport("org.assertj.core.api.Assertions", "within", false); - } + 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); + } - //Make sure there is a static import for "org.assertj.core.api.Assertions.assertThat" (even if not referenced) - maybeAddImport("org.assertj.core.api.Assertions", "assertThat", false); - - // Remove import for "org.testng.Assert" if no longer used. - maybeRemoveImport("org.testng.Assert"); - - return method; - } - - private static 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; } - - JavaType.Primitive parameterType = TypeUtils.asPrimitive(expression.getType()); - return parameterType == JavaType.Primitive.Double || parameterType == JavaType.Primitive.Float; - } + }); } + } diff --git a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNotNullToAssertThat.java b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNotNullToAssertThat.java index 63016ee29..563ec6145 100644 --- a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNotNullToAssertThat.java +++ b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNotNullToAssertThat.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 the original author or authors. + * 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. @@ -30,8 +30,7 @@ import java.util.List; -public class TestNgAssertNotNullToAssertThat - extends Recipe { +public class TestNgAssertNotNullToAssertThat extends Recipe { @Override public String getDisplayName() { @@ -45,68 +44,59 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { - return Preconditions.check(new UsesType<>("org.testng.Assert", false), new AssertNotNullToAssertThatVisitor()); - } - - public static class AssertNotNullToAssertThatVisitor extends JavaIsoVisitor { - private JavaParser.Builder assertionsParser; - - private JavaParser.Builder assertionsParser(ExecutionContext ctx) { - if (assertionsParser == null) { - assertionsParser = JavaParser.fromJavaVersion() - .classpathFromResources(ctx, "assertj-core-3.24"); - } - return assertionsParser; - } - - private static final MethodMatcher TESTNG_ASSERT_NOT_NULL_MATCHER = new MethodMatcher("org.testng.Assert" + " assertNotNull(..)"); + return Preconditions.check(new UsesType<>("org.testng.Assert", false), new JavaIsoVisitor() { + private final MethodMatcher TESTNG_ASSERT_NOT_NULL_MATCHER = new MethodMatcher("org.testng.Assert" + " assertNotNull(..)"); + + @Override + public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { + if (!TESTNG_ASSERT_NOT_NULL_MATCHER.matches(method)) { + return method; + } + + List args = method.getArguments(); + Expression actual = args.get(0); + + if (args.size() == 1) { + method = JavaTemplate.builder("assertThat(#{any()}).isNotNull();") + .staticImports("org.assertj.core.api.Assertions.assertThat") + .javaParser(JavaParser.fromJavaVersion() + .classpathFromResources(ctx, "assertj-core-3.24")) + .build() + .apply( + getCursor(), + method.getCoordinates().replace(), + actual + ); + + } else { + 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();"); + + method = template + .staticImports("org.assertj.core.api.Assertions.assertThat") + .javaParser(JavaParser.fromJavaVersion() + .classpathFromResources(ctx, "assertj-core-3.24")) + .build() + .apply( + getCursor(), + method.getCoordinates().replace(), + actual, + message + ); + } + + //Make sure there is a static import for "org.assertj.core.api.Assertions.assertThat" (even if not referenced) + maybeAddImport("org.assertj.core.api.Assertions", "assertThat", false); + + //And if there are no longer references to the TestNG assertions class, we can remove the import. + maybeRemoveImport("org.testng.Assert"); - @Override - public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { - if (!TESTNG_ASSERT_NOT_NULL_MATCHER.matches(method)) { return method; } - - List args = method.getArguments(); - Expression actual = args.get(0); - - if (args.size() == 1) { - method = JavaTemplate.builder("assertThat(#{any()}).isNotNull();") - .staticImports("org.assertj.core.api.Assertions.assertThat") - .javaParser(assertionsParser(ctx)) - .build() - .apply( - getCursor(), - method.getCoordinates().replace(), - actual - ); - - } else { - 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();"); - - method = template - .staticImports("org.assertj.core.api.Assertions.assertThat") - .javaParser(assertionsParser(ctx)) - .build() - .apply( - getCursor(), - method.getCoordinates().replace(), - actual, - message - ); - } - - //Make sure there is a static import for "org.assertj.core.api.Assertions.assertThat" (even if not referenced) - maybeAddImport("org.assertj.core.api.Assertions", "assertThat", false); - - //And if there are no longer references to the TestNG assertions class, we can remove the import. - maybeRemoveImport("org.testng.Assert"); - - return method; - } + }); } + } diff --git a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNullToAssertThat.java b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNullToAssertThat.java index 8773006d7..9624ce6ca 100644 --- a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNullToAssertThat.java +++ b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNullToAssertThat.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 the original author or authors. + * 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. @@ -30,8 +30,7 @@ import java.util.List; -public class TestNgAssertNullToAssertThat - extends Recipe { +public class TestNgAssertNullToAssertThat extends Recipe { @Override public String getDisplayName() { @@ -45,67 +44,58 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { - return Preconditions.check(new UsesType<>("org.testng.Assert", false), new AssertNullToAssertThatVisitor()); - } - - public static class AssertNullToAssertThatVisitor extends JavaIsoVisitor { - private JavaParser.Builder assertionsParser; - - private JavaParser.Builder assertionsParser(ExecutionContext ctx) { - if (assertionsParser == null) { - assertionsParser = JavaParser.fromJavaVersion() - .classpathFromResources(ctx, "assertj-core-3.24"); - } - return assertionsParser; - } + return Preconditions.check(new UsesType<>("org.testng.Assert", false), new JavaIsoVisitor() { + private final MethodMatcher TESTNG_ASSERT_NULL_MATCHER = new MethodMatcher("org.testng.Assert" + " assertNull(..)"); + + @Override + public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { + if (!TESTNG_ASSERT_NULL_MATCHER.matches(method)) { + return method; + } + + List args = method.getArguments(); + Expression actual = args.get(0); + + if (args.size() == 1) { + method = JavaTemplate.builder("assertThat(#{any()}).isNull();") + .staticImports("org.assertj.core.api.Assertions.assertThat") + .javaParser(JavaParser.fromJavaVersion() + .classpathFromResources(ctx, "assertj-core-3.24")) + .build() + .apply( + getCursor(), + method.getCoordinates().replace(), + actual + ); + } else { + 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();"); + + method = template + .staticImports("org.assertj.core.api.Assertions.assertThat") + .javaParser(JavaParser.fromJavaVersion() + .classpathFromResources(ctx, "assertj-core-3.24")) + .build() + .apply( + getCursor(), + method.getCoordinates().replace(), + actual, + message + ); + } + + // Make sure there is a static import for "org.assertj.core.api.Assertions.assertThat" (even if not referenced) + maybeAddImport("org.assertj.core.api.Assertions", "assertThat", false); + + // Remove import for "org.testng.Assert" if no longer used. + maybeRemoveImport("org.testng.Assert"); - private static final MethodMatcher TESTNG_ASSERT_NULL_MATCHER = new MethodMatcher("org.testng.Assert" + " assertNull(..)"); - - @Override - public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { - if (!TESTNG_ASSERT_NULL_MATCHER.matches(method)) { return method; } - - List args = method.getArguments(); - Expression actual = args.get(0); - - if (args.size() == 1) { - method = JavaTemplate.builder("assertThat(#{any()}).isNull();") - .staticImports("org.assertj.core.api.Assertions.assertThat") - .javaParser(assertionsParser(ctx)) - .build() - .apply( - getCursor(), - method.getCoordinates().replace(), - actual - ); - } else { - 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();"); - - method = template - .staticImports("org.assertj.core.api.Assertions.assertThat") - .javaParser(assertionsParser(ctx)) - .build() - .apply( - getCursor(), - method.getCoordinates().replace(), - actual, - message - ); - } - - // Make sure there is a static import for "org.assertj.core.api.Assertions.assertThat" (even if not referenced) - maybeAddImport("org.assertj.core.api.Assertions", "assertThat", false); - - // Remove import for "org.testng.Assert" if no longer used. - maybeRemoveImport("org.testng.Assert"); - - return method; - } + }); } + } diff --git a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertTrueToAssertThat.java b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertTrueToAssertThat.java index 464ddf620..bfff11fff 100644 --- a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertTrueToAssertThat.java +++ b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertTrueToAssertThat.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 the original author or authors. + * 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. @@ -30,8 +30,7 @@ import java.util.List; -public class TestNgAssertTrueToAssertThat - extends Recipe { +public class TestNgAssertTrueToAssertThat extends Recipe { @Override public String getDisplayName() { return "TestNG `assertTrue` to AssertJ"; @@ -44,67 +43,46 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { - return Preconditions.check(new UsesType<>("org.testng.Assert", false), new AssertTrueToAssertThatVisitor()); - } - - public static class AssertTrueToAssertThatVisitor extends JavaIsoVisitor { - private JavaParser.Builder assertionsParser; - - private JavaParser.Builder assertionsParser(ExecutionContext ctx) { - if (assertionsParser == null) { - assertionsParser = JavaParser.fromJavaVersion() - .classpathFromResources(ctx, "assertj-core-3.24"); - } - return assertionsParser; - } + return Preconditions.check(new UsesType<>("org.testng.Assert", false), new JavaIsoVisitor() { + private final MethodMatcher TESTNG_ASSERT_TRUE = new MethodMatcher("org.testng.Assert" + " assertTrue(boolean, ..)"); + + @Override + public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { + if (!TESTNG_ASSERT_TRUE.matches(method)) { + return method; + } + + List args = method.getArguments(); + Expression actual = args.get(0); + + if (args.size() == 1) { + method = JavaTemplate.builder("assertThat(#{any(boolean)}).isTrue();") + .staticImports("org.assertj.core.api.Assertions.assertThat") + .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3.24")) + .build() + .apply(getCursor(), method.getCoordinates().replace(), actual); + } else { + 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();"); + + method = template + .staticImports("org.assertj.core.api.Assertions.assertThat") + .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3.24")) + .build() + .apply(getCursor(), method.getCoordinates().replace(), actual, message); + } + + //Make sure there is a static import for "org.assertj.core.api.Assertions.assertThat" (even if not referenced) + maybeAddImport("org.assertj.core.api.Assertions", "assertThat", false); + + // Remove import for "org.testng.Assert" if no longer used. + maybeRemoveImport("org.testng.Assert"); - private static final MethodMatcher TESTNG_ASSERT_TRUE = new MethodMatcher("org.testng.Assert" + " assertTrue(boolean, ..)"); - - @Override - public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { - if (!TESTNG_ASSERT_TRUE.matches(method)) { return method; } - - List args = method.getArguments(); - Expression actual = args.get(0); - - if (args.size() == 1) { - method = JavaTemplate.builder("assertThat(#{any(boolean)}).isTrue();") - .staticImports("org.assertj.core.api.Assertions.assertThat") - .javaParser(assertionsParser(ctx)) - .build() - .apply( - getCursor(), - method.getCoordinates().replace(), - actual - ); - } else { - 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();"); - - method = template - .staticImports("org.assertj.core.api.Assertions.assertThat") - .javaParser(assertionsParser(ctx)) - .build() - .apply( - getCursor(), - method.getCoordinates().replace(), - actual, - message - ); - } - - //Make sure there is a static import for "org.assertj.core.api.Assertions.assertThat" (even if not referenced) - maybeAddImport("org.assertj.core.api.Assertions", "assertThat", false); - - // Remove import for "org.testng.Assert" if no longer used. - maybeRemoveImport("org.testng.Assert"); - - return method; - } + }); } } diff --git a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgFailToAssertJFail.java b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgFailToAssertJFail.java index d3d5fdfdd..3c2b6f5da 100644 --- a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgFailToAssertJFail.java +++ b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgFailToAssertJFail.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 the original author or authors. + * 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. @@ -27,11 +27,10 @@ import java.util.List; -public class TestNgFailToAssertJFail - extends Recipe { +public class TestNgFailToAssertJFail extends Recipe { @Override public String getDisplayName() { - return "TestNG fail to AssertJ"; + return "TestNG `fail` to AssertJ"; } @Override @@ -41,118 +40,98 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { - return Preconditions.check(new UsesType<>("org.testng.Assert", false), new TestNgFailToAssertJFailVisitor()); - } - - public static class TestNgFailToAssertJFailVisitor extends JavaIsoVisitor { - private JavaParser.Builder assertionsParser; - - private JavaParser.Builder assertionsParser(ExecutionContext ctx) { - if (assertionsParser == null) { - assertionsParser = JavaParser.fromJavaVersion() - .classpathFromResources(ctx, "assertj-core-3.24"); - } - return assertionsParser; - } + return Preconditions.check(new UsesType<>("org.testng.Assert", false), new JavaIsoVisitor() { + private final MethodMatcher TESTNG_FAIL_MATCHER = new MethodMatcher("org.testng.Assert" + " fail(..)"); - private static final MethodMatcher TESTNG_FAIL_MATCHER = new MethodMatcher("org.testng.Assert" + " fail(..)"); - - @Override - public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { - J.MethodInvocation m = method; - - if (!TESTNG_FAIL_MATCHER.matches(m)) { - return m; - } + @Override + public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { + J.MethodInvocation m = method; - List args = m.getArguments(); + if (!TESTNG_FAIL_MATCHER.matches(m)) { + return m; + } - if (args.size() == 1) { - // fail(), fail(String), fail(Supplier), fail(Throwable) - if (args.get(0) instanceof J.Empty) { - m = JavaTemplate.builder("org.assertj.core.api.Assertions.fail(\"\");") - .javaParser(assertionsParser(ctx)) - .build() - .apply(getCursor(), m.getCoordinates().replace()); - } else if (args.get(0) instanceof J.Literal || - TypeUtils.isAssignableTo("java.lang.String", args.get(0).getType())) { - m = JavaTemplate.builder("org.assertj.core.api.Assertions.fail(#{any()});") - .javaParser(assertionsParser(ctx)) - .build() - .apply( - getCursor(), - m.getCoordinates().replace(), - args.get(0) - ); + List args = m.getArguments(); + + if (args.size() == 1) { + // fail(), fail(String), fail(Supplier), fail(Throwable) + if (args.get(0) instanceof J.Empty) { + m = JavaTemplate.builder("org.assertj.core.api.Assertions.fail(\"\");") + .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3.24")) + .build() + .apply(getCursor(), m.getCoordinates().replace()); + } else if (args.get(0) instanceof J.Literal || + TypeUtils.isAssignableTo("java.lang.String", args.get(0).getType())) { + m = JavaTemplate.builder("org.assertj.core.api.Assertions.fail(#{any()});") + .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3.24")) + .build() + .apply(getCursor(), m.getCoordinates().replace(), args.get(0)); + } else { + m = JavaTemplate.builder("org.assertj.core.api.Assertions.fail(\"\", #{any()});") + .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3.24")) + .build() + .apply(getCursor(), m.getCoordinates().replace(), args.get(0)); + } } else { - m = JavaTemplate.builder("org.assertj.core.api.Assertions.fail(\"\", #{any()});") - .javaParser(assertionsParser(ctx)) + // fail(String, Throwable) + StringBuilder templateBuilder = new StringBuilder("org.assertj.core.api.Assertions.fail("); + for (int i = 0; i < args.size(); i++) { + templateBuilder.append("#{any()}"); + if (i < args.size() - 1) { + templateBuilder.append(", "); + } + } + templateBuilder.append(");"); + + m = JavaTemplate.builder(templateBuilder.toString()) + .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3.24")) .build() .apply( getCursor(), m.getCoordinates().replace(), - args.get(0) + args.toArray() ); } - } else { - // fail(String, Throwable) - StringBuilder templateBuilder = new StringBuilder("org.assertj.core.api.Assertions.fail("); - for (int i = 0; i < args.size(); i++) { - templateBuilder.append("#{any()}"); - if (i < args.size() - 1) { - templateBuilder.append(", "); - } - } - templateBuilder.append(");"); - m = JavaTemplate.builder(templateBuilder.toString()) - .javaParser(assertionsParser(ctx)) - .build() - .apply( - getCursor(), - m.getCoordinates().replace(), - args.toArray() - ); + doAfterVisit(new RemoveUnusedImports().getVisitor()); + doAfterVisit(new UnqualifiedMethodInvocations()); + return m; } + }); + } - doAfterVisit(new RemoveUnusedImports().getVisitor()); - doAfterVisit(new UnqualifiedMethodInvocations()); - return m; - } - - private static class UnqualifiedMethodInvocations extends JavaIsoVisitor { - private static final MethodMatcher ASSERTJ_FAIL_MATCHER = new MethodMatcher("org.assertj.core.api.Assertions" + " fail(..)"); + private static class UnqualifiedMethodInvocations extends JavaIsoVisitor { + private static final MethodMatcher ASSERTJ_FAIL_MATCHER = new MethodMatcher("org.assertj.core.api.Assertions" + " fail(..)"); - @Override - public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { - if (!ASSERTJ_FAIL_MATCHER.matches(method)) { - return method; - } + @Override + public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { + if (!ASSERTJ_FAIL_MATCHER.matches(method)) { + return method; + } - StringBuilder templateBuilder = new StringBuilder("fail("); - List arguments = method.getArguments(); - for (int i = 0; i < arguments.size(); i++) { - templateBuilder.append("#{any()}"); - if (i < arguments.size() - 1) { - templateBuilder.append(", "); - } + StringBuilder templateBuilder = new StringBuilder("fail("); + List arguments = method.getArguments(); + for (int i = 0; i < arguments.size(); i++) { + templateBuilder.append("#{any()}"); + if (i < arguments.size() - 1) { + templateBuilder.append(", "); } - templateBuilder.append(");"); - - method = JavaTemplate.builder(templateBuilder.toString()) - .staticImports("org.assertj.core.api.Assertions" + ".fail") - .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3.24")) - .build() - .apply( - getCursor(), - method.getCoordinates().replace(), - arguments.toArray() - ); - //Make sure there is a static import for "org.assertj.core.api.Assertions.assertThat" (even if not referenced) - maybeAddImport("org.assertj.core.api.Assertions", "fail", false); - maybeRemoveImport("org.testng.Assert.fail"); - return super.visitMethodInvocation(method, ctx); } + templateBuilder.append(");"); + + method = JavaTemplate.builder(templateBuilder.toString()) + .staticImports("org.assertj.core.api.Assertions" + ".fail") + .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3.24")) + .build() + .apply( + getCursor(), + method.getCoordinates().replace(), + arguments.toArray() + ); + //Make sure there is a static import for "org.assertj.core.api.Assertions.assertThat" (even if not referenced) + maybeAddImport("org.assertj.core.api.Assertions", "fail", false); + maybeRemoveImport("org.testng.Assert.fail"); + return super.visitMethodInvocation(method, ctx); } } } From d7e6c0eef4e5e112e1545544cd2e7ed439272f00 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Tue, 10 Dec 2024 23:11:36 +0100 Subject: [PATCH 09/12] Use `UsesMethod` as precondition --- .../testng/TestNgAssertEqualsToAssertThat.java | 10 +++++----- .../testng/TestNgAssertFalseToAssertThat.java | 10 +++++----- .../testng/TestNgAssertNotEqualsToAssertThat.java | 10 +++++----- .../testng/TestNgAssertNotNullToAssertThat.java | 10 +++++----- .../assertj/testng/TestNgAssertNullToAssertThat.java | 10 +++++----- .../assertj/testng/TestNgAssertTrueToAssertThat.java | 10 +++++----- .../assertj/testng/TestNgFailToAssertJFail.java | 12 ++++++------ 7 files changed, 36 insertions(+), 36 deletions(-) diff --git a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertEqualsToAssertThat.java b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertEqualsToAssertThat.java index fd891a293..ed689d32d 100644 --- a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertEqualsToAssertThat.java +++ b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertEqualsToAssertThat.java @@ -23,7 +23,7 @@ import org.openrewrite.java.JavaParser; import org.openrewrite.java.JavaTemplate; import org.openrewrite.java.MethodMatcher; -import org.openrewrite.java.search.UsesType; +import org.openrewrite.java.search.UsesMethod; import org.openrewrite.java.tree.Expression; import org.openrewrite.java.tree.J; import org.openrewrite.java.tree.JavaType; @@ -43,14 +43,14 @@ public String getDescription() { return "Convert TestNG-style `assertEquals()` to AssertJ's `assertThat().isEqualTo()`."; } + private final MethodMatcher TESTNG_ASSERT_METHOD = new MethodMatcher("org.testng.Assert assertEquals(..)"); + @Override public TreeVisitor getVisitor() { - return Preconditions.check(new UsesType<>("org.testng.Assert", false), new JavaIsoVisitor() { - private final MethodMatcher TESTNG_ASSERT_EQUALS = new MethodMatcher("org.testng.Assert" + " assertEquals(..)"); - + return Preconditions.check(new UsesMethod<>(TESTNG_ASSERT_METHOD), new JavaIsoVisitor() { @Override public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { - if (!TESTNG_ASSERT_EQUALS.matches(method)) { + if (!TESTNG_ASSERT_METHOD.matches(method)) { return method; } diff --git a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertFalseToAssertThat.java b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertFalseToAssertThat.java index 648a28c03..65a1afc8e 100644 --- a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertFalseToAssertThat.java +++ b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertFalseToAssertThat.java @@ -23,7 +23,7 @@ import org.openrewrite.java.JavaParser; import org.openrewrite.java.JavaTemplate; import org.openrewrite.java.MethodMatcher; -import org.openrewrite.java.search.UsesType; +import org.openrewrite.java.search.UsesMethod; import org.openrewrite.java.tree.Expression; import org.openrewrite.java.tree.J; import org.openrewrite.java.tree.TypeUtils; @@ -42,14 +42,14 @@ public String getDescription() { return "Convert TestNG-style `assertFalse()` to AssertJ's `assertThat().isFalse()`."; } + private final MethodMatcher TESTNG_ASSERT_METHOD = new MethodMatcher("org.testng.Assert assertFalse(boolean, ..)"); + @Override public TreeVisitor getVisitor() { - return Preconditions.check(new UsesType<>("org.testng.Assert", false), new JavaIsoVisitor() { - private final MethodMatcher TESTNG_ASSERT_FALSE = new MethodMatcher("org.testng.Assert" + " assertFalse(boolean, ..)"); - + return Preconditions.check(new UsesMethod<>(TESTNG_ASSERT_METHOD), new JavaIsoVisitor() { @Override public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { - if (!TESTNG_ASSERT_FALSE.matches(method)) { + if (!TESTNG_ASSERT_METHOD.matches(method)) { return method; } diff --git a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNotEqualsToAssertThat.java b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNotEqualsToAssertThat.java index 5d351ab9b..41f183a78 100644 --- a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNotEqualsToAssertThat.java +++ b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNotEqualsToAssertThat.java @@ -23,7 +23,7 @@ import org.openrewrite.java.JavaParser; import org.openrewrite.java.JavaTemplate; import org.openrewrite.java.MethodMatcher; -import org.openrewrite.java.search.UsesType; +import org.openrewrite.java.search.UsesMethod; import org.openrewrite.java.tree.Expression; import org.openrewrite.java.tree.J; import org.openrewrite.java.tree.JavaType; @@ -43,14 +43,14 @@ public String getDescription() { return "Convert TestNG-style `assertNotEquals()` to AssertJ's `assertThat().isNotEqualTo()`."; } + private final MethodMatcher TESTNG_ASSERT_METHOD = new MethodMatcher("org.testng.Assert assertNotEquals(..)"); + @Override public TreeVisitor getVisitor() { - return Preconditions.check(new UsesType<>("org.testng.Assert", false), new JavaIsoVisitor() { - private final MethodMatcher TESTNG_ASSERT_EQUALS = new MethodMatcher("org.testng.Assert" + " assertNotEquals(..)"); - + return Preconditions.check(new UsesMethod<>(TESTNG_ASSERT_METHOD), new JavaIsoVisitor() { @Override public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { - if (!TESTNG_ASSERT_EQUALS.matches(method)) { + if (!TESTNG_ASSERT_METHOD.matches(method)) { return method; } diff --git a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNotNullToAssertThat.java b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNotNullToAssertThat.java index 563ec6145..1d792f3bd 100644 --- a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNotNullToAssertThat.java +++ b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNotNullToAssertThat.java @@ -23,7 +23,7 @@ import org.openrewrite.java.JavaParser; import org.openrewrite.java.JavaTemplate; import org.openrewrite.java.MethodMatcher; -import org.openrewrite.java.search.UsesType; +import org.openrewrite.java.search.UsesMethod; import org.openrewrite.java.tree.Expression; import org.openrewrite.java.tree.J; import org.openrewrite.java.tree.TypeUtils; @@ -42,14 +42,14 @@ public String getDescription() { return "Convert TestNG-style `assertNotNull()` to AssertJ's `assertThat().isNotNull()`."; } + private final MethodMatcher TESTNG_ASSERT_METHOD = new MethodMatcher("org.testng.Assert assertNotNull(..)"); + @Override public TreeVisitor getVisitor() { - return Preconditions.check(new UsesType<>("org.testng.Assert", false), new JavaIsoVisitor() { - private final MethodMatcher TESTNG_ASSERT_NOT_NULL_MATCHER = new MethodMatcher("org.testng.Assert" + " assertNotNull(..)"); - + return Preconditions.check(new UsesMethod<>(TESTNG_ASSERT_METHOD), new JavaIsoVisitor() { @Override public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { - if (!TESTNG_ASSERT_NOT_NULL_MATCHER.matches(method)) { + if (!TESTNG_ASSERT_METHOD.matches(method)) { return method; } diff --git a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNullToAssertThat.java b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNullToAssertThat.java index 9624ce6ca..6a8ea00de 100644 --- a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNullToAssertThat.java +++ b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNullToAssertThat.java @@ -23,7 +23,7 @@ import org.openrewrite.java.JavaParser; import org.openrewrite.java.JavaTemplate; import org.openrewrite.java.MethodMatcher; -import org.openrewrite.java.search.UsesType; +import org.openrewrite.java.search.UsesMethod; import org.openrewrite.java.tree.Expression; import org.openrewrite.java.tree.J; import org.openrewrite.java.tree.TypeUtils; @@ -42,14 +42,14 @@ public String getDescription() { return "Convert TestNG-style `assertNull()` to AssertJ's `assertThat().isNull()`."; } + private final MethodMatcher TESTNG_ASSERT_METHOD = new MethodMatcher("org.testng.Assert assertNull(..)"); + @Override public TreeVisitor getVisitor() { - return Preconditions.check(new UsesType<>("org.testng.Assert", false), new JavaIsoVisitor() { - private final MethodMatcher TESTNG_ASSERT_NULL_MATCHER = new MethodMatcher("org.testng.Assert" + " assertNull(..)"); - + return Preconditions.check(new UsesMethod<>(TESTNG_ASSERT_METHOD), new JavaIsoVisitor() { @Override public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { - if (!TESTNG_ASSERT_NULL_MATCHER.matches(method)) { + if (!TESTNG_ASSERT_METHOD.matches(method)) { return method; } diff --git a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertTrueToAssertThat.java b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertTrueToAssertThat.java index bfff11fff..8f94f0f3a 100644 --- a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertTrueToAssertThat.java +++ b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertTrueToAssertThat.java @@ -23,7 +23,7 @@ import org.openrewrite.java.JavaParser; import org.openrewrite.java.JavaTemplate; import org.openrewrite.java.MethodMatcher; -import org.openrewrite.java.search.UsesType; +import org.openrewrite.java.search.UsesMethod; import org.openrewrite.java.tree.Expression; import org.openrewrite.java.tree.J; import org.openrewrite.java.tree.TypeUtils; @@ -41,14 +41,14 @@ public String getDescription() { return "Convert TestNG-style `assertTrue()` to AssertJ's `assertThat().isTrue()`."; } + private final MethodMatcher TESTNG_ASSERT_METHOD = new MethodMatcher("org.testng.Assert assertTrue(boolean, ..)"); + @Override public TreeVisitor getVisitor() { - return Preconditions.check(new UsesType<>("org.testng.Assert", false), new JavaIsoVisitor() { - private final MethodMatcher TESTNG_ASSERT_TRUE = new MethodMatcher("org.testng.Assert" + " assertTrue(boolean, ..)"); - + return Preconditions.check(new UsesMethod<>(TESTNG_ASSERT_METHOD), new JavaIsoVisitor() { @Override public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { - if (!TESTNG_ASSERT_TRUE.matches(method)) { + if (!TESTNG_ASSERT_METHOD.matches(method)) { return method; } diff --git a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgFailToAssertJFail.java b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgFailToAssertJFail.java index 3c2b6f5da..3e11f046d 100644 --- a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgFailToAssertJFail.java +++ b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgFailToAssertJFail.java @@ -20,7 +20,7 @@ import org.openrewrite.Recipe; import org.openrewrite.TreeVisitor; import org.openrewrite.java.*; -import org.openrewrite.java.search.UsesType; +import org.openrewrite.java.search.UsesMethod; import org.openrewrite.java.tree.Expression; import org.openrewrite.java.tree.J; import org.openrewrite.java.tree.TypeUtils; @@ -38,16 +38,16 @@ public String getDescription() { return "Convert TestNG-style `fail()` to AssertJ's `fail()`."; } + private final MethodMatcher TESTNG_ASSERT_METHOD = new MethodMatcher("org.testng.Assert fail(..)"); + @Override public TreeVisitor getVisitor() { - return Preconditions.check(new UsesType<>("org.testng.Assert", false), new JavaIsoVisitor() { - private final MethodMatcher TESTNG_FAIL_MATCHER = new MethodMatcher("org.testng.Assert" + " fail(..)"); - + return Preconditions.check(new UsesMethod<>(TESTNG_ASSERT_METHOD), new JavaIsoVisitor() { @Override public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { J.MethodInvocation m = method; - if (!TESTNG_FAIL_MATCHER.matches(m)) { + if (!TESTNG_ASSERT_METHOD.matches(m)) { return m; } @@ -101,7 +101,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu } private static class UnqualifiedMethodInvocations extends JavaIsoVisitor { - private static final MethodMatcher ASSERTJ_FAIL_MATCHER = new MethodMatcher("org.assertj.core.api.Assertions" + " fail(..)"); + private static final MethodMatcher ASSERTJ_FAIL_MATCHER = new MethodMatcher("org.assertj.core.api.Assertions fail(..)"); @Override public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { From 1b029d2a96925d6fea702434137e8ae2e0be3f4e Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Tue, 10 Dec 2024 23:24:38 +0100 Subject: [PATCH 10/12] Enable all custom recipes again --- .../testng/TestNgAssertEqualsToAssertThat.java | 2 +- .../testng/TestNgAssertFalseToAssertThat.java | 2 +- .../testng/TestNgAssertNotEqualsToAssertThat.java | 2 +- .../testng/TestNgAssertNotNullToAssertThat.java | 2 +- .../testng/TestNgAssertNullToAssertThat.java | 2 +- .../testng/TestNgAssertTrueToAssertThat.java | 2 +- .../assertj/testng/TestNgFailToAssertJFail.java | 2 +- src/main/resources/META-INF/rewrite/testng.yml | 14 +++++++------- .../java/testing/testng/TestNgToAssertJTest.java | 1 - 9 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertEqualsToAssertThat.java b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertEqualsToAssertThat.java index ed689d32d..777d1821c 100644 --- a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertEqualsToAssertThat.java +++ b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertEqualsToAssertThat.java @@ -43,7 +43,7 @@ public String getDescription() { return "Convert TestNG-style `assertEquals()` to AssertJ's `assertThat().isEqualTo()`."; } - private final MethodMatcher TESTNG_ASSERT_METHOD = new MethodMatcher("org.testng.Assert assertEquals(..)"); + private static final MethodMatcher TESTNG_ASSERT_METHOD = new MethodMatcher("org.testng.Assert assertEquals(..)"); @Override public TreeVisitor getVisitor() { diff --git a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertFalseToAssertThat.java b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertFalseToAssertThat.java index 65a1afc8e..166cbbb2d 100644 --- a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertFalseToAssertThat.java +++ b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertFalseToAssertThat.java @@ -42,7 +42,7 @@ public String getDescription() { return "Convert TestNG-style `assertFalse()` to AssertJ's `assertThat().isFalse()`."; } - private final MethodMatcher TESTNG_ASSERT_METHOD = new MethodMatcher("org.testng.Assert assertFalse(boolean, ..)"); + private static final MethodMatcher TESTNG_ASSERT_METHOD = new MethodMatcher("org.testng.Assert assertFalse(boolean, ..)"); @Override public TreeVisitor getVisitor() { diff --git a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNotEqualsToAssertThat.java b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNotEqualsToAssertThat.java index 41f183a78..ed1d9b175 100644 --- a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNotEqualsToAssertThat.java +++ b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNotEqualsToAssertThat.java @@ -43,7 +43,7 @@ public String getDescription() { return "Convert TestNG-style `assertNotEquals()` to AssertJ's `assertThat().isNotEqualTo()`."; } - private final MethodMatcher TESTNG_ASSERT_METHOD = new MethodMatcher("org.testng.Assert assertNotEquals(..)"); + private static final MethodMatcher TESTNG_ASSERT_METHOD = new MethodMatcher("org.testng.Assert assertNotEquals(..)"); @Override public TreeVisitor getVisitor() { diff --git a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNotNullToAssertThat.java b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNotNullToAssertThat.java index 1d792f3bd..079787369 100644 --- a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNotNullToAssertThat.java +++ b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNotNullToAssertThat.java @@ -42,7 +42,7 @@ public String getDescription() { return "Convert TestNG-style `assertNotNull()` to AssertJ's `assertThat().isNotNull()`."; } - private final MethodMatcher TESTNG_ASSERT_METHOD = new MethodMatcher("org.testng.Assert assertNotNull(..)"); + private static final MethodMatcher TESTNG_ASSERT_METHOD = new MethodMatcher("org.testng.Assert assertNotNull(..)"); @Override public TreeVisitor getVisitor() { diff --git a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNullToAssertThat.java b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNullToAssertThat.java index 6a8ea00de..dd402efd5 100644 --- a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNullToAssertThat.java +++ b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNullToAssertThat.java @@ -42,7 +42,7 @@ public String getDescription() { return "Convert TestNG-style `assertNull()` to AssertJ's `assertThat().isNull()`."; } - private final MethodMatcher TESTNG_ASSERT_METHOD = new MethodMatcher("org.testng.Assert assertNull(..)"); + private static final MethodMatcher TESTNG_ASSERT_METHOD = new MethodMatcher("org.testng.Assert assertNull(..)"); @Override public TreeVisitor getVisitor() { diff --git a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertTrueToAssertThat.java b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertTrueToAssertThat.java index 8f94f0f3a..f81729ac5 100644 --- a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertTrueToAssertThat.java +++ b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertTrueToAssertThat.java @@ -41,7 +41,7 @@ public String getDescription() { return "Convert TestNG-style `assertTrue()` to AssertJ's `assertThat().isTrue()`."; } - private final MethodMatcher TESTNG_ASSERT_METHOD = new MethodMatcher("org.testng.Assert assertTrue(boolean, ..)"); + private static final MethodMatcher TESTNG_ASSERT_METHOD = new MethodMatcher("org.testng.Assert assertTrue(boolean, ..)"); @Override public TreeVisitor getVisitor() { diff --git a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgFailToAssertJFail.java b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgFailToAssertJFail.java index 3e11f046d..72c8f7f71 100644 --- a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgFailToAssertJFail.java +++ b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgFailToAssertJFail.java @@ -38,7 +38,7 @@ public String getDescription() { return "Convert TestNG-style `fail()` to AssertJ's `fail()`."; } - private final MethodMatcher TESTNG_ASSERT_METHOD = new MethodMatcher("org.testng.Assert fail(..)"); + private static final MethodMatcher TESTNG_ASSERT_METHOD = new MethodMatcher("org.testng.Assert fail(..)"); @Override public TreeVisitor getVisitor() { diff --git a/src/main/resources/META-INF/rewrite/testng.yml b/src/main/resources/META-INF/rewrite/testng.yml index b7c9818e4..827486dfe 100644 --- a/src/main/resources/META-INF/rewrite/testng.yml +++ b/src/main/resources/META-INF/rewrite/testng.yml @@ -30,10 +30,10 @@ recipeList: onlyIfUsing: org.testng.* acceptTransitive: true - tech.picnic.errorprone.refasterrules.TestNGToAssertJRulesRecipes -# - org.openrewrite.java.testing.assertj.testng.TestNgAssertEqualsToAssertThat -# - org.openrewrite.java.testing.assertj.testng.TestNgAssertFalseToAssertThat -# - org.openrewrite.java.testing.assertj.testng.TestNgAssertNotEqualsToAssertThat -# - org.openrewrite.java.testing.assertj.testng.TestNgAssertNotNullToAssertThat -# - org.openrewrite.java.testing.assertj.testng.TestNgAssertNullToAssertThat -# - org.openrewrite.java.testing.assertj.testng.TestNgAssertTrueToAssertThat -# - org.openrewrite.java.testing.assertj.testng.TestNgFailToAssertJFail + - org.openrewrite.java.testing.assertj.testng.TestNgAssertEqualsToAssertThat + - org.openrewrite.java.testing.assertj.testng.TestNgAssertFalseToAssertThat + - org.openrewrite.java.testing.assertj.testng.TestNgAssertNotEqualsToAssertThat + - org.openrewrite.java.testing.assertj.testng.TestNgAssertNotNullToAssertThat + - org.openrewrite.java.testing.assertj.testng.TestNgAssertNullToAssertThat + - org.openrewrite.java.testing.assertj.testng.TestNgAssertTrueToAssertThat + - org.openrewrite.java.testing.assertj.testng.TestNgFailToAssertJFail diff --git a/src/test/java/org/openrewrite/java/testing/testng/TestNgToAssertJTest.java b/src/test/java/org/openrewrite/java/testing/testng/TestNgToAssertJTest.java index 47ef9c5e8..740cbcfa7 100644 --- a/src/test/java/org/openrewrite/java/testing/testng/TestNgToAssertJTest.java +++ b/src/test/java/org/openrewrite/java/testing/testng/TestNgToAssertJTest.java @@ -17,7 +17,6 @@ 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; From 7f8bafff59d24a4b6090399a0275c3552050130d Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Tue, 10 Dec 2024 23:45:05 +0100 Subject: [PATCH 11/12] Drop recipes also picked up from `TestNGToAssertJRulesRecipes` --- .../testng/TestNgAssertFalseToAssertThat.java | 91 ------------ .../TestNgAssertNotNullToAssertThat.java | 102 ------------- .../testng/TestNgAssertNullToAssertThat.java | 101 ------------- .../testng/TestNgAssertTrueToAssertThat.java | 88 ----------- .../testng/TestNgFailToAssertJFail.java | 137 ------------------ .../TestNgAssertEqualsToAssertThat.java | 2 +- .../TestNgAssertNotEqualsToAssertThat.java | 2 +- .../resources/META-INF/rewrite/testng.yml | 9 +- .../testing/testng/TestNgToAssertJTest.java | 72 +++++++++ 9 files changed, 76 insertions(+), 528 deletions(-) delete mode 100644 src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertFalseToAssertThat.java delete mode 100644 src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNotNullToAssertThat.java delete mode 100644 src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNullToAssertThat.java delete mode 100644 src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertTrueToAssertThat.java delete mode 100644 src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgFailToAssertJFail.java rename src/main/java/org/openrewrite/java/testing/{assertj => }/testng/TestNgAssertEqualsToAssertThat.java (99%) rename src/main/java/org/openrewrite/java/testing/{assertj => }/testng/TestNgAssertNotEqualsToAssertThat.java (99%) diff --git a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertFalseToAssertThat.java b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertFalseToAssertThat.java deleted file mode 100644 index 166cbbb2d..000000000 --- a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertFalseToAssertThat.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * 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.testng; - -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.TypeUtils; - -import java.util.List; - -public class TestNgAssertFalseToAssertThat extends Recipe { - - @Override - public String getDisplayName() { - return "TestNG `assertFalse` to AssertJ"; - } - - @Override - public String getDescription() { - return "Convert TestNG-style `assertFalse()` to AssertJ's `assertThat().isFalse()`."; - } - - private static final MethodMatcher TESTNG_ASSERT_METHOD = new MethodMatcher("org.testng.Assert assertFalse(boolean, ..)"); - - @Override - public TreeVisitor getVisitor() { - return Preconditions.check(new UsesMethod<>(TESTNG_ASSERT_METHOD), new JavaIsoVisitor() { - @Override - public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { - if (!TESTNG_ASSERT_METHOD.matches(method)) { - return method; - } - - List args = method.getArguments(); - Expression actual = args.get(0); - - if (args.size() == 1) { - method = JavaTemplate.builder("assertThat(#{any(boolean)}).isFalse();") - .staticImports("org.assertj.core.api.Assertions.assertThat") - .javaParser(JavaParser.fromJavaVersion() - .classpathFromResources(ctx, "assertj-core-3.24")) - .build() - .apply(getCursor(), method.getCoordinates().replace(), actual); - } else { - 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();"); - - method = template - .staticImports("org.assertj.core.api.Assertions.assertThat") - .javaParser(JavaParser.fromJavaVersion() - .classpathFromResources(ctx, "assertj-core-3.24")) - .build() - .apply(getCursor(), method.getCoordinates().replace(), actual, message); - } - - //Make sure there is a static import for "org.assertj.core.api.Assertions.assertThat" (even if not referenced) - maybeAddImport("org.assertj.core.api.Assertions", "assertThat", false); - - // Remove import for "org.testng.Assert" if no longer used. - maybeRemoveImport("org.testng.Assert"); - - return method; - } - }); - } - -} diff --git a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNotNullToAssertThat.java b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNotNullToAssertThat.java deleted file mode 100644 index 079787369..000000000 --- a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNotNullToAssertThat.java +++ /dev/null @@ -1,102 +0,0 @@ -/* - * 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.testng; - -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.TypeUtils; - -import java.util.List; - -public class TestNgAssertNotNullToAssertThat extends Recipe { - - @Override - public String getDisplayName() { - return "TestNG `assertNotNull` to AssertJ"; - } - - @Override - public String getDescription() { - return "Convert TestNG-style `assertNotNull()` to AssertJ's `assertThat().isNotNull()`."; - } - - private static final MethodMatcher TESTNG_ASSERT_METHOD = new MethodMatcher("org.testng.Assert assertNotNull(..)"); - - @Override - public TreeVisitor getVisitor() { - return Preconditions.check(new UsesMethod<>(TESTNG_ASSERT_METHOD), new JavaIsoVisitor() { - @Override - public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { - if (!TESTNG_ASSERT_METHOD.matches(method)) { - return method; - } - - List args = method.getArguments(); - Expression actual = args.get(0); - - if (args.size() == 1) { - method = JavaTemplate.builder("assertThat(#{any()}).isNotNull();") - .staticImports("org.assertj.core.api.Assertions.assertThat") - .javaParser(JavaParser.fromJavaVersion() - .classpathFromResources(ctx, "assertj-core-3.24")) - .build() - .apply( - getCursor(), - method.getCoordinates().replace(), - actual - ); - - } else { - 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();"); - - method = template - .staticImports("org.assertj.core.api.Assertions.assertThat") - .javaParser(JavaParser.fromJavaVersion() - .classpathFromResources(ctx, "assertj-core-3.24")) - .build() - .apply( - getCursor(), - method.getCoordinates().replace(), - actual, - message - ); - } - - //Make sure there is a static import for "org.assertj.core.api.Assertions.assertThat" (even if not referenced) - maybeAddImport("org.assertj.core.api.Assertions", "assertThat", false); - - //And if there are no longer references to the TestNG assertions class, we can remove the import. - maybeRemoveImport("org.testng.Assert"); - - return method; - } - }); - } - -} diff --git a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNullToAssertThat.java b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNullToAssertThat.java deleted file mode 100644 index dd402efd5..000000000 --- a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNullToAssertThat.java +++ /dev/null @@ -1,101 +0,0 @@ -/* - * 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.testng; - -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.TypeUtils; - -import java.util.List; - -public class TestNgAssertNullToAssertThat extends Recipe { - - @Override - public String getDisplayName() { - return "TestNG `assertNull` to AssertJ"; - } - - @Override - public String getDescription() { - return "Convert TestNG-style `assertNull()` to AssertJ's `assertThat().isNull()`."; - } - - private static final MethodMatcher TESTNG_ASSERT_METHOD = new MethodMatcher("org.testng.Assert assertNull(..)"); - - @Override - public TreeVisitor getVisitor() { - return Preconditions.check(new UsesMethod<>(TESTNG_ASSERT_METHOD), new JavaIsoVisitor() { - @Override - public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { - if (!TESTNG_ASSERT_METHOD.matches(method)) { - return method; - } - - List args = method.getArguments(); - Expression actual = args.get(0); - - if (args.size() == 1) { - method = JavaTemplate.builder("assertThat(#{any()}).isNull();") - .staticImports("org.assertj.core.api.Assertions.assertThat") - .javaParser(JavaParser.fromJavaVersion() - .classpathFromResources(ctx, "assertj-core-3.24")) - .build() - .apply( - getCursor(), - method.getCoordinates().replace(), - actual - ); - } else { - 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();"); - - method = template - .staticImports("org.assertj.core.api.Assertions.assertThat") - .javaParser(JavaParser.fromJavaVersion() - .classpathFromResources(ctx, "assertj-core-3.24")) - .build() - .apply( - getCursor(), - method.getCoordinates().replace(), - actual, - message - ); - } - - // Make sure there is a static import for "org.assertj.core.api.Assertions.assertThat" (even if not referenced) - maybeAddImport("org.assertj.core.api.Assertions", "assertThat", false); - - // Remove import for "org.testng.Assert" if no longer used. - maybeRemoveImport("org.testng.Assert"); - - return method; - } - }); - } - -} diff --git a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertTrueToAssertThat.java b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertTrueToAssertThat.java deleted file mode 100644 index f81729ac5..000000000 --- a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertTrueToAssertThat.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * 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.testng; - -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.TypeUtils; - -import java.util.List; - -public class TestNgAssertTrueToAssertThat extends Recipe { - @Override - public String getDisplayName() { - return "TestNG `assertTrue` to AssertJ"; - } - - @Override - public String getDescription() { - return "Convert TestNG-style `assertTrue()` to AssertJ's `assertThat().isTrue()`."; - } - - private static final MethodMatcher TESTNG_ASSERT_METHOD = new MethodMatcher("org.testng.Assert assertTrue(boolean, ..)"); - - @Override - public TreeVisitor getVisitor() { - return Preconditions.check(new UsesMethod<>(TESTNG_ASSERT_METHOD), new JavaIsoVisitor() { - @Override - public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { - if (!TESTNG_ASSERT_METHOD.matches(method)) { - return method; - } - - List args = method.getArguments(); - Expression actual = args.get(0); - - if (args.size() == 1) { - method = JavaTemplate.builder("assertThat(#{any(boolean)}).isTrue();") - .staticImports("org.assertj.core.api.Assertions.assertThat") - .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3.24")) - .build() - .apply(getCursor(), method.getCoordinates().replace(), actual); - } else { - 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();"); - - method = template - .staticImports("org.assertj.core.api.Assertions.assertThat") - .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3.24")) - .build() - .apply(getCursor(), method.getCoordinates().replace(), actual, message); - } - - //Make sure there is a static import for "org.assertj.core.api.Assertions.assertThat" (even if not referenced) - maybeAddImport("org.assertj.core.api.Assertions", "assertThat", false); - - // Remove import for "org.testng.Assert" if no longer used. - maybeRemoveImport("org.testng.Assert"); - - return method; - } - }); - } -} diff --git a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgFailToAssertJFail.java b/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgFailToAssertJFail.java deleted file mode 100644 index 72c8f7f71..000000000 --- a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgFailToAssertJFail.java +++ /dev/null @@ -1,137 +0,0 @@ -/* - * 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.testng; - -import org.openrewrite.ExecutionContext; -import org.openrewrite.Preconditions; -import org.openrewrite.Recipe; -import org.openrewrite.TreeVisitor; -import org.openrewrite.java.*; -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; - -public class TestNgFailToAssertJFail extends Recipe { - @Override - public String getDisplayName() { - return "TestNG `fail` to AssertJ"; - } - - @Override - public String getDescription() { - return "Convert TestNG-style `fail()` to AssertJ's `fail()`."; - } - - private static final MethodMatcher TESTNG_ASSERT_METHOD = new MethodMatcher("org.testng.Assert fail(..)"); - - @Override - public TreeVisitor getVisitor() { - return Preconditions.check(new UsesMethod<>(TESTNG_ASSERT_METHOD), new JavaIsoVisitor() { - @Override - public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { - J.MethodInvocation m = method; - - if (!TESTNG_ASSERT_METHOD.matches(m)) { - return m; - } - - List args = m.getArguments(); - - if (args.size() == 1) { - // fail(), fail(String), fail(Supplier), fail(Throwable) - if (args.get(0) instanceof J.Empty) { - m = JavaTemplate.builder("org.assertj.core.api.Assertions.fail(\"\");") - .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3.24")) - .build() - .apply(getCursor(), m.getCoordinates().replace()); - } else if (args.get(0) instanceof J.Literal || - TypeUtils.isAssignableTo("java.lang.String", args.get(0).getType())) { - m = JavaTemplate.builder("org.assertj.core.api.Assertions.fail(#{any()});") - .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3.24")) - .build() - .apply(getCursor(), m.getCoordinates().replace(), args.get(0)); - } else { - m = JavaTemplate.builder("org.assertj.core.api.Assertions.fail(\"\", #{any()});") - .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3.24")) - .build() - .apply(getCursor(), m.getCoordinates().replace(), args.get(0)); - } - } else { - // fail(String, Throwable) - StringBuilder templateBuilder = new StringBuilder("org.assertj.core.api.Assertions.fail("); - for (int i = 0; i < args.size(); i++) { - templateBuilder.append("#{any()}"); - if (i < args.size() - 1) { - templateBuilder.append(", "); - } - } - templateBuilder.append(");"); - - m = JavaTemplate.builder(templateBuilder.toString()) - .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3.24")) - .build() - .apply( - getCursor(), - m.getCoordinates().replace(), - args.toArray() - ); - } - - doAfterVisit(new RemoveUnusedImports().getVisitor()); - doAfterVisit(new UnqualifiedMethodInvocations()); - return m; - } - }); - } - - private static class UnqualifiedMethodInvocations extends JavaIsoVisitor { - private static final MethodMatcher ASSERTJ_FAIL_MATCHER = new MethodMatcher("org.assertj.core.api.Assertions fail(..)"); - - @Override - public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { - if (!ASSERTJ_FAIL_MATCHER.matches(method)) { - return method; - } - - StringBuilder templateBuilder = new StringBuilder("fail("); - List arguments = method.getArguments(); - for (int i = 0; i < arguments.size(); i++) { - templateBuilder.append("#{any()}"); - if (i < arguments.size() - 1) { - templateBuilder.append(", "); - } - } - templateBuilder.append(");"); - - method = JavaTemplate.builder(templateBuilder.toString()) - .staticImports("org.assertj.core.api.Assertions" + ".fail") - .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3.24")) - .build() - .apply( - getCursor(), - method.getCoordinates().replace(), - arguments.toArray() - ); - //Make sure there is a static import for "org.assertj.core.api.Assertions.assertThat" (even if not referenced) - maybeAddImport("org.assertj.core.api.Assertions", "fail", false); - maybeRemoveImport("org.testng.Assert.fail"); - return super.visitMethodInvocation(method, ctx); - } - } -} diff --git a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertEqualsToAssertThat.java b/src/main/java/org/openrewrite/java/testing/testng/TestNgAssertEqualsToAssertThat.java similarity index 99% rename from src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertEqualsToAssertThat.java rename to src/main/java/org/openrewrite/java/testing/testng/TestNgAssertEqualsToAssertThat.java index 777d1821c..6924497ec 100644 --- a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertEqualsToAssertThat.java +++ b/src/main/java/org/openrewrite/java/testing/testng/TestNgAssertEqualsToAssertThat.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.openrewrite.java.testing.assertj.testng; +package org.openrewrite.java.testing.testng; import org.openrewrite.ExecutionContext; import org.openrewrite.Preconditions; diff --git a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNotEqualsToAssertThat.java b/src/main/java/org/openrewrite/java/testing/testng/TestNgAssertNotEqualsToAssertThat.java similarity index 99% rename from src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNotEqualsToAssertThat.java rename to src/main/java/org/openrewrite/java/testing/testng/TestNgAssertNotEqualsToAssertThat.java index ed1d9b175..1245c3fb9 100644 --- a/src/main/java/org/openrewrite/java/testing/assertj/testng/TestNgAssertNotEqualsToAssertThat.java +++ b/src/main/java/org/openrewrite/java/testing/testng/TestNgAssertNotEqualsToAssertThat.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.openrewrite.java.testing.assertj.testng; +package org.openrewrite.java.testing.testng; import org.openrewrite.ExecutionContext; import org.openrewrite.Preconditions; diff --git a/src/main/resources/META-INF/rewrite/testng.yml b/src/main/resources/META-INF/rewrite/testng.yml index 827486dfe..8bab3e86a 100644 --- a/src/main/resources/META-INF/rewrite/testng.yml +++ b/src/main/resources/META-INF/rewrite/testng.yml @@ -30,10 +30,5 @@ recipeList: onlyIfUsing: org.testng.* acceptTransitive: true - tech.picnic.errorprone.refasterrules.TestNGToAssertJRulesRecipes - - org.openrewrite.java.testing.assertj.testng.TestNgAssertEqualsToAssertThat - - org.openrewrite.java.testing.assertj.testng.TestNgAssertFalseToAssertThat - - org.openrewrite.java.testing.assertj.testng.TestNgAssertNotEqualsToAssertThat - - org.openrewrite.java.testing.assertj.testng.TestNgAssertNotNullToAssertThat - - org.openrewrite.java.testing.assertj.testng.TestNgAssertNullToAssertThat - - org.openrewrite.java.testing.assertj.testng.TestNgAssertTrueToAssertThat - - org.openrewrite.java.testing.assertj.testng.TestNgFailToAssertJFail + - org.openrewrite.java.testing.testng.TestNgAssertEqualsToAssertThat + - org.openrewrite.java.testing.testng.TestNgAssertNotEqualsToAssertThat diff --git a/src/test/java/org/openrewrite/java/testing/testng/TestNgToAssertJTest.java b/src/test/java/org/openrewrite/java/testing/testng/TestNgToAssertJTest.java index 740cbcfa7..8e4f23762 100644 --- a/src/test/java/org/openrewrite/java/testing/testng/TestNgToAssertJTest.java +++ b/src/test/java/org/openrewrite/java/testing/testng/TestNgToAssertJTest.java @@ -102,4 +102,76 @@ void test() { ) ); } + + @Test + void assertNullAndNotNull() { + rewriteRun( + //language=java + java( + """ + import static org.testng.Assert.assertNotNull; + import static org.testng.Assert.assertNull; + + class Test { + void aaa(Object obj) { + assertNull(obj); + assertNull(obj, "foo"); + } + void bbb(Object obj) { + assertNotNull(obj); + assertNotNull(obj, "foo"); + } + } + """, + """ + import static org.assertj.core.api.Assertions.assertThat; + + class Test { + void aaa(Object obj) { + assertThat(obj).isNull(); + assertThat(obj).withFailMessage("foo").isNull(); + } + void bbb(Object obj) { + assertThat(obj).isNotNull(); + assertThat(obj).withFailMessage("foo").isNotNull(); + } + } + """ + ) + ); + } + + @Test + void assertEqualsAndNotEquals() { + rewriteRun( + //language=java + java( + """ + import static org.testng.Assert.assertEquals; + import static org.testng.Assert.assertNotEquals; + + class Test { + void aaa(Object obj) { + assertEquals(1, 1); + assertEquals(1, 1, "foo"); + assertNotEquals(1, 2); + assertNotEquals(1, 2, "foo"); + } + } + """, + """ + import static org.assertj.core.api.Assertions.assertThat; + + class Test { + void aaa(Object obj) { + assertThat(1).isEqualTo(1); + assertThat(1).as("foo").isEqualTo(1); + assertThat(1).isNotEqualTo(2); + assertThat(1).as("foo").isNotEqualTo(2); + } + } + """ + ) + ); + } } From 0afa5130fc396a88760ee12597478d7bf665f7fb Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Wed, 11 Dec 2024 00:03:17 +0100 Subject: [PATCH 12/12] Message is always a String --- .../testng/TestNgAssertEqualsToAssertThat.java | 10 ++-------- .../testng/TestNgAssertNotEqualsToAssertThat.java | 15 ++------------- 2 files changed, 4 insertions(+), 21 deletions(-) diff --git a/src/main/java/org/openrewrite/java/testing/testng/TestNgAssertEqualsToAssertThat.java b/src/main/java/org/openrewrite/java/testing/testng/TestNgAssertEqualsToAssertThat.java index 6924497ec..08ab531ed 100644 --- a/src/main/java/org/openrewrite/java/testing/testng/TestNgAssertEqualsToAssertThat.java +++ b/src/main/java/org/openrewrite/java/testing/testng/TestNgAssertEqualsToAssertThat.java @@ -73,10 +73,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu .apply(getCursor(), method.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)}).isEqualTo(#{any()});") : - JavaTemplate.builder("assertThat(#{any()}).as(#{any(java.util.function.Supplier)}).isEqualTo(#{any()});"); - return template + return JavaTemplate.builder("assertThat(#{any()}).as(#{any(String)}).isEqualTo(#{any()});") .staticImports("org.assertj.core.api.Assertions.assertThat") .imports("java.util.function.Supplier") .javaParser(JavaParser.fromJavaVersion() @@ -106,10 +103,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu //always add the import (even if not referenced) maybeAddImport("org.assertj.core.api.Assertions", "within", false); - 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(String)}).isCloseTo(#{any()}, within(#{any()}));") .staticImports("org.assertj.core.api.Assertions.assertThat", "org.assertj.core.api.Assertions.within") .imports("java.util.function.Supplier") .javaParser(JavaParser.fromJavaVersion() diff --git a/src/main/java/org/openrewrite/java/testing/testng/TestNgAssertNotEqualsToAssertThat.java b/src/main/java/org/openrewrite/java/testing/testng/TestNgAssertNotEqualsToAssertThat.java index 1245c3fb9..bbe8e218b 100644 --- a/src/main/java/org/openrewrite/java/testing/testng/TestNgAssertNotEqualsToAssertThat.java +++ b/src/main/java/org/openrewrite/java/testing/testng/TestNgAssertNotEqualsToAssertThat.java @@ -73,13 +73,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu ); } 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()});"); - - - method = template + method = JavaTemplate.builder("assertThat(#{any()}).as(#{any(String)}).isNotEqualTo(#{any()});") .staticImports("org.assertj.core.api.Assertions.assertThat") .javaParser(JavaParser.fromJavaVersion() .classpathFromResources(ctx, "assertj-core-3.24")) @@ -107,12 +101,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu maybeAddImport("org.assertj.core.api.Assertions", "within", false); } else { 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()}));"); - - method = template + method = JavaTemplate.builder("assertThat(#{any()}).as(#{any(String)}).isNotCloseTo(#{any()}, within(#{any()}));") .staticImports("org.assertj.core.api.Assertions.assertThat", "org.assertj.core.api.Assertions.within") .javaParser(JavaParser.fromJavaVersion() .classpathFromResources(ctx, "assertj-core-3.24"))