|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459 |
- //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();
- }
- }
|