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 java.io.ByteArrayOutputStream;
20 import java.io.PrintStream;
21 import java.io.PrintWriter;
22 import java.io.StringWriter;
23 import java.util.Properties;
24
25 import junit.framework.Test;
26 import junit.framework.TestCase;
27 import junit.framework.TestSuite;
28
29 public class BugsTest extends TestCase
30 {
31 /*** CommandLine instance */
32 private CommandLine _cmdline = null;
33 private Option _option = null;
34
35 public static Test suite() {
36 return new TestSuite( BugsTest.class );
37 }
38
39 public BugsTest( String name )
40 {
41 super( name );
42 }
43
44 public void setUp()
45 {
46 }
47
48 public void tearDown()
49 {
50 }
51
52 public void test11457() {
53 Options options = new Options();
54 options.addOption( OptionBuilder.withLongOpt( "verbose" )
55 .create() );
56 String[] args = new String[] { "--verbose" };
57
58 CommandLineParser parser = new PosixParser();
59
60 try {
61 CommandLine cmd = parser.parse( options, args );
62 assertTrue( cmd.hasOption( "verbose" ) );
63 }
64 catch( ParseException exp ) {
65 exp.printStackTrace();
66 fail( "Unexpected Exception: " + exp.getMessage() );
67 }
68 }
69
70 public void test11458()
71 {
72 Options options = new Options();
73 options.addOption( OptionBuilder.withValueSeparator( '=' )
74 .hasArgs()
75 .create( 'D' ) );
76 options.addOption( OptionBuilder.withValueSeparator( ':' )
77 .hasArgs()
78 .create( 'p' ) );
79 String[] args = new String[] { "-DJAVA_HOME=/opt/java" ,
80 "-pfile1:file2:file3" };
81
82 CommandLineParser parser = new PosixParser();
83
84 try {
85 CommandLine cmd = parser.parse( options, args );
86
87 String[] values = cmd.getOptionValues( 'D' );
88
89 assertEquals( values[0], "JAVA_HOME" );
90 assertEquals( values[1], "/opt/java" );
91
92 values = cmd.getOptionValues( 'p' );
93
94 assertEquals( values[0], "file1" );
95 assertEquals( values[1], "file2" );
96 assertEquals( values[2], "file3" );
97
98 java.util.Iterator iter = cmd.iterator();
99 while( iter.hasNext() ) {
100 Option opt = (Option)iter.next();
101 switch( opt.getId() ) {
102 case 'D':
103 assertEquals( opt.getValue( 0 ), "JAVA_HOME" );
104 assertEquals( opt.getValue( 1 ), "/opt/java" );
105 break;
106 case 'p':
107 assertEquals( opt.getValue( 0 ), "file1" );
108 assertEquals( opt.getValue( 1 ), "file2" );
109 assertEquals( opt.getValue( 2 ), "file3" );
110 break;
111 default:
112 fail( "-D option not found" );
113 }
114 }
115 }
116 catch( ParseException exp ) {
117 fail( "Unexpected Exception:\nMessage:" + exp.getMessage()
118 + "Type: " + exp.getClass().getName() );
119 }
120 }
121
122 public void test11680()
123 {
124 Options options = new Options();
125 options.addOption("f", true, "foobar");
126 options.addOption("m", true, "missing");
127 String[] args = new String[] { "-f" , "foo" };
128
129 CommandLineParser parser = new PosixParser();
130
131 try {
132 CommandLine cmd = parser.parse( options, args );
133
134 try {
135 cmd.getOptionValue( "f", "default f");
136 cmd.getOptionValue( "m", "default m");
137 }
138 catch( NullPointerException exp ) {
139 fail( "NullPointer caught: " + exp.getMessage() );
140 }
141 }
142 catch( ParseException exp ) {
143 fail( "Unexpected Exception: " + exp.getMessage() );
144 }
145 }
146
147 public void test11456()
148 {
149
150 Options options = new Options();
151 options.addOption( OptionBuilder.hasOptionalArg()
152 .create( 'a' ) );
153 options.addOption( OptionBuilder.hasArg()
154 .create( 'b' ) );
155 String[] args = new String[] { "-a", "-bvalue" };
156
157 CommandLineParser parser = new PosixParser();
158
159 try {
160 CommandLine cmd = parser.parse( options, args );
161 assertEquals( cmd.getOptionValue( 'b' ), "value" );
162 }
163 catch( ParseException exp ) {
164 fail( "Unexpected Exception: " + exp.getMessage() );
165 }
166
167
168 options = new Options();
169 options.addOption( OptionBuilder.hasOptionalArg()
170 .create( 'a' ) );
171 options.addOption( OptionBuilder.hasArg()
172 .create( 'b' ) );
173 args = new String[] { "-a", "-b", "value" };
174
175 parser = new GnuParser();
176
177 try {
178 CommandLine cmd = parser.parse( options, args );
179 assertEquals( cmd.getOptionValue( 'b' ), "value" );
180 }
181 catch( ParseException exp ) {
182 fail( "Unexpected Exception: " + exp.getMessage() );
183 }
184
185 }
186
187 public void test12210() {
188
189 Options mainOptions = new Options();
190
191
192
193
194 String[] argv = new String[] { "-exec", "-exec_opt1", "-exec_opt2" };
195 OptionGroup grp = new OptionGroup();
196
197 grp.addOption(new Option("exec",false,"description for this option"));
198
199 grp.addOption(new Option("rep",false,"description for this option"));
200
201 mainOptions.addOptionGroup(grp);
202
203
204 Options execOptions = new Options();
205 execOptions.addOption("exec_opt1",false," desc");
206 execOptions.addOption("exec_opt2",false," desc");
207
208
209 Options repOptions = new Options();
210 repOptions.addOption("repopto",false,"desc");
211 repOptions.addOption("repoptt",false,"desc");
212
213
214 GnuParser parser = new GnuParser();
215
216
217
218
219
220
221 try {
222 CommandLine cmd = parser.parse(mainOptions,argv,true);
223
224 argv = cmd.getArgs();
225
226 if(cmd.hasOption("exec")){
227 cmd = parser.parse(execOptions,argv,false);
228
229 assertTrue( cmd.hasOption("exec_opt1") );
230 assertTrue( cmd.hasOption("exec_opt2") );
231 }
232 else if(cmd.hasOption("rep")){
233 cmd = parser.parse(repOptions,argv,false);
234
235 }
236 else {
237 fail( "exec option not found" );
238 }
239 }
240 catch( ParseException exp ) {
241 fail( "Unexpected exception: " + exp.getMessage() );
242 }
243 }
244
245 public void test13425() {
246 Options options = new Options();
247 Option oldpass = OptionBuilder.withLongOpt( "old-password" )
248 .withDescription( "Use this option to specify the old password" )
249 .hasArg()
250 .create( 'o' );
251 Option newpass = OptionBuilder.withLongOpt( "new-password" )
252 .withDescription( "Use this option to specify the new password" )
253 .hasArg()
254 .create( 'n' );
255
256 String[] args = {
257 "-o",
258 "-n",
259 "newpassword"
260 };
261
262 options.addOption( oldpass );
263 options.addOption( newpass );
264
265 Parser parser = new PosixParser();
266
267 try {
268 CommandLine line = parser.parse( options, args );
269 }
270
271 catch( Exception exp ) {
272 assertTrue( exp != null );
273 return;
274 }
275 fail( "MissingArgumentException not caught." );
276 }
277
278 public void test13666() {
279 Options options = new Options();
280 Option dir = OptionBuilder.withDescription( "dir" )
281 .hasArg()
282 .create( 'd' );
283 options.addOption( dir );
284
285
286 final PrintStream oldSystemOut = System.out;
287 try{
288 final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
289 final PrintStream print = new PrintStream(bytes);
290
291
292 print.println();
293 final String eol = bytes.toString();
294 bytes.reset();
295
296 System.setOut(new PrintStream(bytes));
297 try {
298 HelpFormatter formatter = new HelpFormatter();
299 formatter.printHelp( "dir", options );
300 }
301 catch( Exception exp ) {
302 fail( "Unexpected Exception: " + exp.getMessage() );
303 }
304 assertEquals("usage: dir"+eol+" -d <arg> dir"+eol,bytes.toString());
305 }
306 finally {
307 System.setOut(oldSystemOut);
308 }
309 }
310
311 public void test13935() {
312 OptionGroup directions = new OptionGroup();
313
314 Option left = new Option( "l", "left", false, "go left" );
315 Option right = new Option( "r", "right", false, "go right" );
316 Option straight = new Option( "s", "straight", false, "go straight" );
317 Option forward = new Option( "f", "forward", false, "go forward" );
318 forward.setRequired( true );
319
320 directions.addOption( left );
321 directions.addOption( right );
322 directions.setRequired( true );
323
324 Options opts = new Options();
325 opts.addOptionGroup( directions );
326 opts.addOption( straight );
327
328 CommandLineParser parser = new PosixParser();
329 boolean exception = false;
330
331 String[] args = new String[] { };
332 try {
333 CommandLine line = parser.parse( opts, args );
334 }
335 catch( ParseException exp ) {
336 exception = true;
337 }
338
339 if( !exception ) {
340 fail( "Expected exception not caught.");
341 }
342
343 exception = false;
344
345 args = new String[] { "-s" };
346 try {
347 CommandLine line = parser.parse( opts, args );
348 }
349 catch( ParseException exp ) {
350 exception = true;
351 }
352
353 if( !exception ) {
354 fail( "Expected exception not caught.");
355 }
356
357 exception = false;
358
359 args = new String[] { "-s", "-l" };
360 try {
361 CommandLine line = parser.parse( opts, args );
362 }
363 catch( ParseException exp ) {
364 fail( "Unexpected exception: " + exp.getClass().getName() + ":" + exp.getMessage() );
365 }
366
367 opts.addOption( forward );
368 args = new String[] { "-s", "-l", "-f" };
369 try {
370 CommandLine line = parser.parse( opts, args );
371 }
372 catch( ParseException exp ) {
373 fail( "Unexpected exception: " + exp.getClass().getName() + ":" + exp.getMessage() );
374 }
375 }
376
377 public void test14786() throws Exception {
378 Option o = OptionBuilder.isRequired().withDescription("test").create("test");
379 Options opts = new Options();
380 opts.addOption(o);
381 opts.addOption(o);
382
383 CommandLineParser parser = new GnuParser();
384
385 String[] args = new String[] { "-test" };
386
387 CommandLine line = parser.parse( opts, args );
388 assertTrue( line.hasOption( "test" ) );
389 }
390
391 public void test15046() throws Exception {
392 CommandLineParser parser = new PosixParser();
393 final String[] CLI_ARGS = new String[] {"-z", "c"};
394 Option option = new Option("z", "timezone", true,
395 "affected option");
396 Options cliOptions = new Options();
397 cliOptions.addOption(option);
398 parser.parse(cliOptions, CLI_ARGS);
399
400
401 cliOptions.addOption("c", "conflict", true, "conflict option");
402 CommandLine line = parser.parse(cliOptions, CLI_ARGS);
403 assertEquals( option.getValue(), "c" );
404 assertTrue( !line.hasOption("c") );
405 }
406
407 public void test15648() throws Exception {
408 CommandLineParser parser = new PosixParser();
409 final String[] args = new String[] { "-m", "\"Two Words\"" };
410 Option m = OptionBuilder.hasArgs().create("m");
411 Options options = new Options();
412 options.addOption( m );
413 CommandLine line = parser.parse( options, args );
414 assertEquals( "Two Words", line.getOptionValue( "m" ) );
415 }
416
417 public void test27635() {
418 Option help = new Option("h", "help", false, "print this message");
419 Option version = new Option("v", "version", false, "print version information");
420 Option newRun = new Option("n", "new", false, "Create NLT cache entries only for new items");
421 Option trackerRun = new Option("t", "tracker", false, "Create NLT cache entries only for tracker items");
422
423 Option timeLimit = OptionBuilder.withLongOpt("limit")
424 .hasArg()
425 .withValueSeparator()
426 .withDescription("Set time limit for execution, in mintues")
427 .create("l");
428
429 Option age = OptionBuilder.withLongOpt("age")
430 .hasArg()
431 .withValueSeparator()
432 .withDescription("Age (in days) of cache item before being recomputed")
433 .create("a");
434
435 Option server = OptionBuilder.withLongOpt("server")
436 .hasArg()
437 .withValueSeparator()
438 .withDescription("The NLT server address")
439 .create("s");
440
441 Option numResults = OptionBuilder.withLongOpt("results")
442 .hasArg()
443 .withValueSeparator()
444 .withDescription("Number of results per item")
445 .create("r");
446
447 Option configFile = OptionBuilder.withLongOpt("config")
448 .hasArg()
449 .withValueSeparator()
450 .withDescription("Use the specified configuration file")
451 .create();
452
453 Options mOptions = new Options();
454 mOptions.addOption(help);
455 mOptions.addOption(version);
456 mOptions.addOption(newRun);
457 mOptions.addOption(trackerRun);
458 mOptions.addOption(timeLimit);
459 mOptions.addOption(age);
460 mOptions.addOption(server);
461 mOptions.addOption(numResults);
462 mOptions.addOption(configFile);
463
464 HelpFormatter formatter = new HelpFormatter();
465 final String EOL = System.getProperty("line.separator");
466 StringWriter out = new StringWriter();
467 formatter.printHelp(new PrintWriter(out),80,"commandline","header",mOptions,2,2,"footer",true);
468 assertEquals(
469 "usage: commandline [-a <arg>] [--config <arg>] [-h] [-l <arg>] [-n] [-r <arg>]" + EOL +
470 " [-s <arg>] [-t] [-v]" + EOL +
471 "header"+EOL+
472 " -a,--age <arg> Age (in days) of cache item before being recomputed"+EOL+
473 " --config <arg> Use the specified configuration file"+EOL+
474 " -h,--help print this message"+EOL+
475 " -l,--limit <arg> Set time limit for execution, in mintues"+EOL+
476 " -n,--new Create NLT cache entries only for new items"+EOL+
477 " -r,--results <arg> Number of results per item"+EOL+
478 " -s,--server <arg> The NLT server address"+EOL+
479 " -t,--tracker Create NLT cache entries only for tracker items"+EOL+
480 " -v,--version print version information"+EOL+
481 "footer"+EOL
482 ,out.toString());
483 }
484
485 public void test31148() throws ParseException {
486 Option multiArgOption = new Option("o","option with multiple args");
487 multiArgOption.setArgs(1);
488
489 Options options = new Options();
490 options.addOption(multiArgOption);
491
492 Parser parser = new PosixParser();
493 String[] args = new String[]{};
494 Properties props = new Properties();
495 props.setProperty("o","ovalue");
496 CommandLine cl = parser.parse(options,args,props);
497
498 assertTrue(cl.hasOption('o'));
499 assertEquals("ovalue",cl.getOptionValue('o'));
500 }
501
502 public void test21215() {
503 Options options = new Options();
504 HelpFormatter formatter = new HelpFormatter();
505 String SEP = System.getProperty("line.separator");
506 String header = SEP+"Header";
507 String footer = "Footer";
508 StringWriter out = new StringWriter();
509 formatter.printHelp(new PrintWriter(out),80, "foobar", header, options, 2, 2, footer, true);
510 assertEquals(
511 "usage: foobar"+SEP+
512 ""+SEP+
513 "Header"+SEP+
514 ""+SEP+
515 "Footer"+SEP
516 ,out.toString());
517 }
518
519 public void test19383() {
520 Options options = new Options();
521 options.addOption(new Option("a","aaa",false,"aaaaaaa"));
522 options.addOption(new Option(null,"bbb",false,"bbbbbbb"));
523 options.addOption(new Option("c",null,false,"ccccccc"));
524
525 HelpFormatter formatter = new HelpFormatter();
526 String SEP = System.getProperty("line.separator");
527 StringWriter out = new StringWriter();
528 formatter.printHelp(new PrintWriter(out),80, "foobar", "", options, 2, 2, "", true);
529 assertEquals(
530 "usage: foobar [-a] [--bbb] [-c]"+SEP+
531 " -a,--aaa aaaaaaa"+SEP+
532 " --bbb bbbbbbb"+SEP+
533 " -c ccccccc"+SEP
534 ,out.toString());
535 }
536
537 }