Skip to content

Commit 1112188

Browse files
committed
replace edits lost in merge
1 parent db04069 commit 1112188

File tree

2 files changed

+42
-76
lines changed

2 files changed

+42
-76
lines changed

src/main/java/org/openrewrite/java/testing/junit5/UpdateMockWebServerMockResponse.java

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.openrewrite.java.JavaTemplate;
3131
import org.openrewrite.java.MethodMatcher;
3232
import org.openrewrite.java.search.UsesType;
33+
import org.openrewrite.java.tree.Flag;
3334
import org.openrewrite.java.tree.J;
3435
import org.openrewrite.java.tree.JavaType;
3536
import org.openrewrite.java.tree.TypeUtils;
@@ -54,6 +55,7 @@ public class UpdateMockWebServerMockResponse extends Recipe {
5455
private static final String NEW_MOCKRESPONSE_FQN_BUILDER = NEW_MOCKRESPONSE_FQN + "$Builder";
5556

5657
private static final JavaType.FullyQualified newMockResponseBuilderType = (JavaType.FullyQualified) JavaType.buildType(NEW_MOCKRESPONSE_FQN_BUILDER);
58+
private static final JavaType.FullyQualified newMockResponseType = (JavaType.FullyQualified) JavaType.buildType(NEW_MOCKRESPONSE_FQN);
5759

5860
private static class MethodInvocationReplacement {
5961
private final MethodMatcher methodMatcher;
@@ -213,10 +215,49 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
213215
if (isChainedCall) {
214216
transformed = transformed.withPrefix(arg.getPrefix());
215217
}
216-
return transformed;
218+
return patchBuilderBuildReturnTypeAndName(transformed);
219+
}
220+
return arg;
217221
}));
222+
}
218223
}.visit(j, ctx);
219224
}
220225
});
221226
}
227+
228+
private static J.MethodInvocation patchReturnTypeAndName(J.MethodInvocation mi, JavaType.Method method, JavaType.FullyQualified declaringType, JavaType returnType, String newName) {
229+
assert method != null;
230+
J.MethodInvocation updated = mi.withMethodType(
231+
method.withDeclaringType(declaringType)
232+
.withReturnType(returnType)
233+
.withName(newName)
234+
);
235+
return updated.withName(
236+
updated.getName()
237+
.withSimpleName(newName)
238+
.withType(updated.getMethodType())
239+
);
240+
}
241+
242+
private J.MethodInvocation patchBuilderBuildReturnTypeAndName(J.MethodInvocation builder) {
243+
return patchReturnTypeAndName(
244+
builder,
245+
new JavaType.Method(
246+
null,
247+
Flag.Public.getBitMask() | Flag.Final.getBitMask(),
248+
newMockResponseBuilderType,
249+
"build",
250+
newMockResponseType,
251+
(List<String>) null,
252+
null,
253+
null,
254+
null,
255+
null,
256+
null
257+
),
258+
newMockResponseBuilderType,
259+
newMockResponseType,
260+
"build"
261+
);
262+
}
222263
}

src/test/java/org/openrewrite/java/testing/junit5/UpgradeOkHttpMockWebServerTest.java

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -87,81 +87,6 @@ void shouldUpgradeMavenDependency() {
8787
);
8888
}
8989

90-
// @Test
91-
// void wip() {
92-
// rewriteRun(
93-
// //language=java
94-
// java(
95-
// """
96-
// import okhttp3.Headers;
97-
// import okhttp3.mockwebserver.MockResponse;
98-
//
99-
// class A {
100-
// void someMethod() {
101-
// Headers headers = new Headers.Builder().build();
102-
// MockResponse a = new MockResponse();
103-
// // .status(String): void
104-
// // .getStatus(): String
105-
// // --
106-
// // .setStatus(String): MockResponse[this]
107-
// // ---
108-
// // .headers(Headers): void
109-
// // .setHeaders(Headers): MockResponse
110-
// // .getHeaders(): Headers
111-
// // ---
112-
// // .addHeader(String): MockResponse
113-
// // .addHeader(String,Object): MockResponse
114-
// // .addHeaderLenient(String,Object): MockResponse
115-
// // ---
116-
// // .setHeader(String,Object): MockResponse
117-
// // .removeHeader(String): MockResponse
118-
// // .clearHeaders(): MockResponse
119-
// a.header
120-
// a.trailers(headers);
121-
// }
122-
// }
123-
// """
124-
// )
125-
// );
126-
// }
127-
128-
@DocumentExample
129-
@Test
130-
void shouldUpgradeMavenDependency() {
131-
rewriteRun(
132-
spec -> spec.recipeFromResource("/META-INF/rewrite/junit5.yml", "org.openrewrite.java.testing.junit5.UpgradeOkHttpMockWebServer"),
133-
mavenProject("project",
134-
// TODO: handle solely J.NewClass and update declarative recipe to include new one.
135-
//language=xml
136-
pomXml(
137-
"""
138-
<project>
139-
<modelVersion>4.0.0</modelVersion>
140-
<groupId>com.example</groupId>
141-
<artifactId>demo</artifactId>
142-
<version>0.0.1-SNAPSHOT</version>
143-
<dependencies>
144-
<dependency>
145-
<groupId>com.squareup.okhttp3</groupId>
146-
<artifactId>mockwebserver</artifactId>
147-
<version>4.10.0</version>
148-
<scope>test</scope>
149-
</dependency>
150-
</dependencies>
151-
</project>
152-
""",
153-
spec -> spec.after(pom ->
154-
assertThat(pom)
155-
.doesNotContain("<artifactId>mockwebserver</artifactId>")
156-
.contains("<artifactId>mockwebserver3</artifactId>")
157-
.containsPattern("<version>5\\.(.*)</version>")
158-
.actual()
159-
)
160-
)
161-
)
162-
);
163-
}
164-
16590
// TODO: methods receiving MockResponse - maybe add comment instructing to double check?
16691
@Test
16792
void shouldMigrateMockResponseToBuilder() {

0 commit comments

Comments
 (0)