-
Notifications
You must be signed in to change notification settings - Fork 96
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 all commits
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
114 changes: 114 additions & 0 deletions
114
src/main/java/org/openrewrite/java/testing/testcontainers/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,114 @@ | ||
| /* | ||
| * 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.testcontainers; | ||
|
|
||
| import org.openrewrite.ExecutionContext; | ||
| import org.openrewrite.Preconditions; | ||
| import org.openrewrite.Recipe; | ||
| import org.openrewrite.TreeVisitor; | ||
| import org.openrewrite.internal.ListUtils; | ||
| import org.openrewrite.java.JavaIsoVisitor; | ||
| import org.openrewrite.java.JavaParser; | ||
| import org.openrewrite.java.JavaTemplate; | ||
| import org.openrewrite.java.search.UsesType; | ||
| import org.openrewrite.java.service.AnnotationService; | ||
| import org.openrewrite.java.tree.J; | ||
| import org.openrewrite.java.tree.TypeUtils; | ||
|
|
||
| import static java.util.Comparator.comparing; | ||
|
|
||
| /** | ||
timtebeek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * 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 { | ||
| 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"; | ||
|
|
||
| @Override | ||
| public String getDisplayName() { | ||
| return "Adopt `@Container` and add `@Testcontainers`"; | ||
| } | ||
|
|
||
| @Override | ||
| public String getDescription() { | ||
| return "Convert Testcontainers `@Rule`/`@ClassRule` to JUnit 5 `@Container` and add `@Testcontainers`."; | ||
| } | ||
|
|
||
| @Override | ||
| public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
| TreeVisitor<?, ExecutionContext> usesRule = Preconditions.or( | ||
| new UsesType<>(RULE_FQN, true), | ||
| new UsesType<>(CLASS_RULE_FQN, true) | ||
| ); | ||
| return Preconditions.check(usesRule, new JavaIsoVisitor<ExecutionContext>() { | ||
| @Override | ||
| public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDeclaration, ExecutionContext ctx) { | ||
| J.ClassDeclaration cd = super.visitClassDeclaration(classDeclaration, ctx); | ||
| if (classDeclaration == cd) { | ||
| return cd; | ||
| } | ||
|
|
||
| maybeRemoveImport(RULE_FQN); | ||
| maybeRemoveImport(CLASS_RULE_FQN); | ||
|
|
||
| if (service(AnnotationService.class).isAnnotatedWith(cd, TESTCONTAINERS_FQN)) { | ||
| return cd; | ||
| } | ||
|
|
||
| // Add class level annotation | ||
| maybeAddImport(TESTCONTAINERS_FQN); | ||
| return JavaTemplate.builder("@Testcontainers") | ||
| .imports(TESTCONTAINERS_FQN) | ||
| .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "testcontainers-1", "junit-jupiter-1")) | ||
| .build() | ||
| .apply(updateCursor(cd), cd.getCoordinates().addAnnotation(comparing(J.Annotation::getSimpleName))); | ||
| } | ||
|
|
||
| @Override | ||
| public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations varDecls, ExecutionContext ctx) { | ||
| if (!TypeUtils.isAssignableTo(GENERIC_CONTAINER_FQN, varDecls.getType())) { | ||
| return varDecls; | ||
| } | ||
| if (!service(AnnotationService.class).isAnnotatedWith(varDecls, RULE_FQN) && | ||
| !service(AnnotationService.class).isAnnotatedWith(varDecls, CLASS_RULE_FQN)) { | ||
| return varDecls; | ||
| } | ||
|
|
||
| // Remove ClassRule/Rule annotations | ||
| J.VariableDeclarations vd = varDecls.withLeadingAnnotations(ListUtils.filter(varDecls.getLeadingAnnotations(), | ||
| ann -> !TypeUtils.isAssignableTo(RULE_FQN, ann.getType()) && | ||
| !TypeUtils.isAssignableTo(CLASS_RULE_FQN, ann.getType()))); | ||
| if (vd == varDecls || service(AnnotationService.class).isAnnotatedWith(varDecls, CONTAINER_FQN)) { | ||
| return vd; | ||
| } | ||
|
|
||
| // Add field level annotation | ||
| maybeAddImport(CONTAINER_FQN); | ||
| return JavaTemplate.builder("@Container") | ||
| .imports(CONTAINER_FQN) | ||
| .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "testcontainers-1", "junit-jupiter-1")) | ||
| .build() | ||
| .apply(updateCursor(vd), vd.getCoordinates().addAnnotation(comparing(J.Annotation::getSimpleName))); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| } | ||
Binary file not shown.
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
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.