rleland 2004/04/04 06:53:25 Modified: validator build.properties.sample build.xml project.xml validator/src/share/org/apache/commons/validator EmailValidator.java Form.java FormSet.java ValidatorResources.java ValidatorResourcesInitializer.java validator/src/test/org/apache/commons/validator ValidatorTestSuite.java Added: validator/conf/share validator_1_2_0.dtd validator/src/test/org/apache/commons/validator ExtensionTest.java validator-extension.xml Log: Bug 27870 Patch For Form inheritance provided by Nacho G. Mac Dowell Since this is a major functionality change though amount of code was small Validator version rev changed to 1.2.0-dev Revision Changes Path 1.12 +7 -7 jakarta-commons/validator/build.properties.sample Index: build.properties.sample =================================================================== RCS file: /home/cvs/jakarta-commons/validator/build.properties.sample,v retrieving revision 1.11 retrieving revision 1.12 diff -u -r1.11 -r1.12 --- build.properties.sample 29 Feb 2004 09:48:28 -0000 1.11 +++ build.properties.sample 4 Apr 2004 13:53:25 -0000 1.12 @@ -37,16 +37,16 @@ # from the Jakarta Commons project. commons-logging.jar=../logging/dist/commons-logging.jar -# The JAR file containing version 2.07 (or later) of +# The JAR file containing version 2.08 (or later) of # the the Jakarta ORO project. -oro.jar=${apache.dir}/jakarta-oro-2.0.7/jakarta-oro-2.0.7.jar +oro.jar=${apache.dir}/jakarta-oro-2.0.8/jakarta-oro-2.0.8.jar # The JAR file containing version 3.8.1 (or later) of # JUnit Unit Testing (http://www.junit.org) junit.jar=D:/java/Lib/junit3.8.1/junit.jar -# Needed for building the Javadocs -xerces.jar=${apache.dir}/xerces-1_4_2/xerces.jar +# Needed for building the version Javadocs +xerces.jar=${apache.dir}/xerces-2_5_0/xercesImpl.jar # jsdoc 1.5.2 or Newer is needed for building the Javascript Javadocs jsdoc.home=/cygdrive/d/Projects/Sourceforge/JsDoc/JSDoc-1.5.2 1.34 +4 -2 jakarta-commons/validator/build.xml Index: build.xml =================================================================== RCS file: /home/cvs/jakarta-commons/validator/build.xml,v retrieving revision 1.33 retrieving revision 1.34 diff -u -r1.33 -r1.34 --- build.xml 30 Mar 2004 06:22:54 -0000 1.33 +++ build.xml 4 Apr 2004 13:53:25 -0000 1.34 @@ -67,7 +67,7 @@ <property name="component.title" value="Validator"/> <!-- The current version number of this component --> - <property name="component.version" value="1.1.3-dev"/> + <property name="component.version" value="1.2.0-dev"/> <!-- The base directory for compilation targets --> <property name="build.home" value="target"/> @@ -211,6 +211,8 @@ file="${conf.home}/validator_1_0_1.dtd"/> <copy todir="${build.home}/classes/org/apache/commons/validator/resources/" file="${conf.home}/validator_1_1.dtd"/> + <copy todir="${build.home}/classes/org/apache/commons/validator/resources/" + file="${conf.home}/validator_1_2_0.dtd"/> <copy todir="${build.home}/classes/"> <fileset dir="${javascript.home}" includes="**/*.js"/> 1.42 +7 -1 jakarta-commons/validator/project.xml Index: project.xml =================================================================== RCS file: /home/cvs/jakarta-commons/validator/project.xml,v retrieving revision 1.41 retrieving revision 1.42 diff -u -r1.41 -r1.42 --- project.xml 4 Apr 2004 07:33:52 -0000 1.41 +++ project.xml 4 Apr 2004 13:53:25 -0000 1.42 @@ -19,7 +19,7 @@ <extend>../commons-build/project.xml</extend> <name>Validator</name> <id>commons-validator</id> - <currentVersion>1.1.3-dev</currentVersion> + <currentVersion>1.2.0-dev</currentVersion> <inceptionYear>2002</inceptionYear> <package>org.apache.commons.validator</package> @@ -184,6 +184,12 @@ <email>[EMAIL PROTECTED]</email> <organization></organization> </contributor> + <contributor> + <name>Nacho G. Mac Dowell</name> + <id></id> + <email></email> + <organization></organization> + </contributor> </contributors> 1.1 jakarta-commons/validator/conf/share/validator_1_2_0.dtd Index: validator_1_2_0.dtd =================================================================== <!-- DTD for the Validator Rules Configuration File, Version 1.1.2 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.1.2//EN" "http://jakarta.apache.org/commons/dtds/validator_1_1_2.dtd"> $Id: validator_1_2_0.dtd,v 1.1 2004/04/04 13:53:25 rleland Exp $ --> <!-- 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 validator objects can be used with the fields referenced by the formset elements. elements: validator Defines a new validatior javascript The javascript source code for client side validation. attributes: name The name of this validation classname The java class name that handles server side validation method The java method that handles server side validation methodParams The java class types passed to the serverside method msg a generic message key to use when this validator fails. It can be overridden by the 'msg' element for a specific field. depends The comma-delimited list of validator that are called before this validator. For this validation to succeed, all the listed validators must succeed. jsFunctionName The name of the javascript function which returns all fields of a certain type. jsFunction The name of the javascript function which is passed the form for validation. --> <!ELEMENT validator (javascript?)> <!ATTLIST validator name CDATA #REQUIRED> <!ATTLIST validator classname CDATA #REQUIRED> <!ATTLIST validator method CDATA #REQUIRED> <!ATTLIST validator methodParams CDATA #REQUIRED> <!ATTLIST validator msg CDATA #REQUIRED> <!ATTLIST validator depends CDATA #IMPLIED> <!ATTLIST validator jsFunctionName CDATA #IMPLIED> <!ATTLIST validator jsFunction 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> <!ATTLIST formset country CDATA #IMPLIED> <!ATTLIST formset variant CDATA #IMPLIED> <!-- The "form" element defines a set of fields to be validated. The name corresponds to the identifier the application assigns to the form. --> <!ELEMENT form (field+)> <!ATTLIST form name CDATA #REQUIRED extends CDATA #IMPLIED> <!-- 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 accepts these 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|arg|arg0|arg1|arg2|arg3|var)*> <!ATTLIST field property CDATA #REQUIRED> <!ATTLIST field depends CDATA #IMPLIED> <!ATTLIST field page CDATA #IMPLIED> <!ATTLIST field 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 corresponding field msg is not specified. Each validator applied to a field may have its own msg element. The msg element accepts these attributes. name The name of the validator corresponding to this msg. bundle The resource bundle name that the key should be resolved in. 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 key CDATA #REQUIRED> <!ATTLIST msg name CDATA #REQUIRED> <!ATTLIST msg bundle CDATA #IMPLIED> <!ATTLIST msg resource CDATA #IMPLIED> <!-- The "arg" element defines a replacement value to use with the message template for this validator or this field. The arg element accepts these attributes. name The name of the validator corresponding to this msg. If not supplied, this argument will be used in the given position for every validator. bundle The resource bundle name that the key should be resolved in. 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] position The position of this replacement parameter in the message. For example, position="0" will set the first argument. [0] --> <!ELEMENT arg EMPTY> <!ATTLIST arg key CDATA #REQUIRED> <!ATTLIST arg bundle CDATA #IMPLIED> <!ATTLIST arg name CDATA #IMPLIED> <!ATTLIST arg resource CDATA #IMPLIED> <!ATTLIST arg position CDATA #IMPLIED> <!-- DEPRECATED Use <arg position="0"/> instead. The "arg0" element defines the first replacement value to use with the message template for this validator or this field. The arg0 element accepts these 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> <!ATTLIST arg0 key CDATA #IMPLIED> <!ATTLIST arg0 resource CDATA #IMPLIED> <!-- DEPRECATED Use <arg position="1"/> instead. The "arg1" element defines the second replacement value to use with the message template for this validator or this field. The arg1 element accepts these 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> <!ATTLIST arg1 key CDATA #IMPLIED> <!ATTLIST arg1 resource CDATA #IMPLIED> <!-- DEPRECATED Use <arg position="2"/> instead. The "arg2" element defines the third replacement value to use with the message template for this validator or this field. The arg2 element accepts these 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> <!ATTLIST arg2 key CDATA #IMPLIED> <!ATTLIST arg2 resource CDATA #IMPLIED> <!-- DEPRECATED Use <arg position="3"/> instead. The "arg3" element defines the fourth replacement value to use with the message template for this validator or this field. The arg0 element accepts these 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> <!ATTLIST arg3 key CDATA #IMPLIED> <!ATTLIST arg3 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)> 1.13 +24 -3 jakarta-commons/validator/src/share/org/apache/commons/validator/EmailValidator.java Index: EmailValidator.java =================================================================== RCS file: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/EmailValidator.java,v retrieving revision 1.12 retrieving revision 1.13 diff -u -r1.12 -r1.13 --- EmailValidator.java 21 Feb 2004 17:10:29 -0000 1.12 +++ EmailValidator.java 4 Apr 2004 13:53:25 -0000 1.13 @@ -88,6 +88,8 @@ return false; } + email = stripComments(email); + // Check the whole email address structure Perl5Util emailMatcher = new Perl5Util(); if (!emailMatcher.match(EMAIL_PATTERN, email)) { @@ -212,5 +214,24 @@ return true; } + /** + * Recursively remove comments, and replace with a single space. The simpler + * regexps in the Email Addressing FAQ are imperfect - they will miss escaped + * chars in atoms, for example. + * Derived From Mail::RFC822::Address + */ + protected String stripComments(String emailStr) { + String input = emailStr; + String result = emailStr; + String commentPat = "s/^((?:[^\"\\\\]|\\\\.)*(?:\"(?:[^\"\\\\]|\\\\.)*\"(?:[^\"\\\\]|\111111\\\\.)*)*)\\((?:[^()\\\\]|\\\\.)*\\)/$1 /osx"; + Perl5Util commentMatcher = new Perl5Util(); + result = commentMatcher.substitute(commentPat,input); + // This really needs to be =~ or Perl5Matcher comparison + while (!result.equals(input)) { + input = result; + result = commentMatcher.substitute(commentPat,input); + } + return result; + } } 1.15 +82 -4 jakarta-commons/validator/src/share/org/apache/commons/validator/Form.java Index: Form.java =================================================================== RCS file: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/Form.java,v retrieving revision 1.14 retrieving revision 1.15 diff -u -r1.14 -r1.15 --- Form.java 21 Feb 2004 17:10:29 -0000 1.14 +++ Form.java 4 Apr 2004 13:53:25 -0000 1.15 @@ -59,6 +59,17 @@ */ protected FastHashMap hFields = new FastHashMap(); + /** + * The name/key of the form which this form extends from. + */ + protected String inherit = null; + + /** + * Whether or not the this <code>Form</code> was processed + * for replacing variables in strings with their values. + */ + private boolean processed = false; + /** * Gets the name/key of the set of validation rules. */ @@ -126,8 +137,47 @@ Field f = (Field) i.next(); f.process(globalConstants, constants); } + + processed = true; } + /** + * Processes all of the <code>Form</code>'s <code>Field</code>s. + */ + protected void process(Map globalConstants, Map constants, Map forms) { + + if (isProcessed()) { + return; + } + int n = 0; //we want the fields from its parent first + if (isExtending()) { + Form parent = (Form) forms.get(inherit); + if (parent != null) { + if (!parent.isProcessed()) { + //we want to go all the way up the tree + parent.process(constants, globalConstants, forms); + } + for (Iterator i = parent.getFields().iterator(); i.hasNext();) { + Field f = (Field) i.next(); + //we want to be able to override any fields we like + if (hFields.get(f.getKey()) == null) { + lFields.add(n, f); + hFields.put(f.getKey(), f); + n++; + } + } + } + } + hFields.setFast(true); + //no need to reprocess parent's fields, we iterate from 'n' + for (Iterator i = lFields.listIterator(n); i.hasNext();) { + Field f = (Field) i.next(); + f.process(globalConstants, constants); + } + + processed = true; + } + /** * Returns a string representation of the object. */ @@ -146,7 +196,7 @@ return results.toString(); } - + /** * Validate all Fields in this Form on the given page and below. * @param params A Map of parameter class names to parameter values to pass @@ -175,4 +225,32 @@ return results; } + /** + * Whether or not the this <code>Form</code> was processed + * for replacing variables in strings with their values. + */ + public boolean isProcessed() { + return processed; + } + + /** + * Gets the name/key of the parent set of validation rules. + */ + public String getExtends() { + return inherit; + } + + /** + * Sets the name/key of the parent set of validation rules. + */ + public void setExtends(String string) { + inherit = string; + } + + /** + * Get extends flag. + */ + public boolean isExtending() { + return inherit != null; + } } 1.16 +4 -4 jakarta-commons/validator/src/share/org/apache/commons/validator/FormSet.java Index: FormSet.java =================================================================== RCS file: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/FormSet.java,v retrieving revision 1.15 retrieving revision 1.16 diff -u -r1.15 -r1.16 --- FormSet.java 21 Feb 2004 17:10:29 -0000 1.15 +++ FormSet.java 4 Apr 2004 13:53:25 -0000 1.16 @@ -186,7 +186,7 @@ public synchronized void process(Map globalConstants) { for (Iterator i = forms.values().iterator(); i.hasNext();) { Form f = (Form) i.next(); - f.process(globalConstants, constants); + f.process(globalConstants, constants, forms); } processed = true; 1.31 +6 -4 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.30 retrieving revision 1.31 diff -u -r1.30 -r1.31 --- ValidatorResources.java 21 Feb 2004 17:10:29 -0000 1.30 +++ ValidatorResources.java 4 Apr 2004 13:53:25 -0000 1.31 @@ -64,7 +64,9 @@ "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0.1//EN", "/org/apache/commons/validator/resources/validator_1_0_1.dtd", "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1//EN", - "/org/apache/commons/validator/resources/validator_1_1.dtd" + "/org/apache/commons/validator/resources/validator_1_1.dtd", + "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.2.0//EN", + "/org/apache/commons/validator/resources/validator_1_2_0.dtd" }; /** 1.23 +6 -4 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.22 retrieving revision 1.23 diff -u -r1.22 -r1.23 --- ValidatorResourcesInitializer.java 21 Feb 2004 17:10:29 -0000 1.22 +++ ValidatorResourcesInitializer.java 4 Apr 2004 13:53:25 -0000 1.23 @@ -59,7 +59,9 @@ "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0.1//EN", "/org/apache/commons/validator/resources/validator_1_0_1.dtd", "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1//EN", - "/org/apache/commons/validator/resources/validator_1_1.dtd" + "/org/apache/commons/validator/resources/validator_1_1.dtd", + "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.2.0//EN", + "/org/apache/commons/validator/resources/validator_1_2_0.dtd" }; 1.16 +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.15 retrieving revision 1.16 diff -u -r1.15 -r1.16 --- ValidatorTestSuite.java 21 Feb 2004 17:10:30 -0000 1.15 +++ ValidatorTestSuite.java 4 Apr 2004 13:53:25 -0000 1.16 @@ -51,6 +51,7 @@ suite.addTest(FloatTest.suite()); suite.addTest(DoubleTest.suite()); suite.addTest(TypeTest.suite()); + suite.addTest(ExtensionTest.suite()); suite.addTest(EmailTest.suite()); suite.addTestSuite(CreditCardValidatorTest.class); suite.addTest(ValidatorTest.suite()); 1.1 jakarta-commons/validator/src/test/org/apache/commons/validator/ExtensionTest.java Index: ExtensionTest.java =================================================================== /* * $Header: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/ExtensionTest.java,v 1.1 2004/04/04 13:53:25 rleland Exp $ * $Revision: 1.1 $ * $Date: 2004/04/04 13:53:25 $ * * ==================================================================== * 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.InputStream; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * <p>Performs tests for extension in form definitions. Performs the same tests * RequiredNameTest does but with an equivalent validation definition with extension * definitions (validator-extension.xml), plus an extra check on overriding rules and * another one checking it mantains correct order when extending.</p> */ public class ExtensionTest 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 set of validation * rules from the xml file. */ protected static String FORM_KEY2 = "nameForm2"; /** * The key used to retrieve the set of validation * rules from the xml file. */ protected static String CHECK_MSG_KEY = "nameForm.lastname.displayname"; /** * 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; /** * Constructor de ExtensionTest. * @param arg0 */ public ExtensionTest(String arg0) { super(arg0); } /** * Start the tests. * * @param theArgs the arguments. Not used */ public static void main(String[] theArgs) { junit.awtui.TestRunner.main(new String[] {RequiredNameTest.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(ExtensionTest.class); } /** * Load <code>ValidatorResources</code> from * validator-extension.xml. */ protected void setUp() throws Exception { // Load resources InputStream in = null; try { in = this.getClass().getResourceAsStream("validator-extension.xml"); resources = new ValidatorResources(in); } catch (Exception e) { log.error(e.getMessage(), e); throw e; } finally { if (in != null) { try { in.close(); } catch (Exception e) {} } } } protected void tearDown() { } /** * Tests the required validation failure. */ 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.setParameter(Validator.BEAN_PARAM, 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 failed.", !firstNameResult.isValid(ACTION)); assertNotNull("First 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 first name if it is blank. */ public void testRequiredFirstNameBlank() throws ValidatorException { // Create bean to run test on. NameBean name = new NameBean(); name.setFirstName(""); // 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.setParameter(Validator.BEAN_PARAM, 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("First 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 first name. */ public void testRequiredFirstName() throws ValidatorException { // Create bean to run test on. NameBean name = new NameBean(); name.setFirstName("Joe"); // 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.setParameter(Validator.BEAN_PARAM, 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("First 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 if it is blank. */ public void testRequiredLastNameBlank() throws ValidatorException { // Create bean to run test on. NameBean name = new NameBean(); 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.setParameter(Validator.BEAN_PARAM, 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("First 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.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.setParameter(Validator.BEAN_PARAM, 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("First 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 and last name. */ public void testRequiredName() 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.setParameter(Validator.BEAN_PARAM, 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 if we can override a rule. We "can" override a rule if the message shown * when the firstName required test fails and the lastName test is null. */ public void testOverrideRule() throws ValidatorException { // Create bean to run test on. NameBean name = new NameBean(); name.setLastName("Smith"); // Construct validator based on the loaded resources // and the form key Validator validator = new Validator(resources, FORM_KEY2); // add the name bean to the validator as a resource // for the validations to be performed on. validator.setParameter(Validator.BEAN_PARAM, 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 for the '" + ACTION +"' action should have '" + CHECK_MSG_KEY + " as a key.", firstNameResult.field.getArg(0).getKey().equals(CHECK_MSG_KEY)); assertNull("Last Name ValidatorResult should be null.", lastNameResult); } /** * Tests if the order is mantained when extending a form. Parent form fields should * preceed self form fields, except if we override the rules. */ public void testOrder() throws ValidatorException { Form form = resources.getForm(ValidatorResources.defaultLocale, FORM_KEY); Form form2 = resources.getForm(ValidatorResources.defaultLocale, FORM_KEY2); assertNotNull(FORM_KEY + " is null.", form); assertTrue("There should only be 2 fields in " + FORM_KEY, form.getFields().size() == 2); assertNotNull(FORM_KEY2 + " is null.", form2); assertTrue("There should only be 2 fields in " + FORM_KEY2, form2.getFields().size() == 2); //get the first field Field fieldFirstName = (Field)form.getFields().get(0); //get the second field Field fieldLastName = (Field)form.getFields().get(1); assertTrue("firstName in " + FORM_KEY + " should be the first in the list", fieldFirstName.getKey().equals("firstName")); assertTrue("lastName in " + FORM_KEY + " should be the first in the list", fieldLastName.getKey().equals("lastName")); // get the second field fieldLastName = (Field)form2.getFields().get(0); //get the first field fieldFirstName = (Field)form2.getFields().get(1); assertTrue("firstName in " + FORM_KEY2 + " should be the first in the list", fieldFirstName.getKey().equals("firstName")); assertTrue("lastName in " + FORM_KEY2 + " should be the first in the list", fieldLastName.getKey().equals("lastName")); } } 1.1 jakarta-commons/validator/src/test/org/apache/commons/validator/validator-extension.xml Index: validator-extension.xml =================================================================== <!DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.2.0//EN" "http://jakarta.apache.org/commons/dtds/validator_1_2_0.dtd"> <form-validation> <global> <validator name="required" classname="org.apache.commons.validator.TestValidator" method="validateRequired" methodParams="java.lang.Object,org.apache.commons.validator.Field" msg=""/> </global> <formset> <form name="nameForm" extends="baseForm"> <field property="lastName" depends="required"> <arg0 key="nameForm.lastname.displayname"/> </field> </form> <form name="nameForm2" extends="nameForm"> <!--override rule with no action (set to "" and Field will not find any tokens while iterating StringTokenizer(",") in the process method) --> <field property="lastName" depends=""/> <!--override rule with different message--> <field property="firstName" depends="required"> <arg0 key="nameForm.lastname.displayname"/> </field> </form> <form name="baseForm"> <field property="firstName" depends="required"> <arg0 key="nameForm.firstname.displayname"/> </field> </form> </formset> </form-validation>
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]