1
1
/*
2
- * Copyright 2002-2019 the original author or authors.
2
+ * Copyright 2002-2020 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
19
19
import java .nio .charset .StandardCharsets ;
20
20
import java .util .Collections ;
21
21
import java .util .List ;
22
+ import java .util .Map ;
22
23
23
24
import javax .xml .namespace .QName ;
25
+ import javax .xml .stream .XMLStreamException ;
24
26
import javax .xml .stream .events .XMLEvent ;
25
27
26
28
import org .junit .jupiter .api .Test ;
29
+ import reactor .core .Exceptions ;
27
30
import reactor .core .publisher .Flux ;
28
31
import reactor .core .publisher .Mono ;
29
32
import reactor .test .StepVerifier ;
@@ -66,6 +69,8 @@ public class Jaxb2XmlDecoderTests extends AbstractLeakCheckingTests {
66
69
"</pojo>" +
67
70
"<root/>" ;
68
71
72
+ private static final Map <String , Object > HINTS = Collections .emptyMap ();
73
+
69
74
70
75
private final Jaxb2XmlDecoder decoder = new Jaxb2XmlDecoder ();
71
76
@@ -88,8 +93,7 @@ public void canDecode() {
88
93
89
94
@ Test
90
95
public void splitOneBranches () {
91
- Flux <XMLEvent > xmlEvents = this .xmlEventDecoder
92
- .decode (stringBuffer (POJO_ROOT ), null , null , Collections .emptyMap ());
96
+ Flux <XMLEvent > xmlEvents = this .xmlEventDecoder .decode (toDataBufferMono (POJO_ROOT ), null , null , HINTS );
93
97
Flux <List <XMLEvent >> result = this .decoder .split (xmlEvents , new QName ("pojo" ));
94
98
95
99
StepVerifier .create (result )
@@ -109,9 +113,8 @@ public void splitOneBranches() {
109
113
}
110
114
111
115
@ Test
112
- public void splitMultipleBranches () throws Exception {
113
- Flux <XMLEvent > xmlEvents = this .xmlEventDecoder
114
- .decode (stringBuffer (POJO_CHILD ), null , null , Collections .emptyMap ());
116
+ public void splitMultipleBranches () {
117
+ Flux <XMLEvent > xmlEvents = this .xmlEventDecoder .decode (toDataBufferMono (POJO_CHILD ), null , null , HINTS );
115
118
Flux <List <XMLEvent >> result = this .decoder .split (xmlEvents , new QName ("pojo" ));
116
119
117
120
@@ -158,10 +161,9 @@ private static void assertCharacters(XMLEvent event, String expectedData) {
158
161
}
159
162
160
163
@ Test
161
- public void decodeSingleXmlRootElement () throws Exception {
162
- Mono <DataBuffer > source = stringBuffer (POJO_ROOT );
163
- Mono <Object > output = this .decoder .decodeToMono (source , ResolvableType .forClass (Pojo .class ),
164
- null , Collections .emptyMap ());
164
+ public void decodeSingleXmlRootElement () {
165
+ Mono <DataBuffer > source = toDataBufferMono (POJO_ROOT );
166
+ Mono <Object > output = this .decoder .decodeToMono (source , ResolvableType .forClass (Pojo .class ), null , HINTS );
165
167
166
168
StepVerifier .create (output )
167
169
.expectNext (new Pojo ("foofoo" , "barbar" ))
@@ -170,10 +172,9 @@ public void decodeSingleXmlRootElement() throws Exception {
170
172
}
171
173
172
174
@ Test
173
- public void decodeSingleXmlTypeElement () throws Exception {
174
- Mono <DataBuffer > source = stringBuffer (POJO_ROOT );
175
- Mono <Object > output = this .decoder .decodeToMono (source , ResolvableType .forClass (TypePojo .class ),
176
- null , Collections .emptyMap ());
175
+ public void decodeSingleXmlTypeElement () {
176
+ Mono <DataBuffer > source = toDataBufferMono (POJO_ROOT );
177
+ Mono <Object > output = this .decoder .decodeToMono (source , ResolvableType .forClass (TypePojo .class ), null , HINTS );
177
178
178
179
StepVerifier .create (output )
179
180
.expectNext (new TypePojo ("foofoo" , "barbar" ))
@@ -182,10 +183,9 @@ public void decodeSingleXmlTypeElement() throws Exception {
182
183
}
183
184
184
185
@ Test
185
- public void decodeMultipleXmlRootElement () throws Exception {
186
- Mono <DataBuffer > source = stringBuffer (POJO_CHILD );
187
- Flux <Object > output = this .decoder .decode (source , ResolvableType .forClass (Pojo .class ),
188
- null , Collections .emptyMap ());
186
+ public void decodeMultipleXmlRootElement () {
187
+ Mono <DataBuffer > source = toDataBufferMono (POJO_CHILD );
188
+ Flux <Object > output = this .decoder .decode (source , ResolvableType .forClass (Pojo .class ), null , HINTS );
189
189
190
190
StepVerifier .create (output )
191
191
.expectNext (new Pojo ("foo" , "bar" ))
@@ -195,10 +195,9 @@ public void decodeMultipleXmlRootElement() throws Exception {
195
195
}
196
196
197
197
@ Test
198
- public void decodeMultipleXmlTypeElement () throws Exception {
199
- Mono <DataBuffer > source = stringBuffer (POJO_CHILD );
200
- Flux <Object > output = this .decoder .decode (source , ResolvableType .forClass (TypePojo .class ),
201
- null , Collections .emptyMap ());
198
+ public void decodeMultipleXmlTypeElement () {
199
+ Mono <DataBuffer > source = toDataBufferMono (POJO_CHILD );
200
+ Flux <Object > output = this .decoder .decode (source , ResolvableType .forClass (TypePojo .class ), null , HINTS );
202
201
203
202
StepVerifier .create (output )
204
203
.expectNext (new TypePojo ("foo" , "bar" ))
@@ -208,19 +207,27 @@ public void decodeMultipleXmlTypeElement() throws Exception {
208
207
}
209
208
210
209
@ Test
211
- public void decodeError () throws Exception {
210
+ public void decodeError () {
212
211
Flux <DataBuffer > source = Flux .concat (
213
- stringBuffer ("<pojo>" ),
212
+ toDataBufferMono ("<pojo>" ),
214
213
Flux .error (new RuntimeException ()));
215
214
216
- Mono <Object > output = this .decoder .decodeToMono (source , ResolvableType .forClass (Pojo .class ),
217
- null , Collections .emptyMap ());
215
+ Mono <Object > output = this .decoder .decodeToMono (source , ResolvableType .forClass (Pojo .class ), null , HINTS );
218
216
219
217
StepVerifier .create (output )
220
218
.expectError (RuntimeException .class )
221
219
.verify ();
222
220
}
223
221
222
+ @ Test // gh-24622
223
+ public void decodeErrorWithXmlNotWellFormed () {
224
+ Mono <DataBuffer > source = toDataBufferMono ("<Response><tag>something</tag</Response>" );
225
+ Mono <Object > result = this .decoder .decodeToMono (source , ResolvableType .forClass (Pojo .class ), null , HINTS );
226
+
227
+ StepVerifier .create (result ).verifyErrorSatisfies (ex ->
228
+ assertThat (Exceptions .unwrap (ex )).isInstanceOf (XMLStreamException .class ));
229
+ }
230
+
224
231
@ Test
225
232
public void toExpectedQName () {
226
233
assertThat (this .decoder .toQName (Pojo .class )).isEqualTo (new QName ("pojo" ));
@@ -236,7 +243,7 @@ public void toExpectedQName() {
236
243
237
244
}
238
245
239
- private Mono <DataBuffer > stringBuffer (String value ) {
246
+ private Mono <DataBuffer > toDataBufferMono (String value ) {
240
247
return Mono .defer (() -> {
241
248
byte [] bytes = value .getBytes (StandardCharsets .UTF_8 );
242
249
DataBuffer buffer = this .bufferFactory .allocateBuffer (bytes .length );
0 commit comments