Restore source and binary compatibility in MockMvc

Previous to this commit, MockHttpServletRequestBuilder was not binary
compatible as its methods had moved to a parent class with a generic
argument on the return type. MockMultipartHttpServletRequestBuilder
was also not source compatible as it no longed extended from
MockHttpServletRequestBuilder.

Both these changes were introduced to allow the AssertJ support to
expose builders that implement an extra AssertJ interface, without
copying the features the builders provide.

This commit restore compatibility. For MockHttpServletRequestBuilder
we simply override all methods that returns the generic type into
the resolved type.

MockMultipartHttpServletRequestBuilder is more involved. Because we
need to extend from MockHttpServletRequestBuilder, we have no other
choice than duplicating the code. For now, the abstract builder for
multipart is only used by the AssertJ support, but we can revisit this
again in a major release.

Closes gh-33229
This commit is contained in:
Stéphane Nicoll
2024-07-18 11:21:35 +02:00
parent 5715b2a783
commit 265299c790
4 changed files with 328 additions and 9 deletions

View File

@@ -39,7 +39,7 @@ public class MockMultipartHttpServletRequestBuilderTests {
@Test // gh-26166
void addFileAndParts() throws Exception {
MockMultipartHttpServletRequest mockRequest =
(MockMultipartHttpServletRequest) new MockMultipartHttpServletRequestBuilder().uri("/upload")
(MockMultipartHttpServletRequest) createBuilder("/upload")
.file(new MockMultipartFile("file", "test.txt", "text/plain", "Test".getBytes(UTF_8)))
.part(new MockPart("name", "value".getBytes(UTF_8)))
.buildRequest(new MockServletContext());
@@ -55,7 +55,7 @@ public class MockMultipartHttpServletRequestBuilderTests {
jsonPart.getHeaders().setContentType(MediaType.APPLICATION_JSON);
MockMultipartHttpServletRequest mockRequest =
(MockMultipartHttpServletRequest) new MockMultipartHttpServletRequestBuilder().uri("/upload")
(MockMultipartHttpServletRequest) createBuilder("/upload")
.file(new MockMultipartFile("file", "Test".getBytes(UTF_8)))
.part(jsonPart)
.buildRequest(new MockServletContext());
@@ -70,7 +70,7 @@ public class MockMultipartHttpServletRequestBuilderTests {
void mergeAndBuild() {
MockHttpServletRequestBuilder parent = new MockHttpServletRequestBuilder(HttpMethod.GET).uri("/");
parent.characterEncoding("UTF-8");
Object result = new MockMultipartHttpServletRequestBuilder().uri("/fileUpload").merge(parent);
Object result = createBuilder("/fileUpload").merge(parent);
assertThat(result).isNotNull();
assertThat(result.getClass()).isEqualTo(MockMultipartHttpServletRequestBuilder.class);
@@ -80,4 +80,10 @@ public class MockMultipartHttpServletRequestBuilderTests {
assertThat(request.getCharacterEncoding()).isEqualTo("UTF-8");
}
private MockMultipartHttpServletRequestBuilder createBuilder(String uri) {
MockMultipartHttpServletRequestBuilder builder = new MockMultipartHttpServletRequestBuilder();
builder.uri(uri);
return builder;
}
}