11/*
2- * Copyright 2002-2014 the original author or authors.
2+ * Copyright 2002-2015 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.
4444import org .springframework .util .LinkedMultiValueMap ;
4545import org .springframework .util .MultiValueMap ;
4646
47- import static org .junit .Assert .*;
48- import static org .mockito .BDDMockito .*;
47+ import static org .junit .Assert .assertEquals ;
48+ import static org .junit .Assert .assertFalse ;
49+ import static org .junit .Assert .assertNotNull ;
50+ import static org .junit .Assert .assertNull ;
51+ import static org .junit .Assert .assertTrue ;
52+ import static org .mockito .BDDMockito .never ;
53+ import static org .mockito .BDDMockito .verify ;
4954
5055/**
5156 * @author Arjen Poutsma
57+ * @author Rossen Stoyanchev
5258 */
5359public class FormHttpMessageConverterTests {
5460
61+ public static final Charset UTF_8 = Charset .forName ("UTF-8" );
62+
63+
5564 private FormHttpMessageConverter converter ;
5665
66+
5767 @ Before
5868 public void setUp () {
59- converter = new AllEncompassingFormHttpMessageConverter ();
69+ this . converter = new AllEncompassingFormHttpMessageConverter ();
6070 }
6171
72+
6273 @ Test
6374 public void canRead () {
64- assertTrue (converter .canRead (MultiValueMap .class , new MediaType ("application" , "x-www-form-urlencoded" )));
65- assertFalse (converter .canRead (MultiValueMap .class , new MediaType ("multipart" , "form-data" )));
75+ assertTrue (this .converter .canRead (MultiValueMap .class ,
76+ new MediaType ("application" , "x-www-form-urlencoded" )));
77+ assertFalse (this .converter .canRead (MultiValueMap .class ,
78+ new MediaType ("multipart" , "form-data" )));
6679 }
6780
6881 @ Test
6982 public void canWrite () {
70- assertTrue (converter .canWrite (MultiValueMap .class , new MediaType ("application" , "x-www-form-urlencoded" )));
71- assertTrue (converter .canWrite (MultiValueMap .class , new MediaType ("multipart" , "form-data" )));
72- assertTrue (converter .canWrite (MultiValueMap .class , new MediaType ("multipart" , "form-data" , Charset .forName ("UTF-8" ))));
73- assertTrue (converter .canWrite (MultiValueMap .class , MediaType .ALL ));
83+ assertTrue (this .converter .canWrite (MultiValueMap .class ,
84+ new MediaType ("application" , "x-www-form-urlencoded" )));
85+ assertTrue (this .converter .canWrite (MultiValueMap .class ,
86+ new MediaType ("multipart" , "form-data" )));
87+ assertTrue (this .converter .canWrite (MultiValueMap .class ,
88+ new MediaType ("multipart" , "form-data" , Charset .forName ("UTF-8" ))));
89+ assertTrue (this .converter .canWrite (MultiValueMap .class , MediaType .ALL ));
7490 }
7591
7692 @ Test
@@ -79,7 +95,7 @@ public void readForm() throws Exception {
7995 Charset iso88591 = Charset .forName ("ISO-8859-1" );
8096 MockHttpInputMessage inputMessage = new MockHttpInputMessage (body .getBytes (iso88591 ));
8197 inputMessage .getHeaders ().setContentType (new MediaType ("application" , "x-www-form-urlencoded" , iso88591 ));
82- MultiValueMap <String , String > result = converter .read (null , inputMessage );
98+ MultiValueMap <String , String > result = this . converter .read (null , inputMessage );
8399
84100 assertEquals ("Invalid result" , 3 , result .size ());
85101 assertEquals ("Invalid result" , "value 1" , result .getFirst ("name 1" ));
@@ -98,9 +114,10 @@ public void writeForm() throws IOException {
98114 body .add ("name 2" , "value 2+2" );
99115 body .add ("name 3" , null );
100116 MockHttpOutputMessage outputMessage = new MockHttpOutputMessage ();
101- converter .write (body , MediaType .APPLICATION_FORM_URLENCODED , outputMessage );
117+ this .converter .write (body , MediaType .APPLICATION_FORM_URLENCODED , outputMessage );
118+
102119 assertEquals ("Invalid result" , "name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3" ,
103- outputMessage .getBodyAsString (Charset . forName ( "UTF-8" ) ));
120+ outputMessage .getBodyAsString (UTF_8 ));
104121 assertEquals ("Invalid content-type" , new MediaType ("application" , "x-www-form-urlencoded" ),
105122 outputMessage .getHeaders ().getContentType ());
106123 assertEquals ("Invalid content-length" , outputMessage .getBodyAsBytes ().length ,
@@ -119,7 +136,6 @@ public void writeMultipart() throws Exception {
119136 parts .add ("logo" , logo );
120137
121138 // SPR-12108
122-
123139 Resource utf8 = new ClassPathResource ("/org/springframework/http/converter/logo.jpg" ) {
124140 @ Override
125141 public String getFilename () {
@@ -135,16 +151,17 @@ public String getFilename() {
135151 parts .add ("xml" , entity );
136152
137153 MockHttpOutputMessage outputMessage = new MockHttpOutputMessage ();
138- converter .setMultipartCharset (Charset . forName ( "UTF-8" ) );
139- converter .write (parts , new MediaType ("multipart" , "form-data" , Charset . forName ( "UTF-8" ) ), outputMessage );
154+ this . converter .setMultipartCharset (UTF_8 );
155+ this . converter .write (parts , new MediaType ("multipart" , "form-data" , UTF_8 ), outputMessage );
140156
141157 final MediaType contentType = outputMessage .getHeaders ().getContentType ();
142158 assertNotNull ("No boundary found" , contentType .getParameter ("boundary" ));
143159
144160 // see if Commons FileUpload can read what we wrote
145161 FileItemFactory fileItemFactory = new DiskFileItemFactory ();
146162 FileUpload fileUpload = new FileUpload (fileItemFactory );
147- List <FileItem > items = fileUpload .parseRequest (new MockHttpOutputMessageRequestContext (outputMessage ));
163+ RequestContext requestContext = new MockHttpOutputMessageRequestContext (outputMessage );
164+ List <FileItem > items = fileUpload .parseRequest (requestContext );
148165 assertEquals (6 , items .size ());
149166 FileItem item = items .get (0 );
150167 assertTrue (item .isFormField ());
@@ -181,35 +198,38 @@ public String getFilename() {
181198 verify (outputMessage .getBody (), never ()).close ();
182199 }
183200
201+
184202 private static class MockHttpOutputMessageRequestContext implements RequestContext {
185203
186204 private final MockHttpOutputMessage outputMessage ;
187205
206+
188207 private MockHttpOutputMessageRequestContext (MockHttpOutputMessage outputMessage ) {
189208 this .outputMessage = outputMessage ;
190209 }
191210
211+
192212 @ Override
193213 public String getCharacterEncoding () {
194- MediaType contentType = outputMessage .getHeaders ().getContentType ();
195- return contentType != null && contentType .getCharSet () != null ? contentType .getCharSet ().name () : null ;
214+ MediaType type = this . outputMessage .getHeaders ().getContentType ();
215+ return ( type != null && type .getCharSet () != null ? type .getCharSet ().name () : null ) ;
196216 }
197217
198218 @ Override
199219 public String getContentType () {
200- MediaType contentType = outputMessage .getHeaders ().getContentType ();
201- return contentType != null ? contentType .toString () : null ;
220+ MediaType type = this . outputMessage .getHeaders ().getContentType ();
221+ return ( type != null ? type .toString () : null ) ;
202222 }
203223
204224 @ Override
205225 @ Deprecated
206226 public int getContentLength () {
207- return outputMessage .getBodyAsBytes ().length ;
227+ return this . outputMessage .getBodyAsBytes ().length ;
208228 }
209229
210230 @ Override
211231 public InputStream getInputStream () throws IOException {
212- return new ByteArrayInputStream (outputMessage .getBodyAsBytes ());
232+ return new ByteArrayInputStream (this . outputMessage .getBodyAsBytes ());
213233 }
214234 }
215235
0 commit comments