Search in sources :

Example 41 with Foo

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

the class ControlFlowTests method testQualifiedForLoop.

public void testQualifiedForLoop() {
    ParserContext pCtx = new ParserContext();
    pCtx.setStrongTyping(true);
    pCtx.addImport(Foo.class);
    pCtx.addInput("l", ArrayList.class, new Class[] { Foo.class });
    List l = new ArrayList();
    l.add(new Foo());
    l.add(new Foo());
    l.add(new Foo());
    Map vars = new HashMap();
    vars.put("l", l);
    Serializable s = MVEL.compileExpression("String s = ''; for (Foo f : l) { s += f.name }; s", pCtx);
    String r = (String) MVEL.executeExpression(s, vars);
    assertEquals("dogdogdog", r);
}
Also used : Serializable(java.io.Serializable) Foo(org.mvel2.tests.core.res.Foo) ParserContext(org.mvel2.ParserContext)

Example 42 with Foo

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

the class CoreConfidenceTests method testJIRA156c.

public void testJIRA156c() throws Throwable {
    ClassProvider provider = new ClassProvider();
    provider.getPublic().foo();
    PublicClass.class.getMethod("foo").invoke(provider.getPublic());
    String script = "provider.getPublic().foo()";
    Serializable s = MVEL.compileExpression(script);
    HashMap<String, Object> vars = new HashMap<String, Object>();
    vars.put("provider", provider);
    MVEL.eval(script, vars);
    OptimizerFactory.setDefaultOptimizer("reflective");
    executeExpression(s, vars);
    OptimizerFactory.setDefaultOptimizer("ASM");
    executeExpression(s, vars);
}
Also used : Serializable(java.io.Serializable) PublicClass(org.mvel2.tests.core.res.res2.PublicClass) ClassProvider(org.mvel2.tests.core.res.res2.ClassProvider)

Example 43 with Foo

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

the class IndexedVariablesTests method testVariableInjection2.

public void testVariableInjection2() {
    String[] varNames = { "x", "y", "z" };
    Object[] values = { 10, 20, 30 };
    String expr = "foo = -1; res = x + y + z;\n" + "if (x > 9) {\n" + "   res = z - y - x;\n" + "   int k = 5;\n" + "   foo = k;" + "}; \n" + "for (i = 0; i < 100000; i++) { foo++; }; foo;";
    ParserContext ctx = ParserContext.create();
    ctx.addIndexedInput(varNames);
    ctx.setIndexAllocation(true);
    SimpleVariableSpaceModel model = VariableSpaceCompiler.compile(expr, ctx);
    Serializable indexCompile = MVEL.compileExpression(expr, ctx);
    Serializable dynamicCompile = MVEL.compileExpression(expr, ParserContext.create());
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("x", 10);
    map.put("y", 20);
    map.put("z", 30);
    assertEquals(MVEL.executeExpression(dynamicCompile, map), MVEL.executeExpression(indexCompile, model.createFactory(values)));
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) SimpleVariableSpaceModel(org.mvel2.util.SimpleVariableSpaceModel) ParserContext(org.mvel2.ParserContext)

Example 44 with Foo

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

the class CoreConfidenceTests method testSetAccessorOverloadedEqualsStrictMode.

public void testSetAccessorOverloadedEqualsStrictMode() {
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addInput("foo", Foo.class);
    try {
        CompiledExpression expr = new ExpressionCompiler("foo.bar = 0").compile(ctx);
    } catch (CompileException e) {
        // should fail.
        e.printStackTrace();
        return;
    }
    assertTrue(false);
}
Also used : ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) CompiledExpression(org.mvel2.compiler.CompiledExpression)

Example 45 with Foo

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

the class DebuggerTests method testBreakpoints5.

public void testBreakpoints5() {
    OptimizerFactory.setDefaultOptimizer("ASM");
    String expression = "System.out.println('foo');\r\n" + "a = new Foo244();\r\n" + "a.name = 'bar';\r\n" + "foo.happy();\r\n" + "System.out.println( 'name:' + a.name );               \r\n" + "System.out.println( 'name:' + a.name );         \r\n" + "System.out.println( 'name:' + a.name );     \r\n" + "return a.name;";
    Map<String, Interceptor> interceptors = new HashMap<String, Interceptor>();
    Map<String, Macro> macros = new HashMap<String, Macro>();
    expression = parseMacros(expression, macros);
    ExpressionCompiler compiler = new ExpressionCompiler(expression);
    ParserContext ctx = new ParserContext();
    ctx.setSourceFile("test2.mv");
    ctx.setDebugSymbols(true);
    ctx.addImport("Foo244", Foo.class);
    ctx.setInterceptors(interceptors);
    CompiledExpression compiled = compiler.compile(ctx);
    System.out.println("\nExpression:------------");
    System.out.println(expression);
    System.out.println("------------");
    System.out.println(DebugTools.decompile(compiled));
    MVELRuntime.registerBreakpoint("test2.mv", 1);
    final Set<Integer> breaked = new HashSet<Integer>();
    Debugger testDebugger = new Debugger() {

        public int onBreak(Frame frame) {
            System.out.println("Breakpoint [source:" + frame.getSourceName() + "; line:" + frame.getLineNumber() + "]");
            breaked.add(frame.getLineNumber());
            return Debugger.STEP_OVER;
        }
    };
    MVELRuntime.setThreadDebugger(testDebugger);
    System.out.println("\n==RUN==\n");
    assertEquals("bar", MVEL.executeDebugger(compiled, null, new MapVariableResolverFactory(createTestMap())));
    assertTrue("did not break at line 1", breaked.contains(1));
}
Also used : Debugger(org.mvel2.debug.Debugger) Frame(org.mvel2.debug.Frame) HashMap(java.util.HashMap) Macro(org.mvel2.Macro) CompiledExpression(org.mvel2.compiler.CompiledExpression) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext) Interceptor(org.mvel2.integration.Interceptor) HashSet(java.util.HashSet)

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