commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject svn commit: r1781815 - in /commons/proper/validator/trunk/src: changes/changes.xml main/java/org/apache/commons/validator/routines/CreditCardValidator.java test/java/org/apache/commons/validator/routines/CreditCardValidatorTest.java
Date Sun, 05 Feb 2017 21:58:38 GMT
Author: sebb
Date: Sun Feb  5 21:58:38 2017
New Revision: 1781815

URL: http://svn.apache.org/viewvc?rev=1781815&view=rev
Log:
VALIDATOR-413 Generic CreditCard validation

Modified:
    commons/proper/validator/trunk/src/changes/changes.xml
    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/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/changes/changes.xml?rev=1781815&r1=1781814&r2=1781815&view=diff
==============================================================================
--- commons/proper/validator/trunk/src/changes/changes.xml (original)
+++ commons/proper/validator/trunk/src/changes/changes.xml Sun Feb  5 21:58:38 2017
@@ -90,6 +90,9 @@ The dependencies for Validator have not
 For the current list of dependencies, please see
 http://commons.apache.org/validator/dependencies.html
   ">
+    <action issue="VALIDATOR-413" type="add" dev="sebb">
+    Generic CreditCard validation
+    </action>
     <action issue="VALIDATOR-379" type="fix" dev="sebb">
     CodeValidator unconditionally trim()s the input string - document the behaviour
     </action>

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=1781815&r1=1781814&r2=1781815&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
Sun Feb  5 21:58:38 2017
@@ -57,6 +57,10 @@ public class CreditCardValidator impleme
 
     private static final long serialVersionUID = 5955978921148959496L;
 
+    private static final int MIN_CC_LENGTH = 12; // minimum allowed length
+
+	private static final int MAX_CC_LENGTH = 19; // maximum allowed length
+
     /**
      * Option specifying that no cards are allowed.  This is useful if
      * you want only custom card types to validate so you turn off the
@@ -252,6 +256,36 @@ public class CreditCardValidator impleme
     }
 
     /**
+     * Create a new generic CreditCardValidator which validates the syntax and check digit
only.
+     * @param minLen minimum allowed length
+     * @param maxLen maximum allowed length
+     * @return the validator
+     * @since 1.5.2
+     */
+    public static CreditCardValidator genericCreditCardValidator(int minLen, int maxLen)
{
+    	return new CreditCardValidator(new CodeValidator[] {new CodeValidator("(\\d+)", minLen,
maxLen, LuhnCheckDigit.LUHN_CHECK_DIGIT)});
+    }
+
+    /**
+     * Create a new generic CreditCardValidator which validates the syntax and check digit
only.
+     * @param length exact length
+     * @return the validator
+     * @since 1.5.2
+     */
+    public static CreditCardValidator genericCreditCardValidator(int length) {
+    	return genericCreditCardValidator(length, length);
+    }
+
+    /**
+     * Create a new generic CreditCardValidator which validates the syntax and check digit
only.
+     * @return the validator
+     * @since 1.5.2
+     */
+    public static CreditCardValidator genericCreditCardValidator() {
+    	return genericCreditCardValidator(MIN_CC_LENGTH, MAX_CC_LENGTH);
+    }
+
+    /**
      * Checks if the field is a valid credit card number.
      * @param card The card number to validate.
      * @return Whether the card number is valid.

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=1781815&r1=1781814&r2=1781815&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
Sun Feb  5 21:58:38 2017
@@ -44,6 +44,34 @@ public class CreditCardValidatorTest ext
     private static final String VALID_VPAY2 = "4370000000000012";
     private static final String ERROR_VPAY = "4370000000000069";
 
+    private static final String [] VALID_CARDS = {
+		VALID_VISA,
+		VALID_SHORT_VISA,
+		VALID_AMEX,
+		VALID_MASTERCARD,
+		VALID_DISCOVER,
+		VALID_DISCOVER65,
+		VALID_DINERS,
+		VALID_VPAY,
+		VALID_VPAY2,
+    };
+
+    private static final String [] ERROR_CARDS = {
+		ERROR_VISA,
+		ERROR_SHORT_VISA,
+		ERROR_AMEX,
+		ERROR_MASTERCARD,
+		ERROR_DISCOVER,
+		ERROR_DISCOVER65,
+		ERROR_DINERS,
+		ERROR_VPAY,
+//		ERROR_VPAY2,
+		"",
+		"12345678901", // too short (11)
+		"12345678901234567890", // too long (20)
+		"4417123456789112", // invalid check digit
+    };
+
     /**
      * Constructor for CreditCardValidatorTest.
      */
@@ -522,4 +550,13 @@ public class CreditCardValidatorTest ext
         assertEquals("Valid-D", "5123456789012346", validator.validate("5123456789012346"));
     }
 
+    public void testGeneric() {
+    	CreditCardValidator ccv = CreditCardValidator.genericCreditCardValidator();
+    	for(String s : VALID_CARDS) {
+    		assertTrue(s, ccv.isValid(s));
+    	}
+    	for(String s : ERROR_CARDS) {
+    		assertFalse(s, ccv.isValid(s));
+    	}
+    }
 }



Mime
View raw message