@ -34,6 +34,7 @@ import org.springframework.http.converter.xml.SourceHttpMessageConverter;
import static org.hamcrest.Matchers.equalTo ;
import static org.junit.Assert.assertEquals ;
import static org.junit.Assert.assertNotEquals ;
import static org.junit.Assert.assertThat ;
import static org.junit.Assert.assertTrue ;
import static org.mockito.Mockito.mock ;
@ -65,27 +66,35 @@ public class HttpMessageConvertersTests {
}
@Test
public void overrideExistingConverter ( ) {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter ( ) ;
HttpMessageConverters converters = new HttpMessageConverters ( converter ) ;
assertTrue ( converters . getConverters ( ) . contains ( converter ) ) ;
int count = 0 ;
for ( HttpMessageConverter < ? > httpMessageConverter : converters ) {
if ( httpMessageConverter instanceof MappingJackson2HttpMessageConverter ) {
count + + ;
public void addBeforeExistingConverter ( ) {
MappingJackson2HttpMessageConverter converter1 = new MappingJackson2HttpMessageConverter ( ) ;
MappingJackson2HttpMessageConverter converter2 = new MappingJackson2HttpMessageConverter ( ) ;
HttpMessageConverters converters = new HttpMessageConverters ( converter1 ,
converter2 ) ;
assertTrue ( converters . getConverters ( ) . contains ( converter1 ) ) ;
assertTrue ( converters . getConverters ( ) . contains ( converter2 ) ) ;
List < MappingJackson2HttpMessageConverter > httpConverters = new ArrayList < MappingJackson2HttpMessageConverter > ( ) ;
for ( HttpMessageConverter < ? > candidate : converters ) {
if ( candidate instanceof MappingJackson2HttpMessageConverter ) {
httpConverters . add ( ( MappingJackson2HttpMessageConverter ) candidate ) ;
}
}
// The existing converter is still there, but with a lower priority
assertEquals ( 2 , count ) ;
assertEquals ( 0 , converters . getConverters ( ) . indexOf ( converter ) ) ;
assertEquals ( 3 , httpConverters . size ( ) ) ;
assertEquals ( 0 , httpConverters . indexOf ( converter1 ) ) ;
assertEquals ( 1 , httpConverters . indexOf ( converter2 ) ) ;
assertNotEquals ( 0 , converters . getConverters ( ) . indexOf ( converter1 ) ) ;
}
@Test
public void addNewConverter ( ) {
HttpMessageConverter < ? > converter = mock ( HttpMessageConverter . class ) ;
HttpMessageConverters converters = new HttpMessageConverters ( converter ) ;
assertTrue ( converters . getConverters ( ) . contains ( converter ) ) ;
assertEquals ( converter , converters . getConverters ( ) . get ( 0 ) ) ;
public void addNewConverters ( ) {
HttpMessageConverter < ? > converter1 = mock ( HttpMessageConverter . class ) ;
HttpMessageConverter < ? > converter2 = mock ( HttpMessageConverter . class ) ;
HttpMessageConverters converters = new HttpMessageConverters ( converter1 ,
converter2 ) ;
assertTrue ( converters . getConverters ( ) . contains ( converter1 ) ) ;
assertEquals ( converter1 , converters . getConverters ( ) . get ( 0 ) ) ;
assertEquals ( converter2 , converters . getConverters ( ) . get ( 1 ) ) ;
}
}