-
Notifications
You must be signed in to change notification settings - Fork 96
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 2 commits
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
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
293 changes: 293 additions & 0 deletions
293
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,293 @@ | ||
| 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.NonNull; | ||
| import org.jspecify.annotations.Nullable; | ||
| import org.openrewrite.*; | ||
| import org.openrewrite.internal.ListUtils; | ||
| import org.openrewrite.java.*; | ||
| import org.openrewrite.java.search.UsesType; | ||
| import org.openrewrite.java.tree.*; | ||
| import org.openrewrite.marker.Markers; | ||
|
|
||
| import java.util.*; | ||
| import java.util.stream.IntStream; | ||
|
|
||
| import static java.util.Collections.emptyList; | ||
| import static java.util.Collections.singletonList; | ||
| import static java.util.Objects.requireNonNull; | ||
| import static java.util.stream.Collectors.toList; | ||
| import static org.openrewrite.Tree.randomId; | ||
|
|
||
| public class UpdateMockWebServerMockResponse extends Recipe { | ||
| private static final String OLD_MOCKRESPONSE_FQN = "okhttp3.mockwebserver.MockResponse"; | ||
steve-aom-elliott marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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>()"; | ||
| private static final String OLD_MOCKWEBSERVER_ENQUEUE = "okthttp3.mockwebserver.MockWebServer enqueue(okhttp3.mockwebserver.MockResponse)"; | ||
|
|
||
| @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>() { | ||
| private final Map<UUID, List<Integer>> methodInvocationsToAdjust = new HashMap<>(); | ||
|
|
||
| @Override | ||
| public @Nullable J visit(@Nullable Tree tree, ExecutionContext ctx) { | ||
| J j = (J) tree; | ||
| j = new JavaIsoVisitor<ExecutionContext>() { | ||
| @Override | ||
| public J.MethodInvocation visitMethodInvocation(J.MethodInvocation methodInv, ExecutionContext ctx) { | ||
| J.MethodInvocation mi = super.visitMethodInvocation(methodInv, ctx); | ||
| List<Integer> indexes = IntStream.range(0, mi.getArguments().size()) | ||
| .filter(x -> TypeUtils.isAssignableTo(OLD_MOCKRESPONSE_FQN, mi.getArguments().get(x).getType())) | ||
| .boxed().collect(toList()); | ||
| if (!indexes.isEmpty() && !methodInvocationsToAdjust.containsKey(mi.getId())) { | ||
| methodInvocationsToAdjust.put(mi.getId(), indexes); | ||
| } | ||
| return mi; | ||
| } | ||
| }.visit(j, ctx); | ||
| 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.MethodInvocation visitMethodInvocation(J.MethodInvocation methodInv, ExecutionContext ctx) { | ||
| J.MethodInvocation mi = super.visitMethodInvocation(methodInv, ctx); | ||
| List<Integer> indexes = methodInvocationsToAdjust.getOrDefault(mi.getId(), emptyList()); | ||
| if (!indexes.isEmpty()) { | ||
| methodInvocationsToAdjust.remove(mi.getId()); | ||
| List<JRightPadded<Expression>> exprs = mi.getPadding().getArguments().getPadding().getElements(); | ||
| //mi.getPadding().getArguments().getPadding().getElements().getFirst().getAfter() | ||
| // mi.getCoordinates().replaceArguments() | ||
| J.MethodInvocation updated = mi.withArguments(ListUtils.map(mi.getArguments(), (index, arg) -> { | ||
steve-aom-elliott marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (indexes.contains(index)) { | ||
| // J something = new JavaVisitor<ExecutionContext>() { | ||
| // @Override | ||
| // public @Nullable J visit(@Nullable Tree tree, ExecutionContext executionContext, Cursor parent) { | ||
| // return JavaTemplate | ||
| // .builder("#{any()}.build()") | ||
| // .build() | ||
| // .apply( | ||
| // new Cursor(parent, tree), | ||
| // ((Expression) tree).getCoordinates().replace(), | ||
| // tree | ||
| // ); | ||
| //// return super.visit(tree, executionContext, parent); | ||
| // } | ||
| // // @Override | ||
| //// public J visit(Expression expression, ExecutionContext executionContext) { | ||
| //// return JavaTemplate | ||
| //// .builder("#{any()}.build()") | ||
| ////// .contextSensitive() | ||
| //// .javaParser(JavaParser.fromJavaVersion().classpath("mockwebserver3")) | ||
| //// .build() | ||
| //// .apply(getCursor(), expression.getCoordinates().replace(), expression); | ||
| //// } | ||
| // }.visit(arg, ctx, getCursor()); | ||
| J.MethodInvocation inner = new J.MethodInvocation( | ||
steve-aom-elliott marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| randomId(), | ||
| Space.EMPTY, | ||
| Markers.EMPTY, | ||
| new JRightPadded<>(arg, Space.EMPTY, arg.getMarkers()), | ||
| null, | ||
| new J.Identifier( | ||
| randomId(), | ||
| Space.EMPTY, | ||
| Markers.EMPTY, | ||
| emptyList(), | ||
| "build", | ||
| null, | ||
| null | ||
| ), | ||
| JContainer.build(Space.EMPTY, emptyList(), Markers.EMPTY), | ||
| new JavaType.Method( | ||
| null, | ||
| Flag.Public.getBitMask(), | ||
| TypeUtils.asFullyQualified(arg.getType()), | ||
| "build", | ||
| JavaType.buildType(NEW_MOCKRESPONSE_FQN), | ||
| emptyList(), | ||
| null, | ||
| null, | ||
| null, | ||
| null, | ||
| null | ||
| ) | ||
| ); | ||
| return inner; | ||
| // return (Expression) something; | ||
steve-aom-elliott marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| return arg; | ||
| })); | ||
| return updated; | ||
| } | ||
| return mi; | ||
| } | ||
| }.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.