Skip to content

Commit cb39b07

Browse files
committed
Polishing Jackson encoder tests
1 parent 5077123 commit cb39b07

File tree

6 files changed

+67
-74
lines changed

6 files changed

+67
-74
lines changed

spring-core/src/testFixtures/java/org/springframework/core/testfixture/codec/AbstractEncoderTests.java

Lines changed: 18 additions & 16 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.
@@ -81,7 +81,7 @@ protected AbstractEncoderTests(E encoder) {
8181
* Helper methods that tests for a variety of encoding scenarios. This methods
8282
* invokes:
8383
* <ul>
84-
* <li>{@link #testEncode(Publisher, ResolvableType, Consumer, MimeType, Map)}</li>
84+
* <li>{@link #testEncode(Publisher, ResolvableType, MimeType, Map, Consumer)}</li>
8585
* <li>{@link #testEncodeError(Publisher, ResolvableType, MimeType, Map)}</li>
8686
* <li>{@link #testEncodeCancel(Publisher, ResolvableType, MimeType, Map)}</li>
8787
* <li>{@link #testEncodeEmpty(ResolvableType, MimeType, Map)}</li>
@@ -94,30 +94,32 @@ protected AbstractEncoderTests(E encoder) {
9494
*/
9595
protected <T> void testEncodeAll(Publisher<? extends T> input, Class<? extends T> inputClass,
9696
Consumer<StepVerifier.FirstStep<DataBuffer>> stepConsumer) {
97-
testEncodeAll(input, ResolvableType.forClass(inputClass), stepConsumer, null, null);
97+
98+
testEncodeAll(input, ResolvableType.forClass(inputClass), null, null, stepConsumer);
9899
}
99100

100101
/**
101102
* Helper methods that tests for a variety of decoding scenarios. This methods
102103
* invokes:
103104
* <ul>
104-
* <li>{@link #testEncode(Publisher, ResolvableType, Consumer, MimeType, Map)}</li>
105+
* <li>{@link #testEncode(Publisher, ResolvableType, MimeType, Map, Consumer)}</li>
105106
* <li>{@link #testEncodeError(Publisher, ResolvableType, MimeType, Map)}</li>
106107
* <li>{@link #testEncodeCancel(Publisher, ResolvableType, MimeType, Map)}</li>
107108
* <li>{@link #testEncodeEmpty(ResolvableType, MimeType, Map)}</li>
108109
* </ul>
109110
*
111+
* @param <T> the output type
110112
* @param input the input to be provided to the encoder
111113
* @param inputType the input type
112-
* @param stepConsumer a consumer to {@linkplain StepVerifier verify} the output
113114
* @param mimeType the mime type to use for decoding. May be {@code null}.
114115
* @param hints the hints used for decoding. May be {@code null}.
115-
* @param <T> the output type
116+
* @param stepConsumer a consumer to {@linkplain StepVerifier verify} the output
116117
*/
117118
protected <T> void testEncodeAll(Publisher<? extends T> input, ResolvableType inputType,
118-
Consumer<StepVerifier.FirstStep<DataBuffer>> stepConsumer,
119-
@Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
120-
testEncode(input, inputType, stepConsumer, mimeType, hints);
119+
@Nullable MimeType mimeType, @Nullable Map<String, Object> hints,
120+
Consumer<StepVerifier.FirstStep<DataBuffer>> stepConsumer) {
121+
122+
testEncode(input, inputType, mimeType, hints, stepConsumer);
121123
testEncodeError(input, inputType, mimeType, hints);
122124
testEncodeCancel(input, inputType, mimeType, hints);
123125
testEncodeEmpty(inputType, mimeType, hints);
@@ -133,25 +135,25 @@ protected <T> void testEncodeAll(Publisher<? extends T> input, ResolvableType in
133135
*/
134136
protected <T> void testEncode(Publisher<? extends T> input, Class<? extends T> inputClass,
135137
Consumer<StepVerifier.FirstStep<DataBuffer>> stepConsumer) {
136-
testEncode(input, ResolvableType.forClass(inputClass), stepConsumer, null, null);
138+
139+
testEncode(input, ResolvableType.forClass(inputClass), null, null, stepConsumer);
137140
}
138141

139142
/**
140143
* Test a standard {@link Encoder#encode encode} scenario.
141144
*
145+
* @param <T> the output type
142146
* @param input the input to be provided to the encoder
143147
* @param inputType the input type
144-
* @param stepConsumer a consumer to {@linkplain StepVerifier verify} the output
145148
* @param mimeType the mime type to use for decoding. May be {@code null}.
146149
* @param hints the hints used for decoding. May be {@code null}.
147-
* @param <T> the output type
150+
* @param stepConsumer a consumer to {@linkplain StepVerifier verify} the output
148151
*/
149152
protected <T> void testEncode(Publisher<? extends T> input, ResolvableType inputType,
150-
Consumer<StepVerifier.FirstStep<DataBuffer>> stepConsumer,
151-
@Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
153+
@Nullable MimeType mimeType, @Nullable Map<String, Object> hints,
154+
Consumer<StepVerifier.FirstStep<DataBuffer>> stepConsumer) {
152155

153-
Flux<DataBuffer> result = encoder().encode(input, this.bufferFactory, inputType,
154-
mimeType, hints);
156+
Flux<DataBuffer> result = encoder().encode(input, this.bufferFactory, inputType, mimeType, hints);
155157
StepVerifier.FirstStep<DataBuffer> step = StepVerifier.create(result);
156158
stepConsumer.accept(step);
157159
}

spring-web/src/main/java/org/springframework/http/codec/json/AbstractJackson2Encoder.java

Lines changed: 1 addition & 1 deletion
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.

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

Lines changed: 23 additions & 25 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.
@@ -95,12 +95,12 @@ public void encode() throws Exception {
9595
new Pojo("foofoo", "barbar"),
9696
new Pojo("foofoofoo", "barbarbar"));
9797

98-
testEncodeAll(input, ResolvableType.forClass(Pojo.class), step -> step
98+
testEncodeAll(input, ResolvableType.forClass(Pojo.class), APPLICATION_STREAM_JSON, null, step -> step
9999
.consumeNextWith(expectString("{\"foo\":\"foo\",\"bar\":\"bar\"}\n"))
100100
.consumeNextWith(expectString("{\"foo\":\"foofoo\",\"bar\":\"barbar\"}\n"))
101101
.consumeNextWith(expectString("{\"foo\":\"foofoofoo\",\"bar\":\"barbarbar\"}\n"))
102-
.verifyComplete(),
103-
APPLICATION_STREAM_JSON, null);
102+
.verifyComplete()
103+
);
104104
}
105105

106106
@Test // SPR-15866
@@ -168,15 +168,15 @@ public void encodeAsStreamWithCustomStreamingType() {
168168
new Pojo("foofoofoo", "barbarbar")
169169
);
170170

171-
testEncode(input, ResolvableType.forClass(Pojo.class), step -> step
171+
testEncode(input, ResolvableType.forClass(Pojo.class), barMediaType, null, step -> step
172172
.consumeNextWith(expectString("{\"foo\":\"foo\",\"bar\":\"bar\"}\n")
173173
.andThen(DataBufferUtils::release))
174174
.consumeNextWith(expectString("{\"foo\":\"foofoo\",\"bar\":\"barbar\"}\n")
175175
.andThen(DataBufferUtils::release))
176176
.consumeNextWith(expectString("{\"foo\":\"foofoofoo\",\"bar\":\"barbarbar\"}\n")
177177
.andThen(DataBufferUtils::release))
178-
.verifyComplete(),
179-
barMediaType, null);
178+
.verifyComplete()
179+
);
180180
}
181181

182182
@Test
@@ -190,11 +190,10 @@ public void fieldLevelJsonView() {
190190
ResolvableType type = ResolvableType.forClass(JacksonViewBean.class);
191191
Map<String, Object> hints = singletonMap(JSON_VIEW_HINT, MyJacksonView1.class);
192192

193-
testEncode(input, type, step -> step
194-
.consumeNextWith(expectString("{\"withView1\":\"with\"}")
195-
.andThen(DataBufferUtils::release))
196-
.verifyComplete(),
197-
null, hints);
193+
testEncode(input, type, null, hints, step -> step
194+
.consumeNextWith(expectString("{\"withView1\":\"with\"}").andThen(DataBufferUtils::release))
195+
.verifyComplete()
196+
);
198197
}
199198

200199
@Test
@@ -208,11 +207,10 @@ public void classLevelJsonView() {
208207
ResolvableType type = ResolvableType.forClass(JacksonViewBean.class);
209208
Map<String, Object> hints = singletonMap(JSON_VIEW_HINT, MyJacksonView3.class);
210209

211-
testEncode(input, type, step -> step
212-
.consumeNextWith(expectString("{\"withoutView\":\"without\"}")
213-
.andThen(DataBufferUtils::release))
214-
.verifyComplete(),
215-
null, hints);
210+
testEncode(input, type, null, hints, step -> step
211+
.consumeNextWith(expectString("{\"withoutView\":\"without\"}").andThen(DataBufferUtils::release))
212+
.verifyComplete()
213+
);
216214
}
217215

218216
@Test
@@ -227,11 +225,10 @@ public void jacksonValue() {
227225

228226
ResolvableType type = ResolvableType.forClass(MappingJacksonValue.class);
229227

230-
testEncode(Mono.just(jacksonValue), type, step -> step
231-
.consumeNextWith(expectString("{\"withView1\":\"with\"}")
232-
.andThen(DataBufferUtils::release))
233-
.verifyComplete(),
234-
null, Collections.emptyMap());
228+
testEncode(Mono.just(jacksonValue), type, null, Collections.emptyMap(), step -> step
229+
.consumeNextWith(expectString("{\"withView1\":\"with\"}").andThen(DataBufferUtils::release))
230+
.verifyComplete()
231+
);
235232
}
236233

237234
@Test // gh-22771
@@ -252,11 +249,12 @@ public void encodeWithFlushAfterWriteOff() {
252249
@Test
253250
public void encodeAscii() {
254251
Mono<Object> input = Mono.just(new Pojo("foo", "bar"));
252+
MimeType mimeType = new MimeType("application", "json", StandardCharsets.US_ASCII);
255253

256-
testEncode(input, ResolvableType.forClass(Pojo.class), step -> step
254+
testEncode(input, ResolvableType.forClass(Pojo.class), mimeType, null, step -> step
257255
.consumeNextWith(expectString("{\"foo\":\"foo\",\"bar\":\"bar\"}"))
258-
.verifyComplete(),
259-
new MimeType("application", "json", StandardCharsets.US_ASCII), null);
256+
.verifyComplete()
257+
);
260258
}
261259

262260

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

Lines changed: 4 additions & 8 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.
@@ -108,17 +108,13 @@ public void encode() {
108108
}
109109

110110
@Test
111-
public void encodeError() throws Exception {
111+
public void encodeError() {
112112
Mono<Pojo> input = Mono.error(new InputException());
113-
114-
testEncode(input, Pojo.class, step -> step
115-
.expectError(InputException.class)
116-
.verify());
117-
113+
testEncode(input, Pojo.class, step -> step.expectError(InputException.class).verify());
118114
}
119115

120116
@Test
121-
public void encodeAsStream() throws Exception {
117+
public void encodeAsStream() {
122118
Pojo pojo1 = new Pojo("foo", "bar");
123119
Pojo pojo2 = new Pojo("foofoo", "barbar");
124120
Pojo pojo3 = new Pojo("foofoofoo", "barbarbar");

spring-web/src/test/java/org/springframework/http/codec/xml/Jaxb2XmlEncoderTests.java

Lines changed: 9 additions & 10 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.
@@ -71,29 +71,28 @@ public void encode() {
7171
Mono<Pojo> input = Mono.just(new Pojo("foofoo", "barbar"));
7272

7373
testEncode(input, Pojo.class, step -> step
74-
.consumeNextWith(
75-
expectXml("<?xml version='1.0' encoding='UTF-8' standalone='yes'?>" +
74+
.consumeNextWith(expectXml(
75+
"<?xml version='1.0' encoding='UTF-8' standalone='yes'?>" +
7676
"<pojo><bar>barbar</bar><foo>foofoo</foo></pojo>"))
7777
.verifyComplete());
7878
}
7979

8080
@Test
8181
public void encodeError() {
8282
Flux<Pojo> input = Flux.error(RuntimeException::new);
83-
84-
testEncode(input, Pojo.class, step -> step
85-
.expectError(RuntimeException.class)
86-
.verify());
83+
testEncode(input, Pojo.class, step -> step.expectError(RuntimeException.class).verify());
8784
}
8885

8986
@Test
9087
public void encodeElementsWithCommonType() {
9188
Mono<Container> input = Mono.just(new Container());
9289

9390
testEncode(input, Pojo.class, step -> step
94-
.consumeNextWith(
95-
expectXml("<?xml version='1.0' encoding='UTF-8' standalone='yes'?>" +
96-
"<container><foo><name>name1</name></foo><bar><title>title1</title></bar></container>"))
91+
.consumeNextWith(expectXml(
92+
"<?xml version='1.0' encoding='UTF-8' standalone='yes'?>" +
93+
"<container>" +
94+
"<foo><name>name1</name></foo><bar><title>title1</title></bar>" +
95+
"</container>"))
9796
.verifyComplete());
9897
}
9998

spring-web/src/test/kotlin/org/springframework/http/codec/json/KotlinSerializationJsonEncoderTests.kt

Lines changed: 12 additions & 14 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.
@@ -65,25 +65,23 @@ class KotlinSerializationJsonEncoderTests : AbstractEncoderTests<KotlinSerializa
6565
Pojo("foofoo", "barbar"),
6666
Pojo("foofoofoo", "barbarbar")
6767
)
68-
testEncode(input, Pojo::class.java, { step: FirstStep<DataBuffer?> ->
69-
step
70-
.consumeNextWith(expectString("[" +
71-
"{\"foo\":\"foo\",\"bar\":\"bar\"}," +
72-
"{\"foo\":\"foofoo\",\"bar\":\"barbar\"}," +
73-
"{\"foo\":\"foofoofoo\",\"bar\":\"barbarbar\"}]")
74-
.andThen { dataBuffer: DataBuffer? -> DataBufferUtils.release(dataBuffer) })
75-
.verifyComplete()
68+
testEncode(input, Pojo::class.java, { step: FirstStep<DataBuffer?> -> step
69+
.consumeNextWith(expectString("[" +
70+
"{\"foo\":\"foo\",\"bar\":\"bar\"}," +
71+
"{\"foo\":\"foofoo\",\"bar\":\"barbar\"}," +
72+
"{\"foo\":\"foofoofoo\",\"bar\":\"barbarbar\"}]")
73+
.andThen { dataBuffer: DataBuffer? -> DataBufferUtils.release(dataBuffer) })
74+
.verifyComplete()
7675
})
7776
}
7877

7978
@Test
8079
fun encodeMono() {
8180
val input = Mono.just(Pojo("foo", "bar"))
82-
testEncode(input, Pojo::class.java, { step: FirstStep<DataBuffer?> ->
83-
step
84-
.consumeNextWith(expectString("{\"foo\":\"foo\",\"bar\":\"bar\"}")
85-
.andThen { dataBuffer: DataBuffer? -> DataBufferUtils.release(dataBuffer) })
86-
.verifyComplete()
81+
testEncode(input, Pojo::class.java, { step: FirstStep<DataBuffer?> -> step
82+
.consumeNextWith(expectString("{\"foo\":\"foo\",\"bar\":\"bar\"}")
83+
.andThen { dataBuffer: DataBuffer? -> DataBufferUtils.release(dataBuffer) })
84+
.verifyComplete()
8785
})
8886
}
8987

0 commit comments

Comments
 (0)