You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RunUtils.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /* *******************************************************************
  2. * Copyright (c) 1999-2001 Xerox Corporation,
  3. * 2002 Palo Alto Research Center, Incorporated (PARC).
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Public License v1.0
  7. * which accompanies this distribution and is available at
  8. * http://www.eclipse.org/legal/epl-v10.html
  9. *
  10. * Contributors:
  11. * Xerox/PARC initial implementation
  12. * ******************************************************************/
  13. package org.aspectj.testing.util;
  14. import java.io.PrintStream;
  15. import java.util.Iterator;
  16. import org.aspectj.bridge.IMessage;
  17. import org.aspectj.bridge.IMessageHandler;
  18. import org.aspectj.bridge.IMessageHolder;
  19. import org.aspectj.bridge.MessageUtil;
  20. import org.aspectj.bridge.MessageUtil.IMessageRenderer;
  21. import org.aspectj.testing.harness.bridge.AbstractRunSpec;
  22. import org.aspectj.testing.harness.bridge.IRunSpec;
  23. import org.aspectj.testing.run.IRunStatus;
  24. import org.aspectj.testing.run.RunValidator;
  25. import org.aspectj.util.LangUtil;
  26. /**
  27. *
  28. */
  29. public class RunUtils {
  30. /** enable verbose for this an any related AbstractRunSpec children */
  31. public static void enableVerbose(AbstractRunSpec spec) { // instanceof hack
  32. LangUtil.throwIaxIfNull(spec, "spec");
  33. spec.runtime.setVerbose(true);
  34. for (Iterator iter = spec.getChildren().iterator(); iter.hasNext();) {
  35. IRunSpec child = (IRunSpec) iter.next();
  36. if (child instanceof AbstractRunSpec) {
  37. enableVerbose((AbstractRunSpec) child);
  38. }
  39. }
  40. }
  41. /**
  42. * Calculate failures for this status.
  43. * If the input status has no children and failed, the result is 1.
  44. * If it has children and recurse is false, then
  45. * the result is the number of children whose status has failed
  46. * (so a failed status with some passing and no failing children
  47. * will return 0).
  48. * If it has children and recurse is true,
  49. * then return the number of leaf failures in the tree,
  50. * ignoring (roll-up) node failures.
  51. * @return number of failures in children of this status
  52. */
  53. public static int numFailures(IRunStatus status, boolean recurse) {
  54. int numFails = 0;
  55. IRunStatus[] children = status.getChildren();
  56. int numChildren = (null == children? 0 : children.length);
  57. if (0 == numChildren) {
  58. if (!RunValidator.NORMAL.runPassed(status)) {
  59. return 1;
  60. }
  61. } else {
  62. // int i = 0;
  63. for (int j = 0; j < children.length; j++) {
  64. if (recurse) {
  65. numFails += numFailures(children[j], recurse);
  66. } else {
  67. if (!RunValidator.NORMAL.runPassed(children[j])) {
  68. numFails++;
  69. }
  70. }
  71. }
  72. }
  73. return numFails;
  74. }
  75. // ------------------------ printing status
  76. public static void printShort(PrintStream out, IRunStatus status) {
  77. if ((null == out) || (null == status)) {
  78. return;
  79. }
  80. printShort(out, "", status);
  81. }
  82. public static void printShort(PrintStream out, String prefix, IRunStatus status) {
  83. int numFails = numFailures(status, true);
  84. String fails = (0 == numFails ? "" : " - " + numFails + " failures");
  85. out.println(prefix + toShortString(status) + fails);
  86. IRunStatus[] children = status.getChildren();
  87. int numChildren = (null == children? 0 : children.length);
  88. if (0 < numChildren) {
  89. int i = 0;
  90. for (int j = 0; j < children.length; j++) {
  91. printShort(out, prefix + "[" + LangUtil.toSizedString(i++, 3) + "]: ", children[j]);
  92. if (!RunValidator.NORMAL.runPassed(children[j])) {
  93. numFails++;
  94. }
  95. }
  96. }
  97. }
  98. public static void print(PrintStream out, IRunStatus status) {
  99. if ((null == out) || (null == status)) {
  100. return;
  101. }
  102. print(out, "", status);
  103. }
  104. public static void print(PrintStream out, String prefix, IRunStatus status) {
  105. print(out, prefix, status, MessageUtil.MESSAGE_ALL, MessageUtil.PICK_ALL);
  106. }
  107. public static void print(PrintStream out, String prefix, IRunStatus status,
  108. IMessageRenderer renderer, IMessageHandler selector) {
  109. String label = status.getIdentifier()
  110. + (status.runResult() ? "PASS" : "FAIL");
  111. out.println(prefix + label);
  112. out.println(prefix + debugString(status));
  113. IMessageHolder messageHolder = status;
  114. if ((null != messageHolder) && (0 < messageHolder.numMessages(null, true))) {
  115. MessageUtil.print(out, messageHolder, prefix, renderer, selector);
  116. }
  117. Throwable thrown = status.getThrown();
  118. if (null != thrown) {
  119. out.println(prefix + "--- printing stack trace for thrown");
  120. thrown.printStackTrace(out);
  121. }
  122. IRunStatus[] children = status.getChildren();
  123. int numChildren = (null == children? 0 : children.length);
  124. int numFails = 0;
  125. if (0 < numChildren) {
  126. out.println(prefix + "--- printing children [" + numChildren + "]");
  127. int i = 0;
  128. for (int j = 0; j < children.length; j++) {
  129. print(out, prefix + "[" + LangUtil.toSizedString(i++, 3) + "]: ", children[j]);
  130. if (!RunValidator.NORMAL.runPassed(children[j])) {
  131. numFails++;
  132. }
  133. }
  134. }
  135. if (0 < numFails) {
  136. label = numFails + " fails " + label;
  137. }
  138. out.println("");
  139. }
  140. public static String debugString(IRunStatus status) {
  141. if (null == status) {
  142. return "null";
  143. }
  144. final String[] LABELS =
  145. new String[] {
  146. "runResult",
  147. "id",
  148. "result",
  149. "numChildren",
  150. "completed",
  151. //"parent",
  152. "abort",
  153. "started",
  154. "thrown",
  155. "messages" };
  156. String runResult = status.runResult() ? "PASS" : "FAIL";
  157. Throwable thrown = status.getThrown();
  158. String thrownString = LangUtil.unqualifiedClassName(thrown);
  159. IRunStatus[] children = status.getChildren();
  160. String numChildren = (null == children? "0" : ""+children.length);
  161. String numMessages = ""+status.numMessages(null, IMessageHolder.EQUAL);
  162. Object[] values =
  163. new Object[] {
  164. runResult,
  165. status.getIdentifier(),
  166. status.getResult(),
  167. numChildren,
  168. new Boolean(status.isCompleted()),
  169. //status.getParent(), // costly if parent printing us
  170. status.getAbortRequest(),
  171. new Boolean(status.started()),
  172. thrownString,
  173. numMessages };
  174. return org.aspectj.testing.util.LangUtil.debugStr(status.getClass(), LABELS, values);
  175. }
  176. public static String toShortString(IRunStatus status) {
  177. if (null == status) {
  178. return "null";
  179. }
  180. String runResult = status.runResult() ? " PASS: " : " FAIL: ";
  181. return (runResult + status.getIdentifier());
  182. }
  183. /** renderer for IRunStatus */
  184. public static interface IRunStatusPrinter {
  185. void printRunStatus(PrintStream out, IRunStatus status);
  186. }
  187. public static final IRunStatusPrinter VERBOSE_PRINTER = new IRunStatusPrinter() {
  188. public String toString() { return "VERBOSE_PRINTER"; }
  189. /** Render IRunStatus produced by running an AjcTest */
  190. public void printRunStatus(PrintStream out, IRunStatus status) {
  191. printRunStatus(out, status, "");
  192. }
  193. private void printRunStatus(PrintStream out, IRunStatus status, String prefix) {
  194. LangUtil.throwIaxIfNull(out, "out");
  195. LangUtil.throwIaxIfNull(status, "status");
  196. String label = (status.runResult() ? " PASS: " : " FAIL: ")
  197. + status.getIdentifier();
  198. out.println(prefix + "------------ " + label);
  199. out.println(prefix + "--- result: " + status.getResult());
  200. if (0 < status.numMessages(null, true)) {
  201. out.println(prefix + "--- messages ");
  202. MessageUtil.print(out, status, prefix, MessageUtil.MESSAGE_ALL, MessageUtil.PICK_ALL);
  203. }
  204. Throwable thrown = status.getThrown();
  205. if (null != thrown) {
  206. out.println(prefix + "--- thrown");
  207. thrown.printStackTrace(out);
  208. }
  209. IRunStatus[] children = status.getChildren();
  210. for (int i = 0; i < children.length; i++) {
  211. String number = "[" + LangUtil.toSizedString(i,3) + "] ";
  212. printRunStatus(out, children[i], prefix + number);
  213. }
  214. }
  215. };
  216. /** print only status and fail/abort messages */
  217. public static final IRunStatusPrinter TERSE_PRINTER = new IRunStatusPrinter() {
  218. public String toString() { return "TERSE_PRINTER"; }
  219. /** print only status and fail messages */
  220. public void printRunStatus(PrintStream out, IRunStatus status) {
  221. printRunStatus(out, status, "");
  222. }
  223. private void printRunStatus(PrintStream out, IRunStatus status, String prefix) {
  224. LangUtil.throwIaxIfNull(out, "out");
  225. LangUtil.throwIaxIfNull(status, "status");
  226. String label = (status.runResult() ? "PASS: " : "FAIL: ")
  227. + status.getIdentifier();
  228. out.println(prefix + label);
  229. Object result = status.getResult();
  230. if ((null != result) && (IRunStatus.PASS != result) && (IRunStatus.FAIL != result)) {
  231. out.println(prefix + "--- result: " + status.getResult());
  232. }
  233. if (0 < status.numMessages(IMessage.FAIL, true)) {
  234. MessageUtil.print(out, status, prefix, MessageUtil.MESSAGE_ALL, MessageUtil.PICK_FAIL_PLUS);
  235. }
  236. Throwable thrown = status.getThrown();
  237. if (null != thrown) {
  238. out.println(prefix + "--- thrown: " + LangUtil.renderException(thrown, true));
  239. }
  240. IRunStatus[] children = status.getChildren();
  241. for (int i = 0; i < children.length; i++) {
  242. if (!children[i].runResult()) {
  243. String number = "[" + LangUtil.toSizedString(i,3) + "] ";
  244. printRunStatus(out, children[i], prefix + number);
  245. }
  246. }
  247. out.println("");
  248. }
  249. };
  250. /** Render IRunStatus produced by running an AjcTest.Suite. */
  251. public static final IRunStatusPrinter AJCSUITE_PRINTER = new IRunStatusPrinter() {
  252. public String toString() { return "AJCSUITE_PRINTER"; }
  253. /**
  254. * Render IRunStatus produced by running an AjcTest.Suite.
  255. * This renders only test failures and
  256. * a summary at the end.
  257. */
  258. public void printRunStatus(PrintStream out, IRunStatus status) {
  259. LangUtil.throwIaxIfNull(out, "out");
  260. LangUtil.throwIaxIfNull(status, "status");
  261. final String prefix = "";
  262. final boolean failed = status.runResult();
  263. String label = (status.runResult() ? "PASS: " : "FAIL: ")
  264. + status.getIdentifier();
  265. out.println(prefix + label);
  266. // print all messages - these are validator comments
  267. if (0 < status.numMessages(null, true)) {
  268. MessageUtil.print(out, status, "init", MessageUtil.MESSAGE_ALL, MessageUtil.PICK_ALL);
  269. }
  270. // XXX ignore thrown if failed - will be printed as message anyway?
  271. Throwable thrown = status.getThrown();
  272. if ((null != thrown) && !failed) {
  273. out.println(prefix + "--- printing stack trace for thrown");
  274. thrown.printStackTrace(out);
  275. }
  276. IRunStatus[] children = status.getChildren();
  277. int numChildren = (null == children? 0 : children.length);
  278. int numFails = 0;
  279. if (0 < numChildren) {
  280. for (int j = 0; j < children.length; j++) {
  281. if (!RunValidator.NORMAL.runPassed(children[j])) {
  282. numFails++;
  283. }
  284. }
  285. }
  286. if (0 < numFails) {
  287. out.println(prefix + "--- " + numFails + " failures when running " + children.length + " tests");
  288. for (int j = 0; j < children.length; j++) {
  289. if (!RunValidator.NORMAL.runPassed(children[j])) {
  290. print(out, prefix + "[" + LangUtil.toSizedString(j, 3) + "]: ", children[j]);
  291. out.println("");
  292. }
  293. }
  294. }
  295. label = "ran " + children.length + " tests"
  296. + (numFails == 0 ? "" : "(" + numFails + " fails)");
  297. out.println("");
  298. }
  299. };
  300. /** Render IRunStatus produced by running an AjcTest (verbose) */
  301. public static final IRunStatusPrinter AJCTEST_PRINTER = VERBOSE_PRINTER;
  302. /** print this with messages, then children using AJCRUN_PRINTER */
  303. public static final IRunStatusPrinter AJC_PRINTER = new IRunStatusPrinter() {
  304. public String toString() { return "AJC_PRINTER"; }
  305. /** Render IRunStatus produced by running an AjcTest */
  306. public void printRunStatus(PrintStream out, IRunStatus status) {
  307. LangUtil.throwIaxIfNull(out, "out");
  308. LangUtil.throwIaxIfNull(status, "status");
  309. String label = (status.runResult() ? " PASS: " : " FAIL: ")
  310. + status.getIdentifier();
  311. out.println("------------ " + label);
  312. MessageUtil.print(out, status, "", MessageUtil.MESSAGE_ALL, MessageUtil.PICK_ALL);
  313. IRunStatus[] children = status.getChildren();
  314. for (int i = 0; i < children.length; i++) {
  315. AJCRUN_PRINTER.printRunStatus(out, children[i]);
  316. }
  317. //out.println("------------ END " + label);
  318. out.println("");
  319. }
  320. };
  321. /** print only fail messages */
  322. public static final IRunStatusPrinter AJCRUN_PRINTER = new IRunStatusPrinter() {
  323. public String toString() { return "AJCRUN_PRINTER"; }
  324. /** Render IRunStatus produced by running an AjcTest child */
  325. public void printRunStatus(PrintStream out, IRunStatus status) {
  326. LangUtil.throwIaxIfNull(out, "out");
  327. LangUtil.throwIaxIfNull(status, "status");
  328. final boolean orGreater = false;
  329. int numFails = status.numMessages(IMessage.FAIL, orGreater);
  330. if (0 < numFails) {
  331. out.println("--- " + status.getIdentifier());
  332. IMessage[] fails = status.getMessages(IMessage.FAIL, orGreater);
  333. for (int i = 0; i < fails.length; i++) {
  334. out.println("[fail " + LangUtil.toSizedString(i, 3) + "]: "
  335. + MessageUtil.MESSAGE_ALL.renderToString(fails[i]));
  336. }
  337. }
  338. }
  339. };
  340. private RunUtils() {
  341. }
  342. }