commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r1535153 - in /commons/proper/beanutils/branches/java5/src: main/java/org/apache/commons/beanutils/converters/CharacterConverter.java test/java/org/apache/commons/beanutils/converters/CharacterConverterTestCase.java
Date Wed, 23 Oct 2013 20:16:37 GMT
Author: oheger
Date: Wed Oct 23 20:16:37 2013
New Revision: 1535153

URL: http://svn.apache.org/r1535153
Log:
Generified CharacterConverter.

The converter now also checks whether it supports the passed in target type.

Modified:
    commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/CharacterConverter.java
    commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/CharacterConverterTestCase.java

Modified: commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/CharacterConverter.java
URL: http://svn.apache.org/viewvc/commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/CharacterConverter.java?rev=1535153&r1=1535152&r2=1535153&view=diff
==============================================================================
--- commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/CharacterConverter.java
(original)
+++ commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/CharacterConverter.java
Wed Oct 23 20:16:37 2013
@@ -16,6 +16,8 @@
  */
 package org.apache.commons.beanutils.converters;
 
+import org.apache.commons.beanutils.ConversionException;
+
 /**
  * {@link org.apache.commons.beanutils.Converter} implementaion that handles conversion
  * to and from <b>java.lang.Character</b> objects.
@@ -55,7 +57,7 @@ public final class CharacterConverter ex
      * @since 1.8.0
      */
     @Override
-    protected Class getDefaultType() {
+    protected Class<?> getDefaultType() {
         return Character.class;
     }
 
@@ -82,8 +84,13 @@ public final class CharacterConverter ex
      * @since 1.8.0
      */
     @Override
-    protected Object convertToType(Class type, Object value) throws Exception {
-        return new Character(value.toString().charAt(0));
+    protected <T> T convertToType(Class<T> type, Object value) throws Exception
{
+        if (Character.class.equals(type) || Character.TYPE.equals(type)) {
+            return type.cast(new Character(value.toString().charAt(0)));
+        }
+
+        throw new ConversionException("Can't convert value '" + value
+                + "' to type " + type);
     }
 
 }

Modified: commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/CharacterConverterTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/CharacterConverterTestCase.java?rev=1535153&r1=1535152&r2=1535153&view=diff
==============================================================================
--- commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/CharacterConverterTestCase.java
(original)
+++ commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/CharacterConverterTestCase.java
Wed Oct 23 20:16:37 2013
@@ -19,6 +19,7 @@ package org.apache.commons.beanutils.con
 import junit.framework.TestCase;
 import junit.framework.TestSuite;
 
+import org.apache.commons.beanutils.ConversionException;
 import org.apache.commons.beanutils.Converter;
 
 /**
@@ -80,6 +81,22 @@ public class CharacterConverterTestCase 
         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)));
+    }
+
+    /**
+     * Tests whether the primitive char class can be passed as target type.
+     */
+    public void testConvertToChar() {
+        Converter converter = new CharacterConverter();
+        assertEquals("Wrong result", new Character('F'), converter.convert(Character.TYPE,
"FOO"));
+    }
+
+    /**
+     * Tests a conversion to character for null input if no default value is
+     * provided.
+     */
+    public void testConvertToCharacterNullNoDefault() {
+        Converter converter = new CharacterConverter();
         try {
             converter.convert(Character.class, null);
             fail("Expected a ConversionException for null value");
@@ -95,4 +112,17 @@ public class CharacterConverterTestCase 
         Converter converter = new CharacterConverter("C");
         assertEquals("Default Test",   new Character('C'), converter.convert(Character.class,
null));
     }
+
+    /**
+     * Tries a conversion to an unsupported type.
+     */
+    public void testConvertToUnsupportedType() {
+        Converter converter = new CharacterConverter();
+        try {
+            converter.convert(Integer.class, "Test");
+            fail("Could convert to unsupported type!");
+        } catch (ConversionException cex) {
+            // expected result
+        }
+    }
 }



Mime
View raw message