Skip to content

Commit 1ee57c8

Browse files
ammachadotimtebeek
andauthored
Refactor assertEquals(<boolean>, ...) and assertNotEquals(<boolean>, ...) to assertTrue or assertFalse (#417)
Co-authored-by: Adriano Machado <[email protected]> Co-authored-by: Tim te Beek <[email protected]>
1 parent f65e9cc commit 1ee57c8

File tree

5 files changed

+424
-0
lines changed

5 files changed

+424
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.java.testing.cleanup;
17+
18+
import org.openrewrite.ExecutionContext;
19+
import org.openrewrite.Recipe;
20+
import org.openrewrite.TreeVisitor;
21+
import org.openrewrite.java.JavaParser;
22+
import org.openrewrite.java.JavaTemplate;
23+
import org.openrewrite.java.JavaVisitor;
24+
import org.openrewrite.java.MethodMatcher;
25+
import org.openrewrite.java.tree.J;
26+
import org.openrewrite.java.tree.JavaType;
27+
import org.openrewrite.java.tree.Statement;
28+
29+
public class AssertEqualsBooleanToAssertBoolean extends Recipe {
30+
private static final MethodMatcher ASSERT_EQUALS = new MethodMatcher(
31+
"org.junit.jupiter.api.Assertions assertEquals(..)");
32+
33+
@Override
34+
public String getDisplayName() {
35+
return "Replace JUnit `assertEquals(false, <boolean>)` to `assertFalse(<boolean>)` / `assertTrue(<boolean>)`";
36+
}
37+
38+
@Override
39+
public String getDescription() {
40+
return "Using `assertFalse` or `assertTrue` is simpler and more clear.";
41+
}
42+
43+
@Override
44+
public TreeVisitor<?, ExecutionContext> getVisitor() {
45+
return new JavaVisitor<ExecutionContext>() {
46+
47+
JavaParser.Builder<?, ?> javaParser = null;
48+
49+
private JavaParser.Builder<?, ?> javaParser(ExecutionContext ctx) {
50+
if (javaParser == null) {
51+
javaParser = JavaParser.fromJavaVersion()
52+
.classpathFromResources(ctx, "junit-jupiter-api-5.9");
53+
}
54+
return javaParser;
55+
}
56+
57+
@Override
58+
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
59+
J.MethodInvocation mi = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);
60+
if (ASSERT_EQUALS.matches(mi) && isBooleanLiteral(mi)) {
61+
StringBuilder sb = new StringBuilder();
62+
String assertMethod = Boolean.parseBoolean(((J.Literal) mi.getArguments().get(0)).getValueSource())
63+
? "assertTrue" : "assertFalse";
64+
Statement assertion = (Statement) mi.getArguments().get(1);
65+
if (mi.getSelect() == null) {
66+
maybeRemoveImport("org.junit.jupiter.api.Assertions");
67+
maybeAddImport("org.junit.jupiter.api.Assertions", assertMethod);
68+
} else {
69+
sb.append("Assertions.");
70+
}
71+
sb.append("#{}(#{any(java.lang.Boolean)}");
72+
Object[] args;
73+
if (mi.getArguments().size() == 3) {
74+
args = new Object[]{assertMethod, assertion, mi.getArguments().get(2)};
75+
sb.append(", #{any()}");
76+
} else {
77+
args = new Object[]{assertMethod, mi.getArguments().get(1)};
78+
}
79+
sb.append(")");
80+
JavaTemplate t;
81+
if (mi.getSelect() == null) {
82+
t = JavaTemplate.builder(sb.toString())
83+
.contextSensitive()
84+
.staticImports(String.format("org.junit.jupiter.api.Assertions.%s", assertMethod))
85+
.javaParser(javaParser(ctx))
86+
.build();
87+
} else {
88+
t = JavaTemplate.builder(sb.toString())
89+
.contextSensitive()
90+
.imports(String.format("org.junit.jupiter.api.Assertions.%s", assertMethod))
91+
.javaParser(javaParser(ctx))
92+
.build();
93+
}
94+
return t.apply(updateCursor(mi), mi.getCoordinates().replace(), args);
95+
}
96+
return mi;
97+
}
98+
99+
private boolean isBooleanLiteral(J.MethodInvocation method) {
100+
if (!method.getArguments().isEmpty() && method.getArguments().get(0) instanceof J.Literal) {
101+
J.Literal literal = (J.Literal) method.getArguments().get(0);
102+
return JavaType.Primitive.Boolean.equals(literal.getType());
103+
}
104+
105+
return false;
106+
}
107+
};
108+
}
109+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.java.testing.cleanup;
17+
18+
import org.openrewrite.ExecutionContext;
19+
import org.openrewrite.Recipe;
20+
import org.openrewrite.TreeVisitor;
21+
import org.openrewrite.java.JavaParser;
22+
import org.openrewrite.java.JavaTemplate;
23+
import org.openrewrite.java.JavaVisitor;
24+
import org.openrewrite.java.MethodMatcher;
25+
import org.openrewrite.java.tree.J;
26+
import org.openrewrite.java.tree.JavaType;
27+
import org.openrewrite.java.tree.Statement;
28+
29+
public class AssertNotEqualsBooleanToAssertBoolean extends Recipe {
30+
private static final MethodMatcher ASSERT_NOT_EQUALS = new MethodMatcher(
31+
"org.junit.jupiter.api.Assertions assertNotEquals(..)");
32+
33+
@Override
34+
public String getDisplayName() {
35+
return "Replace JUnit `assertNotEquals(false, <boolean>)` to `assertFalse(<boolean>)` / `assertTrue(<boolean>)`";
36+
}
37+
38+
@Override
39+
public String getDescription() {
40+
return "Using `assertFalse` or `assertTrue` is simpler and more clear.";
41+
}
42+
43+
@Override
44+
public TreeVisitor<?, ExecutionContext> getVisitor() {
45+
return new JavaVisitor<ExecutionContext>() {
46+
47+
JavaParser.Builder<?, ?> javaParser = null;
48+
49+
private JavaParser.Builder<?, ?> javaParser(ExecutionContext ctx) {
50+
if (javaParser == null) {
51+
javaParser = JavaParser.fromJavaVersion()
52+
.classpathFromResources(ctx, "junit-jupiter-api-5.9");
53+
}
54+
return javaParser;
55+
}
56+
57+
@Override
58+
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
59+
J.MethodInvocation mi = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);
60+
if (ASSERT_NOT_EQUALS.matches(mi) && isBooleanLiteral(mi)) {
61+
StringBuilder sb = new StringBuilder();
62+
String assertMethod = Boolean.parseBoolean(((J.Literal) mi.getArguments().get(0)).getValueSource())
63+
? "assertFalse" : "assertTrue";
64+
Statement assertion = (Statement) mi.getArguments().get(1);
65+
if (mi.getSelect() == null) {
66+
maybeRemoveImport("org.junit.jupiter.api.Assertions");
67+
maybeAddImport("org.junit.jupiter.api.Assertions", assertMethod);
68+
} else {
69+
sb.append("Assertions.");
70+
}
71+
sb.append("#{}(#{any(java.lang.Boolean)}");
72+
Object[] args;
73+
if (mi.getArguments().size() == 3) {
74+
args = new Object[]{assertMethod, assertion, mi.getArguments().get(2)};
75+
sb.append(", #{any()}");
76+
} else {
77+
args = new Object[]{assertMethod, mi.getArguments().get(1)};
78+
}
79+
sb.append(")");
80+
JavaTemplate t;
81+
if (mi.getSelect() == null) {
82+
t = JavaTemplate.builder(sb.toString())
83+
.contextSensitive()
84+
.staticImports(String.format("org.junit.jupiter.api.Assertions.%s", assertMethod))
85+
.javaParser(javaParser(ctx))
86+
.build();
87+
} else {
88+
t = JavaTemplate.builder(sb.toString())
89+
.contextSensitive()
90+
.imports(String.format("org.junit.jupiter.api.Assertions.%s", assertMethod))
91+
.javaParser(javaParser(ctx))
92+
.build();
93+
}
94+
return t.apply(updateCursor(mi), mi.getCoordinates().replace(), args);
95+
}
96+
return mi;
97+
}
98+
99+
private boolean isBooleanLiteral(J.MethodInvocation method) {
100+
if (!method.getArguments().isEmpty() && method.getArguments().get(0) instanceof J.Literal) {
101+
J.Literal literal = (J.Literal) method.getArguments().get(0);
102+
return JavaType.Primitive.Boolean.equals(literal.getType());
103+
}
104+
105+
return false;
106+
}
107+
};
108+
}
109+
}

src/main/resources/META-INF/rewrite/junit5.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,8 @@ recipeList:
266266
- org.openrewrite.java.testing.cleanup.AssertFalseEqualsToAssertNotEquals
267267
- org.openrewrite.java.testing.cleanup.AssertEqualsNullToAssertNull
268268
- org.openrewrite.java.testing.cleanup.AssertFalseNullToAssertNotNull
269+
- org.openrewrite.java.testing.cleanup.AssertEqualsBooleanToAssertBoolean
270+
- org.openrewrite.java.testing.cleanup.AssertNotEqualsBooleanToAssertBoolean
269271
- org.openrewrite.java.testing.cleanup.AssertionsArgumentOrder
270272
---
271273
type: specs.openrewrite.org/v1beta/recipe
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.java.testing.cleanup;
17+
18+
import org.junit.jupiter.api.Test;
19+
import org.openrewrite.InMemoryExecutionContext;
20+
import org.openrewrite.Issue;
21+
import org.openrewrite.java.JavaParser;
22+
import org.openrewrite.test.RecipeSpec;
23+
import org.openrewrite.test.RewriteTest;
24+
25+
import static org.openrewrite.java.Assertions.java;
26+
27+
class AssertEqualsBooleanToAssertBooleanTest implements RewriteTest {
28+
29+
@Override
30+
public void defaults(RecipeSpec spec) {
31+
spec
32+
.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "junit-jupiter-api-5.9"))
33+
.recipe(new AssertEqualsBooleanToAssertBoolean());
34+
}
35+
36+
@SuppressWarnings({"ConstantConditions"})
37+
@Test
38+
void assertEqualsFalseToToAssertFalse() {
39+
//language=java
40+
rewriteRun(
41+
java(
42+
"""
43+
import static org.junit.jupiter.api.Assertions.assertEquals;
44+
45+
public class Test {
46+
void test() {
47+
String a = "a";
48+
String c = "c";
49+
assertEquals(false, a.equals(c));
50+
assertEquals(false, a.equals(c), "message");
51+
}
52+
}
53+
""",
54+
"""
55+
import static org.junit.jupiter.api.Assertions.assertFalse;
56+
57+
public class Test {
58+
void test() {
59+
String a = "a";
60+
String c = "c";
61+
assertFalse(a.equals(c));
62+
assertFalse(a.equals(c), "message");
63+
}
64+
}
65+
"""
66+
)
67+
);
68+
}
69+
70+
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/206")
71+
@SuppressWarnings({"ConstantConditions", "SimplifiableAssertion"})
72+
@Test
73+
void preserveStyleOfStaticImportOrNot() {
74+
//language=java
75+
rewriteRun(
76+
java(
77+
"""
78+
import org.junit.jupiter.api.Assertions;
79+
80+
public class Test {
81+
void test() {
82+
String a = "a";
83+
String c = "c";
84+
Assertions.assertEquals(false, a.equals(c), "message");
85+
}
86+
}
87+
""",
88+
"""
89+
import org.junit.jupiter.api.Assertions;
90+
91+
public class Test {
92+
void test() {
93+
String a = "a";
94+
String c = "c";
95+
Assertions.assertFalse(a.equals(c), "message");
96+
}
97+
}
98+
"""
99+
)
100+
);
101+
}
102+
}

0 commit comments

Comments
 (0)