Author: oheger
Date: Wed Oct  1 20:12:24 2014
New Revision: 1628829

URL: http://svn.apache.org/r1628829
Log:
[BEANUTILS-470] NumberConverter now deals with BigDecimal objects correctly.

If a BigDecimal object is passed to the converter it is now directly returned
without trying a conversion that will cause a loss of precision.

Thanks to Tommy Tynjä for the patch.

Modified:
    
commons/proper/beanutils/trunk/src/main/java/org/apache/commons/beanutils/converters/NumberConverter.java
    
commons/proper/beanutils/trunk/src/test/java/org/apache/commons/beanutils/converters/BigDecimalConverterTestCase.java

Modified: 
commons/proper/beanutils/trunk/src/main/java/org/apache/commons/beanutils/converters/NumberConverter.java
URL: 
http://svn.apache.org/viewvc/commons/proper/beanutils/trunk/src/main/java/org/apache/commons/beanutils/converters/NumberConverter.java?rev=1628829&r1=1628828&r2=1628829&view=diff
==============================================================================
--- 
commons/proper/beanutils/trunk/src/main/java/org/apache/commons/beanutils/converters/NumberConverter.java
 (original)
+++ 
commons/proper/beanutils/trunk/src/main/java/org/apache/commons/beanutils/converters/NumberConverter.java
 Wed Oct  1 20:12:24 2014
@@ -371,6 +371,8 @@ public abstract class NumberConverter ex
                 return targetType.cast(new BigDecimal(value.toString()));
             } else if (value instanceof BigInteger) {
                 return targetType.cast(new BigDecimal((BigInteger)value));
+            } else if (value instanceof BigDecimal) {
+                return targetType.cast(new BigDecimal(value.toString()));
             } else {
                 return targetType.cast(BigDecimal.valueOf(value.longValue()));
             }

Modified: 
commons/proper/beanutils/trunk/src/test/java/org/apache/commons/beanutils/converters/BigDecimalConverterTestCase.java
URL: 
http://svn.apache.org/viewvc/commons/proper/beanutils/trunk/src/test/java/org/apache/commons/beanutils/converters/BigDecimalConverterTestCase.java?rev=1628829&r1=1628828&r2=1628829&view=diff
==============================================================================
--- 
commons/proper/beanutils/trunk/src/test/java/org/apache/commons/beanutils/converters/BigDecimalConverterTestCase.java
 (original)
+++ 
commons/proper/beanutils/trunk/src/test/java/org/apache/commons/beanutils/converters/BigDecimalConverterTestCase.java
 Wed Oct  1 20:12:24 2014
@@ -20,7 +20,6 @@ package org.apache.commons.beanutils.con
 import java.math.BigDecimal;
 
 import junit.framework.TestSuite;
-
 import org.apache.commons.beanutils.Converter;
 
 
@@ -91,7 +90,9 @@ public class BigDecimalConverterTestCase
             "from Integer",
             "from Long",
             "from Float",
-            "from Double"
+            "from Double",
+            "from BigDecimal",
+            "from BigDecimal extension"
         };
 
         Object[] input = {
@@ -105,7 +106,9 @@ public class BigDecimalConverterTestCase
             new Integer(9),
             new Long(10),
             new Float("11.1"),
-            new Double("12.2")
+            new Double("12.2"),
+            new BigDecimal("3200.11"),
+            new ExtendingBigDecimal("3200.11")
         };
 
         BigDecimal[] expected = {
@@ -119,7 +122,9 @@ public class BigDecimalConverterTestCase
             new BigDecimal("9"),
             new BigDecimal("10"),
             new BigDecimal("11.1"),
-            new BigDecimal("12.2")
+            new BigDecimal("12.2"),
+            new BigDecimal("3200.11"),
+            new BigDecimal("3200.11")
         };
 
         for(int i=0;i<expected.length;i++) {
@@ -134,5 +139,14 @@ public class BigDecimalConverterTestCase
         }
     }
 
+    /**
+     * A class derived from {@code BigDecimal} used for testing whether
+     * derived number classes are handled correctly.
+     */
+    private class ExtendingBigDecimal extends BigDecimal {
+        private ExtendingBigDecimal(String val) {
+            super(val);
+        }
+    }
 }
 


Reply via email to