2222import  java .util .ArrayList ;
2323import  java .util .List ;
2424
25+ import  org .hamcrest .Matchers ;
2526import  org .junit .Before ;
27+ import  org .junit .Rule ;
2628import  org .junit .Test ;
29+ import  org .junit .rules .ExpectedException ;
2730
2831import  org .springframework .core .ParameterizedTypeReference ;
2932import  org .springframework .http .HttpHeaders ;
3336import  org .springframework .http .client .ClientHttpResponse ;
3437import  org .springframework .http .converter .GenericHttpMessageConverter ;
3538import  org .springframework .http .converter .HttpMessageConverter ;
39+ import  org .springframework .http .converter .HttpMessageNotReadableException ;
3640
3741import  static  org .junit .Assert .*;
3842import  static  org .mockito .BDDMockito .*;
4145 * Test fixture for {@link HttpMessageConverter}. 
4246 * 
4347 * @author Arjen Poutsma 
48+  * @author Brian Clozel 
4449 */ 
4550public  class  HttpMessageConverterExtractorTests  {
4651
4752	private  HttpMessageConverterExtractor <?> extractor ;
4853
4954	private  ClientHttpResponse  response ;
5055
56+ 	@ Rule 
57+ 	public  final  ExpectedException  exception  = ExpectedException .none ();
58+ 
5159	@ Before 
5260	public  void  createMocks () {
5361		response  = mock (ClientHttpResponse .class );
@@ -104,8 +112,6 @@ public void zeroContentLength() throws IOException {
104112	@ SuppressWarnings ("unchecked" )
105113	public  void  emptyMessageBody () throws  IOException  {
106114		HttpMessageConverter <String > converter  = mock (HttpMessageConverter .class );
107- 		List <HttpMessageConverter <?>> converters  = new  ArrayList <>();
108- 		converters .add (converter );
109115		HttpHeaders  responseHeaders  = new  HttpHeaders ();
110116		extractor  = new  HttpMessageConverterExtractor <>(String .class , createConverterList (converter ));
111117		given (response .getStatusCode ()).willReturn (HttpStatus .OK );
@@ -120,13 +126,11 @@ public void emptyMessageBody() throws IOException {
120126	@ SuppressWarnings ("unchecked" )
121127	public  void  normal () throws  IOException  {
122128		HttpMessageConverter <String > converter  = mock (HttpMessageConverter .class );
123- 		List <HttpMessageConverter <?>> converters  = new  ArrayList <>();
124- 		converters .add (converter );
125129		HttpHeaders  responseHeaders  = new  HttpHeaders ();
126130		MediaType  contentType  = MediaType .TEXT_PLAIN ;
127131		responseHeaders .setContentType (contentType );
128132		String  expected  = "Foo" ;
129- 		extractor  = new  HttpMessageConverterExtractor <>(String .class , converters );
133+ 		extractor  = new  HttpMessageConverterExtractor <>(String .class , createConverterList ( converter ) );
130134		given (response .getStatusCode ()).willReturn (HttpStatus .OK );
131135		given (response .getHeaders ()).willReturn (responseHeaders );
132136		given (response .getBody ()).willReturn (new  ByteArrayInputStream (expected .getBytes ()));
@@ -138,20 +142,19 @@ public void normal() throws IOException {
138142		assertEquals (expected , result );
139143	}
140144
141- 	@ Test ( expected  =  RestClientException . class ) 
145+ 	@ Test 
142146	@ SuppressWarnings ("unchecked" )
143147	public  void  cannotRead () throws  IOException  {
144148		HttpMessageConverter <String > converter  = mock (HttpMessageConverter .class );
145- 		List <HttpMessageConverter <?>> converters  = new  ArrayList <>();
146- 		converters .add (converter );
147149		HttpHeaders  responseHeaders  = new  HttpHeaders ();
148150		MediaType  contentType  = MediaType .TEXT_PLAIN ;
149151		responseHeaders .setContentType (contentType );
150- 		extractor  = new  HttpMessageConverterExtractor <>(String .class , converters );
152+ 		extractor  = new  HttpMessageConverterExtractor <>(String .class , createConverterList ( converter ) );
151153		given (response .getStatusCode ()).willReturn (HttpStatus .OK );
152154		given (response .getHeaders ()).willReturn (responseHeaders );
153155		given (response .getBody ()).willReturn (new  ByteArrayInputStream ("Foobar" .getBytes ()));
154156		given (converter .canRead (String .class , contentType )).willReturn (false );
157+ 		exception .expect (RestClientException .class );
155158
156159		extractor .extractData (response );
157160	}
@@ -160,14 +163,13 @@ public void cannotRead() throws IOException {
160163	@ SuppressWarnings ("unchecked" )
161164	public  void  generics () throws  IOException  {
162165		GenericHttpMessageConverter <String > converter  = mock (GenericHttpMessageConverter .class );
163- 		List <HttpMessageConverter <?>> converters  = createConverterList (converter );
164166		HttpHeaders  responseHeaders  = new  HttpHeaders ();
165167		MediaType  contentType  = MediaType .TEXT_PLAIN ;
166168		responseHeaders .setContentType (contentType );
167169		String  expected  = "Foo" ;
168170		ParameterizedTypeReference <List <String >> reference  = new  ParameterizedTypeReference <List <String >>() {};
169171		Type  type  = reference .getType ();
170- 		extractor  = new  HttpMessageConverterExtractor <List <String >>(type , converters );
172+ 		extractor  = new  HttpMessageConverterExtractor <List <String >>(type , createConverterList ( converter ) );
171173		given (response .getStatusCode ()).willReturn (HttpStatus .OK );
172174		given (response .getHeaders ()).willReturn (responseHeaders );
173175		given (response .getBody ()).willReturn (new  ByteArrayInputStream (expected .getBytes ()));
@@ -179,6 +181,46 @@ public void generics() throws IOException {
179181		assertEquals (expected , result );
180182	}
181183
184+ 	@ Test  // SPR-13592 
185+ 	@ SuppressWarnings ("unchecked" )
186+ 	public  void  converterThrowsIOException () throws  IOException  {
187+ 		HttpMessageConverter <String > converter  = mock (HttpMessageConverter .class );
188+ 		HttpHeaders  responseHeaders  = new  HttpHeaders ();
189+ 		MediaType  contentType  = MediaType .TEXT_PLAIN ;
190+ 		responseHeaders .setContentType (contentType );
191+ 		extractor  = new  HttpMessageConverterExtractor <>(String .class , createConverterList (converter ));
192+ 		given (response .getStatusCode ()).willReturn (HttpStatus .OK );
193+ 		given (response .getHeaders ()).willReturn (responseHeaders );
194+ 		given (response .getBody ()).willReturn (new  ByteArrayInputStream ("Foobar" .getBytes ()));
195+ 		given (converter .canRead (String .class , contentType )).willThrow (IOException .class );
196+ 		exception .expect (RestClientException .class );
197+ 		exception .expectMessage ("Error while extracting response for type "  +
198+ 				"[class java.lang.String] and content type [text/plain]" );
199+ 		exception .expectCause (Matchers .instanceOf (IOException .class ));
200+ 
201+ 		extractor .extractData (response );
202+ 	}
203+ 
204+ 	@ Test  // SPR-13592 
205+ 	@ SuppressWarnings ("unchecked" )
206+ 	public  void  converterThrowsHttpMessageNotReadableException () throws  IOException  {
207+ 		HttpMessageConverter <String > converter  = mock (HttpMessageConverter .class );
208+ 		HttpHeaders  responseHeaders  = new  HttpHeaders ();
209+ 		MediaType  contentType  = MediaType .TEXT_PLAIN ;
210+ 		responseHeaders .setContentType (contentType );
211+ 		extractor  = new  HttpMessageConverterExtractor <>(String .class , createConverterList (converter ));
212+ 		given (response .getStatusCode ()).willReturn (HttpStatus .OK );
213+ 		given (response .getHeaders ()).willReturn (responseHeaders );
214+ 		given (response .getBody ()).willReturn (new  ByteArrayInputStream ("Foobar" .getBytes ()));
215+ 		given (converter .canRead (String .class , contentType )).willThrow (HttpMessageNotReadableException .class );
216+ 		exception .expect (RestClientException .class );
217+ 		exception .expectMessage ("Error while extracting response for type "  +
218+ 				"[class java.lang.String] and content type [text/plain]" );
219+ 		exception .expectCause (Matchers .instanceOf (HttpMessageNotReadableException .class ));
220+ 
221+ 		extractor .extractData (response );
222+ 	}
223+ 
182224	private  List <HttpMessageConverter <?>> createConverterList (
183225			HttpMessageConverter <?> converter ) {
184226		List <HttpMessageConverter <?>> converters  = new  ArrayList <>(1 );
0 commit comments