From 9ed37037b76141813030dd6c06a7c065e366facc Mon Sep 17 00:00:00 2001 From: Sasha Sheikin Date: Tue, 4 Jun 2024 21:04:46 +0200 Subject: [PATCH] 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 ++++++------ .../resources/META-INF/rewrite/assertj.yml | 22 +++++++++++++++++++ 7 files changed, 68 insertions(+), 46 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