123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487 |
- /* *******************************************************************
- * Copyright (c) 2003 Contributors.
- * All rights reserved.
- * This program and the accompanying materials are made available
- * under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Wes Isberg initial implementation
- * ******************************************************************/
-
- package org.aspectj.testing.util.options;
-
- import org.aspectj.testing.util.options.Option.InvalidInputException;
-
- import junit.framework.Assert;
- import junit.framework.TestCase;
-
- /**
- */
- public class OptionsTest extends TestCase {
-
- private static Options OPTIONS;
-
- private static Options getOptions() {
- if (null == OPTIONS) {
- OPTIONS = new Options(true);
- Option.Factory factory = new Option.Factory("OptionsTest");
- OPTIONS.addOption(factory.create("verbose"));
- OPTIONS.addOption(factory.create("quiet"));
- OPTIONS.addOption(factory.create("debug"));
- OPTIONS.addOption(
- factory.create("ajc", "compiler", Option.FORCE_PREFIXES, false));
- OPTIONS.addOption(
- factory.create(
- "eclipse",
- "compiler",
- Option.FORCE_PREFIXES,
- false));
- OPTIONS.addOption(
- factory.create(
- "ajdeCompiler",
- "compiler",
- Option.FORCE_PREFIXES,
- false));
- OPTIONS.addOption(
- factory.create("1.3", "compliance", Option.FORCE_PREFIXES, false));
- OPTIONS.addOption(
- factory.create("1.4", "compliance", Option.FORCE_PREFIXES, false));
-
- // treating multi-arg as single - extrinsic flatten/unflatten
- OPTIONS.addOption(
- factory.create("target11", "target", Option.FORCE_PREFIXES, false));
- OPTIONS.addOption(
- factory.create("target12", "target", Option.FORCE_PREFIXES, false));
- OPTIONS.addOption(
- factory.create("source13", "source", Option.FORCE_PREFIXES, false));
- OPTIONS.addOption(
- factory.create("source14", "source", Option.FORCE_PREFIXES, false));
-
- // suffix options (a) -warn:... (b) -g, -g:...
- Assert.assertTrue(factory.setupFamily("warning", true));
- OPTIONS.addOption(
- factory.create("warn", "warning", Option.STANDARD_PREFIXES, true));
-
- Assert.assertTrue(factory.setupFamily("debugSymbols", true));
- OPTIONS.addOption(
- factory.create(
- "g",
- "debugSymbols",
- Option.STANDARD_PREFIXES,
- false));
- OPTIONS.addOption(
- factory.create(
- "g:",
- "debugSymbols",
- Option.STANDARD_PREFIXES,
- true));
-
- // treating multi-arg as single - intrinsic flatten/unflatten
- OPTIONS
- .addOption(
- factory
- .create(
- "target",
- "target",
- Option.FORCE_PREFIXES,
- false,
- new String[][] {
- new String[] { "1.1", "1.2" }
- }));
- OPTIONS
- .addOption(
- factory
- .create(
- "source",
- "source",
- Option.FORCE_PREFIXES,
- false,
- new String[][] {
- new String[] { "1.3", "1.4" }
- }));
- OPTIONS.freeze();
- }
- return OPTIONS;
- }
-
- // private boolean verbose;
- private OptionChecker localOptionChecker;
-
- public void testDebugCase() {
- OptionChecker optionChecker = getOptionChecker();
- String[] input = new String[] {
- };
- String[] expected = new String[0];
- optionChecker.checkOptions(input, expected);
-
- input = new String[] { "-target", "1.1", "^target", "1.2" };
- expected = new String[] { "-target", "1.1" };
-
- optionChecker.checkOptions(input, expected);
- }
- public void testNegDebugCase() {
- OptionChecker optionChecker = getOptionChecker();
- String[] input = new String[] { "-target" };
- String expectedInValuesException = "not enough arguments";
- String expectedMissedMatchErr = null;
- String expectedResolveErr = null;
-
- optionChecker.checkOptionsNegative(
- input,
- expectedInValuesException,
- expectedMissedMatchErr,
- expectedResolveErr);
- }
-
- public void testOptionsPositive() {
- OptionChecker optionChecker = getOptionChecker();
- String[] input = new String[] {
- };
- String[] expected = new String[0];
- optionChecker.checkOptions(input, expected);
-
- input = new String[] { "-verbose" };
- expected = new String[] { "-verbose" };
- optionChecker.checkOptions(input, expected);
-
- input = new String[] { "!verbose" };
- expected = new String[] { "-verbose" };
- optionChecker.checkOptions(input, expected);
-
- input = new String[] { "!verbose", "-verbose" };
- optionChecker.checkOptions(input, expected);
-
- input = new String[] { "-verbose", "!verbose" };
- optionChecker.checkOptions(input, expected);
-
- input = new String[] { "^verbose" };
- expected = new String[] {
- };
- optionChecker.checkOptions(input, expected);
-
- input = new String[] { "^verbose", "-verbose" };
- optionChecker.checkOptions(input, expected);
-
- input = new String[] { "-verbose", "^verbose" };
- optionChecker.checkOptions(input, expected);
-
- input = new String[] { "-verbose", "-debug" };
- expected = new String[] { "-verbose", "-debug" };
- optionChecker.checkOptions(input, expected);
-
- input = new String[] { "!verbose", "!debug" };
- expected = new String[] { "-verbose", "-debug" };
- optionChecker.checkOptions(input, expected);
-
- input = new String[] { "-verbose", "^verbose", "!debug" };
- expected = new String[] { "-debug" };
- optionChecker.checkOptions(input, expected);
-
- input = new String[] { "^verbose", "-verbose", "!debug" };
- optionChecker.checkOptions(input, expected);
-
- input = new String[] { "!debug", "^verbose", "-verbose" };
- optionChecker.checkOptions(input, expected);
-
- input = new String[] { "-ajc" };
- expected = new String[] { "-ajc" };
- optionChecker.checkOptions(input, expected);
-
- input = new String[] { "-eclipse" };
- expected = new String[] { "-eclipse" };
- optionChecker.checkOptions(input, expected);
-
- input = new String[] { "-eclipse", "-ajc", "!ajc" };
- expected = new String[] { "-ajc" };
- optionChecker.checkOptions(input, expected);
-
- input = new String[] { "-eclipse", "!ajc" };
- optionChecker.checkOptions(input, expected);
-
- input = new String[] { "-ajdeCompiler", "^ajc" };
- expected = new String[] { "-ajdeCompiler" };
- optionChecker.checkOptions(input, expected);
-
- input = new String[] { "!ajdeCompiler", "^ajc" };
- optionChecker.checkOptions(input, expected);
-
- input =
- new String[] {
- "-verbose",
- "^verbose",
- "!quiet",
- "-quiet",
- "-debug",
- "^debug" };
- expected = new String[] { "-quiet" };
- optionChecker.checkOptions(input, expected);
-
- input =
- new String[] {
- "-verbose",
- "^debug",
- "!quiet",
- "!quiet",
- "-debug",
- "^verbose" };
- expected = new String[] { "-quiet" };
- optionChecker.checkOptions(input, expected);
- }
-
- public void testOptionsNegative() {
- OptionChecker optionChecker = getOptionChecker();
- String[] input = new String[] { "-unknown" };
- String expectedMissedMatchErr = "-unknown";
- String expectedResolveErr = null;
- optionChecker.checkOptionsNegative(
- input,
- expectedMissedMatchErr,
- expectedResolveErr);
-
- input = new String[] { "!verbose", "^verbose" };
- expectedMissedMatchErr = null;
- expectedResolveErr = "conflict";
- optionChecker.checkOptionsNegative(
- input,
- expectedMissedMatchErr,
- expectedResolveErr);
-
- input = new String[] { "!ajc", "!eclipse" };
- optionChecker.checkOptionsNegative(
- input,
- expectedMissedMatchErr,
- expectedResolveErr);
-
- input = new String[] { "-ajc", "-eclipse" };
- expectedResolveErr = "collision";
- optionChecker.checkOptionsNegative(
- input,
- expectedMissedMatchErr,
- expectedResolveErr);
-
- input = new String[] { "-verbose", "-verbose" };
- expectedResolveErr = null; // duplicates redundant, not colliding
- optionChecker.checkOptionsNegative(
- input,
- expectedMissedMatchErr,
- expectedResolveErr);
- }
-
- public void testMissedMatches() throws InvalidInputException {
- checkMissedMatches(new int[0], Values.EMPTY);
- checkMissedMatches(new int[] { 0 },
- Values.wrapValues(new Option.Value[1])); // null in [0]
- checkMissedMatches(
- new int[] { 0, 1, 2 },
- Values.wrapValues(new Option.Value[] { null, null, null }));
-
- Option.Factory factory = new Option.Factory("testMissedMatches");
- Option single = factory.create("verbose");
- Option multiple =
- factory
- .create(
- "source",
- "source",
- Option.STANDARD_PREFIXES,
- false,
- new String[][] { new String[] { "1.3", "1.4" }
- });
-
- Options options = new Options(false);
- options.addOption(single);
- options.addOption(multiple);
- options.freeze();
- int[] expectNone = new int[0];
- String[] input = new String[] { "-verbose" };
- Values result = options.acceptInput(input);
- checkMissedMatches(expectNone, result);
-
- input = new String[] { "-verbose", "-verbose" };
- result = options.acceptInput(input);
- checkMissedMatches(expectNone, result);
-
- input = new String[] { "-source", "1.3" };
- result = options.acceptInput(input);
- checkMissedMatches(expectNone, result);
-
- input = new String[] { "-source", "1.4" };
- result = options.acceptInput(input);
- checkMissedMatches(expectNone, result);
-
- input = new String[] { "-verbose", "-missed" };
- result = options.acceptInput(input);
- checkMissedMatches(new int[] { 1 }, result);
-
- input = new String[] { "-source", "1.4", "-missed" };
- result = options.acceptInput(input);
- checkMissedMatches(new int[] { 2 }, result);
-
- input = new String[] { "-source", "1.4", "-missed", "-verbose" };
- result = options.acceptInput(input);
- checkMissedMatches(new int[] { 2 }, result);
-
- }
-
- void checkMissedMatches(int[] expected, Values actual) {
- int[] result = actual.indexMissedMatches();
- boolean failed = (result.length != expected.length);
-
- for (int i = 0; !failed && (i < result.length); i++) {
- failed = (result[i] != expected[i]);
- }
- if (failed) {
- assertTrue(
- "expected "
- + Values.IntList.render(expected)
- + " got "
- + Values.IntList.render(result)
- + " for "
- + actual,
- false);
- }
- }
-
- public void testComplexOptionsPositive() {
- OptionChecker optionChecker = getOptionChecker();
- String[] input = new String[] {
- };
- String[] expected = new String[0];
- optionChecker.checkOptions(input, expected);
-
- input = new String[] { "-target11" };
- expected = new String[] { "-target11" };
- optionChecker.checkOptions(input, expected);
-
- input = new String[] { "!target12" };
- expected = new String[] { "-target12" };
- optionChecker.checkOptions(input, expected);
-
- input = new String[] { "!target12", "-target11" };
- optionChecker.checkOptions(input, expected);
-
- input = new String[] { "!target12", "^target11" };
- optionChecker.checkOptions(input, expected);
-
- input = new String[] { "^target12", "^target11" };
- expected = new String[] {
- };
- optionChecker.checkOptions(input, expected);
-
- input = new String[] { "!1.3" };
- expected = new String[] { "-1.3" };
- optionChecker.checkOptions(input, expected);
-
- input = new String[] { "!1.3", "-1.4" };
- optionChecker.checkOptions(input, expected);
-
- input = new String[] { "!1.3", "^1.4" };
- optionChecker.checkOptions(input, expected);
-
- input = new String[] { "^1.3", "^1.4" };
- expected = new String[] {
- };
- optionChecker.checkOptions(input, expected);
-
- }
-
- public void testMultiArgOptionsPositive() {
- OptionChecker optionChecker = getOptionChecker();
- String[] input = new String[] {
- };
- String[] expected = new String[0];
- optionChecker.checkOptions(input, expected);
-
- input = new String[] { "-target", "1.1" };
- expected = new String[] { "-target", "1.1" };
- optionChecker.checkOptions(input, expected);
-
- input = new String[] { "!target", "1.1" };
- optionChecker.checkOptions(input, expected);
-
- input = new String[] { "!target", "1.1", "-target", "1.2" };
- optionChecker.checkOptions(input, expected);
-
- input = new String[] { "-target", "1.1", "^target", "1.2" };
- optionChecker.checkOptions(input, expected);
-
- input = new String[] { "-source", "1.3" };
- expected = new String[] { "-source", "1.3" };
- optionChecker.checkOptions(input, expected);
-
- input = new String[] { "!source", "1.3", "-source", "1.4" };
- optionChecker.checkOptions(input, expected);
-
- input = new String[] { "-source", "1.3", "^source", "1.4" };
- input =
- new String[] {
- "-source",
- "1.3",
- "^source",
- "1.4",
- "!source",
- "1.3",
- "-source",
- "1.4" };
- optionChecker.checkOptions(input, expected);
- }
-
- public void testMultiArgOptionsNegative() {
- OptionChecker optionChecker = getOptionChecker();
- String[] input = new String[] { "-target" };
- String expectedException = "not enough arguments";
-
- optionChecker.checkOptionsNegative(
- input,
- expectedException,
- null,
- null);
-
- input = new String[] { "-source" };
- optionChecker.checkOptionsNegative(
- input,
- expectedException,
- null,
- null);
-
- input = new String[] { "-source", "1.1" };
- expectedException = "not permitted";
- optionChecker.checkOptionsNegative(
- input,
- expectedException,
- null,
- null);
-
- input = new String[] { "-target", "1.3" };
- optionChecker.checkOptionsNegative(
- input,
- expectedException,
- null,
- null);
- }
-
- public void testMultipleInput() {
- OptionChecker optionChecker = getOptionChecker();
- String[][] input =
- new String[][] {
- new String[] { "-warn:deprecated" },
- new String[] { "-warn:deprecated,unverified" },
- new String[] { "-warn:deprecated", "-warn:unusedLocals" },
- new String[] { "-g" },
- new String[] { "-g", "-g:none" },
- new String[] { "-g:vars,source" },
- new String[] { "-verbose", "-g:vars,source" },
- };
- for (int i = 0; i < input.length; i++) {
- optionChecker.checkOptions(input[i], input[i]);
- }
- }
-
- private OptionChecker getOptionChecker() {
- if (null == localOptionChecker) {
- localOptionChecker = new OptionChecker(getOptions());
- }
- return localOptionChecker;
- }
- }
|