commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hen...@apache.org
Subject svn commit: r1690529 - in /commons/proper/jexl/trunk/src: main/java/org/apache/commons/jexl3/internal/Interpreter.java main/java/org/apache/commons/jexl3/parser/Parser.jjt site/xdoc/changes.xml test/java/org/apache/commons/jexl3/ArithmeticTest.java
Date Sun, 12 Jul 2015 20:46:13 GMT
Author: henrib
Date: Sun Jul 12 20:46:13 2015
New Revision: 1690529

URL: http://svn.apache.org/r1690529
Log:
JEXL:
JEXL-163 fix, check for double 0.0d instead of long 0L for numbers, updated parser to do a
better job at parsing doubles, added test, updated changes.xml

Modified:
    commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/Interpreter.java
    commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/parser/Parser.jjt
    commons/proper/jexl/trunk/src/site/xdoc/changes.xml
    commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/ArithmeticTest.java

Modified: commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/Interpreter.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/Interpreter.java?rev=1690529&r1=1690528&r2=1690529&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/Interpreter.java
(original)
+++ commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/Interpreter.java
Sun Jul 12 20:46:13 2015
@@ -1227,16 +1227,8 @@ public class Interpreter extends ParserV
             return opcall;
         }
         if (object instanceof Number) {
-            Number number = (Number) object;
-            if (number instanceof Double) {
-                double value = number.doubleValue();
-                return value == 0. || Double.isNaN(value) ? Boolean.TRUE : Boolean.FALSE;
-            }
-            if (number instanceof Float) {
-                float value = number.floatValue();
-                return value == 0. || Float.isNaN(value) ? Boolean.TRUE : Boolean.FALSE;
-            }
-            return number.intValue() == 0 ? Boolean.TRUE : Boolean.FALSE;
+            double d = ((Number) object).doubleValue();
+            return Double.isNaN(d) || d == 0.d ? Boolean.TRUE : Boolean.FALSE;
         }
         if (object instanceof String) {
             return "".equals(object) ? Boolean.TRUE : Boolean.FALSE;

Modified: commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/parser/Parser.jjt
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/parser/Parser.jjt?rev=1690529&r1=1690528&r2=1690529&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/parser/Parser.jjt (original)
+++ commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/parser/Parser.jjt Sun
Jul 12 20:46:13 2015
@@ -202,7 +202,10 @@ PARSER_END(Parser)
   < FLOAT_LITERAL:
     "#NaN"
     |
-    (<DIGIT>)+ "." (<DIGIT>)+ ((["e","E"])(["+","-"])?(<DIGIT>)+)? (["d","D","f","F","b","B"])?
>
+    (<DIGIT>)+ "." (<DIGIT>)+ ((["e","E"])(["+","-"])?(<DIGIT>)+)? (["d","D","f","F","b","B"])?
+    |
+    (<DIGIT>)+ (".")? ((["e","E"])(["+","-"])?(<DIGIT>)+)? ["d","D","f","F","b","B"]
+>
 }
 
 <*> TOKEN :

Modified: commons/proper/jexl/trunk/src/site/xdoc/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/site/xdoc/changes.xml?rev=1690529&r1=1690528&r2=1690529&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/site/xdoc/changes.xml (original)
+++ commons/proper/jexl/trunk/src/site/xdoc/changes.xml Sun Jul 12 20:46:13 2015
@@ -26,6 +26,9 @@
     </properties>
     <body>
         <release version="3.0" date="unreleased">
+            <action dev="henrib" type="fix" issue="JEXL-163" due-to="Dmitri Blinov">
+                empty(new ("java.lang.Long", 4294967296)) returns true
+            </action>
             <action dev="henrib" type="fix" issue="JEXL-162" due-to="Dmitri Blinov">
                 empty() function throws an exception : unsupported type
             </action>

Modified: commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/ArithmeticTest.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/ArithmeticTest.java?rev=1690529&r1=1690528&r2=1690529&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/ArithmeticTest.java (original)
+++ commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/ArithmeticTest.java Sun
Jul 12 20:46:13 2015
@@ -22,6 +22,7 @@ import java.io.ByteArrayInputStream;
 import java.io.InputStream;
 import java.util.HashMap;
 import java.util.Map;
+
 import java.math.BigDecimal;
 import java.math.BigInteger;
 import javax.xml.parsers.DocumentBuilder;
@@ -34,6 +35,8 @@ import org.w3c.dom.Node;
 
 @SuppressWarnings({"UnnecessaryBoxing", "AssertEqualsBetweenInconvertibleTypes"})
 public class ArithmeticTest extends JexlTestCase {
+    /** A small delta to compare doubles. */
+    private static final double EPSILON = 1.e-6;
     private final Asserter asserter;
 
     public ArithmeticTest() {
@@ -833,4 +836,66 @@ public class ArithmeticTest extends Jexl
         size = (Integer) s1.execute(null, x);
         Assert.assertEquals(0, size);
     }
+
+    @Test
+    public void testEmptyLong() throws Exception {
+       Object x;
+       x = JEXL.createScript("new('java.lang.Long', 4294967296)").execute(null);
+       Assert.assertEquals(4294967296L, ((Long) x).longValue());
+       x = JEXL.createScript("new('java.lang.Long', '4294967296')").execute(null);
+       Assert.assertEquals(4294967296L, ((Long) x).longValue());
+       x = JEXL.createScript("4294967296l").execute(null);
+       Assert.assertEquals(4294967296L, ((Long) x).longValue());
+       x = JEXL.createScript("4294967296L").execute(null);
+       Assert.assertEquals(4294967296L, ((Long) x).longValue());
+       checkEmpty(x, false);
+       x = JEXL.createScript("0L").execute(null);
+       Assert.assertEquals(0, ((Long) x).longValue());
+       checkEmpty(x, true);
+    }
+
+    @Test
+    public void testEmptyFloat() throws Exception {
+       Object x;
+       x = JEXL.createScript("4294967296.f").execute(null);
+       Assert.assertEquals(4294967296.0f, (Float) x, EPSILON);
+       checkEmpty(x, false);
+       x = JEXL.createScript("4294967296.0f").execute(null);
+       Assert.assertEquals(4294967296.0f, (Float) x, EPSILON);
+       checkEmpty(x, false);
+       x = JEXL.createScript("0.0f").execute(null);
+       Assert.assertEquals(0.0f, (Float) x, EPSILON);
+       checkEmpty(x, true);
+       x = Float.NaN;
+       checkEmpty(x, true);
+    }
+
+    @Test
+    public void testEmptyDouble() throws Exception {
+       Object x;
+       x = JEXL.createScript("4294967296.d").execute(null);
+       Assert.assertEquals(4294967296.0d, (Double) x, EPSILON);
+       checkEmpty(x, false);
+       x = JEXL.createScript("4294967296.0d").execute(null);
+       Assert.assertEquals(4294967296.0d, (Double) x, EPSILON);
+       checkEmpty(x, false);
+       x = JEXL.createScript("0.0d").execute(null);
+       Assert.assertEquals(0.0d, (Double) x, EPSILON);
+       checkEmpty(x, true);
+       x = Double.NaN;
+       checkEmpty(x, true);
+
+    }
+
+    void checkEmpty(Object x, boolean expect) {
+        JexlScript s0 = JEXL.createScript("empty(x)", "x");
+        boolean empty = (Boolean) s0.execute(null, x);
+        Assert.assertEquals(expect, empty);
+        JexlScript s1 = JEXL.createScript("empty x", "x");
+        empty = (Boolean) s1.execute(null, x);
+        Assert.assertEquals(expect, empty);
+        JexlScript s2 = JEXL.createScript("x.empty()", "x");
+        empty = (Boolean) s2.execute(null, x);
+        Assert.assertEquals(expect, empty);
+    }
 }



Mime
View raw message