You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by tu...@apache.org on 2002/09/19 19:11:46 UTC

cvs commit: jakarta-commons/validator/src/test/org/apache/commons/validator RequiredIfTest.java validator-requiredif.xml TestValidator.java ValidatorTestSuite.java

turner      2002/09/19 10:11:46

  Modified:    validator/src/share/org/apache/commons/validator
                        Validator.java ValidatorResults.java
               validator/src/test/org/apache/commons/validator
                        TestValidator.java ValidatorTestSuite.java
  Added:       validator/src/test/org/apache/commons/validator
                        RequiredIfTest.java validator-requiredif.xml
  Log:
  Committing enhancement speced in Bug 11541, allows validator rules to
  gain access to validator object, and exposes hResources so that rules can pluck
  off the submitted form bean,
  
  Revision  Changes    Path
  1.8       +35 -4     jakarta-commons/validator/src/share/org/apache/commons/validator/Validator.java
  
  Index: Validator.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/Validator.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- Validator.java	30 Mar 2002 04:28:35 -0000	1.7
  +++ Validator.java	19 Sep 2002 17:11:46 -0000	1.8
  @@ -87,6 +87,7 @@
    * and the validation rules for a JavaBean.</p>
    *
    * @author David Winterfeldt
  + * @author James Turner
    * @version $Revision$ $Date$
   */
   public class Validator implements Serializable {
  @@ -118,6 +119,13 @@
      public static String FIELD_KEY = "org.apache.commons.validator.Field";
   
      /**
  +    * Resources key the <code>Validator</code> is stored under.  
  +    * This will be automatically passed into a validation method 
  +    * with the current <code>Validator</code> if it is 
  +    * specified in the method signature.
  +   */
  +   public static String VALIDATOR_KEY = "org.apache.commons.validator.Validator";
  +   /**
       * Resources key the <code>Locale</code> is stored.
       * This will be used to retrieve the appropriate 
       * <code>FormSet</code> and <code>Form</code> to be 
  @@ -189,6 +197,20 @@
      }
   
      /**
  +    * Get a resource to be used during the processing 
  +    * of validations.
  +    *
  +    * @param	key		The full class name of the parameter 
  +    *				of the validation method that 
  +    *				corresponds to the value/instance 
  +    *				passed in with it.
  +   */
  +
  +  public Object getResource(String key) {
  +      return hResources.get(key);
  +  }
  +   
  +   /**
       * Gets the form name which is the key 
       * to a set of validation rules.
      */
  @@ -320,12 +342,16 @@
         if (hResources.containsKey(LOCALE_KEY)) {
            locale = (Locale)hResources.get(LOCALE_KEY);
         }
  +      hResources.put(VALIDATOR_KEY, this);
         
         if (locale == null) {
            locale = Locale.getDefault();
         }
            
         Form form = null;
  +      if (resources == null) {
  +	  throw new ValidatorException("Resources not defined for Validator");
  +      }
         if ((form = resources.get(locale, formName)) != null) {	    
            Map hActions = resources.getValidatorActions();
            List lActions = new ArrayList();
  @@ -413,12 +439,17 @@
                        	  int size = lParams.size();
                        	  int beanIndexPos = -1;
                        	  int fieldIndexPos = -1;
  +                     	  int validatorIndexPos = -1;
                        	  Class[] paramClass = new Class[size];
                        	  Object[] paramValue = new Object[size];
         
                        	  for (int x = 0; x < size; x++) {
                        	     String paramKey = (String)lParams.get(x);
                	             
  +			     if (VALIDATOR_KEY.equals(paramKey)) {
  +				 validatorIndexPos = x;
  +			     }
  +
                	             if (BEAN_KEY.equals(paramKey)) {
                	                beanIndexPos = x;
                	             }
  
  
  
  1.3       +15 -5     jakarta-commons/validator/src/share/org/apache/commons/validator/ValidatorResults.java
  
  Index: ValidatorResults.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/ValidatorResults.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ValidatorResults.java	30 Mar 2002 04:28:35 -0000	1.2
  +++ ValidatorResults.java	19 Sep 2002 17:11:46 -0000	1.3
  @@ -74,6 +74,7 @@
    * validation rules processed on JavaBean.</p>
    *
    * @author David Winterfeldt
  + * @author James Turner
    * @version $Revision$ $Date$
   */
   public class ValidatorResults implements Serializable {
  @@ -83,6 +84,15 @@
      */
      protected Map hResults = new HashMap();
   
  +    /** 
  +     * Merge another ValidatorResults into mine
  +     */
  +
  +    public void merge(ValidatorResults res) {
  +	this.hResults.putAll(res.hResults);
  +    }
  +	
  +
      /**
       * Add a the result of a validator action.
      */
  @@ -218,4 +228,4 @@
         return results;
      }
      
  -}
  \ No newline at end of file
  +}
  
  
  
  1.5       +267 -187  jakarta-commons/validator/src/test/org/apache/commons/validator/TestValidator.java
  
  Index: TestValidator.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/TestValidator.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- TestValidator.java	2 Apr 2002 01:59:44 -0000	1.4
  +++ TestValidator.java	19 Sep 2002 17:11:46 -0000	1.5
  @@ -1,187 +1,267 @@
  -/*
  - * $Header$
  - * $Revision$
  - * $Date$
  - *
  - * ====================================================================
  - *
  - * The Apache Software License, Version 1.1
  - *
  - * Copyright (c) 1999-2002 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 acknowlegement:
  - *       "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 "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 names without prior written
  - *    permission of the Apache Group.
  - *
  - * 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;
  -                                                          
  -/**                                                       
  - * <p>Contains validation methods for different 
  - * unit tests.</p> 
  - *
  - * @author David Winterfeldt
  - * @version $Revision$ $Date$
  -*/                                                       
  -public class TestValidator {
  -                                                          
  -   /**
  -    * Checks if the field is required.
  -    *
  -    * @param 	value 		The value validation is being performed on.
  -    * @return	boolean		If the field isn't <code>null</code> and 
  -    *                           has a length greater than zero, 
  -    *                           <code>true</code> is returned.  
  -    *                           Otherwise <code>false</code>.
  -   */
  -   public static boolean validateRequired(Object bean, Field field) {
  -      String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
  -
  -      return !GenericValidator.isBlankOrNull(value);
  -   }
  -
  -   /**
  -    * Checks if the field can be successfully converted to a <code>byte</code>.
  -    *
  -    * @param 	value 		The value validation is being performed on.
  -    * @return	boolean		If the field can be successfully converted 
  -    *                           to a <code>byte</code> <code>true</code> is returned.  
  -    *                           Otherwise <code>false</code>.
  -   */
  -   public static boolean validateByte(Object bean, Field field) {
  -      String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
  -
  -      return GenericValidator.isByte(value);
  -   }
  -
  -   /**
  -    * Checks if the field can be successfully converted to a <code>short</code>.
  -    *
  -    * @param 	value 		The value validation is being performed on.
  -    * @return	boolean		If the field can be successfully converted 
  -    *                           to a <code>short</code> <code>true</code> is returned.  
  -    *                           Otherwise <code>false</code>.
  -   */
  -   public static boolean validateShort(Object bean, Field field) {
  -      String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
  -
  -      return GenericValidator.isShort(value);
  -   }
  -
  -   /**
  -    * Checks if the field can be successfully converted to a <code>int</code>.
  -    *
  -    * @param 	value 		The value validation is being performed on.
  -    * @return	boolean		If the field can be successfully converted 
  -    *                           to a <code>int</code> <code>true</code> is returned.  
  -    *                           Otherwise <code>false</code>.
  -   */
  -   public static boolean validateInt(Object bean, Field field) {
  -      String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
  -
  -      return GenericValidator.isInt(value);
  -   }
  -
  -   /**
  -    * Checks if the field can be successfully converted to a <code>long</code>.
  -    *
  -    * @param 	value 		The value validation is being performed on.
  -    * @return	boolean		If the field can be successfully converted 
  -    *                           to a <code>long</code> <code>true</code> is returned.  
  -    *                           Otherwise <code>false</code>.
  -   */
  -   public static boolean validateLong(Object bean, Field field) {
  -      String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
  -
  -      return GenericValidator.isLong(value);
  -   }
  -
  -   /**
  -    * Checks if the field can be successfully converted to a <code>float</code>.
  -    *
  -    * @param 	value 		The value validation is being performed on.
  -    * @return	boolean		If the field can be successfully converted 
  -    *                           to a <code>float</code> <code>true</code> is returned.  
  -    *                           Otherwise <code>false</code>.
  -   */
  -   public static boolean validateFloat(Object bean, Field field) {
  -      String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
  -
  -      return GenericValidator.isFloat(value);
  -   }
  -   
  -   /**
  -    * Checks if the field can be successfully converted to a <code>double</code>.
  -    *
  -    * @param 	value 		The value validation is being performed on.
  -    * @return	boolean		If the field can be successfully converted 
  -    *                           to a <code>double</code> <code>true</code> is returned.  
  -    *                           Otherwise <code>false</code>.
  -   */
  -   public static boolean validateDouble(Object bean, Field field) {
  -      String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
  -
  -      return GenericValidator.isDouble(value);
  -   }
  -
  -   /**
  -    * Checks if the field is an e-mail address.
  -    *
  -    * @param 	value 		The value validation is being performed on.
  -    * @return	boolean		If the field is an e-mail address
  -    *                           <code>true</code> is returned.  
  -    *                           Otherwise <code>false</code>.
  -   */
  -   public static boolean validateEmail(Object bean, Field field) {
  -      String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
  -
  -      return GenericValidator.isEmail(value);
  -   }
  -      
  -}                                                         
  \ No newline at end of file
  +/*
  + * $Header$
  + * $Revision$
  + * $Date$
  + *
  + * ====================================================================
  + *
  + * The Apache Software License, Version 1.1
  + *
  + * Copyright (c) 1999-2002 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 acknowlegement:
  + *       "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 "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 names without prior written
  + *    permission of the Apache Group.
  + *
  + * 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;
  +                                                          
  +/**                                                       
  + * <p>Contains validation methods for different 
  + * unit tests.</p> 
  + *
  + * @author David Winterfeldt
  + * @version $Revision$ $Date$
  +*/                                                       
  +public class TestValidator {
  +                                                          
  +   /**
  +    * Checks if the field is required.
  +    *
  +    * @param 	value 		The value validation is being performed on.
  +    * @return	boolean		If the field isn't <code>null</code> and 
  +    *                           has a length greater than zero, 
  +    *                           <code>true</code> is returned.  
  +    *                           Otherwise <code>false</code>.
  +   */
  +   public static boolean validateRequired(Object bean, Field field) {
  +      String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
  +
  +      return !GenericValidator.isBlankOrNull(value);
  +   }
  +
  +   /**
  +    * Checks if the field can be successfully converted to a <code>byte</code>.
  +    *
  +    * @param 	value 		The value validation is being performed on.
  +    * @return	boolean		If the field can be successfully converted 
  +    *                           to a <code>byte</code> <code>true</code> is returned.  
  +    *                           Otherwise <code>false</code>.
  +   */
  +   public static boolean validateByte(Object bean, Field field) {
  +      String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
  +
  +      return GenericValidator.isByte(value);
  +   }
  +
  +   /**
  +    * Checks if the field can be successfully converted to a <code>short</code>.
  +    *
  +    * @param 	value 		The value validation is being performed on.
  +    * @return	boolean		If the field can be successfully converted 
  +    *                           to a <code>short</code> <code>true</code> is returned.  
  +    *                           Otherwise <code>false</code>.
  +   */
  +   public static boolean validateShort(Object bean, Field field) {
  +      String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
  +
  +      return GenericValidator.isShort(value);
  +   }
  +
  +   /**
  +    * Checks if the field can be successfully converted to a <code>int</code>.
  +    *
  +    * @param 	value 		The value validation is being performed on.
  +    * @return	boolean		If the field can be successfully converted 
  +    *                           to a <code>int</code> <code>true</code> is returned.  
  +    *                           Otherwise <code>false</code>.
  +   */
  +   public static boolean validateInt(Object bean, Field field) {
  +      String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
  +
  +      return GenericValidator.isInt(value);
  +   }
  +
  +   /**
  +    * Checks if the field can be successfully converted to a <code>long</code>.
  +    *
  +    * @param 	value 		The value validation is being performed on.
  +    * @return	boolean		If the field can be successfully converted 
  +    *                           to a <code>long</code> <code>true</code> is returned.  
  +    *                           Otherwise <code>false</code>.
  +   */
  +   public static boolean validateLong(Object bean, Field field) {
  +      String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
  +
  +      return GenericValidator.isLong(value);
  +   }
  +
  +   /**
  +    * Checks if the field can be successfully converted to a <code>float</code>.
  +    *
  +    * @param 	value 		The value validation is being performed on.
  +    * @return	boolean		If the field can be successfully converted 
  +    *                           to a <code>float</code> <code>true</code> is returned.  
  +    *                           Otherwise <code>false</code>.
  +   */
  +   public static boolean validateFloat(Object bean, Field field) {
  +      String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
  +
  +      return GenericValidator.isFloat(value);
  +   }
  +   
  +   /**
  +    * Checks if the field can be successfully converted to a <code>double</code>.
  +    *
  +    * @param 	value 		The value validation is being performed on.
  +    * @return	boolean		If the field can be successfully converted 
  +    *                           to a <code>double</code> <code>true</code> is returned.  
  +    *                           Otherwise <code>false</code>.
  +   */
  +   public static boolean validateDouble(Object bean, Field field) {
  +      String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
  +
  +      return GenericValidator.isDouble(value);
  +   }
  +
  +   /**
  +    * Checks if the field is an e-mail address.
  +    *
  +    * @param 	value 		The value validation is being performed on.
  +    * @return	boolean		If the field is an e-mail address
  +    *                           <code>true</code> is returned.  
  +    *                           Otherwise <code>false</code>.
  +   */
  +   public static boolean validateEmail(Object bean, Field field) {
  +      String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
  +
  +      return GenericValidator.isEmail(value);
  +   }
  +
  +  public final static String FIELD_TEST_NULL = "NULL";
  +  public final static String FIELD_TEST_NOTNULL = "NOTNULL";
  +  public final static String FIELD_TEST_EQUAL = "EQUAL";
  +
  +  public static boolean validateRequiredIf(Object bean,
  +      Field field,
  +      Validator validator) {
  +    Object form = validator.getResource(Validator.BEAN_KEY);
  +    String value = null;
  +    boolean required = false;
  +    if (isString(bean)) {
  +      value = (String) bean;
  +    } else {
  +      value = ValidatorUtil.getValueAsString(bean, field.getProperty());
  +    }
  +    int i = 0;
  +    String fieldJoin = "AND";
  +    if (!GenericValidator.isBlankOrNull(field.getVarValue("field-join"))) {
  +      fieldJoin = field.getVarValue("field-join");
  +    }
  +    if (fieldJoin.equalsIgnoreCase("AND")) {
  +      required = true;
  +    }
  +    while (!GenericValidator.isBlankOrNull(field.getVarValue("field[" + i + "]"))) {
  +      String dependProp = field.getVarValue("field[" + i + "]");
  +      String dependTest = field.getVarValue("field-test[" + i + "]");
  +      String dependTestValue = field.getVarValue("field-value[" + i + "]");
  +      String dependIndexed = field.getVarValue("field-indexed[" + i + "]");
  +      if (dependIndexed == null) dependIndexed="false";
  +      String dependVal = null;
  +      boolean this_required = false;
  +      if (field.isIndexed() && dependIndexed.equalsIgnoreCase("true")) {
  +        String key = field.getKey();
  +        if ((key.indexOf("[") > -1) &&
  +            (key.indexOf("]") > -1)) {
  +          String ind = key.substring(0, key.indexOf(".") + 1);
  +          dependProp = ind + dependProp;
  +        }
  +      }
  +      dependVal = ValidatorUtil.getValueAsString(form, dependProp);
  +      if (dependTest.equals(FIELD_TEST_NULL)) {
  +        if ((dependVal != null ) && (dependVal.length() > 0)) {
  +          this_required =  false;
  +        } else {
  +          this_required =  true;
  +        }
  +      }
  +      if (dependTest.equals(FIELD_TEST_NOTNULL)) {
  +        if ((dependVal != null ) && (dependVal.length() > 0)) {
  +          this_required =  true;
  +        } else {
  +          this_required =  false;
  +        }
  +      }
  +      if (dependTest.equals(FIELD_TEST_EQUAL)) {
  +        this_required =  dependTestValue.equalsIgnoreCase(dependVal);
  +      }
  +      if (fieldJoin.equalsIgnoreCase("AND")) {
  +        required = required && this_required;
  +      } else {
  +        required = required || this_required;
  +      }
  +      i++;
  +    }
  +    if (required) {
  +      if ((value != null) && (value.length() > 0)) {
  +        return true;
  +      } else {
  +        return false;
  +      }
  +    }
  +    return true;
  +  }
  +  private static Class stringClass = new String().getClass();
  +
  +  private static boolean isString(Object o) {
  +    if (o == null) return true;
  +    return (stringClass.isInstance(o));
  +  }
  +      
  +}                                                         
  
  
  
  1.4       +105 -104  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.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ValidatorTestSuite.java	2 Apr 2002 01:59:44 -0000	1.3
  +++ ValidatorTestSuite.java	19 Sep 2002 17:11:46 -0000	1.4
  @@ -1,104 +1,105 @@
  -/*
  - * $Header$
  - * $Revision$
  - * $Date$
  - *
  - * ====================================================================
  - *
  - * The Apache Software License, Version 1.1
  - *
  - * Copyright (c) 1999-2002 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 acknowlegement:
  - *       "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 "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 names without prior written
  - *    permission of the Apache Group.
  - *
  - * 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.Test;                           
  -import junit.framework.TestCase;                          
  -import junit.framework.TestSuite;
  -
  -                                                          
  -/**                                                       
  - * Test suite for <code>org.apache.commons.validator</code>
  - * package.
  - *
  - * @author David Winterfeldt
  - * @version $Revision$ $Date$
  -*/                                                       
  -public class ValidatorTestSuite extends TestCase {            
  -                                                          
  -    public ValidatorTestSuite(String name) {                  
  -        super(name);
  -    }                                                     
  -
  -    public static Test suite() {
  -       TestSuite suite = new TestSuite();
  -
  -       suite.addTest(RequiredNameTest.suite()); 
  -       suite.addTest(ByteTest.suite()); 
  -       suite.addTest(ShortTest.suite()); 
  -       suite.addTest(IntegerTest.suite()); 
  -       suite.addTest(LongTest.suite()); 
  -       suite.addTest(FloatTest.suite()); 
  -       suite.addTest(DoubleTest.suite()); 
  -       suite.addTest(TypeTest.suite()); 
  -       suite.addTest(EmailTest.suite()); 
  -       suite.addTest(ValidatorTest.suite()); 
  -
  -       return suite;
  -    }
  -                                                          
  -    public static void main(String args[]) {              
  -        junit.textui.TestRunner.run(suite());
  -    }          
  -                                               
  -}
  +/*
  + * $Header$
  + * $Revision$
  + * $Date$
  + *
  + * ====================================================================
  + *
  + * The Apache Software License, Version 1.1
  + *
  + * Copyright (c) 1999-2002 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 acknowlegement:
  + *       "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 "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 names without prior written
  + *    permission of the Apache Group.
  + *
  + * 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.Test;                           
  +import junit.framework.TestCase;                          
  +import junit.framework.TestSuite;
  +
  +                                                          
  +/**                                                       
  + * Test suite for <code>org.apache.commons.validator</code>
  + * package.
  + *
  + * @author David Winterfeldt
  + * @version $Revision$ $Date$
  +*/                                                       
  +public class ValidatorTestSuite extends TestCase {            
  +                                                          
  +    public ValidatorTestSuite(String name) {                  
  +        super(name);
  +    }                                                     
  +
  +    public static Test suite() {
  +       TestSuite suite = new TestSuite();
  +
  +       suite.addTest(RequiredNameTest.suite()); 
  +       suite.addTest(RequiredIfTest.suite()); 
  +       suite.addTest(ByteTest.suite()); 
  +       suite.addTest(ShortTest.suite()); 
  +       suite.addTest(IntegerTest.suite()); 
  +       suite.addTest(LongTest.suite()); 
  +       suite.addTest(FloatTest.suite()); 
  +       suite.addTest(DoubleTest.suite()); 
  +       suite.addTest(TypeTest.suite()); 
  +       suite.addTest(EmailTest.suite()); 
  +       suite.addTest(ValidatorTest.suite()); 
  +
  +       return suite;
  +    }
  +                                                          
  +    public static void main(String args[]) {              
  +        junit.textui.TestRunner.run(suite());
  +    }          
  +                                               
  +}
  
  
  
  1.1                  jakarta-commons/validator/src/test/org/apache/commons/validator/RequiredIfTest.java
  
  Index: RequiredIfTest.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/RequiredIfTest.java,v 1.1 2002/09/19 17:11:46 turner Exp $
   * $Revision: 1.1 $
   * $Date: 2002/09/19 17:11:46 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2002 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 acknowlegement:
   *       "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 "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 names without prior written
   *    permission of the Apache Group.
   *
   * 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 java.io.IOException;
  import java.io.InputStream;
  import java.util.Map;
  import junit.framework.Test;                           
  import junit.framework.TestCase;                          
  import junit.framework.TestSuite;
  import junit.framework.AssertionFailedError;              
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogSource;
                                                            
  /**                                                       
   * <p>Performs Validation Test.</p> 
   *
   * @author James Turner
   * @version $Revision: 1.1 $ $Date: 2002/09/19 17:11:46 $
  */                                                       
  public class RequiredIfTest extends TestCase {            
     
     /**
      * The key used to retrieve the set of validation 
      * rules from the xml file.
     */
     protected static String FORM_KEY = "nameForm";   
  
     /**
      * The key used to retrieve the validator action.
     */
     protected static String ACTION = "requiredif";
  
     
     /**
      * Commons Logging instance.
     */
     private Log log = LogSource.getInstance(this.getClass().getName());
     
     /**
      * Resources used for validation tests.
     */
     private ValidatorResources resources = null;
     
     public RequiredIfTest(String name) {                  
         super(name);                                      
     }                                                     
  
     /**
      * Start the tests.
      *
      * @param theArgs the arguments. Not used
      */
     public static void main(String[] theArgs) {
         junit.awtui.TestRunner.main(new String[] {RequiredIfTest.class.getName()});
     }
  
     /**
      * @return a test suite (<code>TestSuite</code>) that includes all methods
      *         starting with "test"
      */
     public static Test suite() {
         // All methods starting with "test" will be executed in the test suite.
         return new TestSuite(RequiredIfTest.class);
     }
  
     /**
      * Load <code>ValidatorResources</code> from 
      * validator-name-required.xml.
     */
     protected void setUp() throws IOException {
        // Load resources
        InputStream in = null;
        resources = new ValidatorResources();
        
        try {
           in = this.getClass().getResourceAsStream("validator-requiredif.xml");
           ValidatorResourcesInitializer.initialize(resources, in);
        } catch (IOException e) {
           log.error(e.getMessage(), e);
           throw e;
        } finally {
           if (in != null) {
              try { in.close(); } catch (Exception e) {}	
           }
        }
     }
  
     protected void tearDown() {
     }
  
     /**
      * With nothing provided, we should pass since the fields only fail on
      * null if the other field is non-blank.
     */
     public void testRequired() throws ValidatorException {
        // Create bean to run test on.
        NameBean name = new NameBean();
        
        // Construct validator based on the loaded resources 
        // and the form key
        Validator validator = new Validator(resources, FORM_KEY);
        // add the name bean to the validator as a resource 
        // for the validations to be performed on.
        validator.addResource(Validator.BEAN_KEY, name);
  
        // Get results of the validation.
        ValidatorResults results = null;
        
        // throws ValidatorException, 
        // but we aren't catching for testing 
        // since no validation methods we use 
        // throw this
        results = validator.validate();
        
        assertNotNull("Results are null.", results);
        
        ValidatorResult firstNameResult = results.getValidatorResult("firstName");
        ValidatorResult lastNameResult = results.getValidatorResult("lastName");
        
        assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
        assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
        assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION));
        
        assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult);
        assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
        assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have passed.", lastNameResult.isValid(ACTION));
     }
  
     /**
      * Tests the required validation for first name if it is blank.
     */
     public void testRequiredFirstNameBlank() throws ValidatorException {
        // Create bean to run test on.
        NameBean name = new NameBean();
        name.setFirstName("");
        name.setLastName("Test");
        
        // Construct validator based on the loaded resources 
        // and the form key
        Validator validator = new Validator(resources, FORM_KEY);
        // add the name bean to the validator as a resource 
        // for the validations to be performed on.
        validator.addResource(Validator.BEAN_KEY, name);
  
        // Get results of the validation.
        ValidatorResults results = null;
        
        results = validator.validate();
        
        assertNotNull("Results are null.", results);
        
        ValidatorResult firstNameResult = results.getValidatorResult("firstName");
        ValidatorResult lastNameResult = results.getValidatorResult("lastName");
        
        assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
        assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
        assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have failed.", !firstNameResult.isValid(ACTION));
        
        assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult);
        assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
        assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have passed.", lastNameResult.isValid(ACTION));
     }
  
     /**
      * Tests the required validation for last name.
     */
     public void testRequiredFirstName() throws ValidatorException {
        // Create bean to run test on.
        NameBean name = new NameBean();
        name.setFirstName("Test");
        name.setLastName("Test");
        
        // Construct validator based on the loaded resources 
        // and the form key
        Validator validator = new Validator(resources, FORM_KEY);
        // add the name bean to the validator as a resource 
        // for the validations to be performed on.
        validator.addResource(Validator.BEAN_KEY, name);
  
        // Get results of the validation.
        ValidatorResults results = null;
        
        results = validator.validate();
        
        assertNotNull("Results are null.", results);
        
        ValidatorResult firstNameResult = results.getValidatorResult("firstName");
        ValidatorResult lastNameResult = results.getValidatorResult("lastName");
        
        assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
        assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
        assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION));
        
        assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult);
        assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
        assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have passed.", lastNameResult.isValid(ACTION));
     }
  
     /**
      * Tests the required validation for last name if it is blank.
     */
     public void testRequiredLastNameBlank() throws ValidatorException {
        // Create bean to run test on.
        NameBean name = new NameBean();
        name.setFirstName("Joe");
        name.setLastName("");
        
        // Construct validator based on the loaded resources 
        // and the form key
        Validator validator = new Validator(resources, FORM_KEY);
        // add the name bean to the validator as a resource 
        // for the validations to be performed on.
        validator.addResource(Validator.BEAN_KEY, name);
  
        // Get results of the validation.
        ValidatorResults results = null;
        
        results = validator.validate();
        
        assertNotNull("Results are null.", results);
        
        ValidatorResult firstNameResult = results.getValidatorResult("firstName");
        ValidatorResult lastNameResult = results.getValidatorResult("lastName");
        
        assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
        assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
        assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION));
        
        assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult);
        assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
        assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have failed.", !lastNameResult.isValid(ACTION));
     }
  
     /**
      * Tests the required validation for last name.
     */
     public void testRequiredLastName() throws ValidatorException {
        // Create bean to run test on.
        NameBean name = new NameBean();
        name.setFirstName("Joe");
        name.setLastName("Smith");
        
        // Construct validator based on the loaded resources 
        // and the form key
        Validator validator = new Validator(resources, FORM_KEY);
        // add the name bean to the validator as a resource 
        // for the validations to be performed on.
        validator.addResource(Validator.BEAN_KEY, name);
  
        // Get results of the validation.
        ValidatorResults results = null;
        
        results = validator.validate();
        
        assertNotNull("Results are null.", results);
        
        ValidatorResult firstNameResult = results.getValidatorResult("firstName");
        ValidatorResult lastNameResult = results.getValidatorResult("lastName");
        
        assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
        assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
        assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION));
        
        assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult);
        assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
        assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have passed.", lastNameResult.isValid(ACTION));
  
     }
     
  }                                                         
  
  
  
  1.1                  jakarta-commons/validator/src/test/org/apache/commons/validator/validator-requiredif.xml
  
  Index: validator-requiredif.xml
  ===================================================================
  <form-validation>
     <global>
        <validator name="requiredif"
                   classname="org.apache.commons.validator.TestValidator"
                   method="validateRequiredIf"
                   methodParams="java.lang.Object,org.apache.commons.validator.Field,org.apache.commons.validator.Validator"/>
     </global>
     <formset>
        <form    name="nameForm">
           <field    property="firstName"  depends="requiredif">
           	     <arg0 key="nameForm.firstname.displayname"/>
                    <var>
                     <var-name>field[0]</var-name>
                     <var-value>lastName</var-value>
                    </var>
                    <var>
                     <var-name>field-test[0]</var-name>
                     <var-value>NOTNULL</var-value>
                    </var>
           </field>    
           <field    property="lastName"
           	   depends="requiredif">
           	     <arg0 key="nameForm.lastname.displayname"/>
                    <var>
                     <var-name>field[0]</var-name>
                     <var-value>firstName</var-value>
                    </var>
                    <var>
                     <var-name>field-test[0]</var-name>
                     <var-value>NOTNULL</var-value>
                    </var>
           </field>
        </form>
     </formset>   
  </form-validation>
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>