use of org.apache.commons.jexl3.JexlExpression in project commons-jexl by apache.
the class SandboxTest method testSetNullKeyAllowed0.
@Test
public void testSetNullKeyAllowed0() throws Exception {
Arithmetic350 a350 = new Arithmetic350(true);
JexlEngine jexl = new JexlBuilder().arithmetic(a350).sandbox(new JexlSandbox(true)).create();
JexlContext jc = new MapContext();
JexlExpression expression = jexl.createExpression("{null : 'foo'}[null] = 'bar'");
expression.evaluate(jc);
Map<?, ?> map = a350.getLastMap();
Assert.assertEquals("bar", map.get(null));
}
use of org.apache.commons.jexl3.JexlExpression in project commons-jexl by apache.
the class SandboxTest method testGetNullKeyAllowed0.
@Test
public void testGetNullKeyAllowed0() throws Exception {
JexlEngine jexl = new JexlBuilder().sandbox(new JexlSandbox(true)).create();
JexlExpression expression = jexl.createExpression("{null : 'foo'}[null]");
Object o = expression.evaluate(null);
Assert.assertEquals("foo", o);
}
use of org.apache.commons.jexl3.JexlExpression in project dotwebstack-framework by dotwebstack.
the class JexlHelper method evaluateExpression.
public <T> Optional<T> evaluateExpression(String expressionString, JexlContext context, Class<T> clazz) {
JexlExpression expression = this.engine.createExpression(expressionString);
Object evaluated = expression.evaluate(context);
if (evaluated == null) {
return Optional.empty();
} else if (!clazz.isInstance(evaluated)) {
throw illegalArgumentException("Jexl evaluateDirectiveArgument type mismatch: expected[{}], but was [{}].", clazz, evaluated.getClass());
} else {
return Optional.of(cast(clazz, evaluated));
}
}
use of org.apache.commons.jexl3.JexlExpression in project Qualitis by WeBankFinTech.
the class JexlUtil method executeExpression.
public static Object executeExpression(String jexlExpression, Map<String, Object> map) {
JexlExpression expression = jexlEngine.createExpression(jexlExpression);
JexlContext context = new MapContext();
if (map != null && !map.isEmpty()) {
map.forEach(context::set);
}
return expression.evaluate(context);
}
use of org.apache.commons.jexl3.JexlExpression in project waltz by finos.
the class PredicateEvaluatorTest method evaluate.
@Test
public void evaluate() {
ImmutableAssessmentContextValue assessmentValue = ImmutableAssessmentContextValue.builder().ratingCode("A").ratingName("aRating").ratingExternalId("A_RATING").ratingComment("a rating description").build();
ImmutableSurveyQuestionResponseContextValue surveyValue = ImmutableSurveyQuestionResponseContextValue.builder().value("yes").comment("a survey comment").build();
ImmutableContextVariable<Object> assessmentVar = ImmutableContextVariable.builder().name("assessmentVar").value(assessmentValue).entityRef(EntityReference.mkRef(EntityKind.APPLICATION, 1L)).build();
ImmutableContextVariable<Object> surveyVar = ImmutableContextVariable.builder().name("surveyVar").value(surveyValue).entityRef(EntityReference.mkRef(EntityKind.APPLICATION, 1L)).build();
Set<ContextVariable<?>> contextVariables = asSet(assessmentVar, surveyVar);
JexlBuilder builder = new JexlBuilder();
JexlEngine jexl = builder.create();
JexlExpression pred1Expr = jexl.createExpression("assessmentVar.ratingCode == 'A'");
JexlExpression pred2Expr = jexl.createExpression("assessmentVar.ratingCode == 'C' || surveyVar.value == 'yes'");
JexlExpression pred3Expr = jexl.createExpression("assessmentVar.ratingCode == 'C'");
JexlExpression invalidExpr = jexl.createExpression("foo?.bar");
MapContext ctx = createContext(contextVariables);
assertTrue(evaluatePredicate(pred1Expr, ctx), "pred1Expr should evaluate true, rating equals A");
assertTrue(evaluatePredicate(pred2Expr, ctx), "pred2Expr should evaluate true, survey value equals 'yes");
assertFalse(evaluatePredicate(pred3Expr, ctx), "pred3Expr should evaluate false, rating equals A");
assertFalse(evaluatePredicate(invalidExpr, ctx), "Invalid expressions should return false");
Object evaluate = invalidExpr.evaluate(ctx);
System.out.println(evaluate);
}
Aggregations