Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@
import org.openrewrite.marker.Markers;

import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import static java.util.Arrays.asList;

public class MigrateJUnitTestCase extends Recipe {

Expand Down Expand Up @@ -101,6 +106,8 @@ public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionCon
private static class TestCaseVisitor extends JavaIsoVisitor<ExecutionContext> {
private static final AnnotationMatcher OVERRIDE_ANNOTATION_MATCHER = new AnnotationMatcher("@java.lang.Override");
private static final MethodMatcher TEST_CASE_SUPER_MATCHER = new MethodMatcher("junit.framework.TestCase <constructor>(..)");
private static final Set<String> SUPERTYPES_REMOVED_BY_MIGRATION = new HashSet<>(asList(
"junit.framework.TestCase", "junit.framework.Assert", "junit.framework.Test"));

@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) {
Expand Down Expand Up @@ -148,7 +155,7 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
} else if ("tearDown".equals(md.getSimpleName()) && md.getLeadingAnnotations().stream().noneMatch(JUNIT_AFTER_ANNOTATION_MATCHER::matches)) {
md = updateMethodDeclarationAnnotationAndModifier(md, "@AfterEach", "org.junit.jupiter.api.AfterEach", ctx);
}
return md;
return maybeRemoveOverrideAnnotation(md, ctx);
}

@Override
Expand All @@ -171,7 +178,6 @@ private J.MethodDeclaration updateMethodDeclarationAnnotationAndModifier(J.Metho
.imports(fullyQualifiedAnnotation).build()
.apply(getCursor(), methodDeclaration.getCoordinates().addAnnotation(Comparator.comparing(J.Annotation::getSimpleName)));
md = maybeAddPublicModifier(md);
md = maybeRemoveOverrideAnnotation(md);
maybeAddImport(fullyQualifiedAnnotation);
}
return md;
Expand All @@ -187,14 +193,27 @@ private J.MethodDeclaration maybeAddPublicModifier(J.MethodDeclaration md) {
return md.withModifiers(modifiers);
}

private J.MethodDeclaration maybeRemoveOverrideAnnotation(J.MethodDeclaration md) {
return md.withLeadingAnnotations(ListUtils.map(md.getLeadingAnnotations(), annotation -> {
if (OVERRIDE_ANNOTATION_MATCHER.matches(annotation)) {
//noinspection DataFlowIssue
return null;
private J.MethodDeclaration maybeRemoveOverrideAnnotation(J.MethodDeclaration md, ExecutionContext ctx) {
JavaType.Method methodType = md.getMethodType();
if (methodType == null ||
md.getLeadingAnnotations().stream().noneMatch(OVERRIDE_ANNOTATION_MATCHER::matches) ||
stillOverridesAfterMigration(methodType)) {
return md;
}
J.MethodDeclaration withoutBody = (J.MethodDeclaration) new RemoveAnnotationVisitor(OVERRIDE_ANNOTATION_MATCHER)
.visitNonNull(md.withBody(null), ctx, getCursor().getParentOrThrow());
return withoutBody.withBody(md.getBody());
}

private static boolean stillOverridesAfterMigration(JavaType.Method method) {
Optional<JavaType.Method> overridden = TypeUtils.findOverriddenMethod(method);
while (overridden.isPresent()) {
if (!SUPERTYPES_REMOVED_BY_MIGRATION.contains(overridden.get().getDeclaringType().getFullyQualifiedName())) {
return true;
}
return annotation;
}));
overridden = TypeUtils.findOverriddenMethod(overridden.get());
}
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1025,4 +1025,65 @@ public void shouldPass() {
)
);
}

@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/1103")
@Test
void removeOverrideOnMigratedTestCaseMethods() {
rewriteRun(
//language=java
java(
"""
import junit.framework.TestCase;

import org.junit.Before;
import org.junit.Test;

public class MathTest extends TestCase {
protected long value1;

@Override
@Before
public void setUp() {
value1 = 2;
}

@Override
public String toString() {
return "math";
}

@Test
public void testAdd() {
assertEquals(2, value1);
}
}
""",
"""
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class MathTest {
protected long value1;

@BeforeEach
public void setUp() {
value1 = 2;
}

@Override
public String toString() {
return "math";
}

@Test
public void testAdd() {
assertEquals(2, value1);
}
}
"""
)
);
}
}
Loading
Loading