geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a..@apache.org
Subject cvs commit: incubator-geronimo/specs/activation/src/test/javax/activation MimeTypeParameterListTest.java MimeTypeTest.java
Date Sun, 07 Dec 2003 16:11:44 GMT
adc         2003/12/07 08:11:44

  Modified:    specs/activation/src/java/javax/activation MimeType.java
                        MimeTypeParameterList.java
  Added:       specs/activation/src/test/javax/activation
                        MimeTypeParameterListTest.java MimeTypeTest.java
  Log:
  Partial JAF spec by Laurent Foret.
  
  Revision  Changes    Path
  1.2       +63 -30    incubator-geronimo/specs/activation/src/java/javax/activation/MimeType.java
  
  Index: MimeType.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/specs/activation/src/java/javax/activation/MimeType.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- MimeType.java	16 Aug 2003 18:07:45 -0000	1.1
  +++ MimeType.java	7 Dec 2003 16:11:44 -0000	1.2
  @@ -67,86 +67,119 @@
   import java.io.ObjectInput;
   import java.io.ObjectOutput;
   
  +
   /**
  - *
  - *
  - *
    * @version $Revision$ $Date$
    */
   public class MimeType implements Externalizable {
  +    private final static String TYPE_SEPARATOR = "/";
  +    private final static String PARAMETER_SEPARATOR = ";";
  +    private final static String STAR_SUB_TYPE = "*";
  +
  +    private String primaryType = "text";
  +    private String subType = "plain";
  +    private MimeTypeParameterList parameterList = new MimeTypeParameterList();;
  +
       public MimeType() {
  -        /*@todo implement*/
       }
   
       public MimeType(String rawdata) throws MimeTypeParseException {
  -        /*@todo implement*/
  +        parseMimeType(rawdata);
       }
   
       public MimeType(String primary, String sub) throws MimeTypeParseException {
  -        /*@todo implement*/
  +        setPrimaryType(primary);
  +        setSubType(sub);
       }
   
       public String getPrimaryType() {
  -        /*@todo implement*/
  -        return null;
  +        return primaryType;
       }
   
       public void setPrimaryType(String primary) throws MimeTypeParseException {
  -        /*@todo implement*/
  +        primaryType = parseToken(primary);
       }
   
       public String getSubType() {
  -        /*@todo implement*/
  -        return null;
  +        return subType;
       }
   
       public void setSubType(String sub) throws MimeTypeParseException {
  -        /*@todo implement*/
  +        subType = parseToken(sub);
       }
   
       public MimeTypeParameterList getParameters() {
  -        /*@todo implement*/
  -        return null;
  +        return parameterList;
       }
   
       public String getParameter(String name) {
  -        /*@todo implement*/
  -        return null;
  +        return parameterList.get(name);
       }
   
       public void setParameter(String name, String value) {
  -        /*@todo implement*/
  +        parameterList.set(name, value);
       }
   
       public void removeParameter(String name) {
  -        /*@todo implement*/
  +        parameterList.remove(name);
       }
   
       public String toString() {
  -        /*@todo implement*/
  -        return null;
  +        return getBaseType() +
  +                (parameterList == null
  +                ? ""
  +                : PARAMETER_SEPARATOR + parameterList.toString());
       }
   
       public String getBaseType() {
  -        /*@todo implement*/
  -        return null;
  +        return getPrimaryType() + TYPE_SEPARATOR + getSubType();
       }
   
       public boolean match(MimeType type) {
  -        /*@todo implement*/
  -        return false;
  +        return (
  +                getPrimaryType().equals(type.getPrimaryType())
  +                && (getSubType().equals(STAR_SUB_TYPE)
  +                || type.getSubType().equals(STAR_SUB_TYPE)
  +                || getSubType().equals(type.getSubType())));
       }
   
       public boolean match(String rawdata) throws MimeTypeParseException {
  -         /*@todo implement*/
  -        return false;
  +        return match(new MimeType(rawdata));
       }
   
       public void writeExternal(ObjectOutput out) throws IOException {
  -        /*@todo implement*/
  +        out.writeUTF(toString());
  +        out.flush();
       }
   
       public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
{
  -        /*@todo implement*/
  +        try {
  +            parseMimeType(in.readUTF());
  +        } catch (MimeTypeParseException mtpex) {
  +            throw new IOException(mtpex.getMessage());
  +        }
       }
  -}
  \ No newline at end of file
  +
  +    private void parseMimeType(String rawData) throws MimeTypeParseException {
  +        int typeSeparatorPos = rawData.indexOf(TYPE_SEPARATOR);
  +        int parameterSeparatorPos = rawData.indexOf(PARAMETER_SEPARATOR);
  +
  +        if (typeSeparatorPos < 0) {
  +            throw new MimeTypeParseException("Unable to find subtype");
  +        }
  +
  +        setPrimaryType(rawData.substring(0, typeSeparatorPos));
  +        if (parameterSeparatorPos < 0) {
  +            setSubType(rawData.substring(typeSeparatorPos + 1));
  +        } else {
  +            setSubType(rawData.substring(typeSeparatorPos + 1, parameterSeparatorPos));
  +            parameterList = new MimeTypeParameterList(rawData.substring(parameterSeparatorPos
+ 1));
  +        }
  +    }
  +
  +    private static String parseToken(String tokenString) {
  +        // TODO it seems to have unauthorized chars
  +        return tokenString;
  +    }
  +
  +}
  
  
  
  1.2       +68 -19    incubator-geronimo/specs/activation/src/java/javax/activation/MimeTypeParameterList.java
  
  Index: MimeTypeParameterList.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/specs/activation/src/java/javax/activation/MimeTypeParameterList.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- MimeTypeParameterList.java	16 Aug 2003 18:07:45 -0000	1.1
  +++ MimeTypeParameterList.java	7 Dec 2003 16:11:44 -0000	1.2
  @@ -63,56 +63,105 @@
   package javax.activation;
   
   import java.util.Enumeration;
  +import java.util.StringTokenizer;
  +import java.util.Collections;
  +import java.util.HashMap;
  +import java.util.Map;
  +
   
   /**
  - *
  - *
  - *
    * @version $Revision$ $Date$
    */
   public class MimeTypeParameterList {
  +    private final static String PARAMETER_SEPARATOR = ";";
  +    private final static String NAME_VALUE_SEPARATOR = "=";
  +
  +    Map _mimeTypeParameterMap = new HashMap();
  +
       public MimeTypeParameterList() {
  -        /*@todo implement*/
  +
       }
   
       public MimeTypeParameterList(String parameterList) throws MimeTypeParseException {
  -        /*@todo implement*/
  +        parse(parameterList);
       }
   
       protected void parse(String parameterList) throws MimeTypeParseException {
  -        /*@todo implement*/
  +        if (parameterList == null) {
  +            return;
  +        }
  +
  +        StringTokenizer tokenizer = new StringTokenizer(parameterList, PARAMETER_SEPARATOR);
  +        while (tokenizer.hasMoreTokens()) {
  +            String parameter = tokenizer.nextToken();
  +            if (parameter.length() == 0) {
  +                continue;
  +            }
  +            int eq = parameter.indexOf(NAME_VALUE_SEPARATOR);
  +            String name = null;
  +            if (eq > -1) {
  +                name = parseToken(parameter.substring(0, eq));
  +            }
  +            String value = parseToken(parameter.substring(eq + 1));
  +            if ((name == null || name.length() == 0) && value.length() == 0) {
  +                continue;
  +            }
  +            if (name.length() == 0 || value.length() == 0) {
  +                throw new MimeTypeParseException("Name or value is Missing");
  +            }
  +            set(name, value);
  +
  +        }
  +
       }
   
       public int size() {
  -        /*@todo implement*/
  -        return -1;
  +        return _mimeTypeParameterMap.size();
       }
   
       public boolean isEmpty() {
  -        /*@todo implement*/
  -        return false;
  +        return _mimeTypeParameterMap.isEmpty();
       }
   
       public String get(String name) {
  -        /*@todo implement*/
  -        return null;
  +        return (String) _mimeTypeParameterMap.get(name);
       }
   
       public void set(String name, String value) {
  -        /*@todo implement*/
  +        name = parseToken(name);
  +        value = parseToken(value);
  +        _mimeTypeParameterMap.put(name, value);
       }
   
       public void remove(String name) {
  -        /*@todo implement*/
  +        _mimeTypeParameterMap.remove(name);
       }
   
       public Enumeration getNames() {
  -        /*@todo implement*/
  -        return null;
  +        return Collections.enumeration(_mimeTypeParameterMap.keySet());
       }
   
       public String toString() {
  -        /*@todo implement*/
  -        return null;
  +        StringBuffer buf = new StringBuffer();
  +        for (Enumeration enum = getNames(); enum.hasMoreElements();) {
  +            buf.append(PARAMETER_SEPARATOR);
  +            String name = (String) enum.nextElement();
  +            buf.append(name).append(NAME_VALUE_SEPARATOR).append(get(name));
  +        }
  +        return buf.toString();
  +    }
  +
  +    private String parseToken(String token) {
  +        // TODO it seems to have unauthorized chars
  +        return removeBlank(token);
  +    }
  +
  +    private String removeBlank(String str) {
  +        StringBuffer buf = new StringBuffer();
  +        StringTokenizer tokenizer = new StringTokenizer(str);
  +        while (tokenizer.hasMoreTokens()) {
  +            buf.append(tokenizer.nextToken());
  +        }
  +        return buf.toString();
       }
   }
  
  
  
  1.1                  incubator-geronimo/specs/activation/src/test/javax/activation/MimeTypeParameterListTest.java
  
  Index: MimeTypeParameterListTest.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http:www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Geronimo" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Geronimo", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http:www.apache.org/>.
   *
   * ====================================================================
   */
  package javax.activation;
  
  import junit.framework.TestCase;
  
  
  /**
   *
   * @version $Revision: 1.1 $ $Date: 2003/12/07 16:11:44 $
   */
  public class MimeTypeParameterListTest extends TestCase {
  
  	private String simpleParameterListStr;
  	private String withWhiteSpacesParameterListStr;
  	private String longParameterListStr;
  	private String noNameParameterListStr;
  	private String noValueParameterListStr;
  
  	public MimeTypeParameterListTest(String name) {
  		super(name);
  	}
  
  	protected void setUp() throws Exception {
  		super.setUp();
  		simpleParameterListStr = ";name=value";
  		withWhiteSpacesParameterListStr = "; name= value ;  ";
  		longParameterListStr = "; name1 =value1;;   ; name2= value2;name3= value3;name4  =value4;";
  		noNameParameterListStr = "; = value";
  		noValueParameterListStr = "; name=";
  	}
  
  	public void testEmptyParameterList() {
  		MimeTypeParameterList parameterList = new MimeTypeParameterList();
  		assertEquals(0, parameterList.size());
  	}
  
  	public void testSimpleParameterList() throws MimeTypeParseException {
  		MimeTypeParameterList parameterList = new MimeTypeParameterList(simpleParameterListStr);
  		assertEquals(simpleParameterListStr, parameterList.toString());
  	}
  
  	public void testWhiteSpacesParameterList() throws MimeTypeParseException {
  		MimeTypeParameterList parameterList = new MimeTypeParameterList(withWhiteSpacesParameterListStr);
  		assertEquals(simpleParameterListStr, parameterList.toString());
  	}
  
  	public void testLongParameterList() throws MimeTypeParseException {
  		MimeTypeParameterList parameterList = new MimeTypeParameterList(longParameterListStr);
  		assertEquals(4, parameterList.size());
  		assertEquals("value1", parameterList.get("name1"));
  		assertEquals("value2", parameterList.get("name2"));
  		assertEquals("value3", parameterList.get("name3"));
  		assertEquals("value4", parameterList.get("name4"));
  	}
  
  	public void testNoNameParameterList() {
  		boolean catched = false;
  		try {
  			MimeTypeParameterList parameterList = new MimeTypeParameterList(noNameParameterListStr);
  		} catch (MimeTypeParseException mtpEx) {
  			catched = true;
  		}
  		assertTrue(catched);
  	}
  
  	public void testNoValueParameterList() {
  		boolean catched = false;
  		try {
  			MimeTypeParameterList parameterList = new MimeTypeParameterList(noValueParameterListStr);
  		} catch (MimeTypeParseException mtpEx) {
  			catched = true;
  		}
  		assertTrue(catched);
  	}
  }
  
  
  
  
  1.1                  incubator-geronimo/specs/activation/src/test/javax/activation/MimeTypeTest.java
  
  Index: MimeTypeTest.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http:www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Geronimo" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Geronimo", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http:www.apache.org/>.
   *
   * ====================================================================
   */
  package javax.activation;
  
  import junit.framework.TestCase;
  
  
  /**
   *
   * @version $Revision: 1.1 $ $Date: 2003/12/07 16:11:44 $
   */
  public class MimeTypeTest extends TestCase {
  
  	private final static String DEFAULT_PRIMARY_TYPE = "text";
  	private final static String DEFAULT_SUB_TYPE = "plain";
  
  	private String defaultRawdata;
  	private String primary;
  	private String sub;
  	private String withParamsRawdata;
  
  	public MimeTypeTest(String name) {
  		super(name);
  	}
  
  	public void setUp() throws Exception {
  		super.setUp();
  		defaultRawdata = "text/plain;";
  		primary = "primary";
  		sub = "sub";
  		withParamsRawdata = primary + "/" + sub + "; name1 =value1; name2 = value2;";
  	}
  
  	public void test1DefaultConstructor() {
  		MimeType mimeType = new MimeType();
  		assertEquals(DEFAULT_PRIMARY_TYPE, mimeType.getPrimaryType());
  		assertEquals(DEFAULT_SUB_TYPE, mimeType.getSubType());
  		assertEquals(0, mimeType.getParameters().size());
  		assertTrue(mimeType.match(new MimeType()));
  	}
  
  	public void test2OthersConstructor() throws MimeTypeParseException {
  		MimeType mimeType = new MimeType(defaultRawdata);
  		MimeType defaultMimeType = new MimeType();
  		assertEquals(defaultMimeType.getBaseType(), mimeType.getBaseType());
  		assertTrue(mimeType.match(defaultMimeType));
  
  		mimeType = new MimeType(withParamsRawdata);
  		assertEquals(primary, mimeType.getPrimaryType());
  		assertEquals(sub, mimeType.getSubType());
  		assertEquals(2, mimeType.getParameters().size());
  		assertEquals("value1", mimeType.getParameter("name1"));
  
  		MimeType mimeType2 = new MimeType(primary, sub);
  		assertEquals(primary, mimeType2.getPrimaryType());
  		assertEquals(sub, mimeType2.getSubType());
  		assertTrue(mimeType2.match(mimeType));
  	}
  
  	public void test3MatchMethods() throws MimeTypeParseException {
  		assertTrue(new MimeType().match(new MimeType()));
  		assertTrue(new MimeType().match(defaultRawdata));
  	}
  
  	public void test4ExternalMethods() {
  		// TODO
  	}
  }
  
  
  
  

Mime
View raw message