PageRenderTime 15ms CodeModel.GetById 2ms app.highlight 8ms RepoModel.GetById 2ms app.codeStats 0ms

/hudson-test-framework/src/main/java/org/jvnet/hudson/test/rhino/JavaScriptDebugger.java

http://github.com/hudson/hudson
Java | 75 lines | 22 code | 6 blank | 47 comment | 1 complexity | a7c5d2baaa3d4bb2b6e609a40d64088e MD5 | raw file
 1/*
 2 * The MIT License
 3 * 
 4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
 5 * 
 6 * Permission is hereby granted, free of charge, to any person obtaining a copy
 7 * of this software and associated documentation files (the "Software"), to deal
 8 * in the Software without restriction, including without limitation the rights
 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 * 
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 * 
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24package org.jvnet.hudson.test.rhino;
25
26import net.sourceforge.htmlunit.corejs.javascript.Context;
27import net.sourceforge.htmlunit.corejs.javascript.debug.DebugFrame;
28import net.sourceforge.htmlunit.corejs.javascript.debug.DebuggableScript;
29import net.sourceforge.htmlunit.corejs.javascript.debug.Debugger;
30import org.jvnet.hudson.test.HudsonTestCase;
31
32import java.util.List;
33import java.util.ArrayList;
34
35/**
36 * Monitors the execution of the JavaScript inside HtmlUnit, and provides debug information
37 * such as call stacks, variables, arguments, etc.
38 *
39 * <h2>Usage</h2>
40 * <p>
41 * When you set a break point in Java code that are directly/indirectly invoked through JavaScript,
42 * use {@link #toString()} to see the JavaScript stack trace (and variables at each stack frame.)
43 * This helps you see where the problem is.
44 *
45 * <p>
46 * TODO: add programmatic break point API, selective method invocation tracing, and
47 * allow arbitrary script evaluation in arbitrary stack frame.
48 *
49 * @author Kohsuke Kawaguchi
50 * @see HudsonTestCase#jsDebugger
51 */
52public class JavaScriptDebugger implements Debugger {
53    /**
54     * Call stack as a list. The list grows at the end, so the first element in the list
55     * is the oldest stack frame.
56     */
57    public final List<CallStackFrame> callStack = new ArrayList<CallStackFrame>();
58
59    public void handleCompilationDone(Context cx, DebuggableScript fnOrScript, String source) {
60    }
61
62    public DebugFrame getFrame(Context cx, DebuggableScript fnOrScript) {
63        return new CallStackFrame(this,fnOrScript);
64    }
65
66    /**
67     * Formats the current call stack into a human readable string.
68     */
69    public String toString() {
70        StringBuilder buf = new StringBuilder();
71        for( int i=callStack.size()-1; i>=0; i-- )
72            buf.append(callStack.get(i)).append('\n');
73        return buf.toString();
74    }
75}