You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oh...@apache.org on 2013/10/24 22:26:59 UTC

svn commit: r1535527 - in /commons/proper/beanutils/branches/java5/src: main/java/org/apache/commons/beanutils/converters/FileConverter.java test/java/org/apache/commons/beanutils/converters/FileConverterTestCase.java

Author: oheger
Date: Thu Oct 24 20:26:59 2013
New Revision: 1535527

URL: http://svn.apache.org/r1535527
Log:
Generified FileConverter.

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/FileConverter.java
    commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/FileConverterTestCase.java

Modified: commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/FileConverter.java
URL: http://svn.apache.org/viewvc/commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/FileConverter.java?rev=1535527&r1=1535526&r2=1535527&view=diff
==============================================================================
--- commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/FileConverter.java (original)
+++ commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/FileConverter.java Thu Oct 24 20:26:59 2013
@@ -57,13 +57,14 @@ public final class FileConverter extends
      * @since 1.8.0
      */
     @Override
-    protected Class getDefaultType() {
+    protected Class<?> getDefaultType() {
         return File.class;
     }
 
     /**
      * <p>Convert the input object into a java.io.File.</p>
      *
+     * @param <T> The target type of the conversion.
      * @param type Data type to which this value should be converted.
      * @param value The input value to be converted.
      * @return The converted value.
@@ -71,7 +72,11 @@ public final class FileConverter extends
      * @since 1.8.0
      */
     @Override
-    protected Object convertToType(Class type, Object value) throws Throwable {
-        return new File(value.toString());
+    protected <T> T convertToType(Class<T> type, Object value) throws Throwable {
+        if (File.class.equals(type)) {
+            return type.cast(new File(value.toString()));
+        }
+
+        throw conversionException(type, value);
     }
 }

Modified: commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/FileConverterTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/FileConverterTestCase.java?rev=1535527&r1=1535526&r2=1535527&view=diff
==============================================================================
--- commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/FileConverterTestCase.java (original)
+++ commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/FileConverterTestCase.java Thu Oct 24 20:26:59 2013
@@ -22,6 +22,7 @@ import java.io.File;
 import junit.framework.TestCase;
 import junit.framework.TestSuite;
 
+import org.apache.commons.beanutils.ConversionException;
 import org.apache.commons.beanutils.Converter;
 
 
@@ -63,7 +64,7 @@ public class FileConverterTestCase exten
         return new FileConverter();
     }
 
-    protected Class getExpectedType() {
+    protected Class<?> getExpectedType() {
         return File.class;
     }
 
@@ -94,5 +95,16 @@ public class FileConverterTestCase exten
         }
     }
 
+    /**
+     * Tries a conversion to an unsupported target type.
+     */
+    public void testUnsupportedTargetType() {
+        try {
+            converter.convert(Integer.class, "/tmp");
+            fail("Could convert to unsupported type!");
+        } catch (ConversionException cex) {
+            // expected result
+        }
+    }
 }