commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject svn commit: r1782740 - in /commons/proper/validator/trunk/src: main/java/org/apache/commons/validator/routines/CreditCardValidator.java test/java/org/apache/commons/validator/routines/CreditCardValidatorTest.java
Date Mon, 13 Feb 2017 10:55:52 GMT
Author: sebb
Date: Mon Feb 13 10:55:52 2017
New Revision: 1782740

URL: http://svn.apache.org/viewvc?rev=1782740&view=rev
Log:
VALIDATOR-415 Extend syntax to allow disjoint lengths

Modified:
    commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/CreditCardValidator.java
    commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/CreditCardValidatorTest.java

Modified: commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/CreditCardValidator.java
URL: http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/CreditCardValidator.java?rev=1782740&r1=1782739&r2=1782740&view=diff
==============================================================================
--- commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/CreditCardValidator.java
(original)
+++ commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/CreditCardValidator.java
Mon Feb 13 10:55:52 2017
@@ -64,6 +64,7 @@ import java.util.ArrayList;
  *        new CreditCardRange("3095", null, 14, 14), // Diners
  *        new CreditCardRange("36",   null, 14, 14), // Diners
  *        new CreditCardRange("38",   "39", 14, 14), // Diners
+ *        new CreditCardRange("4",    null, new int[] {13, 16}), // VISA
  *    }
  * );
  * </code>
@@ -88,13 +89,15 @@ public class CreditCardValidator impleme
     private static final int MAX_CC_LENGTH = 19; // maximum allowed length
 
     /**
-     * Class that represents a credit card range. 
+     * Class that represents a credit card range.
+     * @since 1.6
      */
     public static class CreditCardRange {
         final String low; // e.g. 34 or 644
         final String high; // e.g. 34 or 65
-        final int minLen; // e.g. 16
-        final int maxLen; // e.g. 19
+        final int minLen; // e.g. 16 or -1
+        final int maxLen; // e.g. 19 or -1
+        final int lengths[]; // e.g. 16,18,19
 
         /**
          * Create a credit card range specifier for use in validation
@@ -117,6 +120,30 @@ public class CreditCardValidator impleme
             this.high = high;
             this.minLen = minLen;
             this.maxLen = maxLen;
+            this.lengths = null;
+        }
+
+        /**
+         * Create a credit card range specifier for use in validation
+         * of the number syntax including the IIN range.
+         * <p>
+         * The low and high parameters may be shorter than the length
+         * of an IIN (currently 6 digits) in which case subsequent digits
+         * are ignored and may range from 0-9.
+         * <br>
+         * The low and high parameters may be different lengths.
+         * e.g. Discover "644" and "65".
+         * </p>
+         * @param low the low digits of the IIN range 
+         * @param high the high digits of the IIN range
+         * @param lengths array of valid lengths
+         */
+        public CreditCardRange(String low, String high, int [] lengths) {
+            this.low = low;
+            this.high = high;
+            this.minLen = -1;
+            this.maxLen = -1;
+            this.lengths = lengths.clone();
         }
     }
 
@@ -422,6 +449,19 @@ public class CreditCardValidator impleme
     }
 
     // package protected for unit test access
+    static boolean validLength(int valueLength, CreditCardRange range) {
+        if (range.lengths != null) {
+            for(int length : range.lengths) {
+                if (valueLength == length) {
+                    return true;
+                }
+            }
+            return false;
+        }
+        return valueLength >= range.minLen && valueLength <= range.maxLen;
+    }
+
+    // package protected for unit test access
     static CodeValidator createRangeValidator(final CreditCardRange[] creditCardRanges, final
CheckDigit digitCheck ) {
         return new CodeValidator(
                 // must be numeric (rest of validation is done later)
@@ -434,7 +474,7 @@ public class CreditCardValidator impleme
                         if (super.match(value) != null) {
                             int length = value.length();
                             for(CreditCardRange range : ccr) {
-                                if (length >= range.minLen && length <= range.maxLen)
{
+                                if (validLength(length, range)) {
                                     if (range.high == null) { // single prefix only
                                         if (value.startsWith(range.low)) {
                                             return value;

Modified: commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/CreditCardValidatorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/CreditCardValidatorTest.java?rev=1782740&r1=1782739&r2=1782740&view=diff
==============================================================================
--- commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/CreditCardValidatorTest.java
(original)
+++ commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/CreditCardValidatorTest.java
Mon Feb 13 10:55:52 2017
@@ -27,23 +27,23 @@ import org.apache.commons.validator.rout
  */
 public class CreditCardValidatorTest extends TestCase {
     
-    private static final String VALID_VISA = "4417123456789113";
-    private static final String ERROR_VISA = "4417123456789112";
-    private static final String VALID_SHORT_VISA = "4222222222222";
+    private static final String VALID_VISA       = "4417123456789113"; // 16
+    private static final String ERROR_VISA       = "4417123456789112"; 
+    private static final String VALID_SHORT_VISA = "4222222222222"; // 13
     private static final String ERROR_SHORT_VISA = "4222222222229";
-    private static final String VALID_AMEX = "378282246310005";
-    private static final String ERROR_AMEX = "378282246310001";
+    private static final String VALID_AMEX       = "378282246310005"; // 15
+    private static final String ERROR_AMEX       = "378282246310001";
     private static final String VALID_MASTERCARD = "5105105105105100";
     private static final String ERROR_MASTERCARD = "5105105105105105";
-    private static final String VALID_DISCOVER = "6011000990139424";
-    private static final String ERROR_DISCOVER = "6011000990139421";
+    private static final String VALID_DISCOVER   = "6011000990139424";
+    private static final String ERROR_DISCOVER   = "6011000990139421";
     private static final String VALID_DISCOVER65 = "6534567890123458"; // FIXME need verified
test data for Discover with "65" prefix
     private static final String ERROR_DISCOVER65 = "6534567890123450"; // FIXME need verified
test data for Discover with "65" prefix
-    private static final String VALID_DINERS = "30569309025904";
-    private static final String ERROR_DINERS = "30569309025901";
-    private static final String VALID_VPAY = "4370000000000061";
-    private static final String VALID_VPAY2 = "4370000000000012";
-    private static final String ERROR_VPAY = "4370000000000069";
+    private static final String VALID_DINERS     = "30569309025904"; // 14
+    private static final String ERROR_DINERS     = "30569309025901";
+    private static final String VALID_VPAY       = "4370000000000061"; // 16
+    private static final String VALID_VPAY2      = "4370000000000012";
+    private static final String ERROR_VPAY       = "4370000000000069";
 
     private static final String [] VALID_CARDS = {
         VALID_VISA,
@@ -606,4 +606,45 @@ public class CreditCardValidatorTest ext
             assertFalse(s, ccv.isValid(s));
         }
     }
+
+    public void testValidLength() {
+        assertTrue(CreditCardValidator.validLength(14, new CreditCardRange("", "", 14, 14)));
+        assertFalse(CreditCardValidator.validLength(15, new CreditCardRange("", "", 14, 14)));
+        assertFalse(CreditCardValidator.validLength(13, new CreditCardRange("", "", 14, 14)));
+
+        assertFalse(CreditCardValidator.validLength(14, new CreditCardRange("", "", 15, 17)));
+        assertTrue(CreditCardValidator.validLength(15, new CreditCardRange("", "", 15, 17)));
+        assertTrue(CreditCardValidator.validLength(16, new CreditCardRange("", "", 15, 17)));
+        assertTrue(CreditCardValidator.validLength(17, new CreditCardRange("", "", 15, 17)));
+        assertFalse(CreditCardValidator.validLength(18, new CreditCardRange("", "", 15, 17)));
+
+        assertFalse(CreditCardValidator.validLength(14, new CreditCardRange("", "", new int[]{15,
17})));
+        assertTrue(CreditCardValidator.validLength(15, new CreditCardRange("", "", new int[]{15,
17})));
+        assertFalse(CreditCardValidator.validLength(16, new CreditCardRange("", "", new int[]{15,
17})));
+        assertTrue(CreditCardValidator.validLength(17, new CreditCardRange("", "", new int[]{15,
17})));
+        assertFalse(CreditCardValidator.validLength(18, new CreditCardRange("", "", new int[]{15,
17})));
+    }
+
+    public void testDisjointRange() {
+        CreditCardValidator ccv = new CreditCardValidator(
+            new CreditCardRange[]{
+                    new CreditCardRange("305", "4", new int[]{13, 16}),
+                }
+            );
+        assertEquals(13, VALID_SHORT_VISA.length());
+        assertEquals(16, VALID_VISA.length());
+        assertEquals(14, VALID_DINERS.length());
+        assertTrue(ccv.isValid(VALID_SHORT_VISA));
+        assertTrue(ccv.isValid(VALID_VISA));
+        assertFalse(ccv.isValid(ERROR_SHORT_VISA));
+        assertFalse(ccv.isValid(ERROR_VISA));
+        assertFalse(ccv.isValid(VALID_DINERS));
+        ccv = new CreditCardValidator(
+            new CreditCardRange[]{
+                    // add 14 as a valid length
+                    new CreditCardRange("305", "4", new int[]{13, 14, 16}),
+                }
+            );
+        assertTrue(ccv.isValid(VALID_DINERS));
+    }
 }



Mime
View raw message