Author: julien(a)jboss.com
Date: 2008-03-18 10:27:24 -0400 (Tue, 18 Mar 2008)
New Revision: 10306
Added:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/net/media/
modules/common/trunk/common/src/main/java/org/jboss/portal/common/net/media/ContentType.java
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/Parameter.java
modules/common/trunk/common/src/main/java/org/jboss/portal/common/net/media/SubtypeDef.java
modules/common/trunk/common/src/main/java/org/jboss/portal/common/net/media/TypeDef.java
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/net/media/
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/net/media/ContentTypeTestCase.java
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/net/media/MediaTypeTestCase.java
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/net/media/SubtypeDefTestCase.java
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/net/media/TypeDefTestCase.java
Log:
started to add classes for handling media type and content type properly
Added:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/net/media/ContentType.java
===================================================================
---
modules/common/trunk/common/src/main/java/org/jboss/portal/common/net/media/ContentType.java
(rev 0)
+++
modules/common/trunk/common/src/main/java/org/jboss/portal/common/net/media/ContentType.java 2008-03-18
14:27:24 UTC (rev 10306)
@@ -0,0 +1,178 @@
+/******************************************************************************
+ * 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.common.net.media;
+
+import java.util.List;
+import java.util.Collections;
+import java.util.ArrayList;
+import java.util.Iterator;
+
+/**
+ * @author <a href="mailto:julien@jboss-portal.org">Julien
Viet</a>
+ * @version $Revision: 630 $
+ */
+public class ContentType
+{
+
+ public static ContentType create(String contentTypeValue)
+ {
+ int slashIndex = contentTypeValue.indexOf('/');
+
+ if (slashIndex == -1)
+ {
+
+ }
+
+ String typeName = contentTypeValue.substring(0, slashIndex);
+
+ int semiColonIndex = contentTypeValue.indexOf(';', slashIndex + 1);
+
+ if (semiColonIndex == -1)
+ {
+ return new ContentType(MediaType.create(typeName,
contentTypeValue.substring(slashIndex + 1)));
+
+ }
+
+ //
+ String subtypeName = contentTypeValue.substring(slashIndex + 1, semiColonIndex);
+
+ //
+ MediaType mediaType = MediaType.create(typeName, subtypeName);
+
+ //
+ List<Parameter> parameters = null;
+
+ for (int i = semiColonIndex + 1;semiColonIndex != -1;i = semiColonIndex + 1)
+ {
+ semiColonIndex = contentTypeValue.indexOf(';', i);
+
+ //
+ if (semiColonIndex == 0)
+ {
+ // We have 2 following semi colons
+ throw new IllegalArgumentException();
+ }
+
+ // Compute the to
+ int to = semiColonIndex == -1 ? contentTypeValue.length() : semiColonIndex;
+
+ // Get the equals char position
+ int equalsIndex = contentTypeValue.indexOf('=', i);
+
+ //
+ if (equalsIndex != -1 && equalsIndex < to)
+ {
+ if (parameters == null)
+ {
+ parameters = new ArrayList<Parameter>();
+ }
+
+ //
+ String name = contentTypeValue.substring(i, equalsIndex);
+ String value = contentTypeValue.substring(equalsIndex + 1, to);
+ parameters.add(new Parameter(name, value));
+ }
+ else
+ {
+ throw new IllegalArgumentException("No equals char found in the string
" + contentTypeValue.substring(i, to));
+ }
+ }
+
+ //
+ if (parameters == null)
+ {
+ parameters = Collections.emptyList();
+ }
+
+ //
+ return new ContentType(mediaType, parameters);
+ }
+
+ /** . */
+ private MediaType mediaType;
+
+ /** . */
+ private List<Parameter> parameters;
+
+ public ContentType(MediaType mediaType, List<Parameter> parameters)
+ {
+ if (mediaType == null)
+ {
+ throw new IllegalArgumentException("No null media type accepted");
+ }
+ if (parameters == null)
+ {
+ throw new IllegalArgumentException("No null parameter list
accepted");
+ }
+
+ //
+ this.mediaType = mediaType;
+ this.parameters = parameters;
+ }
+
+ public ContentType(MediaType mediaType)
+ {
+ if (mediaType == null)
+ {
+ throw new IllegalArgumentException("No null media type accepted");
+ }
+
+ //
+ this.mediaType = mediaType;
+ this.parameters = Collections.emptyList();
+ }
+
+ public MediaType getMediaType()
+ {
+ return mediaType;
+ }
+
+ public List<Parameter> getParameters()
+ {
+ return parameters;
+ }
+
+ public String getValue()
+ {
+ StringBuilder builder = new StringBuilder();
+
builder.append(mediaType.getType().getName()).append('/').append(mediaType.getSubtype().getName());
+ for (Iterator<Parameter> i = parameters.iterator();;)
+ {
+ Parameter parameter = i.next();
+
+ //
+
builder.append(parameter.getName()).append('=').append(parameter.getValue());
+
+ //
+ if (i.hasNext())
+ {
+ builder.append(';');
+ }
+ else
+ {
+ break;
+ }
+ }
+ return builder.toString();
+ }
+}
Added:
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
(rev 0)
+++
modules/common/trunk/common/src/main/java/org/jboss/portal/common/net/media/MediaType.java 2008-03-18
14:27:24 UTC (rev 10306)
@@ -0,0 +1,145 @@
+/******************************************************************************
+ * 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.common.net.media;
+
+/**
+ * @author <a href="mailto:julien@jboss-portal.org">Julien
Viet</a>
+ * @version $Revision: 630 $
+ */
+public final class MediaType
+{
+
+ /** . */
+ public static final MediaType TEXT_HTML = new MediaType(TypeDef.TEXT,
SubtypeDef.HTML);
+
+ /** . */
+ public static final MediaType TEXT_JAVASCRIPT = new MediaType(TypeDef.TEXT,
SubtypeDef.JAVASCRIPT);
+
+ /** . */
+ public static final MediaType TEXT_CSS = new MediaType(TypeDef.TEXT, SubtypeDef.CSS);
+
+ /** . */
+ public static final MediaType APPLICATION_X_WWW_FORM_URLENCODED = new
MediaType(TypeDef.APPLICATION, SubtypeDef.X_WWW_FORM_URLENCODED);
+
+ /** The type identifier. */
+ private final TypeDef type;
+
+ /** The sub type identifier. */
+ private final SubtypeDef subtype;
+
+ /** . */
+ private volatile Integer hashCode;
+
+ /** . */
+ private volatile String toString;
+
+ /** . */
+ private volatile String value;
+
+ public static MediaType create(String typeName, String subtypeName)
+ {
+ TypeDef type = TypeDef.create(typeName);
+
+ //
+ if (type == null)
+ {
+ return null;
+ }
+
+ //
+ SubtypeDef subtype = SubtypeDef.create(subtypeName);
+
+ //
+ return new MediaType(type, subtype);
+ }
+
+ public static MediaType create(TypeDef type, SubtypeDef subtype)
+ {
+ if (type == null)
+ {
+ throw new IllegalArgumentException("No null type accepted");
+ }
+ if (subtype == null)
+ {
+ throw new IllegalArgumentException("No null subtype accepted");
+ }
+ return new MediaType(type, subtype);
+ }
+
+ MediaType(TypeDef type, SubtypeDef subtype)
+ {
+ this.type = type;
+ this.subtype = subtype;
+ }
+
+ public TypeDef getType()
+ {
+ return type;
+ }
+
+ public SubtypeDef getSubtype()
+ {
+ return subtype;
+ }
+
+ public String getValue()
+ {
+ if (value == null)
+ {
+ value = type.getName() + "/" + subtype.getName();
+ }
+ return value;
+ }
+
+ public int hashCode()
+ {
+ if (hashCode == null)
+ {
+ hashCode = type.hashCode() + subtype.hashCode();
+ }
+ return hashCode;
+ }
+
+ public boolean equals(Object obj)
+ {
+ if (obj == this)
+ {
+ return true;
+ }
+ if (obj instanceof MediaType)
+ {
+ MediaType that = (MediaType)obj;
+ return type.equals(that.type) && subtype.equals(that.subtype);
+ }
+ return false;
+ }
+
+ public String toString()
+ {
+ if (toString == null)
+ {
+ toString = "MediaType[name=" + type + ",subtype=" + subtype
+ "]";
+ }
+ return toString;
+ }
+}
Added:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/net/media/Parameter.java
===================================================================
---
modules/common/trunk/common/src/main/java/org/jboss/portal/common/net/media/Parameter.java
(rev 0)
+++
modules/common/trunk/common/src/main/java/org/jboss/portal/common/net/media/Parameter.java 2008-03-18
14:27:24 UTC (rev 10306)
@@ -0,0 +1,53 @@
+/******************************************************************************
+ * 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.common.net.media;
+
+/**
+ * @author <a href="mailto:julien@jboss-portal.org">Julien
Viet</a>
+ * @version $Revision: 630 $
+ */
+public class Parameter
+{
+
+ /** . */
+ private final String name;
+
+ /** . */
+ private final String value;
+
+ public Parameter(String name, String value)
+ {
+ this.name = name;
+ this.value = value;
+ }
+
+ public String getName()
+ {
+ return name;
+ }
+
+ public String getValue()
+ {
+ return value;
+ }
+}
Added:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/net/media/SubtypeDef.java
===================================================================
---
modules/common/trunk/common/src/main/java/org/jboss/portal/common/net/media/SubtypeDef.java
(rev 0)
+++
modules/common/trunk/common/src/main/java/org/jboss/portal/common/net/media/SubtypeDef.java 2008-03-18
14:27:24 UTC (rev 10306)
@@ -0,0 +1,121 @@
+/******************************************************************************
+ * 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.common.net.media;
+
+import java.util.Locale;
+
+/**
+ * @author <a href="mailto:julien@jboss-portal.org">Julien
Viet</a>
+ * @version $Revision: 630 $
+ */
+public final class SubtypeDef
+{
+
+ /** . */
+ public static final SubtypeDef HTML = new SubtypeDef("html");
+
+ /** . */
+ public static final SubtypeDef XML = new SubtypeDef("xml");
+
+ /** . */
+ public static final SubtypeDef CSS = new SubtypeDef("css");
+
+ /** . */
+ public static final SubtypeDef PLAIN = new SubtypeDef("plain");
+
+ /** . */
+ public static final SubtypeDef JAVASCRIPT = new SubtypeDef("javascript");
+
+ /** . */
+ public static final SubtypeDef X_WWW_FORM_URLENCODED = new
SubtypeDef("x-www-form-urlencoded");
+
+ /**
+ * Returns a corresponding subtype definition for the specified subtype name.
+ *
+ * @param subtypeName the subtype name
+ * @return a subtype definition
+ * @throws IllegalArgumentException if the subtype name is null or is empty
+ */
+ public static SubtypeDef create(String subtypeName) throws IllegalArgumentException
+ {
+ if (subtypeName == null)
+ {
+ throw new IllegalArgumentException("No null subtype name accepted");
+ }
+ if (subtypeName.length() == 0)
+ {
+ throw new IllegalArgumentException("No empty subtype name accepted");
+ }
+ return new SubtypeDef(subtypeName);
+ }
+
+ /** . */
+ private final String name;
+
+ /** . */
+ private volatile String toString;
+
+ SubtypeDef(String name)
+ {
+ if (name == null)
+ {
+ throw new IllegalArgumentException("No null name allowed");
+ }
+
+ //
+ this.name = name.toLowerCase(Locale.ENGLISH);
+ }
+
+ public String getName()
+ {
+ return name;
+ }
+
+ public int hashCode()
+ {
+ return name.hashCode();
+ }
+
+ public boolean equals(Object obj)
+ {
+ if (obj == this)
+ {
+ return true;
+ }
+ if (obj instanceof SubtypeDef)
+ {
+ SubtypeDef that = (SubtypeDef)obj;
+ return name.equals(that.name);
+ }
+ return false;
+ }
+
+ public String toString()
+ {
+ if (toString == null)
+ {
+ toString = "SubtypeDef[" + name + "]";
+ }
+ return toString;
+ }
+}
Added:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/net/media/TypeDef.java
===================================================================
---
modules/common/trunk/common/src/main/java/org/jboss/portal/common/net/media/TypeDef.java
(rev 0)
+++
modules/common/trunk/common/src/main/java/org/jboss/portal/common/net/media/TypeDef.java 2008-03-18
14:27:24 UTC (rev 10306)
@@ -0,0 +1,148 @@
+/******************************************************************************
+ * 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.common.net.media;
+
+/**
+ * A top level media type definition defined by <a
href="http://tools.ietf.org/html/rfc2046#section-2">RFC2046 section
2</a>).
+ *
+ * @author <a href="mailto:julien@jboss-portal.org">Julien
Viet</a>
+ * @version $Revision: 630 $
+ */
+public final class TypeDef
+{
+
+ // The five discrete top-level media types
+
+ /** . */
+ public static final TypeDef TEXT = new TypeDef("text", "",
false);
+
+ /** . */
+ public static final TypeDef IMAGE = new TypeDef("image", "",
false);
+
+ /** . */
+ public static final TypeDef AUDIO = new TypeDef("audio", "",
false);
+
+ /** . */
+ public static final TypeDef VIDEO = new TypeDef("video", "",
false);
+
+ /** . */
+ public static final TypeDef APPLICATION = new TypeDef("application",
"", false);
+
+ // The two composite top-level media types
+
+ /** . */
+ public static final TypeDef MULTIPART = new TypeDef("multipart",
"", true);
+
+ /** . */
+ public static final TypeDef MESSAGE = new TypeDef("message", "",
true);
+
+ /**
+ * Returns the corresponding type definition for the given top level type name.
+ * If the type name does not correspond to a top level name, then null is returned.
+ *
+ * @param typeName the name of the type def
+ * @return the corresponding type def
+ * @throws IllegalArgumentException if the type name is null
+ */
+ public static TypeDef create(String typeName) throws IllegalArgumentException
+ {
+ if (typeName == null)
+ {
+ throw new IllegalArgumentException("no null type name accepted");
+ }
+ if (TEXT.name.equalsIgnoreCase(typeName))
+ {
+ return TEXT;
+ }
+ if (APPLICATION.name.equalsIgnoreCase(typeName))
+ {
+ return APPLICATION;
+ }
+ if (MULTIPART.name.equalsIgnoreCase(typeName))
+ {
+ return MULTIPART;
+ }
+ if (IMAGE.name.equalsIgnoreCase(typeName))
+ {
+ return IMAGE;
+ }
+ if (AUDIO.name.equalsIgnoreCase(typeName))
+ {
+ return AUDIO;
+ }
+ if (VIDEO.name.equalsIgnoreCase(typeName))
+ {
+ return VIDEO;
+ }
+ if (MESSAGE.name.equalsIgnoreCase(typeName))
+ {
+ return MESSAGE;
+ }
+ return null;
+ }
+
+ /** . */
+ private final String name;
+
+ /** . */
+ private final String description;
+
+ /** . */
+ private final boolean composite;
+
+ /** . */
+ private final String toString;
+
+ private TypeDef(String name, String description, boolean composite)
+ {
+ this.name = name;
+ this.description = description;
+ this.composite = composite;
+ this.toString = "TypeDef[" + name + "]";
+ }
+
+ public String getName()
+ {
+ return name;
+ }
+
+ public String getDescription()
+ {
+ return description;
+ }
+
+ public boolean isComposite()
+ {
+ return composite;
+ }
+
+ public boolean isDiscrete()
+ {
+ return !composite;
+ }
+
+ public String toString()
+ {
+ return toString;
+ }
+}
Added:
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/net/media/ContentTypeTestCase.java
===================================================================
---
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/net/media/ContentTypeTestCase.java
(rev 0)
+++
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/net/media/ContentTypeTestCase.java 2008-03-18
14:27:24 UTC (rev 10306)
@@ -0,0 +1,97 @@
+/******************************************************************************
+ * 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.ContentType;
+import org.jboss.portal.common.net.media.Parameter;
+import org.jboss.portal.common.net.media.MediaType;
+
+/**
+ * @author <a href="mailto:julien@jboss-portal.org">Julien
Viet</a>
+ * @version $Revision: 630 $
+ */
+public class ContentTypeTestCase extends TestCase
+{
+
+ public void testBlah()
+ {
+ assertEquals("text/html", MediaType.TEXT_HTML);
+ assertEquals("text/html;a=b", MediaType.TEXT_HTML, new
Parameter("a", "b"));
+ assertEquals("text/html;a=b;c=d", MediaType.TEXT_HTML, new
Parameter("a", "b"), new Parameter("c", "d"));
+ }
+
+ public void testBlih()
+ {
+ assertCannotBuild("foo/html");
+ assertCannotBuild("text/");
+ assertCannotBuild("texthtml");
+ assertCannotBuild("text/html;");
+ assertCannotBuild("text/html;;");
+ assertCannotBuild("text/html;ab");
+ assertCannotBuild("text/html;ab;");
+ assertCannotBuild("text/html;a=b;");
+ assertCannotBuild("text/html;a=;");
+ assertCannotBuild("text/html;=b;");
+ assertCannotBuild("text/html;=;");
+ }
+
+ private void assertCannotBuild(String contentTypeValue)
+ {
+ try
+ {
+ ContentType.create(contentTypeValue);
+ fail("Was able to build content type with value " +
contentTypeValue);
+ }
+ catch (Exception ignore)
+ {
+ }
+ }
+
+ private void assertEquals(String contentTypeValue, MediaType mediaType)
+ {
+ assertEquals(contentTypeValue, mediaType, new Parameter[0]);
+ }
+
+ private void assertEquals(String contentTypeValue, MediaType mediaType, Parameter
parameter)
+ {
+ assertEquals(contentTypeValue, mediaType, new Parameter[]{parameter});
+ }
+
+ private void assertEquals(String contentTypeValue, MediaType mediaType, Parameter...
parameters)
+ {
+ ContentType ct = ContentType.create(contentTypeValue);
+ assertNotNull(ct);
+ assertEquals(ct.getMediaType(), mediaType);
+ assertNotNull(ct.getParameters());
+ assertEquals(parameters.length, ct.getParameters().size());
+ for (int i = 0;i < ct.getParameters().size();i++)
+ {
+ Parameter parameter = ct.getParameters().get(i);
+ assertNotNull(parameter);
+ assertEquals(parameters[i].getName(), parameter.getName());
+ assertEquals(parameters[i].getValue(), parameter.getValue());
+ }
+ }
+
+}
Added:
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/net/media/MediaTypeTestCase.java
===================================================================
---
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/net/media/MediaTypeTestCase.java
(rev 0)
+++
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/net/media/MediaTypeTestCase.java 2008-03-18
14:27:24 UTC (rev 10306)
@@ -0,0 +1,97 @@
+/******************************************************************************
+ * 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;
+
+/**
+ * @author <a href="mailto:julien@jboss-portal.org">Julien
Viet</a>
+ * @version $Revision: 630 $
+ */
+public class MediaTypeTestCase extends TestCase
+{
+ public void testBasic()
+ {
+ MediaType textHTML = MediaType.create(TypeDef.TEXT, SubtypeDef.HTML);
+ assertEquals(TypeDef.TEXT, textHTML.getType());
+ assertEquals(SubtypeDef.HTML, textHTML.getSubtype());
+
+ //
+ MediaType textHTML2 = MediaType.create("text", "html");
+ assertEquals(TypeDef.TEXT, textHTML2.getType());
+ assertEquals(SubtypeDef.HTML, textHTML2.getSubtype());
+
+ //
+ assertEquals(MediaType.TEXT_HTML, textHTML);
+ assertEquals(MediaType.TEXT_HTML, textHTML2);
+ }
+
+ public void testIAE()
+ {
+ try
+ {
+ MediaType.create(null, SubtypeDef.HTML);
+ }
+ catch (IllegalArgumentException e)
+ {
+ }
+ try
+ {
+ MediaType.create(TypeDef.TEXT, null);
+ }
+ catch (Exception e)
+ {
+ }
+ try
+ {
+ MediaType.create((TypeDef)null, null);
+ }
+ catch (Exception e)
+ {
+ }
+ try
+ {
+ MediaType.create((String)null, null);
+ }
+ catch (Exception e)
+ {
+ }
+ try
+ {
+ MediaType.create(null, "html");
+ }
+ catch (Exception e)
+ {
+ }
+ try
+ {
+ MediaType.create("test", null);
+ }
+ catch (Exception e)
+ {
+ }
+ }
+}
Added:
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/net/media/SubtypeDefTestCase.java
===================================================================
---
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/net/media/SubtypeDefTestCase.java
(rev 0)
+++
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/net/media/SubtypeDefTestCase.java 2008-03-18
14:27:24 UTC (rev 10306)
@@ -0,0 +1,67 @@
+/******************************************************************************
+ * 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.SubtypeDef;
+
+/**
+ * @author <a href="mailto:julien@jboss-portal.org">Julien
Viet</a>
+ * @version $Revision: 630 $
+ */
+public class SubtypeDefTestCase extends TestCase
+{
+
+ public void testCaseSensitivity()
+ {
+ assertEquals(SubtypeDef.HTML, SubtypeDef.create("html"));
+ assertEquals(SubtypeDef.HTML, SubtypeDef.create("hTml"));
+ assertEquals(SubtypeDef.HTML, SubtypeDef.create("HTML"));
+
+ //
+ SubtypeDef foo = SubtypeDef.create("foo");
+ assertEquals(foo, SubtypeDef.create("foo"));
+ assertEquals(foo, SubtypeDef.create("fOo"));
+ assertEquals(foo, SubtypeDef.create("FOO"));
+ }
+
+ public void testIAE()
+ {
+ try
+ {
+ SubtypeDef.create(null);
+ fail();
+ }
+ catch (IllegalArgumentException ignore)
+ {
+ }
+ try
+ {
+ SubtypeDef.create("");
+ fail();
+ }
+ catch (IllegalArgumentException ignore)
+ {
+ }
+ }
+}
Added:
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/net/media/TypeDefTestCase.java
===================================================================
---
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/net/media/TypeDefTestCase.java
(rev 0)
+++
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/net/media/TypeDefTestCase.java 2008-03-18
14:27:24 UTC (rev 10306)
@@ -0,0 +1,80 @@
+/******************************************************************************
+ * 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.TypeDef;
+
+/**
+ * @author <a href="mailto:julien@jboss-portal.org">Julien
Viet</a>
+ * @version $Revision: 630 $
+ */
+public class TypeDefTestCase extends TestCase
+{
+
+ public void testIAE()
+ {
+ try
+ {
+ TypeDef.create(null);
+ }
+ catch (IllegalArgumentException ignore)
+ {
+ }
+ }
+
+ public void testNotRecognized()
+ {
+ assertNull(TypeDef.create(""));
+ assertNull(TypeDef.create("foo"));
+ }
+
+ public void testRecognized()
+ {
+ assertEquals(TypeDef.TEXT, TypeDef.create("text"));
+ assertEquals(TypeDef.AUDIO, TypeDef.create("audio"));
+ assertEquals(TypeDef.VIDEO, TypeDef.create("video"));
+ assertEquals(TypeDef.IMAGE, TypeDef.create("image"));
+ assertEquals(TypeDef.APPLICATION, TypeDef.create("application"));
+ assertEquals(TypeDef.MESSAGE, TypeDef.create("message"));
+ assertEquals(TypeDef.MULTIPART, TypeDef.create("multipart"));
+
+ //
+ assertEquals(TypeDef.TEXT, TypeDef.create("TEXT"));
+ assertEquals(TypeDef.AUDIO, TypeDef.create("AUDIO"));
+ assertEquals(TypeDef.VIDEO, TypeDef.create("VIDEO"));
+ assertEquals(TypeDef.IMAGE, TypeDef.create("IMAGE"));
+ assertEquals(TypeDef.APPLICATION, TypeDef.create("APPLICATION"));
+ assertEquals(TypeDef.MESSAGE, TypeDef.create("MESSAGE"));
+ assertEquals(TypeDef.MULTIPART, TypeDef.create("MULTIPART"));
+
+ //
+ assertEquals(TypeDef.TEXT, TypeDef.create("tExt"));
+ assertEquals(TypeDef.AUDIO, TypeDef.create("aUdio"));
+ assertEquals(TypeDef.VIDEO, TypeDef.create("vIdeo"));
+ assertEquals(TypeDef.IMAGE, TypeDef.create("iMage"));
+ assertEquals(TypeDef.APPLICATION, TypeDef.create("aPplication"));
+ assertEquals(TypeDef.MESSAGE, TypeDef.create("mEssage"));
+ assertEquals(TypeDef.MULTIPART, TypeDef.create("mUltipart"));
+ }
+}