-
Notifications
You must be signed in to change notification settings - Fork 100
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
Merged
Merged
Changes from all commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
db0f224
WiP
steve-aom-elliott 1ee3aac
WiP 2 - am able to get `.build()` added, but seems to drop the after …
steve-aom-elliott a5d633f
Expanding tests and conversions to include headers methods
steve-aom-elliott e05bb49
Adapted the failing recipe to add as much debug/retry information as …
Jenson3210 bea4715
Reapplying paddings from arguments as they were prior to chaining `.b…
steve-aom-elliott 03d0f2a
wip
steve-aom-elliott e4c5f2a
WiP - Ripped out a lot
steve-aom-elliott 78bd41e
wip classpath jars
steve-aom-elliott 86ba868
iterate over a list of old to new replacement methods
dsgrieve 349a3af
use template for adding .build() on MockResponseBuilder
dsgrieve de9f20c
try to handle chained call
dsgrieve 6533f7e
inline static patchReturnTypeAndName method call
dsgrieve 04357bd
Apply suggestions from code review
dsgrieve 701dbc6
Update src/test/java/org/openrewrite/java/testing/junit5/UpgradeOkHtt…
dsgrieve 63af1a9
Update src/main/java/org/openrewrite/java/testing/junit5/UpdateMockWe…
dsgrieve db04069
patch .build() return type and name
dsgrieve 1112188
replace edits lost in merge
dsgrieve c56c46c
update recipes.csv
dsgrieve 1c4be6a
add test for method name changes and test with MockResponse return type
dsgrieve b5faa24
fix unit tests
dsgrieve 76c47bb
clean up
dsgrieve e43c594
convert MockResponse "with" methods
dsgrieve a471228
fix line wrap
dsgrieve b7ba58c
Use separate test classes for the two cases
timtebeek e14c33b
Remove unused field
timtebeek ee6d74d
remove duplicate code in patchReturnTypeAndName
dsgrieve e0ab385
Inline the delegating `patchReturnTypeAndName`
timtebeek a269a71
Replace the ChangePackage with a call to UpdateMockWebServerMockResponse
timtebeek cdb7af1
Use `ChangeMethodName` instead of `MethodInvocationReplacement`
timtebeek ca0cc34
Inline the `patchReturnTypeAndName` used once
timtebeek 6cbc52b
Move existing recipes into `getRecipeList()`
timtebeek ed5e923
Move replacements down to only usage
timtebeek 282594b
Change `methodInvocationsToAdjust` to local field
timtebeek 2e589c0
Drop unnecessary list of indices
timtebeek f381a20
Always carry over prefix from arg
timtebeek 5edb94a
Simplify `patchBuilderBuildReturnTypeAndName`
timtebeek 42e7ed1
Use inlined `JavaType.ShallowClass.build`
timtebeek 61bd01d
Inline `patchBuilderBuildReturnTypeAndName`
timtebeek 6e7bc5b
Final touches
timtebeek 3b43f53
Merge branch 'main' into 1589-correcting-mockwebserver-migration
timtebeek 5d3439e
Change return type before renaming method; add missing imports
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
110 changes: 110 additions & 0 deletions
110
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,110 @@ | ||
| /* | ||
| * 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; | ||
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 lombok.Getter; | ||
| import org.openrewrite.*; | ||
| import org.openrewrite.internal.ListUtils; | ||
| import org.openrewrite.java.*; | ||
| import org.openrewrite.java.search.UsesType; | ||
| import org.openrewrite.java.tree.*; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| public class UpdateMockWebServerMockResponse extends Recipe { | ||
| private static final String OLD_MOCKRESPONSE_FQN = "okhttp3.mockwebserver.MockResponse"; | ||
| private static final String NEW_MOCKRESPONSE_FQN = "mockwebserver3.MockResponse"; | ||
| private static final String NEW_MOCKRESPONSE_FQN_BUILDER = NEW_MOCKRESPONSE_FQN + "$Builder"; | ||
|
|
||
| @Getter | ||
| final String displayName = "OkHttp `MockWebServer` `MockResponse` to 5.x `MockWebServer3` `MockResponse`"; | ||
|
|
||
| @Getter | ||
| final String description = "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 J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { | ||
| J.MethodInvocation mi = super.visitMethodInvocation(method, ctx); | ||
| return mi.withArguments(ListUtils.map(mi.getArguments(), | ||
| arg -> TypeUtils.isAssignableTo(OLD_MOCKRESPONSE_FQN, arg.getType()) ? | ||
| // Wrap MockResponse.Builder arguments with .build() | ||
| appendBuildInvocation(ctx, arg) : arg)); | ||
| } | ||
|
|
||
| private J.MethodInvocation appendBuildInvocation(ExecutionContext ctx, Expression arg) { | ||
| String nl = arg instanceof J.MethodInvocation ? "\n" : ""; | ||
| J.MethodInvocation builder = JavaTemplate | ||
| .builder("#{any(" + NEW_MOCKRESPONSE_FQN_BUILDER + ")}" + nl + ".build()") | ||
| .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "mockwebserver3")) | ||
| .imports("mockwebserver3.MockResponse", "mockwebserver3.MockResponse.Builder") | ||
| .build() | ||
| .apply(new Cursor(getCursor(), arg), arg.getCoordinates().replace(), arg); | ||
| JavaType.Method buildMethodType = new JavaType.Method( | ||
| null, | ||
| Flag.Public.getBitMask() | Flag.Final.getBitMask(), | ||
| JavaType.ShallowClass.build(NEW_MOCKRESPONSE_FQN_BUILDER), | ||
| "build", | ||
| JavaType.ShallowClass.build(NEW_MOCKRESPONSE_FQN), | ||
| (List<String>) null, | ||
| null, | ||
| null, | ||
| null, | ||
| null, | ||
| null | ||
| ); | ||
| return builder.withPrefix(arg.getPrefix()) | ||
| .withMethodType(buildMethodType) | ||
| .withName(builder.getName().withType(buildMethodType)); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| private static final Map<String, String> REPLACEMENTS = new HashMap<String, String>() {{ | ||
| put("setBody(*)", "body"); | ||
| put("setBodyDelay(long, java.util.concurrent.TimeUnit)", "bodyDelay"); | ||
| put("setChunkedBody(*, int)", "chunkedBody"); | ||
| put("setErrorCode(int)", "code"); | ||
| put("setHeaders(okhttp3.Headers)", "headers"); | ||
| put("setHeadersDelay(long, java.util.concurrent.TimeUnit)", "headersDelay"); | ||
| put("setHttp2ErrorCode(int)", "code"); | ||
| put("setResponseCode(int)", "code"); | ||
| put("setStatus(java.lang.String)", "status"); | ||
| put("setThrottleBody(long, long, java.util.concurrent.TimeUnit)", "throttleBody"); | ||
| put("setTrailers(okhttp3.Headers)", "trailers"); | ||
| put("withPush(okhttp3.mockwebserver.PushPromise)", "addPush"); | ||
| put("withSettings(okhttp3.internal.http2.Settings)", "settings"); | ||
| put("withWebSocketUpgrade(okhttp3.WebSocketListener)", "webSocketUpgrade"); | ||
| }}; | ||
|
|
||
| @Override | ||
| public List<Recipe> getRecipeList() { | ||
| List<Recipe> recipes = new ArrayList<>(); | ||
| for (Map.Entry<String, String> rep : REPLACEMENTS.entrySet()) { | ||
| String methodPattern = OLD_MOCKRESPONSE_FQN.replace("$", ".") + "#" + rep.getKey(); | ||
| recipes.add(new ChangeMethodInvocationReturnType(methodPattern, NEW_MOCKRESPONSE_FQN_BUILDER.replace("$", "."))); | ||
| recipes.add(new ChangeMethodName(methodPattern, rep.getValue(), true, false)); | ||
| } | ||
| recipes.add(new ChangeType(OLD_MOCKRESPONSE_FQN, NEW_MOCKRESPONSE_FQN_BUILDER, true)); | ||
| recipes.add(new ChangePackage("okhttp3.mockwebserver", "mockwebserver3", false)); | ||
| return recipes; | ||
| } | ||
| } | ||
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.