Use URI#create instead of URI constructor where feasible in spring-webmvc

This commit is contained in:
Sam Brannen 2022-12-09 13:27:13 -05:00
parent 67644a28b6
commit 284cb12f8f
3 changed files with 34 additions and 41 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -17,7 +17,6 @@
package org.springframework.web.servlet.mvc.method.annotation;
import java.awt.Color;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@ -102,19 +101,17 @@ import static org.assertj.core.api.Assertions.assertThat;
* @see HandlerMethodAnnotationDetectionTests
* @see ServletAnnotationControllerHandlerMethodTests
*/
public class RequestMappingHandlerAdapterIntegrationTests {
class RequestMappingHandlerAdapterIntegrationTests {
private final Object handler = new Handler();
private final MockHttpServletRequest request = new MockHttpServletRequest();
private final MockHttpServletResponse response = new MockHttpServletResponse();
private RequestMappingHandlerAdapter handlerAdapter;
private MockHttpServletRequest request;
private MockHttpServletResponse response;
@BeforeEach
public void setup() throws Exception {
void setup() throws Exception {
ConfigurableWebBindingInitializer bindingInitializer = new ConfigurableWebBindingInitializer();
bindingInitializer.setValidator(new StubValidator());
@ -132,9 +129,6 @@ public class RequestMappingHandlerAdapterIntegrationTests {
handlerAdapter.setBeanFactory(context.getBeanFactory());
handlerAdapter.afterPropertiesSet();
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
request.setMethod("POST");
// Expose request to the current thread (for SpEL expressions)
@ -142,13 +136,13 @@ public class RequestMappingHandlerAdapterIntegrationTests {
}
@AfterEach
public void teardown() {
void teardown() {
RequestContextHolder.resetRequestAttributes();
}
@Test
public void handle() throws Exception {
void handle() throws Exception {
Class<?>[] parameterTypes = new Class<?>[] {int.class, String.class, String.class, String.class, Map.class,
Date.class, Map.class, String.class, String.class, TestBean.class, Errors.class, TestBean.class,
Color.class, HttpServletRequest.class, HttpServletResponse.class, TestBean.class, TestBean.class,
@ -226,11 +220,11 @@ public class RequestMappingHandlerAdapterIntegrationTests {
assertThat(model.get("sessionAttribute")).isSameAs(sessionAttribute);
assertThat(model.get("requestAttribute")).isSameAs(requestAttribute);
assertThat(model.get("url")).isEqualTo(new URI("http://localhost/contextPath/main/path"));
assertThat(model.get("url")).isEqualTo(URI.create("http://localhost/contextPath/main/path"));
}
@Test
public void handleInInterface() throws Exception {
void handleInInterface() throws Exception {
Class<?>[] parameterTypes = new Class<?>[] {int.class, String.class, String.class, String.class, Map.class,
Date.class, Map.class, String.class, String.class, TestBean.class, Errors.class, TestBean.class,
Color.class, HttpServletRequest.class, HttpServletResponse.class, TestBean.class, TestBean.class,
@ -307,11 +301,11 @@ public class RequestMappingHandlerAdapterIntegrationTests {
assertThat(model.get("sessionAttribute")).isSameAs(sessionAttribute);
assertThat(model.get("requestAttribute")).isSameAs(requestAttribute);
assertThat(model.get("url")).isEqualTo(new URI("http://localhost/contextPath/main/path"));
assertThat(model.get("url")).isEqualTo(URI.create("http://localhost/contextPath/main/path"));
}
@Test
public void handleRequestBody() throws Exception {
void handleRequestBody() throws Exception {
Class<?>[] parameterTypes = new Class<?>[] {byte[].class};
request.setMethod("POST");
@ -328,7 +322,7 @@ public class RequestMappingHandlerAdapterIntegrationTests {
}
@Test
public void handleAndValidateRequestBody() throws Exception {
void handleAndValidateRequestBody() throws Exception {
Class<?>[] parameterTypes = new Class<?>[] {TestBean.class, Errors.class};
request.addHeader("Content-Type", "text/plain; charset=utf-8");
@ -344,7 +338,7 @@ public class RequestMappingHandlerAdapterIntegrationTests {
}
@Test
public void handleHttpEntity() throws Exception {
void handleHttpEntity() throws Exception {
Class<?>[] parameterTypes = new Class<?>[] {HttpEntity.class};
request.addHeader("Content-Type", "text/plain; charset=utf-8");
@ -364,7 +358,7 @@ public class RequestMappingHandlerAdapterIntegrationTests {
// SPR-13867
@Test
public void handleHttpEntityWithCacheControl() throws Exception {
void handleHttpEntityWithCacheControl() throws Exception {
Class<?>[] parameterTypes = new Class<?>[] {HttpEntity.class};
request.addHeader("Content-Type", "text/plain; charset=utf-8");
request.setContent("Hello Server".getBytes("UTF-8"));
@ -379,7 +373,7 @@ public class RequestMappingHandlerAdapterIntegrationTests {
}
@Test
public void handleRequestPart() throws Exception {
void handleRequestPart() throws Exception {
MockMultipartHttpServletRequest multipartRequest = new MockMultipartHttpServletRequest();
multipartRequest.addFile(new MockMultipartFile("requestPart", "", "text/plain", "content".getBytes("UTF-8")));
@ -391,7 +385,7 @@ public class RequestMappingHandlerAdapterIntegrationTests {
}
@Test
public void handleAndValidateRequestPart() throws Exception {
void handleAndValidateRequestPart() throws Exception {
MockMultipartHttpServletRequest multipartRequest = new MockMultipartHttpServletRequest();
multipartRequest.addFile(new MockMultipartFile("requestPart", "", "text/plain", "content".getBytes("UTF-8")));
@ -403,7 +397,7 @@ public class RequestMappingHandlerAdapterIntegrationTests {
}
@Test
public void handleAndCompleteSession() throws Exception {
void handleAndCompleteSession() throws Exception {
HandlerMethod handlerMethod = handlerMethod("handleAndCompleteSession", SessionStatus.class);
handlerAdapter.handle(request, response, handlerMethod);
@ -654,8 +648,8 @@ public class RequestMappingHandlerAdapterIntegrationTests {
}
}
@Target({ ElementType.PARAMETER, ElementType.ANNOTATION_TYPE })
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AuthenticationPrincipal {}
@interface AuthenticationPrincipal {}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -75,7 +75,7 @@ import static org.springframework.web.bind.annotation.RequestMethod.POST;
* @author Brian Clozel
* @author Sam Brannen
*/
public class RequestPartIntegrationTests {
class RequestPartIntegrationTests {
private RestTemplate restTemplate;
@ -85,7 +85,7 @@ public class RequestPartIntegrationTests {
@BeforeAll
public static void startServer() throws Exception {
static void startServer() throws Exception {
// Let server pick its own random, available port.
server = new Server(0);
@ -106,14 +106,14 @@ public class RequestPartIntegrationTests {
}
@AfterAll
public static void stopServer() throws Exception {
static void stopServer() throws Exception {
if (server != null) {
server.stop();
}
}
@BeforeEach
public void setup() {
void setup() {
ByteArrayHttpMessageConverter emptyBodyConverter = new ByteArrayHttpMessageConverter();
emptyBodyConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));
@ -132,13 +132,13 @@ public class RequestPartIntegrationTests {
@Test
public void standardMultipartResolver() throws Exception {
void standardMultipartResolver() throws Exception {
testCreate(baseUrl + "/standard-resolver/test", "Jason");
testCreate(baseUrl + "/standard-resolver/test", "Arjen");
}
@Test // SPR-13319
public void standardMultipartResolverWithEncodedFileName() throws Exception {
void standardMultipartResolverWithEncodedFileName() throws Exception {
String boundaryText = MimeTypeUtils.generateMultipartBoundaryString();
Map<String, String> params = Collections.singletonMap("boundary", boundaryText);
@ -152,7 +152,7 @@ public class RequestPartIntegrationTests {
"--" + boundaryText + "--";
RequestEntity<byte[]> requestEntity =
RequestEntity.post(new URI(baseUrl + "/standard-resolver/spr13319"))
RequestEntity.post(URI.create(baseUrl + "/standard-resolver/spr13319"))
.contentType(new MediaType(MediaType.MULTIPART_FORM_DATA, params))
.body(content.getBytes(StandardCharsets.US_ASCII));

View File

@ -27,7 +27,6 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.security.Principal;
import java.text.SimpleDateFormat;
@ -170,7 +169,7 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy;
* @author Juergen Hoeller
* @author Sam Brannen
*/
public class ServletAnnotationControllerHandlerMethodTests extends AbstractServletHandlerMethodTests {
class ServletAnnotationControllerHandlerMethodTests extends AbstractServletHandlerMethodTests {
static Stream<Boolean> pathPatternsArguments() {
return Stream.of(true, false);
@ -3654,14 +3653,14 @@ public class ServletAnnotationControllerHandlerMethodTests extends AbstractServl
static class ResponseEntityController {
@PostMapping("/foo")
public ResponseEntity<String> foo(HttpEntity<byte[]> requestEntity) throws Exception {
public ResponseEntity<String> foo(HttpEntity<byte[]> requestEntity) {
assertThat(requestEntity).isNotNull();
assertThat(requestEntity.getHeaders().getFirst("MyRequestHeader")).isEqualTo("MyValue");
String body = new String(requestEntity.getBody(), "UTF-8");
String body = new String(requestEntity.getBody(), StandardCharsets.UTF_8);
assertThat(body).isEqualTo("Hello World");
URI location = new URI("/foo");
URI location = URI.create("/foo");
return ResponseEntity.created(location).header("MyResponseHeader", "MyValue").body(body);
}
@ -3868,9 +3867,9 @@ public class ServletAnnotationControllerHandlerMethodTests extends AbstractServl
@RequestMapping(value = "/", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public HttpHeaders create() throws URISyntaxException {
public HttpHeaders create() {
HttpHeaders headers = new HttpHeaders();
headers.setLocation(new URI("/test/items/123"));
headers.setLocation(URI.create("/test/items/123"));
return headers;
}