Browse Source

Polishing

See gh-28189
pull/28433/head
rstoyanchev 3 years ago
parent
commit
8378af9e39
  1. 4
      spring-test/src/main/java/org/springframework/test/web/reactive/server/ExchangeResult.java
  2. 7
      spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ResponseBodyResultHandlerTests.java
  3. 46
      spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestResponseBodyMethodProcessorTests.java

4
spring-test/src/main/java/org/springframework/test/web/reactive/server/ExchangeResult.java

@ -1,5 +1,5 @@ @@ -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.
@ -60,7 +60,7 @@ public class ExchangeResult { @@ -60,7 +60,7 @@ public class ExchangeResult {
private static final Log logger = LogFactory.getLog(ExchangeResult.class);
private static final List<MediaType> PRINTABLE_MEDIA_TYPES = Arrays.asList(
MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML,
MediaType.parseMediaType("application/*+json"), MediaType.APPLICATION_XML,
MediaType.parseMediaType("text/*"), MediaType.APPLICATION_FORM_URLENCODED);

7
spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ResponseBodyResultHandlerTests.java

@ -1,5 +1,5 @@ @@ -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.
@ -162,6 +162,7 @@ public class ResponseBodyResultHandlerTests { @@ -162,6 +162,7 @@ public class ResponseBodyResultHandlerTests {
return new HandlerResult(handlerMethod, returnValue, handlerMethod.getReturnType());
}
@SuppressWarnings("SameParameterValue")
private void assertResponseBody(MockServerWebExchange exchange, @Nullable String responseBody) {
StepVerifier.create(exchange.getResponse().getBody())
.consumeNextWith(buf -> assertThat(buf.toString(UTF_8)).isEqualTo(responseBody))
@ -171,7 +172,7 @@ public class ResponseBodyResultHandlerTests { @@ -171,7 +172,7 @@ public class ResponseBodyResultHandlerTests {
@RestController
@SuppressWarnings("unused")
@SuppressWarnings({"unused", "ConstantConditions"})
private static class TestRestController {
public Mono<Void> handleToMonoVoid() { return null;}
@ -200,7 +201,7 @@ public class ResponseBodyResultHandlerTests { @@ -200,7 +201,7 @@ public class ResponseBodyResultHandlerTests {
@Controller
@SuppressWarnings("unused")
@SuppressWarnings({"unused", "ConstantConditions"})
private static class TestController {
@ResponseBody

46
spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestResponseBodyMethodProcessorTests.java

@ -21,6 +21,7 @@ import java.io.OutputStream; @@ -21,6 +21,7 @@ import java.io.OutputStream;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@ -130,7 +131,7 @@ public class RequestResponseBodyMethodProcessorTests { @@ -130,7 +131,7 @@ public class RequestResponseBodyMethodProcessorTests {
@Test
public void resolveArgumentParameterizedType() throws Exception {
String content = "[{\"name\" : \"Jad\"}, {\"name\" : \"Robert\"}]";
this.servletRequest.setContent(content.getBytes("UTF-8"));
this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8));
this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE);
List<HttpMessageConverter<?>> converters = new ArrayList<>();
@ -150,7 +151,7 @@ public class RequestResponseBodyMethodProcessorTests { @@ -150,7 +151,7 @@ public class RequestResponseBodyMethodProcessorTests {
public void resolveArgumentRawTypeFromParameterizedType() throws Exception {
String content = "fruit=apple&vegetable=kale";
this.servletRequest.setMethod("GET");
this.servletRequest.setContent(content.getBytes("UTF-8"));
this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8));
this.servletRequest.setContentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE);
List<HttpMessageConverter<?>> converters = new ArrayList<>();
@ -169,7 +170,7 @@ public class RequestResponseBodyMethodProcessorTests { @@ -169,7 +170,7 @@ public class RequestResponseBodyMethodProcessorTests {
@Test
public void resolveArgumentClassJson() throws Exception {
String content = "{\"name\" : \"Jad\"}";
this.servletRequest.setContent(content.getBytes("UTF-8"));
this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8));
this.servletRequest.setContentType("application/json");
List<HttpMessageConverter<?>> converters = new ArrayList<>();
@ -186,7 +187,7 @@ public class RequestResponseBodyMethodProcessorTests { @@ -186,7 +187,7 @@ public class RequestResponseBodyMethodProcessorTests {
@Test
public void resolveArgumentClassString() throws Exception {
String content = "foobarbaz";
this.servletRequest.setContent(content.getBytes("UTF-8"));
this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8));
this.servletRequest.setContentType("application/json");
List<HttpMessageConverter<?>> converters = new ArrayList<>();
@ -201,7 +202,7 @@ public class RequestResponseBodyMethodProcessorTests { @@ -201,7 +202,7 @@ public class RequestResponseBodyMethodProcessorTests {
}
@Test // SPR-9942
public void resolveArgumentRequiredNoContent() throws Exception {
public void resolveArgumentRequiredNoContent() {
this.servletRequest.setContent(new byte[0]);
this.servletRequest.setContentType("text/plain");
List<HttpMessageConverter<?>> converters = new ArrayList<>();
@ -230,7 +231,7 @@ public class RequestResponseBodyMethodProcessorTests { @@ -230,7 +231,7 @@ public class RequestResponseBodyMethodProcessorTests {
MethodParameter methodParam = handlerMethod.getMethodParameters()[0];
String content = "{\"name\" : \"Jad\"}";
this.servletRequest.setContent(content.getBytes("UTF-8"));
this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8));
this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE);
List<HttpMessageConverter<?>> converters = new ArrayList<>();
@ -250,7 +251,7 @@ public class RequestResponseBodyMethodProcessorTests { @@ -250,7 +251,7 @@ public class RequestResponseBodyMethodProcessorTests {
MethodParameter methodParam = handlerMethod.getMethodParameters()[0];
String content = "[{\"name\" : \"Jad\"}, {\"name\" : \"Robert\"}]";
this.servletRequest.setContent(content.getBytes("UTF-8"));
this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8));
this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE);
List<HttpMessageConverter<?>> converters = new ArrayList<>();
@ -273,7 +274,7 @@ public class RequestResponseBodyMethodProcessorTests { @@ -273,7 +274,7 @@ public class RequestResponseBodyMethodProcessorTests {
MethodParameter methodParam = handlerMethod.getMethodParameters()[0];
String content = "{\"name\" : \"Jad\"}";
this.servletRequest.setContent(content.getBytes("UTF-8"));
this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8));
this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE);
List<HttpMessageConverter<?>> converters = new ArrayList<>();
@ -609,7 +610,7 @@ public class RequestResponseBodyMethodProcessorTests { @@ -609,7 +610,7 @@ public class RequestResponseBodyMethodProcessorTests {
@Test // SPR-12501
public void resolveArgumentWithJacksonJsonView() throws Exception {
String content = "{\"withView1\" : \"with\", \"withView2\" : \"with\", \"withoutView\" : \"without\"}";
this.servletRequest.setContent(content.getBytes("UTF-8"));
this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8));
this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE);
Method method = JacksonController.class.getMethod("handleRequestBody", JacksonViewBean.class);
@ -634,7 +635,7 @@ public class RequestResponseBodyMethodProcessorTests { @@ -634,7 +635,7 @@ public class RequestResponseBodyMethodProcessorTests {
@Test // SPR-12501
public void resolveHttpEntityArgumentWithJacksonJsonView() throws Exception {
String content = "{\"withView1\" : \"with\", \"withView2\" : \"with\", \"withoutView\" : \"without\"}";
this.servletRequest.setContent(content.getBytes("UTF-8"));
this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8));
this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE);
Method method = JacksonController.class.getMethod("handleHttpEntity", HttpEntity.class);
@ -664,7 +665,7 @@ public class RequestResponseBodyMethodProcessorTests { @@ -664,7 +665,7 @@ public class RequestResponseBodyMethodProcessorTests {
"<withView1>with</withView1>" +
"<withView2>with</withView2>" +
"<withoutView>without</withoutView></root>";
this.servletRequest.setContent(content.getBytes("UTF-8"));
this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8));
this.servletRequest.setContentType(MediaType.APPLICATION_XML_VALUE);
Method method = JacksonController.class.getMethod("handleRequestBody", JacksonViewBean.class);
@ -692,7 +693,7 @@ public class RequestResponseBodyMethodProcessorTests { @@ -692,7 +693,7 @@ public class RequestResponseBodyMethodProcessorTests {
"<withView1>with</withView1>" +
"<withView2>with</withView2>" +
"<withoutView>without</withoutView></root>";
this.servletRequest.setContent(content.getBytes("UTF-8"));
this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8));
this.servletRequest.setContentType(MediaType.APPLICATION_XML_VALUE);
Method method = JacksonController.class.getMethod("handleHttpEntity", HttpEntity.class);
@ -774,7 +775,7 @@ public class RequestResponseBodyMethodProcessorTests { @@ -774,7 +775,7 @@ public class RequestResponseBodyMethodProcessorTests {
@Test // SPR-14520
public void resolveArgumentTypeVariableWithGenericInterface() throws Exception {
this.servletRequest.setContent("\"foo\"".getBytes("UTF-8"));
this.servletRequest.setContent("\"foo\"".getBytes(StandardCharsets.UTF_8));
this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE);
Method method = MyControllerImplementingInterface.class.getMethod("handle", Object.class);
@ -794,7 +795,7 @@ public class RequestResponseBodyMethodProcessorTests { @@ -794,7 +795,7 @@ public class RequestResponseBodyMethodProcessorTests {
@Test // gh-24127
public void resolveArgumentTypeVariableWithGenericInterfaceAndSubclass() throws Exception {
this.servletRequest.setContent("\"foo\"".getBytes("UTF-8"));
this.servletRequest.setContent("\"foo\"".getBytes(StandardCharsets.UTF_8));
this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE);
Method method = SubControllerImplementingInterface.class.getMethod("handle", Object.class);
@ -836,6 +837,7 @@ public class RequestResponseBodyMethodProcessorTests { @@ -836,6 +837,7 @@ public class RequestResponseBodyMethodProcessorTests {
}
@SuppressWarnings("ConstantConditions")
String handle(
@RequestBody List<SimpleBean> list,
@RequestBody SimpleBean simpleBean,
@ -845,19 +847,23 @@ public class RequestResponseBodyMethodProcessorTests { @@ -845,19 +847,23 @@ public class RequestResponseBodyMethodProcessorTests {
return null;
}
@SuppressWarnings("ConstantConditions")
Resource getImage() {
return null;
}
@SuppressWarnings("ConstantConditions")
ProblemDetail handleAndReturnProblemDetail() {
return null;
}
@SuppressWarnings("ConstantConditions")
@RequestMapping
OutputStream handleAndReturnOutputStream() {
return null;
}
@SuppressWarnings("ConstantConditions")
SimpleBean getSimpleBean() {
return null;
}
@ -895,7 +901,7 @@ public class RequestResponseBodyMethodProcessorTests { @@ -895,7 +901,7 @@ public class RequestResponseBodyMethodProcessorTests {
}
@SuppressWarnings({ "serial" })
@SuppressWarnings("NotNullFieldNotInitialized")
private static class SimpleBean implements Identifiable {
private Long id;
@ -922,7 +928,7 @@ public class RequestResponseBodyMethodProcessorTests { @@ -922,7 +928,7 @@ public class RequestResponseBodyMethodProcessorTests {
}
private final class ValidatingBinderFactory implements WebDataBinderFactory {
private static final class ValidatingBinderFactory implements WebDataBinderFactory {
@Override
public WebDataBinder createBinder(NativeWebRequest request, @Nullable Object target, String objectName) {
@ -943,6 +949,7 @@ public class RequestResponseBodyMethodProcessorTests { @@ -943,6 +949,7 @@ public class RequestResponseBodyMethodProcessorTests {
return "hello";
}
@SuppressWarnings("ConstantConditions")
@RequestMapping
public CharSequence handleWithCharSequence() {
return null;
@ -965,6 +972,7 @@ public class RequestResponseBodyMethodProcessorTests { @@ -965,6 +972,7 @@ public class RequestResponseBodyMethodProcessorTests {
private interface MyJacksonView2 {}
@SuppressWarnings("NotNullFieldNotInitialized")
private static class JacksonViewBean {
@JsonView(MyJacksonView1.class)
@ -983,6 +991,7 @@ public class RequestResponseBodyMethodProcessorTests { @@ -983,6 +991,7 @@ public class RequestResponseBodyMethodProcessorTests {
this.withView1 = withView1;
}
@Nullable
public String getWithView2() {
return withView2;
}
@ -991,6 +1000,7 @@ public class RequestResponseBodyMethodProcessorTests { @@ -991,6 +1000,7 @@ public class RequestResponseBodyMethodProcessorTests {
this.withView2 = withView2;
}
@Nullable
public String getWithoutView() {
return withoutView;
}
@ -1001,7 +1011,8 @@ public class RequestResponseBodyMethodProcessorTests { @@ -1001,7 +1011,8 @@ public class RequestResponseBodyMethodProcessorTests {
}
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@SuppressWarnings("NotNullFieldNotInitialized")
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
public static class ParentClass {
private String parentProperty;
@ -1092,6 +1103,7 @@ public class RequestResponseBodyMethodProcessorTests { @@ -1092,6 +1103,7 @@ public class RequestResponseBodyMethodProcessorTests {
return bean;
}
@SuppressWarnings("ConstantConditions")
@RequestMapping
@ResponseBody
public JacksonViewBean handleHttpEntity(@JsonView(MyJacksonView1.class) HttpEntity<JacksonViewBean> entity) {

Loading…
Cancel
Save