use of org.mule.mvel2.ParserContext in project mvel by mvel.
the class DebuggerTests method testDebugSymbolsSingleStatement.
public void testDebugSymbolsSingleStatement() {
String ex = "System.out.println( Cheese.STILTON );";
ParserContext ctx = new ParserContext();
ctx.setStrongTyping(true);
ctx.addImport(Cheese.class);
try {
ExpressionCompiler compiler = new ExpressionCompiler(ex, ctx);
CompiledExpression expr = compiler.compile();
// executing the following line with a MVEL.executeExpression() works fine
// but executeDebugger() fails
MVEL.executeDebugger(expr, null, (VariableResolverFactory) null);
} catch (Throwable e) {
e.printStackTrace();
fail("Should not raise exception: " + e.getMessage());
}
}
use of org.mule.mvel2.ParserContext in project mvel by mvel.
the class DebuggerTests method testBreakpoints3.
public void testBreakpoints3() {
String expr = "System.out.println( \"a1\" );\n" + "System.out.println( \"a2\" );\n" + "System.out.println( \"a3\" );\n" + "System.out.println( \"a4\" );\n";
ParserContext context = new ParserContext();
context.addImport("System", System.class);
context.setStrictTypeEnforcement(true);
context.setDebugSymbols(true);
context.setSourceFile("mysource");
ExpressionCompiler compiler = new ExpressionCompiler(expr, context);
String s = org.mvel2.debug.DebugTools.decompile(compiler.compile());
System.out.println("output: " + s);
int fromIndex = 0;
int count = 0;
while ((fromIndex = s.indexOf("DEBUG_SYMBOL", fromIndex + 1)) > -1) {
count++;
}
assertEquals(4, count);
}
use of org.mule.mvel2.ParserContext in project mvel by mvel.
the class DebuggerTests method testBreakpoints.
public void testBreakpoints() {
ParserContext ctx = new ParserContext();
ctx.setSourceFile("test.mv");
ctx.setDebugSymbols(true);
ExpressionCompiler compiler = new ExpressionCompiler("a = 5;\nb = 5;\n\nif (a == b) {\n\nSystem.out.println('Good');\nreturn a + b;\n}\n", ctx);
System.out.println("-------\n" + compiler.getExpression() + "\n-------\n");
CompiledExpression compiled = compiler.compile();
MVELRuntime.registerBreakpoint("test.mv", 7);
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 0;
}
};
MVELRuntime.setThreadDebugger(testDebugger);
assertEquals(10, MVEL.executeDebugger(compiled, null, new MapVariableResolverFactory(createTestMap())));
assertTrue("did not break at line 7", breaked.contains(7));
}
use of org.mule.mvel2.ParserContext in project mvel by mvel.
the class IndexedVariablesTests method testVariableInjection1.
public void testVariableInjection1() {
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 < 5000; i++) { foo++; }; foo;";
ParserContext ctx = ParserContext.create();
ctx.addIndexedInput(varNames);
ctx.setIndexAllocation(true);
SharedVariableSpaceModel model = VariableSpaceCompiler.compileShared(expr, ctx, values);
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()));
//
// for (int x = 0; x < 10; x++) {
// long tm = System.currentTimeMillis();
// for (int i = 0; i < 10000; i++) {
// MVEL.executeExpression(indexCompile, model.createFactory());
// }
// tm = System.currentTimeMillis() - tm;
// System.out.println("(StaticInjection (ms): " + tm + ")");
//
// tm = System.currentTimeMillis();
// Map<String, Object> map = new HashMap<String, Object>();
// map.put("x", 10);
// map.put("y", 20);
// map.put("z", 30);
//
// MapVariableResolverFactory factory = new MapVariableResolverFactory(map);
// for (int i = 0; i < 10000; i++) {
// MVEL.executeExpression(dynamicCompile, factory);
// }
// tm = System.currentTimeMillis() - tm;
// System.out.println("(MapInjection (ms): " + tm + ")");
// }
}
use of org.mule.mvel2.ParserContext in project mvel by mvel.
the class IndexedVariablesTests method testVariableInjection3.
public void testVariableInjection3() {
String[] varNames = { "x", "y", "z" };
Object[] values = { 10, 20, 30 };
String expr = "def add(a,b) { a + b }; foo = -1; res = x + y + z;\n" + "if (x > 9) {\n" + " res = z - y - x;\n" + " int k = 5;\n" + " foo = add(5,10);" + "}; \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)));
}
Aggregations