Skip to content

Commit

Permalink
Implement assertNull() and assertNotNull() migration
Browse files Browse the repository at this point in the history
  • Loading branch information
Philzen committed Jun 12, 2024
1 parent 6f4e1a0 commit d760e6f
Show file tree
Hide file tree
Showing 2 changed files with 208 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,112 @@ void after(boolean expr, String msg) {
Assertions.assertTrue(expr, msg);
}
}

@RecipeDescriptor(
name = "Replace `Assert#assertNull(Object)`",
description = "Replace `org.testng.Assert#assertNull(Object)` with `org.junit.jupiter.api.Assertions#assertNull(Object)`."
)
public static class MigrateAssertNull {

@BeforeTemplate void before(Object expr) {
Assert.assertNull(expr);
}

@AfterTemplate void after(Object expr) {
Assertions.assertNull(expr);
}
}

@RecipeDescriptor(
name = "Replace `Assert#assertNull(Object, String)`",
description = "Replace `org.testng.Assert#assertNull(Object, String)` with `org.junit.jupiter.api.Assertions#assertNull(Object, String)`."
)
public static class MigrateAssertNullWithMsg {

@BeforeTemplate void before(Object expr, String msg) {
Assert.assertNull(expr, msg);
}

@AfterTemplate void after(Object expr, String msg) {
Assertions.assertNull(expr, msg);
}
}
<<<<<<< HEAD
=======

@RecipeDescriptor(
name = "Replace `Assert#assertNotNull(Object)`",
description = "Replace `org.testng.Assert#assertNotNull(Object)` with `org.junit.jupiter.api.Assertions#assertNotNull(Object)`."
)
public static class MigrateAssertNotNull {

@BeforeTemplate void before(Object expr) {
Assert.assertNotNull(expr);
}

@AfterTemplate void after(Object expr) {
Assertions.assertNotNull(expr);
}
}

@RecipeDescriptor(
name = "Replace `Assert#assertNotNull(Object, String)`",
description = "Replace `org.testng.Assert#assertNotNull(Object, String)` with `org.junit.jupiter.api.Assertions#assertNotNull(Object, String)`."
)
public static class MigrateAssertNotNullWithMsg {

@BeforeTemplate void before(Object expr, String msg) {
Assert.assertNotNull(expr, msg);
}

@AfterTemplate void after(Object expr, String msg) {
Assertions.assertNotNull(expr, msg);
}
}

@RecipeDescriptor(
name = "Replace `Assert#fail()`",
description = "Replace `org.testng.Assert#fail()` with `org.junit.jupiter.api.Assertions#fail()`."
)
public static class MigrateFailNoArgs {

@BeforeTemplate void before() {
Assert.fail();
}

@AfterTemplate void after() {
Assertions.fail();
}
}

@RecipeDescriptor(
name = "Replace `Assert#fail(String)`",
description = "Replace `org.testng.Assert#fail(String)` with `org.junit.jupiter.api.Assertions#fail(String)`."
)
public static class MigrateFailWithMessage {

@BeforeTemplate void before(String message) {
Assert.fail(message);
}

@AfterTemplate void after(String message) {
Assertions.fail(message);
}
}

@RecipeDescriptor(
name = "Replace `Assert#fail(String)`",
description = "Replace `org.testng.Assert#fail(String)` with `org.junit.jupiter.api.Assertions#fail(String)`."
)
public static class MigrateFailWithMessageAndCause {

@BeforeTemplate void before(String message, Throwable cause) {
Assert.fail(message, cause);
}

@AfterTemplate void after(String message, Throwable cause) {
Assertions.fail(message, cause);
}
}
>>>>>>> 3c937e8 (fixup! Implement assertNull() migration)
}
Original file line number Diff line number Diff line change
Expand Up @@ -543,4 +543,104 @@ void testMethod() {
));
}
}

@SuppressWarnings({"DataFlowIssue", "ObviousNullCheck"})
@Nested class MigrateAssertNull {

@Test void withErrorMessage() {
//language=java
rewriteRun(java("""
import org.testng.Assert;
class MyTest {
void testMethod() {
Assert.assertNull(null, "Near-missed the billion dollar mistake.");
}
}
""",
"""
import org.junit.jupiter.api.Assertions;
class MyTest {
void testMethod() {
Assertions.assertNull(null, "Near-missed the billion dollar mistake.");
}
}
"""
));
}

@Test void withoutErrorMessage() {
//language=java
rewriteRun(java("""
import org.testng.Assert;
class MyTest {
void testMethod() {
Assert.assertNull("Not null");
}
}
""",
"""
import org.junit.jupiter.api.Assertions;
class MyTest {
void testMethod() {
Assertions.assertNull("Not null");
}
}
"""
));
}
}

@SuppressWarnings({"DataFlowIssue", "ObviousNullCheck"})
@Nested class MigrateAssertNotNull {

@Test void withErrorMessage() {
// language=java
rewriteRun(java("""
import org.testng.Assert;
class MyTest {
void testMethod() {
Assert.assertNotNull(null, "The billion dollar mistake hit again.");
}
}
""",
"""
import org.junit.jupiter.api.Assertions;
class MyTest {
void testMethod() {
Assertions.assertNotNull(null, "The billion dollar mistake hit again.");
}
}
"""
));
}

@Test void withoutErrorMessage() {
// language=java
rewriteRun(java("""
import org.testng.Assert;
class MyTest {
void testMethod() {
Assert.assertNotNull("Not null");
}
}
""",
"""
import org.junit.jupiter.api.Assertions;
class MyTest {
void testMethod() {
Assertions.assertNotNull("Not null");
}
}
"""
));
}
}
}

0 comments on commit d760e6f

Please sign in to comment.