Skip to content

Commit

Permalink
Adapt copied JUnit classes to TestNG
Browse files Browse the repository at this point in the history
  • Loading branch information
ssheikin committed Jun 4, 2024
1 parent 85b99a8 commit 9ed3703
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<?, ExecutionContext> 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<ExecutionContext> {
Expand All @@ -60,23 +60,23 @@ public static class AssertEqualsToAssertThatVisitor extends JavaIsoVisitor<Execu
return assertionsParser;
}

private static final MethodMatcher JUNIT_ASSERT_EQUALS = new MethodMatcher("org.junit.jupiter.api.Assertions" + " assertEquals(..)");
private static final MethodMatcher TESTNG_ASSERT_EQUALS = new MethodMatcher("org.testng.Assert" + " assertEquals(..)");

@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
if (!JUNIT_ASSERT_EQUALS.matches(method)) {
if (!TESTNG_ASSERT_EQUALS.matches(method)) {
return method;
}

List<Expression> 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()});")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<?, ExecutionContext> 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<ExecutionContext> {
Expand All @@ -59,11 +59,11 @@ public static class AssertFalseToAssertThatVisitor extends JavaIsoVisitor<Execut
return assertionsParser;
}

private static final MethodMatcher JUNIT_ASSERT_FALSE = new MethodMatcher("org.junit.jupiter.api.Assertions" + " assertFalse(boolean, ..)");
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 (!JUNIT_ASSERT_FALSE.matches(method)) {
if (!TESTNG_ASSERT_FALSE.matches(method)) {
return method;
}

Expand Down Expand Up @@ -101,8 +101,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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ public class TestNgAssertNotEqualsToAssertThat

@Override
public String getDisplayName() {
return "JUnit `assertNotEquals` to AssertJ";
return "TestNG `assertNotEquals` to AssertJ";
}

@Override
public String getDescription() {
return "Convert JUnit-style `assertNotEquals()` to AssertJ's `assertThat().isNotEqualTo()`.";
return "Convert TestNG-style `assertNotEquals()` to AssertJ's `assertThat().isNotEqualTo()`.";
}

@Override
public TreeVisitor<?, ExecutionContext> 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<ExecutionContext> {
Expand All @@ -60,18 +60,18 @@ public static class AssertNotEqualsToAssertThatVisitor extends JavaIsoVisitor<Ex
return assertionsParser;
}

private static final MethodMatcher JUNIT_ASSERT_EQUALS = new MethodMatcher("org.junit.jupiter.api.Assertions" + " assertNotEquals(..)");
private static final MethodMatcher TESTNG_ASSERT_EQUALS = new MethodMatcher("org.testng.Assert" + " assertNotEquals(..)");

@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
if (!JUNIT_ASSERT_EQUALS.matches(method)) {
if (!TESTNG_ASSERT_EQUALS.matches(method)) {
return method;
}

List<Expression> 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()});")
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<?, ExecutionContext> 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<ExecutionContext> {
Expand All @@ -59,11 +59,11 @@ public static class AssertNotNullToAssertThatVisitor extends JavaIsoVisitor<Exec
return assertionsParser;
}

private static final MethodMatcher JUNIT_ASSERT_NOT_NULL_MATCHER = new MethodMatcher("org.junit.jupiter.api.Assertions" + " assertNotNull(..)");
private static final MethodMatcher TESTNG_ASSERT_NOT_NULL_MATCHER = new MethodMatcher("org.testng.Assert" + " assertNotNull(..)");

@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
if (!JUNIT_ASSERT_NOT_NULL_MATCHER.matches(method)) {
if (!TESTNG_ASSERT_NOT_NULL_MATCHER.matches(method)) {
return method;
}

Expand Down Expand Up @@ -103,8 +103,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);

//And if there are no longer references to the JUnit assertions class, we can remove the import.
maybeRemoveImport("org.junit.jupiter.api.Assertions");
//And if there are no longer references to the TestNG assertions class, we can remove the import.
maybeRemoveImport("org.testng.Assert");

return method;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ public class TestNgAssertNullToAssertThat

@Override
public String getDisplayName() {
return "JUnit `assertNull` to AssertJ";
return "TestNG `assertNull` to AssertJ";
}

@Override
public String getDescription() {
return "Convert JUnit-style `assertNull()` to AssertJ's `assertThat().isNull()`.";
return "Convert TestNG-style `assertNull()` to AssertJ's `assertThat().isNull()`.";
}

@Override
public TreeVisitor<?, ExecutionContext> 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<ExecutionContext> {
Expand All @@ -59,11 +59,11 @@ public static class AssertNullToAssertThatVisitor extends JavaIsoVisitor<Executi
return assertionsParser;
}

private static final MethodMatcher JUNIT_ASSERT_NULL_MATCHER = new MethodMatcher("org.junit.jupiter.api.Assertions" + " assertNull(..)");
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 (!JUNIT_ASSERT_NULL_MATCHER.matches(method)) {
if (!TESTNG_ASSERT_NULL_MATCHER.matches(method)) {
return method;
}

Expand Down Expand Up @@ -102,8 +102,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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ public class TestNgAssertTrueToAssertThat
extends Recipe {
@Override
public String getDisplayName() {
return "JUnit `assertTrue` to AssertJ";
return "TestNG `assertTrue` to AssertJ";
}

@Override
public String getDescription() {
return "Convert JUnit-style `assertTrue()` to AssertJ's `assertThat().isTrue()`.";
return "Convert TestNG-style `assertTrue()` to AssertJ's `assertThat().isTrue()`.";
}

@Override
public TreeVisitor<?, ExecutionContext> 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<ExecutionContext> {
Expand All @@ -58,11 +58,11 @@ public static class AssertTrueToAssertThatVisitor extends JavaIsoVisitor<Executi
return assertionsParser;
}

private static final MethodMatcher JUNIT_ASSERT_TRUE = new MethodMatcher("org.junit.jupiter.api.Assertions" + " assertTrue(boolean, ..)");
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 (!JUNIT_ASSERT_TRUE.matches(method)) {
if (!TESTNG_ASSERT_TRUE.matches(method)) {
return method;
}

Expand Down Expand Up @@ -101,8 +101,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;
}
Expand Down
22 changes: 22 additions & 0 deletions src/main/resources/META-INF/rewrite/assertj.yml
Original file line number Diff line number Diff line change
Expand Up @@ -364,3 +364,25 @@ 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.dependencies.AddDependency:
groupId: org.assertj
artifactId: assertj-core
version: 3.x
onlyIfUsing: org.assertj.core.api.Assertions
acceptTransitive: true

0 comments on commit 9ed3703

Please sign in to comment.