diff --git a/src/main/java/org/openrewrite/java/testing/junit5/EnclosedToNested.java b/src/main/java/org/openrewrite/java/testing/junit5/EnclosedToNested.java index c4ff6cc5c..861244fc7 100644 --- a/src/main/java/org/openrewrite/java/testing/junit5/EnclosedToNested.java +++ b/src/main/java/org/openrewrite/java/testing/junit5/EnclosedToNested.java @@ -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() { @@ -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 getVisitor() { return Preconditions.check(new UsesType<>(ENCLOSED, false), new JavaIsoVisitor() { - @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 { - @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(); - } - } } diff --git a/src/test/java/org/openrewrite/java/testing/junit5/EnclosedToNestedTest.java b/src/test/java/org/openrewrite/java/testing/junit5/EnclosedToNestedTest.java index e7b45842a..1d5cc7f96 100644 --- a/src/test/java/org/openrewrite/java/testing/junit5/EnclosedToNestedTest.java +++ b/src/test/java/org/openrewrite/java/testing/junit5/EnclosedToNestedTest.java @@ -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 @@ -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 @@ -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 @@ -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() { } } @@ -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