Skip to content

Commit 0e45f91

Browse files
committed
Merged ParseRequiredTest into ParserTestCase
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/trunk@778920 13f79535-47bb-0310-9956-ffa450edef68
1 parent 58d89c0 commit 0e45f91

2 files changed

Lines changed: 103 additions & 139 deletions

File tree

src/test/org/apache/commons/cli/ParseRequiredTest.java

Lines changed: 0 additions & 138 deletions
This file was deleted.

src/test/org/apache/commons/cli/ParserTestCase.java

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
*/
3131
public abstract class ParserTestCase extends TestCase
3232
{
33-
protected Parser parser;
33+
protected CommandLineParser parser;
3434

3535
protected Options options;
3636

@@ -393,4 +393,106 @@ public void testPartialLongOptionWithShort() throws Exception
393393
assertTrue("Confirm --version is set", cl.hasOption("version"));
394394
assertTrue("Confirm -v is not set", !cl.hasOption("v"));
395395
}
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+
}
396498
}

0 commit comments

Comments
 (0)