Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

HarnessJUnitUtil.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /* *******************************************************************
  2. * Copyright (c) 2003 Contributors
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v 2.0
  6. * which accompanies this distribution and is available at
  7. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  8. *
  9. * Contributors:
  10. * Wes Isberg initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.testing.drivers;
  13. import java.io.*;
  14. import java.util.*;
  15. import junit.framework.*;
  16. import org.aspectj.bridge.*;
  17. //import org.aspectj.bridge.MessageHandler;
  18. import org.aspectj.testing.harness.bridge.*;
  19. import org.aspectj.testing.run.IRunStatus;
  20. import org.aspectj.testing.util.RunUtils;
  21. import org.aspectj.testing.util.RunUtils.IRunStatusPrinter;
  22. import org.aspectj.testing.xml.AjcSpecXmlReader;
  23. /**
  24. * Utilities for adapting AjcTest.{Suite.}Spec to JUnit.
  25. */
  26. public class HarnessJUnitUtil {
  27. /** bug?: eclipse RemoteTestRunner hangs if n>1 */
  28. public static final boolean ONE_ERROR_PER_TEST = true;
  29. public static final boolean FLATTEN_RESULTS = true;
  30. public static final boolean PRINT_OTHER_MESSAGES = false;
  31. /**
  32. * Create TestSuite with all suites running all options.
  33. * @param suites the String[] of paths to harness test suite files
  34. * @param options the String[][] of option sets to run (may be null)
  35. * @return Test with all TestSuites and TestCases
  36. * specified in suites and options.
  37. */
  38. public static TestSuite suite(String name, String[] suites, String[][] options) {
  39. if (null == name) {
  40. name = AjcHarnessTestsUsingJUnit.class.getName();
  41. }
  42. TestSuite suite = new TestSuite(name);
  43. if (!HarnessJUnitUtil.isEmpty(suites)) {
  44. if (HarnessJUnitUtil.isEmpty(options)) {
  45. options = new String[][] {new String[0]};
  46. }
  47. for (String s : suites) {
  48. for (String[] option : options) {
  49. Test t = AjctestsAdapter.make(s, option);
  50. suite.addTest(t);
  51. }
  52. }
  53. }
  54. return suite;
  55. }
  56. public static boolean isEmpty(Object[] ra) {
  57. return ((null == ra) || (0 == ra.length));
  58. }
  59. /**
  60. * Render status using a given printer.
  61. * @param status the IRunStatus to render to String
  62. * @param printer the IRunStatusPrinter to use
  63. * (defaults to AJC_PRINTER if null)
  64. * @return the String rendering of the status,
  65. * or "((IRunStatus) null)" if null
  66. */
  67. public static String render(IRunStatus status, IRunStatusPrinter printer) {
  68. if (null == status) {
  69. return "((IRunStatus) null)";
  70. }
  71. if (null == printer) {
  72. printer = RunUtils.AJC_PRINTER;
  73. }
  74. ByteArrayOutputStream outStream = new ByteArrayOutputStream();
  75. PrintStream out = new PrintStream(outStream);
  76. printer.printRunStatus(out, status);
  77. out.flush();
  78. return outStream.toString();
  79. }
  80. /**
  81. * Dump results for Test from status into TestResult.
  82. * FAIL is a failure,
  83. * ERROR and ABORT are errors,
  84. * and INFO, WARNING, and DEBUG are ignored.
  85. * If test instanceof IHasAjcSpec, and the keywords contain "expect-fail",
  86. * then failures are not reported (but passes are reported as a failure).
  87. * @param result the TestResult sink
  88. * @param status the IRunStatus source
  89. * @param test the Test to associate with the results
  90. * @param numIncomplete ignored
  91. * @return 0 (ignored)
  92. */
  93. public static int reportResult(
  94. TestResult result,
  95. IRunStatus status,
  96. Test test,
  97. int numIncomplete) {
  98. boolean expectFail = false;
  99. if (test instanceof IHasAjcSpec) {
  100. AjcTest.Spec spec = ((IHasAjcSpec) test).getAjcTestSpec();
  101. expectFail = spec.getKeywordsList().contains("expect-fail");
  102. }
  103. if (status.runResult()) {
  104. if (expectFail) {
  105. String m = "did not fail as expected per expect-fail keyword";
  106. reportResultToJUnit(m, false, true, test, result);
  107. }
  108. } else if (!expectFail) {
  109. final boolean includeChildren = true;
  110. if (status.hasAnyMessage(IMessage.FAIL, false, includeChildren)) {
  111. String m = render(status, null);
  112. reportResultToJUnit(m, false, true, test, result);
  113. } else if (status.hasAnyMessage(IMessage.ERROR, true, includeChildren)) {
  114. String m = render(status, null);
  115. reportResultToJUnit(m, true, false, test, result);
  116. } // /XXX skip INFO, DEBUG
  117. }
  118. return 0; // XXX not doing incomplete
  119. }
  120. /**
  121. * Report results as error, failure, or success (ignored),
  122. * differently if result is null
  123. * @param description the String description of the result
  124. * @param isError if true, report as failure
  125. * @param isFailure if true and not isError, report as failure
  126. * @param test the Test case
  127. * @param result the TestResult sink - ignored if null
  128. * @return 0
  129. */
  130. private static int reportResultToJUnit(String description, boolean isError, boolean isFailure, Test test, TestResult result) {
  131. if (null != result) {
  132. if (isError) {
  133. result.addError(test, new AssertionFailedError(description));
  134. } else if (isFailure) {
  135. result.addFailure(test, new AssertionFailedError(description));
  136. } // no need to log success
  137. } else { // have to throw failure
  138. if (isError) {
  139. String m = safeTestName(test) + " " + description;
  140. throw new Error(m);
  141. } else if (isFailure) {
  142. // String m = safeTestName(test) + " " + description;
  143. throw new AssertionFailedError(description);
  144. } // no need to log success
  145. }
  146. return 0;
  147. }
  148. // public static int reportResultComplex(
  149. // TestResult result,
  150. // IRunStatus status,
  151. // Test test,
  152. // int numIncomplete) {
  153. // int errs = 0;
  154. // if (FLATTEN_RESULTS) {
  155. // IRunStatus[] kids = status.getChildren();
  156. // for (int i = 0; i < kids.length; i++) {
  157. // errs += reportResult(result, kids[i], test, 0);
  158. // if ((errs > 0) && ONE_ERROR_PER_TEST) {
  159. // return errs;
  160. // }
  161. // }
  162. // }
  163. //
  164. // Throwable thrown = status.getThrown();
  165. // if (null != thrown) { // always report this? XXX what if expected?
  166. // result.addError(test, thrown);
  167. // errs++;
  168. // }
  169. // boolean previewPass = status.runResult();
  170. // IMessage[] errors = status.getMessages(null, true);
  171. // for (int i = 0; ((errs == 0) || !ONE_ERROR_PER_TEST)
  172. // && i < errors.length; i++) {
  173. // IMessage message = errors[i];
  174. // if (message.isAbort()) {
  175. // result.addError(test, new ErrorMessage(message));
  176. // errs++;
  177. // } else if (message.isFailed()) {
  178. // result.addFailure(test, new ErrorMessage(message));
  179. // errs++;
  180. // } else if (PRINT_OTHER_MESSAGES || !previewPass) {
  181. // System.out.println("#### message for " + test + ": ");
  182. // System.out.println(message);
  183. // }
  184. // }
  185. // if (((errs == 0) || !ONE_ERROR_PER_TEST)
  186. // && ((errs == 0) != status.runResult())) {
  187. // String s = "expected pass=" + (errs == 0);
  188. // result.addFailure(test, new ErrorMessage(s));
  189. // errs++;
  190. // }
  191. // if (((errs == 0) || !ONE_ERROR_PER_TEST)
  192. // && !status.isCompleted()) {
  193. // result.addFailure(test, new ErrorMessage("test incomplete? "));
  194. // errs++;
  195. // }
  196. // if (((errs == 0) || !ONE_ERROR_PER_TEST)
  197. // && (0 < numIncomplete)) {
  198. // result.addFailure(test, new ErrorMessage("incomplete steps: " + numIncomplete));
  199. // errs++;
  200. // }
  201. // return errs;
  202. // }
  203. /**
  204. * @return TestCase.getName() or Test.toString() or "nullTest"
  205. */
  206. public static String safeTestName(Test test) {
  207. if (test instanceof TestCase) {
  208. return ((TestCase) test).getName();
  209. } else if (null != test) {
  210. return test.toString();
  211. } else {
  212. return "nullTest";
  213. }
  214. }
  215. /**
  216. * Fix up test names for JUnit.
  217. * (i.e., workaround eclipse JUnit bugs)
  218. * @param name the String identifier for the test
  219. * @return the String permitted by (Eclipse) JUnit support
  220. */
  221. public static String cleanTestName(String name) {
  222. name = name.replace(',', ' ');
  223. name = name.replace('[', ' ');
  224. name = name.replace(']', ' ');
  225. name = name.replace('-', ' ');
  226. return name;
  227. }
  228. public static boolean readBooleanSystemProperty(String name) {
  229. boolean result = false;
  230. try {
  231. result = Boolean.getBoolean(name);
  232. } catch (Throwable t) {
  233. // ignore
  234. }
  235. return result;
  236. }
  237. /**
  238. * Get the test suite specifications from the suite file,
  239. * apply the options to all,
  240. * and report any messages to the holder.
  241. * @param suitePath the String path to the harness suite file
  242. * @param options the String[] options for the tests - may be null
  243. * @param holder the IMessageHolder for any messages - may be null
  244. * @return AjcTest.Suite.Spec test descriptions
  245. * (non-null but empty if some error)
  246. */
  247. public static AjcTest.Suite.Spec getSuiteSpec(
  248. String suitePath,
  249. String[] options,
  250. IMessageHolder holder) {
  251. if (null == suitePath) {
  252. MessageUtil.fail(holder, "null suitePath");
  253. return EmptySuite.ME;
  254. }
  255. File suiteFile = new File(suitePath);
  256. if (!suiteFile.canRead() || !suiteFile.isFile()) {
  257. MessageUtil.fail(holder, "unable to read file " + suitePath);
  258. return EmptySuite.ME;
  259. }
  260. try {
  261. AjcTest.Suite.Spec tempSpec;
  262. AbstractRunSpec.RT runtime = new AbstractRunSpec.RT();
  263. tempSpec = AjcSpecXmlReader.getReader().
  264. readAjcSuite(suiteFile);
  265. tempSpec.setSuiteDirFile(suiteFile.getParentFile());
  266. if (null == options) {
  267. options = new String[0];
  268. }
  269. runtime.setOptions(options);
  270. boolean skip = !tempSpec.adoptParentValues(runtime, holder);
  271. if (skip) {
  272. tempSpec = EmptySuite.ME;
  273. }
  274. return tempSpec;
  275. } catch (IOException e) {
  276. MessageUtil.abort(holder, "IOException", e);
  277. return EmptySuite.ME;
  278. }
  279. }
  280. private static class EmptySuite extends AjcTest.Suite.Spec {
  281. static final EmptySuite ME = new EmptySuite();
  282. final ArrayList children;
  283. private EmptySuite(){
  284. children = new ArrayList() {
  285. // XXX incomplete...
  286. public void add(int arg0, Object arg1) { fail();}
  287. public boolean addAll(int arg0, Collection arg1) { return fail();}
  288. public boolean addAll(Collection o) { return fail(); }
  289. public boolean add(Object o) { return fail(); }
  290. public boolean remove(Object o) { return fail(); }
  291. private boolean fail() {
  292. throw new Error("unmodifiable");
  293. }
  294. };
  295. }
  296. public ArrayList getChildren() {
  297. return children;
  298. }
  299. }
  300. public interface IHasAjcSpec {
  301. AjcTest.Spec getAjcTestSpec();
  302. }
  303. }