Search in sources :

Example 6 with JexlOptions

use of org.apache.commons.jexl3.JexlOptions in project commons-jexl by apache.

the class Issues100Test method test106.

@Test
public void test106() throws Exception {
    final JexlEvalContext context = new JexlEvalContext();
    final JexlOptions options = context.getEngineOptions();
    options.setStrict(true);
    options.setStrictArithmetic(true);
    context.set("a", new BigDecimal(1));
    context.set("b", new BigDecimal(3));
    final JexlEngine jexl = new Engine();
    try {
        final Object value = jexl.createExpression("a / b").evaluate(context);
        Assert.assertNotNull(value);
    } catch (final JexlException xjexl) {
        Assert.fail("should not occur");
    }
    options.setMathContext(MathContext.UNLIMITED);
    options.setMathScale(2);
    try {
        jexl.createExpression("a / b").evaluate(context);
        Assert.fail("should fail");
    } catch (final JexlException xjexl) {
    // ok  to fail
    }
}
Also used : BigDecimal(java.math.BigDecimal) Engine(org.apache.commons.jexl3.internal.Engine) Test(org.junit.Test)

Example 7 with JexlOptions

use of org.apache.commons.jexl3.JexlOptions in project commons-jexl by apache.

the class Issues100Test method testScaleIssue.

@Test
public void testScaleIssue() throws Exception {
    final JexlEngine jexlX = new Engine();
    final String expStr1 = "result == salary/month * work.percent/100.00";
    final JexlExpression exp1 = jexlX.createExpression(expStr1);
    final JexlEvalContext ctx = new JexlEvalContext();
    final JexlOptions options = ctx.getEngineOptions();
    ctx.set("result", new BigDecimal("9958.33"));
    ctx.set("salary", new BigDecimal("119500.00"));
    ctx.set("month", new BigDecimal("12.00"));
    ctx.set("work.percent", new BigDecimal("100.00"));
    // will fail because default scale is 5
    Assert.assertFalse((Boolean) exp1.evaluate(ctx));
    // will succeed with scale = 2
    options.setMathScale(2);
    Assert.assertTrue((Boolean) exp1.evaluate(ctx));
}
Also used : Engine(org.apache.commons.jexl3.internal.Engine) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Aggregations

JexlOptions (org.apache.commons.jexl3.JexlOptions)4 Test (org.junit.Test)3 BigDecimal (java.math.BigDecimal)2 JxltEngine (org.apache.commons.jexl3.JxltEngine)2 Engine (org.apache.commons.jexl3.internal.Engine)2 JexlArithmetic (org.apache.commons.jexl3.JexlArithmetic)1 JexlContext (org.apache.commons.jexl3.JexlContext)1 JexlEngine (org.apache.commons.jexl3.JexlEngine)1 JexlException (org.apache.commons.jexl3.JexlException)1 Script (org.apache.commons.jexl3.internal.Script)1 TemplateExpression (org.apache.commons.jexl3.internal.TemplateEngine.TemplateExpression)1 ASTJexlLambda (org.apache.commons.jexl3.parser.ASTJexlLambda)1 JexlNode (org.apache.commons.jexl3.parser.JexlNode)1