Skip to content

Commit 8378af9

Browse files
committed
Polishing
See gh-28189
1 parent 78ab4d7 commit 8378af9

File tree

3 files changed

+35
-22
lines changed

3 files changed

+35
-22
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -60,7 +60,7 @@ public class ExchangeResult {
6060
private static final Log logger = LogFactory.getLog(ExchangeResult.class);
6161

6262
private static final List<MediaType> PRINTABLE_MEDIA_TYPES = Arrays.asList(
63-
MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML,
63+
MediaType.parseMediaType("application/*+json"), MediaType.APPLICATION_XML,
6464
MediaType.parseMediaType("text/*"), MediaType.APPLICATION_FORM_URLENCODED);
6565

6666

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -162,6 +162,7 @@ private HandlerResult getHandlerResult(Object controller, @Nullable Object retur
162162
return new HandlerResult(handlerMethod, returnValue, handlerMethod.getReturnType());
163163
}
164164

165+
@SuppressWarnings("SameParameterValue")
165166
private void assertResponseBody(MockServerWebExchange exchange, @Nullable String responseBody) {
166167
StepVerifier.create(exchange.getResponse().getBody())
167168
.consumeNextWith(buf -> assertThat(buf.toString(UTF_8)).isEqualTo(responseBody))
@@ -171,7 +172,7 @@ private void assertResponseBody(MockServerWebExchange exchange, @Nullable String
171172

172173

173174
@RestController
174-
@SuppressWarnings("unused")
175+
@SuppressWarnings({"unused", "ConstantConditions"})
175176
private static class TestRestController {
176177

177178
public Mono<Void> handleToMonoVoid() { return null;}
@@ -200,7 +201,7 @@ public ProblemDetail handleToProblemDetail() {
200201

201202

202203
@Controller
203-
@SuppressWarnings("unused")
204+
@SuppressWarnings({"unused", "ConstantConditions"})
204205
private static class TestController {
205206

206207
@ResponseBody

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

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.Serializable;
2222
import java.lang.reflect.Method;
2323
import java.lang.reflect.Type;
24+
import java.nio.charset.StandardCharsets;
2425
import java.util.ArrayList;
2526
import java.util.Arrays;
2627
import java.util.Collections;
@@ -130,7 +131,7 @@ public void setup() throws Exception {
130131
@Test
131132
public void resolveArgumentParameterizedType() throws Exception {
132133
String content = "[{\"name\" : \"Jad\"}, {\"name\" : \"Robert\"}]";
133-
this.servletRequest.setContent(content.getBytes("UTF-8"));
134+
this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8));
134135
this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE);
135136

136137
List<HttpMessageConverter<?>> converters = new ArrayList<>();
@@ -150,7 +151,7 @@ public void resolveArgumentParameterizedType() throws Exception {
150151
public void resolveArgumentRawTypeFromParameterizedType() throws Exception {
151152
String content = "fruit=apple&vegetable=kale";
152153
this.servletRequest.setMethod("GET");
153-
this.servletRequest.setContent(content.getBytes("UTF-8"));
154+
this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8));
154155
this.servletRequest.setContentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE);
155156

156157
List<HttpMessageConverter<?>> converters = new ArrayList<>();
@@ -169,7 +170,7 @@ public void resolveArgumentRawTypeFromParameterizedType() throws Exception {
169170
@Test
170171
public void resolveArgumentClassJson() throws Exception {
171172
String content = "{\"name\" : \"Jad\"}";
172-
this.servletRequest.setContent(content.getBytes("UTF-8"));
173+
this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8));
173174
this.servletRequest.setContentType("application/json");
174175

175176
List<HttpMessageConverter<?>> converters = new ArrayList<>();
@@ -186,7 +187,7 @@ public void resolveArgumentClassJson() throws Exception {
186187
@Test
187188
public void resolveArgumentClassString() throws Exception {
188189
String content = "foobarbaz";
189-
this.servletRequest.setContent(content.getBytes("UTF-8"));
190+
this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8));
190191
this.servletRequest.setContentType("application/json");
191192

192193
List<HttpMessageConverter<?>> converters = new ArrayList<>();
@@ -201,7 +202,7 @@ public void resolveArgumentClassString() throws Exception {
201202
}
202203

203204
@Test // SPR-9942
204-
public void resolveArgumentRequiredNoContent() throws Exception {
205+
public void resolveArgumentRequiredNoContent() {
205206
this.servletRequest.setContent(new byte[0]);
206207
this.servletRequest.setContentType("text/plain");
207208
List<HttpMessageConverter<?>> converters = new ArrayList<>();
@@ -230,7 +231,7 @@ public void resolveArgumentTypeVariable() throws Exception {
230231
MethodParameter methodParam = handlerMethod.getMethodParameters()[0];
231232

232233
String content = "{\"name\" : \"Jad\"}";
233-
this.servletRequest.setContent(content.getBytes("UTF-8"));
234+
this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8));
234235
this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE);
235236

236237
List<HttpMessageConverter<?>> converters = new ArrayList<>();
@@ -250,7 +251,7 @@ public void resolveParameterizedWithTypeVariableArgument() throws Exception {
250251
MethodParameter methodParam = handlerMethod.getMethodParameters()[0];
251252

252253
String content = "[{\"name\" : \"Jad\"}, {\"name\" : \"Robert\"}]";
253-
this.servletRequest.setContent(content.getBytes("UTF-8"));
254+
this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8));
254255
this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE);
255256

256257
List<HttpMessageConverter<?>> converters = new ArrayList<>();
@@ -273,7 +274,7 @@ public void resolveArgumentTypeVariableWithNonGenericConverter() throws Exceptio
273274
MethodParameter methodParam = handlerMethod.getMethodParameters()[0];
274275

275276
String content = "{\"name\" : \"Jad\"}";
276-
this.servletRequest.setContent(content.getBytes("UTF-8"));
277+
this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8));
277278
this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE);
278279

279280
List<HttpMessageConverter<?>> converters = new ArrayList<>();
@@ -609,7 +610,7 @@ public void jacksonJsonViewWithResponseEntityAndXmlMessageConverter() throws Exc
609610
@Test // SPR-12501
610611
public void resolveArgumentWithJacksonJsonView() throws Exception {
611612
String content = "{\"withView1\" : \"with\", \"withView2\" : \"with\", \"withoutView\" : \"without\"}";
612-
this.servletRequest.setContent(content.getBytes("UTF-8"));
613+
this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8));
613614
this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE);
614615

615616
Method method = JacksonController.class.getMethod("handleRequestBody", JacksonViewBean.class);
@@ -634,7 +635,7 @@ public void resolveArgumentWithJacksonJsonView() throws Exception {
634635
@Test // SPR-12501
635636
public void resolveHttpEntityArgumentWithJacksonJsonView() throws Exception {
636637
String content = "{\"withView1\" : \"with\", \"withView2\" : \"with\", \"withoutView\" : \"without\"}";
637-
this.servletRequest.setContent(content.getBytes("UTF-8"));
638+
this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8));
638639
this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE);
639640

640641
Method method = JacksonController.class.getMethod("handleHttpEntity", HttpEntity.class);
@@ -664,7 +665,7 @@ public void resolveArgumentWithJacksonJsonViewAndXmlMessageConverter() throws Ex
664665
"<withView1>with</withView1>" +
665666
"<withView2>with</withView2>" +
666667
"<withoutView>without</withoutView></root>";
667-
this.servletRequest.setContent(content.getBytes("UTF-8"));
668+
this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8));
668669
this.servletRequest.setContentType(MediaType.APPLICATION_XML_VALUE);
669670

670671
Method method = JacksonController.class.getMethod("handleRequestBody", JacksonViewBean.class);
@@ -692,7 +693,7 @@ public void resolveHttpEntityArgumentWithJacksonJsonViewAndXmlMessageConverter()
692693
"<withView1>with</withView1>" +
693694
"<withView2>with</withView2>" +
694695
"<withoutView>without</withoutView></root>";
695-
this.servletRequest.setContent(content.getBytes("UTF-8"));
696+
this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8));
696697
this.servletRequest.setContentType(MediaType.APPLICATION_XML_VALUE);
697698

698699
Method method = JacksonController.class.getMethod("handleHttpEntity", HttpEntity.class);
@@ -774,7 +775,7 @@ public void jacksonSubTypeList() throws Exception {
774775

775776
@Test // SPR-14520
776777
public void resolveArgumentTypeVariableWithGenericInterface() throws Exception {
777-
this.servletRequest.setContent("\"foo\"".getBytes("UTF-8"));
778+
this.servletRequest.setContent("\"foo\"".getBytes(StandardCharsets.UTF_8));
778779
this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE);
779780

780781
Method method = MyControllerImplementingInterface.class.getMethod("handle", Object.class);
@@ -794,7 +795,7 @@ public void resolveArgumentTypeVariableWithGenericInterface() throws Exception {
794795

795796
@Test // gh-24127
796797
public void resolveArgumentTypeVariableWithGenericInterfaceAndSubclass() throws Exception {
797-
this.servletRequest.setContent("\"foo\"".getBytes("UTF-8"));
798+
this.servletRequest.setContent("\"foo\"".getBytes(StandardCharsets.UTF_8));
798799
this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE);
799800

800801
Method method = SubControllerImplementingInterface.class.getMethod("handle", Object.class);
@@ -836,6 +837,7 @@ private void assertContentDisposition(RequestResponseBodyMethodProcessor process
836837
}
837838

838839

840+
@SuppressWarnings("ConstantConditions")
839841
String handle(
840842
@RequestBody List<SimpleBean> list,
841843
@RequestBody SimpleBean simpleBean,
@@ -845,19 +847,23 @@ String handle(
845847
return null;
846848
}
847849

850+
@SuppressWarnings("ConstantConditions")
848851
Resource getImage() {
849852
return null;
850853
}
851854

855+
@SuppressWarnings("ConstantConditions")
852856
ProblemDetail handleAndReturnProblemDetail() {
853857
return null;
854858
}
855859

860+
@SuppressWarnings("ConstantConditions")
856861
@RequestMapping
857862
OutputStream handleAndReturnOutputStream() {
858863
return null;
859864
}
860865

866+
@SuppressWarnings("ConstantConditions")
861867
SimpleBean getSimpleBean() {
862868
return null;
863869
}
@@ -895,7 +901,7 @@ private static class MySimpleParameterizedControllerWithList extends MyParameter
895901
}
896902

897903

898-
@SuppressWarnings({ "serial" })
904+
@SuppressWarnings("NotNullFieldNotInitialized")
899905
private static class SimpleBean implements Identifiable {
900906

901907
private Long id;
@@ -922,7 +928,7 @@ public void setName(String name) {
922928
}
923929

924930

925-
private final class ValidatingBinderFactory implements WebDataBinderFactory {
931+
private static final class ValidatingBinderFactory implements WebDataBinderFactory {
926932

927933
@Override
928934
public WebDataBinder createBinder(NativeWebRequest request, @Nullable Object target, String objectName) {
@@ -943,6 +949,7 @@ public String handle() {
943949
return "hello";
944950
}
945951

952+
@SuppressWarnings("ConstantConditions")
946953
@RequestMapping
947954
public CharSequence handleWithCharSequence() {
948955
return null;
@@ -965,6 +972,7 @@ private interface MyJacksonView1 {}
965972
private interface MyJacksonView2 {}
966973

967974

975+
@SuppressWarnings("NotNullFieldNotInitialized")
968976
private static class JacksonViewBean {
969977

970978
@JsonView(MyJacksonView1.class)
@@ -983,6 +991,7 @@ public void setWithView1(String withView1) {
983991
this.withView1 = withView1;
984992
}
985993

994+
@Nullable
986995
public String getWithView2() {
987996
return withView2;
988997
}
@@ -991,6 +1000,7 @@ public void setWithView2(String withView2) {
9911000
this.withView2 = withView2;
9921001
}
9931002

1003+
@Nullable
9941004
public String getWithoutView() {
9951005
return withoutView;
9961006
}
@@ -1001,7 +1011,8 @@ public void setWithoutView(String withoutView) {
10011011
}
10021012

10031013

1004-
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
1014+
@SuppressWarnings("NotNullFieldNotInitialized")
1015+
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
10051016
public static class ParentClass {
10061017

10071018
private String parentProperty;
@@ -1092,6 +1103,7 @@ public JacksonViewBean handleRequestBody(@JsonView(MyJacksonView1.class) @Reques
10921103
return bean;
10931104
}
10941105

1106+
@SuppressWarnings("ConstantConditions")
10951107
@RequestMapping
10961108
@ResponseBody
10971109
public JacksonViewBean handleHttpEntity(@JsonView(MyJacksonView1.class) HttpEntity<JacksonViewBean> entity) {

0 commit comments

Comments
 (0)