Return-Path: Delivered-To: apmail-commons-commits-archive@locus.apache.org Received: (qmail 80712 invoked from network); 30 May 2008 19:09:18 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 30 May 2008 19:09:18 -0000 Received: (qmail 74604 invoked by uid 500); 30 May 2008 19:09:19 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 74177 invoked by uid 500); 30 May 2008 19:09:19 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 74168 invoked by uid 99); 30 May 2008 19:09:18 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 30 May 2008 12:09:18 -0700 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 30 May 2008 19:08:31 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id C722923889FA; Fri, 30 May 2008 12:08:52 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r661819 - in /commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli: ParseTest.java PosixParserTest.java Date: Fri, 30 May 2008 19:08:52 -0000 To: commits@commons.apache.org From: ebourg@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20080530190852.C722923889FA@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: ebourg Date: Fri May 30 12:08:52 2008 New Revision: 661819 URL: http://svn.apache.org/viewvc?rev=661819&view=rev Log: Merged ParseTest into PosixParserTest Removed: commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/ParseTest.java Modified: commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/PosixParserTest.java Modified: commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/PosixParserTest.java URL: http://svn.apache.org/viewvc/commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/PosixParserTest.java?rev=661819&r1=661818&r2=661819&view=diff ============================================================================== --- commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/PosixParserTest.java (original) +++ commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/PosixParserTest.java Fri May 30 12:08:52 2008 @@ -20,12 +20,192 @@ import junit.framework.TestCase; /** - * This is a collection of tests that test real world - * applications command lines focusing on options with - * long and short names. + * Test case for the PosixParser. + * + * @version $Revision$, $Date$ */ -public class PosixParserTest extends TestCase { +public class PosixParserTest extends TestCase +{ + private Options options = null; + private Parser parser = null; + + public void setUp() + { + options = new Options() + .addOption("a", "enable-a", false, "turn [a] on or off") + .addOption("b", "bfile", true, "set the value of [b]") + .addOption("c", "copt", false, "turn [c] on or off"); + parser = new PosixParser(); + } + + public void testSimpleShort() throws Exception + { + String[] args = new String[] { "-a", + "-b", "toast", + "foo", "bar" }; + + CommandLine cl = parser.parse(options, args); + + assertTrue( "Confirm -a is set", cl.hasOption("a") ); + assertTrue( "Confirm -b is set", cl.hasOption("b") ); + assertTrue( "Confirm arg of -b", cl.getOptionValue("b").equals("toast") ); + assertTrue( "Confirm size of extra args", cl.getArgList().size() == 2); + } + + public void testSimpleLong() throws Exception + { + String[] args = new String[] { "--enable-a", + "--bfile", "toast", + "foo", "bar" }; + + CommandLine cl = parser.parse(options, args); + + assertTrue( "Confirm -a is set", cl.hasOption("a") ); + assertTrue( "Confirm -b is set", cl.hasOption("b") ); + assertTrue( "Confirm arg of -b", cl.getOptionValue("b").equals("toast") ); + assertTrue( "Confirm arg of --bfile", cl.getOptionValue( "bfile" ).equals( "toast" ) ); + assertTrue( "Confirm size of extra args", cl.getArgList().size() == 2); + } + + public void testComplexShort() throws Exception + { + String[] args = new String[] { "-acbtoast", + "foo", "bar" }; + + CommandLine cl = parser.parse(options, args); + + assertTrue( "Confirm -a is set", cl.hasOption("a") ); + assertTrue( "Confirm -b is set", cl.hasOption("b") ); + assertTrue( "Confirm -c is set", cl.hasOption("c") ); + assertTrue( "Confirm arg of -b", cl.getOptionValue("b").equals("toast") ); + assertTrue( "Confirm size of extra args", cl.getArgList().size() == 2); + } + + public void testExtraOption() throws Exception + { + String[] args = new String[] { "-adbtoast", + "foo", "bar" }; + + boolean caught = false; + + try + { + CommandLine cl = parser.parse(options, args); + + assertTrue( "Confirm -a is set", cl.hasOption("a") ); + assertTrue( "Confirm -b is set", cl.hasOption("b") ); + assertTrue( "confirm arg of -b", cl.getOptionValue("b").equals("toast") ); + assertTrue( "Confirm size of extra args", cl.getArgList().size() == 3); + } + catch (UnrecognizedOptionException e) + { + caught = true; + } + + assertTrue( "Confirm UnrecognizedOptionException caught", caught ); + } + + public void testMissingArg() throws Exception + { + String[] args = new String[] { "-acb" }; + + boolean caught = false; + + try + { + parser.parse(options, args); + } + catch (MissingArgumentException e) + { + caught = true; + } + + assertTrue( "Confirm MissingArgumentException caught", caught ); + } + + public void testStop() throws Exception + { + String[] args = new String[] { "-c", + "foober", + "-btoast" }; + + CommandLine cl = parser.parse(options, args, true); + assertTrue( "Confirm -c is set", cl.hasOption("c") ); + assertTrue( "Confirm 2 extra args: " + cl.getArgList().size(), cl.getArgList().size() == 2); + } + + public void testMultiple() throws Exception + { + String[] args = new String[] { "-c", + "foobar", + "-btoast" }; + + CommandLine cl = parser.parse(options, args, true); + assertTrue( "Confirm -c is set", cl.hasOption("c") ); + assertTrue( "Confirm 2 extra args: " + cl.getArgList().size(), cl.getArgList().size() == 2); + + cl = parser.parse(options, cl.getArgs() ); + + assertTrue( "Confirm -c is not set", ! cl.hasOption("c") ); + assertTrue( "Confirm -b is set", cl.hasOption("b") ); + assertTrue( "Confirm arg of -b", cl.getOptionValue("b").equals("toast") ); + assertTrue( "Confirm 1 extra arg: " + cl.getArgList().size(), cl.getArgList().size() == 1); + assertTrue( "Confirm value of extra arg: " + cl.getArgList().get(0), cl.getArgList().get(0).equals("foobar") ); + } + + public void testMultipleWithLong() throws Exception + { + String[] args = new String[] { "--copt", + "foobar", + "--bfile", "toast" }; + + CommandLine cl = parser.parse(options,args, + true); + assertTrue( "Confirm -c is set", cl.hasOption("c") ); + assertTrue( "Confirm 3 extra args: " + cl.getArgList().size(), cl.getArgList().size() == 3); + + cl = parser.parse(options, cl.getArgs() ); + + assertTrue( "Confirm -c is not set", ! cl.hasOption("c") ); + assertTrue( "Confirm -b is set", cl.hasOption("b") ); + assertTrue( "Confirm arg of -b", cl.getOptionValue("b").equals("toast") ); + assertTrue( "Confirm 1 extra arg: " + cl.getArgList().size(), cl.getArgList().size() == 1); + assertTrue( "Confirm value of extra arg: " + cl.getArgList().get(0), cl.getArgList().get(0).equals("foobar") ); + } + + public void testDoubleDash() throws Exception + { + String[] args = new String[] { "--copt", + "--", + "-b", "toast" }; + + CommandLine cl = parser.parse(options, args); + + assertTrue( "Confirm -c is set", cl.hasOption("c") ); + assertTrue( "Confirm -b is not set", ! cl.hasOption("b") ); + assertTrue( "Confirm 2 extra args: " + cl.getArgList().size(), cl.getArgList().size() == 2); + } + + public void testSingleDash() throws Exception + { + String[] args = new String[] { "--copt", + "-b", "-", + "-a", + "-" }; + + CommandLine cl = parser.parse(options, args); + + assertTrue( "Confirm -a is set", cl.hasOption("a") ); + assertTrue( "Confirm -b is set", cl.hasOption("b") ); + assertTrue( "Confirm arg of -b", cl.getOptionValue("b").equals("-") ); + assertTrue( "Confirm 1 extra arg: " + cl.getArgList().size(), cl.getArgList().size() == 1); + assertTrue( "Confirm value of extra arg: " + cl.getArgList().get(0), cl.getArgList().get(0).equals("-") ); + } + + /** + * Real world test with long and short options. + */ public void testLongOptionWithShort() throws Exception { Option help = new Option("h", "help", false, "print this message"); Option version = new Option("v", "version", false, "print version information");