commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hus...@apache.org
Subject cvs commit: jakarta-commons/validator/src/test/org/apache/commons/validator JXPathTest.java ObjectBean.java validator-jxpath.xml ValidatorTestSuite.java
Date Tue, 08 Jun 2004 15:05:39 GMT
husted      2004/06/08 08:05:39

  Modified:    validator project.xml
               validator/src/test/org/apache/commons/validator
                        ValidatorTestSuite.java
  Added:       validator/src/share/org/apache/commons/validator
                        JXPathValidator.java
               validator/src/test/org/apache/commons/validator
                        JXPathTest.java ObjectBean.java
                        validator-jxpath.xml
  Log:
  Apply  #29145 "Add JXPath Validator" submitted by Don Brown.
  
  Revision  Changes    Path
  1.46      +6 -0      jakarta-commons/validator/project.xml
  
  Index: project.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/project.xml,v
  retrieving revision 1.45
  retrieving revision 1.46
  diff -u -r1.45 -r1.46
  --- project.xml	8 Jun 2004 14:48:35 -0000	1.45
  +++ project.xml	8 Jun 2004 15:05:38 -0000	1.46
  @@ -261,6 +261,12 @@
         <version>1.0.3</version>
         <url>http://jakarta.apache.org/commons/logging.html</url>
       </dependency>
  +    
  +    <dependency>
  +      <id>commons-jxpath</id>
  +      <version>1.1</version>
  +      <url>http://jakarta.apache.org/commons/jxpath.html</url>
  +    </dependency>
   
       <dependency>
         <id>oro</id>
  
  
  
  1.1                  jakarta-commons/validator/src/share/org/apache/commons/validator/JXPathValidator.java
  
  Index: JXPathValidator.java
  ===================================================================
  /*
   *  $Header: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/JXPathValidator.java,v
1.1 2004/06/08 15:05:38 husted Exp $
   *  $Revision: 1.1 $
   *  $Date: 2004/06/08 15:05:38 $
   *
   *  ====================================================================
   *  Copyright 2001-2004 The Apache Software Foundation
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *  http://www.apache.org/licenses/LICENSE-2.0
   *
   *  Unless required by applicable law or agreed to in writing, software
   *  distributed under the License is distributed on an "AS IS" BASIS,
   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *  See the License for the specific language governing permissions and
   *  limitations under the License.
   */
  package org.apache.commons.validator;
  
  import org.apache.commons.jxpath.JXPathContext;
  import org.apache.commons.jxpath.Pointer;
  import java.util.Iterator;
  import java.lang.reflect.InvocationTargetException;
  import org.apache.commons.beanutils.PropertyUtils;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  
  /**
   *  <p>
   *
   *  Perform jxpath validations.</p> <p>
   *
   *  The xpath expression in var "test" will be evaluated against the object
   *  scoped by the field property value, another xpath expression. Multiple
   *  matches for the field property xpath expression are allowed. </p> <p>
   *
   *  This implementation is NOT thread safe, however it can be reused multiple
   *  times by one thread. </p> .
   */
  public class JXPathValidator {
  
      private final static Log log = LogFactory.getLog(JXPathValidator.class);
      private Object bean = null;
      private JXPathContext ctx = null;
  
  
      /**
       *  Determine whether the field is valid by evaluating the xpath expression
       *  in the "test" variable and returning the boolean result.
       *
       *@param  obj    The object to validate
       *@param  field  The field to validate
       *@return        The boolean result of the xpath evaluation
       */
      public boolean isValid(Object obj, Field field) {
  
          // Build the context for this bean if one doesn't exist
          if (obj != bean) {
              this.bean = obj;
              ctx = JXPathContext.newContext(this.bean);
          }
  
          // Iterate through the field matches and run the test
          Pointer ptr;
          JXPathContext localCtx;
          for (Iterator i = ctx.iteratePointers(field.getProperty()); i.hasNext(); ) {
              ptr = (Pointer) i.next();
              localCtx = ctx.getRelativeContext(ptr);
              return evalTest(localCtx, field.getVarValue("test"));
          }
          return false;
      }
  
  
      /**
       *  Evaluate the xpath expression for the field context
       *
       *@param  ctx   The field context
       *@param  path  The xpath to evaluate
       *@return       Description of the Return Value
       */
      private boolean evalTest(JXPathContext ctx, String path) {
          Boolean passed = (Boolean) ctx.getValue(path, Boolean.class);
          return passed.booleanValue();
      }
  
  }
  
  
  
  
  1.18      +4 -3      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.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- ValidatorTestSuite.java	8 Jun 2004 14:48:35 -0000	1.17
  +++ ValidatorTestSuite.java	8 Jun 2004 15:05:38 -0000	1.18
  @@ -61,6 +61,7 @@
          suite.addTestSuite(FlagsTest.class);
          suite.addTestSuite(ExceptionTest.class);
          suite.addTest(UrlTest.suite());
  +       suite.addTest(JXPathTest.suite());
          
          return suite;
       }
  
  
  
  1.1                  jakarta-commons/validator/src/test/org/apache/commons/validator/JXPathTest.java
  
  Index: JXPathTest.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/JXPathTest.java,v
1.1 2004/06/08 15:05:38 husted Exp $
   * $Revision: 1.1 $
   * $Date: 2004/06/08 15:05:38 $
   *
   * ====================================================================
   * Copyright 2001-2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package org.apache.commons.validator;
  
  import java.io.IOException;
  
  import junit.framework.Test;
  import junit.framework.TestSuite;
  
  import org.xml.sax.SAXException;
  
  /**                                                       
   * Performs Validation Test for jxpath validations.
   */                                                       
  public class JXPathTest extends TestCommon {
  
     /**
      * The key used to retrieve the validator action.
      */
     protected static String ACTION = "jxpath";
  
  
     public JXPathTest(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[] {JXPathTest.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(JXPathTest.class);
     }
  
     /**
      * Load <code>ValidatorResources</code> from 
      * validator-jxpath.xml.
      */
     protected void setUp() throws IOException, SAXException {
        loadResources("validator-jxpath.xml");
     }
  
     protected void tearDown() {
     }
  
     /**
      * Tests the jxpath validation.
      */
     public void testJXPath() throws ValidatorException {
        // Create bean to run test on.
        ValueBean info = new ValueBean();
  
        info.setValue("foo");
        valueTest(info, "jxpathForm", true);
        info.setValue("foobar");
        valueTest(info, "jxpathForm", false);
     }
     
     /**
      * Tests jxpath validation test for a numeric range
      */
     public void testJXPath_numeric() throws ValidatorException {
        // Create bean to run test on.
        ValueBean info = new ValueBean();
  
        info.setValue("12");
        valueTest(info, "jxpathForm-numeric",true);
        info.setValue("123");
        valueTest(info, "jxpathForm-numeric",false);
        info.setValue("foobar");
        valueTest(info, "jxpathForm-numeric",false);
     }
     
     /**
      * Tests jxpath validation on nested objects
      */
     public void testJXPath_nested() throws ValidatorException {
        // Create bean to run test on.
        ObjectBean info = new ObjectBean();
        ValueBean kid = new ValueBean();
        kid.setValue("foo");
        info.setObject(kid);
        
        objectTest(info, "jxpathForm-nested",true);
        kid.setValue("foobar");
        objectTest(info, "jxpathForm-nested",false);
     }
  
  
  
     /**
      * Utlity class to run a test on a value.
      *
      * @param info	Value to run test on.
      * @param passed	Whether or not the test is expected to pass.
      */
     private void valueTest(ValueBean info, String form, boolean passed) throws ValidatorException
{
        // Construct validator based on the loaded resources 
        // and the form key
        Validator validator = new Validator(resources, form);
        // add the name bean to the validator as a resource 
        // for the validations to be performed on.
        validator.setParameter(Validator.BEAN_PARAM, info);
  
        // 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 result = results.getValidatorResult("value");
  
        assertNotNull(ACTION + " value ValidatorResult should not be null.", result);
        assertTrue("Value "+info.getValue()+" ValidatorResult should contain the '" + ACTION
+"' action.", result.containsAction(ACTION));
        assertTrue("Value "+info.getValue()+"ValidatorResult for the '" + ACTION +"' action
should have " + (passed ? "passed" : "failed") + ".", (passed ? result.isValid(ACTION) : !result.isValid(ACTION)));
      }
      
      /**
      * Utlity class to run a test on an object.
      *
      * @param info	Object to run test on.
      * @param passed	Whether or not the test is expected to pass.
      */
     private void objectTest(ObjectBean info, String form, boolean passed) throws ValidatorException
{
        // Construct validator based on the loaded resources 
        // and the form key
        Validator validator = new Validator(resources, form);
        // add the name bean to the validator as a resource 
        // for the validations to be performed on.
        validator.setParameter(Validator.BEAN_PARAM, info);
  
        // 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 result = results.getValidatorResult("object");
  
        assertNotNull(ACTION + " object ValidatorResult should not be null.", result);
        assertTrue("Object "+info.getObject()+" ValidatorResult should contain the '" + ACTION
+"' action.", result.containsAction(ACTION));
        assertTrue("Object "+info.getObject()+"ValidatorResult for the '" + ACTION +"' action
should have " + (passed ? "passed" : "failed") + ".", (passed ? result.isValid(ACTION) : !result.isValid(ACTION)));
      }
  }                                                         
  
  
  
  1.1                  jakarta-commons/validator/src/test/org/apache/commons/validator/ObjectBean.java
  
  Index: ObjectBean.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/ObjectBean.java,v
1.1 2004/06/08 15:05:38 husted Exp $
   * $Revision: 1.1 $
   * $Date: 2004/06/08 15:05:38 $
   *
   * ====================================================================
   * Copyright 2000-2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package org.apache.commons.validator;
  
  /**                                                       
   * Object object for storing a value to run tests on. 
   */                                                       
  public class ObjectBean {
     
     protected Object object = null;
     
     
     /**
      * Gets the object.
      */
     public Object getObject() {
        return object;	
     }
  
     /**
      * Sets the object.
      */
     public void setObject(Object object) {
        this.object = object;	
     }
        
  }                                                         
  
  
  1.1                  jakarta-commons/validator/src/test/org/apache/commons/validator/validator-jxpath.xml
  
  Index: validator-jxpath.xml
  ===================================================================
  <!DOCTYPE form-validation PUBLIC
       "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1//EN"
       "http://jakarta.apache.org/commons/dtds/validator_1_1.dtd">
       
  <form-validation>
     <global>
        <validator name="jxpath"
                   classname="org.apache.commons.validator.JXPathValidator"
                   method="isValid"
                   methodParams="java.lang.Object,org.apache.commons.validator.Field"
                   msg=""/>
     </global>
     <formset>
        <form name="jxpathForm">
           <field property="value" depends="jxpath">
              <var>
               <var-name>test</var-name>
               <var-value>.="foo"</var-value>
              </var>
           </field> 
        </form>
        <form name="jxpathForm-numeric">
           <field property="value" depends="jxpath">
              <var>
               <var-name>test</var-name>
               <var-value>number(.)>0 and number(.)&lt;100</var-value>
              </var>
           </field> 
        </form>
        <form name="jxpathForm-nested">
           <field property="object" depends="jxpath">
              <var>
               <var-name>test</var-name>
               <var-value>./value="foo"</var-value>
              </var>
           </field> 
        </form>
     </formset>   
  </form-validation>
  
  
  

---------------------------------------------------------------------
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