users@jsr311.java.net

Re: MediaType.isComaptible

From: Marc Hadley <Marc.Hadley_at_Sun.COM>
Date: Tue, 12 Aug 2008 17:43:44 -0400

On Aug 12, 2008, at 5:15 PM, Reto Bachmann-Gmür wrote:

> According to the API MediaType.isComaptible returns "true if other
> is a subtype of this media type, false otherwise".

That could probably do with a rewording. Something like "returns true
if this is compatible with other, false otherwise".

> Insecure on whether a mediatype is considered a subtype of itself I
> made a test:
>
> MediaType type1 = new MediaType("image", "jpeg");
> Assert.assertTrue(type1.isCompatible(type1));
> Assert.assertTrue(MediaType.WILDCARD_TYPE.isCompatible(type1));
> Assert
> .assertTrue
> (MediaType.WILDCARD_TYPE.isCompatible(MediaType.WILDCARD_TYPE));
>
> all assertions passing till here, which is good as I think the
> method is more useful when returning true when type and subtype are
> equals.
>
> However I was surprised to see the following assertion fail:
>
> Assert.assertFalse(type1.isCompatible(MediaType.WILDCARD_TYPE));
>
image/jpeg is compatible with */* so I think the assertion is wrong
given the clarified javadoc suggested above.

Marc.

>
> Reto
>
> The complete failing test:
> ----
> import javax.ws.rs.core.ApplicationConfig;
> import javax.ws.rs.core.MediaType;
>
> import javax.ws.rs.core.Response.ResponseBuilder;
> import javax.ws.rs.core.UriBuilder;
> import javax.ws.rs.core.Variant.VariantListBuilder;
> import javax.ws.rs.ext.RuntimeDelegate;
> import junit.framework.Assert;
>
> import org.junit.Test;
>
> public class MediaTypeComparatorTest {
> @Test
> public void apiTest() {
> RuntimeDelegate.setInstance(new RuntimeDelegate() {
>
> @Override
> public UriBuilder createUriBuilder() {
> throw new UnsupportedOperationException("Not supported yet.");
> }
>
> @Override
> public ResponseBuilder createResponseBuilder() {
> throw new UnsupportedOperationException("Not supported yet.");
> }
>
> @Override
> public VariantListBuilder createVariantListBuilder() {
> throw new UnsupportedOperationException("Not supported yet.");
> }
>
> @Override
> public <T> T createEndpoint(ApplicationConfig arg0,
> Class<T> arg1) throws IllegalArgumentException,
> UnsupportedOperationException {
> throw new UnsupportedOperationException("Not supported yet.");
> }
>
> @Override
> public <T> HeaderDelegate<T> createHeaderDelegate(Class<T> arg0) {
> return null;
> }
> });
> MediaType type1 = new MediaType("image", "jpeg");
> Assert.assertTrue(type1.isCompatible(type1));
> Assert.assertTrue(MediaType.WILDCARD_TYPE.isCompatible(type1));
> Assert.assertFalse(type1.isCompatible(MediaType.WILDCARD_TYPE));
>
> Assert
> .assertTrue
> (MediaType.WILDCARD_TYPE.isCompatible(MediaType.WILDCARD_TYPE));
> }
> }
>

---
Marc Hadley <marc.hadley at sun.com>
CTO Office, Sun Microsystems.