Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@www.apache.org Received: (qmail 49743 invoked from network); 9 Jan 2006 06:47:55 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 9 Jan 2006 06:47:55 -0000 Received: (qmail 3519 invoked by uid 500); 9 Jan 2006 06:47:53 -0000 Delivered-To: apmail-jakarta-commons-dev-archive@jakarta.apache.org Received: (qmail 2971 invoked by uid 500); 9 Jan 2006 06:47:51 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 2960 invoked by uid 500); 9 Jan 2006 06:47:51 -0000 Received: (qmail 2957 invoked by uid 99); 9 Jan 2006 06:47:51 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 08 Jan 2006 22:47:51 -0800 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.29) with SMTP; Sun, 08 Jan 2006 22:47:50 -0800 Received: (qmail 49594 invoked by uid 65534); 9 Jan 2006 06:47:30 -0000 Message-ID: <20060109064730.49593.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r367226 - in /jakarta/commons/proper/validator/trunk: src/javascript/org/apache/commons/validator/javascript/ src/share/org/apache/commons/validator/ src/test/org/apache/commons/validator/ xdocs/ Date: Mon, 09 Jan 2006 06:47:29 -0000 To: commons-cvs@jakarta.apache.org From: niallp@apache.org X-Mailer: svnmailer-1.0.5 X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Author: niallp Date: Sun Jan 8 22:47:15 2006 New Revision: 367226 URL: http://svn.apache.org/viewcvs?rev=367226&view=rev Log: Fix for bug 37962 - Fix min/max length validation for different line endings - reported by Alan Olmanson Added: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/GenericValidatorTest.java (with props) Modified: jakarta/commons/proper/validator/trunk/src/javascript/org/apache/commons/validator/javascript/validateMaxLength.js jakarta/commons/proper/validator/trunk/src/javascript/org/apache/commons/validator/javascript/validateMinLength.js jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/GenericValidator.java jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/ValidatorTestSuite.java jakarta/commons/proper/validator/trunk/xdocs/changes.xml Modified: jakarta/commons/proper/validator/trunk/src/javascript/org/apache/commons/validator/javascript/validateMaxLength.js URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/validator/trunk/src/javascript/org/apache/commons/validator/javascript/validateMaxLength.js?rev=367226&r1=367225&r2=367226&view=diff ============================================================================== --- jakarta/commons/proper/validator/trunk/src/javascript/org/apache/commons/validator/javascript/validateMaxLength.js (original) +++ jakarta/commons/proper/validator/trunk/src/javascript/org/apache/commons/validator/javascript/validateMaxLength.js Sun Jan 8 22:47:15 2006 @@ -28,8 +28,29 @@ field.type == 'textarea') && field.disabled == false) { + /* Adjust length for carriage returns - see Bug 37962 */ + var lineEndLength = oMaxLength[x][2]("lineEndLength"); + var adjustAmount = 0; + if (lineEndLength) { + var rCount = 0; + var nCount = 0; + var crPos = 0; + while (crPos < field.value.length) { + var currChar = field.value.charAt(crPos); + if (currChar == '\r') { + rCount++; + } + if (currChar == '\n') { + nCount++; + } + crPos++; + } + var endLength = parseInt(lineEndLength); + adjustAmount = (nCount * endLength) - (rCount + nCount); + } + var iMax = parseInt(oMaxLength[x][2]("maxlength")); - if (field.value.length > iMax) { + if ((field.value.length + adjustAmount) > iMax) { if (i == 0) { focusField = field; } Modified: jakarta/commons/proper/validator/trunk/src/javascript/org/apache/commons/validator/javascript/validateMinLength.js URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/validator/trunk/src/javascript/org/apache/commons/validator/javascript/validateMinLength.js?rev=367226&r1=367225&r2=367226&view=diff ============================================================================== --- jakarta/commons/proper/validator/trunk/src/javascript/org/apache/commons/validator/javascript/validateMinLength.js (original) +++ jakarta/commons/proper/validator/trunk/src/javascript/org/apache/commons/validator/javascript/validateMinLength.js Sun Jan 8 22:47:15 2006 @@ -29,8 +29,29 @@ field.type == 'textarea') && field.disabled == false) { + /* Adjust length for carriage returns - see Bug 37962 */ + var lineEndLength = oMinLength[x][2]("lineEndLength"); + var adjustAmount = 0; + if (lineEndLength) { + var rCount = 0; + var nCount = 0; + var crPos = 0; + while (crPos < field.value.length) { + var currChar = field.value.charAt(crPos); + if (currChar == '\r') { + rCount++; + } + if (currChar == '\n') { + nCount++; + } + crPos++; + } + var endLength = parseInt(lineEndLength); + adjustAmount = (nCount * endLength) - (rCount + nCount); + } + var iMin = parseInt(oMinLength[x][2]("minlength")); - if ((trim(field.value).length > 0) && (field.value.length < iMin)) { + if ((trim(field.value).length > 0) && ((field.value.length + adjustAmount) < iMin)) { if (i == 0) { focusField = field; } Modified: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/GenericValidator.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/GenericValidator.java?rev=367226&r1=367225&r2=367226&view=diff ============================================================================== --- jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/GenericValidator.java (original) +++ jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/GenericValidator.java Sun Jan 8 22:47:15 2006 @@ -280,6 +280,19 @@ } /** + *

Checks if the value's adjusted length is less than or equal to the max.

+ * + * @param value The value validation is being performed on. + * @param max The maximum length. + * @param lineEndLength The length to use for line endings. + * @return true if the value's length is less than the specified maximum. + */ + public static boolean maxLength(String value, int max, int lineEndLength) { + int adjustAmount = adjustForLineEnding(value, lineEndLength); + return ((value.length() + adjustAmount) <= max); + } + + /** *

Checks if the value's length is greater than or equal to the min.

* * @param value The value validation is being performed on. @@ -288,6 +301,41 @@ */ public static boolean minLength(String value, int min) { return (value.length() >= min); + } + + /** + *

Checks if the value's length is greater than or equal to the min.

+ * + * @param value The value validation is being performed on. + * @param min The minimum length. + * @return true if the value's length is more than the specified minimum. + */ + public static boolean minLength(String value, int min, int lineEndLength) { + int adjustAmount = adjustForLineEnding(value, lineEndLength); + return ((value.length() + adjustAmount) >= min); + } + + /** + * Calculate an adjustment amount for line endings. + * + * See Bug 37962 for the rational behind this. + * + * @param value The value validation is being performed on. + * @param lineEndLength The length to use for line endings. + * @return the adjustment amount. + */ + private static int adjustForLineEnding(String value, int lineEndLength) { + int nCount = 0; + int rCount = 0; + for (int i = 0; i < value.length(); i++) { + if (value.charAt(i) == '\n') { + nCount++; + } + if (value.charAt(i) == '\r') { + rCount++; + } + } + return ((nCount * lineEndLength) - (rCount + nCount)); } // See http://issues.apache.org/bugzilla/show_bug.cgi?id=29015 WRT the "value" methods Added: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/GenericValidatorTest.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/GenericValidatorTest.java?rev=367226&view=auto ============================================================================== --- jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/GenericValidatorTest.java (added) +++ jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/GenericValidatorTest.java Sun Jan 8 22:47:15 2006 @@ -0,0 +1,80 @@ +/* + * $Id$ + * $Rev$ + * $Date$ + * + * ==================================================================== + * Copyright 2006 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 junit.framework.TestCase; + +/** + * Test the GenericValidator class. + */ +public class GenericValidatorTest extends TestCase { + + /** + * Constructor for GenericValidatorTest. + */ + public GenericValidatorTest(String name) { + super(name); + } + + public void testMinLength() { +System.out.println("NIALL"); + // Use 0 for line end length + assertTrue("Min=5 End=0", GenericValidator.minLength("12345\n\r", 5, 0)); + assertFalse("Min=6 End=0", GenericValidator.minLength("12345\n\r", 6, 0)); + assertFalse("Min=7 End=0", GenericValidator.minLength("12345\n\r", 7, 0)); + assertFalse("Min=8 End=0", GenericValidator.minLength("12345\n\r", 8, 0)); + + // Use 1 for line end length + assertTrue("Min=5 End=1", GenericValidator.minLength("12345\n\r", 5, 1)); + assertTrue("Min=6 End=1", GenericValidator.minLength("12345\n\r", 6, 1)); + assertFalse("Min=7 End=1", GenericValidator.minLength("12345\n\r", 7, 1)); + assertFalse("Min=8 End=1", GenericValidator.minLength("12345\n\r", 8, 1)); + + // Use 2 for line end length + assertTrue("Min=5 End=2", GenericValidator.minLength("12345\n\r", 5, 2)); + assertTrue("Min=6 End=2", GenericValidator.minLength("12345\n\r", 6, 2)); + assertTrue("Min=7 End=2", GenericValidator.minLength("12345\n\r", 7, 2)); + assertFalse("Min=8 End=2", GenericValidator.minLength("12345\n\r", 8, 2)); + } + + public void testMaxLength() { + + // Use 0 for line end length + assertFalse("Max=4 End=0", GenericValidator.maxLength("12345\n\r", 4, 0)); + assertTrue("Max=5 End=0", GenericValidator.maxLength("12345\n\r", 5, 0)); + assertTrue("Max=6 End=0", GenericValidator.maxLength("12345\n\r", 6, 0)); + assertTrue("Max=7 End=0", GenericValidator.maxLength("12345\n\r", 7, 0)); + + // Use 1 for line end length + assertFalse("Max=4 End=1", GenericValidator.maxLength("12345\n\r", 4, 1)); + assertFalse("Max=5 End=1", GenericValidator.maxLength("12345\n\r", 5, 1)); + assertTrue("Max=6 End=1", GenericValidator.maxLength("12345\n\r", 6, 1)); + assertTrue("Max=7 End=1", GenericValidator.maxLength("12345\n\r", 7, 1)); + + // Use 2 for line end length + assertFalse("Max=4 End=2", GenericValidator.maxLength("12345\n\r", 4, 2)); + assertFalse("Max=5 End=2", GenericValidator.maxLength("12345\n\r", 5, 2)); + assertFalse("Max=6 End=2", GenericValidator.maxLength("12345\n\r", 6, 2)); + assertTrue("Max=7 End=2", GenericValidator.maxLength("12345\n\r", 7, 2)); + } + +} Propchange: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/GenericValidatorTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/GenericValidatorTest.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Modified: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/ValidatorTestSuite.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/ValidatorTestSuite.java?rev=367226&r1=367225&r2=367226&view=diff ============================================================================== --- jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/ValidatorTestSuite.java (original) +++ jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/ValidatorTestSuite.java Sun Jan 8 22:47:15 2006 @@ -4,7 +4,7 @@ * $Date$ * * ==================================================================== - * Copyright 2000-2005 The Apache Software Foundation + * Copyright 2000-2006 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. @@ -66,6 +66,7 @@ suite.addTest(UrlTest.suite()); suite.addTest(ValidatorTest.suite()); suite.addTest(VarTest.suite()); + suite.addTestSuite(GenericValidatorTest.class); return suite; } Modified: jakarta/commons/proper/validator/trunk/xdocs/changes.xml URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/validator/trunk/xdocs/changes.xml?rev=367226&r1=367225&r2=367226&view=diff ============================================================================== --- jakarta/commons/proper/validator/trunk/xdocs/changes.xml (original) +++ jakarta/commons/proper/validator/trunk/xdocs/changes.xml Sun Jan 8 22:47:15 2006 @@ -39,6 +39,9 @@ + + Fix min/max length validation for different line endings. + Fix email validator to not allow spaces at the end of the user component or start of the domain component. --------------------------------------------------------------------- To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-dev-help@jakarta.apache.org