summaryrefslogtreecommitdiffstats
path: root/tests/debugger/Tester.java
blob: 770c04c44c395037d1bbc743c6f69f8c4b55ade4 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
//package debugger;

import com.sun.jdi.*;
import com.sun.jdi.event.*;
import com.sun.jdi.request.*;
import java.io.*;
import java.util.*;
import org.aspectj.tools.debugger.*;

/**
 * Tester.java
 *
 *
 * Created: Wed Sep 06 15:53:29 2000
 *
 * @author <a href="mailto:palm@parc.xerox.com"Jeffrey Palm</a>
 */

public abstract class Tester extends DebuggerAdapter implements DebuggerListener {

    public abstract boolean test();
    public abstract String getClassName();

    public       static String  ROOT         = "."; //"C:/aspectj/tests/debugger";
    public final static String  PCKG         = ""; //"debugger.";
    public final static String  PATH         = ""; //"debugger/";
    public              String  FILE         = getClassName() + ".java";
    public              String  CLASS        = PCKG + getClassName();
    public       static String  classPath    = "..";
    public              int     time         = 0;    
    public       static boolean verboseSuccess = false; //true;    
    protected AJDebugger d;
    protected PrintStream out = System.out;
    protected PrintStream err = System.err;
    protected boolean mutex;
    protected boolean good = true;
    protected Vector failures = new Vector();

    protected static boolean debug = false; //true;
    public static void setDebug(boolean _debug) {
        debug = _debug;
    }

    protected final static String  errFile = "err.txt";

    public Tester(boolean debug) {
        this.debug = debug;
        this.classPath = classPath;
        if (debug()) {
            outln("Testing..." + getClassName());
        }
        setErr();
    }
    
    public Tester() {
        this(false);
    }

    public void go(String[] args){
        good &= test();
        sd();
        if (!good) {
            outln("The test failed with the following:\n" + d.iter(failures));
        }
    }

    public boolean debug() {
        return debug | false;
    }

    public static void setClassPath(String _classPath) {
        classPath = _classPath;
    }

    public static void setRoot(String root) {
        ROOT = root;
    }

    public static void setVerbose(boolean _verboseSuccess) {
        verboseSuccess = _verboseSuccess;
    }

    /****************************** Tests ******************************/

    protected HashMap breaks = new HashMap();

    static class IntVector extends Vector {
        public void add(int i) {
            super.add(new Integer(i));
        }
    }

    protected void quit() throws DebuggerException {
        db("Quitting tester..." + getClassName());
        d.quitCommand();
        //d.exit(false);
        d = null;
        db("Quit.");
    }

    protected Value print(Object obj) throws DebuggerException {
        return d.printCommand(obj);
    }

    protected void stopin(String method) throws DebuggerException {
        stopin(getClassName(), method);
    }

    protected void stopin(String className, String method) throws DebuggerException {
        d.stopInCommand(PCKG + className, method);        
    }

    protected void stopat(int line) throws DebuggerException {
        stopat(CLASS, line);
    }

    protected void stopat(String className, int line) throws DebuggerException {
        d.stopAtCommand(PCKG + className, line);
    }

    protected void stopon(int line) throws DebuggerException {
        d.stopOnCommand(PATH + FILE, line);
    }

    protected void clear(int line) throws DebuggerException {
        d.clearOnCommand(PATH + FILE, line);
    }

    protected void clear(String className, int line) throws DebuggerException {
        d.clearAtCommand(PCKG + className, line);
    }

    protected void clear(String method) throws DebuggerException {
        clear(CLASS, method);
    }

    protected void clear(String className, String method) throws DebuggerException {
        d.clearInCommand(PCKG + className, method);        
    }    

    protected void step() throws DebuggerException {
        d.stepCommand();
    }

    protected void stepi() throws DebuggerException {
        d.stepiCommand();
    }

    protected void stepup() throws DebuggerException {
        d.stepUpCommand();
    }

    protected void next() throws DebuggerException {
        d.nextCommand();
    }

    protected void de(Throwable de) {
        de.printStackTrace();
        good = false;
    }
   
    static class Case {
        String msg;
        int line;
        int frames;
        List locals;
        List names;
        List sizes;
        int time;
        public Case(String msg, int line, int frames, List locals, List names, List sizes, int time) {
            this.msg = msg;
            this.line = line;
            this.frames = frames;
            this.locals = locals;
            this.names = names;
            this.sizes = sizes;
            this.time  = time;
        }
        public String toString() {
            return
                "msg=" + msg +
                " line=" + line +
                " frames=" + frames +
                " locals=" + locals +
                " names=" + names +
                " sizes=" + sizes +
                " time=" + time;
        }
    }

    protected void stop(final Vector cases) {
        d.addStopListener(new StopAdapter() {
                public void breakpointEvent(BreakpointEvent e) {
                    try {                            
                        if (cases.size() > time) {
                            Case caze = (Case) cases.get(time);
                            //System.out.println(caze);
                            //System.out.println(d.format(e));
                            String msg = caze.msg;
                            int line = caze.line;
                            int frames = caze.frames;
                            List locals = caze.locals;
                            List names = caze.names;
                            List sizes = caze.sizes;
                            int caseTime = caze.time;
                            check(time == caseTime, "Out of sync " + time + ":" + caseTime);
                            int lineNumber = d.lineNumber(e.location());
                            String methodName = d.methodName(e);
                            if (lineNumber > 0) {
                                check(lineNumber == line, "Lines don't match " +
                                       lineNumber + ":" + line);
                            } else {
                                check(msg.endsWith(methodName), 
                                       "Method '" + msg + "' does not match '" + methodName + "'.");
                            }
                            msg(msg + ": " + d.format(e));                                
                            threads(names, sizes);
                            where("", frames);
                            locals(locals);
                            cont();
                        }
                    } catch (/*Debugger*/Exception de) {
                        de.printStackTrace(out);
                        good = false;
                    }
                    time++;
                }});        
    }

    protected boolean locals(List locals) throws DebuggerException {
        List vars = d.localsCommand();
        boolean allGood = true;
        for (int i = 0; i < locals.size(); i++) {
            boolean there = false;
            if (vars != null) {
                for (int j = 0; j < vars.size(); j++) {
                    LocalVariable lv = (LocalVariable) vars.get(j);
                    if (lv.name().equals(locals.get(i))) {
                        there = true;
                    }
                }
            }
            allGood &= check(there, "The local variable '" + locals.get(i) +
                              "' was not found in\n" + d.locals(vars));
        }
        return allGood;
    }

    protected void threads(List names, List sizes) throws DebuggerException {
        for (int i = 0; i < names.size(); i++) {
            List threads = d.threadsCommand(names.get(i) + "");
            check(threads.size() == ((Integer) sizes.get(i)).intValue(),
                   "need " + sizes.get(i) + " thread(s) in '" + names.get(i) + "':\n" + d.threads(threads));
        }
    }
    
    protected void where(String name, int frames) throws DebuggerException {
        try {
            List stack = d.whereCommand(name);            
            check(stack.size() == frames,
                   "need " + frames + " frame(s) in '" + name + "':\n" + d.frames(stack));
        } catch (WhereRequest.BadThreadStateException e) {
            //TODO
        }
    }

    /****************************** DebuggerListener ******************************/

    public void requestSetEvent(RequestEvent re) {
        msg("Set " + re.getRequest());
    }
    public void requestClearEvent(RequestEvent re) {
        msg("Cleared " + re.getRequest());
    }    
    public void requestDeferredEvent(RequestEvent re) {

    }
    public void requestFailedEvent(RequestEvent re) {
        msg("Unable to set " + re.getRequest() + " : " + re.getErrorMessage());
    }    


    /****************************** Misc. ******************************/

    protected void setErr() {
        try {
            err = new PrintStream(new BufferedOutputStream(new FileOutputStream(errFile)), true) {
                    public void write(int b) {
                        super.write(b);
                    }
                };            
        } catch (IOException ioe) {
        }
        System.setErr(err);        
    }

    protected void setOut() {
        PrintStream redirect = new PrintStream(new OutputStream() {
                public void write(int b) {}
            });        
        System.setOut(redirect);
    }

    protected void down() {
        mutex = true;
    }

    protected void up() {
        mutex = false;
    }

    protected void stall() {
        stall(getMaxStallTime());
    }

    protected long getMaxStallTime() {
        return (long) 20000;
    }

    protected void stall(long time) {
        long start = System.currentTimeMillis();
        while (mutex) {
            if ((System.currentTimeMillis() - start) > time) {
                errln("Stalled for too long");
                break;
            }
        }
    }

    protected void cont() {
        try {
            d.contCommand();
        } catch (DebuggerException de) {
        }
    }

    protected void sd() {
        if (d != null) {
            d.shutDown();
        }
        d = null;
    }

    protected void db(Object o) {
        if (debug()) {
            System.out.println(o);
        }
    }
    
    protected void db() {
        sd();
        d = new AJDebugger(this, false);
        d.addDebuggerListener(this);
        ex("use " + ROOT);
    }

    protected void stop() {
        stop(5000);
    }

    protected void stop(long time) {
        long start = System.currentTimeMillis();
        while (!d.isAtBreakpoint()) {
            if ((System.currentTimeMillis() - start) > time) {
                errln("Stopped for too long");
                break;
            }
        }
    }
    
    protected Object ex(String command) {
        return d.execute(command);
    }
    
    public void outln(Object o) {
        if ((o+"").startsWith("Initializing ajdb...")) {
            return;
        }
        out(o);
        out("\n");
    }
    
    protected void out(Object o) {
        out.print(o);
        out.flush();
    }

    protected void err(Object o) {
        err.print(o);
        err.flush();
    }

    protected void errln(Object o) {
        err(o);
        err("\n");
    }

    protected boolean check(boolean b, String msg) {
        if (!b) {
            outln("<<FAIL>> " + msg);
            good = false;
            failures.add(msg);
        } else if (verboseSuccess) {
            outln("<<SUCESS>> " + msg);
        }
        return b;
    }

    protected boolean  check(Object o, String msg) {
        return check(o != null, msg);
    }
    
    protected void msg(Object o) {
        if (debug()) {
            outln(o);
        } else {
            err.println(o);
        }
    }

    private String runArgs = "";
    public String getRunArgs() {
        return runArgs;
    }
    public void setRunArgs(String runArgs) {
        this.runArgs = runArgs;
    }

    private final String _getArgs() {
        String args = getRunArgs();
        if (args != null && !args.equals("") && !args.startsWith(" ")) {
            args = " " + args;
        }
        return args;
    }

    protected void startTest() {
        String cmd = "run " + classPath() + " " + CLASS + _getArgs();
        startTest(cmd);
    }

    protected static String classPath() {
        if (classPath == null || classPath.equals("")) {
            return "";
        }
        return "-classpath \"" + classPath + "\"";
    }

    protected void startTest(String cmd) {
        d.addVMListener(new VMAdapter() {
                public void vmDisconnectEvent(VMDisconnectEvent e) {
                    msg("Done");
                    up();
                }});
        ex(cmd);
        down();
        stall();
    }
}