camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1294588 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/converter/ main/java/org/apache/camel/impl/converter/ main/java/org/apache/camel/util/ test/java/org/apache/camel/builder/xml/ test/java/org/apache/camel/converter/
Date Tue, 28 Feb 2012 11:28:35 GMT
Author: davsclaus
Date: Tue Feb 28 11:28:34 2012
New Revision: 1294588

URL: http://svn.apache.org/viewvc?rev=1294588&view=rev
Log:
CAMEL-4959: Type convertion with NaN numbers requires a bit special handling.

Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/converter/ObjectConverter.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/BaseTypeConverterRegistry.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/ExtractXPathWithNamespaceTest.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/converter/ObjectConverterNaNTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/converter/ObjectConverter.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/ObjectConverter.java?rev=1294588&r1=1294587&r2=1294588&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/converter/ObjectConverter.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/converter/ObjectConverter.java Tue
Feb 28 11:28:34 2012
@@ -80,10 +80,6 @@ public final class ObjectConverter {
         if (value instanceof Byte) {
             return (Byte) value;
         } else if (value instanceof Number) {
-            if (isNaN(value)) {
-                // return zero for a NaN value
-                return Byte.valueOf((byte) 0);
-            }
             Number number = (Number) value;
             return number.byteValue();
         } else if (value instanceof String) {
@@ -139,10 +135,6 @@ public final class ObjectConverter {
         if (value instanceof Short) {
             return (Short) value;
         } else if (value instanceof Number) {
-            if (isNaN(value)) {
-                // return zero for a NaN value
-                return Short.valueOf((short) 0);
-            }
             Number number = (Number) value;
             return number.shortValue();
         } else if (value instanceof String) {
@@ -160,10 +152,6 @@ public final class ObjectConverter {
         if (value instanceof Integer) {
             return (Integer) value;
         } else if (value instanceof Number) {
-            if (isNaN(value)) {
-                // return zero for a NaN value
-                return Integer.valueOf(0);
-            }
             Number number = (Number) value;
             return number.intValue();
         } else if (value instanceof String) {
@@ -181,10 +169,6 @@ public final class ObjectConverter {
         if (value instanceof Long) {
             return (Long) value;
         } else if (value instanceof Number) {
-            if (isNaN(value)) {
-                // return zero for a NaN value
-                return Long.valueOf(0);
-            }
             Number number = (Number) value;
             return number.longValue();
         } else if (value instanceof String) {
@@ -202,7 +186,7 @@ public final class ObjectConverter {
         if (value instanceof Float) {
             return (Float) value;
         } else if (value instanceof Number) {
-            if (isNaN(value)) {
+            if (ObjectHelper.isNaN(value)) {
                 return Float.NaN;
             }
             Number number = (Number) value;
@@ -222,7 +206,7 @@ public final class ObjectConverter {
         if (value instanceof Double) {
             return (Double) value;
         } else if (value instanceof Number) {
-            if (isNaN(value)) {
+            if (ObjectHelper.isNaN(value)) {
                 return Double.NaN;
             }
             Number number = (Number) value;
@@ -234,11 +218,6 @@ public final class ObjectConverter {
         }
     }
 
-    private static boolean isNaN(Object value) {
-        // 'value' should have been already checked to be 'instanceof Number', that's 'value
!= null'
-        return value.equals(Float.NaN) || value.equals(Double.NaN);
-    }
-
     // add fast type converters from most common used
 
     @Converter

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/BaseTypeConverterRegistry.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/BaseTypeConverterRegistry.java?rev=1294588&r1=1294587&r2=1294588&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/BaseTypeConverterRegistry.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/BaseTypeConverterRegistry.java
Tue Feb 28 11:28:34 2012
@@ -176,6 +176,18 @@ public abstract class BaseTypeConverterR
             // we have tried before but we cannot convert this one
             return Void.TYPE;
         }
+        
+        // special for NaN numbers, which we can only convert for flating numbers
+        if (ObjectHelper.isNaN(value)) {
+            if (Float.class.isAssignableFrom(type)) {
+                return Float.NaN;
+            } else if (Double.class.isAssignableFrom(type)) {
+                return Double.NaN;
+            } else {
+                // we cannot convert the NaN
+                return Void.TYPE;
+            }
+        }
 
         // try to find a suitable type converter
         TypeConverter converter = getOrFindTypeConverter(type, value);

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java?rev=1294588&r1=1294587&r2=1294588&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java Tue Feb 28
11:28:34 2012
@@ -1377,6 +1377,19 @@ public final class ObjectHelper {
         return null;
     }
 
+    /**
+     * Is the given value a numeric NaN type
+     * 
+     * @param value the value
+     * @return <tt>true</tt> if its a {@link Float#NaN} or {@link Double#NaN}.
+     */
+    public static boolean isNaN(Object value) {
+        if (value == null) {
+            return false;
+        }
+        return value.equals(Float.NaN) || value.equals(Double.NaN);
+    }
+
     private static final class ExceptionIterator implements Iterator<Throwable> {
         private List<Throwable> tree = new ArrayList<Throwable>();
         private Iterator<Throwable> it;

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/ExtractXPathWithNamespaceTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/ExtractXPathWithNamespaceTest.java?rev=1294588&r1=1294587&r2=1294588&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/ExtractXPathWithNamespaceTest.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/ExtractXPathWithNamespaceTest.java
Tue Feb 28 11:28:34 2012
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.builder.xml;
 
+import junit.framework.Assert;
+
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
@@ -48,11 +50,11 @@ public class ExtractXPathWithNamespaceTe
     public void testXPathWithNamespaceDifferentNamespace() throws Exception {
         MockEndpoint mock = getMockEndpoint("mock:result");
         mock.expectedBodiesReceived("<number xmlns=\"http://acme.com/cake\">55</number>");
-        mock.expectedHeaderReceived("foo", 0);
-        
+
         template.sendBody("direct:in", "<number xmlns=\"http://acme.com/cake\">55</number>");
 
         mock.assertIsSatisfied();
+        Assert.assertNull(mock.getExchanges().get(0).getIn().getHeader("foo"));
     }
 
     protected RouteBuilder createRouteBuilder() throws Exception {

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/ObjectConverterNaNTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/ObjectConverterNaNTest.java?rev=1294588&r1=1294587&r2=1294588&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/converter/ObjectConverterNaNTest.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/converter/ObjectConverterNaNTest.java
Tue Feb 28 11:28:34 2012
@@ -30,50 +30,61 @@ public class ObjectConverterNaNTest exte
 
     public void testDoubleToLongWithNaN() throws Exception {
         assertEquals(Long.valueOf("4"), context.getTypeConverter().convertTo(Long.class,
Double.valueOf("4")));
-        assertEquals(Long.valueOf("0"), context.getTypeConverter().convertTo(Long.class,
Double.NaN));
+        assertEquals(null, context.getTypeConverter().convertTo(Long.class, Double.NaN));
         assertEquals(Long.valueOf("3"), context.getTypeConverter().convertTo(Long.class,
Double.valueOf("3")));
     }
 
     public void testFloatToLongWithNaN() throws Exception {
         assertEquals(Long.valueOf("4"), context.getTypeConverter().convertTo(Long.class,
Float.valueOf("4")));
-        assertEquals(Long.valueOf("0"), context.getTypeConverter().convertTo(Long.class,
Float.NaN));
+        assertEquals(null, context.getTypeConverter().convertTo(Long.class, Float.NaN));
         assertEquals(Long.valueOf("3"), context.getTypeConverter().convertTo(Long.class,
Float.valueOf("3")));
     }
 
     public void testDoubleToIntegerWithNaN() throws Exception {
         assertEquals(Integer.valueOf("4"), context.getTypeConverter().convertTo(Integer.class,
Double.valueOf("4")));
-        assertEquals(Integer.valueOf("0"), context.getTypeConverter().convertTo(Integer.class,
Double.NaN));
+        assertEquals(null, context.getTypeConverter().convertTo(Integer.class, Double.NaN));
         assertEquals(Integer.valueOf("3"), context.getTypeConverter().convertTo(Integer.class,
Double.valueOf("3")));
     }
 
     public void testFloatToIntegerWithNaN() throws Exception {
         assertEquals(Integer.valueOf("4"), context.getTypeConverter().convertTo(Integer.class,
Float.valueOf("4")));
-        assertEquals(Integer.valueOf("0"), context.getTypeConverter().convertTo(Integer.class,
Float.NaN));
+        assertEquals(null, context.getTypeConverter().convertTo(Integer.class, Float.NaN));
         assertEquals(Integer.valueOf("3"), context.getTypeConverter().convertTo(Integer.class,
Float.valueOf("3")));
     }
 
     public void testDoubleToShortWithNaN() throws Exception {
         assertEquals(Short.valueOf("4"), context.getTypeConverter().convertTo(Short.class,
Double.valueOf("4")));
-        assertEquals(Short.valueOf("0"), context.getTypeConverter().convertTo(Short.class,
Double.NaN));
+        assertEquals(null, context.getTypeConverter().convertTo(Short.class, Double.NaN));
         assertEquals(Short.valueOf("3"), context.getTypeConverter().convertTo(Short.class,
Double.valueOf("3")));
     }
 
     public void testFloatToShortWithNaN() throws Exception {
         assertEquals(Short.valueOf("4"), context.getTypeConverter().convertTo(Short.class,
Float.valueOf("4")));
-        assertEquals(Short.valueOf("0"), context.getTypeConverter().convertTo(Short.class,
Float.NaN));
+        assertEquals(null, context.getTypeConverter().convertTo(Short.class, Float.NaN));
         assertEquals(Short.valueOf("3"), context.getTypeConverter().convertTo(Short.class,
Float.valueOf("3")));
     }
 
     public void testDoubleToByteWithNaN() throws Exception {
         assertEquals(Byte.valueOf("4"), context.getTypeConverter().convertTo(Byte.class,
Double.valueOf("4")));
-        assertEquals(Byte.valueOf("0"), context.getTypeConverter().convertTo(Byte.class,
Double.NaN));
+        assertEquals(null, context.getTypeConverter().convertTo(Byte.class, Double.NaN));
         assertEquals(Byte.valueOf("3"), context.getTypeConverter().convertTo(Byte.class,
Double.valueOf("3")));
     }
 
     public void testFloatToByteWithNaN() throws Exception {
         assertEquals(Byte.valueOf("4"), context.getTypeConverter().convertTo(Byte.class,
Float.valueOf("4")));
-        assertEquals(Byte.valueOf("0"), context.getTypeConverter().convertTo(Byte.class,
Float.NaN));
+        assertEquals(null, context.getTypeConverter().convertTo(Byte.class, Float.NaN));
         assertEquals(Byte.valueOf("3"), context.getTypeConverter().convertTo(Byte.class,
Float.valueOf("3")));
     }
 
+    public void testDoubleToFloatWithNaN() throws Exception {
+        assertEquals(Float.valueOf("4"), context.getTypeConverter().convertTo(Float.class,
Double.valueOf("4")));
+        assertEquals(Float.NaN, context.getTypeConverter().convertTo(Float.class, Double.NaN));
+        assertEquals(Float.valueOf("3"), context.getTypeConverter().convertTo(Float.class,
Double.valueOf("3")));
+    }
+
+    public void testFloatToDoubleWithNaN() throws Exception {
+        assertEquals(Double.valueOf("4"), context.getTypeConverter().convertTo(Double.class,
Float.valueOf("4")));
+        assertEquals(Double.NaN, context.getTypeConverter().convertTo(Double.class, Float.NaN));
+        assertEquals(Double.valueOf("3"), context.getTypeConverter().convertTo(Double.class,
Float.valueOf("3")));
+    }
 }



Mime
View raw message