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 2003/01/20 07:15:06 UTC

cvs commit: jakarta-commons/validator/src/test/org/apache/commons/validator LocaleTest.java validator-locale.xml ValidatorTestSuite.java

turner      2003/01/19 22:15:06

  Modified:    validator/src/share/org/apache/commons/validator
                        ValidatorResources.java
                        ValidatorResourcesInitializer.java
               validator/src/test/org/apache/commons/validator
                        ValidatorTestSuite.java
  Added:       validator/conf/share validator_1_0_1.dtd
               validator/src/test/org/apache/commons/validator
                        LocaleTest.java validator-locale.xml
  Log:
  Fixes bug 16073.
  
  Makes variant work for formsets, and adds it back into the DTD.
  
  Moves a few info statements to debug statements to clear up log clutter
  
  Revision  Changes    Path
  1.1                  jakarta-commons/validator/conf/share/validator_1_0_1.dtd
  
  Index: validator_1_0_1.dtd
  ===================================================================
  <!--
      DTD for the Validator Rules Configuration File, Version 1.0
  
      To allow for XML validation of your rules configuration
      file, include the following DOCTYPE element at the beginning (after
      the "xml" declaration):
  
      <!DOCTYPE form-validation PUBLIC
       "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0.1//EN"
       "http://jakarta.apache.org/commons/dtds/validator_1_0_1.dtd">
  
      $Id: validator_1_0_1.dtd,v 1.1 2003/01/20 06:15:06 turner Exp $
  -->
  
  
  <!-- =================================================== Top Level Elements -->
  
  
  <!--
       The "form-validation" element is the root of the configuration file
       hierarchy, and contains nested elements for all of the other
       configuration settings.
  -->
  <!ELEMENT form-validation (global*, formset*)>
  
  
  <!--
      The elements defined here are all global and must be nested within a
      "global" element.
  -->
  <!ELEMENT global (validator*, constant*)>
  
  
  <!--
       The "validator" element defines what validators objects can be used with
       the fields referenced by the formset elements. The validator element can
       accept 4 properties: name, classname, method, methodparams, msg, depends,
       and jsFunctionName.
  -->
  <!ELEMENT validator (javascript?)>
  <!ATTLIST validator name           CDATA #REQUIRED
                      classname      CDATA #REQUIRED
                      method         CDATA #REQUIRED
                      methodParams   CDATA #REQUIRED
                      msg            CDATA #REQUIRED
                      depends        CDATA #IMPLIED
                      jsFunctionName CDATA #IMPLIED >
  
  
  <!--
       The "javascript" element defines a JavaScript that can be used to perform
       client-side validators.
  -->
  <!ELEMENT javascript (#PCDATA)>
  
  
  <!--
       The "constant" element defines a static value that can be used as
       replacement parameters within "field" elements. The "constant-name" and
       "constant-value" elements define the constant's reference id and replacement
       value.
  -->
  <!ELEMENT constant (constant-name, constant-value)>
  <!ELEMENT constant-name  (#PCDATA)>
  <!ELEMENT constant-value (#PCDATA)>
  
  
  <!--
        The "formset" element defines a set of forms for a locale. Formsets for
        specific locales can override only those fields that change. The
        localization is properly scoped, so that a formset can override just the
        language, or just the country, or both.
  -->
  <!ELEMENT formset (constant*, form+)>
  <!ATTLIST formset language     CDATA #IMPLIED
                    country      CDATA #IMPLIED 
                    variant      CDATA #IMPLIED >
  
  
  <!--
       The "form" element defines a set of fields to be validated. The name
       corresponds to the identifer the application assigns to the form. 
  -->
  <!ELEMENT form    (field+ )>
  <!ATTLIST form    name         CDATA #REQUIRED>
  
  
  <!--
       The "field" element defines the properties to be validated. In a
       web application, a field would also correspond to a control on
       a HTML form. To validate the properties, the validator works through
       a JavaBean representation. The field element can accept up to 4 
       attributes:
  
       property        The property on the JavaBean corresponding to this
                       field element.
  
       depends         The comma-delimited list of validators to apply against
                       this field. For the field to succeed, all the
                       validators must succeed.
  
       page            The JavaBean corresponding to this form may include
                       a page property. Only fields with a "page" attribute
                       value that is equal to or less than the page property
                       on the form JavaBean are processed. This is useful when
                       using a "wizard" approach to completing a large form,
                       to ensure that a page is not skipped.
                       [0]
  
       indexedListProperty
                       The "indexedListProperty" is the method name that will
                       return an array or a Collection used to retrieve the
                       list and then loop through the list performing the
                       validations for this field.
  
  -->
  <!ELEMENT field   (msg|arg0|arg1|arg2|arg3|var)*>
  <!ATTLIST field   property CDATA #REQUIRED
                    depends  CDATA #IMPLIED
                    page     CDATA #IMPLIED
                    indexedListProperty CDATA #IMPLIED >
  
  
  <!--
       The "msg" element defines a custom message key to use when one of the
       validators for this field fails. Each validator has a default message
       property that is used when a corresonding field msg is not specified.
       Each validator applied to a field may have its own msg element.
       The msg element accepts up to three attributes.
  
        name        The name of the validator corresponding to this msg.
  
        key         The key that will return the message template from a
                    resource bundle.
  
        resource    If set to "false", the key is taken to be a literal
                    value rather than a bundle key.
                    [true]
  -->
  <!ELEMENT msg     EMPTY>
  <!ATTLIST msg     name     CDATA #IMPLIED
                    key      CDATA #IMPLIED
                    resource CDATA #IMPLIED >
  
  
  <!--
       The "arg0" element defines the first replacement value to use with the
       message template for this validator or this field.
       The arg0 element accepts up to three attributes.
  
        name        The name of the validator corresponding to this msg.
  
        key         The key that will return the message template from a
                    resource bundle.
  
        resource    If set to "false", the key is taken to be a literal
                    value rather than a bundle key.
                    [true]
  -->
  <!ELEMENT arg0    EMPTY>
  <!ATTLIST arg0    name     CDATA #IMPLIED
                    key      CDATA #IMPLIED
                    resource CDATA #IMPLIED >
  
  
  <!--
       The "arg1" element defines the second replacement value to use with the
       message template for this validator or this field.
       The arg1 element accepts up to three attributes.
  
        name        The name of the validator corresponding to this msg.
  
        key         The key that will return the message template from a
                    resource bundle.
  
        resource    If set to "false", the key is taken to be a literal
                    value rather than a bundle key.
                    [true]
  -->
  <!ELEMENT arg1    EMPTY>
  <!ATTLIST arg1    name     CDATA #IMPLIED
                    key      CDATA #IMPLIED
                    resource CDATA #IMPLIED >
  
  
  <!--
       The "arg2" element defines the third replacement value to use with the
       message template for this validator or this field.
       The arg2 element accepts up to three attributes.
  
        name        The name of the validator corresponding to this msg.
  
        key         The key that will return the message template from a
                    resource bundle.
  
        resource    If set to "false", the key is taken to be a literal
                    value rather than a bundle key.
                    [true]
  -->
  <!ELEMENT arg2    EMPTY>
  <!ATTLIST arg2    name     CDATA #IMPLIED
                    key      CDATA #IMPLIED
                    resource CDATA #IMPLIED >
  
  <!--
       The "arg3" element defines the fourth replacement value to use with the
       message template for this validator or this field.
       The arg0 element accepts up to three attributes.
  
        name        The name of the validator corresponding to this msg.
  
        key         The key that will return the message template from a
                    resource bundle.
  
        resource    If set to "false", the key is taken to be a literal
                    value rather than a bundle key.
                    [true]
  -->
  <!ELEMENT arg3    EMPTY>
  <!ATTLIST arg3    name     CDATA #IMPLIED
                    key      CDATA #IMPLIED
                    resource CDATA #IMPLIED >
  
  
  <!--
       The "var" element can set parameters that a field may need to pass to
       one of its validators, such as the minimum and maximum values in a
       range validation. These parameters may also be referenced by one of the
       arg? elements using a shell syntax: ${var:var-name}.
  -->
  <!ELEMENT var (var-name, var-value)>
  
  
  
  <!--
       The name of the var parameter to provide to a field's validators.
  -->
  <!ELEMENT var-name  (#PCDATA)>
  
  
  
  <!--
       The value of the var parameter to provide to a field's validators.
  -->
  <!ELEMENT var-value (#PCDATA)>
  
  <!-- eof -->
  
  
  
  1.10      +23 -13    jakarta-commons/validator/src/share/org/apache/commons/validator/ValidatorResources.java
  
  Index: ValidatorResources.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/ValidatorResources.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- ValidatorResources.java	15 Dec 2002 21:46:41 -0000	1.9
  +++ ValidatorResources.java	20 Jan 2003 06:15:06 -0000	1.10
  @@ -131,8 +131,8 @@
   		formsets.add(fs);
   	    }
   
  -          if (log.isInfoEnabled()) {
  -	      log.info("Adding FormSet '" + fs.toString() + "'.");
  +          if (log.isDebugEnabled()) {
  +	      log.debug("Adding FormSet '" + fs.toString() + "'.");
   	   }
      	}
      }
  @@ -147,8 +147,8 @@
            hConstants.put(c.getName(), c.getValue());
         }
         
  -      if (log.isInfoEnabled()) {
  -         log.info("Add Global Constant: " + c.getName() + "," + c.getValue());
  +      if (log.isDebugEnabled()) {
  +         log.debug("Add Global Constant: " + c.getName() + "," + c.getValue());
         }
      }
   
  @@ -161,8 +161,8 @@
            hConstants.put(name, value);
         }
         
  -      if (log.isInfoEnabled()) {
  -         log.info("Add Global Constant: " + name + "," + value);
  +      if (log.isDebugEnabled()) {
  +         log.debug("Add Global Constant: " + name + "," + value);
         }
      }
       
  @@ -181,8 +181,8 @@
   
            hActions.put(va.getName(), va);
            
  -         if (log.isInfoEnabled()) {
  -            log.info("Add ValidatorAction: " + va.getName() + "," + va.getClassname());   
  +         if (log.isDebugEnabled()) {
  +            log.debug("Add ValidatorAction: " + va.getName() + "," + va.getClassname());   
            }
         }
      }
  @@ -378,7 +378,7 @@
         if (!GenericValidator.isBlankOrNull(language) && 
             !GenericValidator.isBlankOrNull(country) && 
             !GenericValidator.isBlankOrNull(variant)) {
  -         Form form = get(language, country, null, formKey);
  +         Form form = get(language, country, variant, formKey);
   
            if (form.getFieldMap().containsKey(fieldKey)) {
               field = (Field)form.getFieldMap().get(fieldKey);
  @@ -388,6 +388,16 @@
         if (field == null) {
            if (!GenericValidator.isBlankOrNull(language) && 
                !GenericValidator.isBlankOrNull(country)) {
  +            Form form = get(language, country, null, formKey);
  +         
  +            if (form.getFieldMap().containsKey(fieldKey)) {
  +               field = (Field)form.getFieldMap().get(fieldKey);
  +            }
  +         }
  +      }  
  +
  +      if (field == null) {
  +         if (!GenericValidator.isBlankOrNull(language)) {
               Form form = get(language, null, null, formKey);
            
               if (form.getFieldMap().containsKey(fieldKey)) {
  
  
  
  1.11      +6 -6      jakarta-commons/validator/src/share/org/apache/commons/validator/ValidatorResourcesInitializer.java
  
  Index: ValidatorResourcesInitializer.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/ValidatorResourcesInitializer.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- ValidatorResourcesInitializer.java	15 Dec 2002 21:46:41 -0000	1.10
  +++ ValidatorResourcesInitializer.java	20 Jan 2003 06:15:06 -0000	1.11
  @@ -94,8 +94,8 @@
        * <strong>MUST</strong> be an even number of Strings in this list!
        */
       protected static String registrations[] = {
  -        "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0//EN",
  -	"/org/apache/commons/validator/resources/validator_1_0.dtd"
  +        "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0.1//EN",
  +	"/org/apache/commons/validator/resources/validator_1_0_1.dtd"
      };
   
   
  
  
  
  1.6       +5 -4      jakarta-commons/validator/src/test/org/apache/commons/validator/ValidatorTestSuite.java
  
  Index: ValidatorTestSuite.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/ValidatorTestSuite.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ValidatorTestSuite.java	24 Sep 2002 20:23:35 -0000	1.5
  +++ ValidatorTestSuite.java	20 Jan 2003 06:15:06 -0000	1.6
  @@ -96,6 +96,7 @@
          suite.addTest(TypeTest.suite());
          suite.addTest(EmailTest.suite());
          suite.addTest(ValidatorTest.suite());
  +       suite.addTest(LocaleTest.suite());
   
          return suite;
       }
  
  
  
  1.1                  jakarta-commons/validator/src/test/org/apache/commons/validator/LocaleTest.java
  
  Index: LocaleTest.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/LocaleTest.java,v 1.1 2003/01/20 06:15:06 turner Exp $
   * $Revision: 1.1 $
   * $Date: 2003/01/20 06:15:06 $
   *
   * ====================================================================
   *
   * 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.LogFactory;
  import java.util.Locale;
                                                            
  /**                                                       
   * <p>Performs Validation Test for <code>long</code> validations.</p> 
   *
   * @author David Winterfeldt
   * @version $Revision: 1.1 $ $Date: 2003/01/20 06:15:06 $
  */                                                       
  public class LocaleTest 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 = "required";
  
     
     /**
      * Commons Logging instance.
     */
     private Log log = LogFactory.getLog(this.getClass());
     
     /**
      * Resources used for validation tests.
     */
     private ValidatorResources resources = null;
     
     public LocaleTest(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[] {LocaleTest.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(LocaleTest.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-locale.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() {
     }
  
     /**
      * See what happens when we try to validate with a Locale, Country and variant
     */
     public void testLocale1() throws ValidatorException {
        // Create bean to run test on.
        NameBean name = new NameBean();
        name.setFirstName("");
        name.setLastName("");
        
        valueTest(name, new Locale("en", "US", "TEST1"), false, false);
     }
  
     /**
      * See what happens when we try to validate with a Locale, Country and variant
     */
     public void testLocale2() throws ValidatorException {
        // Create bean to run test on.
        NameBean name = new NameBean();
        name.setFirstName("");
        name.setLastName("");
        
        valueTest(name, new Locale("en", "US", "TEST2"), true, false);
     }
  
     /**
      * See what happens when we try to validate with a Locale, Country and variant
     */
     public void testLocale3() throws ValidatorException {
        // Create bean to run test on.
        NameBean name = new NameBean();
        name.setFirstName("");
        name.setLastName("");
        
        valueTest(name, new Locale("en", "UK"), false, true);
     }
  
     /**
      * 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(Object name, Locale loc, boolean firstGood, boolean lastGood) throws ValidatorException {
        // 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);
        validator.addResource(Validator.LOCALE_KEY, loc);
        // 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 resultlast = results.getValidatorResult("lastName");
        ValidatorResult resultfirst = results.getValidatorResult("firstName");
  
        if (firstGood) {
  	  assertNull(ACTION + " firstName ValidatorResult should be null.", resultfirst);
        } else {
  	  assertNotNull(ACTION + " firstName ValidatorResult should not be null.", resultfirst);
        }
        if (lastGood) {
  	  assertNull(ACTION + " lastName ValidatorResult should be null.", resultlast);
        } else {
  	  assertNotNull(ACTION + " lastName ValidatorResult should not be null.", resultlast);
        }
     }
  }
  
  
  
  1.1                  jakarta-commons/validator/src/test/org/apache/commons/validator/validator-locale.xml
  
  Index: validator-locale.xml
  ===================================================================
  <form-validation>
     <global>
        <validator name="required"
                   classname="org.apache.commons.validator.TestValidator"
                   method="validateRequired"
                   methodParams="java.lang.Object,org.apache.commons.validator.Field"/>
     </global>
     <formset>
        <form    name="nameForm" >
           <field    property="firstName"  depends="required">
           	     <arg0 key="nameForm.firstname.displayname"/>
           </field>    
           <field    property="lastName"
           	   depends="required">
           	     <arg0 key="nameForm.lastname.displayname"/>
           </field>
        </form>
     </formset>   
     <formset language="en" country="US" variant="TEST1">
        <form    name="nameForm" >
           <field    property="firstName"  depends="required">
           	     <arg0 key="nameForm.firstname.displayname"/>
           </field>    
           <field    property="lastName"
           	   depends="required">
           	     <arg0 key="nameForm.lastname.displayname"/>
           </field>
        </form>
     </formset>   
     <formset language="en" country="US" variant="TEST2">
        <form    name="nameForm" >
           <field    property="firstName"  depends="">
           	     <arg0 key="nameForm.firstname.displayname"/>
           </field>    
           <field    property="lastName"
           	   depends="required">
           	     <arg0 key="nameForm.lastname.displayname"/>
           </field>
        </form>
     </formset>   
     <formset language="en" country="UK">
        <form    name="nameForm" >
           <field    property="firstName"  depends="required">
           	     <arg0 key="nameForm.firstname.displayname"/>
           </field>    
           <field    property="lastName"
           	   depends="">
           	     <arg0 key="nameForm.lastname.displayname"/>
           </field>
        </form>
     </formset>   
  </form-validation>
  
  
  

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