Skip to content

Commit

Permalink
Drop duplication between EnclosedToNested and AddMissingNested
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed Oct 21, 2024
1 parent aa80a5c commit 6dd020c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,15 @@
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.*;
import org.openrewrite.java.search.FindAnnotations;
import org.openrewrite.java.search.UsesType;
import org.openrewrite.java.tree.J;

import java.util.Comparator;

@Value
@EqualsAndHashCode(callSuper = false)
public class EnclosedToNested extends Recipe {
private static final String ENCLOSED = "org.junit.experimental.runners.Enclosed";
private static final String RUN_WITH = "org.junit.runner.RunWith";
private static final String NESTED = "org.junit.jupiter.api.Nested";
private static final String TEST_JUNIT4 = "org.junit.Test";
private static final String TEST_JUNIT_JUPITER = "org.junit.jupiter.api.Test";
private static final String RUN_WITH_ENCLOSED = String.format("@%s(%s.class)", RUN_WITH, ENCLOSED);

@Override
public String getDisplayName() {
Expand All @@ -44,49 +39,19 @@ public String getDisplayName() {

@Override
public String getDescription() {
return "Removes the `Enclosed` specification from a class, and adds `Nested` to its inner classes.";
return "Removes the `Enclosed` specification from a class, with `Nested` added to its inner classes by `AddMissingNested`.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(new UsesType<>(ENCLOSED, false), new JavaIsoVisitor<ExecutionContext>() {
@SuppressWarnings("ConstantConditions")
@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) {
J.ClassDeclaration cd = super.visitClassDeclaration(classDecl, ctx);
String runwithEnclosed = String.format("@%s(%s.class)", RUN_WITH, ENCLOSED);
if (!FindAnnotations.find(cd.withBody(null), runwithEnclosed).isEmpty()) {
cd = (J.ClassDeclaration) new RemoveAnnotationVisitor(new AnnotationMatcher(runwithEnclosed)).visit(cd, ctx);
cd = cd.withBody((J.Block) new AddNestedAnnotationVisitor().visit(cd.getBody(), ctx, updateCursor(cd)));

maybeRemoveImport(ENCLOSED);
maybeRemoveImport(RUN_WITH);
maybeAddImport(NESTED);
}
return cd;
maybeRemoveImport(ENCLOSED);
maybeRemoveImport(RUN_WITH);
return (J.ClassDeclaration) new RemoveAnnotationVisitor(new AnnotationMatcher(RUN_WITH_ENCLOSED)).visitNonNull(cd, ctx);
}
});
}

public static class AddNestedAnnotationVisitor extends JavaIsoVisitor<ExecutionContext> {
@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) {
J.ClassDeclaration cd = super.visitClassDeclaration(classDecl, ctx);
if (hasTestMethods(cd)) {
cd = JavaTemplate.builder("@Nested")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "junit-jupiter-api-5.9"))
.imports(NESTED)
.build()
.apply(getCursor(), cd.getCoordinates().addAnnotation(Comparator.comparing(J.Annotation::getSimpleName)));
cd.getModifiers().removeIf(modifier -> modifier.getType() == J.Modifier.Type.Static);
return maybeAutoFormat(classDecl, cd, ctx);
}
return cd;
}

private boolean hasTestMethods(final J.ClassDeclaration cd) {
return !FindAnnotations.find(cd, "@" + TEST_JUNIT4).isEmpty() ||
!FindAnnotations.find(cd, "@" + TEST_JUNIT_JUPITER).isEmpty();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void defaults(RecipeSpec spec) {
spec
.parser(JavaParser.fromJavaVersion()
.classpathFromResources(new InMemoryExecutionContext(), "junit-4.13"))
.recipe(new EnclosedToNested());
.recipeFromResources("org.openrewrite.java.testing.junit5.JUnit4to5Migration");
}

@DocumentExample
Expand All @@ -55,8 +55,8 @@ public void test() {
}
""",
"""
import org.junit.Test;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
public class RootTest {
@Nested
Expand Down Expand Up @@ -103,8 +103,8 @@ public void test() {
}
""",
"""
import org.junit.Test;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
public class RootTest {
@Nested
Expand Down Expand Up @@ -153,13 +153,17 @@ public void test() {
}
""",
"""
import org.junit.Test;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import java.util.concurrent.TimeUnit;
public class RootTest {
@Nested
public class InnerTest {
@Test(timeout = 10)
@Test
@Timeout(value = 10, unit = TimeUnit.MILLISECONDS)
public void test() {
}
}
Expand Down Expand Up @@ -194,8 +198,8 @@ public void bar() {
}
""",
"""
import org.junit.Test;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
public class RootTest {
@Nested
Expand Down

0 comments on commit 6dd020c

Please sign in to comment.