-
Notifications
You must be signed in to change notification settings - Fork 95
Correcting MockWebServer migration for OkHttp #849
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
Draft
steve-aom-elliott
wants to merge
5
commits into
main
Choose a base branch
from
1589-correcting-mockwebserver-migration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
33e4903
WiP
steve-aom-elliott 9cdc52a
WiP 2 - am able to get `.build()` added, but seems to drop the after …
steve-aom-elliott 0abe5ec
Expanding tests and conversions to include headers methods
steve-aom-elliott 94fd580
Adapted the failing recipe to add as much debug/retry information as …
Jenson3210 58ef5a9
Reapplying paddings from arguments as they were prior to chaining `.b…
steve-aom-elliott 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
Some comments aren't visible on the classic Files Changed page.
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
222 changes: 222 additions & 0 deletions
222
src/main/java/org/openrewrite/java/testing/junit5/UpdateMockWebServerMockResponse.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,222 @@ | ||
| package org.openrewrite.java.testing.junit5; | ||
steve-aom-elliott marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
steve-aom-elliott marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import org.jspecify.annotations.Nullable; | ||
| import org.openrewrite.*; | ||
| import org.openrewrite.java.*; | ||
| import org.openrewrite.java.search.UsesType; | ||
| import org.openrewrite.java.tree.J; | ||
| import org.openrewrite.java.tree.JavaType; | ||
| import org.openrewrite.java.tree.Statement; | ||
| import org.openrewrite.java.tree.TypeUtils; | ||
|
|
||
| import static java.util.Objects.requireNonNull; | ||
steve-aom-elliott marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| public class UpdateMockWebServerMockResponse extends Recipe { | ||
| private static final String OLD_MOCKRESPONSE_FQN = "okhttp3.mockwebserver.MockResponse"; | ||
| private static final MethodMatcher OLD_MOCKRESPONSE_CONSTRUCTOR_MATCHER = new MethodMatcher(OLD_MOCKRESPONSE_FQN + " <constructor>()"); | ||
| private static final String OLD_MOCKRESPONSE_STATUS = OLD_MOCKRESPONSE_FQN + " status(java.lang.String)"; | ||
| private static final MethodMatcher OLD_MOCKRESPONSE_STATUS_MATCHER = new MethodMatcher(OLD_MOCKRESPONSE_STATUS); | ||
| private static final String OLD_MOCKRESPONSE_SETSTATUS = OLD_MOCKRESPONSE_FQN + " setStatus(java.lang.String)"; | ||
| private static final MethodMatcher OLD_MOCKRESPONSE_SETSTATUS_MATCHER = new MethodMatcher(OLD_MOCKRESPONSE_SETSTATUS); | ||
| private static final String NEW_MOCKRESPONSE_FQN = "mockwebserver3.MockResponse"; | ||
| private static final String NEW_MOCKRESPONSE_BUILDER_FQN = NEW_MOCKRESPONSE_FQN + ".Builder"; | ||
| private static final String NEW_MOCKRESPONSE_BUILDER_CONSTRUCTOR = NEW_MOCKRESPONSE_BUILDER_FQN + " <constructor>()"; | ||
|
|
||
| @Override | ||
| public String getDisplayName() { | ||
| return "OkHttp `MockWebServer` `MockResponse` to 5.x `MockWebServer3` `MockResponse`"; | ||
| } | ||
|
|
||
| @Override | ||
| public String getDescription() { | ||
| return "Replace usages of OkHttp MockWebServer `MockResponse` with 5.x MockWebServer3 `MockResponse` and it's `Builder`."; | ||
| } | ||
|
|
||
| @Override | ||
| public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
| return Preconditions.check(new UsesType<>(OLD_MOCKRESPONSE_FQN, false), new JavaIsoVisitor<ExecutionContext>() { | ||
| @Override | ||
| public @Nullable J visit(@Nullable Tree tree, ExecutionContext ctx) { | ||
| J j = (J) tree; | ||
| // TODO: look into whether I could have used ...MockResponse$Builder to any other effect | ||
| j = (J) new ChangeMethodInvocationReturnType( | ||
| OLD_MOCKRESPONSE_STATUS, | ||
| OLD_MOCKRESPONSE_FQN | ||
| ).getVisitor().visit(j, ctx); | ||
| j = (J) new ChangeMethodName( | ||
| OLD_MOCKRESPONSE_SETSTATUS, | ||
| "status", | ||
| null, | ||
| null | ||
| ).getVisitor().visit(j, ctx); | ||
| j = (J) new ChangeType( | ||
| OLD_MOCKRESPONSE_FQN, | ||
| NEW_MOCKRESPONSE_BUILDER_FQN, | ||
| null | ||
| ).getVisitor().visit(j, ctx); | ||
| j = new JavaIsoVisitor<ExecutionContext>() { | ||
steve-aom-elliott marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
steve-aom-elliott marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| @Override | ||
| public J.NewClass visitNewClass(J.NewClass newClass, ExecutionContext ctx) { | ||
| J.NewClass nc = super.visitNewClass(newClass, ctx); | ||
| if (new MethodMatcher(NEW_MOCKRESPONSE_BUILDER_CONSTRUCTOR).matches(nc)) { | ||
| maybeAddImport(NEW_MOCKRESPONSE_FQN); | ||
| maybeRemoveImport(NEW_MOCKRESPONSE_BUILDER_FQN); | ||
| return JavaTemplate | ||
| .builder("new MockResponse.Builder()") | ||
| .imports("mockwebserver3.MockResponse") | ||
| .javaParser(JavaParser.fromJavaVersion().classpath("mockwebserver3")) | ||
| .build() | ||
| .apply(getCursor(), nc.getCoordinates().replace()); | ||
| } | ||
| return nc; | ||
| } | ||
|
|
||
| @Override | ||
| public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations varDecl, ExecutionContext ctx) { | ||
| J.VariableDeclarations vd = super.visitVariableDeclarations(varDecl, ctx); | ||
| if (TypeUtils.isAssignableTo(NEW_MOCKRESPONSE_BUILDER_FQN, requireNonNull(vd.getVariables().get(0).getVariableType()).getType())) { | ||
| return vd.withTypeExpression(requireNonNull(((J.Identifier) vd.getTypeExpression())).withSimpleName("MockResponse.Builder")); | ||
| } | ||
| return vd; | ||
| } | ||
| }.visit(j, ctx); | ||
| // j = (J) new ChangeMethodInvocationReturnType( | ||
| // OLD_MOCKRESPONSE_STATUS, | ||
| // NEW_MOCKRESPONSE_BUILDER_FQN | ||
| // ).getVisitor().visit(j, ctx); | ||
| // j = (J) new ChangeMethodInvocationReturnType( | ||
| // OLD_MOCKRESPONSE_SETSTATUS, | ||
| // NEW_MOCKRESPONSE_BUILDER_FQN | ||
| // ).getVisitor().visit(j, ctx); | ||
| // j = new ChangeFieldType<ExecutionContext>( | ||
| // (JavaType.FullyQualified) JavaType.buildType(OLD_MOCKRESPONSE_FQN), | ||
| // (JavaType.FullyQualified) JavaType.buildType(NEW_MOCKRESPONSE_BUILDER_FQN) | ||
| // ).visit(j, ctx); | ||
| // j = (J) new ChangeMethodName( | ||
| // OLD_MOCKRESPONSE_SETSTATUS, | ||
| // "status", | ||
| // null, | ||
| // null | ||
| // ).getVisitor().visit(j, ctx); | ||
| // j = new JavaIsoVisitor<ExecutionContext>() { | ||
| // @Override | ||
| // public J.NewClass visitNewClass(J.NewClass newClass, ExecutionContext ctx) { | ||
| // J.NewClass nc = super.visitNewClass(newClass, ctx); | ||
| // if (OLD_MOCKRESPONSE_CONSTRUCTOR_MATCHER.matches(nc)) { | ||
| // maybeAddImport(NEW_MOCKRESPONSE_FQN); | ||
| // maybeRemoveImport(OLD_MOCKRESPONSE_FQN); | ||
| // return JavaTemplate | ||
| // .builder("new MockResponse.Builder()") | ||
| // .imports("mockwebserver3.MockResponse", "mockwebserver3.MockResponse.Builder") | ||
| // .javaParser(JavaParser.fromJavaVersion().classpath("mockwebserver3")) | ||
| // .build() | ||
| // .apply(getCursor(), nc.getCoordinates().replace()); | ||
| // } | ||
| // return nc; | ||
| // } | ||
| // }.visit(j, ctx); | ||
| // j = new ChangeMethodInvocationReturnType() | ||
| // j = new JavaIsoVisitor<ExecutionContext>() { | ||
| // @Override | ||
| // public J.MethodInvocation visitMethodInvocation(J.MethodInvocation methodInv, ExecutionContext ctx) { | ||
| // J.MethodInvocation mi = super.visitMethodInvocation(methodInv, ctx); | ||
| // if (OLD_MOCKRESPONSE_SETSTATUS_MATCHER.matches(mi)) { | ||
| // mi = JavaTemplate | ||
| // .builder("status(#{any(java.lang.String)})") | ||
| // .imports("mockwebserver3.MockResponse", "mockwebserver3.MockResponse.Builder") | ||
| // .contextSensitive() | ||
| // .build() | ||
| // .apply(updateCursor(mi), mi.getCoordinates().replaceMethod(), mi.getArguments().get(0)); | ||
| // } | ||
| //// if (OLD_MOCKRESPONSE_STATUS_MATCHER.matches(mi)) { | ||
| //// mi = JavaTemplate | ||
| //// .builder("status(#{any(java.lang.String)})") | ||
| //// .imports("mockwebserver3.MockResponse", "mockwebserver3.MockResponse.Builder") | ||
| //// .contextSensitive() | ||
| //// .build() | ||
| //// .apply(updateCursor(mi), mi.getCoordinates().replaceMethod(), mi.getArguments().get(0)); | ||
| //// } | ||
| // return mi; | ||
| // } | ||
| // }.visit(tree, ctx); | ||
| j = new JavaIsoVisitor<ExecutionContext>() { | ||
steve-aom-elliott marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
steve-aom-elliott marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // @Override | ||
| // public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations varDecl, ExecutionContext ctx) { | ||
| // if (TypeUtils.isAssignableTo(OLD_MOCKRESPONSE_FQN, varDecl.getType())) { | ||
| // return varDecl.withTypeExpression(requireNonNull(((J.Identifier) varDecl.getTypeExpression())) | ||
| // .withSimpleName("MockResponse.Builder") | ||
| // .withType(JavaType.buildType(NEW_MOCKRESPONSE_BUILDER_FQN)) | ||
| // ); | ||
| // } | ||
| // return super.visitVariableDeclarations(varDecl, ctx); | ||
| // } | ||
| // | ||
| // @Override | ||
| // public J.NewClass visitNewClass(J.NewClass newClass, ExecutionContext ctx) { | ||
| // J.NewClass nc = super.visitNewClass(newClass, ctx); | ||
| // if (OLD_MOCKRESPONSE_CONSTRUCTOR_MATCHER.matches(nc)) { | ||
| // maybeAddImport(NEW_MOCKRESPONSE_FQN); | ||
| // maybeRemoveImport(OLD_MOCKRESPONSE_FQN); | ||
| // return JavaTemplate | ||
| // .builder("new MockResponse.Builder()") | ||
| // .imports("mockwebserver3.MockResponse") | ||
| // .javaParser(JavaParser.fromJavaVersion().classpath("mockwebserver3")) | ||
| // .build() | ||
| // .apply(getCursor(), nc.getCoordinates().replace()); | ||
| // } | ||
| // return nc; | ||
| // } | ||
| // | ||
| // @Override | ||
| // public J.MethodInvocation visitMethodInvocation(J.MethodInvocation methodInv, ExecutionContext ctx) { | ||
| // J.MethodInvocation mi = super.visitMethodInvocation(methodInv, ctx); | ||
| // if (OLD_MOCKRESPONSE_SETSTATUS_MATCHER.matches(mi)) { | ||
| // return JavaTemplate | ||
| // .builder("status(#{any(java.lang.String)})") | ||
| // .contextSensitive() | ||
| // .build() | ||
| // .apply(getCursor(), mi.getCoordinates().replaceMethod(), mi.getArguments().get(0)); | ||
| // } | ||
| // return mi; | ||
| // } | ||
| }.visit(j, ctx); | ||
| return j; | ||
steve-aom-elliott marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
steve-aom-elliott marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| // @Override | ||
| // public J.NewClass visitNewClass(J.NewClass newClass, ExecutionContext ctx) { | ||
| // J.NewClass nc = super.visitNewClass(newClass, ctx); | ||
| // if (OLD_MOCKRESPONSE_CONSTRUCTOR_MATCHER.matches(nc)) { | ||
| // maybeAddImport(NEW_MOCKRESPONSE_FQN); | ||
| // maybeRemoveImport(OLD_MOCKRESPONSE_FQN); | ||
| // return JavaTemplate | ||
| // .builder("new MockResponse.Builder()") | ||
| // .imports("mockwebserver3.MockResponse") | ||
| // .javaParser(JavaParser.fromJavaVersion().classpath("mockwebserver3")) | ||
| // .build() | ||
| // .apply(getCursor(), nc.getCoordinates().replace()); | ||
| // } | ||
| // return nc; | ||
| // } | ||
|
|
||
| // @Override | ||
| // public J.MethodInvocation visitMethodInvocation(J.MethodInvocation methodInv, ExecutionContext ctx) { | ||
| // J.MethodInvocation mi = super.visitMethodInvocation(methodInv, ctx); | ||
| // if (OLD_MOCKRESPONSE_STATUS_MATCHER.matches(mi)) { | ||
| // return JavaTemplate | ||
| // .builder("setStatus(#{any(java.lang.String)})") | ||
| // .contextSensitive() | ||
| // .build() | ||
| // .apply(getCursor(), mi.getCoordinates().replaceMethod(), mi.getArguments().get(0)); | ||
| // } | ||
| // return mi; | ||
| // } | ||
|
|
||
| // @Override | ||
| // public J.Block visitBlock(J.Block block, ExecutionContext ctx) { | ||
| // | ||
| // J.Block b = super.visitBlock(block, ctx); | ||
| // return b; | ||
| // } | ||
| }); | ||
| } | ||
| } | ||
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.
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.