You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by rl...@apache.org on 2003/08/26 17:18:56 UTC

cvs commit: jakarta-commons/validator/src/test/org/apache/commons/validator MultipleTests.java NameBean.java TestValidator.java validator-multipletest.xml

rleland     2003/08/26 08:18:56

  Modified:    validator/src/test/org/apache/commons/validator
                        MultipleTests.java NameBean.java TestValidator.java
                        validator-multipletest.xml
  Log:
  Bug#:	22664, Patches submitted by Mammen Thomas
  Add unit tests to capture how dependencies between validators
  currently works.
  
  Thanks, will be in Validator 1.1.0 release.
  
  Revision  Changes    Path
  1.11      +156 -4    jakarta-commons/validator/src/test/org/apache/commons/validator/MultipleTests.java
  
  Index: MultipleTests.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/MultipleTests.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- MultipleTests.java	23 Aug 2003 02:24:07 -0000	1.10
  +++ MultipleTests.java	26 Aug 2003 15:18:56 -0000	1.11
  @@ -77,6 +77,7 @@
    * <p>Performs Validation Test.</p>
    *
    * @author James Turner
  + * @author Arun Mammen Thomas
    * @version $Revision$ $Date$
   */
   public class MultipleTests extends TestCase {
  @@ -292,5 +293,156 @@
         assertTrue("Last Name ValidatorResult for the 'int' action should have passed.", lastNameResult.isValid("int"));
      }
   
  +   /**
  +    * If middle name is not there, then the required dependent test should fail.
  +    * No other tests should run
  +    *
  +    * @throws ValidatorException
  +    */
  +   public void testFailingFirstDependentValidator() 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;
  +
  +       results = validator.validate();
  +
  +       assertNotNull("Results are null.", results);
  +
  +       ValidatorResult middleNameResult = results.getValidatorResult("middleName");
  +
  +       assertNotNull("Middle Name ValidatorResult should not be null.", middleNameResult);
  +
  +       assertTrue("Middle Name ValidatorResult should contain the 'required' action.", middleNameResult.containsAction("required"));
  +       assertTrue("Middle Name ValidatorResult for the 'required' action should have failed", !middleNameResult.isValid("required"));
  +
  +       assertTrue("Middle Name ValidatorResult should not contain the 'int' action.", !middleNameResult.containsAction("int"));
  +
  +       assertTrue("Middle Name ValidatorResult should not contain the 'positive' action.", !middleNameResult.containsAction("positive"));
  +   }
  +
  +   /**
  +    * If middle name is there but not int, then the required dependent test
  +    * should pass, but the int dependent test should fail. No other tests should
  +    * run.
  +    *
  +    * @throws ValidatorException
  +    */
  +   public void testFailingNextDependentValidator() throws ValidatorException {
  +       // Create bean to run test on.
  +       NameBean name = new NameBean();
  +       name.setMiddleName("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 middleNameResult = results.getValidatorResult("middleName");
  +
  +       assertNotNull("Middle Name ValidatorResult should not be null.", middleNameResult);
  +
  +       assertTrue("Middle Name ValidatorResult should contain the 'required' action.", middleNameResult.containsAction("required"));
  +       assertTrue("Middle Name ValidatorResult for the 'required' action should have passed", middleNameResult.isValid("required"));
  +
  +       assertTrue("Middle Name ValidatorResult should contain the 'int' action.", middleNameResult.containsAction("int"));
  +       assertTrue("Middle Name ValidatorResult for the 'int' action should have failed", !middleNameResult.isValid("int"));
  +
  +       assertTrue("Middle Name ValidatorResult should not contain the 'positive' action.", !middleNameResult.containsAction("positive"));
  +   }
   
  +   /**
  +    * If middle name is there and a negative int, then the required and int
  +    * dependent tests should pass, but the positive test should fail.
  +    *
  +    * @throws ValidatorException
  +    */
  +   public void testPassingDependentsFailingMain() throws ValidatorException {
  +       // Create bean to run test on.
  +       NameBean name = new NameBean();
  +       name.setMiddleName("-2534");
  +
  +       // 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 middleNameResult = results.getValidatorResult("middleName");
  +
  +       assertNotNull("Middle Name ValidatorResult should not be null.", middleNameResult);
  +
  +       assertTrue("Middle Name ValidatorResult should contain the 'required' action.", middleNameResult.containsAction("required"));
  +       assertTrue("Middle Name ValidatorResult for the 'required' action should have passed", middleNameResult.isValid("required"));
  +
  +       assertTrue("Middle Name ValidatorResult should contain the 'int' action.", middleNameResult.containsAction("int"));
  +       assertTrue("Middle Name ValidatorResult for the 'int' action should have passed", middleNameResult.isValid("int"));
  +
  +       assertTrue("Middle Name ValidatorResult should contain the 'positive' action.", middleNameResult.containsAction("positive"));
  +       assertTrue("Middle Name ValidatorResult for the 'positive' action should have failed", !middleNameResult.isValid("positive"));
  +   }
  +
  +   /**
  +    * If middle name is there and a positve int, then the required and int
  +    * dependent tests should pass, and the positive test should pass.
  +    *
  +    * @throws ValidatorException
  +    */
  +   public void testPassingDependentsPassingMain() throws ValidatorException {
  +       // Create bean to run test on.
  +       NameBean name = new NameBean();
  +       name.setMiddleName("2534");
  +
  +       // 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 middleNameResult = results.getValidatorResult("middleName");
  +
  +       assertNotNull("Middle Name ValidatorResult should not be null.", middleNameResult);
  +
  +       assertTrue("Middle Name ValidatorResult should contain the 'required' action.", middleNameResult.containsAction("required"));
  +       assertTrue("Middle Name ValidatorResult for the 'required' action should have passed", middleNameResult.isValid("required"));
  +
  +       assertTrue("Middle Name ValidatorResult should contain the 'int' action.", middleNameResult.containsAction("int"));
  +       assertTrue("Middle Name ValidatorResult for the 'int' action should have passed", middleNameResult.isValid("int"));
  +
  +       assertTrue("Middle Name ValidatorResult should contain the 'positive' action.", middleNameResult.containsAction("positive"));
  +       assertTrue("Middle Name ValidatorResult for the 'positive' action should have passed", middleNameResult.isValid("positive"));
  +   }
   }
  
  
  
  1.4       +21 -4     jakarta-commons/validator/src/test/org/apache/commons/validator/NameBean.java
  
  Index: NameBean.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/NameBean.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- NameBean.java	23 Aug 2003 02:24:07 -0000	1.3
  +++ NameBean.java	26 Aug 2003 15:18:56 -0000	1.4
  @@ -73,6 +73,8 @@
      
      protected String firstName = null;
      
  +   protected String middleName = null;
  +   
      protected String lastName = null;
                                                                
      /**
  @@ -87,6 +89,21 @@
      */
      public void setFirstName(String firstName) {
         this.firstName = firstName;	
  +   }
  +
  +      
  +   /**
  +    * @return
  +    */
  +   public String getMiddleName() {
  +       return middleName;
  +   }
  +
  +   /**
  +    * @param string
  +    */
  +   public void setMiddleName(String middleName) {
  +       this.middleName = middleName;
      }
   
      /**
  
  
  
  1.11      +18 -4     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.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- TestValidator.java	23 Aug 2003 02:24:07 -0000	1.10
  +++ TestValidator.java	26 Aug 2003 15:18:56 -0000	1.11
  @@ -131,6 +131,20 @@
      }
   
      /**
  +    * Checks if field is positive assuming it is an integer
  +    * 
  +    * @param    value       The value validation is being performed on.
  +    * @param    field       Description of the field to be evaluated
  +    * @return   boolean     If the integer field is greater than zero, returns
  +    *                        true, otherwise returns false.
  +    */
  +   public static boolean validatePositive(Object bean , Field field) {
  +      String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
  +   
  +      return GenericTypeValidator.formatInt(value).intValue() > 0;                                                      
  +   }
  +
  +   /**
       * Checks if the field can be successfully converted to a <code>long</code>.
       *
       * @param 	value 		The value validation is being performed on.
  
  
  
  1.4       +9 -0      jakarta-commons/validator/src/test/org/apache/commons/validator/validator-multipletest.xml
  
  Index: validator-multipletest.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/validator-multipletest.xml,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- validator-multipletest.xml	12 Aug 2003 00:29:34 -0000	1.3
  +++ validator-multipletest.xml	26 Aug 2003 15:18:56 -0000	1.4
  @@ -15,6 +15,12 @@
                    method="validateRequired"
                    methodParams="java.lang.Object,org.apache.commons.validator.Field"
                    msg=""/>
  +      <validator name="positive"
  +                 classname="org.apache.commons.validator.TestValidator"
  +                 method="validatePositive"
  +                 methodParams="java.lang.Object,org.apache.commons.validator.Field"
  +                 depends="required,int" 
  +                 msg=""/>
      </global>
      
      <formset>
  @@ -22,6 +28,9 @@
            <field    property="firstName"  depends="required">
            	     <arg key="nameForm.firstname.displayname"/>
            </field>    
  +         <field    property="middleName" depends="positive">
  +                 <arg0 key="nameForm.middlename.displayname"/> 
  +         </field>   
            <field    property="lastName"
            	   depends="required,int">
            	     <arg key="nameForm.lastname.displayname"/>