Skip to content

Commit 30b0de3

Browse files
authored
Composed existing JUnit5 assertion cleanup recipes into a well-ordere… (#232)
* Composed existing JUnit5 assertion cleanup recipes into a well-ordered parent recipe, and adjusted JUnit5BestPractices to use that new recipe instead of a small subset * Avoiding ClassCastException in assertion cleanup recipes
1 parent f227765 commit 30b0de3

File tree

5 files changed

+144
-13
lines changed

5 files changed

+144
-13
lines changed

src/main/java/org/openrewrite/java/testing/cleanup/AssertFalseNullToAssertNotNull.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,14 @@ private boolean isEqualBinary(J.MethodInvocation method) {
104104
return false;
105105
}
106106

107-
J.Binary binary = (J.Binary) method.getArguments().get(0);
107+
final Expression firstArgument = method.getArguments().get(0);
108+
if (!(firstArgument instanceof J.Binary)) {
109+
return false;
110+
}
111+
112+
J.Binary binary = (J.Binary) firstArgument;
108113
J.Binary.Type operator = binary.getOperator();
109114
return operator.equals(J.Binary.Type.Equal);
110-
111-
112115
}
113116
};
114117
}

src/main/java/org/openrewrite/java/testing/cleanup/AssertTrueComparisonToAssertEquals.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,14 @@ private boolean isEqualBinary(J.MethodInvocation method) {
9696
return false;
9797
}
9898

99-
J.Binary binary = (J.Binary) method.getArguments().get(0);
99+
final Expression firstArgument = method.getArguments().get(0);
100+
if (!(firstArgument instanceof J.Binary)) {
101+
return false;
102+
}
103+
104+
J.Binary binary = (J.Binary) firstArgument;
100105
J.Binary.Type operator = binary.getOperator();
101106
return operator.equals(J.Binary.Type.Equal);
102-
103-
104107
}
105108
};
106109
}

src/main/java/org/openrewrite/java/testing/cleanup/AssertTrueNullToAssertNull.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,14 @@ private boolean isEqualBinary(J.MethodInvocation method) {
104104
return false;
105105
}
106106

107-
J.Binary binary = (J.Binary) method.getArguments().get(0);
107+
final Expression firstArgument = method.getArguments().get(0);
108+
if (!(firstArgument instanceof J.Binary)) {
109+
return false;
110+
}
111+
112+
J.Binary binary = (J.Binary) firstArgument;
108113
J.Binary.Type operator = binary.getOperator();
109114
return operator.equals(J.Binary.Type.Equal);
110-
111-
112115
}
113116
};
114117
}

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ tags:
2424
recipeList:
2525
- org.openrewrite.java.testing.junit5.StaticImports
2626
- org.openrewrite.java.testing.junit5.JUnit4to5Migration
27-
- org.openrewrite.java.testing.cleanup.AssertEqualsNullToAssertNull
28-
- org.openrewrite.java.testing.cleanup.AssertFalseEqualsToAssertNotEquals
29-
- org.openrewrite.java.testing.cleanup.AssertTrueEqualsToAssertEquals
27+
- org.openrewrite.java.testing.junit5.CleanupAssertions
3028
---
3129
type: specs.openrewrite.org/v1beta/recipe
3230
name: org.openrewrite.java.testing.junit5.StaticImports
@@ -174,4 +172,19 @@ recipeList:
174172
artifactId: vertx-junit5
175173
version: 4.x
176174
onlyIfUsing: io.vertx.junit5.VertxExtension
177-
175+
---
176+
type: specs.openrewrite.org/v1beta/recipe
177+
name: org.openrewrite.java.testing.junit5.CleanupAssertions
178+
displayName: Clean Up Assertions
179+
description: Simplifies JUnit Jupiter assertions to their most-direct equivalents
180+
tags:
181+
- testing
182+
- junit
183+
recipeList:
184+
- org.openrewrite.java.testing.cleanup.AssertTrueNegationToAssertFalse
185+
- org.openrewrite.java.testing.cleanup.AssertFalseNegationToAssertTrue
186+
- org.openrewrite.java.testing.cleanup.AssertTrueEqualsToAssertEquals
187+
- org.openrewrite.java.testing.cleanup.AssertTrueComparisonToAssertEquals
188+
- org.openrewrite.java.testing.cleanup.AssertFalseEqualsToAssertNotEquals
189+
- org.openrewrite.java.testing.cleanup.AssertEqualsNullToAssertNull
190+
- org.openrewrite.java.testing.cleanup.AssertFalseNullToAssertNotNull
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Copyright 2021 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.junit5
17+
18+
import org.junit.jupiter.api.Test
19+
import org.openrewrite.config.Environment
20+
import org.openrewrite.java.JavaParser
21+
import org.openrewrite.test.RecipeSpec
22+
import org.openrewrite.test.RewriteTest
23+
24+
class CleanupAssertionsTest : RewriteTest {
25+
override fun defaults(spec: RecipeSpec) {
26+
spec.recipe(Environment.builder()
27+
.scanRuntimeClasspath("org.openrewrite.java.testing.junit5")
28+
.build()
29+
.activateRecipes("org.openrewrite.java.testing.junit5.CleanupAssertions"))
30+
.parser(JavaParser.fromJavaVersion()
31+
.classpath("junit-jupiter-api")
32+
.build())
33+
}
34+
35+
@Test
36+
fun assertTrueComparisonNullToAssertNull() = rewriteRun(java(
37+
"""
38+
import org.junit.jupiter.api.Assertions;
39+
import org.junit.jupiter.api.Test;
40+
41+
class ExampleTest {
42+
@Test
43+
void test() {
44+
Assertions.assertTrue("" == null);
45+
}
46+
}
47+
""", """
48+
import org.junit.jupiter.api.Assertions;
49+
import org.junit.jupiter.api.Test;
50+
51+
class ExampleTest {
52+
@Test
53+
void test() {
54+
Assertions.assertNull("");
55+
}
56+
}
57+
"""
58+
))
59+
60+
@Test
61+
fun assertFalseNegatedEqualsToAssertEquals() = rewriteRun(java(
62+
"""
63+
import org.junit.jupiter.api.Assertions;
64+
import org.junit.jupiter.api.Test;
65+
66+
class ExampleTest {
67+
@Test
68+
void test() {
69+
Assertions.assertFalse(!"".equals(""));
70+
}
71+
}
72+
""", """
73+
import org.junit.jupiter.api.Assertions;
74+
import org.junit.jupiter.api.Test;
75+
76+
class ExampleTest {
77+
@Test
78+
void test() {
79+
Assertions.assertEquals("", "");
80+
}
81+
}
82+
"""
83+
))
84+
85+
@Test
86+
fun assertFalseNegatedEqualsNullToAssertNull() = rewriteRun(java(
87+
"""
88+
import org.junit.jupiter.api.Assertions;
89+
import org.junit.jupiter.api.Test;
90+
91+
class ExampleTest {
92+
@Test
93+
void test() {
94+
Assertions.assertFalse(!"".equals(null));
95+
}
96+
}
97+
""", """
98+
import org.junit.jupiter.api.Assertions;
99+
import org.junit.jupiter.api.Test;
100+
101+
class ExampleTest {
102+
@Test
103+
void test() {
104+
Assertions.assertNull("");
105+
}
106+
}
107+
"""
108+
))
109+
}

0 commit comments

Comments
 (0)