Search in sources :

Example 11 with Foo

use of org.mvel2.tests.core.res.Foo in project mvel by mikebrock.

the class MacroProcessorTest method testMacroSupport.

public void testMacroSupport() {
    Map<String, Object> vars = new HashMap<String, Object>();
    vars.put("foo", new Foo());
    Map<String, Interceptor> interceptors = new HashMap<String, Interceptor>();
    Map<String, Macro> macros = new HashMap<String, Macro>();
    interceptors.put("Modify", new Interceptor() {

        public int doBefore(ASTNode node, VariableResolverFactory factory) {
            ((WithNode) node).getNestedStatement().getValue(null, factory);
            factory.createVariable("mod", "FOOBAR!");
            return 0;
        }

        public int doAfter(Object val, ASTNode node, VariableResolverFactory factory) {
            return 0;
        }
    });
    macros.put("modify", new Macro() {

        public String doMacro() {
            return "@Modify with";
        }
    });
    ExpressionCompiler compiler = new ExpressionCompiler(parseMacros("modify (foo) { aValue = 'poo = poo', bValue = 'poo, poo' }; mod", macros));
    ParserContext ctx = new ParserContext(null, interceptors, null);
    ctx.setSourceFile("test.mv");
    ctx.setDebugSymbols(true);
    assertEquals("FOOBAR!", executeExpression(compiler.compile(ctx), null, vars));
}
Also used : HashMap(java.util.HashMap) Foo(org.mvel2.tests.core.res.Foo) WithNode(org.mvel2.ast.WithNode) VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ASTNode(org.mvel2.ast.ASTNode) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) Interceptor(org.mvel2.integration.Interceptor)

Example 12 with Foo

use of org.mvel2.tests.core.res.Foo in project mvel by mikebrock.

the class MacroProcessorTest method testMacroSupportWithStrings.

public void testMacroSupportWithStrings() {
    Map<String, Object> vars = new HashMap<String, Object>();
    Foo foo = new Foo();
    vars.put("foo", foo);
    Map<String, Macro> macros = new HashMap<String, Macro>();
    macros.put("modify", new Macro() {

        public String doMacro() {
            return "drools.modify";
        }
    });
    assertEquals("", foo.aValue);
    ExpressionCompiler compiler = new ExpressionCompiler(parseMacros("\"This is an modify()\"", macros));
    ParserContext ctx = new ParserContext(null, null, null);
    ctx.setSourceFile("test.mv");
    ctx.setDebugSymbols(true);
    assertEquals("This is an modify()", executeExpression(compiler.compile(ctx), null, vars));
}
Also used : HashMap(java.util.HashMap) Foo(org.mvel2.tests.core.res.Foo) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler)

Example 13 with Foo

use of org.mvel2.tests.core.res.Foo in project mvel by mikebrock.

the class MutationsTests method testDeepAssignment2.

public void testDeepAssignment2() {
    Map map = createTestMap();
    ExpressionCompiler compiler = new ExpressionCompiler("foo.bar.age = 21");
    ParserContext ctx = new ParserContext();
    ctx.addInput("foo", Foo.class);
    ctx.setStrongTyping(true);
    CompiledExpression ce = compiler.compile(ctx);
    executeExpression(ce, map);
    assertEquals(((Foo) map.get("foo")).getBar().getAge(), 21);
}
Also used : Foo(org.mvel2.tests.core.res.Foo) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext) Map(java.util.Map) HashMap(java.util.HashMap) CompiledExpression(org.mvel2.compiler.CompiledExpression)

Example 14 with Foo

use of org.mvel2.tests.core.res.Foo in project mvel by mikebrock.

the class ProjectionsTests method testProjectionSupport3.

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]"));
}
Also used : Map(java.util.Map) Base(org.mvel2.tests.core.res.Base)

Example 15 with Foo

use of org.mvel2.tests.core.res.Foo in project mvel by mikebrock.

the class PropertyAccessTests method testDynamicDeop.

public void testDynamicDeop() {
    Serializable s = compileExpression("name");
    assertEquals("dog", executeExpression(s, new Foo()));
    assertEquals("dog", executeExpression(s, new Foo().getBar()));
}
Also used : Serializable(java.io.Serializable) Foo(org.mvel2.tests.core.res.Foo)

Aggregations

ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)27 Foo (org.mvel2.tests.core.res.Foo)22 ParserContext (org.mvel2.ParserContext)18 CompiledExpression (org.mvel2.compiler.CompiledExpression)15 Serializable (java.io.Serializable)14 HashMap (java.util.HashMap)12 Test (org.junit.Test)9 KieServices (org.kie.api.KieServices)9 KieFileSystem (org.kie.api.builder.KieFileSystem)9 ReleaseId (org.kie.api.builder.ReleaseId)9 KieContainer (org.kie.api.runtime.KieContainer)9 PropertyAccessException (org.mvel2.PropertyAccessException)9 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)9 KieSession (org.kie.api.runtime.KieSession)8 ConsequenceException (org.kie.api.runtime.rule.ConsequenceException)7 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)7 Map (java.util.Map)5 DefaultLocalVariableResolverFactory (org.mvel2.integration.impl.DefaultLocalVariableResolverFactory)5 Interceptor (org.mvel2.integration.Interceptor)4 ASTNode (org.mvel2.ast.ASTNode)3