You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by se...@apache.org on 2009/08/06 14:02:58 UTC
svn commit: r801617 - in /commons/proper/jexl/branches/2.0:
src/main/java/org/apache/commons/jexl/Interpreter.java
src/test/java/org/apache/commons/jexl/AssignTest.java
src/test/java/org/apache/commons/jexl/IfTest.java xdocs/reference/syntax.xml
Author: sebb
Date: Thu Aug 6 12:02:57 2009
New Revision: 801617
URL: http://svn.apache.org/viewvc?rev=801617&view=rev
Log:
JEXL-78 Ternary operator throws Exception when JexlEngine in strict mode
Modified:
commons/proper/jexl/branches/2.0/src/main/java/org/apache/commons/jexl/Interpreter.java
commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/AssignTest.java
commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/IfTest.java
commons/proper/jexl/branches/2.0/xdocs/reference/syntax.xml
Modified: commons/proper/jexl/branches/2.0/src/main/java/org/apache/commons/jexl/Interpreter.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/branches/2.0/src/main/java/org/apache/commons/jexl/Interpreter.java?rev=801617&r1=801616&r2=801617&view=diff
==============================================================================
--- commons/proper/jexl/branches/2.0/src/main/java/org/apache/commons/jexl/Interpreter.java (original)
+++ commons/proper/jexl/branches/2.0/src/main/java/org/apache/commons/jexl/Interpreter.java Thu Aug 6 12:02:57 2009
@@ -1061,7 +1061,7 @@
public Object visit(ASTTernaryNode node, Object data) {
Object condition = node.jjtGetChild(0).jjtAccept(this, data);
if (node.jjtGetNumChildren() == 3) {
- if (arithmetic.toBoolean(condition)) {
+ if (condition != null && arithmetic.toBoolean(condition)) {
return node.jjtGetChild(1).jjtAccept(this, data);
} else {
return node.jjtGetChild(2).jjtAccept(this, data);
Modified: commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/AssignTest.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/AssignTest.java?rev=801617&r1=801616&r2=801617&view=diff
==============================================================================
--- commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/AssignTest.java (original)
+++ commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/AssignTest.java Thu Aug 6 12:02:57 2009
@@ -148,32 +148,6 @@
o = ENGINE.getProperty(quux, "['froboz']['value']");
assertEquals("Result is not 1000", new Integer(1000), o);
}
-
-
- public void testTernary() throws Exception {
- JexlContext jc = JexlHelper.createContext();
- Expression e = ENGINE.createExpression("x.y.z = foo ?'bar':'quux'");
- String canonical = e.dump();
- System.out.print(canonical);
- Object o = e.evaluate(jc);
- assertEquals("Should be quux", "quux", o);
- jc.getVars().put("foo",Boolean.TRUE);
- o = e.evaluate(jc);
- assertEquals("Should be bar", "bar", o);
- o = jc.getVars().get("x.y.z");
- assertEquals("Should be bar", "bar", o);
- }
-
- public void testNotNull() throws Exception {
- JexlContext jc = JexlHelper.createContext();
- Expression e = ENGINE.createExpression("x.y.z = foo?:'quux'");
- Object o = e.evaluate(jc);
- assertEquals("Should be quux", "quux", o);
- jc.getVars().put("foo","bar");
- o = e.evaluate(jc);
- assertEquals("Should be bar", "bar", o);
- o = jc.getVars().get("x.y.z");
- assertEquals("Should be bar", "bar", o);
- }
+
}
\ No newline at end of file
Modified: commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/IfTest.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/IfTest.java?rev=801617&r1=801616&r2=801617&view=diff
==============================================================================
--- commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/IfTest.java (original)
+++ commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/IfTest.java Thu Aug 6 12:02:57 2009
@@ -158,4 +158,116 @@
Object result = jc.getVars().get("y");
assertEquals("y has the wrong value", new Integer(1), result);
}
+
+ /**
+ * Ternary operator condition undefined or null evaluates to false
+ * independantly of engine flags.
+ * @throws Exception
+ */
+ public void testTernary() throws Exception {
+ JexlEngine jexl = new JexlEngine();
+ JexlContext jc = JexlHelper.createContext();
+ Expression e = jexl.createExpression("x.y.z = foo ?'bar':'quux'");
+ Object o;
+
+ // undefined foo
+
+ for(int l = 0; l < 4; ++l) {
+ jexl.setLenient((l & 1) != 0);
+ jexl.setSilent((l & 2) != 0);
+ o = e.evaluate(jc);
+ assertEquals("Should be quux", "quux", o);
+ o = jc.getVars().get("x.y.z");
+ assertEquals("Should be quux", "quux", o);
+ }
+
+ jc.getVars().put("foo", null);
+
+ for(int l = 0; l < 4; ++l) {
+ jexl.setLenient((l & 1) != 0);
+ jexl.setSilent((l & 2) != 0);
+ o = e.evaluate(jc);
+ assertEquals("Should be quux", "quux", o);
+ o = jc.getVars().get("x.y.z");
+ assertEquals("Should be quux", "quux", o);
+ }
+
+ jc.getVars().put("foo",Boolean.FALSE);
+
+ for(int l = 0; l < 4; ++l) {
+ jexl.setLenient((l & 1) != 0);
+ jexl.setSilent((l & 2) != 0);
+ o = e.evaluate(jc);
+ assertEquals("Should be quux", "quux", o);
+ o = jc.getVars().get("x.y.z");
+ assertEquals("Should be quux", "quux", o);
+ }
+
+ jc.getVars().put("foo",Boolean.TRUE);
+
+ for(int l = 0; l < 4; ++l) {
+ jexl.setLenient((l & 1) != 0);
+ jexl.setSilent((l & 2) != 0);
+ o = e.evaluate(jc);
+ assertEquals("Should be bar", "bar", o);
+ o = jc.getVars().get("x.y.z");
+ assertEquals("Should be bar", "bar", o);
+ }
+ }
+
+ /**
+ * Ternary operator condition undefined or null evaluates to false
+ * independantly of engine flags.
+ * @throws Exception
+ */
+ public void testTernaryShorthand() throws Exception {
+ JexlEngine jexl = new JexlEngine();
+ JexlContext jc = JexlHelper.createContext();
+ Expression e = JEXL.createExpression("x.y.z = foo?:'quux'");
+ Object o;
+
+ // undefined foo
+
+ for(int l = 0; l < 4; ++l) {
+ jexl.setLenient((l & 1) != 0);
+ jexl.setSilent((l & 2) != 0);
+ o = e.evaluate(jc);
+ assertEquals("Should be quux", "quux", o);
+ o = jc.getVars().get("x.y.z");
+ assertEquals("Should be quux", "quux", o);
+ }
+
+ jc.getVars().put("foo", null);
+
+ for(int l = 0; l < 4; ++l) {
+ jexl.setLenient((l & 1) != 0);
+ jexl.setSilent((l & 2) != 0);
+ o = e.evaluate(jc);
+ assertEquals("Should be quux", "quux", o);
+ o = jc.getVars().get("x.y.z");
+ assertEquals("Should be quux", "quux", o);
+ }
+
+ jc.getVars().put("foo", Boolean.FALSE);
+
+ for(int l = 0; l < 4; ++l) {
+ jexl.setLenient((l & 1) != 0);
+ jexl.setSilent((l & 2) != 0);
+ o = e.evaluate(jc);
+ assertEquals("Should be quux", "quux", o);
+ o = jc.getVars().get("x.y.z");
+ assertEquals("Should be quux", "quux", o);
+ }
+
+ jc.getVars().put("foo","bar");
+
+ for(int l = 0; l < 4; ++l) {
+ jexl.setLenient((l & 1) != 0);
+ jexl.setSilent((l & 2) != 0);
+ o = e.evaluate(jc);
+ assertEquals("Should be bar", "bar", o);
+ o = jc.getVars().get("x.y.z");
+ assertEquals("Should be bar", "bar", o);
+ }
+ }
}
Modified: commons/proper/jexl/branches/2.0/xdocs/reference/syntax.xml
URL: http://svn.apache.org/viewvc/commons/proper/jexl/branches/2.0/xdocs/reference/syntax.xml?rev=801617&r1=801616&r2=801617&view=diff
==============================================================================
--- commons/proper/jexl/branches/2.0/xdocs/reference/syntax.xml (original)
+++ commons/proper/jexl/branches/2.0/xdocs/reference/syntax.xml Thu Aug 6 12:02:57 2009
@@ -294,11 +294,17 @@
<tr>
<td>Ternary conditional <code>?:</code> </td>
<td>
- The usual ternary conditional operator <code>condition? if_true : if_null_or_false;</code> operator can be used as well as
- the abbreviation <code>value ?: if_null_or_false</code> which returns the <code>value</code> if
- it is non-null and non-false, e.g.
+ The usual ternary conditional operator <code>condition ? if_true : if_false</code> operator can be
+ used as well as the abbreviation <code>value ?: if_false</code> which returns the <code>value</code> if
+ its evaluation is defined, non-null and non-false, e.g.
<source>val1 ? val1 : val2</source> and
<source>val1 ?: val2 </source> are equivalent.
+ <p>
+ <strong>NOTE:</strong> The condition will evaluate to <code>false</code> when it
+ refers to an undefined variable or <code>null</code> for all <code>JexlEngine</code>
+ flag combinations. This allows explicit syntactic leniency and treats the condition
+ 'if undefined or null or false' the same way in all cases.
+ </p>
</td>
</tr>
<tr>