Skip to content

Commit e228702

Browse files
committed
Polishing
See gh-35889
1 parent 9e18c75 commit e228702

File tree

1 file changed

+46
-48
lines changed

1 file changed

+46
-48
lines changed

spring-web/src/test/java/org/springframework/http/converter/json/JacksonJsonHttpMessageConverterTests.java

Lines changed: 46 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -70,64 +70,64 @@ class JacksonJsonHttpMessageConverterTests {
7070

7171
@Test
7272
void canRead() {
73-
assertThat(converter.canRead(MyBean.class, new MediaType("application", "json"))).isTrue();
74-
assertThat(converter.canRead(Map.class, new MediaType("application", "json"))).isTrue();
75-
assertThat(converter.canRead(MyBean.class, new MediaType("application", "json", StandardCharsets.UTF_8))).isTrue();
76-
assertThat(converter.canRead(MyBean.class, new MediaType("application", "json", StandardCharsets.US_ASCII))).isTrue();
77-
assertThat(converter.canRead(MyBean.class, new MediaType("application", "json", StandardCharsets.ISO_8859_1))).isTrue();
73+
assertThat(this.converter.canRead(MyBean.class, MediaType.APPLICATION_JSON)).isTrue();
74+
assertThat(this.converter.canRead(Map.class, MediaType.APPLICATION_JSON)).isTrue();
75+
assertThat(this.converter.canRead(MyBean.class, new MediaType("application", "json", StandardCharsets.UTF_8))).isTrue();
76+
assertThat(this.converter.canRead(MyBean.class, new MediaType("application", "json", StandardCharsets.US_ASCII))).isTrue();
77+
assertThat(this.converter.canRead(MyBean.class, new MediaType("application", "json", StandardCharsets.ISO_8859_1))).isTrue();
7878
}
7979

8080
@Test
8181
void canReadWithObjectMapperRegistrationForType() {
8282
MediaType halJsonMediaType = MediaType.parseMediaType("application/hal+json");
8383
MediaType halFormsJsonMediaType = MediaType.parseMediaType("application/prs.hal-forms+json");
8484

85-
assertThat(converter.canRead(MyBean.class, halJsonMediaType)).isTrue();
86-
assertThat(converter.canRead(MyBean.class, MediaType.APPLICATION_JSON)).isTrue();
87-
assertThat(converter.canRead(MyBean.class, halFormsJsonMediaType)).isTrue();
88-
assertThat(converter.canRead(Map.class, MediaType.APPLICATION_JSON)).isTrue();
85+
assertThat(this.converter.canRead(MyBean.class, halJsonMediaType)).isTrue();
86+
assertThat(this.converter.canRead(MyBean.class, MediaType.APPLICATION_JSON)).isTrue();
87+
assertThat(this.converter.canRead(MyBean.class, halFormsJsonMediaType)).isTrue();
88+
assertThat(this.converter.canRead(Map.class, MediaType.APPLICATION_JSON)).isTrue();
8989

90-
converter.registerMappersForType(MyBean.class, map -> {
90+
this.converter.registerMappersForType(MyBean.class, map -> {
9191
map.put(halJsonMediaType, new JsonMapper());
9292
map.put(MediaType.APPLICATION_JSON, new JsonMapper());
9393
});
9494

95-
assertThat(converter.canRead(MyBean.class, halJsonMediaType)).isTrue();
96-
assertThat(converter.canRead(MyBean.class, MediaType.APPLICATION_JSON)).isTrue();
97-
assertThat(converter.canRead(Map.class, MediaType.APPLICATION_JSON)).isTrue();
95+
assertThat(this.converter.canRead(MyBean.class, halJsonMediaType)).isTrue();
96+
assertThat(this.converter.canRead(MyBean.class, MediaType.APPLICATION_JSON)).isTrue();
97+
assertThat(this.converter.canRead(Map.class, MediaType.APPLICATION_JSON)).isTrue();
9898
}
9999

100100
@Test
101101
@SuppressWarnings("removal")
102102
void canWrite() {
103-
assertThat(converter.canWrite(MyBean.class, MediaType.APPLICATION_JSON)).isTrue();
104-
assertThat(converter.canWrite(Map.class, MediaType.APPLICATION_JSON)).isTrue();
105-
assertThat(converter.canWrite(MyBean.class, new MediaType("application", "json", StandardCharsets.UTF_8))).isTrue();
106-
assertThat(converter.canWrite(MyBean.class, new MediaType("application", "json", StandardCharsets.US_ASCII))).isTrue();
107-
assertThat(converter.canWrite(MyBean.class, new MediaType("application", "json", StandardCharsets.ISO_8859_1))).isFalse();
108-
assertThatThrownBy(() -> converter.canWrite(MappingJacksonValue.class, MediaType.APPLICATION_JSON)).isInstanceOf(UnsupportedOperationException.class);
103+
assertThat(this.converter.canWrite(MyBean.class, MediaType.APPLICATION_JSON)).isTrue();
104+
assertThat(this.converter.canWrite(Map.class, MediaType.APPLICATION_JSON)).isTrue();
105+
assertThat(this.converter.canWrite(MyBean.class, new MediaType("application", "json", StandardCharsets.UTF_8))).isTrue();
106+
assertThat(this.converter.canWrite(MyBean.class, new MediaType("application", "json", StandardCharsets.US_ASCII))).isTrue();
107+
assertThat(this.converter.canWrite(MyBean.class, new MediaType("application", "json", StandardCharsets.ISO_8859_1))).isFalse();
108+
assertThatThrownBy(() -> this.converter.canWrite(MappingJacksonValue.class, MediaType.APPLICATION_JSON)).isInstanceOf(UnsupportedOperationException.class);
109109
}
110110

111111
@Test // SPR-7905
112112
void canReadAndWriteMicroformats() {
113-
assertThat(converter.canRead(MyBean.class, new MediaType("application", "vnd.test-micro-type+json"))).isTrue();
114-
assertThat(converter.canWrite(MyBean.class, new MediaType("application", "vnd.test-micro-type+json"))).isTrue();
113+
assertThat(this.converter.canRead(MyBean.class, new MediaType("application", "vnd.test-micro-type+json"))).isTrue();
114+
assertThat(this.converter.canWrite(MyBean.class, new MediaType("application", "vnd.test-micro-type+json"))).isTrue();
115115
}
116116

117117
@Test
118118
void getSupportedMediaTypes() {
119119
MediaType[] defaultMediaTypes = {MediaType.APPLICATION_JSON, MediaType.parseMediaType("application/*+json")};
120-
assertThat(converter.getSupportedMediaTypes()).containsExactly(defaultMediaTypes);
121-
assertThat(converter.getSupportedMediaTypes(MyBean.class)).containsExactly(defaultMediaTypes);
120+
assertThat(this.converter.getSupportedMediaTypes()).containsExactly(defaultMediaTypes);
121+
assertThat(this.converter.getSupportedMediaTypes(MyBean.class)).containsExactly(defaultMediaTypes);
122122

123123
MediaType halJson = MediaType.parseMediaType("application/hal+json");
124-
converter.registerMappersForType(MyBean.class, map -> {
124+
this.converter.registerMappersForType(MyBean.class, map -> {
125125
map.put(halJson, new JsonMapper());
126126
map.put(MediaType.APPLICATION_JSON, new JsonMapper());
127127
});
128128

129-
assertThat(converter.getSupportedMediaTypes(MyBean.class)).containsExactly(halJson, MediaType.APPLICATION_JSON);
130-
assertThat(converter.getSupportedMediaTypes(Map.class)).containsExactly(defaultMediaTypes);
129+
assertThat(this.converter.getSupportedMediaTypes(MyBean.class)).containsExactly(halJson, MediaType.APPLICATION_JSON);
130+
assertThat(this.converter.getSupportedMediaTypes(Map.class)).containsExactly(defaultMediaTypes);
131131
}
132132

133133
@Test
@@ -141,7 +141,7 @@ void readTyped() throws IOException {
141141
"\"fraction\":42.0}";
142142
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
143143
inputMessage.getHeaders().setContentType(new MediaType("application", "json"));
144-
MyBean result = (MyBean) converter.read(MyBean.class, inputMessage);
144+
MyBean result = (MyBean) this.converter.read(MyBean.class, inputMessage);
145145
assertThat(result.getString()).isEqualTo("Foo");
146146
assertThat(result.getNumber()).isEqualTo(42);
147147
assertThat(result.getFraction()).isCloseTo(42F, within(0F));
@@ -162,7 +162,7 @@ void readUntyped() throws IOException {
162162
"\"fraction\":42.0}";
163163
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
164164
inputMessage.getHeaders().setContentType(new MediaType("application", "json"));
165-
HashMap<String, Object> result = (HashMap<String, Object>) converter.read(HashMap.class, inputMessage);
165+
HashMap<String, Object> result = (HashMap<String, Object>) this.converter.read(HashMap.class, inputMessage);
166166
assertThat(result).containsEntry("string", "Foo");
167167
assertThat(result).containsEntry("number", 42);
168168
assertThat((Double) result.get("fraction")).isCloseTo(42D, within(0D));
@@ -184,7 +184,7 @@ void write() throws IOException {
184184
body.setArray(new String[] {"Foo", "Bar"});
185185
body.setBool(true);
186186
body.setBytes(new byte[] {0x1, 0x2});
187-
converter.write(body, null, outputMessage);
187+
this.converter.write(body, null, outputMessage);
188188
String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8);
189189
assertThat(result).contains("\"string\":\"Foo\"");
190190
assertThat(result).contains("\"number\":42");
@@ -206,7 +206,7 @@ void writeWithBaseType() throws IOException {
206206
body.setArray(new String[] {"Foo", "Bar"});
207207
body.setBool(true);
208208
body.setBytes(new byte[] {0x1, 0x2});
209-
converter.write(body, ResolvableType.forClass(MyBase.class), null, outputMessage, null);
209+
this.converter.write(body, ResolvableType.forClass(MyBase.class), null, outputMessage, null);
210210
String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8);
211211
assertThat(result).contains("\"string\":\"Foo\"");
212212
assertThat(result).contains("\"number\":42");
@@ -223,7 +223,7 @@ void writeUTF16() throws IOException {
223223
MediaType contentType = new MediaType("application", "json", StandardCharsets.UTF_16BE);
224224
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
225225
String body = "H\u00e9llo W\u00f6rld";
226-
converter.write(body, contentType, outputMessage);
226+
this.converter.write(body, contentType, outputMessage);
227227
assertThat(outputMessage.getBodyAsString(StandardCharsets.UTF_16BE)).as("Invalid result").isEqualTo(("\"" + body + "\""));
228228
assertThat(outputMessage.getHeaders().getContentType()).as("Invalid content-type").isEqualTo(contentType);
229229
}
@@ -234,22 +234,22 @@ void readInvalidJson() {
234234
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
235235
inputMessage.getHeaders().setContentType(new MediaType("application", "json"));
236236
assertThatExceptionOfType(HttpMessageNotReadableException.class)
237-
.isThrownBy(() -> converter.read(MyBean.class, inputMessage));
237+
.isThrownBy(() -> this.converter.read(MyBean.class, inputMessage));
238238
}
239239

240240
@Test
241241
void readValidJsonWithUnknownProperty() throws IOException {
242242
String body = "{\"string\":\"string\",\"unknownProperty\":\"value\"}";
243243
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
244244
inputMessage.getHeaders().setContentType(new MediaType("application", "json"));
245-
converter.read(MyBean.class, inputMessage);
245+
this.converter.read(MyBean.class, inputMessage);
246246
// Assert no HttpMessageNotReadableException is thrown
247247
}
248248

249249
@Test
250250
@SuppressWarnings("unchecked")
251251
void readAndWriteGenerics() throws Exception {
252-
JacksonJsonHttpMessageConverter converter = new JacksonJsonHttpMessageConverter() {
252+
JacksonJsonHttpMessageConverter customizedConverter = new JacksonJsonHttpMessageConverter() {
253253
@Override
254254
protected JavaType getJavaType(Type type, @Nullable Class<?> contextClass) {
255255
if (type instanceof Class && List.class.isAssignableFrom((Class<?>)type)) {
@@ -270,7 +270,7 @@ protected JavaType getJavaType(Type type, @Nullable Class<?> contextClass) {
270270
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
271271
inputMessage.getHeaders().setContentType(new MediaType("application", "json"));
272272

273-
List<MyBean> results = (List<MyBean>) converter.read(List.class, inputMessage);
273+
List<MyBean> results = (List<MyBean>) customizedConverter.read(List.class, inputMessage);
274274
assertThat(results).hasSize(1);
275275
MyBean result = results.get(0);
276276
assertThat(result.getString()).isEqualTo("Foo");
@@ -281,7 +281,7 @@ protected JavaType getJavaType(Type type, @Nullable Class<?> contextClass) {
281281
assertThat(result.getBytes()).isEqualTo(new byte[] {0x1, 0x2});
282282

283283
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
284-
converter.write(results, MediaType.APPLICATION_JSON, outputMessage);
284+
customizedConverter.write(results, MediaType.APPLICATION_JSON, outputMessage);
285285
JSONAssert.assertEquals(body, outputMessage.getBodyAsString(StandardCharsets.UTF_8), true);
286286
}
287287

@@ -300,8 +300,7 @@ void readAndWriteParameterizedType() throws Exception {
300300
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
301301
inputMessage.getHeaders().setContentType(MediaType.APPLICATION_JSON);
302302

303-
JacksonJsonHttpMessageConverter converter = new JacksonJsonHttpMessageConverter();
304-
List<MyBean> results = (List<MyBean>) converter.read(ResolvableType.forType(beansList), inputMessage, null);
303+
List<MyBean> results = (List<MyBean>) this.converter.read(ResolvableType.forType(beansList), inputMessage, null);
305304
assertThat(results).hasSize(1);
306305
MyBean result = results.get(0);
307306
assertThat(result.getString()).isEqualTo("Foo");
@@ -312,7 +311,7 @@ void readAndWriteParameterizedType() throws Exception {
312311
assertThat(result.getBytes()).isEqualTo(new byte[] {0x1, 0x2});
313312

314313
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
315-
converter.write(results, ResolvableType.forType(beansList), MediaType.APPLICATION_JSON, outputMessage, null);
314+
this.converter.write(results, ResolvableType.forType(beansList), MediaType.APPLICATION_JSON, outputMessage, null);
316315
JSONAssert.assertEquals(body, outputMessage.getBodyAsString(StandardCharsets.UTF_8), true);
317316
}
318317

@@ -332,8 +331,7 @@ void writeParameterizedBaseType() throws Exception {
332331
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
333332
inputMessage.getHeaders().setContentType(MediaType.APPLICATION_JSON);
334333

335-
JacksonJsonHttpMessageConverter converter = new JacksonJsonHttpMessageConverter();
336-
List<MyBean> results = (List<MyBean>) converter.read(ResolvableType.forType(beansList), inputMessage, null);
334+
List<MyBean> results = (List<MyBean>) this.converter.read(ResolvableType.forType(beansList), inputMessage, null);
337335
assertThat(results).hasSize(1);
338336
MyBean result = results.get(0);
339337
assertThat(result.getString()).isEqualTo("Foo");
@@ -344,7 +342,7 @@ void writeParameterizedBaseType() throws Exception {
344342
assertThat(result.getBytes()).isEqualTo(new byte[] {0x1, 0x2});
345343

346344
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
347-
converter.write(results, ResolvableType.forType(baseList), MediaType.APPLICATION_JSON, outputMessage, null);
345+
this.converter.write(results, ResolvableType.forType(baseList), MediaType.APPLICATION_JSON, outputMessage, null);
348346
JSONAssert.assertEquals(body, outputMessage.getBodyAsString(StandardCharsets.UTF_8), true);
349347
}
350348

@@ -353,7 +351,7 @@ void writeOptional() throws IOException {
353351
ParameterizedTypeReference<Optional<MyParent>> optionalParent = new ParameterizedTypeReference<>() {};
354352
Optional<MyParent> result = Optional.of(new Impl1());
355353
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
356-
converter.write(result, ResolvableType.forType(optionalParent.getType()),
354+
this.converter.write(result, ResolvableType.forType(optionalParent.getType()),
357355
MediaType.APPLICATION_JSON, outputMessage, null);
358356

359357
assertThat(outputMessage.getBodyAsString(StandardCharsets.UTF_8)).contains("@type");
@@ -550,7 +548,7 @@ void writeAscii() throws Exception {
550548
body.put("foo", "bar");
551549
Charset charset = StandardCharsets.US_ASCII;
552550
MediaType contentType = new MediaType("application", "json", charset);
553-
converter.write(body, contentType, outputMessage);
551+
this.converter.write(body, contentType, outputMessage);
554552

555553
String result = outputMessage.getBodyAsString(charset);
556554
assertThat(result).isEqualTo("{\"foo\":\"bar\"}");
@@ -570,7 +568,7 @@ void readWithCustomized() throws IOException {
570568
assertThatExceptionOfType(HttpMessageNotReadableException.class)
571569
.isThrownBy(() -> customizedConverter.read(MyCustomizedBean.class, inputMessage1));
572570

573-
MyCustomizedBean customizedResult = (MyCustomizedBean) converter.read(MyCustomizedBean.class, inputMessage2);
571+
MyCustomizedBean customizedResult = (MyCustomizedBean) this.converter.read(MyCustomizedBean.class, inputMessage2);
574572
assertThat(customizedResult.getProperty()).isEqualTo(MyCustomEnum.VAL1);
575573
}
576574

@@ -582,7 +580,7 @@ void writeWithCustomized() throws IOException {
582580
MockHttpOutputMessage outputMessage2 = new MockHttpOutputMessage();
583581
MyCustomizedBean body = new MyCustomizedBean();
584582
body.setProperty(MyCustomEnum.VAL2);
585-
converter.write(body, null, outputMessage1);
583+
this.converter.write(body, null, outputMessage1);
586584
customizedConverter.write(body, null, outputMessage2);
587585
String result1 = outputMessage1.getBodyAsString(StandardCharsets.UTF_8);
588586
assertThat(result1).contains("\"property\":\"Value2\"");
@@ -595,12 +593,12 @@ void repeatableWrites() throws IOException {
595593
MockHttpOutputMessage outputMessage1 = new MockHttpOutputMessage();
596594
MyBean body = new MyBean();
597595
body.setString("Foo");
598-
converter.write(body, null, outputMessage1);
596+
this.converter.write(body, null, outputMessage1);
599597
String result = outputMessage1.getBodyAsString(StandardCharsets.UTF_8);
600598
assertThat(result).contains("\"string\":\"Foo\"");
601599

602600
MockHttpOutputMessage outputMessage2 = new MockHttpOutputMessage();
603-
converter.write(body, null, outputMessage2);
601+
this.converter.write(body, null, outputMessage2);
604602
result = outputMessage2.getBodyAsString(StandardCharsets.UTF_8);
605603
assertThat(result).contains("\"string\":\"Foo\"");
606604
}

0 commit comments

Comments
 (0)