Skip to content

Commit 354635e

Browse files
committed
Polishing
1 parent f19afe0 commit 354635e

File tree

2 files changed

+111
-102
lines changed

2 files changed

+111
-102
lines changed

spring-web/src/test/java/org/springframework/http/codec/json/Jackson2JsonDecoderTests.java

+47-52
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.math.BigDecimal;
2121
import java.nio.charset.Charset;
2222
import java.nio.charset.StandardCharsets;
23+
import java.util.Arrays;
2324
import java.util.Collections;
2425
import java.util.List;
2526
import java.util.Map;
@@ -47,12 +48,8 @@
4748
import org.springframework.util.MimeType;
4849
import org.springframework.web.testfixture.xml.Pojo;
4950

50-
import static java.util.Arrays.asList;
51-
import static java.util.Collections.emptyMap;
52-
import static java.util.Collections.singletonMap;
5351
import static org.assertj.core.api.Assertions.assertThat;
5452
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
55-
import static org.springframework.core.ResolvableType.forClass;
5653
import static org.springframework.http.MediaType.APPLICATION_JSON;
5754
import static org.springframework.http.MediaType.APPLICATION_STREAM_JSON;
5855
import static org.springframework.http.MediaType.APPLICATION_XML;
@@ -79,12 +76,12 @@ public Jackson2JsonDecoderTests() {
7976
@Override
8077
@Test
8178
public void canDecode() {
82-
assertThat(decoder.canDecode(forClass(Pojo.class), APPLICATION_JSON)).isTrue();
83-
assertThat(decoder.canDecode(forClass(Pojo.class), APPLICATION_STREAM_JSON)).isTrue();
84-
assertThat(decoder.canDecode(forClass(Pojo.class), null)).isTrue();
79+
assertThat(decoder.canDecode(ResolvableType.forClass(Pojo.class), APPLICATION_JSON)).isTrue();
80+
assertThat(decoder.canDecode(ResolvableType.forClass(Pojo.class), APPLICATION_STREAM_JSON)).isTrue();
81+
assertThat(decoder.canDecode(ResolvableType.forClass(Pojo.class), null)).isTrue();
8582

86-
assertThat(decoder.canDecode(forClass(String.class), null)).isFalse();
87-
assertThat(decoder.canDecode(forClass(Pojo.class), APPLICATION_XML)).isFalse();
83+
assertThat(decoder.canDecode(ResolvableType.forClass(String.class), null)).isFalse();
84+
assertThat(decoder.canDecode(ResolvableType.forClass(Pojo.class), APPLICATION_XML)).isFalse();
8885
assertThat(this.decoder.canDecode(ResolvableType.forClass(Pojo.class),
8986
new MediaType("application", "json", StandardCharsets.UTF_8))).isTrue();
9087
assertThat(this.decoder.canDecode(ResolvableType.forClass(Pojo.class),
@@ -133,7 +130,7 @@ public void decodeToMono() {
133130
ResolvableType elementType = ResolvableType.forClassWithGenerics(List.class, Pojo.class);
134131

135132
testDecodeToMonoAll(input, elementType, step -> step
136-
.expectNext(asList(new Pojo("f1", "b1"), new Pojo("f2", "b2")))
133+
.expectNext(Arrays.asList(new Pojo("f1", "b1"), new Pojo("f2", "b2")))
137134
.expectComplete()
138135
.verify(), null, null);
139136
}
@@ -143,48 +140,48 @@ public void decodeToMono() {
143140
public void decodeEmptyArrayToFlux() {
144141
Flux<DataBuffer> input = Flux.from(stringBuffer("[]"));
145142

146-
testDecode(input, Pojo.class, step -> step.verifyComplete());
143+
testDecode(input, Pojo.class, StepVerifier.LastStep::verifyComplete);
147144
}
148145

149146
@Test
150147
public void fieldLevelJsonView() {
151-
Flux<DataBuffer> input = Flux.from(
152-
stringBuffer("{\"withView1\" : \"with\", \"withView2\" : \"with\", \"withoutView\" : \"without\"}"));
153-
ResolvableType elementType = forClass(JacksonViewBean.class);
154-
Map<String, Object> hints = singletonMap(JSON_VIEW_HINT, MyJacksonView1.class);
148+
Flux<DataBuffer> input = Flux.from(stringBuffer(
149+
"{\"withView1\" : \"with\", \"withView2\" : \"with\", \"withoutView\" : \"without\"}"));
150+
151+
ResolvableType elementType = ResolvableType.forClass(JacksonViewBean.class);
152+
Map<String, Object> hints = Collections.singletonMap(JSON_VIEW_HINT, MyJacksonView1.class);
155153

156154
testDecode(input, elementType, step -> step
157-
.consumeNextWith(o -> {
158-
JacksonViewBean b = (JacksonViewBean) o;
159-
assertThat(b.getWithView1()).isEqualTo("with");
160-
assertThat(b.getWithView2()).isNull();
161-
assertThat(b.getWithoutView()).isNull();
155+
.consumeNextWith(value -> {
156+
JacksonViewBean bean = (JacksonViewBean) value;
157+
assertThat(bean.getWithView1()).isEqualTo("with");
158+
assertThat(bean.getWithView2()).isNull();
159+
assertThat(bean.getWithoutView()).isNull();
162160
}), null, hints);
163161
}
164162

165163
@Test
166164
public void classLevelJsonView() {
167165
Flux<DataBuffer> input = Flux.from(stringBuffer(
168166
"{\"withView1\" : \"with\", \"withView2\" : \"with\", \"withoutView\" : \"without\"}"));
169-
ResolvableType elementType = forClass(JacksonViewBean.class);
170-
Map<String, Object> hints = singletonMap(JSON_VIEW_HINT, MyJacksonView3.class);
167+
168+
ResolvableType elementType = ResolvableType.forClass(JacksonViewBean.class);
169+
Map<String, Object> hints = Collections.singletonMap(JSON_VIEW_HINT, MyJacksonView3.class);
171170

172171
testDecode(input, elementType, step -> step
173-
.consumeNextWith(o -> {
174-
JacksonViewBean b = (JacksonViewBean) o;
175-
assertThat(b.getWithoutView()).isEqualTo("without");
176-
assertThat(b.getWithView1()).isNull();
177-
assertThat(b.getWithView2()).isNull();
172+
.consumeNextWith(value -> {
173+
JacksonViewBean bean = (JacksonViewBean) value;
174+
assertThat(bean.getWithoutView()).isEqualTo("without");
175+
assertThat(bean.getWithView1()).isNull();
176+
assertThat(bean.getWithView2()).isNull();
178177
})
179178
.verifyComplete(), null, hints);
180179
}
181180

182181
@Test
183182
public void invalidData() {
184-
Flux<DataBuffer> input =
185-
Flux.from(stringBuffer("{\"foofoo\": \"foofoo\", \"barbar\": \"barbar\""));
186-
testDecode(input, Pojo.class, step -> step
187-
.verifyError(DecodingException.class));
183+
Flux<DataBuffer> input = Flux.from(stringBuffer("{\"foofoo\": \"foofoo\", \"barbar\": \"barbar\""));
184+
testDecode(input, Pojo.class, step -> step.verifyError(DecodingException.class));
188185
}
189186

190187
@Test // gh-22042
@@ -197,10 +194,9 @@ public void decodeWithNullLiteral() {
197194

198195
@Test
199196
public void noDefaultConstructor() {
200-
Flux<DataBuffer> input =
201-
Flux.from(stringBuffer("{\"property1\":\"foo\",\"property2\":\"bar\"}"));
202-
ResolvableType elementType = forClass(BeanWithNoDefaultConstructor.class);
203-
Flux<Object> flux = new Jackson2JsonDecoder().decode(input, elementType, null, emptyMap());
197+
Flux<DataBuffer> input = Flux.from(stringBuffer("{\"property1\":\"foo\",\"property2\":\"bar\"}"));
198+
ResolvableType elementType = ResolvableType.forClass(BeanWithNoDefaultConstructor.class);
199+
Flux<Object> flux = new Jackson2JsonDecoder().decode(input, elementType, null, Collections.emptyMap());
204200
StepVerifier.create(flux).verifyError(CodecException.class);
205201
}
206202

@@ -228,9 +224,10 @@ public void bigDecimalFlux() {
228224
@SuppressWarnings("unchecked")
229225
public void decodeNonUtf8Encoding() {
230226
Mono<DataBuffer> input = stringBuffer("{\"foo\":\"bar\"}", StandardCharsets.UTF_16);
227+
ResolvableType type = ResolvableType.forType(new ParameterizedTypeReference<Map<String, String>>() {});
231228

232-
testDecode(input, ResolvableType.forType(new ParameterizedTypeReference<Map<String, String>>() {}),
233-
step -> step.assertNext(o -> assertThat((Map<String, String>) o).containsEntry("foo", "bar"))
229+
testDecode(input, type, step -> step
230+
.assertNext(value -> assertThat((Map<String, String>) value).containsEntry("foo", "bar"))
234231
.verifyComplete(),
235232
MediaType.parseMediaType("application/json; charset=utf-16"),
236233
null);
@@ -239,12 +236,11 @@ public void decodeNonUtf8Encoding() {
239236
@Test
240237
@SuppressWarnings("unchecked")
241238
public void decodeNonUnicode() {
242-
Flux<DataBuffer> input = Flux.concat(
243-
stringBuffer("{\"føø\":\"bår\"}", StandardCharsets.ISO_8859_1)
244-
);
239+
Flux<DataBuffer> input = Flux.concat(stringBuffer("{\"føø\":\"bår\"}", StandardCharsets.ISO_8859_1));
240+
ResolvableType type = ResolvableType.forType(new ParameterizedTypeReference<Map<String, String>>() {});
245241

246-
testDecode(input, ResolvableType.forType(new ParameterizedTypeReference<Map<String, String>>() {}),
247-
step -> step.assertNext(o -> assertThat((Map<String, String>) o).containsEntry("føø", "bår"))
242+
testDecode(input, type, step -> step
243+
.assertNext(o -> assertThat((Map<String, String>) o).containsEntry("føø", "bår"))
248244
.verifyComplete(),
249245
MediaType.parseMediaType("application/json; charset=iso-8859-1"),
250246
null);
@@ -254,9 +250,10 @@ public void decodeNonUnicode() {
254250
@SuppressWarnings("unchecked")
255251
public void decodeMonoNonUtf8Encoding() {
256252
Mono<DataBuffer> input = stringBuffer("{\"foo\":\"bar\"}", StandardCharsets.UTF_16);
253+
ResolvableType type = ResolvableType.forType(new ParameterizedTypeReference<Map<String, String>>() {});
257254

258-
testDecodeToMono(input, ResolvableType.forType(new ParameterizedTypeReference<Map<String, String>>() {}),
259-
step -> step.assertNext(o -> assertThat((Map<String, String>) o).containsEntry("foo", "bar"))
255+
testDecodeToMono(input, type, step -> step
256+
.assertNext(value -> assertThat((Map<String, String>) value).containsEntry("foo", "bar"))
260257
.verifyComplete(),
261258
MediaType.parseMediaType("application/json; charset=utf-16"),
262259
null);
@@ -265,12 +262,11 @@ public void decodeMonoNonUtf8Encoding() {
265262
@Test
266263
@SuppressWarnings("unchecked")
267264
public void decodeAscii() {
268-
Flux<DataBuffer> input = Flux.concat(
269-
stringBuffer("{\"foo\":\"bar\"}", StandardCharsets.US_ASCII)
270-
);
265+
Flux<DataBuffer> input = Flux.concat(stringBuffer("{\"foo\":\"bar\"}", StandardCharsets.US_ASCII));
266+
ResolvableType type = ResolvableType.forType(new ParameterizedTypeReference<Map<String, String>>() {});
271267

272-
testDecode(input, ResolvableType.forType(new ParameterizedTypeReference<Map<String, String>>() {}),
273-
step -> step.assertNext(o -> assertThat((Map<String, String>) o).containsEntry("foo", "bar"))
268+
testDecode(input, type, step -> step
269+
.assertNext(value -> assertThat((Map<String, String>) value).containsEntry("foo", "bar"))
274270
.verifyComplete(),
275271
MediaType.parseMediaType("application/json; charset=us-ascii"),
276272
null);
@@ -291,7 +287,6 @@ private Mono<DataBuffer> stringBuffer(String value, Charset charset) {
291287
}
292288

293289

294-
@SuppressWarnings("unused")
295290
private static class BeanWithNoDefaultConstructor {
296291

297292
private final String property1;
@@ -314,7 +309,7 @@ public String getProperty2() {
314309

315310

316311
@JsonDeserialize(using = Deserializer.class)
317-
public static class TestObject {
312+
private static class TestObject {
318313

319314
private int test;
320315

@@ -327,7 +322,7 @@ public void setTest(int test) {
327322
}
328323

329324

330-
public static class Deserializer extends StdDeserializer<TestObject> {
325+
private static class Deserializer extends StdDeserializer<TestObject> {
331326

332327
private static final long serialVersionUID = 1L;
333328

0 commit comments

Comments
 (0)