Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

TestDiffs.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 Common Public License v1.0
  7. * which accompanies this distribution and is available at
  8. * http://www.eclipse.org/legal/cpl-v10.html
  9. *
  10. * Contributors:
  11. * Xerox/PARC initial implementation
  12. * ******************************************************************/
  13. package org.aspectj.testing.util;
  14. import java.io.BufferedReader;
  15. import java.io.File;
  16. import java.io.FileReader;
  17. import java.io.IOException;
  18. import java.io.PrintStream;
  19. import java.util.ArrayList;
  20. import java.util.Collections;
  21. import java.util.Comparator;
  22. import java.util.Iterator;
  23. import java.util.List;
  24. import java.util.ListIterator;
  25. import org.aspectj.util.LangUtil;
  26. /**
  27. * Calculated differences between two test runs
  28. * based on their output files
  29. * assuming that tests are logged with prefix [PASS|FAIL]
  30. * (as they are when using <tt>-traceTestsMin</tt> with the Harness).
  31. * @see org.aspectj.testing.drivers.Harness
  32. */
  33. public class TestDiffs { // XXX pretty dumb implementation
  34. /** @param args expected, actual test result files */
  35. public static void main(String[] args) {
  36. if ((null == args) || (2 > args.length)) {
  37. System.err.println("java " + TestDiffs.class.getName() + " expectedFile actualFile {test}");
  38. return;
  39. }
  40. File expected = new File(args[0]);
  41. File actual = new File(args[1]);
  42. TestDiffs result = compareResults(expected, actual);
  43. System.out.println("## Differences between test runs");
  44. print(System.out, result.added, "added");
  45. print(System.out, result.missing, "missing");
  46. print(System.out, result.fixed, "fixed");
  47. print(System.out, result.broken, "broken");
  48. System.out.println("## Summary");
  49. System.out.println(" # expected " + result.expected.size() + " tests: " + args[0] );
  50. System.out.println(" # actual " + result.actual.size() + " tests: " + args[1]);
  51. StringBuffer sb = new StringBuffer();
  52. append(sb, result.added, " added");
  53. append(sb, result.missing, " missing");
  54. append(sb, result.broken, " broken");
  55. append(sb, result.fixed, " fixed");
  56. append(sb, result.stillPassing, " still passing");
  57. append(sb, result.stillFailing, " still failing");
  58. System.out.println(" # diffs: " + sb);
  59. }
  60. /**
  61. * @param expected the expected/old File results with Harness -traceTestsMin lines
  62. * @param actual the actual/new File results with Harness -traceTestsMin lines
  63. * @return TestDiffs null if error, valid otherwise
  64. */
  65. public static TestDiffs compareResults(File expected, File actual) {
  66. ArrayList exp = null;
  67. ArrayList act = null;
  68. File reading = expected;
  69. try {
  70. exp = TestDiffs.readTestResults(expected, expected.getPath());
  71. reading = actual;
  72. act = TestDiffs.readTestResults(actual, actual.getPath());
  73. Diffs tests = Diffs.makeDiffs("tests", exp, act, TestResult.BY_NAME);
  74. // remove missing/unexpected (removed, added) tests from results
  75. // otherwise, unexpected-[pass|fail] look like [fixes|broken]
  76. ArrayList expResults = trimByName(exp, tests.missing);
  77. ArrayList actResults = trimByName(act, tests.unexpected);
  78. Diffs results = Diffs.makeDiffs("results", expResults, actResults, TestResult.BY_PASSNAME);
  79. // broken tests show up in results as unexpected-fail or missing-pass
  80. // fixed tests show up in results as unexpected-pass or missing-fail
  81. ArrayList broken = new ArrayList();
  82. ArrayList fixed = new ArrayList();
  83. split(results.unexpected, fixed, broken);
  84. return new TestDiffs(
  85. exp,
  86. act,
  87. tests.missing,
  88. tests.unexpected,
  89. broken,
  90. fixed);
  91. } catch (IOException e) {
  92. System.err.println("error reading " + reading);
  93. e.printStackTrace(System.err); // XXX
  94. return null;
  95. }
  96. }
  97. private static void append(StringBuffer sb, List list, String label) {
  98. if (!LangUtil.isEmpty(list)) {
  99. if (0 < sb.length()) {
  100. sb.append(" ");
  101. }
  102. sb.append(list.size() + label);
  103. }
  104. }
  105. private static void print(PrintStream out, List list, String label) {
  106. if ((null == out) || LangUtil.isEmpty(list)) {
  107. return;
  108. }
  109. // int i = 0;
  110. final String suffix = " " + label;
  111. final String LABEL = list.size() + suffix;
  112. out.println("## START " + LABEL);
  113. for (Iterator iter = list.iterator(); iter.hasNext();) {
  114. TestResult result = (TestResult) iter.next();
  115. out.println(" " + result.test + " ## " + suffix);
  116. }
  117. out.println("## END " + LABEL);
  118. }
  119. /**
  120. * Create ArrayList with input TestResult list
  121. * but without elements in trim list,
  122. * comparing based on test name only.
  123. * @param input
  124. * @param trim
  125. * @return ArrayList with all input except those in trim (by name)
  126. */
  127. private static ArrayList trimByName(List input, List trim) {
  128. ArrayList result = new ArrayList();
  129. result.addAll(input);
  130. if (!LangUtil.isEmpty(input) && !LangUtil.isEmpty(trim)) {
  131. for (ListIterator iter = result.listIterator(); iter.hasNext();) {
  132. TestResult inputItem = (TestResult) iter.next();
  133. for (Iterator iterator = trim.iterator();
  134. iterator.hasNext();
  135. ) {
  136. TestResult trimItem = (TestResult) iterator.next();
  137. if (inputItem.test.equals(trimItem.test)) {
  138. iter.remove();
  139. break;
  140. }
  141. }
  142. }
  143. }
  144. return result;
  145. }
  146. /** split input List by whether the TestResult element passed or failed */
  147. private static void split(List input, ArrayList pass, ArrayList fail) {
  148. for (ListIterator iter = input.listIterator(); iter.hasNext();) {
  149. TestResult result = (TestResult) iter.next();
  150. if (result.pass) {
  151. pass.add(result);
  152. } else {
  153. fail.add(result);
  154. }
  155. }
  156. }
  157. /**
  158. * Read a file of test results,
  159. * defined as lines starting with [PASS|FAIL]
  160. * (produced by Harness option <tt>-traceTestsmin</tt>).
  161. * @return ArrayList of TestResult, one for every -traceTestsMin line in File
  162. */
  163. private static ArrayList readTestResults(File file, String config) throws IOException {
  164. LangUtil.throwIaxIfNull(file, "file");
  165. if (null == config) {
  166. config = file.getPath();
  167. }
  168. ArrayList result = new ArrayList();
  169. FileReader in = null;
  170. try {
  171. in = new FileReader(file);
  172. BufferedReader input = new BufferedReader(in);
  173. String line;
  174. // XXX handle stream interleaving more carefully
  175. // XXX clip trailing ()
  176. // XXX fix elision in test name rendering by -traceTestsMin?
  177. while (null != (line = input.readLine())) {
  178. boolean pass = line.startsWith("PASS");
  179. boolean fail = false;
  180. if (!pass) {
  181. fail = line.startsWith("FAIL");
  182. }
  183. if (pass || fail) {
  184. String test = line.substring(4).trim();
  185. result.add(new TestResult(test, config, pass));
  186. }
  187. }
  188. } finally {
  189. if (null != in) {
  190. try { in.close(); }
  191. catch (IOException e) {} // ignore
  192. }
  193. }
  194. return result;
  195. }
  196. private static List safeList(List list) {
  197. return (null == list
  198. ? Collections.EMPTY_LIST
  199. : Collections.unmodifiableList(list));
  200. }
  201. /** List of TestResult results from expected run. */
  202. public final List expected;
  203. /** List of TestResult results from actual run. */
  204. public final List actual;
  205. /** List of TestResult tests disappeared from test suite between expected and actual runs. */
  206. public final List missing;
  207. /** List of TestResult tests added to test suite between expected and actual runs. */
  208. public final List added;
  209. /** List of TestResult tests in both runs, expected to pass but actually failed */
  210. public final List broken;
  211. /** List of TestResult tests in both runs, expected to fail but actually passed */
  212. public final List fixed;
  213. /** List of TestResult passed tests in expected run */
  214. public final List expectedPassed;
  215. /** List of TestResult failed tests in expected run */
  216. public final List expectedFailed;
  217. /** List of TestResult passed tests in actual run */
  218. public final List actualPassed;
  219. /** List of TestResult tests failed in actual run */
  220. public final List actualFailed;
  221. /** List of TestResult tests passed in both expected and actual run */
  222. public final List stillPassing;
  223. /** List of TestResult tests failed in both expected and actual run */
  224. public final List stillFailing;
  225. private TestDiffs(
  226. List expected,
  227. List actual,
  228. List missing,
  229. List added,
  230. List broken,
  231. List fixed) {
  232. this.expected = safeList(expected);
  233. this.actual = safeList(actual);
  234. this.missing = safeList(missing);
  235. this.added = safeList(added);
  236. this.broken = safeList(broken);
  237. this.fixed = safeList(fixed);
  238. // expected[Passed|Failed]
  239. ArrayList passed = new ArrayList();
  240. ArrayList failed = new ArrayList();
  241. split(this.expected, passed, failed);
  242. expectedPassed = safeList(passed);
  243. expectedFailed = safeList(failed);
  244. // actual[Passed|Failed]
  245. passed = new ArrayList();
  246. failed = new ArrayList();
  247. split(this.actual, passed, failed);
  248. actualPassed = safeList(passed);
  249. actualFailed = safeList(failed);
  250. // stillPassing: expected.passed w/o broken, missingPasses
  251. passed = new ArrayList();
  252. passed.addAll(expectedPassed);
  253. passed = trimByName(passed, this.broken);
  254. ArrayList missingPasses = new ArrayList();
  255. ArrayList missingFails = new ArrayList();
  256. split(this.missing, missingPasses, missingFails);
  257. passed = trimByName(passed, missingPasses);
  258. stillPassing = safeList(passed);
  259. // stillFailing: expected.failed w/o fixed, missingFails
  260. failed = new ArrayList();
  261. failed.addAll(expectedFailed);
  262. failed = trimByName(failed, this.fixed);
  263. failed = trimByName(failed, missingFails);
  264. stillFailing = safeList(failed);
  265. }
  266. /** results of a test */
  267. public static class TestResult {
  268. public static final Comparator BY_PASSNAME = new Comparator() {
  269. public int compare(Object o1, Object o2) {
  270. if (o1 == o2) {
  271. return 0;
  272. }
  273. TestResult lhs = (TestResult) o1;
  274. TestResult rhs = (TestResult) o2;
  275. return (lhs.pass == rhs.pass
  276. ? lhs.test.compareTo(rhs.test)
  277. : (lhs.pass ? 1 : -1 ));
  278. }
  279. public boolean equals(Object lhs, Object rhs) {
  280. return (0 == compare(lhs, rhs));
  281. }
  282. };
  283. public static final Comparator BY_NAME = new Comparator() {
  284. public int compare(Object o1, Object o2) {
  285. if (o1 == o2) {
  286. return 0;
  287. }
  288. TestResult lhs = (TestResult) o1;
  289. TestResult rhs = (TestResult) o2;
  290. return lhs.test.compareTo(rhs.test);
  291. }
  292. public boolean equals(Object lhs, Object rhs) {
  293. return (0 == compare(lhs, rhs));
  294. }
  295. };
  296. //private static final ArrayList TESTS = new ArrayList();
  297. public static final String FIELDSEP = "\t";
  298. public final String test;
  299. public final String config;
  300. public final boolean pass;
  301. private final String toString;
  302. public TestResult(String test, String config, boolean pass) {
  303. LangUtil.throwIaxIfNull(test, "test");
  304. LangUtil.throwIaxIfNull(test, "config");
  305. this.test = test;
  306. this.config = config;
  307. this.pass = pass;
  308. toString = (pass ? "PASS" : "FAIL") + FIELDSEP + test + FIELDSEP + config;
  309. }
  310. /** @return [PASS|FAIL]{FIELDSEP}test{FIELDSEP}config */
  311. public String toString() {
  312. return toString;
  313. }
  314. }
  315. }