Search in sources :

Example 41 with MapContext

use of org.apache.commons.jexl3.MapContext in project syncope by apache.

the class MailTemplateTest method evaluate.

private String evaluate(final String template, final Map<String, Object> jexlVars) {
    StringWriter writer = new StringWriter();
    JexlUtils.newJxltEngine().createTemplate(template).evaluate(new MapContext(jexlVars), writer);
    return writer.toString();
}
Also used : StringWriter(java.io.StringWriter) MapContext(org.apache.commons.jexl3.MapContext)

Example 42 with MapContext

use of org.apache.commons.jexl3.MapContext in project syncope by apache.

the class MappingTest method realmConnObjectLink.

@Test
public void realmConnObjectLink() {
    Realm realm = realmDAO.findByFullPath("/even/two");
    assertNotNull(realm);
    JexlContext jexlContext = new MapContext();
    JexlUtils.addFieldsToContext(realm, jexlContext);
    String connObjectLink = "syncope:fullPath2Dn(fullPath, 'ou') + ',o=isp'";
    assertEquals("ou=two,ou=even,o=isp", JexlUtils.evaluate(connObjectLink, jexlContext));
    realm = realmDAO.findByFullPath("/even");
    assertNotNull(realm);
    jexlContext = new MapContext();
    JexlUtils.addFieldsToContext(realm, jexlContext);
    assertEquals("ou=even,o=isp", JexlUtils.evaluate(connObjectLink, jexlContext));
}
Also used : JexlContext(org.apache.commons.jexl3.JexlContext) MapContext(org.apache.commons.jexl3.MapContext) Realm(org.apache.syncope.core.persistence.api.entity.Realm) Test(org.junit.jupiter.api.Test)

Example 43 with MapContext

use of org.apache.commons.jexl3.MapContext in project jdbi by jdbi.

the class TestBindExpression method testJexl.

@Test
public void testJexl() throws Exception {
    JexlEngine engine = new JexlEngine();
    Object topping = engine.createExpression("breakfast.waffle.topping").evaluate(new MapContext(ImmutableMap.<String, Object>of("breakfast", new Breakfast())));
    assertThat(topping).isEqualTo("syrup");
}
Also used : JexlEngine(org.apache.commons.jexl2.JexlEngine) MapContext(org.apache.commons.jexl2.MapContext) Test(org.junit.Test)

Example 44 with MapContext

use of org.apache.commons.jexl3.MapContext in project newts by OpenNMS.

the class ResultDescriptor method expression.

public ResultDescriptor expression(String label, String expression) {
    final JexlExpression expr = JEXL_ENGINE.createExpression(expression);
    final String[] labels = getLabels().toArray(new String[0]);
    CalculationFunction evaluate = new CalculationFunction() {

        private static final long serialVersionUID = -3328049421398096252L;

        @Override
        public double apply(double... ds) {
            JexlContext jc = new MapContext();
            for (int i = 0; i < labels.length; i++) {
                jc.set(labels[i], ds[i]);
            }
            return ((Number) expr.evaluate(jc)).doubleValue();
        }
    };
    return calculate(label, evaluate, labels);
}
Also used : JexlContext(org.apache.commons.jexl3.JexlContext) JexlExpression(org.apache.commons.jexl3.JexlExpression) MapContext(org.apache.commons.jexl3.MapContext)

Example 45 with MapContext

use of org.apache.commons.jexl3.MapContext in project jmeter by apache.

the class Jexl3Function method execute.

/**
 * {@inheritDoc}
 */
@Override
public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
    // $NON-NLS-1$
    String str = "";
    CompoundVariable var = (CompoundVariable) values[0];
    String exp = var.execute();
    // $NON-NLS-1$
    String varName = "";
    if (values.length > 1) {
        varName = ((CompoundVariable) values[1]).execute().trim();
    }
    JMeterContext jmctx = JMeterContextService.getContext();
    JMeterVariables vars = jmctx.getVariables();
    try {
        JexlContext jc = new MapContext();
        // $NON-NLS-1$
        jc.set("log", log);
        // $NON-NLS-1$
        jc.set("ctx", jmctx);
        // $NON-NLS-1$
        jc.set("vars", vars);
        // $NON-NLS-1$
        jc.set("props", JMeterUtils.getJMeterProperties());
        // Previously mis-spelt as theadName
        // $NON-NLS-1$
        jc.set("threadName", Thread.currentThread().getName());
        // $NON-NLS-1$ (may be null)
        jc.set("sampler", currentSampler);
        // $NON-NLS-1$ (may be null)
        jc.set("sampleResult", previousResult);
        // $NON-NLS-1$
        jc.set("OUT", System.out);
        // Now evaluate the script, getting the result
        JexlScript e = threadLocalJexl.get().createScript(exp);
        Object o = e.execute(jc);
        if (o != null) {
            str = o.toString();
        }
        if (vars != null && varName.length() > 0) {
            // vars will be null on TestPlan
            vars.put(varName, str);
        }
    } catch (Exception e) {
        log.error("An error occurred while evaluating the expression \"{}\"\n", exp, e);
    }
    return str;
}
Also used : CompoundVariable(org.apache.jmeter.engine.util.CompoundVariable) JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterContext(org.apache.jmeter.threads.JMeterContext) JexlContext(org.apache.commons.jexl3.JexlContext) JexlScript(org.apache.commons.jexl3.JexlScript) MapContext(org.apache.commons.jexl3.MapContext)

Aggregations

MapContext (org.apache.commons.jexl2.MapContext)32 MapContext (org.apache.commons.jexl3.MapContext)26 JexlContext (org.apache.commons.jexl2.JexlContext)23 Expression (org.apache.commons.jexl2.Expression)20 JexlEngine (org.apache.commons.jexl2.JexlEngine)20 JexlContext (org.apache.commons.jexl3.JexlContext)17 Test (org.testng.annotations.Test)13 HashMap (java.util.HashMap)7 Map (java.util.Map)7 HashSet (java.util.HashSet)4 JexlException (org.apache.commons.jexl2.JexlException)4 JexlBuilder (org.apache.commons.jexl3.JexlBuilder)4 JexlExpression (org.apache.commons.jexl3.JexlExpression)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Method (java.lang.reflect.Method)3 ArrayList (java.util.ArrayList)3 JexlEngine (org.apache.commons.jexl3.JexlEngine)3 Test (org.junit.jupiter.api.Test)3 Device (org.traccar.model.Device)3 IOException (java.io.IOException)2