|
| 1 | +/* |
| 2 | + * Copyright 2025 the original author or authors. |
| 3 | + * <p> |
| 4 | + * Licensed under the Moderne Source Available License (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://docs.moderne.io/licensing/moderne-source-available-license |
| 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.testcontainers; |
| 17 | + |
| 18 | +import org.openrewrite.ExecutionContext; |
| 19 | +import org.openrewrite.Preconditions; |
| 20 | +import org.openrewrite.Recipe; |
| 21 | +import org.openrewrite.TreeVisitor; |
| 22 | +import org.openrewrite.internal.ListUtils; |
| 23 | +import org.openrewrite.java.JavaIsoVisitor; |
| 24 | +import org.openrewrite.java.JavaParser; |
| 25 | +import org.openrewrite.java.JavaTemplate; |
| 26 | +import org.openrewrite.java.search.UsesType; |
| 27 | +import org.openrewrite.java.service.AnnotationService; |
| 28 | +import org.openrewrite.java.tree.J; |
| 29 | +import org.openrewrite.java.tree.TypeUtils; |
| 30 | + |
| 31 | +import static java.util.Comparator.comparing; |
| 32 | + |
| 33 | +/** |
| 34 | + * An OpenRewrite recipe that migrates JUnit 4 Testcontainers {@code @Rule} or {@code @ClassRule} |
| 35 | + * fields to JUnit 5's {@code @Container} and adds the {@code @Testcontainers} annotation to the |
| 36 | + * class if necessary. |
| 37 | + */ |
| 38 | +public class AddTestcontainersAnnotations extends Recipe { |
| 39 | + private static final String CLASS_RULE_FQN = "org.junit.ClassRule"; |
| 40 | + private static final String RULE_FQN = "org.junit.Rule"; |
| 41 | + private static final String GENERIC_CONTAINER_FQN = "org.testcontainers.containers.GenericContainer"; |
| 42 | + private static final String TESTCONTAINERS_FQN = "org.testcontainers.junit.jupiter.Testcontainers"; |
| 43 | + private static final String CONTAINER_FQN = "org.testcontainers.junit.jupiter.Container"; |
| 44 | + |
| 45 | + @Override |
| 46 | + public String getDisplayName() { |
| 47 | + return "Adopt `@Container` and add `@Testcontainers`"; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public String getDescription() { |
| 52 | + return "Convert Testcontainers `@Rule`/`@ClassRule` to JUnit 5 `@Container` and add `@Testcontainers`."; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public TreeVisitor<?, ExecutionContext> getVisitor() { |
| 57 | + TreeVisitor<?, ExecutionContext> usesRule = Preconditions.or( |
| 58 | + new UsesType<>(RULE_FQN, true), |
| 59 | + new UsesType<>(CLASS_RULE_FQN, true) |
| 60 | + ); |
| 61 | + return Preconditions.check(usesRule, new JavaIsoVisitor<ExecutionContext>() { |
| 62 | + @Override |
| 63 | + public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDeclaration, ExecutionContext ctx) { |
| 64 | + J.ClassDeclaration cd = super.visitClassDeclaration(classDeclaration, ctx); |
| 65 | + if (classDeclaration == cd) { |
| 66 | + return cd; |
| 67 | + } |
| 68 | + |
| 69 | + maybeRemoveImport(RULE_FQN); |
| 70 | + maybeRemoveImport(CLASS_RULE_FQN); |
| 71 | + |
| 72 | + if (service(AnnotationService.class).isAnnotatedWith(cd, TESTCONTAINERS_FQN)) { |
| 73 | + return cd; |
| 74 | + } |
| 75 | + |
| 76 | + // Add class level annotation |
| 77 | + maybeAddImport(TESTCONTAINERS_FQN); |
| 78 | + return JavaTemplate.builder("@Testcontainers") |
| 79 | + .imports(TESTCONTAINERS_FQN) |
| 80 | + .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "testcontainers-1", "junit-jupiter-1")) |
| 81 | + .build() |
| 82 | + .apply(updateCursor(cd), cd.getCoordinates().addAnnotation(comparing(J.Annotation::getSimpleName))); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations varDecls, ExecutionContext ctx) { |
| 87 | + if (!TypeUtils.isAssignableTo(GENERIC_CONTAINER_FQN, varDecls.getType())) { |
| 88 | + return varDecls; |
| 89 | + } |
| 90 | + if (!service(AnnotationService.class).isAnnotatedWith(varDecls, RULE_FQN) && |
| 91 | + !service(AnnotationService.class).isAnnotatedWith(varDecls, CLASS_RULE_FQN)) { |
| 92 | + return varDecls; |
| 93 | + } |
| 94 | + |
| 95 | + // Remove ClassRule/Rule annotations |
| 96 | + J.VariableDeclarations vd = varDecls.withLeadingAnnotations(ListUtils.filter(varDecls.getLeadingAnnotations(), |
| 97 | + ann -> !TypeUtils.isAssignableTo(RULE_FQN, ann.getType()) && |
| 98 | + !TypeUtils.isAssignableTo(CLASS_RULE_FQN, ann.getType()))); |
| 99 | + if (vd == varDecls || service(AnnotationService.class).isAnnotatedWith(varDecls, CONTAINER_FQN)) { |
| 100 | + return vd; |
| 101 | + } |
| 102 | + |
| 103 | + // Add field level annotation |
| 104 | + maybeAddImport(CONTAINER_FQN); |
| 105 | + return JavaTemplate.builder("@Container") |
| 106 | + .imports(CONTAINER_FQN) |
| 107 | + .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "testcontainers-1", "junit-jupiter-1")) |
| 108 | + .build() |
| 109 | + .apply(updateCursor(vd), vd.getCoordinates().addAnnotation(comparing(J.Annotation::getSimpleName))); |
| 110 | + } |
| 111 | + }); |
| 112 | + } |
| 113 | + |
| 114 | +} |
0 commit comments