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.
026: * </p>
027: *
028: * <p>
029: * The following are the applications that are tested:
030: * <ul>
031: * <li>Ant</li>
032: * </ul>
033: * </p>
034: *
035: * @author John Keyes (john at integralsource.com)
036: */
037: public class ApplicationTest extends TestCase {
038:
039: public static Test suite() {
040: return new TestSuite(ApplicationTest.class);
041: }
042:
043: public ApplicationTest(String name) {
044: super (name);
045: }
046:
047: /**
048: *
049: */
050: public void testLs() {
051: // create the command line parser
052: CommandLineParser parser = new PosixParser();
053: Options options = new Options();
054: options.addOption("a", "all", false,
055: "do not hide entries starting with .");
056: options.addOption("A", "almost-all", false,
057: "do not list implied . and ..");
058: options.addOption("b", "escape", false,
059: "print octal escapes for nongraphic characters");
060: options.addOption(OptionBuilder.withLongOpt("block-size")
061: .withDescription("use SIZE-byte blocks")
062: .withValueSeparator('=').hasArg().create());
063: options.addOption("B", "ignore-backups", false,
064: "do not list implied entried ending with ~");
065: options
066: .addOption(
067: "c",
068: false,
069: "with -lt: sort by, and show, ctime (time of last modification of file status information) with -l:show ctime and sort by name otherwise: sort by ctime");
070: options.addOption("C", false, "list entries by columns");
071:
072: String[] args = new String[] { "--block-size=10" };
073:
074: try {
075: CommandLine line = parser.parse(options, args);
076: assertTrue(line.hasOption("block-size"));
077: assertEquals(line.getOptionValue("block-size"), "10");
078: } catch (ParseException exp) {
079: fail("Unexpected exception:" + exp.getMessage());
080: }
081: }
082:
083: /**
084: * Ant test
085: */
086: public void testAnt() {
087: // use the GNU parser
088: CommandLineParser parser = new GnuParser();
089: Options options = new Options();
090: options.addOption("help", false, "print this message");
091: options.addOption("projecthelp", false,
092: "print project help information");
093: options.addOption("version", false,
094: "print the version information and exit");
095: options.addOption("quiet", false, "be extra quiet");
096: options.addOption("verbose", false, "be extra verbose");
097: options.addOption("debug", false, "print debug information");
098: options.addOption("version", false,
099: "produce logging information without adornments");
100: options.addOption("logfile", true, "use given file for log");
101: options.addOption("logger", true,
102: "the class which is to perform the logging");
103: options.addOption("listener", true,
104: "add an instance of a class as a project listener");
105: options.addOption("buildfile", true, "use given buildfile");
106: options.addOption(OptionBuilder.withDescription(
107: "use value for given property").hasArgs()
108: .withValueSeparator().create('D'));
109: //, null, true, , false, true );
110: options
111: .addOption("find", true,
112: "search for buildfile towards the root of the filesystem and use it");
113:
114: String[] args = new String[] { "-buildfile", "mybuild.xml",
115: "-Dproperty=value", "-Dproperty1=value1",
116: "-projecthelp" };
117:
118: try {
119: CommandLine line = parser.parse(options, args);
120:
121: // check multiple values
122: String[] opts = line.getOptionValues("D");
123: assertEquals("property", opts[0]);
124: assertEquals("value", opts[1]);
125: assertEquals("property1", opts[2]);
126: assertEquals("value1", opts[3]);
127:
128: // check single value
129: assertEquals(line.getOptionValue("buildfile"),
130: "mybuild.xml");
131:
132: // check option
133: assertTrue(line.hasOption("projecthelp"));
134: } catch (ParseException exp) {
135: fail("Unexpected exception:" + exp.getMessage());
136: }
137:
138: }
139:
140: }
|