Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert assertTrue(false, String) and assertFalse(true, String) with fail(String) #558

Merged
merged 6 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package org.openrewrite.java.testing.cleanup;
Laurens-W marked this conversation as resolved.
Show resolved Hide resolved

import org.openrewrite.*;
import org.openrewrite.java.JavaParser;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.search.UsesMethod;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;

public class AssertFalseLiteralTrueToFail extends Recipe {
private static final MethodMatcher ASSERT_FALSE = new MethodMatcher(
"org.junit.jupiter.api.Assertions assertFalse(boolean, String)");

@Override
public @NlsRewrite.DisplayName String getDisplayName() {
return "Replace JUnit `assertFalse(true, \"reason\")` with `fail(\"reason\")`";
}

@Override
public @NlsRewrite.Description String getDescription() {
return "Using fail is more direct and clear.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(new UsesMethod<>(ASSERT_FALSE), new JavaVisitor<ExecutionContext>() {

JavaParser.Builder<?, ?> javaParser = null;

private JavaParser.Builder<?, ?> javaParser(ExecutionContext ctx) {
if (javaParser == null) {
javaParser = JavaParser.fromJavaVersion()
.classpathFromResources(ctx, "junit-jupiter-api-5.9");
}
return javaParser;
}

@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation mi = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);
if (ASSERT_FALSE.matches(mi)) {
if (mi.getArguments().get(0) instanceof J.Literal) {
J.Literal literal = (J.Literal) mi.getArguments().get(0);
if (JavaType.Primitive.Boolean.equals(literal.getType()) && Boolean.TRUE.equals(literal.getValue())) {
StringBuilder sb = new StringBuilder();
if (mi.getSelect() == null) {
maybeRemoveImport("org.junit.jupiter.api.Assertions");
maybeAddImport("org.junit.jupiter.api.Assertions", "fail");
} else {
sb.append("Assertions.");
}
sb.append("fail(#{any(java.lang.String)})");
Object[] args = new Object[]{mi.getArguments().get(1)};
JavaTemplate t;
if (mi.getSelect() == null) {
t = JavaTemplate.builder(sb.toString())
.contextSensitive()
.staticImports("org.junit.jupiter.api.Assertions.fail")
.javaParser(javaParser(ctx))
.build();
} else {
t = JavaTemplate.builder(sb.toString())
.contextSensitive()
.imports("org.junit.jupiter.api.Assertions")
.javaParser(javaParser(ctx))
.build();
}
return t.apply(updateCursor(mi), mi.getCoordinates().replace(), args);
}
}
}
return mi;
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package org.openrewrite.java.testing.cleanup;
Laurens-W marked this conversation as resolved.
Show resolved Hide resolved

import org.openrewrite.*;
import org.openrewrite.java.JavaParser;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.search.UsesMethod;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;

public class AssertTrueLiteralFalseToFail extends Recipe {
private static final MethodMatcher ASSERT_TRUE = new MethodMatcher(
"org.junit.jupiter.api.Assertions assertTrue(boolean, String)");

@Override
public @NlsRewrite.DisplayName String getDisplayName() {
return "Replace JUnit `assertTrue(false, \"reason\")` with `fail(\"reason\")`";
}

@Override
public @NlsRewrite.Description String getDescription() {
return "Using fail is more direct and clear.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(new UsesMethod<>(ASSERT_TRUE), new JavaVisitor<ExecutionContext>() {

JavaParser.Builder<?, ?> javaParser = null;

private JavaParser.Builder<?, ?> javaParser(ExecutionContext ctx) {
if (javaParser == null) {
javaParser = JavaParser.fromJavaVersion()
.classpathFromResources(ctx, "junit-jupiter-api-5.9");
}
return javaParser;
}

@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation mi = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);
if (ASSERT_TRUE.matches(mi)) {
if (mi.getArguments().get(0) instanceof J.Literal) {
J.Literal literal = (J.Literal) mi.getArguments().get(0);
if (JavaType.Primitive.Boolean.equals(literal.getType()) && Boolean.FALSE.equals(literal.getValue())) {
StringBuilder sb = new StringBuilder();
if (mi.getSelect() == null) {
maybeRemoveImport("org.junit.jupiter.api.Assertions");
maybeAddImport("org.junit.jupiter.api.Assertions", "fail");
} else {
sb.append("Assertions.");
}
sb.append("fail(#{any(java.lang.String)})");
Object[] args = new Object[]{mi.getArguments().get(1)};
JavaTemplate t;
if (mi.getSelect() == null) {
t = JavaTemplate.builder(sb.toString())
.contextSensitive()
.staticImports("org.junit.jupiter.api.Assertions.fail")
.javaParser(javaParser(ctx))
.build();
} else {
t = JavaTemplate.builder(sb.toString())
.contextSensitive()
.imports("org.junit.jupiter.api.Assertions")
.javaParser(javaParser(ctx))
.build();
}
return t.apply(updateCursor(mi), mi.getCoordinates().replace(), args);
}
}
}
return mi;
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package org.openrewrite.java.testing.cleanup;
Laurens-W marked this conversation as resolved.
Show resolved Hide resolved

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 AssertFalseLiteralTrueToFailTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec
.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "junit-jupiter-api-5.9"))
.recipe(new AssertFalseLiteralTrueToFail());
}

@Test
Laurens-W marked this conversation as resolved.
Show resolved Hide resolved
void assertFalseToFail() {
//language=java
rewriteRun(
java(
"""
import static org.junit.jupiter.api.Assertions.assertFalse;

public class Test {
void test() {
assertFalse(true, "message");
}
}
""",
"""
import static org.junit.jupiter.api.Assertions.fail;

public class Test {
void test() {
fail("message");
}
}
"""
)
);
}

@Test
void assertFalseToFailNonStatic() {
//language=java
rewriteRun(
java(
"""
import org.junit.jupiter.api.Assertions;

public class Test {
void test() {
Assertions.assertFalse(true, "message");
}
}
""",
"""
import org.junit.jupiter.api.Assertions;

public class Test {
void test() {
Assertions.fail("message");
}
}
"""
)
);
}

@Test
void assertFalseNonLiteralNoChange() {
//language=java
rewriteRun(
java(
"""
import static org.junit.jupiter.api.Assertions.assertFalse;

public class Test {
void test() {
String a1 = "a";
String a2 = "a";
assertFalse(a1.equals(a2), "message");
}
}
"""
)
);
}

}
Laurens-W marked this conversation as resolved.
Show resolved Hide resolved
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package org.openrewrite.java.testing.cleanup;
Laurens-W marked this conversation as resolved.
Show resolved Hide resolved

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 AssertTrueLiteralFalseToFailTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec
.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "junit-jupiter-api-5.9"))
.recipe(new AssertTrueLiteralFalseToFail());
}

@Test
Laurens-W marked this conversation as resolved.
Show resolved Hide resolved
void assertTrueToFail() {
//language=java
rewriteRun(
java(
"""
import static org.junit.jupiter.api.Assertions.assertTrue;

public class Test {
void test() {
assertTrue(false, "message");
}
}
""",
"""
import static org.junit.jupiter.api.Assertions.fail;

public class Test {
void test() {
fail("message");
}
}
"""
)
);
}

@Test
void assertTrueToFailNonStatic() {
//language=java
rewriteRun(
java(
"""
import org.junit.jupiter.api.Assertions;

public class Test {
void test() {
Assertions.assertTrue(false, "message");
}
}
""",
"""
import org.junit.jupiter.api.Assertions;

public class Test {
void test() {
Assertions.fail("message");
}
}
"""
)
);
}

@Test
void assertTrueNonLiteralNoChange() {
//language=java
rewriteRun(
java(
"""
import static org.junit.jupiter.api.Assertions.assertTrue;

public class Test {
void test() {
String a = "a";
String b = "b";
assertTrue(a.equals(b), "message");
}
}
"""
)
);
}

}
Laurens-W marked this conversation as resolved.
Show resolved Hide resolved
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
Loading