1   /***
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.cli;
18  
19  import junit.framework.Test;
20  import junit.framework.TestCase;
21  import junit.framework.TestSuite;
22  
23  /***
24   * <p>
25   * This is a collection of tests that test real world
26   * applications command lines.
27   * </p>
28   * 
29   * <p>
30   * The following are the applications that are tested:
31   * <ul>
32   * <li>Ant</li>
33   * </ul>
34   * </p>
35   *
36   * @author John Keyes (john at integralsource.com)
37   */
38  public class ApplicationTest extends TestCase {
39  
40      public static Test suite() { 
41          return new TestSuite(ApplicationTest.class); 
42      }
43  
44      public ApplicationTest(String name)
45      {
46          super(name);
47      }
48      
49      /***
50       *	
51       */
52      public void testLs() {
53          // create the command line parser
54          CommandLineParser parser = new PosixParser();
55          Options options = new Options();
56          options.addOption( "a", "all", false, "do not hide entries starting with ." );
57          options.addOption( "A", "almost-all", false, "do not list implied . and .." );
58          options.addOption( "b", "escape", false, "print octal escapes for nongraphic characters" );
59          options.addOption( OptionBuilder.withLongOpt( "block-size" )
60                                          .withDescription( "use SIZE-byte blocks" )
61                                          .withValueSeparator( '=' )
62                                          .hasArg()
63                                          .create() );
64          options.addOption( "B", "ignore-backups", false, "do not list implied entried ending with ~");
65          options.addOption( "c", false, "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" );
66          options.addOption( "C", false, "list entries by columns" );
67  
68          String[] args = new String[]{ "--block-size=10" };
69  
70          try {
71              CommandLine line = parser.parse( options, args );
72              assertTrue( line.hasOption( "block-size" ) );
73              assertEquals( line.getOptionValue( "block-size" ), "10" );
74          }
75          catch( ParseException exp ) {
76              fail( "Unexpected exception:" + exp.getMessage() );
77          }
78      }
79  
80      /***
81       * Ant test
82       */
83      public void testAnt() {
84          // use the GNU parser
85          CommandLineParser parser = new GnuParser( );
86          Options options = new Options();
87          options.addOption( "help", false, "print this message" );
88          options.addOption( "projecthelp", false, "print project help information" );
89          options.addOption( "version", false, "print the version information and exit" );
90          options.addOption( "quiet", false, "be extra quiet" );
91          options.addOption( "verbose", false, "be extra verbose" );
92          options.addOption( "debug", false, "print debug information" );
93          options.addOption( "version", false, "produce logging information without adornments" );
94          options.addOption( "logfile", true, "use given file for log" );
95          options.addOption( "logger", true, "the class which is to perform the logging" );
96          options.addOption( "listener", true, "add an instance of a class as a project listener" );
97          options.addOption( "buildfile", true, "use given buildfile" );
98          options.addOption( OptionBuilder.withDescription( "use value for given property" )
99                                          .hasArgs()
100                                         .withValueSeparator()
101                                         .create( 'D' ) );
102                            //, null, true, , false, true );
103         options.addOption( "find", true, "search for buildfile towards the root of the filesystem and use it" );
104 
105         String[] args = new String[]{ "-buildfile", "mybuild.xml",
106             "-Dproperty=value", "-Dproperty1=value1",
107             "-projecthelp" };
108 
109         try {
110             CommandLine line = parser.parse( options, args );
111 
112             // check multiple values
113             String[] opts = line.getOptionValues( "D" );
114             assertEquals( "property", opts[0] );
115             assertEquals( "value", opts[1] );
116             assertEquals( "property1", opts[2] );
117             assertEquals( "value1", opts[3] );
118 
119             // check single value
120             assertEquals( line.getOptionValue( "buildfile"), "mybuild.xml" );
121 
122             // check option
123             assertTrue( line.hasOption( "projecthelp") );
124         }
125         catch( ParseException exp ) {
126             fail( "Unexpected exception:" + exp.getMessage() );
127         }
128 
129     }
130 
131 }