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.

HarnessSelectionTest.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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.drivers;
  14. import org.aspectj.bridge.IMessage;
  15. import org.aspectj.bridge.MessageHandler;
  16. import org.aspectj.bridge.MessageUtil;
  17. import org.aspectj.testing.harness.bridge.AbstractRunSpec;
  18. import org.aspectj.testing.harness.bridge.AjcTest;
  19. import org.aspectj.testing.harness.bridge.IRunSpec;
  20. import org.aspectj.testing.run.IRunStatus;
  21. import org.aspectj.testing.run.RunValidator;
  22. import org.aspectj.testing.util.BridgeUtil;
  23. import org.aspectj.testing.util.RunUtils;
  24. import org.aspectj.testing.xml.AjcSpecXmlReader;
  25. import org.aspectj.util.LangUtil;
  26. import java.io.File;
  27. import java.io.IOException;
  28. import java.util.ArrayList;
  29. import java.util.Hashtable;
  30. import java.util.List;
  31. import junit.framework.TestCase;
  32. /**
  33. * The harness supports custom-coded queries based on
  34. * -ajctest{kind}={query} parameters
  35. * (until we move to an XML database with real queries).
  36. */
  37. public class HarnessSelectionTest extends TestCase {
  38. private static final String TESTDATA = "../testing-drivers/testdata";
  39. private static final String INC_HARNESS_DIR
  40. = TESTDATA + "/incremental/harness";
  41. private static final String SELECT
  42. = INC_HARNESS_DIR + "/selectionTest.xml";
  43. /** @see testIncrementalSuite() */
  44. private static final String INCREMENTAL
  45. = INC_HARNESS_DIR + "/suite.xml";
  46. private static final String TITLE_LIST_ONE
  47. = INC_HARNESS_DIR + "/titleListOne.txt";
  48. private static final String TITLE_LIST_PLURAL
  49. = INC_HARNESS_DIR + "/titleListPlural.txt";
  50. private static Hashtable SPECS = new Hashtable();
  51. private static AjcTest.Suite.Spec getSpec(String suiteFile) {
  52. AjcTest.Suite.Spec result = (AjcTest.Suite.Spec) SPECS.get(suiteFile);
  53. if (null == result) {
  54. try {
  55. result = AjcSpecXmlReader.getReader().readAjcSuite(new File(suiteFile));
  56. SPECS.put(suiteFile, result);
  57. } catch (IOException e) {
  58. e.printStackTrace(System.err);
  59. }
  60. }
  61. try {
  62. return (AjcTest.Suite.Spec) result.clone();
  63. } catch (CloneNotSupportedException e) {
  64. e.printStackTrace(System.err);
  65. assertTrue("clone failed: " + e, false);
  66. return null; // keep compiler happy
  67. }
  68. }
  69. private boolean verbose;
  70. public HarnessSelectionTest(String name) {
  71. super(name);
  72. }
  73. public void testFilesAvailable() {
  74. String[] files = new String[] {
  75. SELECT, INCREMENTAL, TITLE_LIST_ONE, TITLE_LIST_PLURAL
  76. };
  77. for (String file : files) {
  78. assertTrue(file, new File(file).canRead());
  79. }
  80. }
  81. public void testIncrementalSuite() {
  82. System.err.println("skipping testIncrementalSuite - too long");
  83. if (true) return;
  84. if (!eclipseAvailable()) {
  85. System.err.println("skipping test - eclipse classes not available");
  86. return;
  87. }
  88. String[] options = new String[]
  89. { "!verbose", "!eclipse",
  90. };
  91. Exp exp = new Exp(6, 6, 0, 6, 0, 0, 0);
  92. checkSelection(INCREMENTAL, options, "INFIX IGNORED", exp);
  93. }
  94. public void testKeywordSelectionBoth() {
  95. if (!eclipseAvailable()) {
  96. System.err.println("skipping test - eclipse classes not available");
  97. return;
  98. }
  99. String[] options = new String[]
  100. { "-ajctestRequireKeywords=requireKeyword",
  101. "-ajctestSkipKeywords=skipKeyword,skipUnenforcedAjcLimit",
  102. "!verbose",
  103. "-eclipse",
  104. };
  105. Exp exp = new Exp(17, 1, 16, 1, 0, 0, 1);
  106. checkSelection(SELECT, options, "keyword skipKeyword was found", exp);
  107. }
  108. public void testKeywordSelectionRequire() {
  109. if (!eclipseAvailable()) {
  110. System.err.println("skipping test - eclipse classes not available");
  111. return;
  112. }
  113. String[] options = new String[]
  114. { "-ajctestRequireKeywords=skipKeyword",
  115. "!verbose",
  116. "-eclipse",
  117. };
  118. Exp exp = new Exp(17, 1, 16, 1, 0, 0, 16);
  119. checkSelection(SELECT, options, "keyword skipKeyword was not found", exp);
  120. }
  121. public void testKeywordSelectionSkip() {
  122. if (!eclipseAvailable()) {
  123. System.err.println("skipping test - eclipse classes not available");
  124. return;
  125. }
  126. String[] options = new String[]
  127. { "-ajctestSkipKeywords=requireKeyword",
  128. "!verbose",
  129. "-eclipse",
  130. };
  131. Exp exp = new Exp(17, 0, 17, 0, 0, 0, 17);
  132. checkSelection(SELECT, options, "keyword requireKeyword was found", exp);
  133. }
  134. public void testNoOptions() {
  135. if (!ajcAvailable()) {
  136. System.err.println("skipping test - ajc classes not available");
  137. return;
  138. }
  139. String[] options = new String[]
  140. { "!ajc"
  141. };
  142. Exp exp = new Exp(17, 3, 14, 3, 0, 0, 4);
  143. checkSelection(SELECT, options, "normally-valid", exp);
  144. }
  145. public void testEclipseOptionsSkip() {
  146. String[] options = new String[]
  147. { "-eclipse",
  148. "-ajctestRequireKeywords=eclipseOptionSkip"
  149. };
  150. Exp exp = new Exp(17, 0, 17, 0, 0, 0, 6);
  151. checkSelection(SELECT, options, "old ajc 1.0 option", exp);
  152. }
  153. public void testAjcEclipseConflict() {
  154. if (!ajcAvailable()) {
  155. System.err.println("skipping test - ajc classes not available");
  156. return;
  157. }
  158. String[] options = new String[]
  159. { "!ajc"
  160. };
  161. Exp exp = new Exp(17, 3, 14, 3, 0, 0, 6);
  162. checkSelection(SELECT, options, "conflict between !eclipse and !ajc", exp);
  163. }
  164. public void testEclipseConflict() {
  165. String[] options = new String[]
  166. { "^eclipse",
  167. "-ajctestSkipKeywords=skipUnenforcedAjcLimit"
  168. };
  169. Exp exp = new Exp(17, 3, 14, 3, 0, 0, 6);
  170. checkSelection(SELECT, options, "force conflict between eclipse", exp);
  171. }
  172. public void testSinglePR() {
  173. String[] options = new String[]
  174. { "-eclipse", "-ajctestPR=100"
  175. };
  176. Exp exp = new Exp(17, 1, 16, 1, 0, 0, 16);
  177. checkSelection(SELECT, options, "bugId required", exp);
  178. }
  179. public void testTwoPR() {
  180. String[] options = new String[]
  181. { "-eclipse", "-ajctestPR=100,101"
  182. };
  183. Exp exp = new Exp(17, 2, 15, 2, 0, 0, 15);
  184. checkSelection(SELECT, options, "bugId required", exp);
  185. }
  186. public void testTitleContainsSubstringSelection() {
  187. String[] options = new String[]
  188. { "-ajctestTitleContains=run and ",
  189. "-eclipse"
  190. };
  191. Exp exp = new Exp(17, 1, 16, 1, 0, 0, 16);
  192. checkSelection(SELECT, options, "run and", exp);
  193. }
  194. public void testTitleContainsSubstringSelectionPlural() {
  195. String[] options = new String[]
  196. { "-ajctestTitleContains= run and , if skipKeyword ",
  197. "-eclipse"
  198. };
  199. Exp exp = new Exp(17, 2, 15, 2, 0, 0, 15);
  200. checkSelection(SELECT, options, "title", exp);
  201. }
  202. public void testTitleContainsExactSelection() {
  203. String[] options = new String[]
  204. { "-ajctestTitleContains=run and pass",
  205. "-eclipse"
  206. };
  207. Exp exp = new Exp(17, 1, 16, 1, 0, 0, 16);
  208. checkSelection(SELECT, options, "run and pass", exp);
  209. }
  210. public void testTitleContainsExactSelectionPlural() {
  211. String[] options = new String[]
  212. { "-ajctestTitleContains= run and pass , omit if skipKeyword ",
  213. "-eclipse"
  214. };
  215. Exp exp = new Exp(17, 2, 15, 2, 0, 0, 15);
  216. checkSelection(SELECT, options, "title", exp);
  217. }
  218. public void testTitleListSelection() {
  219. String[] options = new String[]
  220. { "-ajctestTitleList=run and pass",
  221. "-eclipse"
  222. };
  223. Exp exp = new Exp(17, 1, 16, 1, 0, 0, 16);
  224. checkSelection(SELECT, options, "run and pass", exp);
  225. }
  226. public void testTitleListSelectionPlural() {
  227. String[] options = new String[]
  228. { "-ajctestTitleList= run and pass , omit if skipKeyword ",
  229. "-eclipse"
  230. };
  231. Exp exp = new Exp(17, 2, 15, 2, 0, 0, 15);
  232. checkSelection(SELECT, options, "title", exp);
  233. }
  234. public void testTitleListFileSelection() {
  235. String[] options = new String[]
  236. { "-ajctestTitleList=" + TITLE_LIST_ONE,
  237. "-eclipse"
  238. };
  239. Exp exp = new Exp(17, 1, 16, 1, 0, 0, 16);
  240. checkSelection(SELECT, options, TITLE_LIST_ONE, exp);
  241. }
  242. public void testTitleListFileSelectionPlural() {
  243. String[] options = new String[]
  244. { "-ajctestTitleList=" + TITLE_LIST_PLURAL,
  245. "-eclipse"
  246. };
  247. Exp exp = new Exp(17, 2, 15, 2, 0, 0, 15);
  248. checkSelection(SELECT, options, TITLE_LIST_PLURAL, exp);
  249. // Now check the "fail only" path
  250. options = new String[]
  251. { "-ajctestTitleFailList=" + TITLE_LIST_PLURAL,
  252. "-eclipse"
  253. };
  254. // 1 messages skipped when run under 1.4 for other reasons,
  255. // so count "skip" instead of TITLE_LIST_PLURAL
  256. exp = new Exp(17, 2, 15, 2, 0, 0, 15);
  257. checkSelection(SELECT, options, "skip", exp);
  258. }
  259. /**
  260. * Run the static test suite with the given options.
  261. * @param setupHolder the IMessageHolder for any setup messages
  262. * @return null if setup failed or Harness.RunResult if suite completed.
  263. */
  264. private Harness.RunResult runSuite(String suiteFile, String[] options, MessageHandler setupHolder) {
  265. AbstractRunSpec.RT runtime = new AbstractRunSpec.RT();
  266. runtime.setOptions(options);
  267. AjcTest.Suite.Spec spec = getSpec(suiteFile);
  268. assertNotNull(spec);
  269. ArrayList<IRunSpec> kids = spec.getChildren();
  270. assertNotNull(kids);
  271. if ((suiteFile == SELECT) && (17 != kids.size())) {
  272. assertTrue("expected 17 kids, got " + kids.size(), false);
  273. }
  274. if (!spec.adoptParentValues(runtime, setupHolder)) {
  275. return null;
  276. } else {
  277. class TestHarness extends Harness {
  278. public RunResult run(AjcTest.Suite.Spec spec) {
  279. return super.run(spec);
  280. }
  281. }
  282. TestHarness h = new TestHarness();
  283. return h.run(spec);
  284. }
  285. }
  286. class Exp {
  287. public final int tests;
  288. public final int testsRun;
  289. public final int skipped;
  290. public final int passed;
  291. public final int failed;
  292. public final int incomplete;
  293. public final int infix;
  294. Exp(int tests, int testsRun, int skipped, int passed, int failed, int incomplete, int infix) {
  295. this.tests = tests;
  296. this.testsRun = testsRun;
  297. this.skipped = skipped;
  298. this.passed = passed;
  299. this.failed = failed;
  300. this.incomplete = incomplete;
  301. this.infix = infix;
  302. }
  303. }
  304. public void checkSelection(String suiteFile, String[] options, String infoInfix, Exp exp) {
  305. MessageHandler holder = new MessageHandler();
  306. Harness.RunResult result = runSuite(suiteFile, options, holder);
  307. if (verbose) {
  308. MessageUtil.print(System.out, holder, " setup - ");
  309. }
  310. assertNotNull("Harness.RunResult", result);
  311. // XXX sync hack snooping of message text with skip messages, harness
  312. final List skipList = MessageUtil.getMessages(holder, IMessage.INFO, false, "skip");
  313. final int numSkipped = skipList.size();
  314. IRunStatus status = result.status;
  315. assertNotNull(status);
  316. if (verbose) {
  317. RunUtils.print(System.out, "result - ", status);
  318. System.out.println(BridgeUtil.childString(status, numSkipped, result.numIncomplete));
  319. }
  320. assertEquals("skips", exp.skipped, numSkipped);
  321. IRunStatus[] children = status.getChildren();
  322. assertNotNull(children);
  323. assertTrue(children.length + "!= expRun=" + exp.testsRun,
  324. exp.testsRun == children.length);
  325. int actPass = 0;
  326. for (IRunStatus child : children) {
  327. if (RunValidator.NORMAL.runPassed(child)) {
  328. actPass++;
  329. }
  330. }
  331. if (exp.passed != actPass) {
  332. assertTrue("exp.passed=" + exp.passed + " != actPass=" + actPass, false);
  333. }
  334. if (!LangUtil.isEmpty(infoInfix)) {
  335. int actInfix = MessageUtil.getMessages(holder, IMessage.INFO, false, infoInfix).size();
  336. if (actInfix != exp.infix) {
  337. String s = "for infix \"" + infoInfix
  338. + "\" actInfix=" + actInfix + " != expInfix=" + exp.infix;
  339. assertTrue(s, false);
  340. }
  341. }
  342. }
  343. private boolean ajcAvailable() { // XXX util
  344. try {
  345. Class.forName("org.aspectj.compiler.base.JavaCompiler");
  346. return true;
  347. } catch (ClassNotFoundException e) {
  348. return false;
  349. }
  350. }
  351. private boolean eclipseAvailable() { // XXX util
  352. try {
  353. Class.forName("org.aspectj.ajdt.ajc.AjdtCommand");
  354. return true;
  355. } catch (ClassNotFoundException e) {
  356. return false;
  357. }
  358. }
  359. }