|
22 | 22 | import java.io.StringReader;
|
23 | 23 | import java.nio.charset.Charset;
|
24 | 24 | import java.util.List;
|
| 25 | +import javax.xml.bind.annotation.XmlElement; |
| 26 | +import javax.xml.bind.annotation.XmlRootElement; |
25 | 27 | import javax.xml.transform.Source;
|
26 | 28 | import javax.xml.transform.stream.StreamSource;
|
27 | 29 |
|
@@ -198,6 +200,47 @@ public String getFilename() {
|
198 | 200 | verify(outputMessage.getBody(), never()).close();
|
199 | 201 | }
|
200 | 202 |
|
| 203 | + // SPR-13309 |
| 204 | + |
| 205 | + @Test |
| 206 | + public void writeMultipartOrder() throws Exception { |
| 207 | + MyBean myBean = new MyBean(); |
| 208 | + myBean.setString("foo"); |
| 209 | + |
| 210 | + MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>(); |
| 211 | + parts.add("part1", myBean); |
| 212 | + |
| 213 | + HttpHeaders entityHeaders = new HttpHeaders(); |
| 214 | + entityHeaders.setContentType(MediaType.TEXT_XML); |
| 215 | + HttpEntity<MyBean> entity = new HttpEntity<MyBean>(myBean, entityHeaders); |
| 216 | + parts.add("part2", entity); |
| 217 | + |
| 218 | + MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); |
| 219 | + this.converter.setMultipartCharset(UTF_8); |
| 220 | + this.converter.write(parts, new MediaType("multipart", "form-data", UTF_8), outputMessage); |
| 221 | + |
| 222 | + final MediaType contentType = outputMessage.getHeaders().getContentType(); |
| 223 | + assertNotNull("No boundary found", contentType.getParameter("boundary")); |
| 224 | + |
| 225 | + // see if Commons FileUpload can read what we wrote |
| 226 | + FileItemFactory fileItemFactory = new DiskFileItemFactory(); |
| 227 | + FileUpload fileUpload = new FileUpload(fileItemFactory); |
| 228 | + RequestContext requestContext = new MockHttpOutputMessageRequestContext(outputMessage); |
| 229 | + List<FileItem> items = fileUpload.parseRequest(requestContext); |
| 230 | + assertEquals(2, items.size()); |
| 231 | + |
| 232 | + FileItem item = items.get(0); |
| 233 | + assertTrue(item.isFormField()); |
| 234 | + assertEquals("part1", item.getFieldName()); |
| 235 | + assertEquals("{\"string\":\"foo\"}", item.getString()); |
| 236 | + |
| 237 | + item = items.get(1); |
| 238 | + assertTrue(item.isFormField()); |
| 239 | + assertEquals("part2", item.getFieldName()); |
| 240 | + assertEquals("<MyBean><string>foo</string></MyBean>", item.getString()); |
| 241 | + } |
| 242 | + |
| 243 | + |
201 | 244 |
|
202 | 245 | private static class MockHttpOutputMessageRequestContext implements RequestContext {
|
203 | 246 |
|
@@ -233,4 +276,17 @@ public InputStream getInputStream() throws IOException {
|
233 | 276 | }
|
234 | 277 | }
|
235 | 278 |
|
| 279 | + public static class MyBean { |
| 280 | + |
| 281 | + private String string; |
| 282 | + |
| 283 | + public String getString() { |
| 284 | + return this.string; |
| 285 | + } |
| 286 | + |
| 287 | + public void setString(String string) { |
| 288 | + this.string = string; |
| 289 | + } |
| 290 | + } |
| 291 | + |
236 | 292 | }
|
0 commit comments