Author: oheger
Date: Thu Oct 24 20:28:12 2013
New Revision: 1535529

URL: http://svn.apache.org/r1535529
Log:
Generified URLConverter.

The class now also checks whether the target type is supported.

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

Modified: 
commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/URLConverter.java
URL: 
http://svn.apache.org/viewvc/commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/URLConverter.java?rev=1535529&r1=1535528&r2=1535529&view=diff
==============================================================================
--- 
commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/URLConverter.java
 (original)
+++ 
commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/URLConverter.java
 Thu Oct 24 20:28:12 2013
@@ -57,7 +57,7 @@ public final class URLConverter extends 
      * @since 1.8.0
      */
     @Override
-    protected Class getDefaultType() {
+    protected Class<?> getDefaultType() {
         return URL.class;
     }
 
@@ -71,8 +71,12 @@ public final class URLConverter extends 
      * @since 1.8.0
      */
     @Override
-    protected Object convertToType(Class type, Object value) throws Throwable {
-        return new URL(value.toString());
+    protected <T> T convertToType(Class<T> type, Object value) throws 
Throwable {
+        if (URL.class.equals(type)) {
+            return type.cast(new URL(value.toString()));
+        }
+
+        throw conversionException(type, value);
     }
 
 }

Modified: 
commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/URLConverterTestCase.java
URL: 
http://svn.apache.org/viewvc/commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/URLConverterTestCase.java?rev=1535529&r1=1535528&r2=1535529&view=diff
==============================================================================
--- 
commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/URLConverterTestCase.java
 (original)
+++ 
commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/URLConverterTestCase.java
 Thu Oct 24 20:28:12 2013
@@ -17,13 +17,14 @@
 
 package org.apache.commons.beanutils.converters;
 
-import junit.framework.TestSuite;
+import java.net.URL;
+
 import junit.framework.TestCase;
+import junit.framework.TestSuite;
 
+import org.apache.commons.beanutils.ConversionException;
 import org.apache.commons.beanutils.Converter;
 
-import java.net.URL;
-
 
 /**
  * Test Case for the URLConverter class.
@@ -63,7 +64,7 @@ public class URLConverterTestCase extend
         return new URLConverter();
     }
 
-    protected Class getExpectedType() {
+    protected Class<?> getExpectedType() {
         return URL.class;
     }
 
@@ -113,5 +114,16 @@ public class URLConverterTestCase extend
         }
     }
 
+    /**
+     * Tests a conversion to an unsupported type.
+     */
+    public void testUnsupportedType() {
+        try {
+            converter.convert(Integer.class, "http://www.apache.org";);
+            fail("Unsupported type could be converted!");
+        } catch (ConversionException cex) {
+            // expected result
+        }
+    }
 }
 


Reply via email to