commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nia...@apache.org
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 GMT
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 @@
     }
 
     /**
+     * <p>Checks if the value's adjusted length is less than or equal to the max.</p>
+     *
+     * @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);
+    }
+
+    /**
      * <p>Checks if the value's length is greater than or equal to the min.</p>
      *
      * @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);
+    }
+
+    /**
+     * <p>Checks if the value's length is greater than or equal to the min.</p>
+     *
+     * @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 @@
   <body>
 
     <release version="1.2.1 (alpha)" date="in SVN">
+      <action dev="niallp" type="fix" issue="37962" due-to="Alan Olmanson">
+         Fix min/max length validation for different line endings.
+      </action>
       <action dev="niallp" type="fix" issue="38051" due-to="Stephanie Codron">
          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


Mime
View raw message