KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > cli > ApplicationTest


1 package org.apache.commons.cli;
2
3 import junit.framework.Test;
4 import junit.framework.TestCase;
5 import junit.framework.TestSuite;
6
7 /**
8  * <p>
9  * This is a collection of tests that test real world
10  * applications command lines.
11  * </p>
12  *
13  * <p>
14  * The following are the applications that are tested:
15  * <ul>
16  * <li>Ant</li>
17  * </ul>
18  * </p>
19  *
20  * @author John Keyes (john at integralsource.com)
21  */

22 public class ApplicationTest extends TestCase {
23
24     public static Test suite() {
25         return new TestSuite(ApplicationTest.class);
26     }
27
28     public ApplicationTest(String JavaDoc name)
29     {
30         super(name);
31     }
32     
33     /**
34      *
35      */

36     public void testLs() {
37         // create the command line parser
38
CommandLineParser parser = new PosixParser();
39         Options options = new Options();
40         options.addOption( "a", "all", false, "do not hide entries starting with ." );
41         options.addOption( "A", "almost-all", false, "do not list implied . and .." );
42         options.addOption( "b", "escape", false, "print octal escapes for nongraphic characters" );
43         options.addOption( OptionBuilder.withLongOpt( "block-size" )
44                                         .withDescription( "use SIZE-byte blocks" )
45                                         .withValueSeparator( '=' )
46                                         .hasArg()
47                                         .create() );
48         options.addOption( "B", "ignore-backups", false, "do not list implied entried ending with ~");
49         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" );
50         options.addOption( "C", false, "list entries by columns" );
51
52         String JavaDoc[] args = new String JavaDoc[]{ "--block-size=10" };
53
54         try {
55             CommandLine line = parser.parse( options, args );
56             assertTrue( line.hasOption( "block-size" ) );
57             assertEquals( line.getOptionValue( "block-size" ), "10" );
58         }
59         catch( ParseException exp ) {
60             fail( "Unexpected exception:" + exp.getMessage() );
61         }
62     }
63
64     /**
65      * Ant test
66      */

67     public void testAnt() {
68         // use the GNU parser
69
CommandLineParser parser = new GnuParser( );
70         Options options = new Options();
71         options.addOption( "help", false, "print this message" );
72         options.addOption( "projecthelp", false, "print project help information" );
73         options.addOption( "version", false, "print the version information and exit" );
74         options.addOption( "quiet", false, "be extra quiet" );
75         options.addOption( "verbose", false, "be extra verbose" );
76         options.addOption( "debug", false, "print debug information" );
77         options.addOption( "version", false, "produce logging information without adornments" );
78         options.addOption( "logfile", true, "use given file for log" );
79         options.addOption( "logger", true, "the class which is to perform the logging" );
80         options.addOption( "listener", true, "add an instance of a class as a project listener" );
81         options.addOption( "buildfile", true, "use given buildfile" );
82         options.addOption( OptionBuilder.withDescription( "use value for given property" )
83                                         .hasArgs()
84                                         .withValueSeparator()
85                                         .create( 'D' ) );
86                            //, null, true, , false, true );
87
options.addOption( "find", true, "search for buildfile towards the root of the filesystem and use it" );
88
89         String JavaDoc[] args = new String JavaDoc[]{ "-buildfile", "mybuild.xml",
90             "-Dproperty=value", "-Dproperty1=value1",
91             "-projecthelp" };
92
93         try {
94             CommandLine line = parser.parse( options, args );
95
96             // check multiple values
97
String JavaDoc[] opts = line.getOptionValues( "D" );
98             assertEquals( "property", opts[0] );
99             assertEquals( "value", opts[1] );
100             assertEquals( "property1", opts[2] );
101             assertEquals( "value1", opts[3] );
102
103             // check single value
104
assertEquals( line.getOptionValue( "buildfile"), "mybuild.xml" );
105
106             // check option
107
assertTrue( line.hasOption( "projecthelp") );
108         }
109         catch( ParseException exp ) {
110             fail( "Unexpected exception:" + exp.getMessage() );
111         }
112
113     }
114
115 }
Popular Tags