commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jke...@apache.org
Subject cvs commit: jakarta-commons/cli/src/test/org/apache/commons/cli OptionBuilderTest.java
Date Mon, 29 Jul 2002 22:12:38 GMT
jkeyes      2002/07/29 15:12:38

  Modified:    cli      project.xml
               cli/src/java/org/apache/commons/cli Option.java
  Added:       cli/src/java/org/apache/commons/cli OptionBuilder.java
               cli/src/test/org/apache/commons/cli OptionBuilderTest.java
  Log:
  OptionBuilder first cut
  
  Revision  Changes    Path
  1.5       +8 -0      jakarta-commons/cli/project.xml
  
  Index: project.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/cli/project.xml,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- project.xml	25 Jul 2002 02:36:45 -0000	1.4
  +++ project.xml	29 Jul 2002 22:12:37 -0000	1.5
  @@ -54,4 +54,12 @@
     
     </dependencies>
     
  +  <build>
  +    <!-- Unit test classes -->
  +    <unitTest>
  +      <includes>
  +        <include>**/*Test*.java</include>
  +      </includes>
  +    </unitTest>
  +  </build>
   </project>
  
  
  
  1.4       +70 -5     jakarta-commons/cli/src/java/org/apache/commons/cli/Option.java
  
  Index: Option.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/Option.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- Option.java	9 Jul 2002 23:12:15 -0000	1.3
  +++ Option.java	29 Jul 2002 22:12:37 -0000	1.4
  @@ -116,6 +116,43 @@
       private ArrayList  values       = new ArrayList();
       
   
  +    private void validateOption( String opt ) 
  +    throws IllegalArgumentException
  +    {
  +        if( opt == null ) {
  +            throw new IllegalArgumentException( "opt is null" );
  +        }
  +        else if( opt.length() == 1 ) {
  +            if ( !isValidOpt( opt.charAt( 0 ) ) ) {
  +                throw new IllegalArgumentException( "illegal option value '" 
  +                                                    + opt.charAt( 0 ) + "'" );
  +            }
  +        }
  +        else {
  +            char[] chars = opt.toCharArray();
  +            for( int i = 0; i < chars.length; i++ ) {
  +                if( !isValidChar( chars[i] ) ) {
  +                    throw new IllegalArgumentException( "opt contains illegal character
value '" + chars[i] + "'" );
  +                }
  +            }
  +        }
  +    }
  +
  +    private boolean isValidOpt( char c ) 
  +    {
  +        if ( ! ( isValidChar( c ) || c == '?' || c == '@') ) {
  +            return false;
  +        }
  +        return true;
  +    }
  +
  +    private boolean isValidChar( char c ) 
  +    {
  +        if ( ! ( Character.isLetter( c ) ) ) {
  +            return false;
  +        }
  +        return true;
  +    }
       /**
        * Creates an Option using the specified parameters.
        *
  @@ -123,7 +160,9 @@
        * @param hasArg specifies whether the Option takes an argument or not
        * @param description describes the function of the option
        */
  -    public Option(String opt, boolean hasArg, String description) {
  +    public Option(String opt, boolean hasArg, String description) 
  +    throws IllegalArgumentException
  +    {
           this(opt, null, hasArg, description, false, false);
       }
       
  @@ -135,7 +174,9 @@
        * @param hasArg specifies whether the Option takes an argument or not
        * @param description describes the function of the option
        */
  -    public Option(String opt, String longOpt, boolean hasArg, String description) {
  +    public Option(String opt, String longOpt, boolean hasArg, String description) 
  +    throws IllegalArgumentException
  +    {
           this(opt, longOpt, hasArg, description, false, false );
       }
   
  @@ -149,7 +190,9 @@
        * @param required specifies whether the option is required or not
        */
       public Option(String opt, String longOpt, boolean hasArg, String description,
  -                  boolean required ) {
  +                  boolean required ) 
  +    throws IllegalArgumentException
  +    {
           this(opt, longOpt, hasArg, description, required, false );
       }
   
  @@ -165,7 +208,9 @@
        * values
        */
       public Option(String opt, String longOpt, boolean hasArg, String description, 
  -                  boolean required, boolean multipleArgs ) {
  +                  boolean required, boolean multipleArgs ) 
  +    throws IllegalArgumentException
  +    {
           this(opt, longOpt, hasArg, description, required, multipleArgs, null );
       }
   
  @@ -182,7 +227,11 @@
        * @param type specifies the type of the option
        */
       public Option(String opt, String longOpt, boolean hasArg, String description, 
  -                  boolean required, boolean multipleArgs, Object type ) {
  +                  boolean required, boolean multipleArgs, Object type ) 
  +    throws IllegalArgumentException
  +    {
  +        validateOption( opt );
  +
           this.opt          = opt;
           this.longOpt      = longOpt;
           this.hasArg       = hasArg;
  @@ -213,6 +262,10 @@
       public Object getType() {
           return this.type;
       }
  +
  +    public void setType( Object type ) {
  +        this.type = type;
  +    }
       
       /** <p>Retrieve the long name of this Option</p>
        *
  @@ -221,6 +274,10 @@
       public String getLongOpt() {
           return this.longOpt;
       }
  +
  +    public void setLongOpt( String longOpt ) {
  +        this.longOpt = longOpt;
  +    }
       
       /** <p>Query to see if this Option has a long name</p>
        *
  @@ -254,12 +311,20 @@
            return this.required;
        }
   
  +     public void setRequired( boolean required ) {
  +         this.required = required;
  +     }
  +
        /** <p>Query to see if this Option can take multiple values</p>
         *
         * @return boolean flag indicating if multiple values are allowed
         */
        public boolean hasMultipleArgs() {
            return this.multipleArgs;
  +     }
  +
  +     public void setMultipleArgs( boolean multipleArgs ) {
  +         this.multipleArgs = multipleArgs;
        }
   
       /** <p>Dump state, suitable for debugging.</p>
  
  
  
  1.1                  jakarta-commons/cli/src/java/org/apache/commons/cli/OptionBuilder.java
  
  Index: OptionBuilder.java
  ===================================================================
  package org.apache.commons.cli;
  
  public class OptionBuilder {
  
      private static String longopt;
      private static String description;
      private static boolean arg;
      private static boolean required;
      private static boolean multipleArgs;
      private static Object type;
  
      private static OptionBuilder instance = new OptionBuilder();
  
      // private constructor
      private OptionBuilder() {
      }
  
      private static void reset() {
          description = null;
          longopt = null;
          type = null;
          arg = false;
          required = false;
          multipleArgs = false;
      }
  
      public static OptionBuilder withLongOpt( String longopt ) {
          instance.longopt = longopt;
          return instance;
      }
  
      public static OptionBuilder hasArg( ) {
          instance.arg = true;
          return instance;
      }
  
      public static OptionBuilder isRequired( ) {
          instance.required = true;
          return instance;
      }
  
      public static OptionBuilder hasMultipleArgs( ) {
          instance.multipleArgs = true;
          return instance;
      }
  
      public static OptionBuilder withType( Object type ) {
          instance.type = type;
          return instance;
      }
  
      public static OptionBuilder withDescription( String description ) {
          instance.description = description;
          return instance;
      }
  
      public static Option create( char opt )
      throws IllegalArgumentException
      {
          return create( String.valueOf( opt ) );
      }
  
      public static Option create( String opt ) 
      throws IllegalArgumentException
      {
          Option option = new Option( opt, arg, description );
          option.setLongOpt( longopt );
          option.setRequired( required );
          option.setMultipleArgs( multipleArgs );
          option.setType( type );
          instance.reset();
          return option;
      }
  }
  
  
  1.1                  jakarta-commons/cli/src/test/org/apache/commons/cli/OptionBuilderTest.java
  
  Index: OptionBuilderTest.java
  ===================================================================
  package org.apache.commons.cli;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  
  import junit.textui.TestRunner;
  
  public class OptionBuilderTest extends TestCase {
  
      public OptionBuilderTest( String name ) {
          super( name );
      }
  
      public static Test suite() { 
          return new TestSuite( OptionBuilderTest.class ); 
      }
  
      public static void main( String args[] ) { 
          TestRunner.run( suite() );
      }
  
      public void testCompleteOption( ) {
          Option simple = OptionBuilder.withLongOpt( "simple option")
                                       .hasArg( )
                                       .isRequired( )
                                       .hasMultipleArgs( )
                                       .withType( new Float( 10 ) )
                                       .withDescription( "this is a simple option" )
                                       .create( 's' );
  
          assertEquals( "s", simple.getOpt() );
          assertEquals( "simple option", simple.getLongOpt() );
          assertEquals( "this is a simple option", simple.getDescription() );
          assertEquals( simple.getType().getClass(), Float.class );
          assertTrue( simple.hasArg() );
          assertTrue( simple.isRequired() );
          assertTrue( simple.hasMultipleArgs() );
      }
  
      public void testTwoCompleteOptions( ) {
          Option simple = OptionBuilder.withLongOpt( "simple option")
                                       .hasArg( )
                                       .isRequired( )
                                       .hasMultipleArgs( )
                                       .withType( new Float( 10 ) )
                                       .withDescription( "this is a simple option" )
                                       .create( 's' );
  
          assertEquals( "s", simple.getOpt() );
          assertEquals( "simple option", simple.getLongOpt() );
          assertEquals( "this is a simple option", simple.getDescription() );
          assertEquals( simple.getType().getClass(), Float.class );
          assertTrue( simple.hasArg() );
          assertTrue( simple.isRequired() );
          assertTrue( simple.hasMultipleArgs() );
  
          simple = OptionBuilder.withLongOpt( "dimple option")
                                .hasArg( )
                                .withDescription( "this is a dimple option" )
                                .create( 'd' );
  
          assertEquals( "d", simple.getOpt() );
          assertEquals( "dimple option", simple.getLongOpt() );
          assertEquals( "this is a dimple option", simple.getDescription() );
          assertNull( simple.getType() );
          assertTrue( simple.hasArg() );
          assertTrue( !simple.isRequired() );
          assertTrue( !simple.hasMultipleArgs() );
      }
  
      public void testBaseOptionCharOpt() {
          Option base = OptionBuilder.withDescription( "option description")
                                     .create( 'o' );
  
          assertEquals( "o", base.getOpt() );
          assertEquals( "option description", base.getDescription() );
          assertTrue( !base.hasArg() );
      }
  
      public void testBaseOptionStringOpt() {
          Option base = OptionBuilder.withDescription( "option description")
                                     .create( "o" );
  
          assertEquals( "o", base.getOpt() );
          assertEquals( "option description", base.getDescription() );
          assertTrue( !base.hasArg() );
      }
  
      public void testSpecialOptChars() {
  
          // '?'
          try {
              Option opt = OptionBuilder.withDescription( "help options" )
                                        .create( '?' );
              assertEquals( "?", opt.getOpt() );
          }
          catch( IllegalArgumentException arg ) {
              fail( "IllegalArgumentException caught" );
          }
  
          // '@'
          try {
              Option opt = OptionBuilder.withDescription( "read from stdin" )
                                        .create( '@' );
              assertEquals( "@", opt.getOpt() );
          }
          catch( IllegalArgumentException arg ) {
              fail( "IllegalArgumentException caught" );
          }
      }
  
      public void testIllegalOptions() {
          // bad single character option
          try {
              Option opt = OptionBuilder.withDescription( "option description" )
                                        .create( '"' );
              fail( "IllegalArgumentException not caught" );
          }
          catch( IllegalArgumentException exp ) {
              // success
          }
  
          // bad character in option string
          try {
              Option opt = OptionBuilder.create( "opt`" );
              fail( "IllegalArgumentException not caught" );
          }
          catch( IllegalArgumentException exp ) {
              // success
          }
  
          // null option
          try {
              Option opt = OptionBuilder.create( null );
              fail( "IllegalArgumentException not caught" );
          }
          catch( IllegalArgumentException exp ) {
              // success
          }
  
          // valid option 
          try {
              Option opt = OptionBuilder.create( "opt" );
              // success
          }
          catch( IllegalArgumentException exp ) {
              fail( "IllegalArgumentException caught" );
          }
      }
  }
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message