users@jsr311.java.net

MediaType.isComaptible

From: Reto Bachmann-Gmür <reto_at_gmuer.ch>
Date: Tue, 12 Aug 2008 23:15:06 +0200

According to the API MediaType.isComaptible returns "true if other is a
subtype of this media type, 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));


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));
	}
}