Search in sources :

Example 1 with Foo

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

the class PropertyHandlerTests method testNullPropertyHandler2.

public void testNullPropertyHandler2() {
    MVEL.COMPILER_OPT_ALLOW_OVERRIDE_ALL_PROPHANDLING = true;
    OptimizerFactory.setDefaultOptimizer("reflective");
    PropertyHandlerFactory.setNullPropertyHandler(new PropertyHandler() {

        public Object getProperty(String name, Object contextObj, VariableResolverFactory variableFactory) {
            return "NULL";
        }

        public Object setProperty(String name, Object contextObj, VariableResolverFactory variableFactory, Object value) {
            return "NULL";
        }
    });
    Foo foo = new Foo();
    Bar bar = foo.getBar();
    foo.setBar(null);
    Map map = new HashMap();
    map.put("foo", foo);
    Serializable s = MVEL.compileExpression("foo.bar");
    assertEquals("NULL", MVEL.executeExpression(s, map));
    assertEquals("NULL", MVEL.executeExpression(s, map));
    foo.setBar(bar);
    assertEquals(bar, MVEL.executeExpression(s, map));
}
Also used : Bar(org.mvel2.tests.core.res.Bar) Serializable(java.io.Serializable) HashMap(java.util.HashMap) Foo(org.mvel2.tests.core.res.Foo) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with Foo

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

the class PropertyHandlerTests method _testListener.

public void _testListener() {
    MVEL.COMPILER_OPT_ALLOW_OVERRIDE_ALL_PROPHANDLING = true;
    class MyListener implements Listener {

        public int count;

        public void onEvent(Object context, String contextName, VariableResolverFactory variableFactory, Object value) {
            count++;
        }
    }
    MyListener listener = new MyListener();
    GlobalListenerFactory.registerGetListener(listener);
    PropertyHandlerFactory.setNullPropertyHandler(new PropertyHandler() {

        public Object getProperty(String name, Object contextObj, VariableResolverFactory variableFactory) {
            List someList = new ArrayList();
            someList.add(new Foo());
            return someList;
        }

        public Object setProperty(String name, Object contextObj, VariableResolverFactory variableFactory, Object value) {
            return null;
        }
    });
    PropertyHandlerFactory.registerPropertyHandler(List.class, new PropertyHandler() {

        public Object getProperty(String name, Object contextObj, VariableResolverFactory variableFactory) {
            List list = (List) contextObj;
            int index = Integer.valueOf(name);
            while (index >= list.size()) {
                list.add(new Foo());
            }
            return list.get(index);
        }

        public Object setProperty(String name, Object contextObj, VariableResolverFactory variableFactory, Object value) {
            return null;
        }
    });
    Foo foo = new Foo();
    final Serializable fooExpr0 = MVEL.compileSetExpression("collectionTest[0].name");
    final Serializable fooExpr1 = MVEL.compileSetExpression("collectionTest[1].name");
    MVEL.executeSetExpression(fooExpr0, foo, "John Galt");
    MVEL.executeSetExpression(fooExpr1, foo, "The Joker");
    assertEquals(2, listener.count);
    MVEL.COMPILER_OPT_ALLOW_OVERRIDE_ALL_PROPHANDLING = false;
}
Also used : Serializable(java.io.Serializable) Foo(org.mvel2.tests.core.res.Foo) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with Foo

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

the class PropertyHandlerTests method testNullPropertyHandler.

public void testNullPropertyHandler() {
    MVEL.COMPILER_OPT_ALLOW_OVERRIDE_ALL_PROPHANDLING = true;
    OptimizerFactory.setDefaultOptimizer("ASM");
    PropertyHandlerFactory.setNullPropertyHandler(new PropertyHandler() {

        public Object getProperty(String name, Object contextObj, VariableResolverFactory variableFactory) {
            return "NULL";
        }

        public Object setProperty(String name, Object contextObj, VariableResolverFactory variableFactory, Object value) {
            return "NULL";
        }
    });
    Foo foo = new Foo();
    Bar bar = foo.getBar();
    foo.setBar(null);
    Map map = new HashMap();
    map.put("foo", foo);
    Serializable s = MVEL.compileExpression("foo.bar");
    assertEquals("NULL", MVEL.executeExpression(s, map));
    assertEquals("NULL", MVEL.executeExpression(s, map));
    foo.setBar(bar);
    assertEquals(bar, MVEL.executeExpression(s, map));
}
Also used : Bar(org.mvel2.tests.core.res.Bar) Serializable(java.io.Serializable) HashMap(java.util.HashMap) Foo(org.mvel2.tests.core.res.Foo) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with Foo

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

the class WithTests method testNewUsingWith.

public void testNewUsingWith() {
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addImport(Foo.class);
    ctx.addImport(Bar.class);
    Serializable s = compileExpression("[ 'foo' : (with ( new Foo() )" + " { bar = with ( new Bar() ) { name = 'ziggy' } }) ]", ctx);
    OptimizerFactory.setDefaultOptimizer("reflective");
    assertEquals("ziggy", (((Foo) ((Map) executeExpression(s)).get("foo")).getBar().getName()));
}
Also used : Serializable(java.io.Serializable) ParserContext(org.mvel2.ParserContext)

Example 5 with Foo

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

the class WithTests method testInlineWith3.

public void testInlineWith3() {
    CompiledExpression expr = new ExpressionCompiler("foo.{name = 'poopy', aValue = 'bar', bar.{name = 'foobie'}, toUC('doopy')}").compile();
    Foo f = (Foo) executeExpression(expr, createTestMap());
    assertEquals("poopy", f.getName());
    assertEquals("bar", f.aValue);
    assertEquals("foobie", f.getBar().getName());
    assertEquals("doopy", f.register);
}
Also used : Foo(org.mvel2.tests.core.res.Foo) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) CompiledExpression(org.mvel2.compiler.CompiledExpression)

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