01: package org.mvel.tests;
02:
03: import junit.framework.TestCase;
04: import org.mvel.MVEL;
05: import org.mvel.MVELRuntime;
06: import org.mvel.ParserContext;
07: import org.mvel.compiler.CompiledExpression;
08: import org.mvel.compiler.ExpressionCompiler;
09: import static org.mvel.debug.DebugTools.decompile;
10: import org.mvel.debug.Debugger;
11: import org.mvel.debug.Frame;
12: import org.mvel.integration.VariableResolverFactory;
13: import org.mvel.integration.impl.DefaultLocalVariableResolverFactory;
14:
15: public class DebuggerTests extends TestCase {
16:
17: private static int count;
18: private static int a1 = 0;
19: private static int a4 = 0;
20:
21: public void testDebuggerInvoke() {
22: count = 0;
23:
24: MVELRuntime.resetDebugger();
25: MVELRuntime.setThreadDebugger(new Debugger() {
26: public int onBreak(Frame frame) {
27: if (frame.getFactory().getVariableResolver("a1") != null) {
28: a1++;
29: }
30: if (frame.getFactory().getVariableResolver("a4") != null) {
31: a4++;
32: System.out.println("HEI " + frame.getLineNumber());
33: }
34: count++;
35: return 0;
36: }
37: });
38:
39: String src = "a1=7;\na2=8;\na3=9;\na4=10;\na5=11;\na6=12;\na7=13;\na8=14;";
40: ExpressionCompiler c = new ExpressionCompiler(src);
41: c.setDebugSymbols(true);
42: ParserContext ctx = new ParserContext();
43: ctx.setSourceFile("mysource");
44: CompiledExpression compexpr = c.compile(ctx);
45:
46: System.out.println(decompile(compexpr));
47:
48: MVELRuntime.registerBreakpoint(ctx.getSourceFile(), 1);
49: MVELRuntime.registerBreakpoint(ctx.getSourceFile(), 3);
50: MVELRuntime.registerBreakpoint(ctx.getSourceFile(), 7);
51:
52: VariableResolverFactory factory = new DefaultLocalVariableResolverFactory();
53: MVEL.executeDebugger(compexpr, null, factory);
54:
55: System.out.println(a1);
56: System.out.println(a4);
57: System.out.println(count);
58: assertEquals(2, a1);
59: assertEquals(1, a4); // test passes but the breakpoint should be received by line 7, not by line 3
60: assertEquals(3, count); // three breakpoints FAILS
61: }
62:
63: public void testDebuggerInvoke2() {
64: count = 0;
65:
66: MVELRuntime.resetDebugger();
67: MVELRuntime.setThreadDebugger(new Debugger() {
68: public int onBreak(Frame frame) {
69: count++;
70: return 0;
71: }
72: });
73:
74: String src = "a1=7;\na2=8;\nSystem.out.println(\"h\");\nac=23;\nde=23;\nge=23;\ngef=34;";
75:
76: ExpressionCompiler c = new ExpressionCompiler(src);
77: c.setDebugSymbols(true);
78: ParserContext ctx = new ParserContext();
79: ctx.setSourceFile("mysource");
80: CompiledExpression compexpr = c.compile(ctx);
81:
82: System.out.println(decompile(compexpr));
83:
84: MVELRuntime.registerBreakpoint(ctx.getSourceFile(), 1);
85: MVELRuntime.registerBreakpoint(ctx.getSourceFile(), 2);
86: MVELRuntime.registerBreakpoint(ctx.getSourceFile(), 3);
87: MVELRuntime.registerBreakpoint(ctx.getSourceFile(), 4);
88: MVELRuntime.registerBreakpoint(ctx.getSourceFile(), 5);
89:
90: VariableResolverFactory factory = new DefaultLocalVariableResolverFactory();
91: MVEL.executeDebugger(compexpr, null, factory);
92:
93: System.out.println(count);
94: assertEquals(5, count);
95: }
96:
97: }
|