commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ebo...@apache.org
Subject svn commit: r695410 - in /commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli: ApplicationTest.java BasicParserTest.java GnuParserTest.java ParserTestCase.java PosixParserTest.java
Date Mon, 15 Sep 2008 10:25:39 GMT
Author: ebourg
Date: Mon Sep 15 03:25:38 2008
New Revision: 695410

URL: http://svn.apache.org/viewvc?rev=695410&view=rev
Log:
Merged the common parser tests into ParserTestCase
Fixed the integration test for Groovy, highlighting a regression in CLI 1.2

Added:
    commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/ParserTestCase.java
  (with props)
Modified:
    commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/ApplicationTest.java
    commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/BasicParserTest.java
    commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/GnuParserTest.java
    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/ApplicationTest.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/ApplicationTest.java?rev=695410&r1=695409&r2=695410&view=diff
==============================================================================
--- commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/ApplicationTest.java
(original)
+++ commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/ApplicationTest.java
Mon Sep 15 03:25:38 2008
@@ -20,15 +20,14 @@
 import junit.framework.TestCase;
 
 /**
+ * This is a collection of tests that test real world applications command lines.
+ *
  * <p>
- * This is a collection of tests that test real world
- * applications command lines.
- * </p>
- * 
- * <p>
- * The following are the applications that are tested:
+ * The following applications are tested:
  * <ul>
- * <li>Ant</li>
+ *   <li>ls</li>
+ *   <li>Ant</li>
+ *   <li>Groovy</li>
  * </ul>
  * </p>
  *
@@ -164,7 +163,7 @@
             .create('a'));
 
         Parser parser = new PosixParser();
-        CommandLine line = parser.parse(options, new String[] { "-e", "println 'hello'" });
+        CommandLine line = parser.parse(options, new String[] { "-e", "println 'hello'" },
true);
 
         assertTrue(line.hasOption('e'));
         assertEquals("println 'hello'", line.getOptionValue('e'));

Modified: commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/BasicParserTest.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/BasicParserTest.java?rev=695410&r1=695409&r2=695410&view=diff
==============================================================================
--- commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/BasicParserTest.java
(original)
+++ commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/BasicParserTest.java
Mon Sep 15 03:25:38 2008
@@ -17,24 +17,40 @@
 
 package org.apache.commons.cli;
 
-import junit.framework.TestCase;
-
 /**
  * @author Emmanuel Bourg
  * @version $Revision$, $Date$
  */
-public class BasicParserTest extends TestCase
+public class BasicParserTest extends ParserTestCase
 {
-    public void testParser() throws Exception {
-        String[] args = new String[] { "-f", "1" };
+    public void setUp()
+    {
+        super.setUp();
+        parser = new BasicParser();
+    }
 
-        Options options = new Options();
-        options.addOption("f", "foo", true, null);
+    public void testPropertiesOption() throws Exception
+    {
+        // not supported by the BasicParser
+    }
 
-        CommandLine cl = new BasicParser().parse(options, args);
+    public void testShortWithEqual() throws Exception
+    {
+        // not supported by the BasicParser
+    }
 
-        assertNotNull("null CommandLine", cl);
-        assertEquals("1", cl.getOptionValue("foo"));
+    public void testShortWithoutEqual() throws Exception
+    {
+        // not supported by the BasicParser
+    }
+
+    public void testLongWithEqual() throws Exception
+    {
+        // not supported by the BasicParser
+    }
 
+    public void testLongWithEqualSingleDash() throws Exception
+    {
+        // not supported by the BasicParser
     }
 }

Modified: commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/GnuParserTest.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/GnuParserTest.java?rev=695410&r1=695409&r2=695410&view=diff
==============================================================================
--- commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/GnuParserTest.java
(original)
+++ commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/GnuParserTest.java
Mon Sep 15 03:25:38 2008
@@ -17,251 +17,11 @@
 
 package org.apache.commons.cli;
 
-import java.util.Arrays;
-import java.util.List;
-
-import junit.framework.TestCase;
-
-public class GnuParserTest extends TestCase
+public class GnuParserTest extends ParserTestCase
 {
-    private Options options;
-    private Parser parser;
-
     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 GnuParser( );
-    }
-
-    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 size of extra args", cl.getArgList().size() == 2);
-    }
-
-    public void testUnrecognizedOption() throws Exception
-    {
-        String[] args = new String[] { "-a", "-d", "-b", "toast", "foo", "bar" };
-
-        try
-        {
-            parser.parse(options, args);
-            fail("UnrecognizedOptionException wasn't thrown");
-        }
-        catch (UnrecognizedOptionException e)
-        {
-            assertEquals("-d", e.getOption());
-        }
-    }
-
-    public void testMissingArg() throws Exception
-    {
-        String[] args = new String[] { "-b" };
-
-        boolean caught = false;
-
-        try
-        {
-            parser.parse(options, args);
-        }
-        catch (MissingArgumentException e)
-        {
-            caught = true;
-            assertEquals("option missing an argument", "b", e.getOption().getOpt());
-        }
-
-        assertTrue( "Confirm MissingArgumentException caught", caught );
-    }
-
-    public void testStop() throws Exception
-    {
-        String[] args = new String[] { "-c",
-                                       "foober",
-                                       "-b",
-                                       "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);
-    }
-
-    public void testMultiple() throws Exception
-    {
-        String[] args = new String[] { "-c",
-                                       "foobar",
-                                       "-b",
-                                       "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 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("-"));
-    }
-
-    public void testNegativeArgument() throws Exception
-    {
-        String[] args = new String[] { "-a", "-1"} ;
-
-        Options options = new Options();
-        options.addOption(OptionBuilder.hasArg().create("a"));
-
-        Parser parser = new GnuParser();
-        CommandLine cl = parser.parse(options, args);
-        assertEquals("-1", cl.getOptionValue("a"));
-    }
-
-    public void testShortWithEqual() throws Exception
-    {
-        String[] args = new String[] { "-f=bar" };
-
-        Options options = new Options();
-        options.addOption(OptionBuilder.withLongOpt("foo").hasArg().create('f'));
-
-        Parser parser = new GnuParser();
-        CommandLine cl = parser.parse(options, args);
-
-        assertEquals("bar", cl.getOptionValue("foo"));
-    }
-
-    public void testShortWithoutEqual() throws Exception
-    {
-        String[] args = new String[] { "-fbar" };
-
-        Options options = new Options();
-        options.addOption(OptionBuilder.withLongOpt("foo").hasArg().create('f'));
-
-        Parser parser = new GnuParser();
-        CommandLine cl = parser.parse(options, args);
-
-        assertEquals("bar", cl.getOptionValue("foo"));
-    }
-
-    public void testLongWithEqual() throws Exception
-    {
-        String[] args = new String[] { "--foo=bar" };
-
-        Options options = new Options();
-        options.addOption(OptionBuilder.withLongOpt("foo").hasArg().create('f'));
-
-        Parser parser = new GnuParser();
-        CommandLine cl = parser.parse(options, args);
-
-        assertEquals("bar", cl.getOptionValue("foo"));
-    }
-
-    public void testLongWithEqualSingleDash() throws Exception
-    {
-        String[] args = new String[] { "-foo=bar" };
-
-        Options options = new Options();
-        options.addOption(OptionBuilder.withLongOpt("foo").hasArg().create('f'));
-
-        Parser parser = new GnuParser();
-        CommandLine cl = parser.parse(options, args);
-
-        assertEquals("bar", cl.getOptionValue("foo"));
-    }
-
-    public void testPropertiesOption() throws Exception
-    {
-        String[] args = new String[] { "-Jsource=1.5", "-Jtarget=1.5", "foo" };
-
-        Options options = new Options();
-        options.addOption(OptionBuilder.withValueSeparator().hasArgs(2).create('J'));
-
-        Parser parser = new GnuParser();
-        CommandLine cl = parser.parse(options, args);
-
-        List values = Arrays.asList(cl.getOptionValues("J"));
-        assertNotNull("null values", values);
-        assertEquals("number of values", 4, values.size());
-        assertEquals("value 1", "source", values.get(0));
-        assertEquals("value 2", "1.5", values.get(1));
-        assertEquals("value 3", "target", values.get(2));
-        assertEquals("value 4", "1.5", values.get(3));
-        List argsleft = cl.getArgList();
-        assertEquals("Should be 1 arg left",1,argsleft.size());
-        assertEquals("Expecting foo","foo",argsleft.get(0));
+        super.setUp();
+        parser = new GnuParser();
     }
 }

Added: commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/ParserTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/ParserTestCase.java?rev=695410&view=auto
==============================================================================
--- commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/ParserTestCase.java
(added)
+++ commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/ParserTestCase.java
Mon Sep 15 03:25:38 2008
@@ -0,0 +1,298 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.cli;
+
+import java.util.Arrays;
+import java.util.List;
+
+import junit.framework.TestCase;
+
+/**
+ * Abstract test case testing common parser features.
+ *
+ * @author Emmanuel Bourg
+ * @version $Revision$, $Date$
+ */
+public abstract class ParserTestCase extends TestCase
+{
+    protected Parser parser;
+
+    protected Options options;
+
+    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");
+    }
+
+    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 testMultiple() throws Exception
+    {
+        String[] args = new String[] { "-c",
+                                       "foobar",
+                                       "-b", "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 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 testUnrecognizedOption() throws Exception
+    {
+        String[] args = new String[] { "-a", "-d", "-b", "toast", "foo", "bar" };
+
+        try
+        {
+            parser.parse(options, args);
+            fail("UnrecognizedOptionException wasn't thrown");
+        }
+        catch (UnrecognizedOptionException e)
+        {
+            assertEquals("-d", e.getOption());
+        }
+    }    
+
+    public void testMissingArg() throws Exception
+    {
+        String[] args = new String[] { "-b" };
+
+        boolean caught = false;
+
+        try
+        {
+            parser.parse(options, args);
+        }
+        catch (MissingArgumentException e)
+        {
+            caught = true;
+            assertEquals("option missing an argument", "b", e.getOption().getOpt());
+        }
+
+        assertTrue( "Confirm MissingArgumentException caught", caught );
+    }
+
+    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("-"));
+    }
+
+    public void testStopAtUnexpectedArg() throws Exception
+    {
+        String[] args = new String[] { "-c",
+                                       "foober",
+                                       "-b",
+                                       "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);
+    }
+
+   public void testStopAtExpectedArg() throws Exception
+    {
+        String[] args = new String[]{"-b", "foo"};
+
+        CommandLine cl = parser.parse(options, args, true);
+
+        assertTrue("Confirm -b is set", cl.hasOption('b'));
+        assertEquals("Confirm -b is set", "foo", cl.getOptionValue('b'));
+        assertTrue("Confirm no extra args: " + cl.getArgList().size(), cl.getArgList().size()
== 0);
+    }
+
+    public void testStopAtNonOptionShort() throws Exception
+    {
+        String[] args = new String[]{"-z",
+                                     "-a",
+                                     "-btoast"};
+
+        CommandLine cl = parser.parse(options, args, true);
+        assertFalse("Confirm -a is not set", cl.hasOption("a"));
+        assertTrue("Confirm  3 extra args: " + cl.getArgList().size(), cl.getArgList().size()
== 3);
+    }
+
+    public void testStopAtNonOptionLong() throws Exception
+    {
+        String[] args = new String[]{"--zop==1",
+                                     "-abtoast",
+                                     "--b=bar"};
+
+        CommandLine cl = parser.parse(options, args, true);
+
+        assertFalse("Confirm -a is not set", cl.hasOption("a"));
+        assertFalse("Confirm -b is not set", cl.hasOption("b"));
+        assertTrue("Confirm  3 extra args: " + cl.getArgList().size(), cl.getArgList().size()
== 3);
+    }
+
+    public void testNegativeArgument() throws Exception
+    {
+        String[] args = new String[] { "-b", "-1"} ;
+
+        CommandLine cl = parser.parse(options, args);
+        assertEquals("-1", cl.getOptionValue("b"));
+    }
+
+    public void testShortWithEqual() throws Exception
+    {
+        String[] args = new String[] { "-f=bar" };
+
+        Options options = new Options();
+        options.addOption(OptionBuilder.withLongOpt("foo").hasArg().create('f'));
+
+        CommandLine cl = parser.parse(options, args);
+
+        assertEquals("bar", cl.getOptionValue("foo"));
+    }
+
+    public void testShortWithoutEqual() throws Exception
+    {
+        String[] args = new String[] { "-fbar" };
+
+        Options options = new Options();
+        options.addOption(OptionBuilder.withLongOpt("foo").hasArg().create('f'));
+
+        CommandLine cl = parser.parse(options, args);
+
+        assertEquals("bar", cl.getOptionValue("foo"));
+    }
+
+    public void testLongWithEqual() throws Exception
+    {
+        String[] args = new String[] { "--foo=bar" };
+
+        Options options = new Options();
+        options.addOption(OptionBuilder.withLongOpt("foo").hasArg().create('f'));
+
+        CommandLine cl = parser.parse(options, args);
+
+        assertEquals("bar", cl.getOptionValue("foo"));
+    }
+
+    public void testLongWithEqualSingleDash() throws Exception
+    {
+        String[] args = new String[] { "-foo=bar" };
+
+        Options options = new Options();
+        options.addOption(OptionBuilder.withLongOpt("foo").hasArg().create('f'));
+
+        CommandLine cl = parser.parse(options, args);
+
+        assertEquals("bar", cl.getOptionValue("foo"));
+    }
+
+    public void testPropertiesOption() throws Exception
+    {
+        String[] args = new String[] { "-Jsource=1.5", "-J", "target", "1.5", "foo" };
+
+        Options options = new Options();
+        options.addOption(OptionBuilder.withValueSeparator().hasArgs(2).create('J'));
+
+        CommandLine cl = parser.parse(options, args);
+
+        List values = Arrays.asList(cl.getOptionValues("J"));
+        assertNotNull("null values", values);
+        assertEquals("number of values", 4, values.size());
+        assertEquals("value 1", "source", values.get(0));
+        assertEquals("value 2", "1.5", values.get(1));
+        assertEquals("value 3", "target", values.get(2));
+        assertEquals("value 4", "1.5", values.get(3));
+        List argsleft = cl.getArgList();
+        assertEquals("Should be 1 arg left",1,argsleft.size());
+        assertEquals("Expecting foo","foo",argsleft.get(0));
+    }
+}

Propchange: commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/ParserTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/ParserTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

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=695410&r1=695409&r2=695410&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
Mon Sep 15 03:25:38 2008
@@ -17,61 +17,20 @@
 
 package org.apache.commons.cli;
 
-import java.util.Arrays;
-import java.util.List;
-
-import junit.framework.TestCase;
-
 /**
  * Test case for the PosixParser.
  *
  * @version $Revision$, $Date$
  */
-public class PosixParserTest extends TestCase
+public class PosixParserTest extends ParserTestCase
 {
-    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");
-
+        super.setUp();
         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
+    public void testBursting() throws Exception
     {
         String[] args = new String[] { "-acbtoast",
                                        "foo", "bar" };
@@ -85,7 +44,7 @@
         assertTrue( "Confirm size of extra args", cl.getArgList().size() == 2);
     }
 
-    public void testUnrecognizedOption() throws Exception
+    public void testUnrecognizedOptionWithBursting() throws Exception
     {
         String[] args = new String[] { "-adbtoast", "foo", "bar" };
 
@@ -100,22 +59,7 @@
         }
     }
 
-    public void testUnrecognizedOption2() throws Exception
-    {
-        String[] args = new String[] { "-z", "-abtoast", "foo", "bar" };
-
-        try
-        {
-            parser.parse(options, args);
-            fail("UnrecognizedOptionException wasn't thrown");
-        }
-        catch (UnrecognizedOptionException e)
-        {
-            assertEquals("-z", e.getOption());
-        }
-    }
-
-    public void testMissingArg() throws Exception
+    public void testMissingArgWithBursting() throws Exception
     {
         String[] args = new String[] { "-acb" };
 
@@ -134,41 +78,6 @@
         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 testStop2() throws Exception
-    {
-        String[] args = new String[]{"-z",
-                                     "-a",
-                                     "-btoast"};
-
-        CommandLine cl = parser.parse(options, args, true);
-        assertFalse("Confirm -a is not set", cl.hasOption("a"));
-        assertTrue("Confirm  3 extra args: " + cl.getArgList().size(), cl.getArgList().size()
== 3);
-    }
-
-    public void testStop3() throws Exception
-    {
-        String[] args = new String[]{"--zop==1",
-                                     "-abtoast",
-                                     "--b=bar"};
-
-        CommandLine cl = parser.parse(options, args, true);
-
-        assertFalse("Confirm -a is not set", cl.hasOption("a"));
-        assertFalse("Confirm -b is not set", cl.hasOption("b"));
-        assertTrue("Confirm  3 extra args: " + cl.getArgList().size(), cl.getArgList().size()
== 3);
-    }
-
     public void testStopBursting() throws Exception
     {
         String[] args = new String[] { "-azc" };
@@ -181,71 +90,23 @@
         assertTrue(cl.getArgList().contains("zc"));
     }
 
-    public void testMultiple() throws Exception
+    public void testStopBursting2() 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 set", cl.hasOption("c"));
+        assertTrue("Confirm  2 extra args: " + cl.getArgList().size(), cl.getArgList().size()
== 2);
 
-        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" };
+        cl = parser.parse(options, cl.getArgs());
 
-        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("-")
);
+        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"));
     }
 
     /**
@@ -259,7 +120,7 @@
 
         Option timeLimit = OptionBuilder.withLongOpt("limit").hasArg()
                                         .withValueSeparator()
-                                        .withDescription("Set time limit for execution, in
mintues")
+                                        .withDescription("Set time limit for execution, in
minutes")
                                         .create("l");
 
         Option age = OptionBuilder.withLongOpt("age").hasArg()
@@ -315,25 +176,13 @@
         assertEquals(line.getOptionValue("file"), "filename");
     }
 
-    public void testPropertiesOption() throws Exception
+    public void testLongWithEqualSingleDash() throws Exception
     {
-        String[] args = new String[] { "-Jsource=1.5", "-J", "target", "1.5", "foo" };
-
-        Options options = new Options();
-        options.addOption(OptionBuilder.withValueSeparator().hasArgs(2).create('J'));
-
-        Parser parser = new PosixParser();
-        CommandLine cl = parser.parse(options, args);
+        // not supported by the PosixParser
+    }
 
-        List values = Arrays.asList(cl.getOptionValues("J"));
-        assertNotNull("null values", values);
-        assertEquals("number of values", 4, values.size());
-        assertEquals("value 1", "source", values.get(0));
-        assertEquals("value 2", "1.5", values.get(1));
-        assertEquals("value 3", "target", values.get(2));
-        assertEquals("value 4", "1.5", values.get(3));
-        List argsleft = cl.getArgList();
-        assertEquals("Should be 1 arg left",1,argsleft.size());
-        assertEquals("Expecting foo","foo",argsleft.get(0));
+    public void testShortWithEqual() throws Exception
+    {
+        // not supported by the PosixParser
     }
 }



Mime
View raw message