20
20
import java .math .BigDecimal ;
21
21
import java .nio .charset .Charset ;
22
22
import java .nio .charset .StandardCharsets ;
23
+ import java .util .Arrays ;
23
24
import java .util .Collections ;
24
25
import java .util .List ;
25
26
import java .util .Map ;
47
48
import org .springframework .util .MimeType ;
48
49
import org .springframework .web .testfixture .xml .Pojo ;
49
50
50
- import static java .util .Arrays .asList ;
51
- import static java .util .Collections .emptyMap ;
52
- import static java .util .Collections .singletonMap ;
53
51
import static org .assertj .core .api .Assertions .assertThat ;
54
52
import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
55
- import static org .springframework .core .ResolvableType .forClass ;
56
53
import static org .springframework .http .MediaType .APPLICATION_JSON ;
57
54
import static org .springframework .http .MediaType .APPLICATION_STREAM_JSON ;
58
55
import static org .springframework .http .MediaType .APPLICATION_XML ;
@@ -79,12 +76,12 @@ public Jackson2JsonDecoderTests() {
79
76
@ Override
80
77
@ Test
81
78
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 ();
85
82
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 ();
88
85
assertThat (this .decoder .canDecode (ResolvableType .forClass (Pojo .class ),
89
86
new MediaType ("application" , "json" , StandardCharsets .UTF_8 ))).isTrue ();
90
87
assertThat (this .decoder .canDecode (ResolvableType .forClass (Pojo .class ),
@@ -133,7 +130,7 @@ public void decodeToMono() {
133
130
ResolvableType elementType = ResolvableType .forClassWithGenerics (List .class , Pojo .class );
134
131
135
132
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" )))
137
134
.expectComplete ()
138
135
.verify (), null , null );
139
136
}
@@ -143,48 +140,48 @@ public void decodeToMono() {
143
140
public void decodeEmptyArrayToFlux () {
144
141
Flux <DataBuffer > input = Flux .from (stringBuffer ("[]" ));
145
142
146
- testDecode (input , Pojo .class , step -> step . verifyComplete () );
143
+ testDecode (input , Pojo .class , StepVerifier . LastStep :: verifyComplete );
147
144
}
148
145
149
146
@ Test
150
147
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 );
155
153
156
154
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 ();
162
160
}), null , hints );
163
161
}
164
162
165
163
@ Test
166
164
public void classLevelJsonView () {
167
165
Flux <DataBuffer > input = Flux .from (stringBuffer (
168
166
"{\" 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 );
171
170
172
171
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 ();
178
177
})
179
178
.verifyComplete (), null , hints );
180
179
}
181
180
182
181
@ Test
183
182
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 ));
188
185
}
189
186
190
187
@ Test // gh-22042
@@ -197,10 +194,9 @@ public void decodeWithNullLiteral() {
197
194
198
195
@ Test
199
196
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 ());
204
200
StepVerifier .create (flux ).verifyError (CodecException .class );
205
201
}
206
202
@@ -228,9 +224,10 @@ public void bigDecimalFlux() {
228
224
@ SuppressWarnings ("unchecked" )
229
225
public void decodeNonUtf8Encoding () {
230
226
Mono <DataBuffer > input = stringBuffer ("{\" foo\" :\" bar\" }" , StandardCharsets .UTF_16 );
227
+ ResolvableType type = ResolvableType .forType (new ParameterizedTypeReference <Map <String , String >>() {});
231
228
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" ))
234
231
.verifyComplete (),
235
232
MediaType .parseMediaType ("application/json; charset=utf-16" ),
236
233
null );
@@ -239,12 +236,11 @@ public void decodeNonUtf8Encoding() {
239
236
@ Test
240
237
@ SuppressWarnings ("unchecked" )
241
238
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 >>() {});
245
241
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" ))
248
244
.verifyComplete (),
249
245
MediaType .parseMediaType ("application/json; charset=iso-8859-1" ),
250
246
null );
@@ -254,9 +250,10 @@ public void decodeNonUnicode() {
254
250
@ SuppressWarnings ("unchecked" )
255
251
public void decodeMonoNonUtf8Encoding () {
256
252
Mono <DataBuffer > input = stringBuffer ("{\" foo\" :\" bar\" }" , StandardCharsets .UTF_16 );
253
+ ResolvableType type = ResolvableType .forType (new ParameterizedTypeReference <Map <String , String >>() {});
257
254
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" ))
260
257
.verifyComplete (),
261
258
MediaType .parseMediaType ("application/json; charset=utf-16" ),
262
259
null );
@@ -265,12 +262,11 @@ public void decodeMonoNonUtf8Encoding() {
265
262
@ Test
266
263
@ SuppressWarnings ("unchecked" )
267
264
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 >>() {});
271
267
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" ))
274
270
.verifyComplete (),
275
271
MediaType .parseMediaType ("application/json; charset=us-ascii" ),
276
272
null );
@@ -291,7 +287,6 @@ private Mono<DataBuffer> stringBuffer(String value, Charset charset) {
291
287
}
292
288
293
289
294
- @ SuppressWarnings ("unused" )
295
290
private static class BeanWithNoDefaultConstructor {
296
291
297
292
private final String property1 ;
@@ -314,7 +309,7 @@ public String getProperty2() {
314
309
315
310
316
311
@ JsonDeserialize (using = Deserializer .class )
317
- public static class TestObject {
312
+ private static class TestObject {
318
313
319
314
private int test ;
320
315
@@ -327,7 +322,7 @@ public void setTest(int test) {
327
322
}
328
323
329
324
330
- public static class Deserializer extends StdDeserializer <TestObject > {
325
+ private static class Deserializer extends StdDeserializer <TestObject > {
331
326
332
327
private static final long serialVersionUID = 1L ;
333
328
0 commit comments