KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE file.
7  *
8  * $Id: BugsTest.java,v 1.10 2002/10/24 23:17:49 jkeyes Exp $
9  */

10
11 package org.apache.commons.cli;
12
13 import junit.framework.Test;
14 import junit.framework.TestCase;
15 import junit.framework.TestSuite;
16
17 public class BugsTest extends TestCase
18 {
19     /** CommandLine instance */
20     private CommandLine _cmdline = null;
21     private Option _option = null;
22
23     public static Test suite() {
24         return new TestSuite( BugsTest.class );
25     }
26
27     public BugsTest( String JavaDoc name )
28     {
29         super( name );
30     }
31
32     public void setUp()
33     {
34     }
35
36     public void tearDown()
37     {
38     }
39
40     public void test11457() {
41         Options options = new Options();
42         options.addOption( OptionBuilder.withLongOpt( "verbose" )
43                            .create() );
44         String JavaDoc[] args = new String JavaDoc[] { "--verbose" };
45
46         CommandLineParser parser = new PosixParser();
47
48         try {
49             CommandLine cmd = parser.parse( options, args );
50             assertTrue( cmd.hasOption( "verbose" ) );
51         }
52         catch( ParseException exp ) {
53             exp.printStackTrace();
54             fail( "Unexpected Exception: " + exp.getMessage() );
55         }
56     }
57
58     public void test11458()
59     {
60         Options options = new Options();
61         options.addOption( OptionBuilder.withValueSeparator( '=' )
62                            .hasArgs()
63                            .create( 'D' ) );
64         options.addOption( OptionBuilder.withValueSeparator( ':' )
65                            .hasArgs()
66                            .create( 'p' ) );
67         String JavaDoc[] args = new String JavaDoc[] { "-DJAVA_HOME=/opt/java" ,
68         "-pfile1:file2:file3" };
69
70         CommandLineParser parser = new PosixParser();
71
72         try {
73             CommandLine cmd = parser.parse( options, args );
74
75             String JavaDoc[] values = cmd.getOptionValues( 'D' );
76
77             assertEquals( values[0], "JAVA_HOME" );
78             assertEquals( values[1], "/opt/java" );
79
80             values = cmd.getOptionValues( 'p' );
81
82             assertEquals( values[0], "file1" );
83             assertEquals( values[1], "file2" );
84             assertEquals( values[2], "file3" );
85
86             java.util.Iterator JavaDoc iter = cmd.iterator();
87             while( iter.hasNext() ) {
88                 Option opt = (Option)iter.next();
89                 switch( opt.getId() ) {
90                     case 'D':
91                         assertEquals( opt.getValue( 0 ), "JAVA_HOME" );
92                         assertEquals( opt.getValue( 1 ), "/opt/java" );
93                         break;
94                     case 'p':
95                         assertEquals( opt.getValue( 0 ), "file1" );
96                         assertEquals( opt.getValue( 1 ), "file2" );
97                         assertEquals( opt.getValue( 2 ), "file3" );
98                         break;
99                     default:
100                         fail( "-D option not found" );
101                 }
102             }
103         }
104         catch( ParseException exp ) {
105             fail( "Unexpected Exception:\nMessage:" + exp.getMessage()
106                   + "Type: " + exp.getClass().getName() );
107         }
108     }
109
110     public void test11680()
111     {
112         Options options = new Options();
113         options.addOption("f", true, "foobar");
114     options.addOption("m", true, "missing");
115         String JavaDoc[] args = new String JavaDoc[] { "-f" , "foo" };
116
117         CommandLineParser parser = new PosixParser();
118
119         try {
120             CommandLine cmd = parser.parse( options, args );
121
122             try {
123                 cmd.getOptionValue( "f", "default f");
124                 cmd.getOptionValue( "m", "default m");
125             }
126             catch( NullPointerException JavaDoc exp ) {
127                 fail( "NullPointer caught: " + exp.getMessage() );
128             }
129         }
130         catch( ParseException exp ) {
131             fail( "Unexpected Exception: " + exp.getMessage() );
132         }
133     }
134
135     public void test11456()
136     {
137         // Posix
138
Options options = new Options();
139         options.addOption( OptionBuilder.hasOptionalArg()
140                            .create( 'a' ) );
141         options.addOption( OptionBuilder.hasArg()
142                            .create( 'b' ) );
143         String JavaDoc[] args = new String JavaDoc[] { "-a", "-bvalue" };
144
145         CommandLineParser parser = new PosixParser();
146
147         try {
148             CommandLine cmd = parser.parse( options, args );
149             assertEquals( cmd.getOptionValue( 'b' ), "value" );
150         }
151         catch( ParseException exp ) {
152             fail( "Unexpected Exception: " + exp.getMessage() );
153         }
154
155         // GNU
156
options = new Options();
157         options.addOption( OptionBuilder.hasOptionalArg()
158                            .create( 'a' ) );
159         options.addOption( OptionBuilder.hasArg()
160                            .create( 'b' ) );
161         args = new String JavaDoc[] { "-a", "-b", "value" };
162
163         parser = new GnuParser();
164
165         try {
166             CommandLine cmd = parser.parse( options, args );
167             assertEquals( cmd.getOptionValue( 'b' ), "value" );
168         }
169         catch( ParseException exp ) {
170             fail( "Unexpected Exception: " + exp.getMessage() );
171         }
172
173     }
174
175     public void test12210() {
176         // create the main options object which will handle the first parameter
177
Options mainOptions = new Options();
178         // There can be 2 main exclusive options: -exec|-rep
179

180         // Therefore, place them in an option group
181

182         String JavaDoc[] argv = new String JavaDoc[] { "-exec", "-exec_opt1", "-exec_opt2" };
183         OptionGroup grp = new OptionGroup();
184
185         grp.addOption(new Option("exec",false,"description for this option"));
186
187         grp.addOption(new Option("rep",false,"description for this option"));
188
189         mainOptions.addOptionGroup(grp);
190
191         // for the exec option, there are 2 options...
192
Options execOptions = new Options();
193         execOptions.addOption("exec_opt1",false," desc");
194         execOptions.addOption("exec_opt2",false," desc");
195
196         // similarly, for rep there are 2 options...
197
Options repOptions = new Options();
198         repOptions.addOption("repopto",false,"desc");
199         repOptions.addOption("repoptt",false,"desc");
200
201         // create the parser
202
GnuParser parser = new GnuParser();
203
204         // finally, parse the arguments:
205

206         // first parse the main options to see what the user has specified
207
// We set stopAtNonOption to true so it does not touch the remaining
208
// options
209
try {
210             CommandLine cmd = parser.parse(mainOptions,argv,true);
211             // get the remaining options...
212
argv = cmd.getArgs();
213
214             if(cmd.hasOption("exec")){
215                 cmd = parser.parse(execOptions,argv,false);
216                 // process the exec_op1 and exec_opt2...
217
assertTrue( cmd.hasOption("exec_opt1") );
218                 assertTrue( cmd.hasOption("exec_opt2") );
219             }
220             else if(cmd.hasOption("rep")){
221                 cmd = parser.parse(repOptions,argv,false);
222                 // process the rep_op1 and rep_opt2...
223
}
224             else {
225                 fail( "exec option not found" );
226             }
227         }
228         catch( ParseException exp ) {
229             fail( "Unexpected exception: " + exp.getMessage() );
230         }
231     }
232
233     public void test13425() {
234         Options options = new Options();
235         Option oldpass = OptionBuilder.withLongOpt( "old-password" )
236             .withDescription( "Use this option to specify the old password" )
237             .hasArg()
238             .create( 'o' );
239         Option newpass = OptionBuilder.withLongOpt( "new-password" )
240             .withDescription( "Use this option to specify the new password" )
241             .hasArg()
242             .create( 'n' );
243
244         String JavaDoc[] args = {
245             "-o",
246             "-n",
247             "newpassword"
248         };
249
250         options.addOption( oldpass );
251         options.addOption( newpass );
252
253         Parser parser = new PosixParser();
254
255         try {
256             CommandLine line = parser.parse( options, args );
257         }
258         // catch the exception and leave the method
259
catch( Exception JavaDoc exp ) {
260             assertTrue( exp != null );
261             return;
262         }
263         fail( "MissingArgumentException not caught." );
264     }
265
266     public void test13666() {
267         Options options = new Options();
268         Option dir = OptionBuilder.withDescription( "dir" )
269                                        .hasArg()
270                                        .create( 'd' );
271         options.addOption( dir );
272         try {
273             HelpFormatter formatter = new HelpFormatter();
274             formatter.printHelp( "dir", options );
275         }
276         catch( Exception JavaDoc exp ) {
277             fail( "Unexpected Exception: " + exp.getMessage() );
278         }
279     }
280
281     public void test13935() {
282         OptionGroup directions = new OptionGroup();
283
284         Option left = new Option( "l", "left", false, "go left" );
285         Option right = new Option( "r", "right", false, "go right" );
286         Option straight = new Option( "s", "straight", false, "go straight" );
287         Option forward = new Option( "f", "forward", false, "go forward" );
288         forward.setRequired( true );
289
290         directions.addOption( left );
291         directions.addOption( right );
292         directions.setRequired( true );
293
294         Options opts = new Options();
295         opts.addOptionGroup( directions );
296         opts.addOption( straight );
297
298         CommandLineParser parser = new PosixParser();
299         boolean exception = false;
300
301         String JavaDoc[] args = new String JavaDoc[] { };
302         try {
303             CommandLine line = parser.parse( opts, args );
304         }
305         catch( ParseException exp ) {
306             exception = true;
307         }
308
309         if( !exception ) {
310             fail( "Expected exception not caught.");
311         }
312
313         exception = false;
314
315         args = new String JavaDoc[] { "-s" };
316         try {
317             CommandLine line = parser.parse( opts, args );
318         }
319         catch( ParseException exp ) {
320             exception = true;
321         }
322
323         if( !exception ) {
324             fail( "Expected exception not caught.");
325         }
326
327         exception = false;
328
329         args = new String JavaDoc[] { "-s", "-l" };
330         try {
331             CommandLine line = parser.parse( opts, args );
332         }
333         catch( ParseException exp ) {
334             fail( "Unexpected exception: " + exp.getMessage() );
335         }
336
337         opts.addOption( forward );
338         args = new String JavaDoc[] { "-s", "-l", "-f" };
339         try {
340             CommandLine line = parser.parse( opts, args );
341         }
342         catch( ParseException exp ) {
343             fail( "Unexpected exception: " + exp.getMessage() );
344         }
345     }
346 }
347
Popular Tags