001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.commons.cli;
017:
018: import junit.framework.Test;
019: import junit.framework.TestCase;
020: import junit.framework.TestSuite;
021:
022: /**
023: * <p>
024: * This is a collection of tests that test real world
025: * applications command lines focusing on options with
026: * long and short names.
027: * </p>
028: */
029: public class LongOptionWithShort extends TestCase {
030: public LongOptionWithShort(String name) {
031: super (name);
032: }
033:
034: public static Test suite() {
035: return new TestSuite(LongOptionWithShort.class);
036: }
037:
038: /**
039: *
040: */
041: public void testLongOptionWithShort() {
042: Option help = new Option("h", "help", false,
043: "print this message");
044: Option version = new Option("v", "version", false,
045: "print version information");
046: Option newRun = new Option("n", "new", false,
047: "Create NLT cache entries only for new items");
048: Option trackerRun = new Option("t", "tracker", false,
049: "Create NLT cache entries only for tracker items");
050:
051: Option timeLimit = OptionBuilder.withLongOpt("limit").hasArg()
052: .withValueSeparator().withDescription(
053: "Set time limit for execution, in mintues")
054: .create("l");
055:
056: Option age = OptionBuilder
057: .withLongOpt("age")
058: .hasArg()
059: .withValueSeparator()
060: .withDescription(
061: "Age (in days) of cache item before being recomputed")
062: .create("a");
063:
064: Option server = OptionBuilder.withLongOpt("server").hasArg()
065: .withValueSeparator().withDescription(
066: "The NLT server address").create("s");
067:
068: Option numResults = OptionBuilder.withLongOpt("results")
069: .hasArg().withValueSeparator().withDescription(
070: "Number of results per item").create("r");
071:
072: Option configFile = OptionBuilder.withLongOpt("file").hasArg()
073: .withValueSeparator().withDescription(
074: "Use the specified configuration file")
075: .create();
076:
077: Options options = new Options();
078: options.addOption(help);
079: options.addOption(version);
080: options.addOption(newRun);
081: options.addOption(trackerRun);
082: options.addOption(timeLimit);
083: options.addOption(age);
084: options.addOption(server);
085: options.addOption(numResults);
086: options.addOption(configFile);
087:
088: // create the command line parser
089: CommandLineParser parser = new PosixParser();
090:
091: String[] args = new String[] { "-v", "-l", "10", "-age", "5",
092: "-file", "filename" };
093:
094: try {
095: CommandLine line = parser.parse(options, args);
096: assertTrue(line.hasOption("v"));
097: assertEquals(line.getOptionValue("l"), "10");
098: assertEquals(line.getOptionValue("limit"), "10");
099: assertEquals(line.getOptionValue("a"), "5");
100: assertEquals(line.getOptionValue("age"), "5");
101: assertEquals(line.getOptionValue("file"), "filename");
102: } catch (ParseException exp) {
103: fail("Unexpected exception:" + exp.getMessage());
104: }
105: }
106: }
|