Skip to content

Commit a428327

Browse files
authored
Assert instance of migration (#372)
* Migration from obsolete assertTrue(instanceof) Added recipe which migrates JUnit4 (or JUnit5) assertTrue(x instanceof y) into JUnit5 assertInstanceOf(y.class, x) Signed-off-by: matus.matok <[email protected]> * Migration from obsolete assertTrue(instanceof) Added recipe which migrates JUnit4 (or JUnit5) assertTrue(x instanceof y) into JUnit5 assertInstanceOf(y.class, x) Signed-off-by: matus.matok <[email protected]> * Fix method matchers Fixed method matchers to work properly. Signed-off-by: matus.matok <[email protected]> --------- Signed-off-by: matus.matok <[email protected]>
1 parent 6e9cdeb commit a428327

File tree

2 files changed

+292
-0
lines changed

2 files changed

+292
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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.junit5;
17+
18+
import org.openrewrite.ExecutionContext;
19+
import org.openrewrite.Recipe;
20+
import org.openrewrite.TreeVisitor;
21+
import org.openrewrite.java.JavaIsoVisitor;
22+
import org.openrewrite.java.JavaParser;
23+
import org.openrewrite.java.JavaTemplate;
24+
import org.openrewrite.java.MethodMatcher;
25+
import org.openrewrite.java.tree.Expression;
26+
import org.openrewrite.java.tree.J;
27+
28+
public class AssertTrueInstanceofToAssertInstanceOf extends Recipe {
29+
@Override
30+
public String getDisplayName() {
31+
return "assertTrue(x instanceof y) to assertInstanceOf(y.class, x)";
32+
}
33+
34+
@Override
35+
public String getDescription() {
36+
return "Migration of JUnit4 (or potentially JUnit5) test case in form of assertTrue(x instanceof y) to assertInstanceOf(y.class, x).";
37+
}
38+
39+
public TreeVisitor<?, ExecutionContext> getVisitor() {
40+
return new JavaIsoVisitor<ExecutionContext>() {
41+
42+
@Override
43+
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext executionContext) {
44+
J.MethodInvocation mi = super.visitMethodInvocation(method, executionContext);
45+
MethodMatcher junit5Matcher = new MethodMatcher("org.junit.jupiter.api.Assertions assertTrue(boolean, ..)");
46+
MethodMatcher junit4Matcher = new MethodMatcher("org.junit.Assert assertTrue(.., boolean)");
47+
48+
J clazz;
49+
Expression expression;
50+
Expression reason;
51+
52+
if (junit5Matcher.matches(mi)) {
53+
System.out.println("matched");
54+
maybeRemoveImport("org.junit.jupiter.api.Assertions.assertTrue");
55+
Expression argument = mi.getArguments().get(0);
56+
if (mi.getArguments().size() == 1) {
57+
reason = null;
58+
} else if (mi.getArguments().size() == 2) {
59+
reason = mi.getArguments().get(1);
60+
} else return mi;
61+
62+
if (argument instanceof J.InstanceOf) {
63+
J.InstanceOf instanceOf = (J.InstanceOf) argument;
64+
expression = instanceOf.getExpression();
65+
clazz = instanceOf.getClazz();
66+
} else {
67+
return mi;
68+
}
69+
} else if (junit4Matcher.matches(mi)) {
70+
maybeRemoveImport("org.junit.Assert.assertTrue");
71+
Expression argument;
72+
if (mi.getArguments().size() == 1) {
73+
reason = null;
74+
argument = mi.getArguments().get(0);
75+
} else if (mi.getArguments().size() == 2) {
76+
reason = mi.getArguments().get(0);
77+
argument = mi.getArguments().get(1);
78+
} else return mi;
79+
80+
if (argument instanceof J.InstanceOf) {
81+
J.InstanceOf instanceOf = (J.InstanceOf) argument;
82+
expression = instanceOf.getExpression();
83+
clazz = instanceOf.getClazz();
84+
} else {
85+
return mi;
86+
}
87+
} else {
88+
return mi;
89+
}
90+
91+
92+
JavaTemplate template = JavaTemplate
93+
.builder("assertInstanceOf(#{}.class, #{any(java.lang.Object)}" + (reason != null ? ", #{any(java.lang.String)})" : ")"))
94+
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(executionContext, "junit-jupiter-api-5.9", "junit-4.13"))
95+
.staticImports("org.junit.jupiter.api.Assertions.assertInstanceOf")
96+
.build();
97+
98+
J.MethodInvocation methodd = reason != null
99+
? template.apply(getCursor(), mi.getCoordinates().replace(), clazz.toString(), expression, reason)
100+
: template.apply(getCursor(), mi.getCoordinates().replace(), clazz.toString(), expression);
101+
maybeAddImport("org.junit.jupiter.api.Assertions", "assertInstanceOf");
102+
return methodd;
103+
}
104+
};
105+
}
106+
}
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
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.junit5;
17+
18+
import org.junit.jupiter.api.Test;
19+
import org.openrewrite.InMemoryExecutionContext;
20+
import org.openrewrite.java.JavaParser;
21+
import org.openrewrite.test.RecipeSpec;
22+
import org.openrewrite.test.RewriteTest;
23+
24+
import static org.openrewrite.java.Assertions.java;
25+
26+
public class AssertTrueInstanceofToAssertInstanceOfTest implements RewriteTest {
27+
@Override
28+
public void defaults(RecipeSpec spec) {
29+
spec
30+
.parser(JavaParser.fromJavaVersion()
31+
.classpathFromResources(new InMemoryExecutionContext(), "junit-jupiter-api-5.9", "junit-4.13"))
32+
.recipe(new AssertTrueInstanceofToAssertInstanceOf());
33+
}
34+
35+
@Test
36+
void testJUnit5() {
37+
//language=java
38+
rewriteRun(
39+
java(
40+
"""
41+
import org.junit.jupiter.api.Test;
42+
import java.util.ArrayList;
43+
import java.util.List;
44+
45+
import static org.junit.jupiter.api.Assertions.assertTrue;
46+
47+
class ATest {
48+
@Test
49+
void testJUnit5() {
50+
List<String> list = new ArrayList<>();
51+
assertTrue(list instanceof Iterable);
52+
}
53+
}
54+
""",
55+
"""
56+
import org.junit.jupiter.api.Test;
57+
import java.util.ArrayList;
58+
import java.util.List;
59+
60+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
61+
62+
class ATest {
63+
@Test
64+
void testJUnit5() {
65+
List<String> list = new ArrayList<>();
66+
assertInstanceOf(Iterable.class, list);
67+
}
68+
}
69+
"""
70+
));
71+
}
72+
73+
@Test
74+
void testJUnit5WithReason() {
75+
//language=java
76+
rewriteRun(
77+
java(
78+
"""
79+
import org.junit.jupiter.api.Test;
80+
import java.util.ArrayList;
81+
import java.util.List;
82+
83+
import static org.junit.jupiter.api.Assertions.assertTrue;
84+
85+
class ATest {
86+
@Test
87+
void testJUnit5() {
88+
List<String> list = new ArrayList<>();
89+
assertTrue(list instanceof Iterable, "Not instance of Iterable");
90+
}
91+
}
92+
""",
93+
"""
94+
import org.junit.jupiter.api.Test;
95+
import java.util.ArrayList;
96+
import java.util.List;
97+
98+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
99+
100+
class ATest {
101+
@Test
102+
void testJUnit5() {
103+
List<String> list = new ArrayList<>();
104+
assertInstanceOf(Iterable.class, list, "Not instance of Iterable");
105+
}
106+
}
107+
"""
108+
));
109+
}
110+
111+
@Test
112+
void testJUnit4() {
113+
//language=java
114+
rewriteRun(
115+
java(
116+
"""
117+
import org.junit.jupiter.api.Test;
118+
import java.util.ArrayList;
119+
import java.util.List;
120+
121+
import static org.junit.Assert.assertTrue;
122+
123+
class ATest {
124+
@Test
125+
void testJUnit5() {
126+
List<String> list = new ArrayList<>();
127+
assertTrue(list instanceof Iterable);
128+
}
129+
}
130+
""",
131+
"""
132+
import org.junit.jupiter.api.Test;
133+
import java.util.ArrayList;
134+
import java.util.List;
135+
136+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
137+
138+
class ATest {
139+
@Test
140+
void testJUnit5() {
141+
List<String> list = new ArrayList<>();
142+
assertInstanceOf(Iterable.class, list);
143+
}
144+
}
145+
"""
146+
));
147+
}
148+
149+
@Test
150+
void testJUnit4WithReason() {
151+
//language=java
152+
rewriteRun(
153+
java(
154+
"""
155+
import org.junit.jupiter.api.Test;
156+
import java.util.ArrayList;
157+
import java.util.List;
158+
159+
import static org.junit.Assert.assertTrue;
160+
161+
class ATest {
162+
@Test
163+
void testJUnit5() {
164+
List<String> list = new ArrayList<>();
165+
assertTrue("Not instance of Iterable", list instanceof Iterable);
166+
}
167+
}
168+
""",
169+
"""
170+
import org.junit.jupiter.api.Test;
171+
import java.util.ArrayList;
172+
import java.util.List;
173+
174+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
175+
176+
class ATest {
177+
@Test
178+
void testJUnit5() {
179+
List<String> list = new ArrayList<>();
180+
assertInstanceOf(Iterable.class, list, "Not instance of Iterable");
181+
}
182+
}
183+
"""
184+
));
185+
}
186+
}

0 commit comments

Comments
 (0)