summaryrefslogtreecommitdiffstats
path: root/tests/new/AJDBTest.java
blob: 197539fe1b561396949d3798452caf6e88a7db07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import org.aspectj.tools.ajdb.Main;
import org.aspectj.debugger.tty.CommandLineDebugger;
import org.aspectj.debugger.base.*;
import com.sun.jdi.*;
import com.sun.jdi.event.*;
import java.io.*;
import java.util.*;
import org.aspectj.testing.Tester;

public class AJDBTest implements StopListener, VMListener {
    
    String classpath;
    {
        classpath = "\"" + Tester.outputDir();
        String javaClasspath = System.getProperty("java.class.path");
        if (javaClasspath != null) {
            classpath += ";" + javaClasspath;
        }
        classpath += "\"";
    }

    protected String[] stringCommands() {
        return new String[] {
            "workingdir " + Tester.workingDir(),
            "use " + "./new",
            "stop on AJDBClass.java:24",
            "run -classpath " + classpath + " AJDBClass",
        };
    }
    protected String getSourceName() {
        return "AJDBClass.java";
    }

    // Methods for VMListener
    protected void death(VMDeathEvent e) {
        System.out.println("*** Death: " + e);
    }
    protected void disconnect(VMDisconnectEvent e) {
        System.out.println("*** Disconnect: " + e);
    }
    protected void start(VMStartEvent e) {
        System.out.println("*** Start: " + e);
    }
    
    // Methods for StopListener
    protected void access(AccessWatchpointEvent e) {
    }
    protected void breakpoint(BreakpointEvent e) {
        checkLines((List) ex("where"), "next");
    }
    protected void exception(ExceptionEvent e) {
    }
    protected void modification(ModificationWatchpointEvent e) {
    }
    protected void step(StepEvent e) {
        List lines = (List) ex("where");
        checkLines(lines);
        try {
            StackFrame frame = (StackFrame) lines.get(0);
            Location loc = frame.location();
            if (loc.sourceName().equals("Thread.java") &&
                loc.method().name().equals("exit")) {
                isRunning = false;
            }
        } catch (Throwable t) {}
        ex("next");

    }

    public void checkLines(Collection lines, Object then) {
        checkLines(lines);
        if (then != null) ex(then);
    }

    public void checkLines(Collection lines) {
        Iterator iter = lines.iterator();
        while (iter.hasNext()) {
            StackFrame frame = (StackFrame) iter.next();
            String source = "no.source";
            try {
                source = debugger.sourceName(frame.location());
            } catch (Throwable t) {}
            int line = debugger.lineNumber(frame.location());
            if (source.equals(getSourceName())) {
                Tester.check(line>0, "non-mapping line for " + frame);
            }
        }
    }

    // VMListener
    public void vmDeathEvent(VMDeathEvent e) {
        death(e);
    }
    public void vmDisconnectEvent(VMDisconnectEvent e) {
        disconnect(e);
    }
    public void vmStartEvent(VMStartEvent e) {
        start(e);
    }

    // StopListener
    public final void accessWatchpointEvent(AccessWatchpointEvent e) {
        access(e);
    }
    public final void breakpointEvent(BreakpointEvent e) {
        breakpoint(e);
    }
    public final void exceptionEvent(ExceptionEvent e) {
        exception(e);
    }
    public final void modificationWatchpointEvent(ModificationWatchpointEvent e) {
        modification(e);
    }
    public final void stepEvent(StepEvent e) {
        step(e);
    }

    AJDebugger debugger;
    CommandLineDebugger ajdb;
    
    public void realMain(String[] args) {
        String fileName = null;
        String[] newArgs = args;
        if (args.length > 0) {
            fileName = args[0];
            newArgs = new String[args.length-1];
            System.arraycopy(args, 1, newArgs, 0, newArgs.length);
        }
        realMain(fileName, newArgs);
    }

    private void realMain(String fileName, String[] args) {
        debugger = (ajdb = new Main().debug(args)).getDebugger();
        debugger.addStopListener(this);
        debugger.addVMListener(this);
        ex(fileName == null ? commands() : commands(fileName));
        while (isRunning) {
            //System.out.println(">>>>>>>> " + debugger.isRunning());
        }
    }
    private boolean isRunning = true;

    public final Collection commands() {
        Collection list = new Vector();
        String[] commands = stringCommands();
        for (int i = 0; i < commands.length; i++) {
            list.add(commands[i]);
        }
        return list;
    }

    Object ex(Object command) {
        return ajdb.executeCommand(command+"");
    }

    void ex(Collection list) {
        Iterator iter = list.iterator();
        while (iter.hasNext()) {
            ex(iter.next());
        }
    }

    final static String COMMENT = "#";
    final static String FILENAME = "script.txt";
    Collection commands(String fileName) {
        Collection list = new Vector();
        try {
            BufferedReader in = new BufferedReader(new FileReader(fileName));
            String line;
            while ((line = in.readLine()) != null) {
                line = line.trim();
                if (line.startsWith(COMMENT)) {
                    continue;
                }
                list.add(line);
            }
            in.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
            System.exit(-1);
        }
        return list;
    }

    public static void main(String[] args) {
        new AJDBTest().realMain(args);
    }
}