use of org.mvel2.debug.Debugger in project mvel by mvel.
the class DebuggerTests method testBreakpoints4.
public void testBreakpoints4() {
String expression = "System.out.println('foo');\n" + "a = new Foo244();\n" + "update (a) { name = 'bar' };\n" + "System.out.println('name:' + a.name);\n" + "return a.name;";
Map<String, Interceptor> interceptors = new HashMap<String, Interceptor>();
Map<String, Macro> macros = new HashMap<String, Macro>();
class TestResult {
boolean firedBefore;
boolean firedAfter;
}
final TestResult result = new TestResult();
interceptors.put("Update", new Interceptor() {
public int doBefore(ASTNode node, VariableResolverFactory factory) {
((WithNode) node).getNestedStatement().getValue(null, factory);
System.out.println("fired update interceptor -- before");
result.firedBefore = true;
return 0;
}
public int doAfter(Object val, ASTNode node, VariableResolverFactory factory) {
System.out.println("fired update interceptor -- after");
result.firedAfter = true;
return 0;
}
});
macros.put("update", new Macro() {
public String doMacro() {
return "@Update with";
}
});
expression = parseMacros(expression, macros);
ParserContext ctx = new ParserContext();
ctx.setDebugSymbols(true);
ctx.setSourceFile("test2.mv");
ctx.addImport("Foo244", Foo.class);
ctx.setInterceptors(interceptors);
ExpressionCompiler compiler = new ExpressionCompiler(expression, ctx);
CompiledExpression compiled = compiler.compile();
System.out.println("\nExpression:------------");
System.out.println(expression);
System.out.println("------------");
MVELRuntime.registerBreakpoint("test2.mv", 3);
MVELRuntime.registerBreakpoint("test2.mv", 4);
MVELRuntime.registerBreakpoint("test2.mv", 5);
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("bar", MVEL.executeDebugger(compiled, null, new MapVariableResolverFactory(createTestMap())));
assertTrue("did not fire before", result.firedBefore);
assertTrue("did not fire after", result.firedAfter);
assertEquals("did not break at expected points", Make.Set.<Integer>$()._(3)._(4)._(5)._finish(), breaked);
}
use of org.mvel2.debug.Debugger in project drools by kiegroup.
the class HelloWorldTest method testHelloWorldDebug.
@Test
public void testHelloWorldDebug() throws Exception {
final Set<String> knownVariables = new HashSet<String>();
MVELRuntime.resetDebugger();
MVELDebugHandler.setDebugMode(true);
MVELRuntime.setThreadDebugger(new Debugger() {
public int onBreak(Frame frame) {
System.out.println("onBreak");
for (String var : frame.getFactory().getKnownVariables()) {
knownVariables.add(var);
}
return 0;
}
});
String source = "org.drools.integrationtests.Rule_Hello_World";
MVELRuntime.registerBreakpoint(source, 1);
// load up the knowledge base
KieBase kbase = readKnowledgeBase();
KieSession ksession = createKnowledgeSession(kbase);
File testTmpDir = new File("target/test-tmp/");
testTmpDir.mkdirs();
KieRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "target/test-tmp/testHelloWorldDebug");
// go !
Message message = new Message();
message.setMessage("Hello World");
message.setStatus(Message.HELLO);
ksession.insert(message);
ksession.fireAllRules();
logger.close();
assertEquals(6, knownVariables.size());
assertTrue(knownVariables.contains("drools"));
assertTrue(knownVariables.contains("myMessage"));
assertTrue(knownVariables.contains("rule"));
assertTrue(knownVariables.contains("kcontext"));
assertTrue(knownVariables.contains("this"));
assertTrue(knownVariables.contains("m"));
assertTrue(knownVariables.contains("myMessage"));
}
Aggregations