Author: julien(a)jboss.com
Date: 2008-03-18 18:05:40 -0400 (Tue, 18 Mar 2008)
New Revision: 10317
Added:
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/net/media/MediaTypeMap.java
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/net/media/MediaTypeMapImpl.java
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/net/media/MediaTypeMapTestCase.java
Modified:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/net/media/MediaType.java
Log:
added media type map
Modified:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/net/media/MediaType.java
===================================================================
---
modules/common/trunk/common/src/main/java/org/jboss/portal/common/net/media/MediaType.java 2008-03-18
21:47:59 UTC (rev 10316)
+++
modules/common/trunk/common/src/main/java/org/jboss/portal/common/net/media/MediaType.java 2008-03-18
22:05:40 UTC (rev 10317)
@@ -110,7 +110,6 @@
}
return value;
}
-
public int hashCode()
{
if (hashCode == null)
Added:
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/net/media/MediaTypeMap.java
===================================================================
---
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/net/media/MediaTypeMap.java
(rev 0)
+++
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/net/media/MediaTypeMap.java 2008-03-18
22:05:40 UTC (rev 10317)
@@ -0,0 +1,194 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2008, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.test.common.net.media;
+
+import org.jboss.portal.common.net.media.MediaType;
+import org.jboss.portal.common.net.media.TypeDef;
+
+import java.util.Set;
+
+/**
+ * A map containing values associated with media types. The map has the capability to map
wildcard subtypes
+ * (for instance text / *) or the wildcard type (* / *). In order to make the distinction
between what the map
+ * declares and what it supports it is possible to query the map with resolution or not.
+ *
+ * @author <a href="mailto:julien@jboss-portal.org">Julien
Viet</a>
+ * @version $Revision: 630 $
+ */
+public interface MediaTypeMap<V>
+{
+
+ /**
+ * Returns true if the map declares the specified media type.
+ *
+ * @param mediaType the media type
+ * @return true if the map declares the media type
+ * @throws IllegalArgumentException if the argument is null
+ */
+ boolean contains(MediaType mediaType) throws IllegalArgumentException;
+
+ /**
+ * Returns true if the map declares the specified type. It is equivalent to check if
the map
+ * contains a wildard subtype of a type.
+ *
+ * @param type the type
+ * @return true if the map declares the type
+ * @throws IllegalArgumentException if the argument is null
+ */
+ boolean contains(TypeDef type) throws IllegalArgumentException;
+
+ /**
+ * Returns true if the map declares the specified media type with a specified value.
+ *
+ * @param mediaType the media type
+ * @param value the value
+ * @return true if the map declares the media type and the value
+ * @throws IllegalArgumentException if any argument is null
+ */
+ boolean contains(MediaType mediaType, V value) throws IllegalArgumentException;
+
+ /**
+ * Returns true if the map declares the specified type with a specific value. It is
equivalent to check if the map
+ * contains a wildard subtype of a type with a specific value.
+ *
+ * @param type the type
+ * @param value the value
+ * @return true if the map declares the media type and the value
+ * @throws IllegalArgumentException if any argument is null
+ */
+ boolean contains(TypeDef type, V value) throws IllegalArgumentException;
+
+ /**
+ * Returns true if the map declares the specified value for any media type or any
type. It is equivalent to check
+ * if the map contains a wildcard type with a specific value.
+ *
+ * @param value the value
+ * @return true if the map declares the value
+ * @throws IllegalArgumentException if any argument is null
+ */
+ boolean contains(V value) throws IllegalArgumentException;
+
+ /**
+ * Returns the set of values declared for a given media type.
+ *
+ * @param mediaType the media type
+ * @return the values declared
+ * @throws IllegalArgumentException if any argument is null
+ */
+ Set<V> get(MediaType mediaType) throws IllegalArgumentException;
+
+ /**
+ * Returns the set of values declared for a given type.
+ *
+ * @param type the type
+ * @return the values declared
+ * @throws IllegalArgumentException if any argument is null
+ */
+ Set<V> get(TypeDef type) throws IllegalArgumentException;
+
+ /**
+ * Returns true if the map supports the specified media type.
+ *
+ * @param mediaType the media type
+ * @return true if the map supports the media type
+ * @throws IllegalArgumentException if the argument is null
+ */
+ boolean isSupported(MediaType mediaType) throws IllegalArgumentException;
+
+ /**
+ * Returns true if the map supports the specified type.
+ *
+ * @param type the type
+ * @return true if the map declares the type
+ * @throws IllegalArgumentException if the argument is null
+ */
+ boolean isSupported(TypeDef type) throws IllegalArgumentException;
+
+ /**
+ * Returns true if the map supports the specified media type with a specified value.
+ *
+ * @param mediaType the media type
+ * @param value the value
+ * @return true if the map supports the media type and the value
+ * @throws IllegalArgumentException if any argument is null
+ */
+ boolean isSupported(MediaType mediaType, V value) throws IllegalArgumentException;
+
+ /**
+ * Returns true if the map supports the specified type with a specific value.
+ *
+ * @param type the type
+ * @param value the value
+ * @return true if the map supports the media type and the value
+ * @throws IllegalArgumentException if any argument is null
+ */
+ boolean isSupported(TypeDef type, V value) throws IllegalArgumentException;
+
+ /**
+ * Returns true if the map supports the specified value for any media type or any
type.
+ *
+ * @param value the value
+ * @return true if the map supports the value
+ * @throws IllegalArgumentException if any argument is null
+ */
+ boolean isSupported(V value) throws IllegalArgumentException;
+
+ /**
+ * Returns the set of values supported for a given media type.
+ *
+ * @param mediaType the media type
+ * @return the values supported
+ * @throws IllegalArgumentException if any argument is null
+ */
+ Set<V> resolve(MediaType mediaType) throws IllegalArgumentException;
+
+ /**
+ * Returns the set of values supported for a given type.
+ *
+ * @param type the type
+ * @return the values supported
+ * @throws IllegalArgumentException if any argument is null
+ */
+ Set<V> resolve(TypeDef type) throws IllegalArgumentException;
+
+ /**
+ * Returns the set of declared media types.
+ *
+ * @return the media types
+ */
+ Set<MediaType> getMediaTypes();
+
+ /**
+ * Returns the set of declared types.
+ *
+ * @return the types
+ */
+ Set<TypeDef> getTypes();
+
+ /**
+ * Returns the set of declared values.
+ *
+ * @return the values
+ */
+ Set<V> getValues();
+}
Added:
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/net/media/MediaTypeMapImpl.java
===================================================================
---
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/net/media/MediaTypeMapImpl.java
(rev 0)
+++
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/net/media/MediaTypeMapImpl.java 2008-03-18
22:05:40 UTC (rev 10317)
@@ -0,0 +1,397 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2008, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.test.common.net.media;
+
+import org.jboss.portal.common.net.media.MediaType;
+import org.jboss.portal.common.net.media.TypeDef;
+
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Set;
+import java.util.HashSet;
+import java.util.Collections;
+
+/**
+ * @author <a href="mailto:julien@jboss-portal.org">Julien
Viet</a>
+ * @version $Revision: 630 $
+ */
+public class MediaTypeMapImpl<V> implements MediaTypeMap<V>
+{
+
+ /** . */
+ private static final String ANY = "*";
+
+ /** . */
+ private static final String ANY_ANY = "*/*";
+
+ /** . */
+ private static final char SEPARATOR = '/';
+
+ /** . */
+ private final Map<MediaType, Set<V>> mediaTypeToValues = new
HashMap<MediaType,Set<V>>();
+
+ /** . */
+ private final Map<TypeDef, Set<V>> typeToValues = new
HashMap<TypeDef,Set<V>>();
+
+ /** . */
+ private final Map<MediaType, Set<V>> combinedMediaTypeToValues = new
HashMap<MediaType,Set<V>>();
+
+ /** . */
+ private final Map<TypeDef, Set<V>> combinedTypeToValues = new
HashMap<TypeDef,Set<V>>();
+
+ /** . */
+ private final Set<V> anyTypeValues = new HashSet<V>();
+
+ /**
+ * Adds a value to the map using a media type pattern.
+ *
+ * @param mediaTypePattern the media type pattern
+ * @param value the value
+ * @throws IllegalArgumentException if any argument is null or the media type pattern
is not valid
+ */
+ public void put(String mediaTypePattern, V value) throws IllegalArgumentException
+ {
+ if (mediaTypePattern == null)
+ {
+ throw new IllegalArgumentException("No null media type pattern
accepted");
+ }
+ if (value == null)
+ {
+ throw new IllegalArgumentException("No null value accepted");
+ }
+
+ if (ANY.equals(mediaTypePattern) || ANY_ANY.equals(mediaTypePattern))
+ {
+ put(value);
+ }
+ else
+ {
+ int index = mediaTypePattern.indexOf(SEPARATOR);
+
+ //
+ if (index == -1)
+ {
+ throw new IllegalArgumentException("Not a valid media type pattern
value:" + mediaTypePattern);
+ }
+
+ //
+ String type = mediaTypePattern.substring(0, index);
+ String subtype = mediaTypePattern.substring(index + 1);
+
+ //
+ if (ANY.equals(subtype))
+ {
+ put(TypeDef.create(type), value);
+ }
+ else
+ {
+ put(MediaType.create(type, subtype), value);
+ }
+ }
+ }
+
+ /**
+ * Adds a value to the map.
+ *
+ * @param mediaType the media type
+ * @param value the value
+ * @throws IllegalArgumentException if any argument is null
+ */
+ public void put(MediaType mediaType, V value) throws IllegalArgumentException
+ {
+ if (mediaType == null)
+ {
+ throw new IllegalArgumentException("No null media type accepted");
+ }
+ if (value == null)
+ {
+ throw new IllegalArgumentException("No null value accepted");
+ }
+
+ //
+ Set<V> mediaTypeValues = mediaTypeToValues.get(mediaType);
+ if (mediaTypeValues == null)
+ {
+ mediaTypeValues = new HashSet<V>();
+ mediaTypeToValues.put(mediaType, mediaTypeValues);
+ }
+ mediaTypeValues.add(value);
+
+ //
+ Set<V> combinedMediaTypeValues = combinedMediaTypeToValues.get(mediaType);
+ if (combinedMediaTypeValues == null)
+ {
+ combinedMediaTypeValues = new HashSet<V>();
+ combinedMediaTypeToValues.put(mediaType, combinedMediaTypeValues);
+ }
+ combinedMediaTypeValues.add(value);
+
+ //
+ Set<V> combinedTypeValues = combinedTypeToValues.get(mediaType.getType());
+ if (combinedTypeValues != null)
+ {
+ combinedMediaTypeValues.addAll(combinedTypeValues);
+ }
+
+ //
+ combinedMediaTypeValues.addAll(anyTypeValues);
+ }
+
+ /**
+ * Adds a value to the map.
+ *
+ * @param type the type
+ * @param value the value
+ * @throws IllegalArgumentException if any argument is null
+ */
+ public void put(TypeDef type, V value) throws IllegalArgumentException
+ {
+ if (type == null)
+ {
+ throw new IllegalArgumentException("No null type accepted");
+ }
+ if (value == null)
+ {
+ throw new IllegalArgumentException("No null value accepted");
+ }
+
+ //
+ Set<V> typeValues = typeToValues.get(type);
+ if (typeValues == null)
+ {
+ typeValues = new HashSet<V>();
+ typeToValues.put(type, typeValues);
+ }
+ typeValues.add(value);
+
+ //
+ Set<V> combinedTypeValues = combinedTypeToValues.get(type);
+ if (combinedTypeValues == null)
+ {
+ combinedTypeValues = new HashSet<V>();
+ combinedTypeToValues.put(type, combinedTypeValues);
+ }
+ combinedTypeValues.add(value);
+
+ //
+ for (Map.Entry<MediaType, Set<V>> entry :
combinedMediaTypeToValues.entrySet())
+ {
+ if (entry.getKey().getType().equals(type))
+ {
+ entry.getValue().add(value);
+ }
+ }
+ }
+
+ /**
+ * Adds a value to the map.
+ *
+ * @param value the value
+ * @throws IllegalArgumentException if any argument is null
+ */
+ public void put(V value)
+ {
+ if (value == null)
+ {
+ throw new IllegalArgumentException("No null value accepted");
+ }
+
+ //
+ anyTypeValues.add(value);
+
+ //
+ for (Set<V> combinedTypeValues : combinedTypeToValues.values())
+ {
+ combinedTypeValues.add(value);
+ }
+
+ //
+ for (Set<V> combinedMediaTypeValues : combinedMediaTypeToValues.values())
+ {
+ combinedMediaTypeValues.add(value);
+ }
+ }
+
+ public Set<V> resolve(MediaType mediaType)
+ {
+ if (mediaType == null)
+ {
+ throw new IllegalArgumentException("No null media type accepted");
+ }
+
+ //
+ Set<V> values = combinedMediaTypeToValues.get(mediaType);
+
+ //
+ if (values == null)
+ {
+ values = resolve(mediaType.getType());
+ }
+
+ //
+ return values;
+ }
+
+ public Set<V> resolve(TypeDef type)
+ {
+ if (type == null)
+ {
+ throw new IllegalArgumentException("No null type accepted");
+ }
+
+ //
+ Set<V> values = combinedTypeToValues.get(type);
+
+ //
+ if (values == null)
+ {
+ values = getValues();
+ }
+
+ //
+ return values;
+ }
+
+ public Set<V> get(MediaType mediaType)
+ {
+ if (mediaType == null)
+ {
+ throw new IllegalArgumentException("No null media type accepted");
+ }
+
+ //
+ Set<V> values = combinedMediaTypeToValues.get(mediaType);
+
+ //
+ if (values == null)
+ {
+ values = Collections.emptySet();
+ }
+
+ //
+ return values;
+ }
+
+ public Set<V> get(TypeDef type)
+ {
+ if (type == null)
+ {
+ throw new IllegalArgumentException("No null type accepted");
+ }
+
+ //
+ Set<V> values = combinedTypeToValues.get(type);
+
+ //
+ if (values == null)
+ {
+ values = Collections.emptySet();
+ }
+
+ //
+ return values;
+ }
+
+ public Set<V> getValues()
+ {
+ return anyTypeValues;
+ }
+
+ public boolean isSupported(MediaType mediaType)
+ {
+ return resolve(mediaType).size() > 0;
+ }
+
+ public boolean isSupported(TypeDef type)
+ {
+ return resolve(type).size() > 0;
+ }
+
+ public boolean isSupported(MediaType mediaType, V value)
+ {
+ if (value == null)
+ {
+ throw new IllegalArgumentException("No null value accepted");
+ }
+
+ //
+ return resolve(mediaType).contains(value);
+ }
+
+ public boolean isSupported(TypeDef type, V value)
+ {
+ if (value == null)
+ {
+ throw new IllegalArgumentException("No null value accepted");
+ }
+
+ //
+ return resolve(type).contains(value);
+ }
+
+ public boolean isSupported(V value)
+ {
+ if (value == null)
+ {
+ throw new IllegalArgumentException("No null value accepted");
+ }
+
+ //
+ return getValues().contains(value);
+ }
+
+ public boolean contains(MediaType mediaType)
+ {
+ return get(mediaType).size() > 0;
+ }
+
+ public boolean contains(TypeDef type)
+ {
+ return get(type).size() > 0;
+ }
+
+ public boolean contains(MediaType mediaType, V value)
+ {
+ return get(mediaType).contains(value);
+ }
+
+ public boolean contains(TypeDef type, V value)
+ {
+ return get(type).contains(value);
+ }
+
+ public boolean contains(V value)
+ {
+ return getValues().contains(value);
+ }
+
+ public Set<MediaType> getMediaTypes()
+ {
+ return combinedMediaTypeToValues.keySet();
+ }
+
+ public Set<TypeDef> getTypes()
+ {
+ return combinedTypeToValues.keySet();
+ }
+}
Added:
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/net/media/MediaTypeMapTestCase.java
===================================================================
---
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/net/media/MediaTypeMapTestCase.java
(rev 0)
+++
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/net/media/MediaTypeMapTestCase.java 2008-03-18
22:05:40 UTC (rev 10317)
@@ -0,0 +1,499 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2008, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.test.common.net.media;
+
+import junit.framework.TestCase;
+import org.jboss.portal.common.net.media.MediaType;
+import org.jboss.portal.common.net.media.TypeDef;
+import org.jboss.portal.common.net.media.SubtypeDef;
+import org.jboss.portal.common.util.Tools;
+
+import java.util.Set;
+
+/**
+ * @author <a href="mailto:julien@jboss-portal.org">Julien
Viet</a>
+ * @version $Revision: 630 $
+ */
+public class MediaTypeMapTestCase extends TestCase
+{
+
+ private final String v1 = "v1";
+ private final String v2 = "v2";
+ private final String v3 = "v3";
+ private final String v4 = "v4";
+ private final String v5 = "v5";
+ private final String v6 = "v6";
+
+ private final MediaType TEXT_HTML = MediaType.TEXT_HTML;
+ private final TypeDef TEXT = TypeDef.TEXT;
+ private final MediaType TEXT_CSS = MediaType.TEXT_CSS;
+ private final MediaType TEXT_JAVASCRIPT = MediaType.TEXT_JAVASCRIPT;
+ private final MediaType IMAGE_PNG = MediaType.create(TypeDef.IMAGE,
SubtypeDef.create("png"));
+ private final TypeDef IMAGE = TypeDef.IMAGE;
+
+ private final String[] values = {v1,v2,v3,v4,v5,v6};
+
+ private void testValidity(MediaTypeMap map, int index)
+ {
+ assertEquals(all[index], map.getValues());
+
+ //
+ assertEquals(get[index][0], map.get(TEXT_HTML));
+ assertEquals(get[index][1], map.get(TEXT));
+ assertEquals(get[index][2], map.get(TEXT_CSS));
+ assertEquals(get[index][3], map.get(TEXT_JAVASCRIPT));
+ assertEquals(get[index][4], map.get(IMAGE_PNG));
+ assertEquals(get[index][5], map.get(IMAGE));
+
+ //
+ assertEquals(resolve[index][0], map.resolve(TEXT_HTML));
+ assertEquals(resolve[index][1], map.resolve(TEXT));
+ assertEquals(resolve[index][2], map.resolve(TEXT_CSS));
+ assertEquals(resolve[index][3], map.resolve(TEXT_JAVASCRIPT));
+ assertEquals(resolve[index][4], map.resolve(IMAGE_PNG));
+ assertEquals(resolve[index][5], map.resolve(IMAGE));
+
+ for (Object value : values)
+ {
+ assertEquals("Expected " + value + " to be contained in the
map", map.get(TEXT_HTML).contains(value), map.contains(TEXT_HTML, value));
+ assertEquals("Expected " + value + " to be contained in the
map", map.get(TEXT).contains(value), map.contains(TEXT, value));
+ assertEquals("Expected " + value + " to be contained in the
map", map.get(TEXT_CSS).contains(value), map.contains(TEXT_CSS, value));
+ assertEquals("Expected " + value + " to be contained in the
map", map.get(TEXT_JAVASCRIPT).contains(value), map.contains(TEXT_JAVASCRIPT,
value));
+ assertEquals("Expected " + value + " to be contained in the
map", map.get(IMAGE_PNG).contains(value), map.contains(IMAGE_PNG, value));
+ assertEquals("Expected " + value + " to be contained in the
map", map.get(IMAGE).contains(value), map.contains(IMAGE, value));
+
+ //
+ assertEquals("Expected " + value + " to be contained in the
map", map.resolve(TEXT_HTML).contains(value), map.isSupported(TEXT_HTML, value));
+ assertEquals("Expected " + value + " to be contained in the
map", map.resolve(TEXT).contains(value), map.isSupported(TEXT, value));
+ assertEquals("Expected " + value + " to be contained in the
map", map.resolve(TEXT_CSS).contains(value), map.isSupported(TEXT_CSS, value));
+ assertEquals("Expected " + value + " to be contained in the
map", map.resolve(TEXT_JAVASCRIPT).contains(value), map.isSupported(TEXT_JAVASCRIPT,
value));
+ assertEquals("Expected " + value + " to be contained in the
map", map.resolve(IMAGE_PNG).contains(value), map.isSupported(IMAGE_PNG, value));
+ assertEquals("Expected " + value + " to be contained in the
map", map.resolve(IMAGE).contains(value), map.isSupported(IMAGE, value));
+ }
+
+ assertEquals(mediaTypes[index], map.getMediaTypes());
+ assertEquals(types[index], map.getTypes());
+ }
+
+ //
+
+ private final Set all1 = Tools.toSet();
+ private final Set mediaTypes1 = Tools.toSet(TEXT_HTML);
+ private final Set types1 = Tools.toSet();
+ private final Set[] get1 = {
+ Tools.toSet(v1),
+ Tools.toSet(),
+ Tools.toSet(),
+ Tools.toSet(),
+ Tools.toSet(),
+ Tools.toSet(),
+ };
+ private final Set[] resolve1 = {
+ Tools.toSet(v1),
+ Tools.toSet(),
+ Tools.toSet(),
+ Tools.toSet(),
+ Tools.toSet(),
+ Tools.toSet()
+ };
+
+ public void testPut1()
+ {
+ MediaTypeMapImpl map = new MediaTypeMapImpl();
+ map.put(TEXT_HTML, v1);
+ testValidity(map, 0);
+ }
+
+ //
+
+ private final Set mediaTypes2 = Tools.toSet(TEXT_HTML);
+ private final Set types2 = Tools.toSet(TEXT);
+ private final Set all2 = Tools.toSet();
+ private final Set[] get2 = {
+ Tools.toSet(v1, v2),
+ Tools.toSet(v2),
+ Tools.toSet(),
+ Tools.toSet(),
+ Tools.toSet(),
+ Tools.toSet(),
+ };
+ private final Set[] resolve2 = {
+ Tools.toSet(v1, v2),
+ Tools.toSet(v2),
+ Tools.toSet(v2),
+ Tools.toSet(v2),
+ Tools.toSet(),
+ Tools.toSet()
+ };
+
+ public void testPut2()
+ {
+ MediaTypeMapImpl map = new MediaTypeMapImpl();
+ map.put(TEXT_HTML, v1);
+ map.put(TEXT, v2);
+ testValidity(map, 1);
+ }
+
+ //
+
+ private final Set mediaTypes3 = Tools.toSet(TEXT_HTML);
+
+ private final Set types3 = Tools.toSet(TEXT);
+
+ private final Set all3 = Tools.toSet(v3);
+
+ private final Set[] get3 = {
+ Tools.toSet(v1, v2, v3),
+ Tools.toSet(v2, v3),
+ Tools.toSet(),
+ Tools.toSet(),
+ Tools.toSet(),
+ Tools.toSet(),
+ };
+
+ private final Set[] resolve3 = {
+ Tools.toSet(v1, v2, v3),
+ Tools.toSet(v2, v3),
+ Tools.toSet(v2, v3),
+ Tools.toSet(v2, v3),
+ Tools.toSet(v3),
+ Tools.toSet(v3)
+ };
+
+ public void testPut3()
+ {
+ MediaTypeMapImpl map = new MediaTypeMapImpl();
+ map.put(TEXT_HTML, v1);
+ map.put(TEXT, v2);
+ map.put(v3);
+ testValidity(map, 2);
+ }
+
+ //
+
+ private final Set mediaTypes4 = Tools.toSet(TEXT_HTML, TEXT_CSS);
+
+ private final Set types4 = Tools.toSet(TEXT);
+
+ private final Set all4= Tools.toSet(v3);
+
+ private final Set[] get4 = {
+ Tools.toSet(v1, v2, v3),
+ Tools.toSet(v2, v3),
+ Tools.toSet(v2, v3, v4),
+ Tools.toSet(),
+ Tools.toSet(),
+ Tools.toSet(),
+ Tools.toSet(v3)
+ };
+
+ private final Set[] resolve4 = {
+ Tools.toSet(v1, v2, v3),
+ Tools.toSet(v2, v3),
+ Tools.toSet(v2, v3, v4),
+ Tools.toSet(v2, v3),
+ Tools.toSet(v3),
+ Tools.toSet(v3)
+ };
+
+ public void testPut4()
+ {
+ MediaTypeMapImpl map = new MediaTypeMapImpl();
+ map.put(TEXT_HTML, v1);
+ map.put(TEXT, v2);
+ map.put(v3);
+ map.put(TEXT_CSS, v4);
+ testValidity(map, 3);
+ }
+
+ //
+
+ private final Set mediaTypes5 = Tools.toSet(TEXT_HTML, TEXT_CSS);
+
+ private final Set types5 = Tools.toSet(TEXT);
+
+ private final Set all5 = Tools.toSet(v3);
+
+ private final Set[] get5 = {
+ Tools.toSet(v1, v2, v3, v5),
+ Tools.toSet(v2, v3, v5),
+ Tools.toSet(v2, v3, v4, v5),
+ Tools.toSet(),
+ Tools.toSet(),
+ Tools.toSet(),
+ };
+
+ private final Set[] resolve5 = {
+ Tools.toSet(v1, v2, v3, v5),
+ Tools.toSet(v2, v3, v5),
+ Tools.toSet(v2, v3, v4, v5),
+ Tools.toSet(v2, v3, v5),
+ Tools.toSet(v3),
+ Tools.toSet(v3)
+ };
+
+ public void testPut5()
+ {
+ MediaTypeMapImpl map = new MediaTypeMapImpl();
+ map.put(TEXT_HTML, v1);
+ map.put(TEXT, v2);
+ map.put(v3);
+ map.put(TEXT_CSS, v4);
+ map.put(TEXT, v5);
+ testValidity(map, 4);
+ }
+
+ //
+
+ private final Set mediaTypes6 = Tools.toSet(TEXT_HTML, TEXT_CSS);
+
+ private final Set types6 = Tools.toSet(TEXT);
+
+ private final Set all6 = Tools.toSet(v3, v6);
+
+ private final Set[] get6 = {
+ Tools.toSet(v1, v2, v3, v5, v6),
+ Tools.toSet(v2, v3, v5, v6),
+ Tools.toSet(v2, v3, v4, v5, v6),
+ Tools.toSet(),
+ Tools.toSet(),
+ Tools.toSet(),
+ };
+
+ private final Set[] resolve6 = {
+ Tools.toSet(v1, v2, v3, v5, v6),
+ Tools.toSet(v2, v3, v5, v6),
+ Tools.toSet(v2, v3, v4, v5, v6),
+ Tools.toSet(v2, v3, v5, v6),
+ Tools.toSet(v3, v6),
+ Tools.toSet(v3, v6)
+ };
+
+ public void testPut6()
+ {
+ MediaTypeMapImpl map = new MediaTypeMapImpl();
+ map.put(TEXT_HTML, v1);
+ map.put(TEXT, v2);
+ map.put(v3);
+ map.put(TEXT_CSS, v4);
+ map.put(TEXT, v5);
+ map.put(v6);
+ testValidity(map, 5);
+ }
+
+ private Set[] types = {types1,types2,types3,types4,types5,types6};
+ private Set[] mediaTypes =
{mediaTypes1,mediaTypes2,mediaTypes3,mediaTypes4,mediaTypes5,mediaTypes6};
+ private Set[] all = {all1,all2,all3,all4,all5,all6};
+ private Set[][] get = {get1,get2,get3,get4,get5,get6};
+ private Set[][] resolve = {resolve1,resolve2,resolve3,resolve4,resolve5,resolve6};
+
+ public void interfaceThrowsIAE()
+ {
+ try
+ {
+ new MediaTypeMapImpl().get((MediaType)null);
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ }
+ try
+ {
+ new MediaTypeMapImpl().get((TypeDef)null);
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ }
+ try
+ {
+ new MediaTypeMapImpl().resolve((MediaType)null);
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ }
+ try
+ {
+ new MediaTypeMapImpl().resolve((TypeDef)null);
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ }
+ try
+ {
+ new MediaTypeMapImpl().isSupported((MediaType)null);
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ }
+ try
+ {
+ new MediaTypeMapImpl().isSupported(TEXT, null);
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ }
+ try
+ {
+ new MediaTypeMapImpl().isSupported(TEXT_HTML, null);
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ }
+ try
+ {
+ new MediaTypeMapImpl().isSupported((TypeDef)null);
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ }
+ try
+ {
+ new MediaTypeMapImpl().isSupported((Object)null);
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ }
+ try
+ {
+ new MediaTypeMapImpl().contains((MediaType)null);
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ }
+ try
+ {
+ new MediaTypeMapImpl().contains(TEXT, null);
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ }
+ try
+ {
+ new MediaTypeMapImpl().contains(TEXT_HTML, null);
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ }
+ try
+ {
+ new MediaTypeMapImpl().contains((TypeDef)null);
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ }
+ try
+ {
+ new MediaTypeMapImpl().contains((Object)null);
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ }
+ }
+
+ public void testAddUsingPattern1()
+ {
+ MediaTypeMapImpl map = new MediaTypeMapImpl();
+ map.put("text/html", v1);
+ assertEquals(Tools.toSet(v1), map.get(MediaType.TEXT_HTML));
+ }
+
+ public void testAddUsingPattern2()
+ {
+ MediaTypeMapImpl map = new MediaTypeMapImpl();
+ map.put("text/*", v1);
+ assertEquals(Tools.toSet(v1), map.get(TEXT));
+ }
+
+ public void testAddUsingPattern3()
+ {
+ MediaTypeMapImpl map = new MediaTypeMapImpl();
+ map.put("*/*", v1);
+ assertEquals(Tools.toSet(v1), map.getValues());
+ }
+
+ private void testAddUsingPatternThrowsIAE(String pattern, String value)
+ {
+ MediaTypeMapImpl map = new MediaTypeMapImpl();
+ try
+ {
+ map.put(pattern, value);
+ fail() ;
+ }
+ catch (IllegalArgumentException ignore)
+ {
+ }
+ }
+
+ public void testAddUsingPatternThrowsIAE4()
+ {
+ testAddUsingPatternThrowsIAE(null, v1);
+ }
+
+ public void testAddUsingPattern5()
+ {
+ testAddUsingPatternThrowsIAE(null, null);
+ }
+
+ public void testAddUsingPattern6()
+ {
+ testAddUsingPatternThrowsIAE("text/html", null);
+ }
+
+ public void testAddUsingPattern7()
+ {
+ testAddUsingPatternThrowsIAE("", v1);
+ }
+
+ public void testAddUsingPattern8()
+ {
+ testAddUsingPatternThrowsIAE("text", v1);
+ }
+
+ public void testAddUsingPattern9()
+ {
+ testAddUsingPatternThrowsIAE("foo/html", v1);
+ }
+
+ public void testAddUsingPattern10()
+ {
+ testAddUsingPatternThrowsIAE("text/", v1);
+ }
+
+ public void testAddUsingPattern11()
+ {
+ testAddUsingPatternThrowsIAE("/html", v1);
+ }
+}