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.

CompilerRunSpecTest.java 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /* *******************************************************************
  2. * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC),
  3. * 2003 Contributors.
  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. * Wes Isberg 2003 modifications
  13. * ******************************************************************/
  14. package org.aspectj.testing.harness.bridge;
  15. import java.util.Arrays;
  16. import java.util.Iterator;
  17. import java.util.Set;
  18. import org.aspectj.bridge.MessageHandler;
  19. import org.aspectj.bridge.MessageUtil;
  20. import org.aspectj.testing.harness.bridge.CompilerRun.Spec.CRSOptions;
  21. import org.aspectj.testing.util.options.Option;
  22. import org.aspectj.testing.util.options.OptionChecker;
  23. import org.aspectj.testing.util.options.Options;
  24. import org.aspectj.util.LangUtil;
  25. import junit.framework.TestCase;
  26. /**
  27. *
  28. */
  29. public class CompilerRunSpecTest extends TestCase {
  30. private static boolean PRINTING = true;
  31. private static final boolean SETUP_JAVA13 =
  32. haveProperty(Globals.J2SE13_RTJAR_NAME);
  33. private static final boolean SETUP_JAVA14 =
  34. haveProperty(Globals.J2SE14_RTJAR_NAME);
  35. static {
  36. if (!SETUP_JAVA14) {
  37. System.err.println(
  38. "warning: set -D"
  39. + Globals.J2SE14_RTJAR_NAME
  40. + "=... in order to run all tests");
  41. }
  42. if (!SETUP_JAVA13) {
  43. System.err.println(
  44. "warning: set -D"
  45. + Globals.J2SE13_RTJAR_NAME
  46. + "=... in order to run all tests");
  47. }
  48. }
  49. private static String[][] duplicate(String[][] input, String prefix) {
  50. String[][] output = new String[input.length][];
  51. final int prefixLength = (null == prefix ? 0 : prefix.length());
  52. for (int i = 0; i < output.length; i++) {
  53. int length = input[i].length;
  54. output[i] = new String[length];
  55. if ((length > 0) && (prefixLength > 0)) {
  56. System.arraycopy(input[i], 0, output[i], 0, length);
  57. output[i][0] =
  58. prefix + output[i][0].substring(prefixLength);
  59. }
  60. }
  61. return output;
  62. }
  63. private static boolean haveProperty(String key) {
  64. try {
  65. return (null != System.getProperty(key));
  66. } catch (Throwable t) {
  67. // ignore
  68. }
  69. return false;
  70. }
  71. /**
  72. * Constructor for CompilerRunSpecTest.
  73. * @param name
  74. */
  75. public CompilerRunSpecTest(String name) {
  76. super(name);
  77. }
  78. public void testSetupArgs() {
  79. checkSetupArgs("eclipse", true);
  80. checkSetupArgs("verbose", false);
  81. }
  82. public void testCompliance() {
  83. // 1.3 should work
  84. String specOptions = "-1.3";
  85. String[] globalOptions = new String[0];
  86. boolean expectAdopted = true;
  87. String resultContains = "1.3";
  88. String messagesContain = null;
  89. MessageHandler handler =
  90. runTest(
  91. specOptions,
  92. globalOptions,
  93. expectAdopted,
  94. resultContains,
  95. messagesContain);
  96. checkMessages(handler, null);
  97. // 1.3 should work with collision?
  98. globalOptions = new String[] { "-1.3" };
  99. resultContains = "1.3";
  100. handler =
  101. runTest(
  102. specOptions,
  103. globalOptions,
  104. expectAdopted,
  105. resultContains,
  106. messagesContain);
  107. checkMessages(handler, null);
  108. // 1.4 should work
  109. globalOptions = new String[0];
  110. specOptions = "-1.4";
  111. resultContains = "1.4";
  112. handler =
  113. runTest(
  114. specOptions,
  115. globalOptions,
  116. expectAdopted,
  117. resultContains,
  118. messagesContain);
  119. checkMessages(handler, null);
  120. // compliance not checked for valid numbers, so -1.2 would pass
  121. }
  122. private void checkMessages(MessageHandler handler, String contains) {
  123. if (null == contains) {
  124. if (0 != handler.numMessages(null, true)) {
  125. assertTrue("" + handler, false);
  126. }
  127. } else {
  128. String messages = "" + handler;
  129. if (-1 == messages.indexOf(contains)) {
  130. assertTrue(messages, false);
  131. }
  132. }
  133. }
  134. /** @testcase check -target converts for 1.1 and 1.2, not others */
  135. public void testTarget() {
  136. final boolean PASS = true;
  137. final boolean FAIL = false;
  138. checkSourceTargetVersionConversion("target", 1, PASS, null);
  139. checkSourceTargetVersionConversion("target", 2, PASS, null);
  140. checkSourceTargetVersionConversion("target", 3, PASS, null);
  141. checkSourceTargetVersionConversion("target", 4, PASS, null);
  142. checkSourceTargetVersionConversion("target", 5, PASS, null);
  143. checkSourceTargetVersionConversion("target", 6, FAIL, "illegal input");
  144. }
  145. /** @testcase check -source converts for 1.3 and 1.4, not others */
  146. public void testSource() {
  147. final boolean PASS = true;
  148. final boolean FAIL = false;
  149. if (SETUP_JAVA13) {
  150. checkSourceTargetVersionConversion("source", 3, PASS, null);
  151. }
  152. if (SETUP_JAVA14) {
  153. checkSourceTargetVersionConversion("source", 4, PASS, null);
  154. }
  155. checkSourceTargetVersionConversion(
  156. "source",
  157. 2,
  158. FAIL,
  159. "not permitted");
  160. checkSourceTargetVersionConversion(
  161. "source",
  162. 6,
  163. FAIL,
  164. "not permitted");
  165. }
  166. public void testSourceOverride() {
  167. if (SETUP_JAVA13 && SETUP_JAVA14) {
  168. checkSourceTargetOverride("source", 3, 4);
  169. checkSourceTargetOverride("source", 4, 3);
  170. checkSourceTargetOverride("source", 3, 3);
  171. checkSourceTargetOverride("source", 4, 4);
  172. }
  173. }
  174. public void testTargetOverride() {
  175. checkSourceTargetOverride("target", 1, 2);
  176. checkSourceTargetOverride("target", 2, 1);
  177. checkSourceTargetOverride("target", 1, 1);
  178. checkSourceTargetOverride("target", 2, 2);
  179. }
  180. public void testCompilerOptions() {
  181. checkCompilerOption(null, CompilerRun.Spec.DEFAULT_COMPILER);
  182. CRSOptions crsOptions = CompilerRun.Spec.testAccessToCRSOptions();
  183. Set options = crsOptions.compilerOptions();
  184. assertTrue(null != options);
  185. StringBuffer notLoaded = new StringBuffer();
  186. for (Iterator iter = options.iterator(); iter.hasNext();) {
  187. Option compilerOption = (Option) iter.next();
  188. if (!(crsOptions.compilerIsLoadable(compilerOption))) {
  189. notLoaded.append(" " + compilerOption);
  190. } else {
  191. String className = crsOptions.compilerClassName(compilerOption);
  192. String argValue = compilerOption.toString(); // XXX snoop
  193. String arg = Option.ON.render(argValue);
  194. checkCompilerOption(arg, className);
  195. }
  196. }
  197. if (0 < notLoaded.length()) {
  198. System.err.println(
  199. getClass().getName()
  200. + ".testCompilerOptions()"
  201. + " warning: compiler options not tested because not loadable: "
  202. + notLoaded);
  203. }
  204. }
  205. /**
  206. * Checck that setting arg as spec compiler and setting up args
  207. * results in this compiler classname.
  208. * @param arg
  209. * @param className
  210. */
  211. void checkCompilerOption(String arg, String className) {
  212. MessageHandler handler = new MessageHandler();
  213. try {
  214. CompilerRun.Spec spec = null;
  215. try {
  216. spec = new CompilerRun.Spec();
  217. } catch (Throwable t) {
  218. t.printStackTrace(System.err);
  219. }
  220. assertTrue(spec != null);
  221. AbstractRunSpec.RT parentRuntime = new AbstractRunSpec.RT();
  222. String result;
  223. // String expResult;
  224. if (null != arg) {
  225. parentRuntime.setOptions(new String[] { arg });
  226. }
  227. if (!spec.adoptParentValues(parentRuntime, handler)) {
  228. if (0 != handler.numMessages(null, true)) {
  229. assertTrue(handler.toString(), false);
  230. } else {
  231. assertTrue("adopt failed, but no messages", false);
  232. }
  233. }
  234. result = "" + spec.testSetup.commandOptions;
  235. assertEquals("[]", result);
  236. assertEquals(className, spec.testSetup.compilerName);
  237. } finally {
  238. }
  239. }
  240. public void testSpecOptions() {
  241. Options options = CompilerRun.Spec.testAccessToOptions();
  242. OptionChecker optionChecker = new OptionChecker(options);
  243. // known failures: extdirs, aspectpath, Xlintfile <file>
  244. // progress, time, noExit, repeat <n>,
  245. // help,
  246. String[][] input =
  247. new String[][] {
  248. new String[] { "-verbose" },
  249. new String[] { "-incremental" },
  250. new String[] { "-emacssym" },
  251. new String[] { "-Xlint" },
  252. new String[] { "-Xlint:error" },
  253. new String[] { "-1.3" },
  254. new String[] { "-1.4" },
  255. new String[] { "-source", "1.3" },
  256. new String[] { "-source", "1.4" },
  257. new String[] { "-target", "1.1" },
  258. new String[] { "-target", "1.2" },
  259. new String[] { "-preserveAllLocals" },
  260. new String[] { "-referenceInfo" },
  261. new String[] { "-deprecation" },
  262. new String[] { "-noImportError" },
  263. new String[] { "-proceedOnError" }
  264. };
  265. String[][] literalInput =
  266. new String[][] {
  267. new String[] { "-nowarn" },
  268. new String[] { "-warn:deprecated" },
  269. new String[] { "-warn:deprecated,unverified" },
  270. new String[] { "-warn:deprecated", "-warn:unusedLocals" },
  271. new String[] { "-g" },
  272. new String[] { "-g", "-g:none" },
  273. new String[] { "-g:vars,source" },
  274. new String[] { "-verbose", "-g:vars,source" },
  275. };
  276. // normal
  277. for (int i = 0; i < input.length; i++) {
  278. optionChecker.checkOptions(input[i], input[i]);
  279. }
  280. for (int i = 0; i < literalInput.length; i++) {
  281. optionChecker.checkOptions(literalInput[i], literalInput[i]);
  282. }
  283. // force-on
  284. String[][] forceInput = duplicate(input, "!");
  285. for (int i = 0; i < input.length; i++) {
  286. optionChecker.checkOptions(forceInput[i], input[i]);
  287. }
  288. // force-off
  289. forceInput = duplicate(input, "^");
  290. String[] none = new String[0];
  291. for (int i = 0; i < input.length; i++) {
  292. optionChecker.checkOptions(forceInput[i], none);
  293. }
  294. }
  295. public void checkSourceTargetOverride(String name, int from, int to) {
  296. final String specOptions = "-" + name + ", 1." + from;
  297. String[] globalOptions = new String[] { "!" + name, "1." + to };
  298. boolean expectAdopted = true;
  299. String resultContains = "[-" + name + ", 1." + to;
  300. String messagesContain = null;
  301. MessageHandler handler =
  302. runTest(
  303. specOptions,
  304. globalOptions,
  305. expectAdopted,
  306. resultContains,
  307. messagesContain);
  308. checkMessages(handler, null);
  309. }
  310. void checkSourceTargetVersionConversion(
  311. String name,
  312. int i,
  313. boolean expectPass,
  314. String expectedErr) {
  315. final String specOptions = "-" + name + ", 1." + i;
  316. String[] globalOptions = new String[0];
  317. boolean expectAdopted = expectPass;
  318. String resultContains =
  319. !expectPass ? null : "[-" + name + ", 1." + i;
  320. String messagesContain = expectedErr;
  321. /*MessageHandler handler =*/
  322. runTest(
  323. specOptions,
  324. globalOptions,
  325. expectAdopted,
  326. resultContains,
  327. messagesContain);
  328. }
  329. /**
  330. * Drive option-setting for CompilerRun.Spec, including
  331. * expected errors.
  332. * @param specOptions
  333. * @param globalOptions
  334. * @param expectAdopted
  335. * @param resultContains
  336. * @param messagesContain
  337. * @return
  338. */
  339. MessageHandler runTest(
  340. String specOptions,
  341. String[] globalOptions,
  342. boolean expectAdopted,
  343. String resultContains,
  344. String messagesContain) {
  345. MessageHandler handler = new MessageHandler();
  346. try {
  347. CompilerRun.Spec spec = new CompilerRun.Spec();
  348. AbstractRunSpec.RT parentRuntime = new AbstractRunSpec.RT();
  349. if (!LangUtil.isEmpty(specOptions)) {
  350. spec.setOptions(specOptions);
  351. }
  352. if (!LangUtil.isEmpty(globalOptions)) {
  353. parentRuntime.setOptions(globalOptions);
  354. }
  355. boolean adopted =
  356. spec.adoptParentValues(parentRuntime, handler);
  357. if (adopted != expectAdopted) {
  358. String s =
  359. (expectAdopted ? "not " : "")
  360. + "adopted spec="
  361. + specOptions
  362. + " globals="
  363. + (LangUtil.isEmpty(globalOptions)
  364. ? "[]"
  365. : Arrays.asList(globalOptions).toString())
  366. + " -- "
  367. + handler;
  368. assertTrue(s, false);
  369. }
  370. if (null != resultContains) {
  371. String result = "" + spec.testSetup.commandOptions;
  372. if (-1 == result.indexOf(resultContains)) {
  373. assertTrue(
  374. "expected " + resultContains + " got " + result,
  375. false);
  376. }
  377. }
  378. if (null != messagesContain) {
  379. boolean haveMessages =
  380. (0 != handler.numMessages(null, true));
  381. if (!haveMessages) {
  382. assertTrue("expected " + messagesContain, false);
  383. } else {
  384. String messages = handler.toString();
  385. if (-1 == messages.indexOf(messagesContain)) {
  386. assertTrue(
  387. "expected "
  388. + messagesContain
  389. + " got "
  390. + messages,
  391. false);
  392. }
  393. }
  394. }
  395. return handler;
  396. } finally {
  397. }
  398. }
  399. void checkSetupArgs(String arg, final boolean isTestArg) {
  400. MessageHandler handler = new MessageHandler();
  401. try {
  402. CompilerRun.Spec spec = null;
  403. try {
  404. spec = new CompilerRun.Spec();
  405. } catch (Throwable t) {
  406. t.printStackTrace(System.err);
  407. }
  408. assertTrue(spec != null);
  409. AbstractRunSpec.RT parentRuntime = new AbstractRunSpec.RT();
  410. String result;
  411. String expResult;
  412. // -------- local set
  413. // global - (set) does not change local-set
  414. parentRuntime.setOptions(new String[] { "-" + arg });
  415. assertTrue(spec.adoptParentValues(parentRuntime, handler));
  416. if (0 != handler.numMessages(null, true)) {
  417. assertTrue(handler.toString(), false);
  418. }
  419. result = "" + spec.testSetup.commandOptions;
  420. expResult = (isTestArg ? "[]" : "[-" + arg + "]");
  421. assertTrue(result, expResult.equals(result));
  422. // global ^ (force-off) to disable
  423. spec.setOptions("-" + arg);
  424. parentRuntime.setOptions(new String[] { "^" + arg });
  425. assertTrue(spec.adoptParentValues(parentRuntime, handler));
  426. if (0 != handler.numMessages(null, true)) {
  427. assertTrue(handler.toString(), false);
  428. }
  429. result = "" + spec.testSetup.commandOptions;
  430. assertTrue(result, "[]".equals(result));
  431. // global ! (force-on) does not change local-set
  432. parentRuntime.setOptions(new String[] { "!" + arg });
  433. assertTrue(spec.adoptParentValues(parentRuntime, handler));
  434. if (0 != handler.numMessages(null, true)) {
  435. assertTrue(handler.toString(), false);
  436. }
  437. result = "" + spec.testSetup.commandOptions;
  438. expResult = (isTestArg ? "[]" : "[-" + arg + "]");
  439. if (!expResult.equals(result)) {
  440. assertTrue(
  441. "expected " + expResult + " got " + result,
  442. false);
  443. }
  444. // global (unset) does not change local-set
  445. parentRuntime.setOptions(new String[] { "" });
  446. assertTrue(spec.adoptParentValues(parentRuntime, handler));
  447. if (0 != handler.numMessages(null, true)) {
  448. assertTrue(handler.toString(), false);
  449. }
  450. result = "" + spec.testSetup.commandOptions;
  451. expResult = (isTestArg ? "[]" : "[-" + arg + "]");
  452. assertTrue(result, expResult.equals(result));
  453. // -------- local force-on
  454. // global ^ (force-off) conflicts with local force-on
  455. spec.setOptions("!" + arg);
  456. parentRuntime.setOptions(new String[] { "^" + arg });
  457. assertTrue(!spec.adoptParentValues(parentRuntime, handler));
  458. assertTrue(0 != handler.numMessages(null, true));
  459. handler.init();
  460. // global ! (force-on) does not change local force-on
  461. parentRuntime.setOptions(new String[] { "!" + arg });
  462. assertTrue(spec.adoptParentValues(parentRuntime, handler));
  463. if (0 != handler.numMessages(null, true)) {
  464. assertTrue(handler.toString(), false);
  465. }
  466. result = "" + spec.testSetup.commandOptions;
  467. expResult = (isTestArg ? "[]" : "[-" + arg + "]");
  468. assertTrue(result, expResult.equals(result));
  469. // global - (set) does not change local force-on
  470. parentRuntime.setOptions(new String[] { "-" + arg });
  471. assertTrue(spec.adoptParentValues(parentRuntime, handler));
  472. if (0 != handler.numMessages(null, true)) {
  473. assertTrue(handler.toString(), false);
  474. }
  475. result = "" + spec.testSetup.commandOptions;
  476. expResult = (isTestArg ? "[]" : "[-" + arg + "]");
  477. assertTrue(result, expResult.equals(result));
  478. // global (unset) does not change local force-on
  479. parentRuntime.setOptions(new String[] { "" });
  480. assertTrue(spec.adoptParentValues(parentRuntime, handler));
  481. if (0 != handler.numMessages(null, true)) {
  482. assertTrue(handler.toString(), false);
  483. }
  484. result = "" + spec.testSetup.commandOptions;
  485. expResult = (isTestArg ? "[]" : "[-" + arg + "]");
  486. assertTrue(result, expResult.equals(result));
  487. // -------- local force-off
  488. // global ^ (force-off) does not change local force-off
  489. spec.setOptions("^" + arg);
  490. parentRuntime.setOptions(new String[] { "^" + arg });
  491. assertTrue(spec.adoptParentValues(parentRuntime, handler));
  492. if (0 != handler.numMessages(null, true)) {
  493. assertTrue(handler.toString(), false);
  494. }
  495. result = "" + spec.testSetup.commandOptions;
  496. assertTrue(result, ("[]").equals(result));
  497. // global ! (force-on) conflicts with local force-off
  498. parentRuntime.setOptions(new String[] { "!" + arg });
  499. assertTrue(!spec.adoptParentValues(parentRuntime, handler));
  500. assertTrue(0 != handler.numMessages(null, true));
  501. handler.init();
  502. // global - (set) overridden by local force-off
  503. parentRuntime.setOptions(new String[] { "-" + arg });
  504. assertTrue(spec.adoptParentValues(parentRuntime, handler));
  505. if (0 != handler.numMessages(null, true)) {
  506. assertTrue(handler.toString(), false);
  507. }
  508. result = "" + spec.testSetup.commandOptions;
  509. assertTrue(result, ("[]").equals(result));
  510. // global (unset) does not change local force-off
  511. parentRuntime.setOptions(new String[] { "" });
  512. assertTrue(spec.adoptParentValues(parentRuntime, handler));
  513. if (0 != handler.numMessages(null, true)) {
  514. assertTrue(handler.toString(), false);
  515. }
  516. result = "" + spec.testSetup.commandOptions;
  517. assertTrue(result, ("[]").equals(result));
  518. // undefined whether global set overrides local set
  519. // for different sibling options
  520. } finally {
  521. if (PRINTING && (0 < handler.numMessages(null, true))) {
  522. MessageUtil.print(System.err, handler, "checkSetupArgs: ");
  523. }
  524. }
  525. }
  526. }