Skip to content

Commit

Permalink
Use URI#create instead of URI constructor where feasible in spring-we…
Browse files Browse the repository at this point in the history
…bmvc
  • Loading branch information
sbrannen authored and mdeinum committed Jun 29, 2023
1 parent b779359 commit 8156785
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -102,19 +101,17 @@
* @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());

Expand All @@ -132,23 +129,20 @@ public void setup() throws Exception {
handlerAdapter.setBeanFactory(context.getBeanFactory());
handlerAdapter.afterPropertiesSet();

request = new MockHttpServletRequest();
response = new MockHttpServletResponse();

request.setMethod("POST");

// Expose request to the current thread (for SpEL expressions)
RequestContextHolder.setRequestAttributes(new ServletWebRequest(request));
}

@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,
Expand Down Expand Up @@ -226,11 +220,11 @@ public void handle() throws Exception {
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,
Expand Down Expand Up @@ -307,11 +301,11 @@ public void handleInInterface() throws Exception {
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");
Expand All @@ -328,7 +322,7 @@ public void handleRequestBody() throws Exception {
}

@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");
Expand All @@ -344,7 +338,7 @@ public void handleAndValidateRequestBody() throws Exception {
}

@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");
Expand All @@ -364,7 +358,7 @@ public void handleHttpEntity() throws Exception {

// 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"));
Expand All @@ -379,7 +373,7 @@ public void handleHttpEntityWithCacheControl() throws Exception {
}

@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")));

Expand All @@ -391,7 +385,7 @@ public void handleRequestPart() throws Exception {
}

@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")));

Expand All @@ -403,7 +397,7 @@ public void handleAndValidateRequestPart() throws Exception {
}

@Test
public void handleAndCompleteSession() throws Exception {
void handleAndCompleteSession() throws Exception {
HandlerMethod handlerMethod = handlerMethod("handleAndCompleteSession", SessionStatus.class);
handlerAdapter.handle(request, response, handlerMethod);

Expand Down Expand Up @@ -654,8 +648,8 @@ public Object resolveArgument(
}
}

@Target({ ElementType.PARAMETER, ElementType.ANNOTATION_TYPE })
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AuthenticationPrincipal {}
@interface AuthenticationPrincipal {}

}
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -75,7 +75,7 @@
* @author Brian Clozel
* @author Sam Brannen
*/
public class RequestPartIntegrationTests {
class RequestPartIntegrationTests {

private RestTemplate restTemplate;

Expand All @@ -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);

Expand All @@ -106,14 +106,14 @@ public static void startServer() throws Exception {
}

@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));

Expand All @@ -132,13 +132,13 @@ public void setup() {


@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);

Expand All @@ -152,7 +152,7 @@ public void standardMultipartResolverWithEncodedFileName() throws Exception {
"--" + 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));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
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;
Expand Down Expand Up @@ -170,7 +169,7 @@
* @author Juergen Hoeller
* @author Sam Brannen
*/
public class ServletAnnotationControllerHandlerMethodTests extends AbstractServletHandlerMethodTests {
class ServletAnnotationControllerHandlerMethodTests extends AbstractServletHandlerMethodTests {

static Stream<Boolean> pathPatternsArguments() {
return Stream.of(true, false);
Expand Down Expand Up @@ -3654,14 +3653,14 @@ public void templatePath(Writer writer) throws IOException {
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);
}

Expand Down Expand Up @@ -3868,9 +3867,9 @@ static class HttpHeadersResponseController {

@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;
}

Expand Down

0 comments on commit 8156785

Please sign in to comment.