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");


Reply via email to