Skip to content

Commit 5dbee68

Browse files
authored
add recipes AssertFalseNullToAssertNotNull and AssertTrueNullToAssertNull (#217)
closes #202
1 parent ad786ed commit 5dbee68

File tree

4 files changed

+424
-0
lines changed

4 files changed

+424
-0
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright 2022 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.*;
22+
import org.openrewrite.java.search.UsesMethod;
23+
import org.openrewrite.java.tree.Expression;
24+
import org.openrewrite.java.tree.J;
25+
26+
import java.util.function.Supplier;
27+
28+
public class AssertFalseNullToAssertNotNull extends Recipe {
29+
private static final MethodMatcher ASSERT_FALSE = new MethodMatcher(
30+
"org.junit.jupiter.api.Assertions assertFalse(..)");
31+
32+
@Override
33+
public String getDisplayName() {
34+
return "Replace JUnit `assertFalse(a == null)` to `assertNotNull(a)`";
35+
}
36+
37+
@Override
38+
public String getDescription() {
39+
return "Using `assertNotNull(a)` is simpler and more clear.";
40+
}
41+
42+
@Override
43+
protected TreeVisitor<?, ExecutionContext> getSingleSourceApplicableTest() {
44+
return new UsesMethod<>(ASSERT_FALSE);
45+
}
46+
47+
@Override
48+
protected JavaVisitor<ExecutionContext> getVisitor() {
49+
Supplier<JavaParser> javaParser = () -> JavaParser.fromJavaVersion()
50+
//language=java
51+
.dependsOn("" +
52+
"package org.junit.jupiter.api;" +
53+
"public class Assertions {" +
54+
"public static void assertNotNull(Object actual) {}" +
55+
"}")
56+
.build();
57+
58+
return new JavaIsoVisitor<ExecutionContext>() {
59+
private final JavaTemplate assertNotNull = JavaTemplate.builder(this::getCursor, "assertNotNull(#{any(java.lang.Object)})")
60+
.staticImports("org.junit.jupiter.api.Assertions.assertNotNull")
61+
.javaParser(javaParser)
62+
.build();
63+
64+
private final JavaTemplate assertNotNullStaticImport = JavaTemplate.builder(this::getCursor, "Assertions.assertNotNull(#{any(java.lang.Object)})")
65+
.imports("org.junit.jupiter.api.Assertions")
66+
.javaParser(javaParser)
67+
.build();
68+
69+
@Override
70+
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
71+
if (ASSERT_FALSE.matches(method) && isEqualBinary(method)) {
72+
73+
J.Binary binary = (J.Binary) method.getArguments().get(0);
74+
75+
Expression nonNullExpression = getNonNullExpression(binary);
76+
77+
if (method.getSelect() == null) {
78+
maybeRemoveImport("org.junit.jupiter.api.Assertions");
79+
maybeAddImport("org.junit.jupiter.api.Assertions", "assertNotNull");
80+
return method.withTemplate(assertNotNull, method.getCoordinates().replace(), nonNullExpression);
81+
} else {
82+
return method.withTemplate(assertNotNullStaticImport, method.getCoordinates().replace(), nonNullExpression);
83+
}
84+
}
85+
return super.visitMethodInvocation(method, ctx);
86+
}
87+
88+
89+
private Expression getNonNullExpression(J.Binary binary) {
90+
91+
if (binary.getRight() instanceof J.Literal){
92+
boolean isNull = ((J.Literal) binary.getRight()).getValue() == null;
93+
if (isNull){
94+
return binary.getLeft();
95+
}
96+
}
97+
98+
return binary.getRight();
99+
}
100+
101+
private boolean isEqualBinary(J.MethodInvocation method) {
102+
103+
if (method.getArguments().isEmpty()) {
104+
return false;
105+
}
106+
107+
J.Binary binary = (J.Binary) method.getArguments().get(0);
108+
J.Binary.Type operator = binary.getOperator();
109+
return operator.equals(J.Binary.Type.Equal);
110+
111+
112+
}
113+
};
114+
}
115+
116+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright 2022 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.*;
22+
import org.openrewrite.java.search.UsesMethod;
23+
import org.openrewrite.java.tree.Expression;
24+
import org.openrewrite.java.tree.J;
25+
26+
import java.util.function.Supplier;
27+
28+
public class AssertTrueNullToAssertNull extends Recipe {
29+
private static final MethodMatcher ASSERT_TRUE = new MethodMatcher(
30+
"org.junit.jupiter.api.Assertions assertTrue(..)");
31+
32+
@Override
33+
public String getDisplayName() {
34+
return "Replace JUnit `assertTrue(a == null)` to `assertNull(a)`";
35+
}
36+
37+
@Override
38+
public String getDescription() {
39+
return "Using `assertNull(a)` is simpler and more clear.";
40+
}
41+
42+
@Override
43+
protected TreeVisitor<?, ExecutionContext> getSingleSourceApplicableTest() {
44+
return new UsesMethod<>(ASSERT_TRUE);
45+
}
46+
47+
@Override
48+
protected JavaVisitor<ExecutionContext> getVisitor() {
49+
Supplier<JavaParser> javaParser = () -> JavaParser.fromJavaVersion()
50+
//language=java
51+
.dependsOn("" +
52+
"package org.junit.jupiter.api;" +
53+
"public class Assertions {" +
54+
" public static void assertNull(Object actual) {}" +
55+
"}")
56+
.build();
57+
58+
return new JavaIsoVisitor<ExecutionContext>() {
59+
private final JavaTemplate assertNull = JavaTemplate.builder(this::getCursor, "assertNull(#{any(java.lang.Object)})")
60+
.staticImports("org.junit.jupiter.api.Assertions.assertNull")
61+
.javaParser(javaParser)
62+
.build();
63+
64+
private final JavaTemplate assertNullNoStaticImport = JavaTemplate.builder(this::getCursor, "Assertions.assertNull(#{any(java.lang.Object)})")
65+
.imports("org.junit.jupiter.api.Assertions")
66+
.javaParser(javaParser)
67+
.build();
68+
69+
@Override
70+
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
71+
if (ASSERT_TRUE.matches(method) && isEqualBinary(method)) {
72+
73+
J.Binary binary = (J.Binary) method.getArguments().get(0);
74+
75+
Expression nonNullExpression = getNonNullExpression(binary);
76+
77+
if (method.getSelect() == null) {
78+
maybeRemoveImport("org.junit.jupiter.api.Assertions");
79+
maybeAddImport("org.junit.jupiter.api.Assertions", "assertNull");
80+
return method.withTemplate(assertNull, method.getCoordinates().replace(), nonNullExpression);
81+
} else {
82+
return method.withTemplate(assertNullNoStaticImport, method.getCoordinates().replace(), nonNullExpression);
83+
}
84+
}
85+
return super.visitMethodInvocation(method, ctx);
86+
}
87+
88+
89+
private Expression getNonNullExpression(J.Binary binary) {
90+
91+
if (binary.getRight() instanceof J.Literal){
92+
boolean isNull = ((J.Literal) binary.getRight()).getValue() == null;
93+
if (isNull){
94+
return binary.getLeft();
95+
}
96+
}
97+
98+
return binary.getRight();
99+
}
100+
101+
private boolean isEqualBinary(J.MethodInvocation method) {
102+
103+
if (method.getArguments().isEmpty()) {
104+
return false;
105+
}
106+
107+
J.Binary binary = (J.Binary) method.getArguments().get(0);
108+
J.Binary.Type operator = binary.getOperator();
109+
return operator.equals(J.Binary.Type.Equal);
110+
111+
112+
}
113+
};
114+
}
115+
116+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright 2022 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.Issue
20+
import org.openrewrite.Recipe
21+
import org.openrewrite.java.JavaParser
22+
import org.openrewrite.java.JavaRecipeTest
23+
24+
class AssertFalseNullToAssertNotNullTest : JavaRecipeTest {
25+
override val parser: JavaParser = JavaParser.fromJavaVersion()
26+
.logCompilationWarningsAndErrors(true)
27+
.classpath("junit-jupiter-api")
28+
.build()
29+
30+
override val recipe: Recipe
31+
get() = AssertFalseNullToAssertNotNull()
32+
33+
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/202")
34+
@Suppress("ConstantConditions", "SimplifiableAssertion")
35+
@Test
36+
fun simplifyToAssertNull() = assertChanged(
37+
before = """
38+
import static org.junit.jupiter.api.Assertions.assertFalse;
39+
40+
public class Test {
41+
void test() {
42+
String a = null;
43+
assertFalse(a == null);
44+
45+
String b = null;
46+
assertFalse(null == b);
47+
}
48+
}
49+
""",
50+
after = """
51+
import static org.junit.jupiter.api.Assertions.assertNotNull;
52+
53+
public class Test {
54+
void test() {
55+
String a = null;
56+
assertNotNull(a);
57+
58+
String b = null;
59+
assertNotNull(b);
60+
}
61+
}
62+
""",
63+
)
64+
65+
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/202")
66+
@Suppress("ConstantConditions", "SimplifiableAssertion")
67+
@Test
68+
fun preserveStyleOfStaticImportOrNot() = assertChanged(
69+
before = """
70+
import org.junit.jupiter.api.Assertions;
71+
72+
public class Test {
73+
void test() {
74+
String a = null;
75+
Assertions.assertFalse(a == null);
76+
77+
String b = null;
78+
Assertions.assertFalse(null == b);
79+
}
80+
}
81+
""",
82+
after = """
83+
import org.junit.jupiter.api.Assertions;
84+
85+
public class Test {
86+
void test() {
87+
String a = null;
88+
Assertions.assertNotNull(a);
89+
90+
String b = null;
91+
Assertions.assertNotNull(b);
92+
}
93+
}
94+
""",
95+
)
96+
}

0 commit comments

Comments
 (0)