commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dgra...@apache.org
Subject cvs commit: jakarta-commons/validator/src/share/org/apache/commons/validator Field.java
Date Sun, 28 Sep 2003 20:39:58 GMT
dgraham     2003/09/28 13:39:58

  Modified:    validator/src/test/org/apache/commons/validator
                        ValidatorTestSuite.java
               validator/src/share/org/apache/commons/validator Field.java
  Added:       validator/src/test/org/apache/commons/validator
                        FieldTest.java
  Log:
  Added Field.getArgs(String) to make it easier to retrieve all of
  the Args for a given validator.  Without this method, you need
  to know how many Args to retrieve with the getArg(String, int)
  method.
  
  Also, the Field.args array was changed to start with a 0
  length instead of 10.  This way the array will only grow to
  the length of the largest argument position.  This will make
  populating the array slower (because of the resizing) but
  will make it easier for clients to deal with the array returned
  from getArgs().
  
  Revision  Changes    Path
  1.11      +5 -4      jakarta-commons/validator/src/test/org/apache/commons/validator/ValidatorTestSuite.java
  
  Index: ValidatorTestSuite.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/ValidatorTestSuite.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- ValidatorTestSuite.java	8 Jun 2003 21:33:22 -0000	1.10
  +++ ValidatorTestSuite.java	28 Sep 2003 20:39:57 -0000	1.11
  @@ -100,6 +100,7 @@
          suite.addTestSuite(CreditCardValidatorTest.class);
          suite.addTest(ValidatorTest.suite());
          suite.addTest(LocaleTest.suite());
  +       suite.addTestSuite(FieldTest.class);
          suite.addTestSuite(FlagsTest.class);
          suite.addTest(UrlTest.suite());
          
  
  
  
  1.1                  jakarta-commons/validator/src/test/org/apache/commons/validator/FieldTest.java
  
  Index: FieldTest.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/FieldTest.java,v
1.1 2003/09/28 20:39:57 dgraham Exp $
   * $Revision: 1.1 $
   * $Date: 2003/09/28 20:39:57 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowledgement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names, "Apache", "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  package org.apache.commons.validator;
  
  import junit.framework.TestCase;
  
  /**
   * Test <code>Field</code> objects.
   * 
   * @author David Graham
   */
  public class FieldTest extends TestCase {
  
  	/**
  	 * FieldTest constructor.
  	 */
  	public FieldTest() {
  		super();
  	}
  
  	/**
  	 * FieldTest constructor.
  	 * @param name
  	 */
  	public FieldTest(String name) {
  		super(name);
  	}
  
  	public void testGetArgs() {
  		// Arg with a validator name
  		Arg a = new Arg();
  		a.setKey("my.resource.key");
  		a.setName("required");
  
  		// Arg without name so it will be stored under default name
  		Arg a2 = new Arg();
  		a2.setKey("another.resource.key");
  		a2.setPosition(1);
  
  		Field f = new Field();
          // test empty args first
          Arg[] emptyArgs = f.getArgs("required");
          assertEquals(0, emptyArgs.length);
          
          // add args for other tests
  		f.addArg(a);
  		f.addArg(a2);
  
  		// test arg lookup for "required" validator
  		Arg[] args = f.getArgs("required");
  		assertEquals(2, args.length);
  		assertTrue(args[0] == a);
  
  		// test arg lookup for non-existent "required2" validator
  		// should find default arg for position 1
  		Arg[] args2 = f.getArgs("required2");
  		assertEquals(2, args2.length);
  		assertNull(args2[0]); // we didn't define a 0 position arg
  		assertTrue(args2[1] == a2);
  
  	}
  
  }
  
  
  
  1.26      +24 -6     jakarta-commons/validator/src/share/org/apache/commons/validator/Field.java
  
  Index: Field.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/Field.java,v
  retrieving revision 1.25
  retrieving revision 1.26
  diff -u -r1.25 -r1.26
  --- Field.java	28 Sep 2003 18:47:32 -0000	1.25
  +++ Field.java	28 Sep 2003 20:39:57 -0000	1.26
  @@ -145,10 +145,11 @@
   
       /**
        * Holds Maps of arguments.  args[0] returns the Map for the first 
  -     * replacement argument.
  +     * replacement argument.  Start with a 0 length array so that it will
  +     * only grow to the size of the highest argument position.
        * @since Validator 1.1
        */
  -    protected Map[] args = new Map[10];
  +    protected Map[] args = new Map[0];
   
       /**
        * @deprecated This variable is no longer used, use args instead.
  @@ -360,6 +361,23 @@
           }
   
           return (arg == null) ? this.getArg(position) : arg;
  +    }
  +    
  +    /**
  +     * Retrieves the Args for the given validator name.
  +     * @param key The validator's args to retrieve.
  +     * @return An Arg[] sorted by the Args' positions (i.e. the Arg at index 0
  +     * has a position of 0). 
  +     * @since Validator 1.1.1
  +     */
  +    public Arg[] getArgs(String key){
  +        Arg[] args = new Arg[this.args.length];
  +        
  +        for (int i = 0; i < this.args.length; i++) {
  +		  args[i] = this.getArg(key, i);	
  +		}
  +        
  +        return args;
       }
   
       /**
  
  
  

Mime
View raw message