Modified: commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/ExceptionTest.java URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/ExceptionTest.java?rev=1628650&r1=1628649&r2=1628650&view=diff ============================================================================== --- commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/ExceptionTest.java (original) +++ commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/ExceptionTest.java Wed Oct 1 08:57:12 2014 @@ -58,7 +58,7 @@ public class ExceptionTest extends JexlT // empty cotext try { /* Object o = */ e.evaluate(ctxt); - fail("c.e not declared as variable should throw"); + fail("c.e not defined as variable should throw"); } catch (JexlException.Variable xjexl) { String msg = xjexl.getMessage(); assertTrue(msg.indexOf("c.e") > 0); @@ -70,9 +70,9 @@ public class ExceptionTest extends JexlT try { /* Object o = */ e.evaluate(ctxt); fail("c.e as null operand should throw"); - } catch (JexlException xjexl) { + } catch (JexlException.Variable xjexl) { String msg = xjexl.getMessage(); - assertTrue(msg.indexOf("null operand") > 0); + assertTrue(msg.indexOf("c.e") > 0); } // allow null operands @@ -96,4 +96,61 @@ public class ExceptionTest extends JexlT } } + // null local vars and strict arithmetic effects + public void testExVar() throws Exception { + JexlEngine jexl = createEngine(false); + JexlScript e = jexl.createScript("(x)->{ x * 6 }"); + JexlEvalContext ctxt = new JexlEvalContext(); + // ensure errors will throw + ctxt.setSilent(false); + // make unknown vars throw + ctxt.setStrict(true); + ctxt.setStrictArithmetic(true); + // empty cotext + try { + /* Object o = */ e.execute(ctxt); + fail("x is null, should throw"); + } catch (JexlException xjexl) { + String msg = xjexl.getMessage(); + assertTrue(msg.indexOf("null") > 0); + } + + // allow null operands + ctxt.setStrictArithmetic(false); + try { + Object o = e.execute(ctxt); + } catch (JexlException.Variable xjexl) { + fail("arithmetic allows null operands, should not throw"); + } + } + + // Unknown vars and properties versus null operands + public void testExMethod() throws Exception { + JexlEngine jexl = createEngine(false); + JexlExpression e = jexl.createExpression("c.e.foo()"); + JexlEvalContext ctxt = new JexlEvalContext(); + // ensure errors will throw + ctxt.setSilent(false); + // make unknown vars throw + ctxt.setStrict(true); + // empty cotext + try { + /* Object o = */ e.evaluate(ctxt); + fail("c.e not declared as variable should throw"); + } catch (JexlException.Variable xjexl) { + String msg = xjexl.getMessage(); + assertTrue(msg.indexOf("c.e") > 0); + } + + // disallow null operands + ctxt.setStrictArithmetic(true); + ctxt.set("c.e", null); + try { + /* Object o = */ e.evaluate(ctxt); + fail("c.e as null operand should throw"); + } catch (JexlException xjexl) { + String msg = xjexl.getMessage(); + assertTrue(msg.indexOf("c.e") > 0); + } + } }
Modified: commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/LambdaTest.java URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/LambdaTest.java?rev=1628650&r1=1628649&r2=1628650&view=diff ============================================================================== --- commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/LambdaTest.java (original) +++ commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/LambdaTest.java Wed Oct 1 08:57:12 2014 @@ -17,6 +17,7 @@ package org.apache.commons.jexl3; import org.apache.commons.jexl3.internal.Engine; +import java.util.concurrent.Callable; /** * Tests function/lambda/closure features. @@ -107,4 +108,64 @@ public class LambdaTest extends JexlTest Object result = s42.execute(null); assertEquals(42, result); } + + public void testNestLambada() throws Exception { + JexlEngine jexl = new Engine(); + JexlContext ctx = null; + String strs = "(x)->{ (y)->{ x + y } }"; + JexlScript s42 = jexl.createScript(strs); + Object result = s42.execute(ctx, 15); + assertTrue(result instanceof JexlScript); + JexlScript s15 = (JexlScript) result; + Callable<Object> s15b = s15.callable(ctx, 27); + result = s15.execute(ctx, 27); + assertEquals(42, result); + result = s15b.call(); + assertEquals(42, result); + } + + public void testRecurse() throws Exception { + JexlEngine jexl = new Engine(); + JexlContext jc = new MapContext(); + try { + JexlScript script = jexl.createScript("var fact = (x)->{ if (x <= 1) 1; else x * fact(x - 1) }; fact(5)"); + int result = (Integer) script.execute(jc); + assertEquals(120, result); + } catch (JexlException xany) { + String msg = xany.toString(); + throw xany; + } + } + + public void testRecurse2() throws Exception { + JexlEngine jexl = new Engine(); + JexlContext jc = new MapContext(); + // adding some hoisted vars to get it confused + try { + JexlScript script = jexl.createScript( + "var y = 1; var z = 1; " + +"var fact = (x)->{ if (x <= y) z; else x * fact(x - 1) }; fact(6)"); + int result = (Integer) script.execute(jc); + assertEquals(720, result); + } catch (JexlException xany) { + String msg = xany.toString(); + throw xany; + } + } + + public void testRecurse3() throws Exception { + JexlEngine jexl = new Engine(); + JexlContext jc = new MapContext(); + // adding some hoisted vars to get it confused + try { + JexlScript script = jexl.createScript( + "var y = 1; var z = 1;var foo = (x)->{y + z}; " + +"var fact = (x)->{ if (x <= y) z; else x * fact(x - 1) }; fact(6)"); + int result = (Integer) script.execute(jc); + assertEquals(720, result); + } catch (JexlException xany) { + String msg = xany.toString(); + throw xany; + } + } } Modified: commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/introspection/SandboxTest.java URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/introspection/SandboxTest.java?rev=1628650&r1=1628649&r2=1628650&view=diff ============================================================================== --- commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/introspection/SandboxTest.java (original) +++ commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/introspection/SandboxTest.java Wed Oct 1 08:57:12 2014 @@ -295,7 +295,7 @@ public class SandboxTest extends JexlTes String expr; JexlScript script; Object result; - + script = sjexl.createScript("System.exit()"); try { result = script.execute(context); @@ -304,6 +304,14 @@ public class SandboxTest extends JexlTes LOGGER.info(xjexl.toString()); } + script = sjexl.createScript("System.exit(1)"); + try { + result = script.execute(context); + fail("should not allow calling exit!"); + } catch (JexlException xjexl) { + LOGGER.info(xjexl.toString()); + } + script = sjexl.createScript("new('java.io.File', '/tmp/should-not-be-created')"); try { result = script.execute(context); Modified: commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/scripting/JexlScriptEngineTest.java URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/scripting/JexlScriptEngineTest.java?rev=1628650&r1=1628649&r2=1628650&view=diff ============================================================================== --- commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/scripting/JexlScriptEngineTest.java (original) +++ commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/scripting/JexlScriptEngineTest.java Wed Oct 1 08:57:12 2014 @@ -147,4 +147,10 @@ public class JexlScriptEngineTest extend assertTrue(mymap instanceof Map<?, ?>); assertEquals(2,((Map<?, ?>)mymap).size()); } + + public void testDirectNew() throws Exception { + ScriptEngine engine = new JexlScriptEngine(); + final Integer initialValue = Integer.valueOf(123); + assertEquals(initialValue,engine.eval("123")); + } }