|
30 | 30 | */ |
31 | 31 | public abstract class ParserTestCase extends TestCase |
32 | 32 | { |
33 | | - protected Parser parser; |
| 33 | + protected CommandLineParser parser; |
34 | 34 |
|
35 | 35 | protected Options options; |
36 | 36 |
|
@@ -393,4 +393,106 @@ public void testPartialLongOptionWithShort() throws Exception |
393 | 393 | assertTrue("Confirm --version is set", cl.hasOption("version")); |
394 | 394 | assertTrue("Confirm -v is not set", !cl.hasOption("v")); |
395 | 395 | } |
| 396 | + |
| 397 | + public void testWithRequiredOption() throws Exception |
| 398 | + { |
| 399 | + String[] args = new String[] { "-b", "file" }; |
| 400 | + |
| 401 | + Options options = new Options(); |
| 402 | + options.addOption("a", "enable-a", false, null); |
| 403 | + options.addOption(OptionBuilder.withLongOpt("bfile").hasArg().isRequired().create('b')); |
| 404 | + |
| 405 | + CommandLine cl = parser.parse(options,args); |
| 406 | + |
| 407 | + assertTrue("Confirm -a is NOT set", !cl.hasOption("a")); |
| 408 | + assertTrue("Confirm -b is set", cl.hasOption("b")); |
| 409 | + assertTrue("Confirm arg of -b", cl.getOptionValue("b").equals("file")); |
| 410 | + assertTrue("Confirm NO of extra args", cl.getArgList().size() == 0); |
| 411 | + } |
| 412 | + |
| 413 | + public void testOptionAndRequiredOption() throws Exception |
| 414 | + { |
| 415 | + String[] args = new String[] { "-a", "-b", "file" }; |
| 416 | + |
| 417 | + Options options = new Options(); |
| 418 | + options.addOption("a", "enable-a", false, null); |
| 419 | + options.addOption(OptionBuilder.withLongOpt("bfile").hasArg().isRequired().create('b')); |
| 420 | + |
| 421 | + CommandLine cl = parser.parse(options,args); |
| 422 | + |
| 423 | + assertTrue("Confirm -a is set", cl.hasOption("a")); |
| 424 | + assertTrue("Confirm -b is set", cl.hasOption("b")); |
| 425 | + assertTrue("Confirm arg of -b", cl.getOptionValue("b").equals("file")); |
| 426 | + assertTrue("Confirm NO of extra args", cl.getArgList().size() == 0); |
| 427 | + } |
| 428 | + |
| 429 | + public void testMissingRequiredOption() |
| 430 | + { |
| 431 | + String[] args = new String[] { "-a" }; |
| 432 | + |
| 433 | + Options options = new Options(); |
| 434 | + options.addOption("a", "enable-a", false, null); |
| 435 | + options.addOption(OptionBuilder.withLongOpt("bfile").hasArg().isRequired().create('b')); |
| 436 | + |
| 437 | + try |
| 438 | + { |
| 439 | + parser.parse(options,args); |
| 440 | + fail("exception should have been thrown"); |
| 441 | + } |
| 442 | + catch (MissingOptionException e) |
| 443 | + { |
| 444 | + assertEquals( "Incorrect exception message", "Missing required option: b", e.getMessage() ); |
| 445 | + assertTrue(e.getMissingOptions().contains("b")); |
| 446 | + } |
| 447 | + catch (ParseException e) |
| 448 | + { |
| 449 | + fail("expected to catch MissingOptionException"); |
| 450 | + } |
| 451 | + } |
| 452 | + |
| 453 | + public void testMissingRequiredOptions() |
| 454 | + { |
| 455 | + String[] args = new String[] { "-a" }; |
| 456 | + |
| 457 | + Options options = new Options(); |
| 458 | + options.addOption("a", "enable-a", false, null); |
| 459 | + options.addOption(OptionBuilder.withLongOpt("bfile").hasArg().isRequired().create('b')); |
| 460 | + options.addOption(OptionBuilder.withLongOpt("cfile").hasArg().isRequired().create('c')); |
| 461 | + |
| 462 | + try |
| 463 | + { |
| 464 | + parser.parse(options,args); |
| 465 | + fail("exception should have been thrown"); |
| 466 | + } |
| 467 | + catch (MissingOptionException e) |
| 468 | + { |
| 469 | + assertEquals("Incorrect exception message", "Missing required options: b, c", e.getMessage()); |
| 470 | + assertTrue(e.getMissingOptions().contains("b")); |
| 471 | + assertTrue(e.getMissingOptions().contains("c")); |
| 472 | + } |
| 473 | + catch (ParseException e) |
| 474 | + { |
| 475 | + fail("expected to catch MissingOptionException"); |
| 476 | + } |
| 477 | + } |
| 478 | + |
| 479 | + public void testReuseOptionsTwice() throws Exception |
| 480 | + { |
| 481 | + Options opts = new Options(); |
| 482 | + opts.addOption(OptionBuilder.isRequired().create('v')); |
| 483 | + |
| 484 | + // first parsing |
| 485 | + parser.parse(opts, new String[] { "-v" }); |
| 486 | + |
| 487 | + try |
| 488 | + { |
| 489 | + // second parsing, with the same Options instance and an invalid command line |
| 490 | + parser.parse(opts, new String[0]); |
| 491 | + fail("MissingOptionException not thrown"); |
| 492 | + } |
| 493 | + catch (MissingOptionException e) |
| 494 | + { |
| 495 | + // expected |
| 496 | + } |
| 497 | + } |
396 | 498 | } |
0 commit comments