commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject cvs commit: jakarta-commons-sandbox/convert/src/test/org/apache/commons/convert2/conversion TestIdentityConversionFactory.java TestStringToIntegerConversion.java TestDefaultValueDecorator.java TestNumberToIntegerConversionFactory.java TestClassToStringConversion.java TestObjectToStringConversionFactory.java TestTimeZoneToStringConversionFactory.java PackageTestSuite.java
Date Sat, 20 Mar 2004 18:44:12 GMT
scolebourne    2004/03/20 10:44:11

  Modified:    convert/src/java/org/apache/commons/convert2 Conversion.java
                        Converter.java
               convert/src/java/org/apache/commons/convert2/conversion
                        ObjectToStringConversionFactory.java
                        AbstractConversion.java
                        ClassToStringConversion.java
                        TimeZoneToStringConversionFactory.java
               convert/src/test/org/apache/commons/convert2/conversion
                        TestObjectToStringConversionFactory.java
                        TestTimeZoneToStringConversionFactory.java
                        PackageTestSuite.java
  Added:       convert/src/java/org/apache/commons/convert2/conversion
                        AbstractConversionFactory.java
                        DefaultValueDecorator.java
                        IdentityConversionFactory.java
                        NumberToIntegerConversionFactory.java
                        StringToIntegerConversion.java
               convert/src/test/org/apache/commons/convert2/conversion
                        TestIdentityConversionFactory.java
                        TestStringToIntegerConversion.java
                        TestDefaultValueDecorator.java
                        TestNumberToIntegerConversionFactory.java
                        TestClassToStringConversion.java
  Removed:     convert/src/java/org/apache/commons/convert2/conversion
                        DefaultValueConversionDecorator.java
                        DefaultValueConversionFactoryDecorator.java
  Log:
  Update conversion code to include new ideas
  
  Revision  Changes    Path
  1.3       +4 -3      jakarta-commons-sandbox/convert/src/java/org/apache/commons/convert2/Conversion.java
  
  Index: Conversion.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/convert/src/java/org/apache/commons/convert2/Conversion.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Conversion.java	17 Mar 2004 23:37:29 -0000	1.2
  +++ Conversion.java	20 Mar 2004 18:44:11 -0000	1.3
  @@ -33,9 +33,10 @@
        *
        * @param value  the value to be converted, read only, may be null
        * @param converter  the converter being used, not null
  -     * @throws ConversionException (runtime) if conversion fails
  +     * @throws Exception if conversion fails, use ConversionException if creating
  +     *  a new exception, otherwise just allow exceptions to be thrown
        */
  -    public Object convert(Object value, Converter converter);
  +    public Object convert(Object value, Converter converter) throws Exception;
   
       /**
        * The type to convert from.
  
  
  
  1.3       +49 -56    jakarta-commons-sandbox/convert/src/java/org/apache/commons/convert2/Converter.java
  
  Index: Converter.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/convert/src/java/org/apache/commons/convert2/Converter.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Converter.java	7 Mar 2004 01:16:28 -0000	1.2
  +++ Converter.java	20 Mar 2004 18:44:11 -0000	1.3
  @@ -31,10 +31,10 @@
    * @since 1.0
    */
   public class Converter {
  -    
  +
       /* The converter registry */
  -    protected final ConversionRegistry registry;
  -    
  +    private final ConversionRegistry registry;
  +
       /**
        * Constructs a new instance of the Converter useful to create a set
        * of conversions separate from the default set.
  @@ -44,7 +44,7 @@
       public Converter() {
           this(false);
       }
  -    
  +
       /**
        * Constructs a new instance of the Converter useful to create a set
        * of conversions separate from the default set.
  @@ -58,7 +58,7 @@
               registry.addDefaultConversions();
           }
       }
  -    
  +
       /**
        * Factory method for subclasses to change the registry implementation.
        * 
  @@ -79,12 +79,7 @@
        * @throws ConversionException (runtime) if conversion fails
        */
       public Object convert(Object value, Class fromClass, Class toClass) {
  -        Class valueClass = (value == null ? fromClass : value.getClass());
  -        Conversion conv = getRegistry().getConversion(value, valueClass, toClass);
  -        if (conv == null) {
  -            throw new ConversionException("No Converter found to convert " + valueClass + " to " +toClass);
  -        }
  -        return conv.convert(value, this);
  +        return doConversion(value, fromClass, toClass);
       }
   
       /**
  @@ -96,7 +91,7 @@
        * @throws ConversionException (runtime) if conversion fails
        */
       public Object convert(Object value, Class toClass) {
  -        return convert(value, null, toClass);
  +        return doConversion(value, null, toClass);
       }
   
       /**
  @@ -107,7 +102,7 @@
        * @throws ConversionException (runtime) if conversion fails
        */
       public String convertToString(Object value, Class fromClass) {
  -        return (String) convert(value, fromClass, String.class);
  +        return (String) doConversion(value, fromClass, String.class);
       }
   
       /**
  @@ -117,50 +112,48 @@
        * @throws ConversionException (runtime) if conversion fails
        */
       public String convertToString(Object value) {
  -        return (String) convert(value, null, String.class);
  +        return (String) doConversion(value, null, String.class);
       }
   
  -//    /**
  -//     * Convert the specified input object into a primitive <code>int</code>.
  -//     *
  -//     * @param value  the input value to be converted
  -//     * @throws ConversionException (runtime) if conversion fails
  -//     */
  -//    public int convertToInt(Object value) {
  -//        return 0;
  -//    }
  -//
  -//    /**
  -//     * Convert the specified input object into a primitive <code>int</code>.
  -//     *
  -//     * @param value  the input value to be converted
  -//     * @param fromClass  the class to convert from, useful if null passed in
  -//     * @throws ConversionException (runtime) if conversion fails
  -//     */
  -//    public int convertToInt(Object value, Class fromClass) {
  -//        return 0;
  -//    }
  -//
  -//    /**
  -//     * Convert the specified input object into a primitive <code>boolean</code>.
  -//     *
  -//     * @param value  the input value to be converted
  -//     * @throws ConversionException (runtime) if conversion fails
  -//     */
  -//    public boolean convertToBoolean(Object value) {
  -//        return false;
  -//    }
  -//
  -//    /**
  -//     * Convert the specified input object into a primitive <code>boolean</code>.
  -//     *
  -//     * @param value  the input value to be converted
  -//     * @param fromClass  the class to convert from, useful if null passed in
  -//     * @throws ConversionException (runtime) if conversion fails
  -//     */
  -//    public boolean convertToBoolean(Object value, Class fromClass) {
  -//        return false;
  -//    }
  +    //-----------------------------------------------------------------------
  +    /**
  +     * Convert the specified input object into an output object of another type.
  +     * <p>
  +     * This implementation uses {{@link #doConversion(Conversion, Object)}.
  +     *
  +     * @param value  the input value to be converted
  +     * @param fromClass  the class to convert from, useful if null passed in
  +     * @param toClass  the class to convert to
  +     * @throws ConversionException (runtime) if conversion fails
  +     */
  +    protected Object doConversion(Object value, Class fromClass, Class toClass) {
  +        Class valueClass = (value == null ? fromClass : value.getClass());
  +        Conversion conv = getRegistry().getConversion(value, valueClass, toClass);
  +        if (conv == null) {
  +            throw new ConversionException("No Converter found to convert " + valueClass + " to " + toClass);
  +        }
  +        return doConversion(conv, value);
  +    }
  +
  +    /**
  +     * Convert the specified input object into an output object using the conversion.
  +     * <p>
  +     * This implementation catches exceptions and wraps them in a <code>ConversionException</code>.
  +     * 
  +     * @param conversion  the conversion to use
  +     * @param value  the value to convert
  +     * @return the converted value
  +     */
  +    protected Object doConversion(Conversion conversion, Object value) {
  +        try {
  +            return conversion.convert(value, this);
  +
  +        } catch (ConversionException ex) {
  +            throw ex;
  +        } catch (Exception ex) {
  +            throw new ConversionException(conversion.toString(), ex);
  +        }
  +    }
   
       //-----------------------------------------------------------------------
       /**
  
  
  
  1.3       +15 -84    jakarta-commons-sandbox/convert/src/java/org/apache/commons/convert2/conversion/ObjectToStringConversionFactory.java
  
  Index: ObjectToStringConversionFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/convert/src/java/org/apache/commons/convert2/conversion/ObjectToStringConversionFactory.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ObjectToStringConversionFactory.java	17 Mar 2004 23:40:30 -0000	1.2
  +++ ObjectToStringConversionFactory.java	20 Mar 2004 18:44:11 -0000	1.3
  @@ -15,7 +15,6 @@
    */
   package org.apache.commons.convert2.conversion;
   
  -import org.apache.commons.convert2.Conversion;
   import org.apache.commons.convert2.ConversionFactory;
   import org.apache.commons.convert2.Converter;
   
  @@ -26,7 +25,7 @@
    * @version $Id$
    * @since 1.0
    */
  -public class ObjectToStringConversionFactory implements ConversionFactory {
  +public class ObjectToStringConversionFactory extends AbstractConversionFactory {
   
       /** Singleton instance of this factory */
       public static final ConversionFactory INSTANCE = new ObjectToStringConversionFactory();
  @@ -34,98 +33,30 @@
       /**
        * Restricted constructor.
        */
  -    private ObjectToStringConversionFactory() {
  -        super();
  +    protected ObjectToStringConversionFactory() {
  +        super(Object.class, STRING_CLASS);
       }
   
       //-----------------------------------------------------------------------
       /**
  -     * Checks if this factory supports the required conversion returning a
  -     * percentage match.
  -     * <p>
  -     * This implementation returns <code>20</code> if the <code>toType</code> is
  -     * <code>String</code>.
  +     * Returns a low match percent to allow other conversions to replace this one.
        * 
  -     * @param value  the value to be converted, read only, may be null
  -     * @param fromType  the type to convert from, may be null
  -     * @param toType  th type to convert to, may be null
  -     * @return 20 if toType is String
  +     * @return 20
        */
  -    public int getMatchPercent(Object value, Class fromType, Class toType) {
  -        if (toType == String.class) {
  -            return 20;
  -        }
  -        return 0;
  +    protected int getMatchPercent() {
  +        return 20;
       }
   
       /**
  -     * Create a conversion object for the conversion.
  -     * <p>
  -     * The returned conversion must not store the value. The conversion object
  -     * will be used repeatedly for all future conversions between these two types
  -     * without further reference to the factory.
  +     * Convert the specified input object into an output object
  +     * of the another type.
        *
  -     * @param value  the value to be converted, read only, may be null
  -     * @param fromType  the type to convert from, may be null
  -     * @param toType  th type to convert to, may be null
  -     * @return a Conversion object for repeatedly performing conversions
  +     * @param value  the input value to be converted, pre-checked to not be null
  +     * @param converter  the converter being used, not null
  +     * @return the converted value
        */
  -    public Conversion getInstance(Object value, Class fromType, Class toType) {
  -        return new ObjectToStringConversion(fromType);
  -    }
  -
  -    //-----------------------------------------------------------------------
  -    /**
  -     * Conversion implementation for Number to String converions.
  -     */
  -    class ObjectToStringConversion extends AbstractConversion {
  -
  -        /** The type to convert from */
  -        private final Class fromType;
  -
  -        /**
  -         * Constructs a Conversion for a specific Number subclass.
  -         * 
  -         * @param fromType  the type to convert from
  -         */
  -        ObjectToStringConversion(Class fromType) {
  -            super();
  -            this.fromType = fromType;
  -        }
  -
  -        /**
  -         * Convert the specified input object into an output object
  -         * of the another type.
  -         *
  -         * @param value  the input value to be converted, may be null
  -         * @param converter  the converter being used, not null
  -         * @throws ConversionException (runtime) if conversion fails
  -         */
  -        public Object convert(Object value, Converter converter) {
  -            if (value == null) {
  -                return null;
  -            }
  -            return value.toString();
  -        }
  -
  -        /**
  -         * The type to convert from.
  -         *
  -         * @return the Class object representing the class to convert to
  -         */
  -        public Class getFromType() {
  -            return fromType;
  -        }
  -
  -        /**
  -         * The type to convert to.
  -         *
  -         * @return the Class object representing the class to convert from
  -         */
  -        public Class getToType() {
  -            return String.class;
  -        }
  -
  +    protected Object convertValue(Object value, Converter converter) {
  +        return value.toString();
       }
   
   }
  
  
  
  1.2       +77 -5     jakarta-commons-sandbox/convert/src/java/org/apache/commons/convert2/conversion/AbstractConversion.java
  
  Index: AbstractConversion.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/convert/src/java/org/apache/commons/convert2/conversion/AbstractConversion.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- AbstractConversion.java	17 Mar 2004 23:40:30 -0000	1.1
  +++ AbstractConversion.java	20 Mar 2004 18:44:11 -0000	1.2
  @@ -16,21 +16,93 @@
   package org.apache.commons.convert2.conversion;
   
   import org.apache.commons.convert2.Conversion;
  +import org.apache.commons.convert2.Converter;
   
   /**
    * Abstract base class for conversion objects that provides basic features.
  + * <p>
  + * Most Conversion implementations will extend this class. It provides convenient
  + * implementations of the methods of the interface, and simplifies null-handling
  + * in the subclass.
  + * <p>
  + * To implement a conversion from a single class, simply extend this class and
  + * implement the {@link #convert(Object, Converter)} method. To implement a 
  + * conversion from a class and its subclasses, use {@link AbstractConversionFactory}.
    *
    * @author Stephen Colebourne
    * @version $Id$
    * @since 1.0
    */
   public abstract class AbstractConversion implements Conversion {
  -    
  +
  +    /** Useful constant for subclass constructors */
  +    protected static final Class STRING_CLASS = String.class;
  +
  +    /** The type to convert from */
  +    private final Class fromType;
  +    /** The type to convert to */
  +    private final Class toType;
  +
       /**
  -     * Constructor.
  +     * Constructor that stores the from and to types.
  +     * 
  +     * @param fromType  the type to convert from
  +     * @param toType  the type to convert to
        */
  -    protected AbstractConversion() {
  +    protected AbstractConversion(Class fromType, Class toType) {
           super();
  +        this.fromType = fromType;
  +        this.toType = toType;
  +    }
  +
  +    //-----------------------------------------------------------------------
  +    /**
  +     * Converts an object from one type to another.
  +     * <p>
  +     * This implementation delegates to <code>convertValue</code> after handling null.
  +     * If the null-safe behaviour is undesired, override this method.
  +     *
  +     * @param value  the input value to be converted, may be null
  +     * @param converter  the converter being used, not null
  +     * @throws Exception if conversion fails, use ConversionException if creating
  +     *  a new exception, otherwise just allow exceptions to be thrown
  +     */
  +    public Object convert(Object value, Converter converter) throws Exception {
  +        if (value == null) {
  +            return null;
  +        }
  +        return convertValue(value, converter);
  +    }
  +
  +    /**
  +     * Convert the specified non-null value to another type.
  +     * 
  +     * @param value  the input value to be converted, pre-checked to not be null
  +     * @param converter  the converter being used, not null
  +     * @throws Exception if conversion fails, use ConversionException if creating
  +     *  a new exception, otherwise just allow exceptions to be thrown
  +     */
  +    protected Object convertValue(Object value, Converter converter) {
  +        throw new UnsupportedOperationException("Not implemented");
  +    }
  +
  +    //-----------------------------------------------------------------------
  +    /**
  +     * The type to convert from.
  +     *
  +     * @return the Class object representing the class to convert to
  +     */
  +    public Class getFromType() {
  +        return fromType;
  +    }
  +
  +    /**
  +     * The type to convert to.
  +     *
  +     * @return the Class object representing the class to convert from
  +     */
  +    public Class getToType() {
  +        return toType;
       }
   
       //-----------------------------------------------------------------------
  @@ -50,7 +122,7 @@
        * 
        * @param cls  the class to convert
        * @return the class name
  -     */    
  +     */
       private String convertClassToName(Class cls) {
           if (cls == null) {
               return "null";
  
  
  
  1.3       +10 -32    jakarta-commons-sandbox/convert/src/java/org/apache/commons/convert2/conversion/ClassToStringConversion.java
  
  Index: ClassToStringConversion.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/convert/src/java/org/apache/commons/convert2/conversion/ClassToStringConversion.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ClassToStringConversion.java	17 Mar 2004 23:40:30 -0000	1.2
  +++ ClassToStringConversion.java	20 Mar 2004 18:44:11 -0000	1.3
  @@ -31,44 +31,22 @@
       public static final Conversion INSTANCE = new ClassToStringConversion();
   
       /**
  -     * Constructor, setting <code>null</code> as the default value.
  +     * Constructor.
        */
  -    public ClassToStringConversion() {
  -        super();
  +    protected ClassToStringConversion() {
  +        super(Class.class, STRING_CLASS);
       }
   
       //-----------------------------------------------------------------------
       /**
  -     * Convert from <code>Class</code> to <code>String</code> using <code>getName()</code>.
  -     *
  -     * @param value  the input value to be converted, may be null
  +     * Convert the non-null Class to a String using <code>getName()</code>.
  +     * 
  +     * @param value  the input value to be converted, pre-checked to not be null
        * @param converter  the converter being used, not null
  -     * @throws ConversionException (runtime) if conversion fails
  +     * @return the converted value
        */
  -    public Object convert(Object value, Converter converter) {
  -        if (value == null) {
  -            return null;
  -        }
  -        Class cls = (Class) value;
  -        return cls.getName();
  -    }
  -
  -    /**
  -     * The type to convert from.
  -     *
  -     * @return the Class object representing the class to convert to
  -     */
  -    public Class getFromType() {
  -        return Class.class;
  -    }
  -
  -    /**
  -     * The type to convert to.
  -     *
  -     * @return the Class object representing the class to convert from
  -     */
  -    public Class getToType() {
  -        return String.class;
  +    protected Object convertValue(Object value, Converter converter) {
  +        return ((Class) value).getName();
       }
   
   }
  
  
  
  1.3       +10 -95    jakarta-commons-sandbox/convert/src/java/org/apache/commons/convert2/conversion/TimeZoneToStringConversionFactory.java
  
  Index: TimeZoneToStringConversionFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/convert/src/java/org/apache/commons/convert2/conversion/TimeZoneToStringConversionFactory.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- TimeZoneToStringConversionFactory.java	17 Mar 2004 23:40:30 -0000	1.2
  +++ TimeZoneToStringConversionFactory.java	20 Mar 2004 18:44:11 -0000	1.3
  @@ -17,7 +17,6 @@
   
   import java.util.TimeZone;
   
  -import org.apache.commons.convert2.Conversion;
   import org.apache.commons.convert2.ConversionFactory;
   import org.apache.commons.convert2.Converter;
   
  @@ -28,7 +27,7 @@
    * @version $Id$
    * @since 1.0
    */
  -public class TimeZoneToStringConversionFactory implements ConversionFactory {
  +public class TimeZoneToStringConversionFactory extends AbstractConversionFactory {
   
       /** Singleton instance of this factory */
       public static final ConversionFactory INSTANCE = new TimeZoneToStringConversionFactory();
  @@ -36,104 +35,20 @@
       /**
        * Restricted constructor.
        */
  -    private TimeZoneToStringConversionFactory() {
  -        super();
  +    protected TimeZoneToStringConversionFactory() {
  +        super(TimeZone.class, STRING_CLASS);
       }
   
       //-----------------------------------------------------------------------
       /**
  -     * Checks if this factory supports the required conversion returning a
  -     * percentage match.
  -     * <p>
  -     * This implementation returns <code>60</code> if the 
  -     * <code>fromType</code> is TimeZone or subclass and the
  -     * <code>toType</code> is <code>String</code>.
  +     * Convert the non-null TimeZone to a String using <code>getID()</code>.
        * 
  -     * @param value  the value to be converted, read only, may be null
  -     * @param fromType  the type to convert from, may be null
  -     * @param toType  th type to convert to, may be null
  -     * @return 60 if fromType is TimeZone or subclass and toType is String
  +     * @param value  the input value to be converted, pre-checked to not be null
  +     * @param converter  the converter being used, not null
  +     * @return the converted value
        */
  -    public int getMatchPercent(Object value, Class fromType, Class toType) {
  -        if (toType == String.class) {
  -            if (value != null && value instanceof TimeZone) {
  -                return 60;
  -            } else if (TimeZone.class.isAssignableFrom(fromType)) {
  -                return 60;
  -            }
  -        }
  -        return 0;
  -    }
  -
  -    /**
  -     * Create a conversion object for the conversion.
  -     * <p>
  -     * The returned conversion must not store the value. The conversion object
  -     * will be used repeatedly for all future conversions between these two types
  -     * without further reference to the factory.
  -     *
  -     * @param value  the value to be converted, read only, may be null
  -     * @param fromType  the type to convert from, may be null
  -     * @param toType  th type to convert to, may be null
  -     * @return a Conversion object for repeatedly performing conversions
  -     */
  -    public Conversion getInstance(Object value, Class fromType, Class toType) {
  -        return new TimeZoneToStringConversion(fromType);
  -    }
  -
  -    //-----------------------------------------------------------------------
  -    /**
  -     * Conversion implementation for Number to String converions.
  -     */
  -    class TimeZoneToStringConversion extends AbstractConversion {
  -
  -        /** The type to convert from */
  -        private final Class fromType;
  -
  -        /**
  -         * Constructs a Conversion for a specific Number subclass.
  -         * 
  -         * @param fromType  the type to convert from
  -         */
  -        TimeZoneToStringConversion(Class fromType) {
  -            super();
  -            this.fromType = fromType;
  -        }
  -
  -        /**
  -         * Convert the specified input object into an output object
  -         * of the another type.
  -         *
  -         * @param value  the input value to be converted, may be null
  -         * @param converter  the converter being used, not null
  -         * @throws ConversionException (runtime) if conversion fails
  -         */
  -        public Object convert(Object value, Converter converter) {
  -            if (value == null) {
  -                return null;
  -            }
  -            TimeZone zone = (TimeZone) value;
  -            return zone.getID();
  -        }
  -
  -        /**
  -         * The type to convert from.
  -         *
  -         * @return the Class object representing the class to convert to
  -         */
  -        public Class getFromType() {
  -            return fromType;
  -        }
  -
  -        /**
  -         * The type to convert to.
  -         *
  -         * @return the Class object representing the class to convert from
  -         */
  -        public Class getToType() {
  -            return String.class;
  -        }
  -
  +    protected Object convertValue(Object value, Converter converter) {
  +        return ((TimeZone) value).getID();
       }
   
   }
  
  
  
  1.1                  jakarta-commons-sandbox/convert/src/java/org/apache/commons/convert2/conversion/AbstractConversionFactory.java
  
  Index: AbstractConversionFactory.java
  ===================================================================
  /*
   *  Copyright 2004 The Apache Software Foundation
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   *  Unless required by applicable law or agreed to in writing, software
   *  distributed under the License is distributed on an "AS IS" BASIS,
   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *  See the License for the specific language governing permissions and
   *  limitations under the License.
   */
  package org.apache.commons.convert2.conversion;
  
  import org.apache.commons.convert2.Conversion;
  import org.apache.commons.convert2.ConversionFactory;
  import org.apache.commons.convert2.Converter;
  
  /**
   * Abstract base class for creating conversion factories.
   * <p>
   * This class is especially suited for creating factories as it minimises the effort
   * required in the subclass.
   * <p>
   * To implement a conversion from a class and all of it subclasses, simply extend
   * this class and implement the {@link #convertValue(Object, Converter)} method.
   *
   * @author Stephen Colebourne
   * @author Oliver Heger
   * @version $Id: AbstractConversionFactory.java,v 1.1 2004/03/20 18:44:11 scolebourne Exp $
   * @since 1.0
   */
  public abstract class AbstractConversionFactory implements ConversionFactory {
  
      /** Useful constant for subclass constructors */
      protected static final Class STRING_CLASS = String.class;
  
      /** The base type to convert from */
      private final Class baseFromType;
      /** The type to convert to */
      private final Class toType;
  
      /**
       * Restricted constructor.
       */
      protected AbstractConversionFactory(Class baseFromType, Class toType) {
          super();
          this.baseFromType = baseFromType;
          this.toType = toType;
      }
  
      //-----------------------------------------------------------------------
      /**
       * Checks if this factory matches the request conversion.
       * <p>
       * This implementation returns {@link #getMatchPercent()} if the specified
       * <code>fromType</code> is the same as, or a subclass of, the stored
       * <code>baseFromType</code> and the specified <code>toType</code> matches
       * the stored <code>toType</code>.
       * 
       * @param value  the value to be converted, read only, may be null
       * @param fromType  the type to convert from, may be null
       * @param toType  th type to convert to, may be null
       * @return 60 if baseFromType or subclass matches fromType and toType matches
       */
      public int getMatchPercent(Object value, Class fromType, Class toType) {
          if (toType == getToType() && getBaseFromType().isAssignableFrom(fromType)) {
              return getMatchPercent();
          }
          return 0;
      }
  
      /**
       * Gets the percentage to return if a match occurs.
       * <p>
       * This implementation returns <code>60</code>.
       * Subclasses may wish to override and return a different value.
       * 
       * @return 60
       */
      protected int getMatchPercent() {
          return 60;
      }
  
      /**
       * Create a new conversion object for the specified from and to types.
       * <p>
       * This implementation returns a <code>Conversion</code> instance that
       * refers back to this class for its implementation of the actual conversion
       * via {@link #convertValue(Object, Converter)}.
       *
       * @param value  the value to be converted, read only, may be null
       * @param fromType  the type to convert from, may be null
       * @param toType  the type to convert to, may be null
       * @return a Conversion object for repeatedly performing conversions
       */
      public Conversion getInstance(Object value, Class fromType, Class toType) {
          return new FactoryConversion(fromType, toType, this);
      }
  
      /**
       * Convert the non-null value to another type.
       * <p>
       * This method is implemented by subclases to perform the actual conversion.
       * 
       * @param value  the input value to be converted, pre-checked to not be null
       * @param converter  the converter being used, not null
       * @throws Exception if conversion fails, use ConversionException if creating
       *  a new exception, otherwise just allow exceptions to be thrown
       */
      protected abstract Object convertValue(Object value, Converter converter);
  
      //-----------------------------------------------------------------------
      /**
       * The base type to convert from.
       * This type and its subclasses will be matched by this factory.
       *
       * @return the Class object representing the class to convert to
       */
      public Class getBaseFromType() {
          return baseFromType;
      }
  
      /**
       * The type to convert to.
       *
       * @return the Class object representing the class to convert from
       */
      public Class getToType() {
          return toType;
      }
  
      //-----------------------------------------------------------------------
      /**
       * Gets a suitable debugging string.
       * 
       * @return a debugging string
       */
      public String toString() {
          String from = convertClassToName(getBaseFromType());
          String to = convertClassToName(getToType());
          return "ConversionFactory[" + from + "->" + to + "]";
      }
  
      /**
       * Converts a class to a string name for debugging.
       * 
       * @param cls  the class to convert
       * @return the class name
       */
      private String convertClassToName(Class cls) {
          if (cls == null) {
              return "null";
          }
          String str = cls.getName();
          int pos = str.lastIndexOf('.');
          if (str.substring(0, pos).equals("java.lang")) {
              str = str.substring(pos + 1);
          }
          if (str.substring(0, pos).equals("java.util")) {
              str = str.substring(pos + 1);
          }
          return str;
      }
  
      //-----------------------------------------------------------------------
      /**
       * Generic conversion implementation that delegates back to the factory.
       */
      class FactoryConversion extends AbstractConversion {
  
          /** The factory instance that has the actual conversion code */
          private final AbstractConversionFactory factory;
  
          /**
           * Constructs a Conversion.
           * 
           * @param fromType  the type to convert from
           * @param toType  the type to convert to
           * @param factory  the factory instance that actually performs the conversion
           */
          FactoryConversion(Class fromType, Class toType, AbstractConversionFactory factory) {
              super(fromType, toType);
              this.factory = factory;
          }
  
          /**
           * Convert the non-null value to another type.
           * <p>
           * This implementation delegates to the factory method
           * {@link AbstractConversionFactory#convertValue(Object, Converter)}.
           * 
           * @param value  the input value to be converted, pre-checked to not be null
           * @param converter  the converter being used, not null
           * @throws Exception if conversion fails, use ConversionException if creating
           *  a new exception, otherwise just allow exceptions to be thrown
           */
          protected Object convertValue(Object value, Converter converter) {
              return factory.convertValue(value, converter);
          }
      }
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/convert/src/java/org/apache/commons/convert2/conversion/DefaultValueDecorator.java
  
  Index: DefaultValueDecorator.java
  ===================================================================
  /*
   *  Copyright 2004 The Apache Software Foundation
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   *  Unless required by applicable law or agreed to in writing, software
   *  distributed under the License is distributed on an "AS IS" BASIS,
   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *  See the License for the specific language governing permissions and
   *  limitations under the License.
   */
  package org.apache.commons.convert2.conversion;
  
  import org.apache.commons.convert2.Conversion;
  import org.apache.commons.convert2.ConversionFactory;
  import org.apache.commons.convert2.Converter;
  
  /**
   * A utility class for decorating conversions and factories with default value behaviour.
   * <p>
   * Each public static method on this class will decorate the input conversion/factory
   * with additional behaviour. Depending on the method it may return a default value in
   * the case of an exception or null value
   * <p>
   * To use this facility, create an instance of the conversion/factory as normal.
   * Then pass it to one of the static methods on this class.
   * Finally add it as per normal to the <code>ConverterRegistry</code>.
   * 
   * @author Stephen Colebourne
   * @author Oliver Heger
   * @version $Id: DefaultValueDecorator.java,v 1.1 2004/03/20 18:44:11 scolebourne Exp $
   * @since 1.0
   */
  public class DefaultValueDecorator {
  
      /**
       * Restricted constructor.
       */
      protected DefaultValueDecorator() {
      }
  
      //-----------------------------------------------------------------------
      /**
       * Creates a Conversion that decorates the original and adds behaviour to
       * return a default value when the conversion results in null.
       * 
       * @param conversion  the conversion to decorate, not null
       * @param defaultValue  the value to return if conversion result is null
       * @return the decorated conversion
       */
      public static Conversion defaultNullResult(Conversion conversion, Object defaultValue) {
          checkNotNull(conversion);
          return new DefaultNullResultConversion(conversion, defaultValue);
      }
  
      /**
       * Creates a Conversion that decorates the original and adds behaviour to
       * return a default value when the conversion input is null.
       * 
       * @param conversion  the conversion to decorate, not null
       * @param defaultValue  the value to return if conversion input is null
       * @return the decorated conversion
       */
      public static Conversion defaultNullInput(Conversion conversion, Object defaultValue) {
          checkNotNull(conversion);
          return new DefaultNullInputConversion(conversion, defaultValue);
      }
  
      /**
       * Creates a Conversion that decorates the original and adds behaviour to
       * return a default value when the conversion throws an exception.
       * 
       * @param conversion  the conversion to decorate, not null
       * @param defaultValue  the value to return if conversion throws an exception
       * @return the decorated conversion
       */
      public static Conversion defaultException(Conversion conversion, Object defaultValue) {
          checkNotNull(conversion);
          return new DefaultExceptionConversion(conversion, defaultValue);
      }
  
      /**
       * Creates a Conversion that decorates the original and adds behaviour to
       * return a default value for null input, null result and when an exception occurs.
       * 
       * @param conversion  the conversion to decorate, not null
       * @param defaultValue  the default value to return
       * @return the decorated conversion
       */
      public static Conversion defaultNullAndException(Conversion conversion, Object defaultValue) {
          checkNotNull(conversion);
          return new DefaultNullExceptionConversion(conversion, defaultValue);
      }
  
      //-----------------------------------------------------------------------
      /**
       * Creates a ConversionFactory that decorates the original and adds behaviour to
       * return a default value when the conversion results in null.
       * 
       * @param factory  the factory to decorate, not null
       * @param defaultValue  the value to return if conversion result is null
       * @return the decorated factory
       */
      public static ConversionFactory defaultNullResult(ConversionFactory factory, Object defaultValue) {
          checkNotNull(factory);
          return new DefaultNullResultConversionFactory(factory, defaultValue);
      }
  
      /**
       * Creates a ConversionFactory that decorates the original and adds behaviour to
       * return a default value when the conversion input is null.
       * 
       * @param factory  the factory to decorate, not null
       * @param defaultValue  the value to return if conversion input is null
       * @return the decorated factory
       */
      public static ConversionFactory defaultNullInput(ConversionFactory factory, Object defaultValue) {
          checkNotNull(factory);
          return new DefaultNullInputConversionFactory(factory, defaultValue);
      }
  
      /**
       * Creates a ConversionFactory that decorates the original and adds behaviour to
       * return a default value when the conversion throws an exception.
       * 
       * @param factory  the factory to decorate, not null
       * @param defaultValue  the value to return if conversion throws an exception
       * @return the decorated factory
       */
      public static ConversionFactory defaultException(ConversionFactory factory, Object defaultValue) {
          checkNotNull(factory);
          return new DefaultExceptionConversionFactory(factory, defaultValue);
      }
  
      /**
       * Creates a ConversionFactory that decorates the original and adds behaviour to
       * return a default value for null input, null result and when an exception occurs.
       * 
       * @param factory  the factory to decorate, not null
       * @param defaultValue  the default value to return
       * @return the decorated factory
       */
      public static ConversionFactory defaultNullAndException(ConversionFactory factory, Object defaultValue) {
          checkNotNull(factory);
          return new DefaultNullExceptionConversionFactory(factory, defaultValue);
      }
  
      //-----------------------------------------------------------------------    
      /**
       * Checks whether the input conversion is null and throws an exception.
       * 
       * @param conversion  the conversion to check
       */
      private static void checkNotNull(Conversion conversion) {
          if (conversion == null) {
              throw new IllegalArgumentException("Conversion must not be null");
          }
      }
  
      /**
       * Checks whether the input factory is null and throws an exception.
       * 
       * @param factory  the factory to check
       */
      private static void checkNotNull(ConversionFactory factory) {
          if (factory == null) {
              throw new IllegalArgumentException("ConversionFactory must not be null");
          }
      }
  
      //-----------------------------------------------------------------------
      /**
       * Abstract base class for default value decorators.
       */
      static abstract class AbstractDefaultConversion implements Conversion {
  
          /** The decorated conversion */
          private final Conversion conversion;
          /** The default value to return */
          private final Object defaultValue;
  
          /**
           * Constructor.
           * 
           * @param conversion  the conversion to decorate, not null
           * @param defaultValue  the default value to return
           */
          AbstractDefaultConversion(Conversion conversion, Object defaultValue) {
              super();
              this.conversion = conversion;
              this.defaultValue = defaultValue;
          }
  
          /**
           * Returns the type to convert from.
           * 
           * @return the Class object representing the class to convert from
           */
          public Class getFromType() {
              return getDecoratedConversion().getFromType();
          }
  
          /**
           * Returns the type to convert to.
           * 
           * @return the Class object representing the class to convert to
           */
          public Class getToType() {
              return getDecoratedConversion().getToType();
          }
  
          /**
           * Gets the conversion being decorated.
           * 
           * @return the decorated conversion
           */
          public Conversion getDecoratedConversion() {
              return conversion;
          }
  
          /**
           * Gets the default value to be returned.
           * 
           * @return the default value
           */
          public Object getDefaultValue() {
              return defaultValue;
          }
  
          /**
           * Gets a suitable debugging string.
           * 
           * @return a debugging string
           */
          public String toString() {
              String str = getDecoratedConversion().toString();
              return str.substring(0, str.length() - 1) + ",default=" + getDefaultValue() + "]";
          }
      }
  
      //-----------------------------------------------------------------------
      /**
       * Implement the default null result behaviour.
       */
      static class DefaultNullResultConversion extends AbstractDefaultConversion {
  
          /**
           * Constructs a null result conversion.
           * 
           * @param conversion  the conversion to decorate, not null
           * @param defaultValue  the value to return if conversion result is null
           */
          DefaultNullResultConversion(Conversion conversion, Object defaultValue) {
              super(conversion, defaultValue);
          }
  
          /**
           * Performs the conversion by delegating to the original conversion and
           * then handling the default value.
           * 
           * @param value  the object to be converted
           * @param converter  the associated converter
           * @return the result of the conversion
           */
          public Object convert(Object value, Converter converter) throws Exception {
              Object result = getDecoratedConversion().convert(value, converter);
              if (result == null) {
                  return getDefaultValue();
              }
              return result;
          }
      }
  
      //-----------------------------------------------------------------------
      /**
       * Implement the default null input behaviour.
       */
      static class DefaultNullInputConversion extends AbstractDefaultConversion {
  
          /**
           * Constructs a null input conversion.
           * 
           * @param conversion  the conversion to decorate, not null
           * @param defaultValue  the value to return if conversion input is null
           */
          DefaultNullInputConversion(Conversion conversion, Object defaultValue) {
              super(conversion, defaultValue);
          }
  
          /**
           * Performs the conversion by checking for null, then delegating to the
           * original conversion.
           * 
           * @param value  the object to be converted
           * @param converter  the associated converter
           * @return the result of the conversion
           */
          public Object convert(Object value, Converter converter) throws Exception {
              if (value == null) {
                  return getDefaultValue();
              }
              return getDecoratedConversion().convert(value, converter);
          }
      }
  
      //-----------------------------------------------------------------------
      /**
       * Implement the default on exception behaviour.
       */
      static class DefaultExceptionConversion extends AbstractDefaultConversion {
  
          /**
           * Constructs a default on exception conversion.
           * 
           * @param conversion  the conversion to decorate, not null
           * @param defaultValue  the value to return if conversion throws an exception
           */
          DefaultExceptionConversion(Conversion conversion, Object defaultValue) {
              super(conversion, defaultValue);
          }
  
          /**
           * Performs the conversion by delegating to the original conversion and
           * handling the default value.
           * 
           * @param value  the object to be converted
           * @param converter  the associated converter
           * @return the result of the conversion
           */
          public Object convert(Object value, Converter converter) {
              try {
                  return getDecoratedConversion().convert(value, converter);
              } catch (Exception ex) {
                  return getDefaultValue();
              }
          }
      }
  
      //-----------------------------------------------------------------------
      /**
       * Implement the default on exception behaviour.
       */
      static class DefaultNullExceptionConversion extends AbstractDefaultConversion {
  
          /**
           * Constructs a default on nulls and exception conversion.
           * 
           * @param conversion  the conversion to decorate, not null
           * @param defaultValue  the default value to return
           */
          DefaultNullExceptionConversion(Conversion conversion, Object defaultValue) {
              super(conversion, defaultValue);
          }
  
          /**
           * Performs the conversion by delegating to the original conversion and
           * handling the default value.
           * 
           * @param value  the object to be converted
           * @param converter  the associated converter
           * @return the result of the conversion
           */
          public Object convert(Object value, Converter converter) {
              if (value == null) {
                  return getDefaultValue();
              }
              try {
                  Object result = getDecoratedConversion().convert(value, converter);
                  if (result == null) {
                      return getDefaultValue();
                  }
                  return result;
              } catch (Exception ex) {
                  return getDefaultValue();
              }
          }
      }
  
      //-----------------------------------------------------------------------
      /**
       * Implement the default null result behaviour.
       */
      static abstract class AbstractDefaultConversionFactory implements ConversionFactory {
  
          /** The decorated factory */
          private final ConversionFactory factory;
          /** The default value to return */
          private final Object defaultValue;
  
          /**
           * Constructor.
           * 
           * @param factory  the factory to decorate, not null
           * @param defaultValue  the default value to return
           */
          AbstractDefaultConversionFactory(ConversionFactory factory, Object defaultValue) {
              super();
              this.factory = factory;
              this.defaultValue = defaultValue;
          }
  
          /**
           * Gets an indicator as to how well the factory matches.
           * 
           * @param value  the value to be converted
           * @param fromType  the type to be converted from
           * @param toType  the type to be converted to
           * @return a percentage value for the appropriateness of this class
           */
          public int getMatchPercent(Object value, Class fromType, Class toType) {
              return getDecoratedConversionFactory().getMatchPercent(value, fromType, toType);
          }
  
          /**
           * Creates a conversion instance for the specified conversion.
           * This implementation lets the decorated factory create an instance.
           * This instance is then decorated with defaulting behaviour.
           * 
           * @param value  the value to be converted
           * @param fromType  the type to convert from
           * @param toType  the type to convert to
           * @return a conversion instance
           */
          public Conversion getInstance(Object value, Class fromType, Class toType) {
              Conversion conversion = getDecoratedConversionFactory().getInstance(value, fromType, toType);
              return createConversion(conversion, getDefaultValue());
          }
  
          /**
           * Creates a new decorated conversion.
           * 
           * @param conversion  the conversion to decorate
           * @param object  the default value
           * @return a new decorated conversion
           */
          protected abstract Conversion createConversion(Conversion conversion, Object defaultValue);
  
          /**
           * Gets the factory being decorated.
           * 
           * @return the decorated factory
           */
          public ConversionFactory getDecoratedConversionFactory() {
              return factory;
          }
  
          /**
           * Gets the default value to be returned.
           * 
           * @return the default value
           */
          public Object getDefaultValue() {
              return defaultValue;
          }
      }
  
      //-----------------------------------------------------------------------
      /**
       * Implement the default null result behaviour.
       */
      static class DefaultNullResultConversionFactory extends AbstractDefaultConversionFactory {
  
          /**
           * Constructs a null result factory.
           * 
           * @param factory  the factory to decorate, not null
           * @param defaultValue  the default value to return
           */
          DefaultNullResultConversionFactory(ConversionFactory factory, Object defaultValue) {
              super(factory, defaultValue);
          }
  
          /**
           * Creates a new decorated conversion.
           * 
           * @param conversion  the conversion to decorate
           * @param object  the default value
           * @return a new decorated conversion
           */
          protected Conversion createConversion(Conversion conversion, Object defaultValue) {
              return new DefaultNullResultConversion(conversion, defaultValue);
          }
      }
  
      //-----------------------------------------------------------------------
      /**
       * Implement the default null input behaviour.
       */
      static class DefaultNullInputConversionFactory extends AbstractDefaultConversionFactory {
  
          /**
           * Constructs a null input factory.
           * 
           * @param factory  the factory to decorate, not null
           * @param defaultValue  the default value to return
           */
          DefaultNullInputConversionFactory(ConversionFactory factory, Object defaultValue) {
              super(factory, defaultValue);
          }
  
          /**
           * Creates a new decorated conversion.
           * 
           * @param conversion  the conversion to decorate
           * @param object  the default value
           * @return a new decorated conversion
           */
          protected Conversion createConversion(Conversion conversion, Object defaultValue) {
              return new DefaultNullInputConversion(conversion, defaultValue);
          }
      }
  
      //-----------------------------------------------------------------------
      /**
       * Implement the default on exception behaviour.
       */
      static class DefaultExceptionConversionFactory extends AbstractDefaultConversionFactory {
  
          /**
           * Constructs a null result factory.
           * 
           * @param factory  the factory to decorate, not null
           * @param defaultValue  the default value to return
           */
          DefaultExceptionConversionFactory(ConversionFactory factory, Object defaultValue) {
              super(factory, defaultValue);
          }
  
          /**
           * Creates a new decorated conversion.
           * 
           * @param conversion  the conversion to decorate
           * @param object  the default value
           * @return a new decorated conversion
           */
          protected Conversion createConversion(Conversion conversion, Object defaultValue) {
              return new DefaultExceptionConversion(conversion, defaultValue);
          }
      }
  
      //-----------------------------------------------------------------------
      /**
       * Implement the default on nulls and exceptions behaviour.
       */
      static class DefaultNullExceptionConversionFactory extends AbstractDefaultConversionFactory {
  
          /**
           * Constructs a null result factory.
           * 
           * @param factory  the factory to decorate, not null
           * @param defaultValue  the default value to return
           */
          DefaultNullExceptionConversionFactory(ConversionFactory factory, Object defaultValue) {
              super(factory, defaultValue);
          }
  
          /**
           * Creates a new decorated conversion.
           * 
           * @param conversion  the conversion to decorate
           * @param object  the default value
           * @return a new decorated conversion
           */
          protected Conversion createConversion(Conversion conversion, Object defaultValue) {
              return new DefaultNullExceptionConversion(conversion, defaultValue);
          }
      }
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/convert/src/java/org/apache/commons/convert2/conversion/IdentityConversionFactory.java
  
  Index: IdentityConversionFactory.java
  ===================================================================
  /*
   *  Copyright 2004 The Apache Software Foundation
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   *  Unless required by applicable law or agreed to in writing, software
   *  distributed under the License is distributed on an "AS IS" BASIS,
   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *  See the License for the specific language governing permissions and
   *  limitations under the License.
   */
  package org.apache.commons.convert2.conversion;
  
  import org.apache.commons.convert2.Conversion;
  import org.apache.commons.convert2.ConversionFactory;
  import org.apache.commons.convert2.Converter;
  
  /**
   * Conversion from TimeZone to String using <code>getID()</code>.
   *
   * @author Stephen Colebourne
   * @version $Id: IdentityConversionFactory.java,v 1.1 2004/03/20 18:44:11 scolebourne Exp $
   * @since 1.0
   */
  public class IdentityConversionFactory implements ConversionFactory {
  
      /** Singleton instance of this factory */
      public static final ConversionFactory INSTANCE = new IdentityConversionFactory();
  
      /**
       * Restricted constructor.
       */
      protected IdentityConversionFactory() {
          super();
      }
  
      //-----------------------------------------------------------------------
      /**
       * Checks if the types are equal, if so return 80.
       * 
       * @param value  the value to be converted, read only, may be null
       * @param fromType  the type to convert from, may be null
       * @param toType  the type to convert to, may be null
       * @return 80 if types are equal
       */
      public int getMatchPercent(Object value, Class fromType, Class toType) {
          if (fromType == toType && fromType != null) {
              return 80;
          }
          return 0;
      }
  
      /**
       * Create a new conversion object for the specified from and to types.
       * <p>
       * This implementation returns the identity conversion.
       *
       * @param value  the value to be converted, read only, may be null
       * @param fromType  the type to convert from, may be null
       * @param toType  the type to convert to, may be null
       * @return a Conversion object for repeatedly performing conversions
       */
      public Conversion getInstance(Object value, Class fromType, Class toType) {
          return new IdentityConversion(fromType, toType);
      }
      
      /**
       * Gets a string suitable for debugging.
       * 
       * @return a debugging string
       */
      public String toString() {
          return "ConversionFactory[Identity]";
      }
  
      //-----------------------------------------------------------------------
      /**
       * Generic conversion implementation that returns the input value.
       */
      class IdentityConversion extends AbstractConversion {
  
          /**
           * Constructs a Conversion.
           * 
           * @param fromType  the type to convert from
           * @param toType  the type to convert to
           */
          IdentityConversion(Class fromType, Class toType) {
              super(fromType, toType);
          }
  
          /**
           * Converts the input value by returning it unchanged.
           * 
           * @param value  the input value to be converted, pre-checked to not be null
           * @param converter  the converter being used, not null
           * @return the input value
           */
          public Object convert(Object value, Converter converter) {
              return value;
          }
      }
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/convert/src/java/org/apache/commons/convert2/conversion/NumberToIntegerConversionFactory.java
  
  Index: NumberToIntegerConversionFactory.java
  ===================================================================
  /*
   *  Copyright 2004 The Apache Software Foundation
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   *  Unless required by applicable law or agreed to in writing, software
   *  distributed under the License is distributed on an "AS IS" BASIS,
   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *  See the License for the specific language governing permissions and
   *  limitations under the License.
   */
  package org.apache.commons.convert2.conversion;
  
  import org.apache.commons.convert2.ConversionFactory;
  import org.apache.commons.convert2.Converter;
  
  /**
   * Conversion from Number to Integer using <code>intValue()</code>.
   *
   * @author Stephen Colebourne
   * @version $Id: NumberToIntegerConversionFactory.java,v 1.1 2004/03/20 18:44:11 scolebourne Exp $
   * @since 1.0
   */
  public class NumberToIntegerConversionFactory extends AbstractConversionFactory {
  
      /** Singleton instance of this factory */
      public static final ConversionFactory INSTANCE = new NumberToIntegerConversionFactory();
  
      /**
       * Restricted constructor.
       */
      protected NumberToIntegerConversionFactory() {
          super(Number.class, Integer.class);
      }
  
      //-----------------------------------------------------------------------
      /**
       * Convert the non-null Number to an Integer using <code>intValue()</code>.
       * 
       * @param value  the input value to be converted, pre-checked to not be null
       * @param converter  the converter being used, not null
       * @return the converted value
       * @throws RuntimeException if conversion fails
       */
      protected Object convertValue(Object value, Converter converter) {
          return new Integer(((Number) value).intValue());
      }
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/convert/src/java/org/apache/commons/convert2/conversion/StringToIntegerConversion.java
  
  Index: StringToIntegerConversion.java
  ===================================================================
  /*
   *  Copyright 2004 The Apache Software Foundation
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   *  Unless required by applicable law or agreed to in writing, software
   *  distributed under the License is distributed on an "AS IS" BASIS,
   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *  See the License for the specific language governing permissions and
   *  limitations under the License.
   */
  package org.apache.commons.convert2.conversion;
  
  import org.apache.commons.convert2.Conversion;
  import org.apache.commons.convert2.Converter;
  
  /**
   * Conversion from String to Integer using <code>Integer.valueOf()</code>.
   *
   * @author Stephen Colebourne
   * @author Oliver Heger
   * @version $Id: StringToIntegerConversion.java,v 1.1 2004/03/20 18:44:11 scolebourne Exp $
   * @since 1.0
   */
  public class StringToIntegerConversion extends AbstractConversion {
  
      /** Singleton instance of this factory */
      public static final Conversion INSTANCE = new StringToIntegerConversion();
  
      /**
       * Constructor.
       */
      protected StringToIntegerConversion() {
          super(STRING_CLASS, Integer.class);
      }
  
      //-----------------------------------------------------------------------
      /**
       * Convert the non-null String to an Integer using <code>Integer.valueOf()</code>.
       * 
       * @param value  the input value to be converted, pre-checked to not be null
       * @param converter  the converter being used, not null
       * @return the converted value
       * @throws RuntimeException if conversion fails
       */
      protected Object convertValue(Object value, Converter converter) {
          return Integer.valueOf((String) value);
      }
  
  }
  
  
  
  1.2       +25 -22    jakarta-commons-sandbox/convert/src/test/org/apache/commons/convert2/conversion/TestObjectToStringConversionFactory.java
  
  Index: TestObjectToStringConversionFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/convert/src/test/org/apache/commons/convert2/conversion/TestObjectToStringConversionFactory.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TestObjectToStringConversionFactory.java	7 Mar 2004 01:16:29 -0000	1.1
  +++ TestObjectToStringConversionFactory.java	20 Mar 2004 18:44:11 -0000	1.2
  @@ -40,7 +40,7 @@
       public TestObjectToStringConversionFactory(String name) {
           super(name);
       }
  -    
  +
       public static void main(String[] args) {
           TestRunner.run(suite());
       }
  @@ -53,10 +53,11 @@
       public void testClass() {
           Constructor[] con = ObjectToStringConversionFactory.class.getDeclaredConstructors();
           assertEquals(1, con.length);
  -        assertEquals(true, Modifier.isPrivate(con[0].getModifiers()));
  -        assertTrue(ConversionFactory.class.isAssignableFrom(ObjectToStringConversionFactory.class));
  +        assertEquals(true, Modifier.isProtected(con[0].getModifiers()));
  +        assertEquals(false, Conversion.class.isAssignableFrom(ObjectToStringConversionFactory.class));
  +        assertEquals(true, ConversionFactory.class.isAssignableFrom(ObjectToStringConversionFactory.class));
       }
  -    
  +
       public void testGetMatch() {
           assertEquals(0, ObjectToStringConversionFactory.INSTANCE.getMatchPercent(null, null, null));
           assertEquals(0, ObjectToStringConversionFactory.INSTANCE.getMatchPercent(null, Object.class, null));
  @@ -68,9 +69,9 @@
           assertEquals(20, ObjectToStringConversionFactory.INSTANCE.getMatchPercent(null, String.class, String.class));
           assertEquals(20, ObjectToStringConversionFactory.INSTANCE.getMatchPercent(null, Integer.class, String.class));
       }
  -    
  +
       //-----------------------------------------------------------------------
  -    public void testCoversionFromBoolean() {
  +    public void testCoversionFromBoolean() throws Exception {
           Converter converter = new Converter();
           Conversion conv = ObjectToStringConversionFactory.INSTANCE.getInstance(null, Boolean.class, String.class);
           assertEquals(Boolean.class, conv.getFromType());
  @@ -79,8 +80,8 @@
           assertEquals("false", conv.convert(Boolean.FALSE, converter));
           assertEquals(null, conv.convert(null, converter));
       }
  -    
  -    public void testCoversionFromCharacter() {
  +
  +    public void testCoversionFromCharacter() throws Exception {
           Converter converter = new Converter();
           Conversion conv = ObjectToStringConversionFactory.INSTANCE.getInstance(null, Character.class, String.class);
           assertEquals(Character.class, conv.getFromType());
  @@ -89,8 +90,8 @@
           assertEquals("a", conv.convert(new Character('a'), converter));
           assertEquals(null, conv.convert(null, converter));
       }
  -    
  -    public void testCoversionFromByte() {
  +
  +    public void testCoversionFromByte() throws Exception {
           Converter converter = new Converter();
           Conversion conv = ObjectToStringConversionFactory.INSTANCE.getInstance(null, Byte.class, String.class);
           assertEquals(Byte.class, conv.getFromType());
  @@ -99,8 +100,8 @@
           assertEquals("20", conv.convert(new Byte((byte) 20), converter));
           assertEquals(null, conv.convert(null, converter));
       }
  -    
  -    public void testCoversionFromShort() {
  +
  +    public void testCoversionFromShort() throws Exception {
           Converter converter = new Converter();
           Conversion conv = ObjectToStringConversionFactory.INSTANCE.getInstance(null, Short.class, String.class);
           assertEquals(Short.class, conv.getFromType());
  @@ -109,8 +110,8 @@
           assertEquals("20", conv.convert(new Short((short) 20), converter));
           assertEquals(null, conv.convert(null, converter));
       }
  -    
  -    public void testCoversionFromInteger() {
  +
  +    public void testCoversionFromInteger() throws Exception {
           Converter converter = new Converter();
           Conversion conv = ObjectToStringConversionFactory.INSTANCE.getInstance(null, Integer.class, String.class);
           assertEquals(Integer.class, conv.getFromType());
  @@ -119,8 +120,8 @@
           assertEquals("20", conv.convert(new Integer(20), converter));
           assertEquals(null, conv.convert(null, converter));
       }
  -    
  -    public void testCoversionFromLong() {
  +
  +    public void testCoversionFromLong() throws Exception {
           Converter converter = new Converter();
           Conversion conv = ObjectToStringConversionFactory.INSTANCE.getInstance(null, Long.class, String.class);
           assertEquals(Long.class, conv.getFromType());
  @@ -129,18 +130,20 @@
           assertEquals("20", conv.convert(new Long(20), converter));
           assertEquals(null, conv.convert(null, converter));
       }
  -    
  -    public void testCoversionFromList() {
  +
  +    public void testCoversionFromList() throws Exception {
           Converter converter = new Converter();
           Conversion conv = ObjectToStringConversionFactory.INSTANCE.getInstance(null, List.class, String.class);
           assertEquals(List.class, conv.getFromType());
           assertEquals(String.class, conv.getToType());
           assertEquals("[]", conv.convert(new ArrayList(), converter));
  -        assertEquals("[a, b, c]", conv.convert(new ArrayList(Arrays.asList(new Object[] {"a", "b", "c"})), converter));
  +        assertEquals(
  +            "[a, b, c]",
  +            conv.convert(new ArrayList(Arrays.asList(new Object[] { "a", "b", "c" })), converter));
           assertEquals(null, conv.convert(null, converter));
       }
  -    
  -    public void testCoversionFromString() {
  +
  +    public void testCoversionFromString() throws Exception {
           Converter converter = new Converter();
           Conversion conv = ObjectToStringConversionFactory.INSTANCE.getInstance(null, String.class, String.class);
           assertEquals(String.class, conv.getFromType());
  @@ -149,5 +152,5 @@
           assertEquals("20", conv.convert("20", converter));
           assertEquals(null, conv.convert(null, converter));
       }
  -    
  +
   }
  
  
  
  1.2       +10 -9     jakarta-commons-sandbox/convert/src/test/org/apache/commons/convert2/conversion/TestTimeZoneToStringConversionFactory.java
  
  Index: TestTimeZoneToStringConversionFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/convert/src/test/org/apache/commons/convert2/conversion/TestTimeZoneToStringConversionFactory.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TestTimeZoneToStringConversionFactory.java	7 Mar 2004 01:16:29 -0000	1.1
  +++ TestTimeZoneToStringConversionFactory.java	20 Mar 2004 18:44:11 -0000	1.2
  @@ -39,7 +39,7 @@
       public TestTimeZoneToStringConversionFactory(String name) {
           super(name);
       }
  -    
  +
       public static void main(String[] args) {
           TestRunner.run(suite());
       }
  @@ -52,10 +52,11 @@
       public void testClass() {
           Constructor[] con = TimeZoneToStringConversionFactory.class.getDeclaredConstructors();
           assertEquals(1, con.length);
  -        assertEquals(true, Modifier.isPrivate(con[0].getModifiers()));
  -        assertTrue(ConversionFactory.class.isAssignableFrom(TimeZoneToStringConversionFactory.class));
  +        assertEquals(true, Modifier.isProtected(con[0].getModifiers()));
  +        assertEquals(false, Conversion.class.isAssignableFrom(TimeZoneToStringConversionFactory.class));
  +        assertEquals(true, ConversionFactory.class.isAssignableFrom(TimeZoneToStringConversionFactory.class));
       }
  -    
  +
       public void testGetMatch() {
           assertEquals(0, TimeZoneToStringConversionFactory.INSTANCE.getMatchPercent(null, null, null));
           assertEquals(0, TimeZoneToStringConversionFactory.INSTANCE.getMatchPercent(null, TimeZone.class, null));
  @@ -69,8 +70,8 @@
           assertEquals(60, TimeZoneToStringConversionFactory.INSTANCE.getMatchPercent(null, TimeZone.class, String.class));
           assertEquals(60, TimeZoneToStringConversionFactory.INSTANCE.getMatchPercent(null, SimpleTimeZone.class, String.class));
       }
  -    
  -    public void testCoversionFromSimpleTimeZone() {
  +
  +    public void testCoversionFromSimpleTimeZone() throws Exception {
           Converter converter = new Converter();
           Conversion conv = TimeZoneToStringConversionFactory.INSTANCE.getInstance(null, SimpleTimeZone.class, String.class);
           assertEquals(SimpleTimeZone.class, conv.getFromType());
  @@ -79,8 +80,8 @@
           assertEquals("GMT+1", conv.convert(new SimpleTimeZone(0, "GMT+1"), converter));
           assertEquals(null, conv.convert(null, converter));
       }
  -    
  -    public void testCoversionFromTimeZone() {
  +
  +    public void testCoversionFromTimeZone() throws Exception {
           Converter converter = new Converter();
           Conversion conv = TimeZoneToStringConversionFactory.INSTANCE.getInstance(null, TimeZone.class, String.class);
           assertEquals(TimeZone.class, conv.getFromType());
  @@ -89,5 +90,5 @@
           assertEquals("Europe/Paris", conv.convert(TimeZone.getTimeZone("Europe/Paris"), converter));
           assertEquals(null, conv.convert(null, converter));
       }
  -    
  +
   }
  
  
  
  1.2       +5 -0      jakarta-commons-sandbox/convert/src/test/org/apache/commons/convert2/conversion/PackageTestSuite.java
  
  Index: PackageTestSuite.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/convert/src/test/org/apache/commons/convert2/conversion/PackageTestSuite.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- PackageTestSuite.java	7 Mar 2004 01:16:29 -0000	1.1
  +++ PackageTestSuite.java	20 Mar 2004 18:44:11 -0000	1.2
  @@ -37,7 +37,12 @@
   
       public static Test suite() {
           TestSuite suite = new TestSuite("Conversions");
  +        suite.addTest(TestClassToStringConversion.suite());
  +        suite.addTest(TestDefaultValueDecorator.suite());
  +        suite.addTest(TestIdentityConversionFactory.suite());
  +        suite.addTest(TestNumberToIntegerConversionFactory.suite());
           suite.addTest(TestObjectToStringConversionFactory.suite());
  +        suite.addTest(TestStringToIntegerConversion.suite());
           suite.addTest(TestTimeZoneToStringConversionFactory.suite());
           return suite;
       }
  
  
  
  1.1                  jakarta-commons-sandbox/convert/src/test/org/apache/commons/convert2/conversion/TestIdentityConversionFactory.java
  
  Index: TestIdentityConversionFactory.java
  ===================================================================
  /*
   *  Copyright 2004 The Apache Software Foundation
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   *  Unless required by applicable law or agreed to in writing, software
   *  distributed under the License is distributed on an "AS IS" BASIS,
   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *  See the License for the specific language governing permissions and
   *  limitations under the License.
   */
  package org.apache.commons.convert2.conversion;
  
  import java.lang.reflect.Constructor;
  import java.lang.reflect.Modifier;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  import junit.textui.TestRunner;
  
  import org.apache.commons.convert2.Conversion;
  import org.apache.commons.convert2.ConversionFactory;
  import org.apache.commons.convert2.Converter;
  
  /**
   * Test class.
   * 
   * @author Stephen Colebourne
   */
  public class TestIdentityConversionFactory extends TestCase {
  
      public TestIdentityConversionFactory(String name) {
          super(name);
      }
  
      public static void main(String[] args) {
          TestRunner.run(suite());
      }
  
      public static Test suite() {
          return (new TestSuite(TestIdentityConversionFactory.class, "Identity"));
      }
  
      //-----------------------------------------------------------------------
      public void testClass() {
          Constructor[] con = IdentityConversionFactory.class.getDeclaredConstructors();
          assertEquals(1, con.length);
          assertEquals(true, Modifier.isProtected(con[0].getModifiers()));
          assertEquals(false, Conversion.class.isAssignableFrom(IdentityConversionFactory.class));
          assertEquals(true, ConversionFactory.class.isAssignableFrom(IdentityConversionFactory.class));
      }
  
      public void testGetMatch() {
          assertEquals(0, IdentityConversionFactory.INSTANCE.getMatchPercent(null, null, null));
          assertEquals(0, IdentityConversionFactory.INSTANCE.getMatchPercent(null, String.class, null));
  
          assertEquals(0, IdentityConversionFactory.INSTANCE.getMatchPercent(null, String.class, Object.class));
          assertEquals(0, IdentityConversionFactory.INSTANCE.getMatchPercent(null, String.class, Integer.class));
  
          assertEquals(0, IdentityConversionFactory.INSTANCE.getMatchPercent(null, Object.class, String.class));
          assertEquals(0, IdentityConversionFactory.INSTANCE.getMatchPercent(null, Integer.class, String.class));
          
          assertEquals(80, IdentityConversionFactory.INSTANCE.getMatchPercent(null, String.class, String.class));
          assertEquals(80, IdentityConversionFactory.INSTANCE.getMatchPercent(null, Integer.class, Integer.class));
      }
  
      public void testCoversionString() throws Exception {
          Converter converter = new Converter();
          Conversion conv = IdentityConversionFactory.INSTANCE.getInstance(null, String.class, String.class);
          String str = "ABCDE";
          
          assertEquals(String.class, conv.getFromType());
          assertEquals(String.class, conv.getToType());
          assertSame(str, conv.convert(str, converter));
          assertEquals(null, conv.convert(null, converter));
      }
  
      public void testCoversionInteger() throws Exception {
          Converter converter = new Converter();
          Conversion conv = IdentityConversionFactory.INSTANCE.getInstance(null, Integer.class, Integer.class);
          Integer six = new Integer(6);
          
          assertEquals(Integer.class, conv.getFromType());
          assertEquals(Integer.class, conv.getToType());
          assertSame(six, conv.convert(six, converter));
          assertEquals(null, conv.convert(null, converter));
      }
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/convert/src/test/org/apache/commons/convert2/conversion/TestStringToIntegerConversion.java
  
  Index: TestStringToIntegerConversion.java
  ===================================================================
  /*
   *  Copyright 2004 The Apache Software Foundation
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   *  Unless required by applicable law or agreed to in writing, software
   *  distributed under the License is distributed on an "AS IS" BASIS,
   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *  See the License for the specific language governing permissions and
   *  limitations under the License.
   */
  package org.apache.commons.convert2.conversion;
  
  import java.lang.reflect.Constructor;
  import java.lang.reflect.Modifier;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  import junit.textui.TestRunner;
  
  import org.apache.commons.convert2.Conversion;
  import org.apache.commons.convert2.ConversionFactory;
  import org.apache.commons.convert2.Converter;
  
  /**
   * Test class.
   * 
   * @author Stephen Colebourne
   */
  public class TestStringToIntegerConversion extends TestCase {
  
      public TestStringToIntegerConversion(String name) {
          super(name);
      }
  
      public static void main(String[] args) {
          TestRunner.run(suite());
      }
  
      public static Test suite() {
          return (new TestSuite(TestStringToIntegerConversion.class, "NumberToInteger"));
      }
  
      //-----------------------------------------------------------------------
      public void testClass() {
          Constructor[] con = StringToIntegerConversion.class.getDeclaredConstructors();
          assertEquals(1, con.length);
          assertEquals(true, Modifier.isProtected(con[0].getModifiers()));
          assertEquals(true, Conversion.class.isAssignableFrom(StringToIntegerConversion.class));
          assertEquals(false, ConversionFactory.class.isAssignableFrom(StringToIntegerConversion.class));
      }
  
      public void testCoversion() throws Exception {
          Converter converter = new Converter();
          Conversion conv = StringToIntegerConversion.INSTANCE;
  
          assertEquals(String.class, conv.getFromType());
          assertEquals(Integer.class, conv.getToType());
          assertEquals(new Integer(6), conv.convert("6", converter));
          assertEquals(null, conv.convert(null, converter));
          try {
              conv.convert("NoNumber", converter);
              fail();
          } catch (Exception ex) {}
      }
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/convert/src/test/org/apache/commons/convert2/conversion/TestDefaultValueDecorator.java
  
  Index: TestDefaultValueDecorator.java
  ===================================================================
  /*
   *  Copyright 2004 The Apache Software Foundation
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   *  Unless required by applicable law or agreed to in writing, software
   *  distributed under the License is distributed on an "AS IS" BASIS,
   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *  See the License for the specific language governing permissions and
   *  limitations under the License.
   */
  package org.apache.commons.convert2.conversion;
  
  import java.lang.reflect.Constructor;
  import java.lang.reflect.Modifier;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  import junit.textui.TestRunner;
  
  import org.apache.commons.convert2.Conversion;
  import org.apache.commons.convert2.ConversionFactory;
  import org.apache.commons.convert2.Converter;
  
  /**
   * Test class for DefaultValueConversionDecorator.
   * 
   * @author Oliver Heger
   * @author Stephen Colebourne
   */
  public class TestDefaultValueDecorator extends TestCase {
  
      private static final MockConversion MOCK = new MockConversion();
      private static final Integer ONE = new Integer(1);
      private static final Integer DEFAULT = new Integer(42);
      
      Converter converter;
      Conversion baseConversion;
      
      public TestDefaultValueDecorator(String name) {
          super(name);
      }
  
      public static void main(String[] args) {
          TestRunner.run(suite());
      }
  
      public static Test suite() {
          return (new TestSuite(TestDefaultValueDecorator.class, "DefaultValueDecorator"));
      }
  
      protected void setUp() throws Exception {
          converter = new Converter();
          baseConversion = StringToIntegerConversion.INSTANCE;
      }
  
      //-----------------------------------------------------------------------
      public void testClass() {
          Constructor[] con = DefaultValueDecorator.class.getDeclaredConstructors();
          assertEquals(1, con.length);
          assertEquals(true, Modifier.isProtected(con[0].getModifiers()));
          assertEquals(false, Conversion.class.isAssignableFrom(DefaultValueDecorator.class));
          assertEquals(false, ConversionFactory.class.isAssignableFrom(DefaultValueDecorator.class));
      }
  
      //-----------------------------------------------------------------------
      public void testDefaultNullInputConversion() throws Exception {
          Conversion conv = DefaultValueDecorator.defaultNullInput(baseConversion, DEFAULT);
          converter.getRegistry().addConversion(conv);
  
          assertEquals(DEFAULT, conv.convert(null, converter));
          assertEquals(ONE, conv.convert("1", converter));
          try {
              conv.convert("NoNumber", converter);
              fail();
          } catch (Exception ex) {}
          
          Conversion mock = DefaultValueDecorator.defaultNullInput(MOCK, null);
          MockConversion.called = 0;
          mock.convert(null, converter);
          assertEquals(0, MockConversion.called);
          
          MockConversion.called = 0;
          mock.convert("1", converter);
          assertEquals(1, MockConversion.called);
      }
  
      public void testDefaultNullResultConversion() throws Exception {
          Conversion conv = DefaultValueDecorator.defaultNullResult(baseConversion, DEFAULT);
          converter.getRegistry().addConversion(conv);
  
          assertEquals(DEFAULT, conv.convert(null, converter));
          assertEquals(ONE, conv.convert("1", converter));
          try {
              conv.convert("NoNumber", converter);
              fail();
          } catch (Exception ex) {}
          
          Conversion mock = DefaultValueDecorator.defaultNullResult(MOCK, null);
          MockConversion.called = 0;
          mock.convert(null, converter);
          assertEquals(1, MockConversion.called);
          
          MockConversion.called = 0;
          mock.convert("1", converter);
          assertEquals(1, MockConversion.called);
      }
  
      public void testDefaultExceptionConversion() throws Exception {
          Conversion conv = DefaultValueDecorator.defaultException(baseConversion, DEFAULT);
          converter.getRegistry().addConversion(conv);
  
          assertEquals(null, conv.convert(null, converter));
          assertEquals(ONE, conv.convert("1", converter));
          assertEquals(DEFAULT, conv.convert("NoNumber", converter));
          
          Conversion mock = DefaultValueDecorator.defaultException(MOCK, null);
          MockConversion.called = 0;
          mock.convert(null, converter);
          assertEquals(1, MockConversion.called);
          
          MockConversion.called = 0;
          mock.convert("1", converter);
          assertEquals(1, MockConversion.called);
      }
  
      public void testNullAndExceptionConversion() throws Exception {
          Conversion conv = DefaultValueDecorator.defaultNullAndException(baseConversion, DEFAULT);
          converter.getRegistry().addConversion(conv);
  
          assertEquals(DEFAULT, conv.convert(null, converter));
          assertEquals(ONE, conv.convert("1", converter));
          assertEquals(DEFAULT, conv.convert("NoNumber", converter));
          
          Conversion mock = DefaultValueDecorator.defaultNullAndException(MOCK, null);
          MockConversion.called = 0;
          mock.convert(null, converter);
          assertEquals(0, MockConversion.called);
          
          MockConversion.called = 0;
          mock.convert("1", converter);
          assertEquals(1, MockConversion.called);
      }
  
      static class MockConversion implements Conversion {
          static int called = 0;
  
          public Object convert(Object value, Converter converter) throws Exception {
              called++;
              return null;
          }
  
          public Class getFromType() {
              return null;
          }
  
          public Class getToType() {
              return null;
          }
      }
  }
  
  
  
  1.1                  jakarta-commons-sandbox/convert/src/test/org/apache/commons/convert2/conversion/TestNumberToIntegerConversionFactory.java
  
  Index: TestNumberToIntegerConversionFactory.java
  ===================================================================
  /*
   *  Copyright 2004 The Apache Software Foundation
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   *  Unless required by applicable law or agreed to in writing, software
   *  distributed under the License is distributed on an "AS IS" BASIS,
   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *  See the License for the specific language governing permissions and
   *  limitations under the License.
   */
  package org.apache.commons.convert2.conversion;
  
  import java.lang.reflect.Constructor;
  import java.lang.reflect.Modifier;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  import junit.textui.TestRunner;
  
  import org.apache.commons.convert2.Conversion;
  import org.apache.commons.convert2.ConversionFactory;
  import org.apache.commons.convert2.Converter;
  
  /**
   * Test class.
   * 
   * @author Stephen Colebourne
   */
  public class TestNumberToIntegerConversionFactory extends TestCase {
  
      public TestNumberToIntegerConversionFactory(String name) {
          super(name);
      }
  
      public static void main(String[] args) {
          TestRunner.run(suite());
      }
  
      public static Test suite() {
          return (new TestSuite(TestNumberToIntegerConversionFactory.class, "NumberToInteger"));
      }
  
      //-----------------------------------------------------------------------
      public void testClass() {
          Constructor[] con = NumberToIntegerConversionFactory.class.getDeclaredConstructors();
          assertEquals(1, con.length);
          assertEquals(true, Modifier.isProtected(con[0].getModifiers()));
          assertEquals(false, Conversion.class.isAssignableFrom(NumberToIntegerConversionFactory.class));
          assertEquals(true, ConversionFactory.class.isAssignableFrom(NumberToIntegerConversionFactory.class));
      }
  
      public void testGetMatch() {
          assertEquals(0, NumberToIntegerConversionFactory.INSTANCE.getMatchPercent(null, null, null));
          assertEquals(0, NumberToIntegerConversionFactory.INSTANCE.getMatchPercent(null, Number.class, null));
  
          assertEquals(0, NumberToIntegerConversionFactory.INSTANCE.getMatchPercent(null, Number.class, Object.class));
          assertEquals(0, NumberToIntegerConversionFactory.INSTANCE.getMatchPercent(null, Number.class, Double.class));
  
          assertEquals(0, NumberToIntegerConversionFactory.INSTANCE.getMatchPercent(null, Object.class, String.class));
          assertEquals(0, NumberToIntegerConversionFactory.INSTANCE.getMatchPercent(null, Integer.class, String.class));
  
          assertEquals(60, NumberToIntegerConversionFactory.INSTANCE.getMatchPercent(null, Number.class, Integer.class));
          assertEquals(60, NumberToIntegerConversionFactory.INSTANCE.getMatchPercent(null, Double.class, Integer.class));
      }
  
      public void testCoversionFromLong() throws Exception {
          Converter converter = new Converter();
          Conversion conv = NumberToIntegerConversionFactory.INSTANCE.getInstance(null, Long.class, Integer.class);
  
          assertEquals(Long.class, conv.getFromType());
          assertEquals(Integer.class, conv.getToType());
          assertEquals(new Integer(6), conv.convert(new Long(6L), converter));
          assertEquals(null, conv.convert(null, converter));
      }
  
      public void testCoversionFromDouble() throws Exception {
          Converter converter = new Converter();
          Conversion conv = NumberToIntegerConversionFactory.INSTANCE.getInstance(null, Double.class, Integer.class);
  
          assertEquals(Double.class, conv.getFromType());
          assertEquals(Integer.class, conv.getToType());
          assertEquals(new Integer(6), conv.convert(new Double(6d), converter));
          assertEquals(null, conv.convert(null, converter));
      }
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/convert/src/test/org/apache/commons/convert2/conversion/TestClassToStringConversion.java
  
  Index: TestClassToStringConversion.java
  ===================================================================
  /*
   *  Copyright 2004 The Apache Software Foundation
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   *  Unless required by applicable law or agreed to in writing, software
   *  distributed under the License is distributed on an "AS IS" BASIS,
   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *  See the License for the specific language governing permissions and
   *  limitations under the License.
   */
  package org.apache.commons.convert2.conversion;
  
  import java.lang.reflect.Constructor;
  import java.lang.reflect.Modifier;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  import junit.textui.TestRunner;
  
  import org.apache.commons.convert2.Conversion;
  import org.apache.commons.convert2.ConversionFactory;
  import org.apache.commons.convert2.Converter;
  
  /**
   * Test class.
   * 
   * @author Stephen Colebourne
   */
  public class TestClassToStringConversion extends TestCase {
  
      public TestClassToStringConversion(String name) {
          super(name);
      }
  
      public static void main(String[] args) {
          TestRunner.run(suite());
      }
  
      public static Test suite() {
          return (new TestSuite(TestClassToStringConversion.class, "ClassToString"));
      }
  
      //-----------------------------------------------------------------------
      public void testClass() {
          Constructor[] con = ClassToStringConversion.class.getDeclaredConstructors();
          assertEquals(1, con.length);
          assertEquals(true, Modifier.isProtected(con[0].getModifiers()));
          assertEquals(true, Conversion.class.isAssignableFrom(ClassToStringConversion.class));
          assertEquals(false, ConversionFactory.class.isAssignableFrom(ClassToStringConversion.class));
      }
  
      public void testCoversion() throws Exception {
          Converter converter = new Converter();
          Conversion conv = ClassToStringConversion.INSTANCE;
  
          assertEquals(Class.class, conv.getFromType());
          assertEquals(String.class, conv.getToType());
          assertEquals("java.lang.String", conv.convert(String.class, converter));
          assertEquals(null, conv.convert(null, converter));
      }
  
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message