Author: jkeyes
Date: Tue Sep 13 15:42:46 2005
New Revision: 280699

URL: http://svn.apache.org/viewcvs?rev=280699&view=rev
Log:
- added ArgumentBuilder test 

Added:
    
jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/builder/ArgumentBuilderTest.java
Modified:
    
jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/builder/ArgumentBuilder.java
    
jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/ArgumentImpl.java
    
jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/resource/CLIMessageBundle_en_US.properties
    
jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/resource/ResourceConstants.java
    
jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/ParentTest.java

Modified: 
jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/builder/ArgumentBuilder.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/builder/ArgumentBuilder.java?rev=280699&r1=280698&r2=280699&view=diff
==============================================================================
--- 
jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/builder/ArgumentBuilder.java
 (original)
+++ 
jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/builder/ArgumentBuilder.java
 Tue Sep 13 15:42:46 2005
@@ -20,6 +20,8 @@
 
 import org.apache.commons.cli2.Argument;
 import org.apache.commons.cli2.option.ArgumentImpl;
+import org.apache.commons.cli2.resource.ResourceConstants;
+import org.apache.commons.cli2.resource.ResourceHelper;
 import org.apache.commons.cli2.validation.Validator;
 
 /**
@@ -27,6 +29,9 @@
  */
 public class ArgumentBuilder {
 
+       /** i18n */
+       private final static ResourceHelper resources = 
ResourceHelper.getResourceHelper();
+       
     /** name of the argument. Used for display and lookups in CommandLine */
     private String name;
 
@@ -118,6 +123,12 @@
      * @return this ArgumentBuilder
      */
     public final ArgumentBuilder withName(final String newName) {
+       if (newName == null) {
+               throw new 
IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NULL_NAME));
+       }
+       if ("".equals(newName)) {
+               throw new 
IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_EMPTY_NAME));
+       }
         this.name = newName;
         return this;
     }
@@ -142,6 +153,9 @@
      * @return this ArgumentBuilder
      */
     public final ArgumentBuilder withMinimum(final int newMinimum) {
+       if (newMinimum < 0) {
+               throw new 
IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NEGATIVE_MINIMUM));
+       }
         this.minimum = newMinimum;
         return this;
     }
@@ -153,6 +167,9 @@
      * @return this ArgumentBuilder
      */
     public final ArgumentBuilder withMaximum(final int newMaximum) {
+       if (newMaximum < 0) {
+               throw new 
IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NEGATIVE_MAXIMUM));
+       }
         this.maximum = newMaximum;
         return this;
     }
@@ -197,6 +214,9 @@
      * @return this ArgumentBuilder
      */
     public final ArgumentBuilder withValidator(final Validator newValidator) {
+       if (newValidator == null) {
+               throw new 
IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NULL_VALIDATOR));
+       }
         this.validator = newValidator;
         return this;
     }
@@ -210,7 +230,12 @@
      * @return this ArgumentBuilder
      */
     public final ArgumentBuilder withConsumeRemaining(final String 
newConsumeRemaining) {
-
+       if (newConsumeRemaining == null) {
+               throw new 
IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NULL_CONSUME_REMAINING));
+       } 
+       if ( "".equals(newConsumeRemaining)) {
+               throw new 
IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_EMPTY_CONSUME_REMAINING));
+       }
         this.consumeRemaining = newConsumeRemaining;
         return this;
     }
@@ -222,6 +247,10 @@
      * @return this ArgumentBuilder
      */
     public final ArgumentBuilder withDefault(final Object defaultValue) {
+       if (defaultValue == null) {
+               throw new 
IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NULL_DEFAULT));
+       }
+       
         if (this.defaultValues == null) {
             this.defaultValues = new ArrayList(1);
         }
@@ -236,6 +265,9 @@
      * @return this ArgumentBuilder
      */
     public final ArgumentBuilder withDefaults(final List newDefaultValues) {
+       if (newDefaultValues == null) {
+               throw new 
IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NULL_DEFAULTS));
+       }
         this.defaultValues = newDefaultValues;
         return this;
     }

Modified: 
jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/ArgumentImpl.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/ArgumentImpl.java?rev=280699&r1=280698&r2=280699&view=diff
==============================================================================
--- 
jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/ArgumentImpl.java
 (original)
+++ 
jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/ArgumentImpl.java
 Tue Sep 13 15:42:46 2005
@@ -62,7 +62,7 @@
     private final int minimum;
     private final int maximum;
     private final char initialSeparator;
-    private final char subsequentSepatator;
+    private final char subsequentSeparator;
     private final boolean subsequentSplit;
     private final Validator validator;
     private final String consumeRemaining;
@@ -112,7 +112,7 @@
         this.minimum = minimum;
         this.maximum = maximum;
         this.initialSeparator = initialSeparator;
-        this.subsequentSepatator = subsequentSeparator;
+        this.subsequentSeparator = subsequentSeparator;
         this.subsequentSplit = subsequentSeparator != NUL;
         this.validator = validator;
         this.consumeRemaining = consumeRemaining;
@@ -162,7 +162,7 @@
             // should we split the string up?
             else if (subsequentSplit) {
                 final StringTokenizer values =
-                    new StringTokenizer(allValues, 
String.valueOf(subsequentSepatator));
+                    new StringTokenizer(allValues, 
String.valueOf(subsequentSeparator));
 
                 arguments.remove();
 
@@ -206,10 +206,26 @@
         return this.initialSeparator;
     }
 
+    public char getSubsequentSeparator() {
+        return this.subsequentSeparator;
+    }
+
     public Set getTriggers() {
         return Collections.EMPTY_SET;
     }
 
+    public String getConsumeRemaining() {
+       return this.consumeRemaining;
+    }
+    
+    public List getDefaultValues() {
+       return this.defaultValues;
+    }
+    
+    public Validator getValidator() {
+       return this.validator;
+    }
+    
     public void validate(final WriteableCommandLine commandLine)
         throws OptionException {
         validate(commandLine, this);

Modified: 
jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/resource/CLIMessageBundle_en_US.properties
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/resource/CLIMessageBundle_en_US.properties?rev=280699&r1=280698&r2=280699&view=diff
==============================================================================
--- 
jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/resource/CLIMessageBundle_en_US.properties
 (original)
+++ 
jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/resource/CLIMessageBundle_en_US.properties
 Tue Sep 13 15:42:46 2005
@@ -45,3 +45,13 @@
 Unexpected.token = Unexpected {0} while processing

 Missing.option = Missing option

 Cannot.burst = Could not burst "{0}" while processing 

+

+ArgumentBuilder.null.consume.remaining = Cannot use 'null' as the consume 
remaining token.

+ArgumentBuilder.empty.consume.remaining = Cannot use an empty string as the 
consume remaining token.

+ArgumentBuilder.null.defaults = Cannot use 'null' defaults.

+ArgumentBuilder.null.default = Cannot use 'null' default.

+ArgumentBuilder.negative.maximum = Cannot use a negative maximum value.

+ArgumentBuilder.negative.minimum = Cannot use a negative minimum value.

+ArgumentBuilder.null.name = Cannot use 'null' as a name.

+ArgumentBuilder.empty.name = Cannot use an empty string as a name.

+ArgumentBuilder.null.validator = Cannot use 'null' as a validator.


Modified: 
jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/resource/ResourceConstants.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/resource/ResourceConstants.java?rev=280699&r1=280698&r2=280699&view=diff
==============================================================================
--- 
jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/resource/ResourceConstants.java
 (original)
+++ 
jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/resource/ResourceConstants.java
 Tue Sep 13 15:42:46 2005
@@ -54,4 +54,14 @@
     public static final String HELPFORMATTER_GUTTER_TOO_LONG = 
"HelpFormatter.gutter.too.long";

     public static final String HELPFORMATTER_WIDTH_TOO_NARROW = 
"HelpFormatter.width.too.narrow";

     public static final String ENUM_ILLEGAL_VALUE = "Enum.illegal.value";

+    public static final String ARGUMENT_BUILDER_NULL_CONSUME_REMAINING = 
"ArgumentBuilder.null.consume.remaining";

+    public static final String ARGUMENT_BUILDER_EMPTY_CONSUME_REMAINING = 
"ArgumentBuilder.empty.consume.remaining";

+    public static final String ARGUMENT_BUILDER_NULL_DEFAULT = 
"ArgumentBuilder.null.default";

+    public static final String ARGUMENT_BUILDER_NULL_DEFAULTS = 
"ArgumentBuilder.null.defaults";

+    public static final String ARGUMENT_BUILDER_NEGATIVE_MAXIMUM = 
"ArgumentBuilder.negative.maximum";

+    public static final String ARGUMENT_BUILDER_NEGATIVE_MINIMUM = 
"ArgumentBuilder.negative.minimum";

+    public static final String ARGUMENT_BUILDER_NULL_NAME = 
"ArgumentBuilder.null.name";

+    public static final String ARGUMENT_BUILDER_EMPTY_NAME = 
"ArgumentBuilder.empty.name";

+    public static final String ARGUMENT_BUILDER_NULL_VALIDATOR = 
"ArgumentBuilder.null.validator";

+

 }


Added: 
jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/builder/ArgumentBuilderTest.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/builder/ArgumentBuilderTest.java?rev=280699&view=auto
==============================================================================
--- 
jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/builder/ArgumentBuilderTest.java
 (added)
+++ 
jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/builder/ArgumentBuilderTest.java
 Tue Sep 13 15:42:46 2005
@@ -0,0 +1,251 @@
+/*

+ * Copyright 2004-2005 The Apache Software Foundation

+ *

+ * Licensed 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.cli2.builder;

+

+import java.util.ArrayList;

+import java.util.List;

+

+import junit.framework.TestCase;

+

+import org.apache.commons.cli2.option.ArgumentImpl;

+import org.apache.commons.cli2.resource.ResourceConstants;

+import org.apache.commons.cli2.resource.ResourceHelper;

+import org.apache.commons.cli2.validation.DateValidator;

+import org.apache.commons.cli2.validation.Validator;

+

+public class ArgumentBuilderTest

+    extends TestCase {

+    private static final ResourceHelper resources = 
ResourceHelper.getResourceHelper();

+    private ArgumentBuilder argumentBuilder;

+

+    /*

+     * @see TestCase#setUp()

+     */

+    protected void setUp()

+        throws Exception {

+        this.argumentBuilder = new ArgumentBuilder();

+    }

+

+    public void testConsumeRemaining() {

+        this.argumentBuilder.withConsumeRemaining("--");

+        this.argumentBuilder.withName("arg");

+

+        ArgumentImpl arg = (ArgumentImpl) this.argumentBuilder.create();

+

+        assertEquals("incorrect consume remaining token", "--", 
arg.getConsumeRemaining());

+    }

+

+    public void testNullConsumeRemaining() {

+        try {

+            this.argumentBuilder.withConsumeRemaining(null);

+            fail("cannot use null consume remaining token");

+        } catch (IllegalArgumentException exp) {

+            assertEquals("wrong exception message",

+                         
resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NULL_CONSUME_REMAINING),

+                         exp.getMessage());

+        }

+    }

+

+    public void testEmptyConsumeRemaining() {

+        try {

+            this.argumentBuilder.withConsumeRemaining("");

+            fail("cannot use empty string consume remaining token");

+        } catch (IllegalArgumentException exp) {

+            assertEquals("wrong exception message",

+                         
resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_EMPTY_CONSUME_REMAINING),

+                         exp.getMessage());

+        }

+    }

+

+    public void testDefault() {

+        this.argumentBuilder.withDefault("defaultString");

+        this.argumentBuilder.withName("arg");

+

+        ArgumentImpl arg = (ArgumentImpl) this.argumentBuilder.create();

+

+        assertEquals("incorrect number of default values", 1, 
arg.getDefaultValues().size());

+        assertEquals("incorrect default value", "defaultString", 
arg.getDefaultValues().get(0));

+    }

+

+    public void testDefaultX2() {

+        this.argumentBuilder.withDefault("defaultString1");

+        this.argumentBuilder.withDefault("defaultString2");

+        this.argumentBuilder.withName("arg");

+

+        ArgumentImpl arg = (ArgumentImpl) this.argumentBuilder.create();

+

+        assertEquals("incorrect number of default values", 2, 
arg.getDefaultValues().size());

+        assertEquals("incorrect default value-1", "defaultString1", 
arg.getDefaultValues().get(0));

+        assertEquals("incorrect default value-2", "defaultString2", 
arg.getDefaultValues().get(1));

+    }

+

+    public void testNullDefault() {

+        try {

+            this.argumentBuilder.withDefault(null);

+            fail("cannot use null default");

+        } catch (IllegalArgumentException exp) {

+            assertEquals("wrong exception message",

+                         
resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NULL_DEFAULT),

+                         exp.getMessage());

+        }

+    }

+

+    public void testDefaults() {

+        final List defaults = new ArrayList();

+        defaults.add("one");

+        defaults.add("two");

+

+        this.argumentBuilder.withDefaults(defaults);

+        this.argumentBuilder.withName("arg");

+

+        ArgumentImpl arg = (ArgumentImpl) this.argumentBuilder.create();

+

+        assertEquals("incorrect number of default values", 2, 
arg.getDefaultValues().size());

+        assertEquals("incorrect default value-1", "one", 
arg.getDefaultValues().get(0));

+        assertEquals("incorrect default value-2", "two", 
arg.getDefaultValues().get(1));

+        assertEquals("incorrect default values list", defaults, 
arg.getDefaultValues());

+

+    }

+

+    public void testNullDefaults() {

+        try {

+            this.argumentBuilder.withDefaults(null);

+            fail("cannot use null defaults");

+        } catch (IllegalArgumentException exp) {

+            assertEquals("wrong exception message",

+                         
resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NULL_DEFAULTS),

+                         exp.getMessage());

+        }

+    }

+

+    public void testId() {

+        this.argumentBuilder.withId(1);

+        this.argumentBuilder.withName("arg");

+

+        ArgumentImpl arg = (ArgumentImpl) this.argumentBuilder.create();

+

+        assertEquals("incorrect id", 1, arg.getId());

+    }

+

+    public void testInitialSeparator() {

+        this.argumentBuilder.withInitialSeparator(',');

+        this.argumentBuilder.withName("arg");

+

+        ArgumentImpl arg = (ArgumentImpl) this.argumentBuilder.create();

+

+        assertEquals("incorrect initial separator", ',', 
arg.getInitialSeparator());

+    }

+

+    public void testMaximum() {

+        this.argumentBuilder.withMaximum(1);

+        this.argumentBuilder.withName("arg");

+

+        ArgumentImpl arg = (ArgumentImpl) this.argumentBuilder.create();

+

+        assertEquals("incorrect maximum", 1, arg.getMaximum());

+    }

+

+    public void testNegativeMaximum() {

+        try {

+            this.argumentBuilder.withMaximum(-1);

+            fail("cannot use negative maximum");

+        } catch (IllegalArgumentException exp) {

+            assertEquals("wrong exception message",

+                         
resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NEGATIVE_MAXIMUM),

+                         exp.getMessage());

+        }

+    }

+

+    public void testMinimum() {

+        this.argumentBuilder.withMinimum(1);

+        this.argumentBuilder.withName("arg");

+

+        ArgumentImpl arg = (ArgumentImpl) this.argumentBuilder.create();

+

+        assertEquals("incorrect maximum", 1, arg.getMinimum());

+    }

+

+    public void testNegativeMinimum() {

+        try {

+            this.argumentBuilder.withMinimum(-1);

+            fail("cannot use negative minimum");

+        } catch (IllegalArgumentException exp) {

+            assertEquals("wrong exception message",

+                         
resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NEGATIVE_MINIMUM),

+                         exp.getMessage());

+        }

+    }

+

+    public void testName() {

+        this.argumentBuilder.withName("arg");

+

+        ArgumentImpl arg = (ArgumentImpl) this.argumentBuilder.create();

+

+        assertEquals("incorrect preferred name", "arg", 
arg.getPreferredName());

+    }

+

+    public void testNullName() {

+        try {

+            this.argumentBuilder.withName(null);

+            fail("cannot use null name");

+        } catch (IllegalArgumentException exp) {

+            assertEquals("wrong exception message",

+                         
resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NULL_NAME),

+                         exp.getMessage());

+        }

+    }

+

+    public void testEmptyName() {

+        try {

+            this.argumentBuilder.withName("");

+            fail("cannot use empty name");

+        } catch (IllegalArgumentException exp) {

+            assertEquals("wrong exception message",

+                         
resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_EMPTY_NAME),

+                         exp.getMessage());

+        }

+    }

+

+    public void testSubsequentSeparator() {

+        this.argumentBuilder.withSubsequentSeparator(':');

+        this.argumentBuilder.withName("arg");

+

+        ArgumentImpl arg = (ArgumentImpl) this.argumentBuilder.create();

+

+        assertEquals("incorrect subsequent separator", ':', 
arg.getSubsequentSeparator());

+    }

+

+    public void testValidator() {

+        Validator validator = DateValidator.getDateInstance();

+        this.argumentBuilder.withValidator(validator);

+        this.argumentBuilder.withName("arg");

+

+        ArgumentImpl arg = (ArgumentImpl) this.argumentBuilder.create();

+

+        assertEquals("incorrect validator", validator, arg.getValidator());

+    }

+

+    public void testNullValidator() {

+        try {

+            this.argumentBuilder.withValidator(null);

+            fail("cannot use null validator");

+        } catch (IllegalArgumentException exp) {

+            assertEquals("wrong exception message",

+                         
resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NULL_VALIDATOR),

+                         exp.getMessage());

+        }

+    }

+}


Modified: 
jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/ParentTest.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/ParentTest.java?rev=280699&r1=280698&r2=280699&view=diff
==============================================================================
--- 
jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/ParentTest.java
 (original)
+++ 
jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/ParentTest.java
 Tue Sep 13 15:42:46 2005
@@ -391,6 +391,25 @@
         assertFalse(i.hasNext());
     }
 
+    public void testNullPreferredName() {
+        try {
+               new CommandBuilder().create();
+        } catch (IllegalStateException exp) {
+               
assertEquals(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.OPTION_NO_NAME),
 exp.getMessage());
+        }
+    }
+
+    public void testRequired() {
+       Command cmd = new 
CommandBuilder().withRequired(true).withName("blah").create();
+       assertTrue("cmd is not required", cmd.isRequired());
+       assertEquals("id is incorrect", 0, cmd.getId());
+    }
+
+    public void testID() {
+       Command cmd = new 
CommandBuilder().withId('c').withName("blah").create();
+       assertEquals("id is incorrect", 'c', cmd.getId());
+    }
+
     public void testGetId() {
         assertEquals('h', DefaultOptionTest.buildHelpOption().getId());
         assertEquals('X', DefaultOptionTest.buildXOption().getId());



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to