0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one or more
0003: * contributor license agreements. See the NOTICE file distributed with
0004: * this work for additional information regarding copyright ownership.
0005: * The ASF licenses this file to You under the Apache License, Version 2.0
0006: * (the "License"); you may not use this file except in compliance with
0007: * the License. You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
0014: * implied.
0015: *
0016: * See the License for the specific language governing permissions and
0017: * limitations under the License.
0018: */
0019: package org.apache.commons.cli.avalon;
0020:
0021: // Renamed from org.apache.avalon.excalibur.cli
0022:
0023: import java.util.List;
0024:
0025: import junit.framework.TestCase;
0026:
0027: /**
0028: *
0029: */
0030: public final class ClutilTestCase extends TestCase {
0031: private static final String[] ARGLIST1 = new String[] { "--you",
0032: "are", "--all", "-cler", "kid" };
0033:
0034: private static final String[] ARGLIST2 = new String[] {
0035: "-Dstupid=idiot", "are", "--all", "here", "-d" };
0036:
0037: private static final String[] ARGLIST3 = new String[] {
0038: // duplicates
0039: "-Dstupid=idiot", "are", "--all", "--all", "here" };
0040:
0041: private static final String[] ARGLIST4 = new String[] {
0042: // incompatable (blee/all)
0043: "-Dstupid", "idiot", "are", "--all", "--blee", "here" };
0044:
0045: private static final String[] ARGLIST5 = new String[] { "-f",
0046: "myfile.txt" };
0047:
0048: private static final int DEFINE_OPT = 'D';
0049:
0050: private static final int CASE_CHECK_OPT = 'd';
0051:
0052: private static final int YOU_OPT = 'y';
0053:
0054: private static final int ALL_OPT = 'a';
0055:
0056: private static final int CLEAR1_OPT = 'c';
0057:
0058: private static final int CLEAR2_OPT = 'l';
0059:
0060: private static final int CLEAR3_OPT = 'e';
0061:
0062: private static final int CLEAR5_OPT = 'r';
0063:
0064: private static final int BLEE_OPT = 'b';
0065:
0066: private static final int FILE_OPT = 'f';
0067:
0068: private static final int TAINT_OPT = 'T';
0069:
0070: private static final CLOptionDescriptor DEFINE = new CLOptionDescriptor(
0071: "define", CLOptionDescriptor.ARGUMENTS_REQUIRED_2,
0072: DEFINE_OPT, "define");
0073:
0074: private static final CLOptionDescriptor DEFINE_MANY = new CLOptionDescriptor(
0075: "define", CLOptionDescriptor.ARGUMENTS_REQUIRED_2
0076: | CLOptionDescriptor.DUPLICATES_ALLOWED,
0077: DEFINE_OPT, "define");
0078:
0079: private static final CLOptionDescriptor CASE_CHECK = new CLOptionDescriptor(
0080: "charCheck", CLOptionDescriptor.ARGUMENT_DISALLOWED,
0081: CASE_CHECK_OPT, "check character case sensitivity");
0082:
0083: private static final CLOptionDescriptor YOU = new CLOptionDescriptor(
0084: "you", CLOptionDescriptor.ARGUMENT_DISALLOWED, YOU_OPT,
0085: "you");
0086:
0087: private static final CLOptionDescriptor CLEAR1 = new CLOptionDescriptor(
0088: "c", CLOptionDescriptor.ARGUMENT_DISALLOWED, CLEAR1_OPT,
0089: "c");
0090:
0091: private static final CLOptionDescriptor CLEAR2 = new CLOptionDescriptor(
0092: "l", CLOptionDescriptor.ARGUMENT_DISALLOWED, CLEAR2_OPT,
0093: "l");
0094:
0095: private static final CLOptionDescriptor CLEAR3 = new CLOptionDescriptor(
0096: "e", CLOptionDescriptor.ARGUMENT_DISALLOWED, CLEAR3_OPT,
0097: "e");
0098:
0099: private static final CLOptionDescriptor CLEAR5 = new CLOptionDescriptor(
0100: "r", CLOptionDescriptor.ARGUMENT_DISALLOWED, CLEAR5_OPT,
0101: "r");
0102:
0103: private static final CLOptionDescriptor BLEE = new CLOptionDescriptor(
0104: "blee", CLOptionDescriptor.ARGUMENT_DISALLOWED, BLEE_OPT,
0105: "blee");
0106:
0107: private static final CLOptionDescriptor ALL = new CLOptionDescriptor(
0108: "all", CLOptionDescriptor.ARGUMENT_DISALLOWED, ALL_OPT,
0109: "all", new CLOptionDescriptor[] { BLEE });
0110:
0111: private static final CLOptionDescriptor FILE = new CLOptionDescriptor(
0112: "file", CLOptionDescriptor.ARGUMENT_REQUIRED, FILE_OPT,
0113: "the build file.");
0114:
0115: private static final CLOptionDescriptor TAINT = new CLOptionDescriptor(
0116: "taint", CLOptionDescriptor.ARGUMENT_OPTIONAL, TAINT_OPT,
0117: "turn on tainting checks (optional level).");
0118:
0119: private static final CLOptionDescriptor[] OPTIONS = new CLOptionDescriptor[] {
0120: new CLOptionDescriptor("none",
0121: CLOptionDescriptor.ARGUMENT_DISALLOWED
0122: | CLOptionDescriptor.DUPLICATES_ALLOWED,
0123: '0', "no parameter"),
0124:
0125: new CLOptionDescriptor("optional",
0126: CLOptionDescriptor.ARGUMENT_OPTIONAL
0127: | CLOptionDescriptor.DUPLICATES_ALLOWED,
0128: '?', "optional parameter"),
0129:
0130: new CLOptionDescriptor("one",
0131: CLOptionDescriptor.ARGUMENT_REQUIRED
0132: | CLOptionDescriptor.DUPLICATES_ALLOWED,
0133: '1', "one parameter"),
0134:
0135: new CLOptionDescriptor("two",
0136: CLOptionDescriptor.ARGUMENTS_REQUIRED_2
0137: | CLOptionDescriptor.DUPLICATES_ALLOWED,
0138: '2', "two parameters") };
0139:
0140: public ClutilTestCase() {
0141: this ("Command Line Interpreter Test Case");
0142: }
0143:
0144: public ClutilTestCase(String name) {
0145: super (name);
0146: }
0147:
0148: public void testOptionalArgWithSpace() {
0149: final CLOptionDescriptor[] options = new CLOptionDescriptor[] {
0150: ALL, TAINT };
0151:
0152: final String[] args = new String[] { "-T", "param", "-a" };
0153:
0154: final CLArgsParser parser = new CLArgsParser(args, options);
0155:
0156: assertNull(parser.getErrorString(), parser.getErrorString());
0157:
0158: final List clOptions = parser.getArguments();
0159: final int size = clOptions.size();
0160:
0161: assertEquals("Option count", 3, size);
0162:
0163: final CLOption option0 = (CLOption) clOptions.get(0);
0164: assertEquals("Option Code: " + option0.getDescriptor().getId(),
0165: TAINT_OPT, option0.getDescriptor().getId());
0166: assertEquals("Option Arg: " + option0.getArgument(0), null,
0167: option0.getArgument(0));
0168:
0169: final CLOption option1 = (CLOption) clOptions.get(1);
0170: assertEquals(option1.getDescriptor().getId(),
0171: CLOption.TEXT_ARGUMENT);
0172: assertEquals(option1.getArgument(0), "param");
0173:
0174: final CLOption option2 = (CLOption) clOptions.get(2);
0175: assertEquals(option2.getDescriptor().getId(), ALL_OPT);
0176: assertEquals(option2.getArgument(0), null);
0177: }
0178:
0179: public void testOptionalArgLong() {
0180: final CLOptionDescriptor[] options = new CLOptionDescriptor[] {
0181: ALL, TAINT };
0182:
0183: // Check that optional args work woth long options
0184: final String[] args = new String[] { "--taint", "param", "-a" };
0185:
0186: final CLArgsParser parser = new CLArgsParser(args, options);
0187:
0188: assertNull(parser.getErrorString(), parser.getErrorString());
0189:
0190: final List clOptions = parser.getArguments();
0191: final int size = clOptions.size();
0192:
0193: assertEquals("Option count", 3, size);
0194:
0195: final CLOption option0 = (CLOption) clOptions.get(0);
0196: assertEquals("Option Code: " + option0.getDescriptor().getId(),
0197: TAINT_OPT, option0.getDescriptor().getId());
0198: assertEquals("Option Arg: " + option0.getArgument(0), null,
0199: option0.getArgument(0));
0200:
0201: final CLOption option1 = (CLOption) clOptions.get(1);
0202: assertEquals(CLOption.TEXT_ARGUMENT, option1.getDescriptor()
0203: .getId());
0204: assertEquals("param", option1.getArgument(0));
0205:
0206: final CLOption option2 = (CLOption) clOptions.get(2);
0207: assertEquals(option2.getDescriptor().getId(), ALL_OPT);
0208: assertEquals(option2.getArgument(0), null);
0209: }
0210:
0211: public void testOptionalArgLongEquals() {
0212: final CLOptionDescriptor[] options = new CLOptionDescriptor[] {
0213: ALL, TAINT };
0214:
0215: // Check that optional args work woth long options
0216: final String[] args = new String[] { "--taint=param", "-a" };
0217:
0218: final CLArgsParser parser = new CLArgsParser(args, options);
0219:
0220: assertNull(parser.getErrorString(), parser.getErrorString());
0221:
0222: final List clOptions = parser.getArguments();
0223: final int size = clOptions.size();
0224:
0225: assertEquals("Option count", 2, size);
0226:
0227: final CLOption option0 = (CLOption) clOptions.get(0);
0228: assertEquals("Option Code: " + option0.getDescriptor().getId(),
0229: TAINT_OPT, option0.getDescriptor().getId());
0230: assertEquals("Option Arg: " + option0.getArgument(0), "param",
0231: option0.getArgument(0));
0232:
0233: final CLOption option2 = (CLOption) clOptions.get(1);
0234: assertEquals(option2.getDescriptor().getId(), ALL_OPT);
0235: assertEquals(option2.getArgument(0), null);
0236: }
0237:
0238: public void testShortOptArgUnenteredBeforeOtherOpt() {
0239: final CLOptionDescriptor[] options = new CLOptionDescriptor[] {
0240: ALL, TAINT };
0241:
0242: final String[] args = new String[] { "-T", "-a" };
0243:
0244: final CLArgsParser parser = new CLArgsParser(args, options);
0245:
0246: assertNull(parser.getErrorString(), parser.getErrorString());
0247:
0248: final List clOptions = parser.getArguments();
0249: final int size = clOptions.size();
0250:
0251: assertEquals("Option count", 2, size);
0252:
0253: final CLOption option0 = (CLOption) clOptions.get(0);
0254: assertEquals("Option Code: " + option0.getDescriptor().getId(),
0255: TAINT_OPT, option0.getDescriptor().getId());
0256: assertEquals("Option Arg: " + option0.getArgument(0), null,
0257: option0.getArgument(0));
0258:
0259: final CLOption option1 = (CLOption) clOptions.get(1);
0260: assertEquals(option1.getDescriptor().getId(), ALL_OPT);
0261: assertEquals(option1.getArgument(0), null);
0262: }
0263:
0264: public void testOptionalArgsWithArgShortBeforeOtherOpt() {
0265: // "-T3","-a"
0266: final CLOptionDescriptor[] options = new CLOptionDescriptor[] {
0267: ALL, TAINT };
0268:
0269: final String[] args = new String[] { "-T3", "-a" };
0270:
0271: final CLArgsParser parser = new CLArgsParser(args, options);
0272:
0273: assertNull(parser.getErrorString(), parser.getErrorString());
0274:
0275: final List clOptions = parser.getArguments();
0276: final int size = clOptions.size();
0277:
0278: assertEquals(size, 2);
0279: final CLOption option0 = (CLOption) clOptions.get(0);
0280: assertEquals(option0.getDescriptor().getId(), TAINT_OPT);
0281: assertEquals(option0.getArgument(0), "3");
0282:
0283: final CLOption option1 = (CLOption) clOptions.get(1);
0284: assertEquals(ALL_OPT, option1.getDescriptor().getId());
0285: assertEquals(null, option1.getArgument(0));
0286: }
0287:
0288: public void testOptionalArgsWithArgShortEqualsBeforeOtherOpt() {
0289: // "-T3","-a"
0290: final CLOptionDescriptor[] options = new CLOptionDescriptor[] {
0291: ALL, TAINT };
0292:
0293: final String[] args = new String[] { "-T=3", "-a" };
0294:
0295: final CLArgsParser parser = new CLArgsParser(args, options);
0296:
0297: assertNull(parser.getErrorString(), parser.getErrorString());
0298:
0299: final List clOptions = parser.getArguments();
0300: final int size = clOptions.size();
0301:
0302: assertEquals(size, 2);
0303: final CLOption option0 = (CLOption) clOptions.get(0);
0304: assertEquals(option0.getDescriptor().getId(), TAINT_OPT);
0305: assertEquals(option0.getArgument(0), "3");
0306:
0307: final CLOption option1 = (CLOption) clOptions.get(1);
0308: assertEquals(ALL_OPT, option1.getDescriptor().getId());
0309: assertEquals(null, option1.getArgument(0));
0310: }
0311:
0312: public void testOptionalArgsNoArgShortBeforeOtherOpt() {
0313: // "-T","-a"
0314: final CLOptionDescriptor[] options = new CLOptionDescriptor[] {
0315: ALL, TAINT };
0316:
0317: final String[] args = new String[] { "-T", "-a" };
0318:
0319: final CLArgsParser parser = new CLArgsParser(args, options);
0320:
0321: assertNull(parser.getErrorString(), parser.getErrorString());
0322:
0323: final List clOptions = parser.getArguments();
0324: final int size = clOptions.size();
0325:
0326: assertEquals(size, 2);
0327: final CLOption option0 = (CLOption) clOptions.get(0);
0328: assertEquals(TAINT_OPT, option0.getDescriptor().getId());
0329: assertEquals(null, option0.getArgument(0));
0330:
0331: final CLOption option1 = (CLOption) clOptions.get(1);
0332: assertEquals(ALL_OPT, option1.getDescriptor().getId());
0333: assertEquals(null, option1.getArgument(0));
0334: }
0335:
0336: public void testFullParse() {
0337: final CLOptionDescriptor[] options = new CLOptionDescriptor[] {
0338: YOU, ALL, CLEAR1, CLEAR2, CLEAR3, CLEAR5 };
0339:
0340: final CLArgsParser parser = new CLArgsParser(ARGLIST1, options);
0341:
0342: assertNull(parser.getErrorString(), parser.getErrorString());
0343:
0344: final List clOptions = parser.getArguments();
0345: final int size = clOptions.size();
0346:
0347: assertEquals(size, 8);
0348: assertEquals(((CLOption) clOptions.get(0)).getDescriptor()
0349: .getId(), YOU_OPT);
0350: assertEquals(((CLOption) clOptions.get(1)).getDescriptor()
0351: .getId(), 0);
0352: assertEquals(((CLOption) clOptions.get(2)).getDescriptor()
0353: .getId(), ALL_OPT);
0354: assertEquals(((CLOption) clOptions.get(3)).getDescriptor()
0355: .getId(), CLEAR1_OPT);
0356: assertEquals(((CLOption) clOptions.get(4)).getDescriptor()
0357: .getId(), CLEAR2_OPT);
0358: assertEquals(((CLOption) clOptions.get(5)).getDescriptor()
0359: .getId(), CLEAR3_OPT);
0360: assertEquals(((CLOption) clOptions.get(6)).getDescriptor()
0361: .getId(), CLEAR5_OPT);
0362: assertEquals(((CLOption) clOptions.get(7)).getDescriptor()
0363: .getId(), 0);
0364: }
0365:
0366: public void testDuplicateOptions() {
0367: // "-Dstupid=idiot","are","--all","--all","here"
0368: final CLOptionDescriptor[] options = new CLOptionDescriptor[] {
0369: DEFINE, ALL, CLEAR1 };
0370:
0371: final CLArgsParser parser = new CLArgsParser(ARGLIST3, options);
0372:
0373: assertNull(parser.getErrorString(), parser.getErrorString());
0374:
0375: final List clOptions = parser.getArguments();
0376: final int size = clOptions.size();
0377:
0378: assertEquals(size, 5);
0379: assertEquals(((CLOption) clOptions.get(0)).getDescriptor()
0380: .getId(), DEFINE_OPT);
0381: assertEquals(((CLOption) clOptions.get(1)).getDescriptor()
0382: .getId(), 0);
0383: assertEquals(((CLOption) clOptions.get(2)).getDescriptor()
0384: .getId(), ALL_OPT);
0385: assertEquals(((CLOption) clOptions.get(3)).getDescriptor()
0386: .getId(), ALL_OPT);
0387: assertEquals(((CLOption) clOptions.get(4)).getDescriptor()
0388: .getId(), 0);
0389: }
0390:
0391: public void testIncompatableOptions() {
0392: final CLOptionDescriptor[] options = new CLOptionDescriptor[] {
0393: DEFINE, ALL, CLEAR1, BLEE };
0394:
0395: final CLArgsParser parser = new CLArgsParser(ARGLIST4, options);
0396:
0397: assertNotNull(parser.getErrorString());
0398:
0399: final List clOptions = parser.getArguments();
0400: final int size = clOptions.size();
0401:
0402: assertEquals(size, 5);
0403: assertEquals(((CLOption) clOptions.get(0)).getDescriptor()
0404: .getId(), DEFINE_OPT);
0405: assertEquals(((CLOption) clOptions.get(1)).getDescriptor()
0406: .getId(), 0);
0407: assertEquals(((CLOption) clOptions.get(2)).getDescriptor()
0408: .getId(), ALL_OPT);
0409: assertEquals(((CLOption) clOptions.get(3)).getDescriptor()
0410: .getId(), BLEE_OPT);
0411: assertEquals(((CLOption) clOptions.get(4)).getDescriptor()
0412: .getId(), 0);
0413: }
0414:
0415: public void testSingleArg() {
0416: final CLOptionDescriptor[] options = new CLOptionDescriptor[] { FILE };
0417:
0418: final CLArgsParser parser = new CLArgsParser(ARGLIST5, options);
0419:
0420: assertNull(parser.getErrorString(), parser.getErrorString());
0421:
0422: final List clOptions = parser.getArguments();
0423: final int size = clOptions.size();
0424:
0425: assertEquals(size, 1);
0426: assertEquals(((CLOption) clOptions.get(0)).getDescriptor()
0427: .getId(), FILE_OPT);
0428: assertEquals(((CLOption) clOptions.get(0)).getArgument(),
0429: "myfile.txt");
0430: }
0431:
0432: public void testSingleArg2() {
0433: final CLOptionDescriptor[] options = new CLOptionDescriptor[] { FILE };
0434:
0435: final CLArgsParser parser = new CLArgsParser(
0436: new String[] { "-f-=,=-" } // Check
0437: // delimiters
0438: // are
0439: // allowed
0440: , options);
0441:
0442: assertNull(parser.getErrorString(), parser.getErrorString());
0443:
0444: final List clOptions = parser.getArguments();
0445: final int size = clOptions.size();
0446:
0447: assertEquals(1, size);
0448: assertEquals(FILE_OPT, ((CLOption) clOptions.get(0))
0449: .getDescriptor().getId());
0450: assertEquals("-=,=-", ((CLOption) clOptions.get(0))
0451: .getArgument());
0452: }
0453:
0454: public void testSingleArg3() {
0455: final CLOptionDescriptor[] options = new CLOptionDescriptor[] { FILE };
0456:
0457: final CLArgsParser parser = new CLArgsParser(
0458: new String[] { "--file=-=,-" } // Check
0459: // delimiters
0460: // are
0461: // allowed
0462: , options);
0463:
0464: assertNull(parser.getErrorString(), parser.getErrorString());
0465:
0466: final List clOptions = parser.getArguments();
0467: final int size = clOptions.size();
0468:
0469: assertEquals(1, size);
0470: assertEquals(FILE_OPT, ((CLOption) clOptions.get(0))
0471: .getDescriptor().getId());
0472: assertEquals("-=,-", ((CLOption) clOptions.get(0))
0473: .getArgument());
0474: }
0475:
0476: public void testSingleArg4() {
0477: final CLOptionDescriptor[] options = new CLOptionDescriptor[] { FILE };
0478:
0479: final CLArgsParser parser = new CLArgsParser(new String[] {
0480: "--file", "myfile.txt" }, options);
0481:
0482: assertNull(parser.getErrorString(), parser.getErrorString());
0483:
0484: final List clOptions = parser.getArguments();
0485: final int size = clOptions.size();
0486:
0487: assertEquals(1, size);
0488: assertEquals(FILE_OPT, ((CLOption) clOptions.get(0))
0489: .getDescriptor().getId());
0490: assertEquals("myfile.txt", ((CLOption) clOptions.get(0))
0491: .getArgument());
0492: }
0493:
0494: public void testSingleArg5() {
0495: final CLOptionDescriptor[] options = new CLOptionDescriptor[] { FILE };
0496:
0497: final CLArgsParser parser = new CLArgsParser(new String[] {
0498: "-f", "myfile.txt" }, options);
0499:
0500: assertNull(parser.getErrorString(), parser.getErrorString());
0501:
0502: final List clOptions = parser.getArguments();
0503: final int size = clOptions.size();
0504:
0505: assertEquals(1, size);
0506: assertEquals(FILE_OPT, ((CLOption) clOptions.get(0))
0507: .getDescriptor().getId());
0508: assertEquals("myfile.txt", ((CLOption) clOptions.get(0))
0509: .getArgument());
0510: }
0511:
0512: public void testSingleArg6() {
0513: final CLOptionDescriptor[] options = new CLOptionDescriptor[] { FILE };
0514:
0515: final CLArgsParser parser = new CLArgsParser(new String[] {
0516: "-f", "-=-" }, options);
0517:
0518: assertNull(parser.getErrorString(), parser.getErrorString());
0519:
0520: final List clOptions = parser.getArguments();
0521: final int size = clOptions.size();
0522:
0523: assertEquals(1, size);
0524: assertEquals(FILE_OPT, ((CLOption) clOptions.get(0))
0525: .getDescriptor().getId());
0526: assertEquals("-=-", ((CLOption) clOptions.get(0)).getArgument());
0527: }
0528:
0529: public void testSingleArg7() {
0530: final CLOptionDescriptor[] options = new CLOptionDescriptor[] { FILE };
0531:
0532: final CLArgsParser parser = new CLArgsParser(
0533: new String[] { "--file=-=-" }, options);
0534:
0535: assertNull(parser.getErrorString(), parser.getErrorString());
0536:
0537: final List clOptions = parser.getArguments();
0538: final int size = clOptions.size();
0539:
0540: assertEquals(1, size);
0541: assertEquals(FILE_OPT, ((CLOption) clOptions.get(0))
0542: .getDescriptor().getId());
0543: assertEquals("-=-", ((CLOption) clOptions.get(0)).getArgument());
0544: }
0545:
0546: public void testSingleArg8() {
0547: final CLOptionDescriptor[] options = new CLOptionDescriptor[] { FILE };
0548:
0549: final CLArgsParser parser = new CLArgsParser(new String[] {
0550: "--file", "-=-" }, options);
0551:
0552: assertNull(parser.getErrorString(), parser.getErrorString());
0553:
0554: final List clOptions = parser.getArguments();
0555: final int size = clOptions.size();
0556:
0557: assertEquals(1, size);
0558: assertEquals(FILE_OPT, ((CLOption) clOptions.get(0))
0559: .getDescriptor().getId());
0560: assertEquals("-=-", ((CLOption) clOptions.get(0)).getArgument());
0561: }
0562:
0563: public void testSingleArg9() {
0564: final CLOptionDescriptor[] options = new CLOptionDescriptor[] { FILE };
0565:
0566: final CLArgsParser parser = new CLArgsParser(new String[] {
0567: "--file", "-=-" }, options);
0568:
0569: assertNull(parser.getErrorString(), parser.getErrorString());
0570:
0571: final List clOptions = parser.getArguments();
0572: final int size = clOptions.size();
0573:
0574: assertEquals(1, size);
0575: assertEquals(FILE_OPT, ((CLOption) clOptions.get(0))
0576: .getDescriptor().getId());
0577: assertEquals("-=-", ((CLOption) clOptions.get(0)).getArgument());
0578: }
0579:
0580: public void testCombinedArgs1() {
0581: final CLOptionDescriptor[] options = new CLOptionDescriptor[] {
0582: BLEE, TAINT };
0583:
0584: final CLArgsParser parser = new CLArgsParser(new String[] {
0585: "-bT", "rest" }, options);
0586:
0587: assertNull(parser.getErrorString(), parser.getErrorString());
0588:
0589: final List clOptions = parser.getArguments();
0590: final int size = clOptions.size();
0591: assertEquals(3, size);
0592: assertEquals(BLEE_OPT, ((CLOption) clOptions.get(0))
0593: .getDescriptor().getId());
0594: assertEquals(TAINT_OPT, ((CLOption) clOptions.get(1))
0595: .getDescriptor().getId());
0596: assertEquals(0, ((CLOption) clOptions.get(2)).getDescriptor()
0597: .getId());
0598: assertEquals("rest", ((CLOption) clOptions.get(2))
0599: .getArgument());
0600: }
0601:
0602: public void testCombinedArgs2() {
0603: final CLOptionDescriptor[] options = new CLOptionDescriptor[] {
0604: BLEE, TAINT, FILE };
0605:
0606: final CLArgsParser parser = new CLArgsParser(new String[] {
0607: "-bT", "-fa" }, options);
0608:
0609: assertNull(parser.getErrorString(), parser.getErrorString());
0610:
0611: final List clOptions = parser.getArguments();
0612: final int size = clOptions.size();
0613: assertEquals(3, size);
0614: assertEquals(BLEE_OPT, ((CLOption) clOptions.get(0))
0615: .getDescriptor().getId());
0616: assertEquals(TAINT_OPT, ((CLOption) clOptions.get(1))
0617: .getDescriptor().getId());
0618: assertEquals(FILE_OPT, ((CLOption) clOptions.get(2))
0619: .getDescriptor().getId());
0620: assertEquals("a", ((CLOption) clOptions.get(2)).getArgument());
0621: }
0622:
0623: public void testCombinedArgs3() {
0624: final CLOptionDescriptor[] options = new CLOptionDescriptor[] {
0625: BLEE, TAINT, FILE };
0626:
0627: final CLArgsParser parser = new CLArgsParser(new String[] {
0628: "-bT", "--", "-fa" }// Should
0629: // not
0630: // detect
0631: // trailing
0632: // option
0633: , options);
0634:
0635: assertNull(parser.getErrorString(), parser.getErrorString());
0636:
0637: final List clOptions = parser.getArguments();
0638: final int size = clOptions.size();
0639: assertEquals(3, size);
0640: assertEquals(BLEE_OPT, ((CLOption) clOptions.get(0))
0641: .getDescriptor().getId());
0642: assertEquals(TAINT_OPT, ((CLOption) clOptions.get(1))
0643: .getDescriptor().getId());
0644: assertEquals(0, ((CLOption) clOptions.get(2)).getDescriptor()
0645: .getId());
0646: assertEquals("-fa", ((CLOption) clOptions.get(2)).getArgument());
0647: }
0648:
0649: public void testCombinedArgs4() {
0650: final CLOptionDescriptor[] options = new CLOptionDescriptor[] {
0651: BLEE, TAINT, FILE };
0652:
0653: final CLArgsParser parser = new CLArgsParser(new String[] {
0654: "-bT", "rest", "-fa" } // should
0655: // detect
0656: // trailing
0657: // option
0658: , options);
0659:
0660: assertNull(parser.getErrorString(), parser.getErrorString());
0661:
0662: final List clOptions = parser.getArguments();
0663: final int size = clOptions.size();
0664: assertEquals(4, size);
0665: assertEquals(BLEE_OPT, ((CLOption) clOptions.get(0))
0666: .getDescriptor().getId());
0667: assertEquals(TAINT_OPT, ((CLOption) clOptions.get(1))
0668: .getDescriptor().getId());
0669: assertEquals(0, ((CLOption) clOptions.get(2)).getDescriptor()
0670: .getId());
0671: assertEquals("rest", ((CLOption) clOptions.get(2))
0672: .getArgument());
0673: assertEquals(FILE_OPT, ((CLOption) clOptions.get(3))
0674: .getDescriptor().getId());
0675: assertEquals("a", ((CLOption) clOptions.get(3)).getArgument());
0676: }
0677:
0678: public void test2ArgsParse() {
0679: // "-Dstupid=idiot","are","--all","here"
0680: final CLOptionDescriptor[] options = new CLOptionDescriptor[] {
0681: DEFINE, ALL, CLEAR1, CASE_CHECK };
0682:
0683: final CLArgsParser parser = new CLArgsParser(ARGLIST2, options);
0684:
0685: assertNull(parser.getErrorString(), parser.getErrorString());
0686:
0687: final List clOptions = parser.getArguments();
0688: final int size = clOptions.size();
0689:
0690: assertEquals(size, 5);
0691: assertEquals(((CLOption) clOptions.get(0)).getDescriptor()
0692: .getId(), DEFINE_OPT);
0693: assertEquals(((CLOption) clOptions.get(1)).getDescriptor()
0694: .getId(), 0);
0695: assertEquals(((CLOption) clOptions.get(2)).getDescriptor()
0696: .getId(), ALL_OPT);
0697: assertEquals(((CLOption) clOptions.get(3)).getDescriptor()
0698: .getId(), 0);
0699: assertEquals(((CLOption) clOptions.get(4)).getDescriptor()
0700: .getId(), CASE_CHECK_OPT);
0701:
0702: final CLOption option = (CLOption) clOptions.get(0);
0703: assertEquals("stupid", option.getArgument(0));
0704: assertEquals("idiot", option.getArgument(1));
0705: }
0706:
0707: public void test2ArgsParse2() {
0708: final CLOptionDescriptor[] options = new CLOptionDescriptor[] { DEFINE };
0709:
0710: final CLArgsParser parser = new CLArgsParser(new String[] {
0711: "--define", "a-b,c=d-e,f" }, // Check
0712: // "-"
0713: // is
0714: // allowed
0715: // in
0716: // arg2
0717: options);
0718:
0719: assertNull(parser.getErrorString(), parser.getErrorString());
0720:
0721: final List clOptions = parser.getArguments();
0722: final int size = clOptions.size();
0723:
0724: assertEquals(1, size);
0725: assertEquals(DEFINE_OPT, ((CLOption) clOptions.get(0))
0726: .getDescriptor().getId());
0727:
0728: final CLOption option = (CLOption) clOptions.get(0);
0729: assertEquals("a-b,c", option.getArgument(0));
0730: assertEquals("d-e,f", option.getArgument(1));
0731: }
0732:
0733: public void test2ArgsParse3() {
0734: final CLOptionDescriptor[] options = new CLOptionDescriptor[] { DEFINE };
0735:
0736: final CLArgsParser parser = new CLArgsParser(new String[] {
0737: "-D", "A-b,c", "G-e,f" }, options);
0738:
0739: assertNull(parser.getErrorString(), parser.getErrorString());
0740:
0741: final List clOptions = parser.getArguments();
0742: final int size = clOptions.size();
0743:
0744: assertEquals(1, size);
0745: assertEquals(DEFINE_OPT, ((CLOption) clOptions.get(0))
0746: .getDescriptor().getId());
0747:
0748: final CLOption option = (CLOption) clOptions.get(0);
0749: assertEquals("A-b,c", option.getArgument(0));
0750: assertEquals("G-e,f", option.getArgument(1));
0751: }
0752:
0753: public void test2ArgsParse4() {
0754: final CLOptionDescriptor[] options = new CLOptionDescriptor[] { DEFINE_MANY };
0755:
0756: final CLArgsParser parser = new CLArgsParser(new String[] {
0757: "-Dval1=-1", "-D", "val2=-2", "--define=val-3=-3",
0758: "--define", "val4-=-4" }, options);
0759:
0760: assertNull(parser.getErrorString(), parser.getErrorString());
0761:
0762: final List clOptions = parser.getArguments();
0763: final int size = clOptions.size();
0764:
0765: assertEquals(4, size);
0766: for (int i = 0; i < size; i++) {
0767: assertEquals(DEFINE_OPT, ((CLOption) clOptions.get(i))
0768: .getDescriptor().getId());
0769: }
0770:
0771: CLOption option;
0772: option = (CLOption) clOptions.get(0);
0773: assertEquals("val1", option.getArgument(0));
0774: assertEquals("-1", option.getArgument(1));
0775:
0776: option = (CLOption) clOptions.get(1);
0777: assertEquals("val2", option.getArgument(0));
0778: assertEquals("-2", option.getArgument(1));
0779:
0780: option = (CLOption) clOptions.get(2);
0781: assertEquals("val-3", option.getArgument(0));
0782: assertEquals("-3", option.getArgument(1));
0783:
0784: option = (CLOption) clOptions.get(3);
0785: assertEquals("val4-", option.getArgument(0));
0786: assertEquals("-4", option.getArgument(1));
0787: }
0788:
0789: public void testPartParse() {
0790: final CLOptionDescriptor[] options = new CLOptionDescriptor[] { YOU };
0791:
0792: final ParserControl control = new AbstractParserControl() {
0793: public boolean isFinished(int lastOptionCode) {
0794: return (lastOptionCode == YOU_OPT);
0795: }
0796: };
0797:
0798: final CLArgsParser parser = new CLArgsParser(ARGLIST1, options,
0799: control);
0800:
0801: assertNull(parser.getErrorString(), parser.getErrorString());
0802:
0803: final List clOptions = parser.getArguments();
0804: final int size = clOptions.size();
0805:
0806: assertEquals(size, 1);
0807: assertEquals(((CLOption) clOptions.get(0)).getDescriptor()
0808: .getId(), YOU_OPT);
0809: }
0810:
0811: public void test2PartParse() {
0812: final CLOptionDescriptor[] options1 = new CLOptionDescriptor[] { YOU };
0813:
0814: final CLOptionDescriptor[] options2 = new CLOptionDescriptor[] {
0815: ALL, CLEAR1, CLEAR2, CLEAR3, CLEAR5 };
0816:
0817: final ParserControl control1 = new AbstractParserControl() {
0818: public boolean isFinished(int lastOptionCode) {
0819: return (lastOptionCode == YOU_OPT);
0820: }
0821: };
0822:
0823: final CLArgsParser parser1 = new CLArgsParser(ARGLIST1,
0824: options1, control1);
0825:
0826: assertNull(parser1.getErrorString(), parser1.getErrorString());
0827:
0828: final List clOptions1 = parser1.getArguments();
0829: final int size1 = clOptions1.size();
0830:
0831: assertEquals(size1, 1);
0832: assertEquals(((CLOption) clOptions1.get(0)).getDescriptor()
0833: .getId(), YOU_OPT);
0834:
0835: final CLArgsParser parser2 = new CLArgsParser(parser1
0836: .getUnparsedArgs(), options2);
0837:
0838: assertNull(parser2.getErrorString(), parser2.getErrorString());
0839:
0840: final List clOptions2 = parser2.getArguments();
0841: final int size2 = clOptions2.size();
0842:
0843: assertEquals(size2, 7);
0844: assertEquals(((CLOption) clOptions2.get(0)).getDescriptor()
0845: .getId(), 0);
0846: assertEquals(((CLOption) clOptions2.get(1)).getDescriptor()
0847: .getId(), ALL_OPT);
0848: assertEquals(((CLOption) clOptions2.get(2)).getDescriptor()
0849: .getId(), CLEAR1_OPT);
0850: assertEquals(((CLOption) clOptions2.get(3)).getDescriptor()
0851: .getId(), CLEAR2_OPT);
0852: assertEquals(((CLOption) clOptions2.get(4)).getDescriptor()
0853: .getId(), CLEAR3_OPT);
0854: assertEquals(((CLOption) clOptions2.get(5)).getDescriptor()
0855: .getId(), CLEAR5_OPT);
0856: assertEquals(((CLOption) clOptions2.get(6)).getDescriptor()
0857: .getId(), 0);
0858: }
0859:
0860: public void test2PartPartialParse() {
0861: final CLOptionDescriptor[] options1 = new CLOptionDescriptor[] {
0862: YOU, ALL, CLEAR1 };
0863:
0864: final CLOptionDescriptor[] options2 = new CLOptionDescriptor[] {};
0865:
0866: final ParserControl control1 = new AbstractParserControl() {
0867: public boolean isFinished(final int lastOptionCode) {
0868: return (lastOptionCode == CLEAR1_OPT);
0869: }
0870: };
0871:
0872: final CLArgsParser parser1 = new CLArgsParser(ARGLIST1,
0873: options1, control1);
0874:
0875: assertNull(parser1.getErrorString(), parser1.getErrorString());
0876:
0877: final List clOptions1 = parser1.getArguments();
0878: final int size1 = clOptions1.size();
0879:
0880: assertEquals(size1, 4);
0881: assertEquals(((CLOption) clOptions1.get(0)).getDescriptor()
0882: .getId(), YOU_OPT);
0883: assertEquals(((CLOption) clOptions1.get(1)).getDescriptor()
0884: .getId(), 0);
0885: assertEquals(((CLOption) clOptions1.get(2)).getDescriptor()
0886: .getId(), ALL_OPT);
0887: assertEquals(((CLOption) clOptions1.get(3)).getDescriptor()
0888: .getId(), CLEAR1_OPT);
0889:
0890: assertTrue(parser1.getUnparsedArgs()[0].equals("ler"));
0891:
0892: final CLArgsParser parser2 = new CLArgsParser(parser1
0893: .getUnparsedArgs(), options2);
0894:
0895: assertNull(parser2.getErrorString(), parser2.getErrorString());
0896:
0897: final List clOptions2 = parser2.getArguments();
0898: final int size2 = clOptions2.size();
0899:
0900: assertEquals(size2, 2);
0901: assertEquals(((CLOption) clOptions2.get(0)).getDescriptor()
0902: .getId(), 0);
0903: assertEquals(((CLOption) clOptions2.get(1)).getDescriptor()
0904: .getId(), 0);
0905: }
0906:
0907: public void testDuplicatesFail() {
0908: final CLOptionDescriptor[] options = new CLOptionDescriptor[] {
0909: YOU, ALL, CLEAR1, CLEAR2, CLEAR3, CLEAR5 };
0910:
0911: final CLArgsParser parser = new CLArgsParser(ARGLIST1, options);
0912:
0913: assertNull(parser.getErrorString(), parser.getErrorString());
0914: }
0915:
0916: public void testIncomplete2Args() {
0917: // "-Dstupid="
0918: final CLOptionDescriptor[] options = new CLOptionDescriptor[] { DEFINE };
0919:
0920: final CLArgsParser parser = new CLArgsParser(
0921: new String[] { "-Dstupid=" }, options);
0922:
0923: assertNull(parser.getErrorString(), parser.getErrorString());
0924:
0925: final List clOptions = parser.getArguments();
0926: final int size = clOptions.size();
0927:
0928: assertEquals(size, 1);
0929: final CLOption option = (CLOption) clOptions.get(0);
0930: assertEquals(option.getDescriptor().getId(), DEFINE_OPT);
0931: assertEquals(option.getArgument(0), "stupid");
0932: assertEquals(option.getArgument(1), "");
0933: }
0934:
0935: public void testIncomplete2ArgsMixed() {
0936: // "-Dstupid=","-c"
0937: final CLOptionDescriptor[] options = new CLOptionDescriptor[] {
0938: DEFINE, CLEAR1 };
0939:
0940: final String[] args = new String[] { "-Dstupid=", "-c" };
0941:
0942: final CLArgsParser parser = new CLArgsParser(args, options);
0943:
0944: assertNull(parser.getErrorString(), parser.getErrorString());
0945:
0946: final List clOptions = parser.getArguments();
0947: final int size = clOptions.size();
0948:
0949: assertEquals(size, 2);
0950: assertEquals(((CLOption) clOptions.get(1)).getDescriptor()
0951: .getId(), CLEAR1_OPT);
0952: final CLOption option = (CLOption) clOptions.get(0);
0953: assertEquals(option.getDescriptor().getId(), DEFINE_OPT);
0954: assertEquals(option.getArgument(0), "stupid");
0955: assertEquals(option.getArgument(1), "");
0956: }
0957:
0958: public void testIncomplete2ArgsMixedNoEq() {
0959: // "-Dstupid","-c"
0960: final CLOptionDescriptor[] options = new CLOptionDescriptor[] {
0961: DEFINE, CLEAR1 };
0962:
0963: final String[] args = new String[] { "-DStupid", "-c" };
0964:
0965: final CLArgsParser parser = new CLArgsParser(args, options);
0966:
0967: assertNull(parser.getErrorString(), parser.getErrorString());
0968:
0969: final List clOptions = parser.getArguments();
0970: final int size = clOptions.size();
0971:
0972: assertEquals(size, 2);
0973: assertEquals(((CLOption) clOptions.get(1)).getDescriptor()
0974: .getId(), CLEAR1_OPT);
0975: final CLOption option = (CLOption) clOptions.get(0);
0976: assertEquals(option.getDescriptor().getId(), DEFINE_OPT);
0977: assertEquals(option.getArgument(0), "Stupid");
0978: assertEquals(option.getArgument(1), "");
0979: }
0980:
0981: /**
0982: * Test the getArgumentById and getArgumentByName lookup methods.
0983: */
0984: public void testArgumentLookup() {
0985: final String[] args = { "-f", "testarg" };
0986: final CLOptionDescriptor[] options = { FILE };
0987: final CLArgsParser parser = new CLArgsParser(args, options);
0988:
0989: assertNull(parser.getErrorString(), parser.getErrorString());
0990:
0991: CLOption optionById = parser.getArgumentById(FILE_OPT);
0992: assertNotNull(optionById);
0993: assertEquals(FILE_OPT, optionById.getDescriptor().getId());
0994: assertEquals("testarg", optionById.getArgument());
0995:
0996: CLOption optionByName = parser
0997: .getArgumentByName(FILE.getName());
0998: assertNotNull(optionByName);
0999: assertEquals(FILE_OPT, optionByName.getDescriptor().getId());
1000: assertEquals("testarg", optionByName.getArgument());
1001: }
1002:
1003: /**
1004: * Test that you can have null long forms.
1005: */
1006: public void testNullLongForm() {
1007: final CLOptionDescriptor test = new CLOptionDescriptor(null,
1008: CLOptionDescriptor.ARGUMENT_DISALLOWED, 'n',
1009: "test null long form");
1010:
1011: final String[] args = { "-n", "testarg" };
1012: final CLOptionDescriptor[] options = { test };
1013: final CLArgsParser parser = new CLArgsParser(args, options);
1014:
1015: assertNull(parser.getErrorString(), parser.getErrorString());
1016:
1017: final CLOption optionByID = parser.getArgumentById('n');
1018: assertNotNull(optionByID);
1019: assertEquals('n', optionByID.getDescriptor().getId());
1020:
1021: final CLOption optionByName = parser.getArgumentByName(FILE
1022: .getName());
1023: assertNull("Looking for non-existent option by name",
1024: optionByName);
1025: }
1026:
1027: /**
1028: * Test that you can have null descriptions.
1029: */
1030: public void testNullDescription() {
1031: final CLOptionDescriptor test = new CLOptionDescriptor(
1032: "nulltest", CLOptionDescriptor.ARGUMENT_DISALLOWED,
1033: 'n', null);
1034:
1035: final String[] args = { "-n", "testarg" };
1036: final CLOptionDescriptor[] options = { test };
1037: final CLArgsParser parser = new CLArgsParser(args, options);
1038:
1039: assertNull(parser.getErrorString(), parser.getErrorString());
1040:
1041: final CLOption optionByID = parser.getArgumentById('n');
1042: assertNotNull(optionByID);
1043: assertEquals('n', optionByID.getDescriptor().getId());
1044:
1045: final StringBuffer sb = CLUtil.describeOptions(options);
1046: final String lineSeparator = System
1047: .getProperty("line.separator");
1048: assertEquals("Testing display of null description",
1049: "\t-n, --nulltest" + lineSeparator, sb.toString());
1050: }
1051:
1052: public void testCombinations() throws Exception {
1053: check(new String[] {}, "");
1054: check(new String[] { "--none", "-0" }, "-0 -0"); // Canonical form
1055: check(new String[] { "--one=a", "--one", "A", "-1b", "-1=c",
1056: "-1", "d" }, "-1=[a] -1=[A] -1=[b] -1=[c] -1=[d]");
1057: check(new String[] { "-2n=v", "-2", "N=V" },
1058: "-2=[n, v] -2=[N, V]");
1059: check(new String[] { "--two=n=v", "--two", "N=V" },
1060: "-2=[n, v] -2=[N, V]");
1061: // Test optional arguments
1062: check(new String[] { "-?", "A", // Separate argument
1063: "-?=B", "-?C", "-?" }, "-? [A] -?=[B] -?=[C] -?");
1064: check(new String[] { "--optional=A", // OK
1065: "--optional", "B", // should treat B as separate
1066: "--optional" // Should have no arg
1067: }, "-?=[A] -? [B] -?");
1068: }
1069:
1070: private void check(String args[], String canon) {
1071: final CLArgsParser parser = new CLArgsParser(args, OPTIONS);
1072:
1073: assertNull(parser.getErrorString(), parser.getErrorString());
1074:
1075: final List clOptions = parser.getArguments();
1076: final int size = clOptions.size();
1077: StringBuffer sb = new StringBuffer();
1078: for (int i = 0; i < size; i++) {
1079: if (i > 0)
1080: sb.append(" ");
1081: sb.append(((CLOption) clOptions.get(i)).toShortString());
1082: }
1083: assertEquals("Canonical form (" + size + ")", canon, sb
1084: .toString());
1085: }
1086: /*
1087: * TODO add tests to check for: - name clash - long option abbreviations
1088: * (match shortest unique abbreviation)
1089: */
1090:
1091: }
|