Browse Source

Polish

pull/885/head
Rossen Stoyanchev 9 years ago
parent
commit
1e4f674db7
  1. 10
      spring-web/src/main/java/org/springframework/http/converter/AbstractHttpMessageConverter.java
  2. 51
      spring-web/src/test/java/org/springframework/http/converter/HttpMessageConverterTests.java

10
spring-web/src/main/java/org/springframework/http/converter/AbstractHttpMessageConverter.java

@ -101,8 +101,9 @@ public abstract class AbstractHttpMessageConverter<T> implements HttpMessageConv @@ -101,8 +101,9 @@ public abstract class AbstractHttpMessageConverter<T> implements HttpMessageConv
}
/**
* Returns true if any of the {@linkplain #setSupportedMediaTypes(List) supported media types}
* include the given media type.
* Returns true if any of the {@linkplain #setSupportedMediaTypes(List)
* supported} media types {@link MediaType#includes(MediaType) include} the
* given media type.
* @param mediaType the media type to read, can be {@code null} if not specified.
* Typically the value of a {@code Content-Type} header.
* @return {@code true} if the supported media types include the media type,
@ -121,8 +122,9 @@ public abstract class AbstractHttpMessageConverter<T> implements HttpMessageConv @@ -121,8 +122,9 @@ public abstract class AbstractHttpMessageConverter<T> implements HttpMessageConv
}
/**
* This implementation checks if the given class is {@linkplain #supports(Class) supported},
* and if the {@linkplain #getSupportedMediaTypes() supported media types}
* This implementation checks if the given class is
* {@linkplain #supports(Class) supported}, and if the
* {@linkplain #getSupportedMediaTypes() supported} media types
* {@linkplain MediaType#includes(MediaType) include} the given media type.
*/
@Override

51
spring-web/src/test/java/org/springframework/http/converter/HttpMessageConverterTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -24,43 +24,55 @@ import org.springframework.http.HttpInputMessage; @@ -24,43 +24,55 @@ import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import static org.junit.Assert.*;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* Test-case for AbstractHttpMessageConverter.
*
* @author Arjen Poutsma
* @author Rossen Stoyanchev
*/
public class HttpMessageConverterTests {
private static final MediaType MEDIA_TYPE = new MediaType("foo", "bar");
@Test
public void canRead() {
AbstractHttpMessageConverter<MyType> converter = new MyHttpMessageConverter<MyType>(MEDIA_TYPE) {
@Override
protected boolean supports(Class<?> clazz) {
return MyType.class.equals(clazz);
}
MediaType mediaType = new MediaType("foo", "bar");
HttpMessageConverter<MyType> converter = new MyHttpMessageConverter<MyType>(mediaType);
};
assertTrue(converter.canRead(MyType.class, MEDIA_TYPE));
assertTrue(converter.canRead(MyType.class, mediaType));
assertFalse(converter.canRead(MyType.class, new MediaType("foo", "*")));
assertFalse(converter.canRead(MyType.class, MediaType.ALL));
}
@Test
public void canReadWithWildcardSubtype() {
MediaType mediaType = new MediaType("foo");
HttpMessageConverter<MyType> converter = new MyHttpMessageConverter<MyType>(mediaType);
assertTrue(converter.canRead(MyType.class, new MediaType("foo", "bar")));
assertTrue(converter.canRead(MyType.class, new MediaType("foo", "*")));
assertFalse(converter.canRead(MyType.class, MediaType.ALL));
}
@Test
public void canWrite() {
AbstractHttpMessageConverter<MyType> converter = new MyHttpMessageConverter<MyType>(MEDIA_TYPE) {
@Override
protected boolean supports(Class<?> clazz) {
return MyType.class.equals(clazz);
}
MediaType mediaType = new MediaType("foo", "bar");
HttpMessageConverter<MyType> converter = new MyHttpMessageConverter<MyType>(mediaType);
};
assertTrue(converter.canWrite(MyType.class, mediaType));
assertTrue(converter.canWrite(MyType.class, new MediaType("foo", "*")));
assertTrue(converter.canWrite(MyType.class, MediaType.ALL));
}
assertTrue(converter.canWrite(MyType.class, MEDIA_TYPE));
@Test
public void canWriteWithWildcardInSupportedSubtype() {
MediaType mediaType = new MediaType("foo");
HttpMessageConverter<MyType> converter = new MyHttpMessageConverter<MyType>(mediaType);
assertTrue(converter.canWrite(MyType.class, new MediaType("foo", "bar")));
assertTrue(converter.canWrite(MyType.class, new MediaType("foo", "*")));
assertTrue(converter.canWrite(MyType.class, MediaType.ALL));
}
@ -74,8 +86,7 @@ public class HttpMessageConverterTests { @@ -74,8 +86,7 @@ public class HttpMessageConverterTests {
@Override
protected boolean supports(Class<?> clazz) {
fail("Not expected");
return false;
return MyType.class.equals(clazz);
}
@Override

Loading…
Cancel
Save