/
StringDecoderTests.java
237 lines (199 loc) · 6.75 KB
/
StringDecoderTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.core.codec;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;
import org.springframework.core.ResolvableType;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferLimitException;
import org.springframework.core.testfixture.codec.AbstractDecoderTests;
import org.springframework.util.MimeType;
import org.springframework.util.MimeTypeUtils;
import static java.nio.charset.StandardCharsets.UTF_16BE;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Unit tests for {@link StringDecoder}.
*
* @author Sebastien Deleuze
* @author Brian Clozel
* @author Mark Paluch
*/
class StringDecoderTests extends AbstractDecoderTests<StringDecoder> {
private static final ResolvableType TYPE = ResolvableType.forClass(String.class);
StringDecoderTests() {
super(StringDecoder.allMimeTypes());
}
@Override
@Test
public void canDecode() {
assertThat(this.decoder.canDecode(TYPE, MimeTypeUtils.TEXT_PLAIN)).isTrue();
assertThat(this.decoder.canDecode(TYPE, MimeTypeUtils.TEXT_HTML)).isTrue();
assertThat(this.decoder.canDecode(TYPE, MimeTypeUtils.APPLICATION_JSON)).isTrue();
assertThat(this.decoder.canDecode(TYPE, MimeTypeUtils.parseMimeType("text/plain;charset=utf-8"))).isTrue();
assertThat(this.decoder.canDecode(ResolvableType.forClass(Integer.class), MimeTypeUtils.TEXT_PLAIN)).isFalse();
assertThat(this.decoder.canDecode(ResolvableType.forClass(Object.class), MimeTypeUtils.APPLICATION_JSON)).isFalse();
}
@Override
@Test
public void decode() {
String u = "ü";
String e = "é";
String o = "ø";
String s = String.format("%s\n%s\n%s", u, e, o);
Flux<DataBuffer> input = toDataBuffers(s, 1, UTF_8);
testDecodeAll(input, TYPE, step -> step.expectNext(u, e, o).verifyComplete(), null, null);
}
@Test
void decodeMultibyteCharacterUtf16() {
String u = "ü";
String e = "é";
String o = "ø";
String s = String.format("%s\n%s\n%s", u, e, o);
Flux<DataBuffer> source = toDataBuffers(s, 2, UTF_16BE);
MimeType mimeType = MimeTypeUtils.parseMimeType("text/plain;charset=utf-16be");
testDecode(source, TYPE, step -> step.expectNext(u, e, o).verifyComplete(), mimeType, null);
}
private Flux<DataBuffer> toDataBuffers(String s, int length, Charset charset) {
byte[] bytes = s.getBytes(charset);
List<byte[]> chunks = new ArrayList<>();
for (int i = 0; i < bytes.length; i += length) {
chunks.add(Arrays.copyOfRange(bytes, i, i + length));
}
return Flux.fromIterable(chunks)
.map(chunk -> {
DataBuffer dataBuffer = this.bufferFactory.allocateBuffer(length);
dataBuffer.write(chunk, 0, chunk.length);
return dataBuffer;
});
}
@Test
void decodeNewLine() {
Flux<DataBuffer> input = Flux.just(
stringBuffer("\r\nabc\n"),
stringBuffer("def"),
stringBuffer("ghi\r\n\n"),
stringBuffer("jkl"),
stringBuffer("mno\npqr\n"),
stringBuffer("stu"),
stringBuffer("vw"),
stringBuffer("xyz")
);
testDecode(input, String.class, step -> step
.expectNext("")
.expectNext("abc")
.expectNext("defghi")
.expectNext("")
.expectNext("jklmno")
.expectNext("pqr")
.expectNext("stuvwxyz")
.expectComplete()
.verify());
}
@Test
void maxInMemoryLimit() {
Flux<DataBuffer> input = Flux.just(
stringBuffer("abc\n"), stringBuffer("defg\n"), stringBuffer("hijkl\n"));
this.decoder.setMaxInMemorySize(5);
testDecode(input, String.class, step ->
step.expectNext("abc", "defg").verifyError(DataBufferLimitException.class));
}
@Test // gh-24312
void maxInMemoryLimitReleaseUnprocessedLinesFromCurrentBuffer() {
Flux<DataBuffer> input = Flux.just(
stringBuffer("TOO MUCH DATA\nanother line\n\nand another\n"));
this.decoder.setMaxInMemorySize(5);
testDecode(input, String.class, step -> step.verifyError(DataBufferLimitException.class));
}
@Test // gh-24339
void maxInMemoryLimitReleaseUnprocessedLinesWhenUnlimited() {
Flux<DataBuffer> input = Flux.just(stringBuffer("Line 1\nLine 2\nLine 3\n"));
this.decoder.setMaxInMemorySize(-1);
testDecodeCancel(input, ResolvableType.forClass(String.class), null, Collections.emptyMap());
}
@Test
void decodeNewLineIncludeDelimiters() {
this.decoder = StringDecoder.allMimeTypes(StringDecoder.DEFAULT_DELIMITERS, false);
Flux<DataBuffer> input = Flux.just(
stringBuffer("\r\nabc\n"),
stringBuffer("def"),
stringBuffer("ghi\r\n\n"),
stringBuffer("jkl"),
stringBuffer("mno\npqr\n"),
stringBuffer("stu"),
stringBuffer("vw"),
stringBuffer("xyz")
);
testDecode(input, String.class, step -> step
.expectNext("\r\n")
.expectNext("abc\n")
.expectNext("defghi\r\n")
.expectNext("\n")
.expectNext("jklmno\n")
.expectNext("pqr\n")
.expectNext("stuvwxyz")
.expectComplete()
.verify());
}
@Test
void decodeEmptyFlux() {
Flux<DataBuffer> input = Flux.empty();
testDecode(input, String.class, step -> step
.expectComplete()
.verify());
}
@Test
void decodeEmptyDataBuffer() {
Flux<DataBuffer> input = Flux.just(stringBuffer(""));
Flux<String> output = this.decoder.decode(input,
TYPE, null, Collections.emptyMap());
StepVerifier.create(output)
.expectNext("")
.expectComplete().verify();
}
@Override
@Test
public void decodeToMono() {
Flux<DataBuffer> input = Flux.just(
stringBuffer("foo"),
stringBuffer("bar"),
stringBuffer("baz"));
testDecodeToMonoAll(input, String.class, step -> step
.expectNext("foobarbaz")
.expectComplete()
.verify());
}
@Test
void decodeToMonoWithEmptyFlux() {
Flux<DataBuffer> input = Flux.empty();
testDecodeToMono(input, String.class, step -> step
.expectComplete()
.verify());
}
private DataBuffer stringBuffer(String value) {
byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
DataBuffer buffer = this.bufferFactory.allocateBuffer(bytes.length);
buffer.write(bytes);
return buffer;
}
}