Skip to content

Commit 9b3fbc2

Browse files
committed
WebDataBinder and @MVC request param binding detect and introspect MultipartFile arrays as well (SPR-2784)
1 parent ee04046 commit 9b3fbc2

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

org.springframework.web/src/test/java/org/springframework/web/multipart/commons/CommonsMultipartResolverTests.java

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2008 the original author or authors.
2+
* Copyright 2002-2010 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.
@@ -50,6 +50,7 @@
5050
import org.springframework.mock.web.MockHttpServletResponse;
5151
import org.springframework.mock.web.MockServletContext;
5252
import org.springframework.mock.web.PassThroughFilterChain;
53+
import org.springframework.util.MultiValueMap;
5354
import org.springframework.web.bind.ServletRequestDataBinder;
5455
import org.springframework.web.context.WebApplicationContext;
5556
import org.springframework.web.context.support.StaticWebApplicationContext;
@@ -59,7 +60,6 @@
5960
import org.springframework.web.multipart.support.MultipartFilter;
6061
import org.springframework.web.multipart.support.StringMultipartFileEditor;
6162
import org.springframework.web.util.WebUtils;
62-
import org.springframework.util.MultiValueMap;
6363

6464
/**
6565
* @author Juergen Hoeller
@@ -218,15 +218,19 @@ private void doTestFiles(MultipartHttpServletRequest request) throws IOException
218218

219219
private void doTestBinding(MockCommonsMultipartResolver resolver, MockHttpServletRequest originalRequest,
220220
MultipartHttpServletRequest request) throws UnsupportedEncodingException {
221+
221222
MultipartTestBean1 mtb1 = new MultipartTestBean1();
222223
assertEquals(null, mtb1.getField1());
223224
assertEquals(null, mtb1.getField2());
224225
ServletRequestDataBinder binder = new ServletRequestDataBinder(mtb1, "mybean");
225226
binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor());
226227
binder.bind(request);
227-
CommonsMultipartFile file1 = (CommonsMultipartFile) request.getFile("field1");
228+
List<MultipartFile> file1List = request.getFiles("field1");
229+
CommonsMultipartFile file1a = (CommonsMultipartFile) file1List.get(0);
230+
CommonsMultipartFile file1b = (CommonsMultipartFile) file1List.get(1);
228231
CommonsMultipartFile file2 = (CommonsMultipartFile) request.getFile("field2");
229-
assertEquals(file1, mtb1.getField1());
232+
assertEquals(file1a, mtb1.getField1()[0]);
233+
assertEquals(file1b, mtb1.getField1()[1]);
230234
assertEquals(new String(file2.getBytes()), new String(mtb1.getField2()));
231235

232236
MultipartTestBean2 mtb2 = new MultipartTestBean2();
@@ -236,25 +240,27 @@ private void doTestBinding(MockCommonsMultipartResolver resolver, MockHttpServle
236240
binder.registerCustomEditor(String.class, "field1", new StringMultipartFileEditor());
237241
binder.registerCustomEditor(String.class, "field2", new StringMultipartFileEditor("UTF-16"));
238242
binder.bind(request);
239-
assertEquals(new String(file1.getBytes()), mtb2.getField1());
243+
assertEquals(new String(file1a.getBytes()), mtb2.getField1()[0]);
244+
assertEquals(new String(file1b.getBytes()), mtb2.getField1()[1]);
240245
assertEquals(new String(file2.getBytes(), "UTF-16"), mtb2.getField2());
241246

242247
resolver.cleanupMultipart(request);
243-
assertTrue(((MockFileItem) file1.getFileItem()).deleted);
248+
assertTrue(((MockFileItem) file1a.getFileItem()).deleted);
249+
assertTrue(((MockFileItem) file1b.getFileItem()).deleted);
244250
assertTrue(((MockFileItem) file2.getFileItem()).deleted);
245251

246252
resolver.setEmpty(true);
247253
request = resolver.resolveMultipart(originalRequest);
248254
binder.setBindEmptyMultipartFiles(false);
249-
String firstBound = mtb2.getField1();
255+
String firstBound = mtb2.getField2();
250256
binder.bind(request);
251-
assertTrue(mtb2.getField1().length() > 0);
252-
assertEquals(firstBound, mtb2.getField1());
257+
assertTrue(mtb2.getField2().length() > 0);
258+
assertEquals(firstBound, mtb2.getField2());
253259

254260
request = resolver.resolveMultipart(originalRequest);
255261
binder.setBindEmptyMultipartFiles(true);
256262
binder.bind(request);
257-
assertTrue(mtb2.getField1().length() == 0);
263+
assertTrue(mtb2.getField2().length() == 0);
258264
}
259265

260266
@Test
@@ -480,14 +486,14 @@ public OutputStream getOutputStream() throws IOException {
480486

481487
public class MultipartTestBean1 {
482488

483-
private MultipartFile field1;
489+
private MultipartFile[] field1;
484490
private byte[] field2;
485491

486-
public void setField1(MultipartFile field1) {
492+
public void setField1(MultipartFile[] field1) {
487493
this.field1 = field1;
488494
}
489495

490-
public MultipartFile getField1() {
496+
public MultipartFile[] getField1() {
491497
return field1;
492498
}
493499

@@ -503,14 +509,14 @@ public byte[] getField2() {
503509

504510
public class MultipartTestBean2 {
505511

506-
private String field1;
512+
private String[] field1;
507513
private String field2;
508514

509-
public void setField1(String field1) {
515+
public void setField1(String[] field1) {
510516
this.field1 = field1;
511517
}
512518

513-
public String getField1() {
519+
public String[] getField1() {
514520
return field1;
515521
}
516522

0 commit comments

Comments
 (0)