This is an automated email from the ASF dual-hosted git repository. henrib pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-jexl.git
The following commit(s) were added to refs/heads/master by this push: new 2b63192 JEXL-307: added test case on contextually exposed options Task #JEXL-307 - Variable redeclaration option 2b63192 is described below commit 2b63192c9485c75ea2cfff51bb6d153479a1c7ea Author: henrib <hen...@apache.org> AuthorDate: Tue Nov 19 11:42:43 2019 +0100 JEXL-307: added test case on contextually exposed options Task #JEXL-307 - Variable redeclaration option --- .../java/org/apache/commons/jexl3/JexlOptions.java | 2 +- .../java/org/apache/commons/jexl3/LexicalTest.java | 57 +++++++++++++++++++++- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/apache/commons/jexl3/JexlOptions.java b/src/main/java/org/apache/commons/jexl3/JexlOptions.java index 98c46ef..7d3df9e 100644 --- a/src/main/java/org/apache/commons/jexl3/JexlOptions.java +++ b/src/main/java/org/apache/commons/jexl3/JexlOptions.java @@ -31,7 +31,7 @@ import org.apache.commons.jexl3.internal.Engine; * <li>lexicalShade: whether local variables shade global ones even outside their scope</li> * <li>strict: whether unknown or unsolvable identifiers are errors</li> * <li>strictArithmetic: whether null as operand is an error</li> - * <li>immutable: whether these options can be modified at runtime during execution (expert)</li> + * <li>sharedInstance: whether these options can be modified at runtime during execution (expert)</li> * </ul> * The sensible default is cancellable, strict and strictArithmetic. * <p>This interface replaces the now deprecated JexlEngine.Options. diff --git a/src/test/java/org/apache/commons/jexl3/LexicalTest.java b/src/test/java/org/apache/commons/jexl3/LexicalTest.java index 9f7b9c9..2c6b2f7 100644 --- a/src/test/java/org/apache/commons/jexl3/LexicalTest.java +++ b/src/test/java/org/apache/commons/jexl3/LexicalTest.java @@ -18,6 +18,7 @@ package org.apache.commons.jexl3; import java.io.StringReader; import java.io.StringWriter; +import java.util.Map; import java.util.Set; import org.apache.commons.jexl3.internal.LexicalScope; import org.junit.Assert; @@ -388,7 +389,7 @@ public class LexicalTest { } @Test - public void testLexicalOption() throws Exception { + public void testContextualOptions0() throws Exception { JexlFeatures f= new JexlFeatures(); JexlEngine jexl = new JexlBuilder().features(f).strict(true).create(); JexlEvalContext ctxt = new JexlEvalContext(); @@ -405,4 +406,58 @@ public class LexicalTest { Assert.assertNotNull(xf); } } + + /** + * Context augmented with a tryCatch. + */ + public static class TestContext extends JexlEvalContext { + public TestContext() {} + public TestContext(Map<String, Object> map) { + super(map); + } + + /** + * Allows calling a script and catching its raised exception. + * @param tryFn the lambda to call + * @param catchFn the lambda catching the exception + * @param args the arguments to the lambda + * @return the tryFn result or the catchFn if an exception was raised + */ + public Object tryCatch(JexlScript tryFn, JexlScript catchFn, Object... args) { + Object result; + try { + result = tryFn.execute(this, args); + } catch (Throwable xthrow) { + result = catchFn != null ? catchFn.execute(this, xthrow) : xthrow; + } + return result; + } + } + + @Test + public void testContextualOptions1() throws Exception { + JexlFeatures f = new JexlFeatures(); + JexlEngine jexl = new JexlBuilder().features(f).strict(true).create(); + JexlEvalContext ctxt = new TestContext(); + JexlOptions options = ctxt.getEngineOptions(); + options.setSharedInstance(true); + options.setLexical(true); + options.setLexicalShade(true); + ctxt.set("options", options); + JexlScript runner = jexl.createScript( + "options.lexical = flag; options.lexicalShade = flag;" + + "tryCatch(test, catch, 42);", + "flag", "test", "catch"); + JexlScript tested = jexl.createScript("(y)->{ {var x = y;} x }"); + JexlScript catchFn = jexl.createScript("(xany)-> { xany }"); + Object result; + // run it once, old 3.1 semantics, lexical/shade = false + result = runner.execute(ctxt, false, tested, catchFn); + // result 42 + Assert.assertEquals(42, result); + // run it a second time, new 3.2 semantics, lexical/shade = true + result = runner.execute(ctxt, true, tested, catchFn); + // result is exception! + Assert.assertTrue(result instanceof JexlException.Variable); + } }