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.

OptionsTest.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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 Common Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/cpl-v10.html
  8. *
  9. * Contributors:
  10. * Wes Isberg initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.testing.util.options;
  13. import junit.framework.*;
  14. import org.aspectj.testing.util.options.Option.InvalidInputException;
  15. /**
  16. */
  17. public class OptionsTest extends TestCase {
  18. private static Options OPTIONS;
  19. private static Options getOptions() {
  20. if (null == OPTIONS) {
  21. OPTIONS = new Options(true);
  22. Option.Factory factory = new Option.Factory("OptionsTest");
  23. OPTIONS.addOption(factory.create("verbose"));
  24. OPTIONS.addOption(factory.create("quiet"));
  25. OPTIONS.addOption(factory.create("debug"));
  26. OPTIONS.addOption(
  27. factory.create("ajc", "compiler", Option.FORCE_PREFIXES, false));
  28. OPTIONS.addOption(
  29. factory.create(
  30. "eclipse",
  31. "compiler",
  32. Option.FORCE_PREFIXES,
  33. false));
  34. OPTIONS.addOption(
  35. factory.create(
  36. "ajdeCompiler",
  37. "compiler",
  38. Option.FORCE_PREFIXES,
  39. false));
  40. OPTIONS.addOption(
  41. factory.create("1.3", "compliance", Option.FORCE_PREFIXES, false));
  42. OPTIONS.addOption(
  43. factory.create("1.4", "compliance", Option.FORCE_PREFIXES, false));
  44. // treating multi-arg as single - extrinsic flatten/unflatten
  45. OPTIONS.addOption(
  46. factory.create("target11", "target", Option.FORCE_PREFIXES, false));
  47. OPTIONS.addOption(
  48. factory.create("target12", "target", Option.FORCE_PREFIXES, false));
  49. OPTIONS.addOption(
  50. factory.create("source13", "source", Option.FORCE_PREFIXES, false));
  51. OPTIONS.addOption(
  52. factory.create("source14", "source", Option.FORCE_PREFIXES, false));
  53. // suffix options (a) -warn:... (b) -g, -g:...
  54. Assert.assertTrue(factory.setupFamily("warning", true));
  55. OPTIONS.addOption(
  56. factory.create("warn", "warning", Option.STANDARD_PREFIXES, true));
  57. Assert.assertTrue(factory.setupFamily("debugSymbols", true));
  58. OPTIONS.addOption(
  59. factory.create(
  60. "g",
  61. "debugSymbols",
  62. Option.STANDARD_PREFIXES,
  63. false));
  64. OPTIONS.addOption(
  65. factory.create(
  66. "g:",
  67. "debugSymbols",
  68. Option.STANDARD_PREFIXES,
  69. true));
  70. // treating multi-arg as single - intrinsic flatten/unflatten
  71. OPTIONS
  72. .addOption(
  73. factory
  74. .create(
  75. "target",
  76. "target",
  77. Option.FORCE_PREFIXES,
  78. false,
  79. new String[][] {
  80. new String[] { "1.1", "1.2" }
  81. }));
  82. OPTIONS
  83. .addOption(
  84. factory
  85. .create(
  86. "source",
  87. "source",
  88. Option.FORCE_PREFIXES,
  89. false,
  90. new String[][] {
  91. new String[] { "1.3", "1.4" }
  92. }));
  93. OPTIONS.freeze();
  94. }
  95. return OPTIONS;
  96. }
  97. // private boolean verbose;
  98. private OptionChecker localOptionChecker;
  99. public void testDebugCase() {
  100. OptionChecker optionChecker = getOptionChecker();
  101. String[] input = new String[] {
  102. };
  103. String[] expected = new String[0];
  104. optionChecker.checkOptions(input, expected);
  105. input = new String[] { "-target", "1.1", "^target", "1.2" };
  106. expected = new String[] { "-target", "1.1" };
  107. optionChecker.checkOptions(input, expected);
  108. }
  109. public void testNegDebugCase() {
  110. OptionChecker optionChecker = getOptionChecker();
  111. String[] input = new String[] { "-target" };
  112. String expectedInValuesException = "not enough arguments";
  113. String expectedMissedMatchErr = null;
  114. String expectedResolveErr = null;
  115. optionChecker.checkOptionsNegative(
  116. input,
  117. expectedInValuesException,
  118. expectedMissedMatchErr,
  119. expectedResolveErr);
  120. }
  121. public void testOptionsPositive() {
  122. OptionChecker optionChecker = getOptionChecker();
  123. String[] input = new String[] {
  124. };
  125. String[] expected = new String[0];
  126. optionChecker.checkOptions(input, expected);
  127. input = new String[] { "-verbose" };
  128. expected = new String[] { "-verbose" };
  129. optionChecker.checkOptions(input, expected);
  130. input = new String[] { "!verbose" };
  131. expected = new String[] { "-verbose" };
  132. optionChecker.checkOptions(input, expected);
  133. input = new String[] { "!verbose", "-verbose" };
  134. optionChecker.checkOptions(input, expected);
  135. input = new String[] { "-verbose", "!verbose" };
  136. optionChecker.checkOptions(input, expected);
  137. input = new String[] { "^verbose" };
  138. expected = new String[] {
  139. };
  140. optionChecker.checkOptions(input, expected);
  141. input = new String[] { "^verbose", "-verbose" };
  142. optionChecker.checkOptions(input, expected);
  143. input = new String[] { "-verbose", "^verbose" };
  144. optionChecker.checkOptions(input, expected);
  145. input = new String[] { "-verbose", "-debug" };
  146. expected = new String[] { "-verbose", "-debug" };
  147. optionChecker.checkOptions(input, expected);
  148. input = new String[] { "!verbose", "!debug" };
  149. expected = new String[] { "-verbose", "-debug" };
  150. optionChecker.checkOptions(input, expected);
  151. input = new String[] { "-verbose", "^verbose", "!debug" };
  152. expected = new String[] { "-debug" };
  153. optionChecker.checkOptions(input, expected);
  154. input = new String[] { "^verbose", "-verbose", "!debug" };
  155. optionChecker.checkOptions(input, expected);
  156. input = new String[] { "!debug", "^verbose", "-verbose" };
  157. optionChecker.checkOptions(input, expected);
  158. input = new String[] { "-ajc" };
  159. expected = new String[] { "-ajc" };
  160. optionChecker.checkOptions(input, expected);
  161. input = new String[] { "-eclipse" };
  162. expected = new String[] { "-eclipse" };
  163. optionChecker.checkOptions(input, expected);
  164. input = new String[] { "-eclipse", "-ajc", "!ajc" };
  165. expected = new String[] { "-ajc" };
  166. optionChecker.checkOptions(input, expected);
  167. input = new String[] { "-eclipse", "!ajc" };
  168. optionChecker.checkOptions(input, expected);
  169. input = new String[] { "-ajdeCompiler", "^ajc" };
  170. expected = new String[] { "-ajdeCompiler" };
  171. optionChecker.checkOptions(input, expected);
  172. input = new String[] { "!ajdeCompiler", "^ajc" };
  173. optionChecker.checkOptions(input, expected);
  174. input =
  175. new String[] {
  176. "-verbose",
  177. "^verbose",
  178. "!quiet",
  179. "-quiet",
  180. "-debug",
  181. "^debug" };
  182. expected = new String[] { "-quiet" };
  183. optionChecker.checkOptions(input, expected);
  184. input =
  185. new String[] {
  186. "-verbose",
  187. "^debug",
  188. "!quiet",
  189. "!quiet",
  190. "-debug",
  191. "^verbose" };
  192. expected = new String[] { "-quiet" };
  193. optionChecker.checkOptions(input, expected);
  194. }
  195. public void testOptionsNegative() {
  196. OptionChecker optionChecker = getOptionChecker();
  197. String[] input = new String[] { "-unknown" };
  198. String expectedMissedMatchErr = "-unknown";
  199. String expectedResolveErr = null;
  200. optionChecker.checkOptionsNegative(
  201. input,
  202. expectedMissedMatchErr,
  203. expectedResolveErr);
  204. input = new String[] { "!verbose", "^verbose" };
  205. expectedMissedMatchErr = null;
  206. expectedResolveErr = "conflict";
  207. optionChecker.checkOptionsNegative(
  208. input,
  209. expectedMissedMatchErr,
  210. expectedResolveErr);
  211. input = new String[] { "!ajc", "!eclipse" };
  212. optionChecker.checkOptionsNegative(
  213. input,
  214. expectedMissedMatchErr,
  215. expectedResolveErr);
  216. input = new String[] { "-ajc", "-eclipse" };
  217. expectedResolveErr = "collision";
  218. optionChecker.checkOptionsNegative(
  219. input,
  220. expectedMissedMatchErr,
  221. expectedResolveErr);
  222. input = new String[] { "-verbose", "-verbose" };
  223. expectedResolveErr = null; // duplicates redundant, not colliding
  224. optionChecker.checkOptionsNegative(
  225. input,
  226. expectedMissedMatchErr,
  227. expectedResolveErr);
  228. }
  229. public void testMissedMatches() throws InvalidInputException {
  230. checkMissedMatches(new int[0], Values.EMPTY);
  231. checkMissedMatches(new int[] { 0 },
  232. Values.wrapValues(new Option.Value[1])); // null in [0]
  233. checkMissedMatches(
  234. new int[] { 0, 1, 2 },
  235. Values.wrapValues(new Option.Value[] { null, null, null }));
  236. Option.Factory factory = new Option.Factory("testMissedMatches");
  237. Option single = factory.create("verbose");
  238. Option multiple =
  239. factory
  240. .create(
  241. "source",
  242. "source",
  243. Option.STANDARD_PREFIXES,
  244. false,
  245. new String[][] { new String[] { "1.3", "1.4" }
  246. });
  247. Options options = new Options(false);
  248. options.addOption(single);
  249. options.addOption(multiple);
  250. options.freeze();
  251. int[] expectNone = new int[0];
  252. String[] input = new String[] { "-verbose" };
  253. Values result = options.acceptInput(input);
  254. checkMissedMatches(expectNone, result);
  255. input = new String[] { "-verbose", "-verbose" };
  256. result = options.acceptInput(input);
  257. checkMissedMatches(expectNone, result);
  258. input = new String[] { "-source", "1.3" };
  259. result = options.acceptInput(input);
  260. checkMissedMatches(expectNone, result);
  261. input = new String[] { "-source", "1.4" };
  262. result = options.acceptInput(input);
  263. checkMissedMatches(expectNone, result);
  264. input = new String[] { "-verbose", "-missed" };
  265. result = options.acceptInput(input);
  266. checkMissedMatches(new int[] { 1 }, result);
  267. input = new String[] { "-source", "1.4", "-missed" };
  268. result = options.acceptInput(input);
  269. checkMissedMatches(new int[] { 2 }, result);
  270. input = new String[] { "-source", "1.4", "-missed", "-verbose" };
  271. result = options.acceptInput(input);
  272. checkMissedMatches(new int[] { 2 }, result);
  273. }
  274. void checkMissedMatches(int[] expected, Values actual) {
  275. int[] result = actual.indexMissedMatches();
  276. boolean failed = (result.length != expected.length);
  277. for (int i = 0; !failed && (i < result.length); i++) {
  278. failed = (result[i] != expected[i]);
  279. }
  280. if (failed) {
  281. assertTrue(
  282. "expected "
  283. + Values.IntList.render(expected)
  284. + " got "
  285. + Values.IntList.render(result)
  286. + " for "
  287. + actual,
  288. false);
  289. }
  290. }
  291. public void testComplexOptionsPositive() {
  292. OptionChecker optionChecker = getOptionChecker();
  293. String[] input = new String[] {
  294. };
  295. String[] expected = new String[0];
  296. optionChecker.checkOptions(input, expected);
  297. input = new String[] { "-target11" };
  298. expected = new String[] { "-target11" };
  299. optionChecker.checkOptions(input, expected);
  300. input = new String[] { "!target12" };
  301. expected = new String[] { "-target12" };
  302. optionChecker.checkOptions(input, expected);
  303. input = new String[] { "!target12", "-target11" };
  304. optionChecker.checkOptions(input, expected);
  305. input = new String[] { "!target12", "^target11" };
  306. optionChecker.checkOptions(input, expected);
  307. input = new String[] { "^target12", "^target11" };
  308. expected = new String[] {
  309. };
  310. optionChecker.checkOptions(input, expected);
  311. input = new String[] { "!1.3" };
  312. expected = new String[] { "-1.3" };
  313. optionChecker.checkOptions(input, expected);
  314. input = new String[] { "!1.3", "-1.4" };
  315. optionChecker.checkOptions(input, expected);
  316. input = new String[] { "!1.3", "^1.4" };
  317. optionChecker.checkOptions(input, expected);
  318. input = new String[] { "^1.3", "^1.4" };
  319. expected = new String[] {
  320. };
  321. optionChecker.checkOptions(input, expected);
  322. }
  323. public void testMultiArgOptionsPositive() {
  324. OptionChecker optionChecker = getOptionChecker();
  325. String[] input = new String[] {
  326. };
  327. String[] expected = new String[0];
  328. optionChecker.checkOptions(input, expected);
  329. input = new String[] { "-target", "1.1" };
  330. expected = new String[] { "-target", "1.1" };
  331. optionChecker.checkOptions(input, expected);
  332. input = new String[] { "!target", "1.1" };
  333. optionChecker.checkOptions(input, expected);
  334. input = new String[] { "!target", "1.1", "-target", "1.2" };
  335. optionChecker.checkOptions(input, expected);
  336. input = new String[] { "-target", "1.1", "^target", "1.2" };
  337. optionChecker.checkOptions(input, expected);
  338. input = new String[] { "-source", "1.3" };
  339. expected = new String[] { "-source", "1.3" };
  340. optionChecker.checkOptions(input, expected);
  341. input = new String[] { "!source", "1.3", "-source", "1.4" };
  342. optionChecker.checkOptions(input, expected);
  343. input = new String[] { "-source", "1.3", "^source", "1.4" };
  344. input =
  345. new String[] {
  346. "-source",
  347. "1.3",
  348. "^source",
  349. "1.4",
  350. "!source",
  351. "1.3",
  352. "-source",
  353. "1.4" };
  354. optionChecker.checkOptions(input, expected);
  355. }
  356. public void testMultiArgOptionsNegative() {
  357. OptionChecker optionChecker = getOptionChecker();
  358. String[] input = new String[] { "-target" };
  359. String expectedException = "not enough arguments";
  360. optionChecker.checkOptionsNegative(
  361. input,
  362. expectedException,
  363. null,
  364. null);
  365. input = new String[] { "-source" };
  366. optionChecker.checkOptionsNegative(
  367. input,
  368. expectedException,
  369. null,
  370. null);
  371. input = new String[] { "-source", "1.1" };
  372. expectedException = "not permitted";
  373. optionChecker.checkOptionsNegative(
  374. input,
  375. expectedException,
  376. null,
  377. null);
  378. input = new String[] { "-target", "1.3" };
  379. optionChecker.checkOptionsNegative(
  380. input,
  381. expectedException,
  382. null,
  383. null);
  384. }
  385. public void testMultipleInput() {
  386. OptionChecker optionChecker = getOptionChecker();
  387. String[][] input =
  388. new String[][] {
  389. new String[] { "-warn:deprecated" },
  390. new String[] { "-warn:deprecated,unverified" },
  391. new String[] { "-warn:deprecated", "-warn:unusedLocals" },
  392. new String[] { "-g" },
  393. new String[] { "-g", "-g:none" },
  394. new String[] { "-g:vars,source" },
  395. new String[] { "-verbose", "-g:vars,source" },
  396. };
  397. for (int i = 0; i < input.length; i++) {
  398. optionChecker.checkOptions(input[i], input[i]);
  399. }
  400. }
  401. private OptionChecker getOptionChecker() {
  402. if (null == localOptionChecker) {
  403. localOptionChecker = new OptionChecker(getOptions());
  404. }
  405. return localOptionChecker;
  406. }
  407. }