-
Notifications
You must be signed in to change notification settings - Fork 101
Convert Testcontainers @Rule/@ClassRule to JUnit 5 @Container and add @Testcontainers
#838
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
02cb41e
Add recipe
d9c62d3
Apply suggestions
timtebeek fb4aa7f
Regenerate type table
timtebeek cb9efb3
Merge remote-tracking branch 'origin/main' into fork/jc65536/jason/te…
timtebeek 41fb086
Regenerate type table
timtebeek eca478f
Use `classpathFromResources`
timtebeek ea2384d
Polish
timtebeek d732c2b
Move recipe and include with v2 upgrade
timtebeek 15bea88
Merge remote-tracking branch 'origin/main' into fork/jc65536/jason/te…
timtebeek 233bb83
Recreate type table
timtebeek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
src/main/java/org/openrewrite/java/testing/junit5/AddTestcontainersAnnotations.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| /* | ||
| * Copyright 2025 the original author or authors. | ||
| * <p> | ||
| * Licensed under the Moderne Source Available License (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * <p> | ||
| * https://docs.moderne.io/licensing/moderne-source-available-license | ||
| * <p> | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.openrewrite.java.testing.junit5; | ||
|
|
||
| import java.util.Comparator; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import org.openrewrite.ExecutionContext; | ||
|
timtebeek marked this conversation as resolved.
Outdated
|
||
| import org.openrewrite.Recipe; | ||
| import org.openrewrite.TreeVisitor; | ||
| import org.openrewrite.java.JavaIsoVisitor; | ||
| import org.openrewrite.java.JavaParser; | ||
| import org.openrewrite.java.JavaTemplate; | ||
| import org.openrewrite.java.tree.J; | ||
| import org.openrewrite.java.tree.TypeUtils; | ||
|
|
||
| /** | ||
| * An OpenRewrite recipe that migrates JUnit 4 Testcontainers {@code @Rule} or {@code @ClassRule} | ||
| * fields to JUnit 5's {@code @Container} and adds the {@code @Testcontainers} annotation to the | ||
| * class if necessary. | ||
| */ | ||
| public class AddTestcontainersAnnotations extends Recipe { | ||
| @Override | ||
| public String getDisplayName() { | ||
| return "Handle the usage of GenericContainer rules"; | ||
| } | ||
|
|
||
| @Override | ||
| public String getDescription() { | ||
| return "Handles the usage of GenericContainer rules by adding the @Container and @Testcontainers annotations."; | ||
| } | ||
|
|
||
| @Override | ||
| public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
| return new TestcontainersAnnotationsVisitor(); | ||
|
timtebeek marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| private static class TestcontainersAnnotationsVisitor extends JavaIsoVisitor<ExecutionContext> { | ||
| private static final String CLASS_RULE_FQN = "org.junit.ClassRule"; | ||
| private static final String RULE_FQN = "org.junit.Rule"; | ||
| private static final String GENERIC_CONTAINER_FQN = "org.testcontainers.containers.GenericContainer"; | ||
| private static final String TESTCONTAINERS_FQN = "org.testcontainers.junit.jupiter.Testcontainers"; | ||
| private static final String CONTAINER_FQN = "org.testcontainers.junit.jupiter.Container"; | ||
| private static final String[] CLASSPATH = {"testcontainers", "junit-jupiter"}; | ||
|
timtebeek marked this conversation as resolved.
Outdated
|
||
|
|
||
| private static final JavaTemplate CONTAINER_ANNOTATION_TEMPLATE = JavaTemplate.builder("@Container") | ||
| .imports(CONTAINER_FQN) | ||
| .javaParser(JavaParser.fromJavaVersion().classpath(CLASSPATH)) | ||
| .build(); | ||
|
|
||
| private static final JavaTemplate TESTCONTAINERS_ANNOTATION_TEMPLATE = JavaTemplate.builder("@Testcontainers") | ||
| .imports(TESTCONTAINERS_FQN) | ||
| .javaParser(JavaParser.fromJavaVersion().classpath(CLASSPATH)) | ||
| .build(); | ||
|
|
||
| private static boolean isRule(J.VariableDeclarations varDecls) { | ||
| return varDecls.getLeadingAnnotations().stream() | ||
| .map(J.Annotation::getType) | ||
| .anyMatch(t -> TypeUtils.isAssignableTo(RULE_FQN, t) || TypeUtils.isAssignableTo(CLASS_RULE_FQN, t)); | ||
| } | ||
|
|
||
| @Override | ||
| public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) { | ||
| classDecl = super.visitClassDeclaration(classDecl, ctx); | ||
|
|
||
| if (!getCursor().getMessage("hasContainerRule", false)) { | ||
| return classDecl; | ||
| } | ||
|
|
||
| maybeRemoveImport(RULE_FQN); | ||
| maybeRemoveImport(CLASS_RULE_FQN); | ||
|
|
||
| boolean alreadyHasTestcontainersAnnotation = classDecl.getLeadingAnnotations().stream() | ||
| .anyMatch(ann -> TypeUtils.isAssignableTo(TESTCONTAINERS_FQN, ann.getType())); | ||
|
|
||
| if (alreadyHasTestcontainersAnnotation) { | ||
| return classDecl; | ||
| } | ||
|
|
||
| maybeAddImport(TESTCONTAINERS_FQN); | ||
|
|
||
| return TESTCONTAINERS_ANNOTATION_TEMPLATE.apply( | ||
| updateCursor(classDecl), | ||
| classDecl.getCoordinates() | ||
| .addAnnotation(Comparator.comparing(J.Annotation::getSimpleName))); | ||
| } | ||
|
|
||
| @Override | ||
| public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations varDecls, ExecutionContext ctx) { | ||
| if (!isRule(varDecls)) { | ||
| return varDecls; | ||
| } | ||
|
|
||
| if (!TypeUtils.isAssignableTo(GENERIC_CONTAINER_FQN, varDecls.getType())) { | ||
| return varDecls; | ||
| } | ||
|
|
||
| // Tell first enclosing ClassDeclaration it needs to add @Testcontainers annotation | ||
| getCursor().putMessageOnFirstEnclosing(J.ClassDeclaration.class, "hasContainerRule", true); | ||
|
|
||
| List<J.Annotation> annotationsToKeep = varDecls.getLeadingAnnotations().stream() | ||
| .filter(ann -> !TypeUtils.isAssignableTo(RULE_FQN, ann.getType())) | ||
| .filter(ann -> !TypeUtils.isAssignableTo(CLASS_RULE_FQN, ann.getType())) | ||
| .collect(Collectors.toList()); | ||
|
timtebeek marked this conversation as resolved.
Outdated
|
||
|
|
||
| varDecls = varDecls.withLeadingAnnotations(annotationsToKeep); | ||
|
|
||
| boolean alreadyHasContainerAnnotation = annotationsToKeep.stream() | ||
| .anyMatch(ann -> TypeUtils.isAssignableTo(CONTAINER_FQN, ann.getType())); | ||
|
|
||
| if (alreadyHasContainerAnnotation) { | ||
| return varDecls; | ||
| } | ||
|
|
||
| maybeAddImport(CONTAINER_FQN); | ||
|
|
||
| varDecls = CONTAINER_ANNOTATION_TEMPLATE.apply( | ||
| updateCursor(varDecls), | ||
| varDecls.getCoordinates() | ||
|
timtebeek marked this conversation as resolved.
Outdated
|
||
| .addAnnotation(Comparator.comparing(J.Annotation::getSimpleName))); | ||
|
|
||
| return varDecls; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.