@Override public Object apply(Object context) { return MVEL.eval(script, context); } }
@Test public void testBasicMVELFunctions() { //First, set up out functions HashMap<String, Object> functionMap = new HashMap<>(); StringBuffer functions = new StringBuffer("def any(x, y) { return x or y } def all(x, y) { return x and y } "); MVEL.eval(functions.toString(), functionMap); //This stores that functions in the map we pass in. HashMap<String, Object> vars = new HashMap<>(functionMap); //Now, we need to pass the functions in to our variable map vars.put("fg", "Hello"); StringBuffer expression = new StringBuffer(); expression.append("return all(fg == 'Hello', true)"); Boolean result = (Boolean)MVEL.eval(expression.toString(), vars); assert result != null && result; expression = new StringBuffer(); expression.append("return any(fg == 'Goodbye', false)"); Boolean result2 = (Boolean)MVEL.eval(expression.toString(), vars); assert result2 != null && ! result2; }
.append(" return (orderContainsPlusMark(1) and currentItem.product.defaultCategory.name == 'hat');"); Boolean result = (Boolean)MVEL.eval(expression.toString(), vars); assert result != null && result;
@Test public void testMVELPopulate() throws Exception { Object q = MVEL.eval("new " + DumbFact.class.getName() + "()"); Map m = new HashMap(); m.put("obj", q); m.put("val", "mike"); MVEL.eval("obj.name = val", m); m = new HashMap(); m.put("obj", q); m.put("val", "42"); MVEL.eval("obj.age = val", m); m = new HashMap(); m.put("obj", q); m.put("val", "44"); MVEL.eval("obj.number = val", m); DumbFact d = (DumbFact) q; assertEquals("mike", d.getName()); assertEquals(42, d.getAge()); assertEquals(new Long(44), d.getNumber()); }
/** * Evaluate an expression in Boolean-only mode against a root context object and injected variables. * * @param expression A string containing the expression to be evaluated. * @param ctx The context against which to evaluate the expression * @param vars The variables to be injected * @return The resultant value as a Boolean */ public static Boolean evalToBoolean(String expression, Object ctx, Map<String, Object> vars) { return eval(expression, ctx, vars, Boolean.class); }
/** * Evaluates an expression and returns the resultant value as a String. * * @param expression A string containing the expressino to be evaluated. * @return The resultant value */ public static String evalToString(String expression) { return valueOf(eval(expression)); }
final String name = (String) MVEL.eval("$t.name", Collections.singletonMap("$t", value));
public void testProtoFieldAccess() { if (!run) return; Object o = MVEL.eval("proto Person { int age = 5; String name; }; (p = new Person()).age", new HashMap<String, Object>()); assertEquals(5, o); }
public void testShouldFail5() { try { MVEL.eval("["); } catch (Exception e) { return; } shouldThrowException(); }
public void testShouldFail11() { try { MVEL.eval("for (int i = 0; i--; i++) {}"); } catch (Exception e) { e.printStackTrace(); return; } shouldThrowException(); }
public void testMath5a() { String expression = "300.5 / 5.3 / 2.1 / 1.5"; System.out.println("Expression: " + expression); assertEquals(300.5 / 5.3 / 2.1 / 1.5, MVEL.eval(expression)); }
public void testMath35_Interpreted() { String expression = "b/x/b/b*y+a"; Map map = new HashMap(); map.put("a", 10); map.put("b", 20); map.put("c", 30); map.put("x", 40); map.put("y", 50); map.put("z", 60); assertNumEquals(20d / 40d / 20d / 20d * 50d + 10d, MVEL.eval(expression, map)); }
public void testJIRA99_Interpreted() { Map map = new HashMap(); map.put("x", 20); map.put("y", 10); map.put("z", 5); assertEquals(20 - 10 - 5, MVEL.eval("x - y - z", map)); }
public Object getReducedValue(Object ctx, Object thisValue, VariableResolverFactory factory) { VariableResolver resolver = factory.getIndexedVariableResolver(register); resolver.setValue(ctx = MathProcessor.doOperations(resolver.getValue(), operation, eval(expr, start, offset, ctx, factory))); return ctx; } }
public void testCallGlobalStaticFunctionFromMVELFunction() { TestClassAZZ azz = new TestClassAZZ(); String expr = "def foobie12345() { hey(); } foobie12345();"; assertEquals("Heythere!", MVEL.eval(expr, azz, new HashMap<String, Object>())); }
public void testStaticTyping2() { String exp = "int x = 5; int y = 2; new int[] { x, y }"; int[] res = (int[]) MVEL.eval(exp, new HashMap()); assertEquals(5, res[0]); assertEquals(2, res[1]); }
public void testShouldFail4() { try { MVEL.eval("hour zzz", createTestMap()); } catch (Exception e) { return; } shouldThrowException(); }
public void testFunctionPointer() { String ex = "squareRoot = java.lang.Math.sqrt; squareRoot(4)"; Object o = MVEL.eval(ex, new HashMap()); assertEquals(2.0, o); assertEquals(2.0, test(ex)); } /*
public Object eval(TemplateRuntime runtime, TemplateOutputStream appender, Object ctx, VariableResolverFactory factory) { if (runtime.getNamedTemplateRegistry() == null) { runtime.setNamedTemplateRegistry(new SimpleTemplateRegistry()); } runtime.getNamedTemplateRegistry() .addNamedTemplate(MVEL.eval(contents, cStart, cEnd - cStart, ctx, factory, String.class), new CompiledTemplate(runtime.getTemplate(), nestedNode)); return next != null ? next.eval(runtime, appender, ctx, factory) : null; }
public void testProjectionSupport3() { String ex = "(toUpperCase() in ['bar', 'foo'])[1]"; Map vars = createTestMap(); assertEquals("FOO", MVEL.eval(ex, new Base(), vars)); assertEquals("FOO", test("(toUpperCase() in ['bar', 'foo'])[1]")); }