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

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