Author: henrib
Date: Wed Jul  5 10:19:27 2017
New Revision: 1800856

URL: http://svn.apache.org/viewvc?rev=1800856&view=rev
Log:
JEXL-236: modified arguments class checks & casts, added tests

Modified:
    
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/JexlArithmetic.java
    
commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/ArithmeticOperatorTest.java
    
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/JexlArithmetic.java
URL: 
http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/JexlArithmetic.java?rev=1800856&r1=1800855&r2=1800856&view=diff
==============================================================================
--- 
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/JexlArithmetic.java
 (original)
+++ 
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/JexlArithmetic.java
 Wed Jul  5 10:19:27 2017
@@ -846,7 +846,7 @@ public class JexlArithmetic {
         if (container instanceof java.util.regex.Pattern) {
             return ((java.util.regex.Pattern) 
container).matcher(value.toString()).matches();
         }
-        if (container instanceof String) {
+        if (container instanceof CharSequence) {
             return value.toString().matches(container.toString());
         }
         // try contains on map key
@@ -922,8 +922,8 @@ public class JexlArithmetic {
             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;
+        if (object instanceof CharSequence) {
+            return ((CharSequence) object).length() == 0 ? Boolean.TRUE : 
Boolean.FALSE;
         }
         if (object.getClass().isArray()) {
             return Array.getLength(object) == 0 ? Boolean.TRUE : Boolean.FALSE;
@@ -945,8 +945,8 @@ public class JexlArithmetic {
      * @return the size of object or null if there is no arithmetic solution
      */
     public Integer size(Object object) {
-        if (object instanceof String) {
-            return ((String) object).length();
+        if (object instanceof CharSequence) {
+            return ((CharSequence) object).length();
         }
         if (object.getClass().isArray()) {
             return Array.getLength(object);

Modified: 
commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/ArithmeticOperatorTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/ArithmeticOperatorTest.java?rev=1800856&r1=1800855&r2=1800856&view=diff
==============================================================================
--- 
commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/ArithmeticOperatorTest.java
 (original)
+++ 
commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/ArithmeticOperatorTest.java
 Wed Jul  5 10:19:27 2017
@@ -71,6 +71,12 @@ public class ArithmeticOperatorTest exte
         asserter.assertExpression("str !~ match", Boolean.FALSE);
         asserter.assertExpression("str !~ nomatch", Boolean.TRUE);
         asserter.assertExpression("str =~ nomatch", Boolean.FALSE);
+        asserter.setVariable("match", new StringBuilder("abc.*"));
+        asserter.setVariable("nomatch", new StringBuilder(".*123"));
+        asserter.assertExpression("str =~ match", Boolean.TRUE);
+        asserter.assertExpression("str !~ match", Boolean.FALSE);
+        asserter.assertExpression("str !~ nomatch", Boolean.TRUE);
+        asserter.assertExpression("str =~ nomatch", Boolean.FALSE);
         asserter.setVariable("match", 
java.util.regex.Pattern.compile("abc.*"));
         asserter.setVariable("nomatch", 
java.util.regex.Pattern.compile(".*123"));
         asserter.assertExpression("str =~ match", Boolean.TRUE);

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=1800856&r1=1800855&r2=1800856&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
 Wed Jul  5 10:19:27 2017
@@ -633,8 +633,12 @@ public class ArithmeticTest extends Jexl
             "var x = []; return empty(x);", true,
             "var x = [1, 2]; return empty(x);", false,
             "var x = ['a', 'b']; return empty(x);", false,
+            "var x = [...]; return empty(x);", true,
+            "var x = [1, 2,...]; return empty(x);", false,
             "var x = {:}; return empty(x);", true,
-            "var x = {1:'A', 2:'B'}; return empty(x);", false
+            "var x = {1:'A', 2:'B'}; return empty(x);", false,
+            "var x = {}; return empty(x);", true,
+            "var x = {'A','B'}; return empty(x);", false
         };
         JexlEngine jexl = new JexlBuilder().create();
         JexlContext jc = new EmptyTestContext();


Reply via email to