You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by br...@apache.org on 2014/08/25 17:26:22 UTC

svn commit: r1620345 - in /commons/sandbox/beanutils2/trunk/src: main/java/org/apache/commons/beanutils2/TransformerRegistry.java test/java/org/apache/commons/beanutils2/TransformerRegistryTestCase.java

Author: britter
Date: Mon Aug 25 15:26:22 2014
New Revision: 1620345

URL: http://svn.apache.org/r1620345
Log:
Better throw an exception if we're unable to transform from one type to the other

Modified:
    commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/TransformerRegistry.java
    commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/TransformerRegistryTestCase.java

Modified: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/TransformerRegistry.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/TransformerRegistry.java?rev=1620345&r1=1620344&r2=1620345&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/TransformerRegistry.java (original)
+++ commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/TransformerRegistry.java Mon Aug 25 15:26:22 2014
@@ -240,15 +240,14 @@ public class TransformerRegistry {
     public <S, T> Transformer<S, T> lookup(Class<S> sourceType,
             Class<T> targetType) {
 
-        Transformer<S, T> transformer = null;
         TransformerMapKey key = new TransformerMapKey(sourceType, targetType);
 
         if (transformers.containsKey(key)) {
-            transformer = (Transformer<S, T>) transformers.get(key);
+            return (Transformer<S, T>) transformers.get(key);
+        } else {
+            throw new TransformationException(String.format("Unable to transform from %s to %s.",
+                    sourceType.getName(), targetType.getName()));
         }
-
-        return transformer;
-
     }
 
 }

Modified: commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/TransformerRegistryTestCase.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/TransformerRegistryTestCase.java?rev=1620345&r1=1620344&r2=1620345&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/TransformerRegistryTestCase.java (original)
+++ commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/TransformerRegistryTestCase.java Mon Aug 25 15:26:22 2014
@@ -18,9 +18,11 @@ package org.apache.commons.beanutils2;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNull;
+import static org.junit.Assert.fail;
 
 import java.lang.reflect.Type;
 import java.util.ArrayList;
+import java.util.Date;
 import java.util.List;
 
 import org.apache.commons.beanutils2.testtransformers.TestStringFloatTransformer;
@@ -93,16 +95,25 @@ public class TransformerRegistryTestCase
                 getClass(transformerStringIntegerImpl.getTargetType())));
     }
 
+    @Test(expected = TransformationException.class)
+    public void testLookupThrowsExceptionIfNoTransformerFound() throws Exception {
+        registry.lookup(Date.class, String.class);
+    }
+
     /**
      * Tests for the deregister() method in TransformerRegistry
      */
     @Test
     public void testDeregister() {
-
         registry.deregister(transformerStringIntegerImpl);
-        assertNull(registry.lookup(
-                getClass(transformerStringIntegerImpl.getSourceType()),
-                getClass(transformerStringIntegerImpl.getTargetType())));
+        try {
+            registry.lookup(
+                    getClass(transformerStringIntegerImpl.getSourceType()),
+                    getClass(transformerStringIntegerImpl.getTargetType()));
+            fail("Expected TransformationException to be thrown!");
+        } catch (TransformationException expected) {
+            // expected
+        }
     }
 
     /**
@@ -111,9 +122,15 @@ public class TransformerRegistryTestCase
     @Test
     public void testDeregisterAll() {
         registry.deregisterAll();
-        assertNull(registry.lookup(
-                getClass(transformerStringIntegerImpl.getSourceType()),
-                getClass(transformerStringIntegerImpl.getTargetType())));
+
+        try {
+            registry.lookup(
+                    getClass(transformerStringIntegerImpl.getSourceType()),
+                    getClass(transformerStringIntegerImpl.getTargetType()));
+            fail("Expected TransformationException to be thrown!");
+        } catch (TransformationException expected) {
+            // expected
+        }
     }
 
     /**