cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From u..@apache.org
Subject cvs commit: cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/convertor EnumConvertor.java
Date Sun, 16 Nov 2003 10:56:30 GMT
ugo         2003/11/16 02:56:30

  Modified:    src/blocks/woody/java/org/apache/cocoon/woody/datatype/typeimpl
                        EnumType.java
               src/blocks/woody/java/org/apache/cocoon/woody/datatype/convertor
                        EnumConvertor.java
  Added:       src/blocks/woody/test/org/apache/cocoon/woody/datatype
                        Sex.java
               src/blocks/woody/test/org/apache/cocoon/woody/datatype/convertor
                        EnumConvertorTestCase.java
                        EnumConvertorTestCase.conf.xml
  Removed:     src/blocks/woody/test/org/apache/cocoon/woody/datatype
                        EnumConvertorTestCase.conf.xml
               src/blocks/woody/test/org/apache/cocoon/woody/datatype/convertor
                        Sex.java
  Log:
  Added testcase and javadocs for EnumConvertor.
  
  Revision  Changes    Path
  1.1                  cocoon-2.1/src/blocks/woody/test/org/apache/cocoon/woody/datatype/Sex.java
  
  Index: Sex.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, 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 Cocoon" and  "Apache Software Foundation" 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", 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 (INCLU-
   DING, 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 and was  originally created by
   Stefano Mazzocchi  <stefano@apache.org>. For more  information on the Apache
   Software Foundation, please see <http://www.apache.org/>.
  
  */
  package org.apache.cocoon.woody.datatype;
  
  
  /**
   * Description of Sex.
   * @version CVS $Id: Sex.java,v 1.1 2003/11/16 10:56:30 ugo Exp $
   */
  public class Sex {
  
      public static final Sex MALE = new Sex("M");
      public static final Sex FEMALE = new Sex("F");
      private String code;
  
      private Sex(String code) { this.code = code; }
  
      public String toString() {
        // Will probably have some i18n support here
        switch(code.charAt(0)) {
            case 'M' : return this.getClass().getName() + ".MALE";
            case 'F' : return this.getClass().getName() + ".FEMALE";
            default : return "unknown"; // Should never happen
        }
      }
  }
  
  
  
  1.1                  cocoon-2.1/src/blocks/woody/test/org/apache/cocoon/woody/datatype/convertor/EnumConvertorTestCase.java
  
  Index: EnumConvertorTestCase.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, 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 Cocoon" and  "Apache Software Foundation" 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", 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 (INCLU-
   DING, 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 and was  originally created by
   Stefano Mazzocchi  <stefano@apache.org>. For more  information on the Apache
   Software Foundation, please see <http://www.apache.org/>.
  
  */
  package org.apache.cocoon.woody.datatype.convertor;
  
  import java.util.Locale;
  
  import org.apache.cocoon.woody.datatype.Sex;
  
  import junit.framework.TestCase;
  
  /**
   * Test case for the {@link EnumConvertor} class.
   * 
   * @version CVS $Id: EnumConvertorTestCase.java,v 1.1 2003/11/16 10:56:30 ugo Exp $
   */
  public class EnumConvertorTestCase extends TestCase {
  
      public EnumConvertorTestCase(String name) {
          super(name);
      }
  
      /**
       * Test the {@link EnumConvertor#convertFromString(java.lang.String, java.util.Locale,
org.apache.cocoon.woody.datatype.convertor.Convertor.FormatCache)
       * method.
       */
      public void testConvertFromString() {
          EnumConvertor convertor = new EnumConvertor("org.apache.cocoon.woody.datatype.Sex");
          Object sex = convertor.convertFromString
              (Sex.class.getName() + ".FEMALE", Locale.getDefault(), null);
          assertSame("Returned sex must be FEMALE", Sex.FEMALE, sex);
      }
      
      /**
       * Test the {@link EnumConvertor##convertToString(java.lang.Object, java.util.Locale,
org.apache.cocoon.woody.datatype.convertor.Convertor.FormatCache)
       * method.
       */
      public void testConvertToString() {
          EnumConvertor convertor = new EnumConvertor("org.apache.cocoon.woody.datatype.Sex");
          assertEquals("Converted value must match string",
                  Sex.class.getName() + ".MALE",
                  convertor.convertToString
                      (Sex.MALE, Locale.getDefault(), null));
      }
  }
  
  
  
  1.1                  cocoon-2.1/src/blocks/woody/test/org/apache/cocoon/woody/datatype/convertor/EnumConvertorTestCase.conf.xml
  
  Index: EnumConvertorTestCase.conf.xml
  ===================================================================
  <?xml version="1.0" encoding="UTF-8"?>
  
  <wd:field id="sex" xmlns:wd="http://apache.org/cocoon/woody/definition/1.0">
    <wd:label>Sex</wd:label>
    <wd:datatype base="enum">
    	<wd:convertor type="enum">
        <wd:enum>org.apache.cocoon.woody.datatype.Sex</wd:enum>
    	</wd:convertor>
    </wd:datatype>
    <wd:selection-list type="enum" class="org.apache.cocoon.woody.datatype.Sex"/>
  </wd:field>
  
  
  
  1.4       +22 -5     cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/typeimpl/EnumType.java
  
  Index: EnumType.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/typeimpl/EnumType.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- EnumType.java	15 Nov 2003 18:30:03 -0000	1.3
  +++ EnumType.java	16 Nov 2003 10:56:30 -0000	1.4
  @@ -52,10 +52,27 @@
   
   /**
    * A {@link org.apache.cocoon.woody.datatype.Datatype Datatype} implementation for
  - * types implementing Joshua Bloch's <em>{@link 
  - * http://developer.java.sun.com/developer/Books/shiftintojava/page1.html#replaceenums
  - * typesafe enum}</em> pattern.
  - * 
  + * types implementing Joshua Bloch's <a href="http://developer.java.sun.com/developer/Books/shiftintojava/page1.html#replaceenums">
  + * typesafe enum</a> pattern.
  + * <p>See the following code for an example:</p>
  + * <pre>
  + * public class Sex {
  + *
  + *   public static final Sex MALE = new Sex("M");
  + *   public static final Sex FEMALE = new Sex("F");
  + *   private String code;
  + *
  + *   private Sex(String code) { this.code = code; }
  + *
  + *   public String toString() {
  + *     switch(code.charAt(0)) {
  + *         case 'M' : return this.getClass().getName() + ".MALE";
  + *         case 'F' : return this.getClass().getName() + ".FEMALE";
  + *         default : return "unknown"; // Should never happen
  + *     }
  + *   }
  + * }
  + * </pre>
    * @version CVS $Id$
    */
   public class EnumType extends AbstractDatatype {
  
  
  
  1.6       +20 -11    cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/convertor/EnumConvertor.java
  
  Index: EnumConvertor.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/convertor/EnumConvertor.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- EnumConvertor.java	10 Nov 2003 11:26:32 -0000	1.5
  +++ EnumConvertor.java	16 Nov 2003 10:56:30 -0000	1.6
  @@ -58,15 +58,30 @@
   import org.apache.avalon.framework.CascadingRuntimeException;
   
   /**
  - * Description of EnumConvertor.
  + * A {@link org.apache.cocoon.woody.datatype.convertor.Convertor Convertor}
  + * implementation for types implementing Joshua Bloch's
  + * <a href="http://developer.java.sun.com/developer/Books/shiftintojava/page1.html#replaceenums">
  + * typesafe enum</a> pattern.
  + * 
  + * @see org.apache.cocoon.woody.datatype.typeimpl.EnumType
    * @version CVS $Id$
    */
   public class EnumConvertor implements Convertor {
   
  -    private String className;
  +    private Class clazz;
       
  +    /**
  +     * Construct a new EnumConvertor for a class
  +     * @param className The package-qualified name of the class implementing
  +     * the typesafe enum pattern.
  +     */
       public EnumConvertor(String className) {
  -        this.className = className;
  +        try {
  +            clazz = Class.forName(className);
  +        }
  +        catch (ClassNotFoundException e) {
  +            throw new CascadingRuntimeException("Class " + className + " not found", e);
  +        }
       }
       
       /* (non-Javadoc)
  @@ -127,12 +142,6 @@
        * @see org.apache.cocoon.woody.datatype.convertor.Convertor#getTypeClass()
        */
       public Class getTypeClass() {
  -        try {
  -            // FIXME: use the correct class loader.
  -            return Class.forName(className);
  -        }
  -        catch (ClassNotFoundException e) {
  -            throw new CascadingRuntimeException("Class " + className + " not found", e);
  -        }
  +        return clazz;
       }
   }
  
  
  

Mime
View raw message