Author: niallp
Date: Sat Nov  4 19:10:58 2006
New Revision: 471350

URL: http://svn.apache.org/viewvc?view=rev&rev=471350
Log:
BEANUTILS-258 - Switch other Converter implementations to use the new 
AbstractConverter class and add new JUnit tests for CharacterConverter and 
ClassConverter

Added:
    
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/CharacterConverterTestCase.java
   (with props)
    
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/ClassConverterTestCase.java
   (with props)
Modified:
    
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/BooleanConverter.java
    
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/CharacterConverter.java
    
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/ClassConverter.java
    
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/FileConverter.java
    
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/URLConverter.java
    
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/BooleanConverterTestCase.java
    
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/URLConverterTestCase.java

Modified: 
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/BooleanConverter.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/BooleanConverter.java?view=diff&rev=471350&r1=471349&r2=471350
==============================================================================
--- 
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/BooleanConverter.java
 (original)
+++ 
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/BooleanConverter.java
 Sat Nov  4 19:10:58 2006
@@ -14,21 +14,21 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */ 
-
-
 package org.apache.commons.beanutils.converters;
 
 import org.apache.commons.beanutils.ConversionException;
 import org.apache.commons.beanutils.Converter;
 
-
 /**
- * <p>Standard [EMAIL PROTECTED] Converter} implementation that converts an 
incoming
- * String into a <code>java.lang.Boolean</code> object, optionally using a
- * default value or throwing a [EMAIL PROTECTED] ConversionException} if a 
conversion
- * error occurs.</p>
- *
- * <p>By default any object whose string representation is one of the values
+ * Generic [EMAIL PROTECTED] Converter} implementaion that handles conversion
+ * to and from <b>array</b> objects.
+ * [EMAIL PROTECTED] org.apache.commons.beanutils.Converter} implementaion that
+ * handles conversion to and from <b>java.lang.Boolean</b> objects.
+ * <p>
+ * Can be configured to either return a <i>default value</i> or throw a
+ * <code>ConversionException</code> if a conversion error occurs.
+ * <p>
+ * By default any object whose string representation is one of the values
  * {"yes", "y", "true", "on", "1"} is converted to Boolean.TRUE, and 
  * string representations {"no", "n", "false", "off", "0"} are converted
  * to Boolean.FALSE. The recognised true/false strings can be changed by:
@@ -53,8 +53,7 @@
  * @version $Revision$ $Date$
  * @since 1.3
  */
-
-public final class BooleanConverter implements Converter {
+public final class BooleanConverter extends AbstractConverter {
 
 
     // ----------------------------------------------------------- Constructors
@@ -66,9 +65,7 @@
      * not one of the known true strings, nor one of the known false strings.
      */
     public BooleanConverter() {
-
-        this.useDefault = false;
-
+        super(Boolean.class);
     }
 
 
@@ -85,14 +82,32 @@
      *  in which case this constructor acts like the no-argument one.
      */
     public BooleanConverter(Object defaultValue) {
-
-        if (defaultValue == NO_DEFAULT) {
-            this.useDefault = false;
-        } else {
-            this.defaultValue = defaultValue;
-            this.useDefault = true;
+        super(Boolean.class);
+        if (defaultValue != NO_DEFAULT) {
+            setDefaultValue(defaultValue);
         }
+    }
 
+    /**
+     * Create a [EMAIL PROTECTED] Converter} that will throw a [EMAIL 
PROTECTED] ConversionException}
+     * if a conversion error occurs, ie the string value being converted is
+     * not one of the known true strings, nor one of the known false strings.
+     * <p>
+     * The provided string arrays are copied, so that changes to the elements
+     * of the array after this call is made do not affect this object.
+     *
+     * @param trueStrings is the set of strings which should convert to the
+     *  value Boolean.TRUE. The value null must not be present. Case is
+     *  ignored.
+     *
+     * @param falseStrings is the set of strings which should convert to the
+     *  value Boolean.TRUE. The value null must not be present. Case is
+     *  ignored.
+     */
+    public BooleanConverter(String[] trueStrings, String[] falseStrings) {
+        super(Boolean.class);
+        this.trueStrings = copyStrings(trueStrings);
+        this.falseStrings = copyStrings(falseStrings);
     }
 
     /**
@@ -119,17 +134,12 @@
      */
     public BooleanConverter(String[] trueStrings, String[] falseStrings, 
                 Object defaultValue) {
-
+        super(Boolean.class);
         this.trueStrings = copyStrings(trueStrings);
         this.falseStrings = copyStrings(falseStrings);
-        
-        if (defaultValue == NO_DEFAULT) {
-            this.useDefault = false;
-        } else {
-            this.defaultValue = defaultValue;
-            this.useDefault = true;
+        if (defaultValue != NO_DEFAULT) {
+            setDefaultValue(defaultValue);
         }
-
     }
 
 
@@ -141,34 +151,24 @@
      * to the constructor to indicate that no default is desired. Note that
      * the value 'null' cannot be used for this purpose, as the caller may
      * want a null to be returned as the default.
+     * @deprecated Use constructors without default value.
      */
     public static final Object NO_DEFAULT = new Object();
 
 
     // ----------------------------------------------------- Instance Variables
 
-
-    /**
-     * The default value specified to our Constructor, if any.
-     */
-    private Object defaultValue;
-
-    /**
-     * Should we return the default value on conversion errors?
-     */
-    private boolean useDefault;
-
     /**
      * The set of strings that are known to map to Boolean.TRUE.
      */
-    private String[] trueStrings = {"yes", "y", "true", "on", "1"};
+    private String[] trueStrings = {"true", "yes", "y", "on", "1"};
 
     /**
      * The set of strings that are known to map to Boolean.FALSE.
      */
-    private String[] falseStrings = {"no", "n", "false", "off", "0"};
+    private String[] falseStrings = {"false", "no", "n", "off", "0"};
 
-    // --------------------------------------------------------- Public Methods
+    // --------------------------------------------------------- Protected 
Methods
 
     /**
      * Convert the specified input object into an output object of the
@@ -190,19 +190,7 @@
      *  successfully and the constructor was not provided with a default
      *  value to return on conversion failure.
      */
-    public Object convert(Class type, Object value) {
-
-        if (value == null) {
-            if (useDefault) {
-                return (defaultValue);
-            } else {
-                throw new ConversionException("No value specified");
-            }
-        }
-
-        if (value instanceof Boolean) {
-            return (value);
-        }
+    protected Object convertToType(Class type, Object value) throws Exception {
 
         // All the values in the trueStrings and falseStrings arrays are
         // guaranteed to be lower-case. By converting the input value
@@ -220,11 +208,7 @@
                 return Boolean.FALSE;
         }
         
-        if (useDefault) {
-            return defaultValue;
-        }
-
-        throw new ConversionException(value.toString());
+        throw new ConversionException("Cna't convert value '" + value + "' to 
a Boolean");
     }
 
     /**

Modified: 
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/CharacterConverter.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/CharacterConverter.java?view=diff&rev=471350&r1=471349&r2=471350
==============================================================================
--- 
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/CharacterConverter.java
 (original)
+++ 
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/CharacterConverter.java
 Sat Nov  4 19:10:58 2006
@@ -14,111 +14,64 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */ 
-
-
 package org.apache.commons.beanutils.converters;
 
-
-import org.apache.commons.beanutils.ConversionException;
 import org.apache.commons.beanutils.Converter;
 
-
 /**
- * <p>Standard [EMAIL PROTECTED] Converter} implementation that converts an 
incoming
- * String into a <code>java.lang.Character</code> object, optionally using a
- * default value or throwing a [EMAIL PROTECTED] ConversionException} if a 
conversion
- * error occurs.</p>
+ * [EMAIL PROTECTED] Converter} implementaion that handles conversion
+ * to and from <b>java.lang.Character</b> objects.
+ * <p>
+ * Can be configured to either return a <i>default value</i> or throw a
+ * <code>ConversionException</code> if a conversion error occurs.
  *
  * @author Craig R. McClanahan
  * @version $Revision$ $Date$
  * @since 1.3
  */
-
-public final class CharacterConverter implements Converter {
-
-
-    // ----------------------------------------------------------- Constructors
-
+public final class CharacterConverter extends AbstractConverter {
 
     /**
-     * Create a [EMAIL PROTECTED] Converter} that will throw a [EMAIL 
PROTECTED] ConversionException}
-     * if a conversion error occurs.
+     * Construct a <b>java.lang.Character</b> <i>Converter</i> that throws
+     * a <code>ConversionException</code> if an error occurs.
      */
     public CharacterConverter() {
-
-        this.defaultValue = null;
-        this.useDefault = false;
-
+        super(Character.class);
     }
 
-
     /**
-     * Create a [EMAIL PROTECTED] Converter} that will return the specified 
default value
-     * if a conversion error occurs.
+     * Construct a <b>java.lang.Character</b> <i>Converter</i> that returns
+     * a default value if an error occurs.
      *
      * @param defaultValue The default value to be returned
+     * if the value to be converted is missing or an error
+     * occurs converting the value.
      */
     public CharacterConverter(Object defaultValue) {
-
-        this.defaultValue = defaultValue;
-        this.useDefault = true;
-
+        super(Character.class, defaultValue);
     }
 
-
-    // ----------------------------------------------------- Instance Variables
-
-
     /**
-     * The default value specified to our Constructor, if any.
-     */
-    private Object defaultValue = null;
-
-
-    /**
-     * Should we return the default value on conversion errors?
+     * <p>Convert a java.lang.Class or object into a String.</p>
+     *
+     * @param value The input value to be converted
+     * @return the converted String value.
      */
-    private boolean useDefault = true;
-
-
-    // --------------------------------------------------------- Public Methods
-
+    protected String convertToString(Object value) {
+        String strValue = value.toString();
+        return strValue.length() == 0 ? "" : strValue.substring(0, 1);
+    }
 
     /**
-     * Convert the specified input object into an output object of the
-     * specified type.
-     *
-     * @param type Data type to which this value should be converted
-     * @param value The input value to be converted
+     * <p>Convert the input object into a java.lang.Character.</p>
      *
-     * @exception ConversionException if conversion cannot be performed
-     *  successfully
+     * @param type Data type to which this value should be converted.
+     * @param value The input value to be converted.
+     * @return The converted value.
+     * @throws Exception if conversion cannot be performed successfully
      */
-    public Object convert(Class type, Object value) {
-
-        if (value == null) {
-            if (useDefault) {
-                return (defaultValue);
-            } else {
-                throw new ConversionException("No value specified");
-            }
-        }
-
-        if (value instanceof Character) {
-            return (value);
-        }
-
-        try {
-            return (new Character(value.toString().charAt(0)));
-        } catch (Exception e) {
-            if (useDefault) {
-                return (defaultValue);
-            } else {
-                throw new ConversionException(e);
-            }
-        }
-
+    protected Object convertToType(Class type, Object value) throws Exception {
+        return new Character(value.toString().charAt(0));
     }
-
 
 }

Modified: 
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/ClassConverter.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/ClassConverter.java?view=diff&rev=471350&r1=471349&r2=471350
==============================================================================
--- 
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/ClassConverter.java
 (original)
+++ 
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/ClassConverter.java
 Sat Nov  4 19:10:58 2006
@@ -14,118 +14,72 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */ 
-
-
 package org.apache.commons.beanutils.converters;
 
-
-import org.apache.commons.beanutils.ConversionException;
 import org.apache.commons.beanutils.Converter;
 
-
 /**
- * <p>Standard [EMAIL PROTECTED] Converter} implementation that converts an 
incoming
- * String into a <code>java.lang.Class</code> object, optionally using a
- * default value or throwing a [EMAIL PROTECTED] ConversionException} if a 
conversion
- * error occurs.  The class will be loaded from the thread context class
+ * [EMAIL PROTECTED] Converter} implementaion that handles conversion
+ * to and from <b>java.lang.Class</b> objects.
+ * <p>
+ * The class will be loaded from the thread context class
  * loader (if it exists); otherwise the class loader that loaded this class
- * will be used.</p>
+ * will be used.
+ * <p>
+ * Can be configured to either return a <i>default value</i> or throw a
+ * <code>ConversionException</code> if a conversion error occurs.
  *
  * @author Tomas Viberg
  * @version $Revision$ $Date$
  * @since 1.4
  */
-
-public final class ClassConverter implements Converter {
-
-
-    // ----------------------------------------------------------- Constructors
-
+public final class ClassConverter extends AbstractConverter {
 
     /**
-     * Create a [EMAIL PROTECTED] Converter} that will throw a [EMAIL 
PROTECTED] ConversionException}
-     * if a conversion error occurs.
+     * Construct a <b>java.lang.Class</b> <i>Converter</i> that throws
+     * a <code>ConversionException</code> if an error occurs.
      */
     public ClassConverter() {
-
-        this.defaultValue = null;
-        this.useDefault = false;
-
+        super(Class.class);
     }
 
-
     /**
-     * Create a [EMAIL PROTECTED] Converter} that will return the specified 
default value
-     * if a conversion error occurs.
+     * Construct a <b>java.lang.Class</b> <i>Converter</i> that returns
+     * a default value if an error occurs.
      *
      * @param defaultValue The default value to be returned
+     * if the value to be converted is missing or an error
+     * occurs converting the value.
      */
     public ClassConverter(Object defaultValue) {
-
-        this.defaultValue = defaultValue;
-        this.useDefault = true;
-
+        super(Class.class, defaultValue);
     }
 
-
-    // ----------------------------------------------------- Instance Variables
-
-
     /**
-     * The default value specified to our Constructor, if any.
-     */
-    private Object defaultValue = null;
-
-
-    /**
-     * Should we return the default value on conversion errors?
+     * <p>Convert a java.lang.Class or object into a String.</p>
+     *
+     * @param value The input value to be converted
+     * @return the converted String value.
      */
-    private boolean useDefault = true;
-
-
-    // --------------------------------------------------------- Public Methods
-
+    protected String convertToString(Object value) {
+        return (value instanceof Class) ? ((Class)value).getName() : 
value.toString();
+    }
 
     /**
-     * Convert the specified input object into an output object of the
-     * specified type.
+     * <p>Convert the input object into a java.lang.Class.</p>
      *
-     * @param type Data type to which this value should be converted
-     * @param value The input value to be converted
-     *
-     * @exception ConversionException if conversion cannot be performed
-     *  successfully
+     * @param type Data type to which this value should be converted.
+     * @param value The input value to be converted.
+     * @return The converted value.
+     * @throws Exception if conversion cannot be performed successfully
      */
-    public Object convert(Class type, Object value) {
-
-        if (value == null) {
-            if (useDefault) {
-                return (defaultValue);
-            } else {
-                throw new ConversionException("No value specified");
-            }
-        }
-
-        if (value instanceof Class) {
-            return (value);
+    protected Object convertToType(Class type, Object value) throws Exception {
+        ClassLoader classLoader =
+            Thread.currentThread().getContextClassLoader();
+        if (classLoader == null) {
+            classLoader = ClassConverter.class.getClassLoader();
         }
-
-        try {
-            ClassLoader classLoader =
-                Thread.currentThread().getContextClassLoader();
-            if (classLoader == null) {
-                classLoader = ClassConverter.class.getClassLoader();
-            }
-            return (classLoader.loadClass(value.toString()));
-        } catch (Exception e) {
-            if (useDefault) {
-                return (defaultValue);
-            } else {
-                throw new ConversionException(e);
-            }
-        }
-
+        return (classLoader.loadClass(value.toString()));
     }
-
 
 }

Modified: 
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/FileConverter.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/FileConverter.java?view=diff&rev=471350&r1=471349&r2=471350
==============================================================================
--- 
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/FileConverter.java
 (original)
+++ 
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/FileConverter.java
 Sat Nov  4 19:10:58 2006
@@ -14,98 +14,54 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */ 
-
 package org.apache.commons.beanutils.converters;
 
 import java.io.File;
 
-import org.apache.commons.beanutils.ConversionException;
 import org.apache.commons.beanutils.Converter;
 
 /**
- * <p>Standard [EMAIL PROTECTED] Converter} implementation that converts an 
incoming
- * String into a <code>java.io.FileL</code> object, optionally using a
- * default value or throwing a [EMAIL PROTECTED] ConversionException} if a 
conversion
- * error occurs.</p>
+ * [EMAIL PROTECTED] Converter} implementaion that handles conversion
+ * to and from <b>java.io.File</b> objects.
+ * <p>
+ * Can be configured to either return a <i>default value</i> or throw a
+ * <code>ConversionException</code> if a conversion error occurs.
  *
  * @author James Strachan
  * @version $Revision$ $Date$
  * @since 1.6
  */
-public final class FileConverter implements Converter {
-
-    // ----------------------------------------------------- Instance Variables
-
-
-    /**
-     * The default value specified to our Constructor, if any.
-     */
-    private Object defaultValue = null;
-
-
-    /**
-     * Should we return the default value on conversion errors?
-     */
-    private boolean useDefault = true;
-
-
-    // ----------------------------------------------------------- Constructors
-
+public final class FileConverter extends AbstractConverter {
 
     /**
-     * Create a [EMAIL PROTECTED] Converter} that will throw a [EMAIL 
PROTECTED] ConversionException}
-     * if a conversion error occurs.
+     * Construct a <b>java.io.File</b> <i>Converter</i> that throws
+     * a <code>ConversionException</code> if an error occurs.
      */
     public FileConverter() {
-
-        this.defaultValue = null;
-        this.useDefault = false;
-
+        super(File.class);
     }
 
-
     /**
-     * Create a [EMAIL PROTECTED] Converter} that will return the specified 
default value
-     * if a conversion error occurs.
+     * Construct a <b>java.io.File</b> <i>Converter</i> that returns
+     * a default value if an error occurs.
      *
      * @param defaultValue The default value to be returned
+     * if the value to be converted is missing or an error
+     * occurs converting the value.
      */
     public FileConverter(Object defaultValue) {
-
-        this.defaultValue = defaultValue;
-        this.useDefault = true;
-
+        super(File.class, defaultValue);
     }
 
-
-
-    // --------------------------------------------------------- Public Methods
-
-
     /**
-     * Convert the specified input object into an output object of the
-     * specified type.
+     * <p>Convert the input object into a java.io.File.</p>
      *
-     * @param type Data type to which this value should be converted
-     * @param value The input value to be converted
-     *
-     * @exception ConversionException if conversion cannot be performed
-     *  successfully
+     * @param type Data type to which this value should be converted.
+     * @param value The input value to be converted.
+     * @return The converted value.
+     * @throws Exception if conversion cannot be performed successfully
      */
-    public Object convert(Class type, Object value) {
-
-        if (value == null) {
-            if (useDefault) {
-                return (defaultValue);
-            } else {
-                throw new ConversionException("No value specified");
-            }
-        }
-
-        if (value instanceof File) {
-            return (value);
-        }
-
+    protected Object convertToType(Class type, Object value) throws Exception {
         return new File(value.toString());
     }
 }

Modified: 
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/URLConverter.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/URLConverter.java?view=diff&rev=471350&r1=471349&r2=471350
==============================================================================
--- 
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/URLConverter.java
 (original)
+++ 
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/URLConverter.java
 Sat Nov  4 19:10:58 2006
@@ -14,115 +14,55 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */ 
-
-
 package org.apache.commons.beanutils.converters;
 
-
-import org.apache.commons.beanutils.ConversionException;
-import org.apache.commons.beanutils.Converter;
-
 import java.net.URL;
-import java.net.MalformedURLException;
-
 
+import org.apache.commons.beanutils.Converter;
 
 /**
- * <p>Standard [EMAIL PROTECTED] Converter} implementation that converts an 
incoming
- * String into a <code>java.net.URL</code> object, optionally using a
- * default value or throwing a [EMAIL PROTECTED] ConversionException} if a 
conversion
- * error occurs.</p>
+ * [EMAIL PROTECTED] Converter} implementaion that handles conversion
+ * to and from <b>java.net.URL</b> objects.
+ * <p>
+ * Can be configured to either return a <i>default value</i> or throw a
+ * <code>ConversionException</code> if a conversion error occurs.
  *
  * @author Henri Yandell
  * @version $Revision$ $Date$
  * @since 1.3
  */
-
-public final class URLConverter implements Converter {
-
-
-    // ----------------------------------------------------------- Constructors
-
+public final class URLConverter extends AbstractConverter {
 
     /**
-     * Create a [EMAIL PROTECTED] Converter} that will throw a [EMAIL 
PROTECTED] ConversionException}
-     * if a conversion error occurs.
+     * Construct a <b>java.net.URL</b> <i>Converter</i> that throws
+     * a <code>ConversionException</code> if an error occurs.
      */
     public URLConverter() {
-
-        this.defaultValue = null;
-        this.useDefault = false;
-
+        super(URL.class);
     }
 
-
     /**
-     * Create a [EMAIL PROTECTED] Converter} that will return the specified 
default value
-     * if a conversion error occurs.
+     * Construct a <b>java.net.URL</b> <i>Converter</i> that returns
+     * a default value if an error occurs.
      *
      * @param defaultValue The default value to be returned
+     * if the value to be converted is missing or an error
+     * occurs converting the value.
      */
     public URLConverter(Object defaultValue) {
-
-        this.defaultValue = defaultValue;
-        this.useDefault = true;
-
+        super(URL.class, defaultValue);
     }
 
-
-    // ----------------------------------------------------- Instance Variables
-
-
-    /**
-     * The default value specified to our Constructor, if any.
-     */
-    private Object defaultValue = null;
-
-
-    /**
-     * Should we return the default value on conversion errors?
-     */
-    private boolean useDefault = true;
-
-
-    // --------------------------------------------------------- Public Methods
-
-
     /**
-     * Convert the specified input object into an output object of the
-     * specified type.
+     * <p>Convert a java.net.URL or object into a String.</p>
      *
-     * @param type Data type to which this value should be converted
-     * @param value The input value to be converted
-     *
-     * @exception ConversionException if conversion cannot be performed
-     *  successfully
+     * @param type Data type to which this value should be converted.
+     * @param value The input value to be converted.
+     * @return The converted value.
+     * @throws Exception if conversion cannot be performed successfully
      */
-    public Object convert(Class type, Object value) {
-
-        if (value == null) {
-            if (useDefault) {
-                return (defaultValue);
-            } else {
-                throw new ConversionException("No value specified");
-            }
-        }
-
-        if (value instanceof URL) {
-            return (value);
-        }
-
-        try {
-            return new URL(value.toString());
-        } catch(MalformedURLException murle) {
-            if (useDefault) {
-                return (defaultValue);
-            } else {
-                throw new ConversionException(murle);
-            }
-        }
-
+    protected Object convertToType(Class type, Object value) throws Exception {
+        return new URL(value.toString());
     }
-
 
 }

Modified: 
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/BooleanConverterTestCase.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/BooleanConverterTestCase.java?view=diff&rev=471350&r1=471349&r2=471350
==============================================================================
--- 
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/BooleanConverterTestCase.java
 (original)
+++ 
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/BooleanConverterTestCase.java
 Sat Nov  4 19:10:58 2006
@@ -62,7 +62,7 @@
     }
 
     public void testDefaultValue() {
-        Object defaultValue = new Object();
+        Object defaultValue = Boolean.TRUE;
         BooleanConverter converter = new BooleanConverter(defaultValue);
 
         assertSame(defaultValue, converter.convert(Boolean.class, "bogus"));

Added: 
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/CharacterConverterTestCase.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/CharacterConverterTestCase.java?view=auto&rev=471350
==============================================================================
--- 
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/CharacterConverterTestCase.java
 (added)
+++ 
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/CharacterConverterTestCase.java
 Sat Nov  4 19:10:58 2006
@@ -0,0 +1,96 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.beanutils.converters;
+
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.apache.commons.beanutils.Converter;
+
+/**
+ * Test Case for the CharacterConverter class.
+ *
+ * @version $Revision$ $Date$
+ */
+public class CharacterConverterTestCase extends TestCase {
+
+    /**
+     * Construct a new Character Converter test case.
+     * @param name Test Name
+     */
+    public CharacterConverterTestCase(String name) {
+        super(name);
+    }
+    
+    // ------------------------------------------------------------------------
+
+    /**
+     * Create Test Suite
+     * @return test suite
+     */
+    public static TestSuite suite() {
+        return new TestSuite(CharacterConverterTestCase.class);        
+    }
+
+    /** Set Up */
+    public void setUp() throws Exception {
+    }
+
+    /** Tear Down */
+    public void tearDown() throws Exception {
+    }
+
+
+    // ------------------------------------------------------------------------
+
+    /**
+     * Test Conversion to String
+     */
+    public void testConvertToString() {
+
+        Converter converter = new CharacterConverter();
+
+        assertEquals("Character Test", "N", converter.convert(String.class, 
new Character('N')));
+        assertEquals("String Test",    "F", converter.convert(String.class, 
"FOO"));
+        assertEquals("Integer Test",   "3", converter.convert(String.class, 
new Integer(321)));
+        assertEquals("Null Test",     null, converter.convert(String.class, 
null));
+    }
+
+    /**
+     * Test Conversion to Character
+     */
+    public void testConvertToCharacter() {
+        Converter converter = new CharacterConverter();
+        assertEquals("Character Test", new Character('N'), 
converter.convert(Character.class, new Character('N')));
+        assertEquals("String Test",    new Character('F'), 
converter.convert(Character.class, "FOO"));
+        assertEquals("Integer Test",   new Character('3'), 
converter.convert(Character.class, new Integer(321)));
+        try {
+            converter.convert(Character.class, null);
+            fail("Expected a ConversionException for null value");
+        } catch (Exception e) {
+            // expected result
+        }
+    }
+
+    /**
+     * Test Conversion to Character (with default)
+     */
+    public void testDefault() {
+        Converter converter = new CharacterConverter("C");
+        assertEquals("Default Test",   new Character('C'), 
converter.convert(Character.class, null));
+    }
+}

Propchange: 
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/CharacterConverterTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/CharacterConverterTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: 
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/ClassConverterTestCase.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/ClassConverterTestCase.java?view=auto&rev=471350
==============================================================================
--- 
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/ClassConverterTestCase.java
 (added)
+++ 
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/ClassConverterTestCase.java
 Sat Nov  4 19:10:58 2006
@@ -0,0 +1,150 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.beanutils.converters;
+
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.apache.commons.beanutils.ConversionException;
+import org.apache.commons.beanutils.Converter;
+
+/**
+ * Test Case for the ClassConverter class.
+ *
+ * @version $Revision$ $Date$
+ */
+public class ClassConverterTestCase extends TestCase {
+
+    /**
+     * Construct a new Class Converter test case.
+     * @param name Test Name
+     */
+    public ClassConverterTestCase(String name) {
+        super(name);
+    }
+    
+    // ------------------------------------------------------------------------
+
+    /**
+     * Create Test Suite
+     * @return test suite
+     */
+    public static TestSuite suite() {
+        return new TestSuite(ClassConverterTestCase.class);        
+    }
+
+    /** Set Up */
+    public void setUp() throws Exception {
+    }
+
+    /** Tear Down */
+    public void tearDown() throws Exception {
+    }
+
+
+    // ------------------------------------------------------------------------
+
+    /**
+     * Test Conversion to String
+     */
+    public void testConvertToString() {
+        Converter converter = new ClassConverter();
+
+        assertEquals("Class Test", "java.lang.Integer", 
converter.convert(String.class, Integer.class));
+        assertEquals("Value Test", "foo", converter.convert(String.class, 
"foo"));
+        assertEquals("Value Test", "bar", converter.convert(String.class, new 
StringBuffer("bar")));
+        assertEquals("Null Test",   null, converter.convert(String.class, 
null));
+    }
+
+    /**
+     * Test Conversion to Class
+     */
+    public void testConvertToClass() {
+        Converter converter = new ClassConverter();
+
+        assertEquals("Class Test",        Integer.class, 
converter.convert(Class.class, Integer.class));
+        assertEquals("String Test",       Integer.class, 
converter.convert(Class.class, "java.lang.Integer"));
+        assertEquals("StringBuffer Test", Integer.class, 
converter.convert(Class.class, new StringBuffer("java.lang.Integer")));
+
+        // Invalid Test
+        try {
+            converter.convert(Class.class, new Integer(6));
+            fail("Expected invalid value to fail");
+        } catch (ConversionException e) {
+            // expected result
+        }
+
+        // Test Null
+        try {
+            converter.convert(Class.class, null);
+            fail("Expected null value to fail");
+        } catch (ConversionException e) {
+            // expected result
+        }
+    }
+
+    /**
+     * Test Invalid Conversion with default
+     */
+    public void testConvertToClassDefault() {
+
+        Converter converter = new ClassConverter(Object.class);
+
+        assertEquals("Invalid Test", Object.class, 
converter.convert(Class.class, new Integer(6)));
+        assertEquals("Null Test",    Object.class, 
converter.convert(Class.class, null));
+    }
+
+    /**
+     * Test Invalid Conversion with default "null"
+     */
+    public void testConvertToClassDefaultNull() {
+
+        Converter converter = new ClassConverter(null);
+
+        assertEquals("Invalid Test", null, converter.convert(Class.class, new 
Integer(6)));
+        assertEquals("Null Test",    null, converter.convert(Class.class, 
null));
+    }
+
+    /**
+     * Test Array Conversion
+     */
+    public void testArray() {
+        Converter converter = new ClassConverter();
+
+        // Test Array Class to String
+        assertEquals("Array to String", "[Ljava.lang.Boolean;", 
converter.convert(String.class, Boolean[].class));
+
+        // Test String to Array Class
+        assertEquals("String to Array", Boolean[].class, 
converter.convert(Class.class, "[Ljava.lang.Boolean;"));
+    }
+
+    /**
+     * Test Invalid
+     */
+    public void testInvalid() {
+        Converter converter = new ClassConverter();
+
+        // Test invalid class name
+        try {
+            converter.convert(Class.class, "foo.bar");
+            fail("Invalid class name, expected ConversionException");
+        } catch (ConversionException e) {
+            // expected result
+        }
+    }
+
+}

Propchange: 
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/ClassConverterTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/ClassConverterTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Modified: 
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/URLConverterTestCase.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/URLConverterTestCase.java?view=diff&rev=471350&r1=471349&r2=471350
==============================================================================
--- 
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/URLConverterTestCase.java
 (original)
+++ 
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/URLConverterTestCase.java
 Sat Nov  4 19:10:58 2006
@@ -106,6 +106,10 @@
             assertEquals(message[i] + " to 
URL",expected[i],converter.convert(URL.class,input[i]));
             assertEquals(message[i] + " to null 
type",expected[i],converter.convert(null,input[i]));
         }
+        
+        for(int i=0;i<expected.length;i++) {
+            assertEquals(input[i] + " to String", input[i], 
converter.convert(String.class, expected[i]));
+        }
     }
     
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to