Skip to content

Commit e85e34d

Browse files
committed
Clean up leftover calls to TestCase.super()
1 parent 2bed89d commit e85e34d

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/main/java/org/openrewrite/java/testing/junit5/MigrateJUnitTestCase.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionCon
8383

8484
@SuppressWarnings("ConstantConditions")
8585
@Override
86-
public J.@Nullable MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
86+
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
8787
J.MethodInvocation mi = super.visitMethodInvocation(method, ctx);
8888
if ((mi.getSelect() != null && TypeUtils.isOfClassType(mi.getSelect().getType(), "junit.framework.TestCase")) ||
8989
(mi.getMethodType() != null && TypeUtils.isOfClassType(mi.getMethodType().getDeclaringType(), "junit.framework.TestCase"))) {
@@ -103,6 +103,7 @@ public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionCon
103103

104104
private static class TestCaseVisitor extends JavaIsoVisitor<ExecutionContext> {
105105
private static final AnnotationMatcher OVERRIDE_ANNOTATION_MATCHER = new AnnotationMatcher("@java.lang.Override");
106+
private static final MethodMatcher TEST_CASE_SUPER_MATCHER = new MethodMatcher("junit.framework.TestCase <constructor>(..)");
106107

107108
@Override
108109
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) {
@@ -134,6 +135,17 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
134135
return md;
135136
}
136137

138+
@Override
139+
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
140+
// If the class no longer extends TestCase there should no longer be calls to TestCase.super()
141+
// Plenty of edge cases around classes which extend classes which extend TestCase this doesn't account for
142+
if (TEST_CASE_SUPER_MATCHER.matches(method)) {
143+
//noinspection DataFlowIssue
144+
return null;
145+
}
146+
return super.visitMethodInvocation(method, ctx);
147+
}
148+
137149
private J.MethodDeclaration updateMethodDeclarationAnnotationAndModifier(J.MethodDeclaration methodDeclaration, String annotation, String fullyQualifiedAnnotation, ExecutionContext ctx) {
138150
J.MethodDeclaration md = methodDeclaration;
139151
if (FindAnnotations.find(methodDeclaration.withBody(null), "@" + fullyQualifiedAnnotation).isEmpty()) {
@@ -162,6 +174,7 @@ private J.MethodDeclaration maybeAddPublicModifier(J.MethodDeclaration md) {
162174
private J.MethodDeclaration maybeRemoveOverrideAnnotation(J.MethodDeclaration md) {
163175
return md.withLeadingAnnotations(ListUtils.map(md.getLeadingAnnotations(), annotation -> {
164176
if (OVERRIDE_ANNOTATION_MATCHER.matches(annotation)) {
177+
//noinspection DataFlowIssue
165178
return null;
166179
}
167180
return annotation;

src/test/java/org/openrewrite/java/testing/junit5/MigrateJUnitTestCaseTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,4 +337,28 @@ public void tearDown() {
337337
)
338338
);
339339
}
340+
341+
@Test
342+
void testCaseWithConstructorCallingSuperTestName() {
343+
//language=java
344+
rewriteRun(
345+
java(
346+
"""
347+
import junit.framework.TestCase;
348+
349+
public class AppTest extends TestCase {
350+
public AppTest(String testName) {
351+
super(testName);
352+
}
353+
}
354+
""",
355+
"""
356+
public class AppTest {
357+
public AppTest(String testName) {
358+
}
359+
}
360+
"""
361+
)
362+
);
363+
}
340364
}

0 commit comments

Comments
 (0)