commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nia...@apache.org
Subject cvs commit: jakarta-commons/validator/src/test/org/apache/commons/validator FieldTest.java
Date Fri, 12 Nov 2004 16:02:52 GMT
niallp      2004/11/12 08:02:52

  Modified:    validator/conf/share validator_1_2_0.dtd
               validator/src/share/org/apache/commons/validator Arg.java
                        Field.java
               validator/src/test/org/apache/commons/validator
                        FieldTest.java
  Log:
  Bug 31194 Remove the need to specify an Arg's position - raised by David Graham / Martin
Cooper
  
  Revision  Changes    Path
  1.8       +9 -3      jakarta-commons/validator/conf/share/validator_1_2_0.dtd
  
  Index: validator_1_2_0.dtd
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/conf/share/validator_1_2_0.dtd,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- validator_1_2_0.dtd	11 Apr 2004 17:16:17 -0000	1.7
  +++ validator_1_2_0.dtd	12 Nov 2004 16:02:52 -0000	1.8
  @@ -181,8 +181,14 @@
                     [true]
   
         position    The position of this replacement parameter in the message.
  -      			  For example, position="0" will set the first argument.
  -      			  [0]
  +                  For example, position="0" will set the first argument.
  +
  +             N.B. Since 1.2.0 position is ignored for 'default' arguments
  +                  (i.e. those with no 'name' supplied) - it is calculated
  +                  automatically from the order the arguments are added in.
  +                  For 'overriden' arguments (i.e. those with a 'name')
  +                  it is optional. If 'position' is not supplied it will
  +                  use the position of the 'default' argument it follows.
   -->
   <!ELEMENT arg EMPTY>
   <!ATTLIST arg key CDATA #REQUIRED>
  
  
  
  1.19      +8 -4      jakarta-commons/validator/src/share/org/apache/commons/validator/Arg.java
  
  Index: Arg.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/Arg.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- Arg.java	8 Apr 2004 23:05:39 -0000	1.18
  +++ Arg.java	12 Nov 2004 16:02:52 -0000	1.19
  @@ -62,7 +62,7 @@
        * make a replacement in this string: "some msg {0}".
        * @since Validator 1.1
        */
  -    protected int position = 0;
  +    protected int position = -1;
   
       /**
        * Whether or not the key is a message resource (optional).  Defaults to
  @@ -176,6 +176,10 @@
           results.append(name);
           results.append("  key=");
           results.append(key);
  +        results.append("  position=");
  +        results.append(position);
  +        results.append("  bundle=");
  +        results.append(bundle);
           results.append("  resource=");
           results.append(resource);
           results.append("\n");
  
  
  
  1.36      +94 -86    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.35
  retrieving revision 1.36
  diff -u -r1.35 -r1.36
  --- Field.java	11 Nov 2004 15:32:49 -0000	1.35
  +++ Field.java	12 Nov 2004 16:02:52 -0000	1.36
  @@ -50,13 +50,6 @@
   public class Field implements Cloneable, Serializable {
   
       /**
  -     * This is the value that will be used as a key if the <code>Arg</code>
  -     * name field has no value.
  -     */
  -    private static final String DEFAULT_ARG =
  -            "org.apache.commons.validator.Field.DEFAULT";
  -
  -    /**
        * This indicates an indexed property is being referenced.
        */
       public static final String TOKEN_INDEXED = "[]";
  @@ -92,12 +85,16 @@
       protected FastHashMap hMsgs = new FastHashMap();
   
       /**
  -     * Holds Maps of arguments.  args[0] returns the Map for the first 
  -     * 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
  +     * List containing the 'default' arguments
  +     * @since Validator 1.2.0
  +     */
  +    protected List defaultArgs;
  +
  +    /**
  +     * Map containing the 'overriden' arguments.
  +     * @since Validator 1.2.0
        */
  -    protected Map[] args = new Map[0];
  +    protected Map overridenArgs;
   
       /**
        * Gets the page value that the Field is associated with for
  @@ -193,12 +190,14 @@
   
           this.dependencyList.clear();
   
  -        StringTokenizer st = new StringTokenizer(depends, ",");
  -        while (st.hasMoreTokens()) {
  -            String depend = st.nextToken().trim();
  +        if (this.depends != null) {
  +            StringTokenizer st = new StringTokenizer(depends, ",");
  +            while (st.hasMoreTokens()) {
  +                String depend = st.nextToken().trim();
   
  -            if (depend != null && depend.length() > 0) {
  -                this.dependencyList.add(depend);
  +                if (depend != null && depend.length() > 0) {
  +                    this.dependencyList.add(depend);
  +                }
               }
           }
       }
  @@ -240,38 +239,31 @@
        * @since Validator 1.1
        */
       public void addArg(Arg arg) {
  -        // TODO this first if check can go away after arg0, etc. are removed from dtd
  -        if (arg == null || arg.getKey() == null || arg.getKey().length() == 0) {
  -            return;
  -        }
   
  -        this.ensureArgsCapacity(arg);
  +        // Determine if 'default' or 'overriden'
  +        boolean isDefaultArg = arg.getName() == null;
   
  -        Map argMap = this.args[arg.getPosition()];
  -        if (argMap == null) {
  -            argMap = new HashMap();
  -            this.args[arg.getPosition()] = argMap;
  -        }
  +        if (isDefaultArg) {
  +
  +            if (defaultArgs == null) {
  +                defaultArgs = new ArrayList();
  +            }
  +            int pos = defaultArgs.size();
  +            arg.setPosition(pos);
  +            defaultArgs.add(arg);
   
  -        if (arg.getName() == null) {
  -            argMap.put(DEFAULT_ARG, arg);
           } else {
  -            argMap.put(arg.getName(), arg);
  -        }
   
  -    }
  +            if (overridenArgs == null) {
  +                overridenArgs = new HashMap();
  +            }
  +            int position = arg.getPosition();
  +            if (position < 0) {
  +                position = defaultArgs ==  null ? 0 : defaultArgs.size() - 1;
  +                arg.setPosition(position);
  +            }
  +            overridenArgs.put(arg.getName() + position, arg);
   
  -    /**
  -     * Ensures that the args array can hold the given arg.  Resizes the array as
  -     * necessary.
  -     * @param arg Determine if the args array is long enough to store this arg's
  -     * position.
  -     */
  -    private void ensureArgsCapacity(Arg arg) {
  -        if (arg.getPosition() >= this.args.length) {
  -            Map[] newArgs = new Map[arg.getPosition() + 1];
  -            System.arraycopy(this.args, 0, newArgs, 0, this.args.length);
  -            this.args = newArgs;
           }
       }
   
  @@ -281,7 +273,16 @@
        * @since Validator 1.1
        */
       public Arg getArg(int position) {
  -        return this.getArg(DEFAULT_ARG, position);
  +
  +        if (position < 0) {
  +            return null;
  +        }
  +
  +        Arg arg = null; 
  +        if (defaultArgs != null && position < defaultArgs.size()) {
  +            arg = (Arg)defaultArgs.get(position);
  +        }
  +        return arg;
       }
   
       /**
  @@ -295,19 +296,22 @@
        * @since Validator 1.1
        */
       public Arg getArg(String key, int position) {
  -        if ((position >= this.args.length) || (this.args[position] == null)) {
  +
  +        if (position < 0) {
               return null;
           }
   
  -        Arg arg = (Arg) args[position].get(key);
  +        Arg arg = null; 
  +        if (overridenArgs != null) {
  +            arg = (Arg)overridenArgs.get(key + position);
  +        }
   
  -        // Didn't find default arg so exit, otherwise we would get into 
  -        // infinite recursion
  -        if ((arg == null) && key.equals(DEFAULT_ARG)) {
  -            return null;
  +        if (arg == null) {
  +            arg = getArg(position);
           }
   
  -        return (arg == null) ? this.getArg(position) : arg;
  +        return arg;
  +
       }
       
       /**
  @@ -318,11 +322,15 @@
        * @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);	
  -		}
  +
  +        if (defaultArgs == null) {
  +           return new Arg[0];
  +        }
  +
  +        Arg[] args = new Arg[defaultArgs.size()];
  +        for (int i = 0; i < defaultArgs.size(); i++) {
  +            args[i] = getArg(key, i);
  +        }
           
           return args;
       }
  @@ -501,21 +509,19 @@
        * pairs passed in.
        */
       private void processArg(String key, String replaceValue) {
  -        for (int i = 0; i < this.args.length; i++) {
   
  -            Map argMap = this.args[i];
  -            if (argMap == null) {
  -                continue;
  +        if (defaultArgs != null) {
  +            for (int i = 0; i < defaultArgs.size(); i++) {
  +                Arg arg = getArg(i);
  +                arg.setKey(ValidatorUtils.replace(arg.getKey(), key, replaceValue));
               }
  +        }
   
  -            Iterator iter = argMap.values().iterator();
  +        if (overridenArgs != null) {
  +            Iterator iter = overridenArgs.values().iterator();
               while (iter.hasNext()) {
  -                Arg arg = (Arg) iter.next();
  -
  -                if (arg != null) {
  -                    arg.setKey(
  -                            ValidatorUtils.replace(arg.getKey(), key, replaceValue));
  -                }
  +                Arg arg = (Arg)iter.next();
  +                arg.setKey(ValidatorUtils.replace(arg.getKey(), key, replaceValue));
               }
           }
       }
  @@ -539,27 +545,29 @@
        * Creates and returns a copy of this object.
        */
       public Object clone() {
  -        Field field = null;
  -        try {
  -            field = (Field) super.clone();
  -        } catch(CloneNotSupportedException e) {
  -            throw new RuntimeException(e.toString());
  -        }
   
  -        field.args = new Map[this.args.length];
  -        for (int i = 0; i < this.args.length; i++) {
  -            if (this.args[i] == null) {
  -                continue;
  +        Field field = new Field();
  +        field.setDepends(depends);
  +        field.setFieldOrder(fieldOrder);
  +        field.setIndexedListProperty(indexedListProperty);
  +        field.setIndexedProperty(indexedProperty);
  +        field.setKey(key);
  +        field.setPage(page);
  +        field.setProperty(property);
  +
  +        if (defaultArgs != null) {
  +            for (int i = 0; i < defaultArgs.size(); i++) {
  +                Arg arg = getArg(i);
  +                field.addArg((Arg)arg.clone());
               }
  +        }
   
  -            Map argMap = new HashMap(this.args[i]);
  -            Iterator iter = argMap.keySet().iterator();
  +        if (overridenArgs != null) {
  +            Iterator iter = overridenArgs.values().iterator();
               while (iter.hasNext()) {
  -                String validatorName = (String) iter.next();
  -                Arg arg = (Arg) argMap.get(validatorName);
  -                argMap.put(validatorName, arg.clone());
  +                Arg arg = (Arg)iter.next();
  +                field.addArg((Arg)arg.clone());
               }
  -            field.args[i] = argMap;
           }
   
           field.hVars = ValidatorUtils.copyFastHashMap(hVars);
  
  
  
  1.4       +157 -50   jakarta-commons/validator/src/test/org/apache/commons/validator/FieldTest.java
  
  Index: FieldTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/FieldTest.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- FieldTest.java	21 Feb 2004 17:10:30 -0000	1.3
  +++ FieldTest.java	12 Nov 2004 16:02:52 -0000	1.4
  @@ -28,53 +28,160 @@
    */
   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);
   
  -	}
  +    protected Field field;
  +
  +    /**
  +     * FieldTest constructor.
  +     */
  +    public FieldTest() {
  +        super();
  +    }
  +
  +    /**
  +     * FieldTest constructor.
  +     * @param name
  +     */
  +    public FieldTest(String name) {
  +        super(name);
  +    }
  +
  +    /**
  +     * Test setup
  +     */
  +    public void setUp() {
  +        field = new Field();
  +    }
  +
  +    /**
  +     * Test clean up
  +     */
  +    public void tearDown() {
  +        field = null;
  +    }
  +
  +    /**
  +     * test Field with no arguments
  +     */
  +    public void testEmptyArgs() {
  +
  +        assertEquals("Empty Args(1) ", 0, field.getArgs("required").length);
  +        assertEquals("Empty Args(2) ", 0, field.getArgs("required").length);
  +
  +    }
  +
  +    /**
  +     * test Field with only 'default' arguments
  +     */
  +    public void testDefaultArgs() {
  +
  +        field.addArg(defaultArg("position-0"));
  +        field.addArg(defaultArg("position-1"));
  +        field.addArg(defaultArg("position-2"));
  +
  +        assertEquals("testDefaultArgs(1) ", 3, field.getArgs("required").length);
  +        assertEquals("testDefaultArgs(2) ", "position-0", field.getArg("required", 0).getKey());
  +        assertEquals("testDefaultArgs(3) ", "position-1", field.getArg("required", 1).getKey());
  +        assertEquals("testDefaultArgs(4) ", "position-2", field.getArg("required", 2).getKey());
  +
  +    }
  +
  +    /**
  +     * test Field with a 'default' argument overriden using 'position' property
  +     */
  +    public void testOverrideUsingPositionA() {
  +
  +        field.addArg(defaultArg("position-0"));
  +        field.addArg(defaultArg("position-1"));
  +        field.addArg(defaultArg("position-2"));
  +        field.addArg(overrideArg("override-position-1", "required", 1));
  +
  +        // use 'required' as name
  +        assertEquals("testOverrideUsingPositionA(1) ", 3, field.getArgs("required").length);
  +        assertEquals("testOverrideUsingPositionA(2) ", "override-position-1", field.getArg("required",
1).getKey());
  +
  +        // use 'mask' as name
  +        assertEquals("testOverrideUsingPositionA(3) ", 3, field.getArgs("mask").length);
  +        assertEquals("testOverrideUsingPositionA(4) ", "position-1", field.getArg("mask",
1).getKey());
  +
  +        // Get Default
  +        assertEquals("testOverrideUsingPositionA(5) ", "position-1", field.getArg(1).getKey());
  +
  +    }
  +
  +    /**
  +     * test Field with a 'default' argument overriden using 'position' property
  +     */
  +    public void testOverrideUsingPositionB() {
  +
  +        field.addArg(overrideArg("override-position-1", "required", 1));
  +        field.addArg(defaultArg("position-0"));
  +        field.addArg(defaultArg("position-1"));
  +        field.addArg(defaultArg("position-2"));
  +
  +        // use 'required' as name
  +        assertEquals("testOverrideUsingPositionB(1) ", 3, field.getArgs("required").length);
  +        assertEquals("testOverrideUsingPositionB(2) ", "override-position-1", field.getArg("required",
1).getKey());
  +
  +        // use 'mask' as name
  +        assertEquals("testOverrideUsingPositionB(3) ", 3, field.getArgs("mask").length);
  +        assertEquals("testOverrideUsingPositionB(4) ", "position-1", field.getArg("mask",
1).getKey());
  +
  +        // Get Default
  +        assertEquals("testOverrideUsingPositionB(5) ", "position-1", field.getArg(1).getKey());
  +
  +    }
  +
  +    /**
  +     * test Field with a 'default' argument overriden by adding immediately
  +     * after the default argument is added.
  +     */
  +    public void testOverridePositionImplied() {
  +
  +        field.addArg(defaultArg("position-0"));
  +        field.addArg(defaultArg("position-1"));
  +        field.addArg(overrideArg("override-position-1", "required"));
  +        field.addArg(defaultArg("position-2"));
  +
  +        // use 'required' as name
  +        assertEquals("testOverridePositionImplied(1) ", 3, field.getArgs("required").length);
  +        assertEquals("testOverridePositionImplied(2) ", "override-position-1", field.getArg("required",
1).getKey());
  +
  +        // use 'mask' as name
  +        assertEquals("testOverridePositionImplied(3) ", 3, field.getArgs("mask").length);
  +        assertEquals("testOverridePositionImplied(4) ", "position-1", field.getArg("mask",
1).getKey());
  +
  +        // Get Default
  +        assertEquals("testOverridePositionImplied(5) ", "position-1", field.getArg(1).getKey());
  +
  +    }
  +
  +    /**
  +     * Convenience Method - create 'default' argument (i.e. no name)
  +     */
  +    private Arg defaultArg(String key) {
  +        Arg arg = new Arg();
  +        arg.setKey(key);
  +        return arg; 
  +    }
  +
  +    /**
  +     * Convenience Method - create 'overriden' argument (name specified, no position)
  +     */
  +    private Arg overrideArg(String key, String name) {
  +        Arg arg = new Arg();
  +        arg.setKey(key);
  +        arg.setName(name);
  +        return arg; 
  +    }
  +
  +    /**
  +     * Convenience Method - create 'overriden' argument (name & position specified)
  +     */
  +    private Arg overrideArg(String key, String name, int position) {
  +        Arg arg = overrideArg(key, name);
  +        arg.setPosition(position);
  +        return arg; 
  +    }
   
   }
  
  
  

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


Mime
View raw message