Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. //package debugger;
  2. import com.sun.jdi.*;
  3. import com.sun.jdi.event.*;
  4. import com.sun.jdi.request.*;
  5. import java.io.*;
  6. import java.util.*;
  7. import org.aspectj.tools.debugger.*;
  8. /**
  9. * Tester.java
  10. *
  11. *
  12. * Created: Wed Sep 06 15:53:29 2000
  13. *
  14. * @author <a href="mailto:palm@parc.xerox.com"Jeffrey Palm</a>
  15. */
  16. public abstract class Tester extends DebuggerAdapter implements DebuggerListener {
  17. public abstract boolean test();
  18. public abstract String getClassName();
  19. public static String ROOT = "."; //"C:/aspectj/tests/debugger";
  20. public final static String PCKG = ""; //"debugger.";
  21. public final static String PATH = ""; //"debugger/";
  22. public String FILE = getClassName() + ".java";
  23. public String CLASS = PCKG + getClassName();
  24. public static String classPath = "..";
  25. public int time = 0;
  26. public static boolean verboseSuccess = false; //true;
  27. protected AJDebugger d;
  28. protected PrintStream out = System.out;
  29. protected PrintStream err = System.err;
  30. protected boolean mutex;
  31. protected boolean good = true;
  32. protected Vector failures = new Vector();
  33. protected static boolean debug = false; //true;
  34. public static void setDebug(boolean _debug) {
  35. debug = _debug;
  36. }
  37. protected final static String errFile = "err.txt";
  38. public Tester(boolean debug) {
  39. this.debug = debug;
  40. this.classPath = classPath;
  41. if (debug()) {
  42. outln("Testing..." + getClassName());
  43. }
  44. setErr();
  45. }
  46. public Tester() {
  47. this(false);
  48. }
  49. public void go(String[] args){
  50. good &= test();
  51. sd();
  52. if (!good) {
  53. outln("The test failed with the following:\n" + d.iter(failures));
  54. }
  55. }
  56. public boolean debug() {
  57. return debug | false;
  58. }
  59. public static void setClassPath(String _classPath) {
  60. classPath = _classPath;
  61. }
  62. public static void setRoot(String root) {
  63. ROOT = root;
  64. }
  65. public static void setVerbose(boolean _verboseSuccess) {
  66. verboseSuccess = _verboseSuccess;
  67. }
  68. /****************************** Tests ******************************/
  69. protected HashMap breaks = new HashMap();
  70. static class IntVector extends Vector {
  71. public void add(int i) {
  72. super.add(new Integer(i));
  73. }
  74. }
  75. protected void quit() throws DebuggerException {
  76. db("Quitting tester..." + getClassName());
  77. d.quitCommand();
  78. //d.exit(false);
  79. d = null;
  80. db("Quit.");
  81. }
  82. protected Value print(Object obj) throws DebuggerException {
  83. return d.printCommand(obj);
  84. }
  85. protected void stopin(String method) throws DebuggerException {
  86. stopin(getClassName(), method);
  87. }
  88. protected void stopin(String className, String method) throws DebuggerException {
  89. d.stopInCommand(PCKG + className, method);
  90. }
  91. protected void stopat(int line) throws DebuggerException {
  92. stopat(CLASS, line);
  93. }
  94. protected void stopat(String className, int line) throws DebuggerException {
  95. d.stopAtCommand(PCKG + className, line);
  96. }
  97. protected void stopon(int line) throws DebuggerException {
  98. d.stopOnCommand(PATH + FILE, line);
  99. }
  100. protected void clear(int line) throws DebuggerException {
  101. d.clearOnCommand(PATH + FILE, line);
  102. }
  103. protected void clear(String className, int line) throws DebuggerException {
  104. d.clearAtCommand(PCKG + className, line);
  105. }
  106. protected void clear(String method) throws DebuggerException {
  107. clear(CLASS, method);
  108. }
  109. protected void clear(String className, String method) throws DebuggerException {
  110. d.clearInCommand(PCKG + className, method);
  111. }
  112. protected void step() throws DebuggerException {
  113. d.stepCommand();
  114. }
  115. protected void stepi() throws DebuggerException {
  116. d.stepiCommand();
  117. }
  118. protected void stepup() throws DebuggerException {
  119. d.stepUpCommand();
  120. }
  121. protected void next() throws DebuggerException {
  122. d.nextCommand();
  123. }
  124. protected void de(Throwable de) {
  125. de.printStackTrace();
  126. good = false;
  127. }
  128. static class Case {
  129. String msg;
  130. int line;
  131. int frames;
  132. List locals;
  133. List names;
  134. List sizes;
  135. int time;
  136. public Case(String msg, int line, int frames, List locals, List names, List sizes, int time) {
  137. this.msg = msg;
  138. this.line = line;
  139. this.frames = frames;
  140. this.locals = locals;
  141. this.names = names;
  142. this.sizes = sizes;
  143. this.time = time;
  144. }
  145. public String toString() {
  146. return
  147. "msg=" + msg +
  148. " line=" + line +
  149. " frames=" + frames +
  150. " locals=" + locals +
  151. " names=" + names +
  152. " sizes=" + sizes +
  153. " time=" + time;
  154. }
  155. }
  156. protected void stop(final Vector cases) {
  157. d.addStopListener(new StopAdapter() {
  158. public void breakpointEvent(BreakpointEvent e) {
  159. try {
  160. if (cases.size() > time) {
  161. Case caze = (Case) cases.get(time);
  162. //System.out.println(caze);
  163. //System.out.println(d.format(e));
  164. String msg = caze.msg;
  165. int line = caze.line;
  166. int frames = caze.frames;
  167. List locals = caze.locals;
  168. List names = caze.names;
  169. List sizes = caze.sizes;
  170. int caseTime = caze.time;
  171. check(time == caseTime, "Out of sync " + time + ":" + caseTime);
  172. int lineNumber = d.lineNumber(e.location());
  173. String methodName = d.methodName(e);
  174. if (lineNumber > 0) {
  175. check(lineNumber == line, "Lines don't match " +
  176. lineNumber + ":" + line);
  177. } else {
  178. check(msg.endsWith(methodName),
  179. "Method '" + msg + "' does not match '" + methodName + "'.");
  180. }
  181. msg(msg + ": " + d.format(e));
  182. threads(names, sizes);
  183. where("", frames);
  184. locals(locals);
  185. cont();
  186. }
  187. } catch (/*Debugger*/Exception de) {
  188. de.printStackTrace(out);
  189. good = false;
  190. }
  191. time++;
  192. }});
  193. }
  194. protected boolean locals(List locals) throws DebuggerException {
  195. List vars = d.localsCommand();
  196. boolean allGood = true;
  197. for (int i = 0; i < locals.size(); i++) {
  198. boolean there = false;
  199. if (vars != null) {
  200. for (int j = 0; j < vars.size(); j++) {
  201. LocalVariable lv = (LocalVariable) vars.get(j);
  202. if (lv.name().equals(locals.get(i))) {
  203. there = true;
  204. }
  205. }
  206. }
  207. allGood &= check(there, "The local variable '" + locals.get(i) +
  208. "' was not found in\n" + d.locals(vars));
  209. }
  210. return allGood;
  211. }
  212. protected void threads(List names, List sizes) throws DebuggerException {
  213. for (int i = 0; i < names.size(); i++) {
  214. List threads = d.threadsCommand(names.get(i) + "");
  215. check(threads.size() == ((Integer) sizes.get(i)).intValue(),
  216. "need " + sizes.get(i) + " thread(s) in '" + names.get(i) + "':\n" + d.threads(threads));
  217. }
  218. }
  219. protected void where(String name, int frames) throws DebuggerException {
  220. try {
  221. List stack = d.whereCommand(name);
  222. check(stack.size() == frames,
  223. "need " + frames + " frame(s) in '" + name + "':\n" + d.frames(stack));
  224. } catch (WhereRequest.BadThreadStateException e) {
  225. //TODO
  226. }
  227. }
  228. /****************************** DebuggerListener ******************************/
  229. public void requestSetEvent(RequestEvent re) {
  230. msg("Set " + re.getRequest());
  231. }
  232. public void requestClearEvent(RequestEvent re) {
  233. msg("Cleared " + re.getRequest());
  234. }
  235. public void requestDeferredEvent(RequestEvent re) {
  236. }
  237. public void requestFailedEvent(RequestEvent re) {
  238. msg("Unable to set " + re.getRequest() + " : " + re.getErrorMessage());
  239. }
  240. /****************************** Misc. ******************************/
  241. protected void setErr() {
  242. try {
  243. err = new PrintStream(new BufferedOutputStream(new FileOutputStream(errFile)), true) {
  244. public void write(int b) {
  245. super.write(b);
  246. }
  247. };
  248. } catch (IOException ioe) {
  249. }
  250. System.setErr(err);
  251. }
  252. protected void setOut() {
  253. PrintStream redirect = new PrintStream(new OutputStream() {
  254. public void write(int b) {}
  255. });
  256. System.setOut(redirect);
  257. }
  258. protected void down() {
  259. mutex = true;
  260. }
  261. protected void up() {
  262. mutex = false;
  263. }
  264. protected void stall() {
  265. stall(getMaxStallTime());
  266. }
  267. protected long getMaxStallTime() {
  268. return (long) 20000;
  269. }
  270. protected void stall(long time) {
  271. long start = System.currentTimeMillis();
  272. while (mutex) {
  273. if ((System.currentTimeMillis() - start) > time) {
  274. errln("Stalled for too long");
  275. break;
  276. }
  277. }
  278. }
  279. protected void cont() {
  280. try {
  281. d.contCommand();
  282. } catch (DebuggerException de) {
  283. }
  284. }
  285. protected void sd() {
  286. if (d != null) {
  287. d.shutDown();
  288. }
  289. d = null;
  290. }
  291. protected void db(Object o) {
  292. if (debug()) {
  293. System.out.println(o);
  294. }
  295. }
  296. protected void db() {
  297. sd();
  298. d = new AJDebugger(this, false);
  299. d.addDebuggerListener(this);
  300. ex("use " + ROOT);
  301. }
  302. protected void stop() {
  303. stop(5000);
  304. }
  305. protected void stop(long time) {
  306. long start = System.currentTimeMillis();
  307. while (!d.isAtBreakpoint()) {
  308. if ((System.currentTimeMillis() - start) > time) {
  309. errln("Stopped for too long");
  310. break;
  311. }
  312. }
  313. }
  314. protected Object ex(String command) {
  315. return d.execute(command);
  316. }
  317. public void outln(Object o) {
  318. if ((o+"").startsWith("Initializing ajdb...")) {
  319. return;
  320. }
  321. out(o);
  322. out("\n");
  323. }
  324. protected void out(Object o) {
  325. out.print(o);
  326. out.flush();
  327. }
  328. protected void err(Object o) {
  329. err.print(o);
  330. err.flush();
  331. }
  332. protected void errln(Object o) {
  333. err(o);
  334. err("\n");
  335. }
  336. protected boolean check(boolean b, String msg) {
  337. if (!b) {
  338. outln("<<FAIL>> " + msg);
  339. good = false;
  340. failures.add(msg);
  341. } else if (verboseSuccess) {
  342. outln("<<SUCESS>> " + msg);
  343. }
  344. return b;
  345. }
  346. protected boolean check(Object o, String msg) {
  347. return check(o != null, msg);
  348. }
  349. protected void msg(Object o) {
  350. if (debug()) {
  351. outln(o);
  352. } else {
  353. err.println(o);
  354. }
  355. }
  356. private String runArgs = "";
  357. public String getRunArgs() {
  358. return runArgs;
  359. }
  360. public void setRunArgs(String runArgs) {
  361. this.runArgs = runArgs;
  362. }
  363. private final String _getArgs() {
  364. String args = getRunArgs();
  365. if (args != null && !args.equals("") && !args.startsWith(" ")) {
  366. args = " " + args;
  367. }
  368. return args;
  369. }
  370. protected void startTest() {
  371. String cmd = "run " + classPath() + " " + CLASS + _getArgs();
  372. startTest(cmd);
  373. }
  374. protected static String classPath() {
  375. if (classPath == null || classPath.equals("")) {
  376. return "";
  377. }
  378. return "-classpath \"" + classPath + "\"";
  379. }
  380. protected void startTest(String cmd) {
  381. d.addVMListener(new VMAdapter() {
  382. public void vmDisconnectEvent(VMDisconnectEvent e) {
  383. msg("Done");
  384. up();
  385. }});
  386. ex(cmd);
  387. down();
  388. stall();
  389. }
  390. }