1
1
/*
2
- * Copyright 2002-2008 the original author or authors.
2
+ * Copyright 2002-2010 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.
50
50
import org .springframework .mock .web .MockHttpServletResponse ;
51
51
import org .springframework .mock .web .MockServletContext ;
52
52
import org .springframework .mock .web .PassThroughFilterChain ;
53
+ import org .springframework .util .MultiValueMap ;
53
54
import org .springframework .web .bind .ServletRequestDataBinder ;
54
55
import org .springframework .web .context .WebApplicationContext ;
55
56
import org .springframework .web .context .support .StaticWebApplicationContext ;
59
60
import org .springframework .web .multipart .support .MultipartFilter ;
60
61
import org .springframework .web .multipart .support .StringMultipartFileEditor ;
61
62
import org .springframework .web .util .WebUtils ;
62
- import org .springframework .util .MultiValueMap ;
63
63
64
64
/**
65
65
* @author Juergen Hoeller
@@ -218,15 +218,19 @@ private void doTestFiles(MultipartHttpServletRequest request) throws IOException
218
218
219
219
private void doTestBinding (MockCommonsMultipartResolver resolver , MockHttpServletRequest originalRequest ,
220
220
MultipartHttpServletRequest request ) throws UnsupportedEncodingException {
221
+
221
222
MultipartTestBean1 mtb1 = new MultipartTestBean1 ();
222
223
assertEquals (null , mtb1 .getField1 ());
223
224
assertEquals (null , mtb1 .getField2 ());
224
225
ServletRequestDataBinder binder = new ServletRequestDataBinder (mtb1 , "mybean" );
225
226
binder .registerCustomEditor (byte [].class , new ByteArrayMultipartFileEditor ());
226
227
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 );
228
231
CommonsMultipartFile file2 = (CommonsMultipartFile ) request .getFile ("field2" );
229
- assertEquals (file1 , mtb1 .getField1 ());
232
+ assertEquals (file1a , mtb1 .getField1 ()[0 ]);
233
+ assertEquals (file1b , mtb1 .getField1 ()[1 ]);
230
234
assertEquals (new String (file2 .getBytes ()), new String (mtb1 .getField2 ()));
231
235
232
236
MultipartTestBean2 mtb2 = new MultipartTestBean2 ();
@@ -236,25 +240,27 @@ private void doTestBinding(MockCommonsMultipartResolver resolver, MockHttpServle
236
240
binder .registerCustomEditor (String .class , "field1" , new StringMultipartFileEditor ());
237
241
binder .registerCustomEditor (String .class , "field2" , new StringMultipartFileEditor ("UTF-16" ));
238
242
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 ]);
240
245
assertEquals (new String (file2 .getBytes (), "UTF-16" ), mtb2 .getField2 ());
241
246
242
247
resolver .cleanupMultipart (request );
243
- assertTrue (((MockFileItem ) file1 .getFileItem ()).deleted );
248
+ assertTrue (((MockFileItem ) file1a .getFileItem ()).deleted );
249
+ assertTrue (((MockFileItem ) file1b .getFileItem ()).deleted );
244
250
assertTrue (((MockFileItem ) file2 .getFileItem ()).deleted );
245
251
246
252
resolver .setEmpty (true );
247
253
request = resolver .resolveMultipart (originalRequest );
248
254
binder .setBindEmptyMultipartFiles (false );
249
- String firstBound = mtb2 .getField1 ();
255
+ String firstBound = mtb2 .getField2 ();
250
256
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 ());
253
259
254
260
request = resolver .resolveMultipart (originalRequest );
255
261
binder .setBindEmptyMultipartFiles (true );
256
262
binder .bind (request );
257
- assertTrue (mtb2 .getField1 ().length () == 0 );
263
+ assertTrue (mtb2 .getField2 ().length () == 0 );
258
264
}
259
265
260
266
@ Test
@@ -480,14 +486,14 @@ public OutputStream getOutputStream() throws IOException {
480
486
481
487
public class MultipartTestBean1 {
482
488
483
- private MultipartFile field1 ;
489
+ private MultipartFile [] field1 ;
484
490
private byte [] field2 ;
485
491
486
- public void setField1 (MultipartFile field1 ) {
492
+ public void setField1 (MultipartFile [] field1 ) {
487
493
this .field1 = field1 ;
488
494
}
489
495
490
- public MultipartFile getField1 () {
496
+ public MultipartFile [] getField1 () {
491
497
return field1 ;
492
498
}
493
499
@@ -503,14 +509,14 @@ public byte[] getField2() {
503
509
504
510
public class MultipartTestBean2 {
505
511
506
- private String field1 ;
512
+ private String [] field1 ;
507
513
private String field2 ;
508
514
509
- public void setField1 (String field1 ) {
515
+ public void setField1 (String [] field1 ) {
510
516
this .field1 = field1 ;
511
517
}
512
518
513
- public String getField1 () {
519
+ public String [] getField1 () {
514
520
return field1 ;
515
521
}
516
522
0 commit comments