/* * Copyright (C) 2011 The Guava Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.google.common.net; import static com.google.common.base.Charsets.UTF_16; import static com.google.common.base.Charsets.UTF_8; import static com.google.common.net.MediaType.ANY_APPLICATION_TYPE; import static com.google.common.net.MediaType.ANY_AUDIO_TYPE; import static com.google.common.net.MediaType.ANY_IMAGE_TYPE; import static com.google.common.net.MediaType.ANY_TEXT_TYPE; import static com.google.common.net.MediaType.ANY_TYPE; import static com.google.common.net.MediaType.ANY_VIDEO_TYPE; import static com.google.common.net.MediaType.HTML_UTF_8; import static com.google.common.net.MediaType.JPEG; import static com.google.common.net.MediaType.PLAIN_TEXT_UTF_8; import static java.lang.reflect.Modifier.isFinal; import static java.lang.reflect.Modifier.isPublic; import static java.lang.reflect.Modifier.isStatic; import static java.util.Arrays.asList; import com.google.common.annotations.Beta; import com.google.common.annotations.GwtCompatible; import com.google.common.annotations.GwtIncompatible; import com.google.common.base.Function; import com.google.common.base.Optional; import com.google.common.base.Predicate; import com.google.common.base.Throwables; import com.google.common.collect.FluentIterable; import com.google.common.collect.ImmutableListMultimap; import com.google.common.collect.ImmutableMultimap; import com.google.common.testing.EqualsTester; import com.google.common.testing.NullPointerTester; import junit.framework.TestCase; import java.lang.reflect.Field; import java.nio.charset.Charset; import java.nio.charset.IllegalCharsetNameException; import java.nio.charset.UnsupportedCharsetException; /** * Tests for {@link MediaType}. * * @author Gregory Kick */ @Beta @GwtCompatible(emulated = true) public class MediaTypeTest extends TestCase { @GwtIncompatible("reflection") public void testParse_useConstants() throws Exception { for (MediaType constant : getConstants()) { assertSame(constant, MediaType.parse(constant.toString())); } } @GwtIncompatible("reflection") public void testCreate_useConstants() throws Exception { for (MediaType constant : getConstants()) { assertSame(constant, MediaType.create(constant.type(), constant.subtype()) .withParameters(constant.parameters())); } } @GwtIncompatible("reflection") public void testConstants_charset() throws Exception { for (Field field : getConstantFields()) { Optional charset = ((MediaType) field.get(null)).charset(); if (field.getName().endsWith("_UTF_8")) { assertEquals(Optional.of(UTF_8), charset); } else { assertEquals(Optional.absent(), charset); } } } @GwtIncompatible("reflection") private static FluentIterable getConstantFields() { return FluentIterable.from(asList(MediaType.class.getDeclaredFields())) .filter(new Predicate() { @Override public boolean apply(Field input) { int modifiers = input.getModifiers(); return isPublic(modifiers) && isStatic(modifiers) && isFinal(modifiers) && MediaType.class.equals(input.getType()); } }); } @GwtIncompatible("reflection") private static FluentIterable getConstants() { return getConstantFields() .transform(new Function() { @Override public MediaType apply(Field input) { try { return (MediaType) input.get(null); } catch (Exception e) { throw Throwables.propagate(e); } } }); } public void testCreate_invalidType() { try { MediaType.create("te> PARAMETERS = ImmutableListMultimap.of("a", "1", "a", "2", "b", "3"); public void testGetParameters() { assertEquals(ImmutableListMultimap.of(), MediaType.parse("text/plain").parameters()); assertEquals(ImmutableListMultimap.of("charset", "utf-8"), MediaType.parse("application/atom+xml; charset=utf-8").parameters()); assertEquals(PARAMETERS, MediaType.parse("application/atom+xml; a=1; a=2; b=3").parameters()); } public void testWithoutParameters() { assertSame(MediaType.parse("image/gif"), MediaType.parse("image/gif").withoutParameters()); assertEquals(MediaType.parse("image/gif"), MediaType.parse("image/gif; foo=bar").withoutParameters()); } public void testWithParameters() { assertEquals(MediaType.parse("text/plain; a=1; a=2; b=3"), MediaType.parse("text/plain").withParameters(PARAMETERS)); assertEquals(MediaType.parse("text/plain; a=1; a=2; b=3"), MediaType.parse("text/plain; a=1; a=2; b=3").withParameters(PARAMETERS)); } public void testWithParameters_invalidAttribute() { MediaType mediaType = MediaType.parse("text/plain"); ImmutableListMultimap parameters = ImmutableListMultimap.of("a", "1", "@", "2", "b", "3"); try { mediaType.withParameters(parameters); fail(); } catch (IllegalArgumentException expected) {} } public void testWithParameter() { assertEquals(MediaType.parse("text/plain; a=1"), MediaType.parse("text/plain").withParameter("a", "1")); assertEquals(MediaType.parse("text/plain; a=1"), MediaType.parse("text/plain; a=1; a=2").withParameter("a", "1")); assertEquals(MediaType.parse("text/plain; a=3"), MediaType.parse("text/plain; a=1; a=2").withParameter("a", "3")); assertEquals(MediaType.parse("text/plain; a=1; a=2; b=3"), MediaType.parse("text/plain; a=1; a=2").withParameter("b", "3")); } public void testWithParameter_invalidAttribute() { MediaType mediaType = MediaType.parse("text/plain"); try { mediaType.withParameter("@", "2"); fail(); } catch (IllegalArgumentException expected) {} } public void testWithCharset() { assertEquals(MediaType.parse("text/plain; charset=utf-8"), MediaType.parse("text/plain").withCharset(UTF_8)); assertEquals(MediaType.parse("text/plain; charset=utf-8"), MediaType.parse("text/plain; charset=utf-16").withCharset(UTF_8)); } public void testHasWildcard() { assertFalse(PLAIN_TEXT_UTF_8.hasWildcard()); assertFalse(JPEG.hasWildcard()); assertTrue(ANY_TYPE.hasWildcard()); assertTrue(ANY_APPLICATION_TYPE.hasWildcard()); assertTrue(ANY_AUDIO_TYPE.hasWildcard()); assertTrue(ANY_IMAGE_TYPE.hasWildcard()); assertTrue(ANY_TEXT_TYPE.hasWildcard()); assertTrue(ANY_VIDEO_TYPE.hasWildcard()); } public void testIs() { assertTrue(PLAIN_TEXT_UTF_8.is(ANY_TYPE)); assertTrue(JPEG.is(ANY_TYPE)); assertTrue(ANY_TEXT_TYPE.is(ANY_TYPE)); assertTrue(PLAIN_TEXT_UTF_8.is(ANY_TEXT_TYPE)); assertTrue(PLAIN_TEXT_UTF_8.withoutParameters().is(ANY_TEXT_TYPE)); assertFalse(JPEG.is(ANY_TEXT_TYPE)); assertTrue(PLAIN_TEXT_UTF_8.is(PLAIN_TEXT_UTF_8)); assertTrue(PLAIN_TEXT_UTF_8.is(PLAIN_TEXT_UTF_8.withoutParameters())); assertFalse(PLAIN_TEXT_UTF_8.withoutParameters().is(PLAIN_TEXT_UTF_8)); assertFalse(PLAIN_TEXT_UTF_8.is(HTML_UTF_8)); assertFalse(PLAIN_TEXT_UTF_8.withParameter("charset", "UTF-16").is(PLAIN_TEXT_UTF_8)); assertFalse(PLAIN_TEXT_UTF_8.is(PLAIN_TEXT_UTF_8.withParameter("charset", "UTF-16"))); } public void testParse_empty() { try { MediaType.parse(""); fail(); } catch (IllegalArgumentException expected) {} } public void testParse_badInput() { try { MediaType.parse("/"); fail(); } catch (IllegalArgumentException expected) {} try { MediaType.parse("text"); fail(); } catch (IllegalArgumentException expected) {} try { MediaType.parse("text/"); fail(); } catch (IllegalArgumentException expected) {} try { MediaType.parse("te