Author: britter Date: Thu Jan 1 15:55:17 2015 New Revision: 1648862 URL: http://svn.apache.org/r1648862 Log: VALIDATOR-336: CUSIPCheckDigit thinks invalid CUSIP is valid. Patch implemented by Sebb. Thanks to Josh Meyer and Peter Lindberg who also contributed to the fix by providing unit tests and proposing fixes.
Modified: commons/proper/validator/trunk/RELEASE-NOTES.txt commons/proper/validator/trunk/src/changes/changes.xml commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/checkdigit/CUSIPCheckDigit.java commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/CUSIPCheckDigitTest.java Modified: commons/proper/validator/trunk/RELEASE-NOTES.txt URL: http://svn.apache.org/viewvc/commons/proper/validator/trunk/RELEASE-NOTES.txt?rev=1648862&r1=1648861&r2=1648862&view=diff ============================================================================== --- commons/proper/validator/trunk/RELEASE-NOTES.txt (original) +++ commons/proper/validator/trunk/RELEASE-NOTES.txt Thu Jan 1 15:55:17 2015 @@ -59,6 +59,7 @@ BUGS FROM PREVIOUS RELEASE Thanks to Arūnas Bendoraitis. * [VALIDATOR-331] - IBANCheckDigitTest.createInvalidCodes(String[] codes) uses wrong values. + * [VALIDATOR-336] - CUSIPCheckDigit thinks invalid CUSIP is valid. * [VALIDATOR-347] - toLowerCase() method is Locale-sensitive and should not be used * [VALIDATOR-348] - Update TLD list to latest version (Version 2014123000) Modified: commons/proper/validator/trunk/src/changes/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/changes/changes.xml?rev=1648862&r1=1648861&r2=1648862&view=diff ============================================================================== --- commons/proper/validator/trunk/src/changes/changes.xml (original) +++ commons/proper/validator/trunk/src/changes/changes.xml Thu Jan 1 15:55:17 2015 @@ -43,6 +43,9 @@ The <action> type attribute can be add,u <body> <release version="1.4.1" date="tba" description="Maintenance release"> + <action issue="VALIDATOR-336" dev="sebb" type="fix" > + CUSIPCheckDigit thinks invalid CUSIP is valid + </action> <action issue="VALIDATOR-348" dev="sebb" type="update" > Update TLD list to latest version (Version 2014123000) </action> Modified: commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/checkdigit/CUSIPCheckDigit.java URL: http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/checkdigit/CUSIPCheckDigit.java?rev=1648862&r1=1648861&r2=1648862&view=diff ============================================================================== --- commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/checkdigit/CUSIPCheckDigit.java (original) +++ commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/checkdigit/CUSIPCheckDigit.java Thu Jan 1 15:55:17 2015 @@ -61,16 +61,18 @@ public final class CUSIPCheckDigit exten * * @param character The character to convert * @param leftPos The position of the character in the code, counting from left to right - * @param rightPos The positionof the character in the code, counting from right to left + * @param rightPos The position of the character in the code, counting from right to left * @return The integer value of the character * @throws CheckDigitException if character is not alphanumeric */ protected int toInt(char character, int leftPos, int rightPos) throws CheckDigitException { int charValue = Character.getNumericValue(character); - if (charValue < 0 || charValue > 35) { + // the final character is only allowed to reach 9 + final int charMax = rightPos == 1 ? 9 : 35; + if (charValue < 0 || charValue > charMax) { throw new CheckDigitException("Invalid Character[" + - leftPos + "] = '" + charValue + "'"); + leftPos + "," + rightPos + "] = '" + charValue + "' out of range 0 to " + charMax); } return charValue; } Modified: commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/CUSIPCheckDigitTest.java URL: http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/CUSIPCheckDigitTest.java?rev=1648862&r1=1648861&r2=1648862&view=diff ============================================================================== --- commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/CUSIPCheckDigitTest.java (original) +++ commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/CUSIPCheckDigitTest.java Thu Jan 1 15:55:17 2015 @@ -51,4 +51,24 @@ public class CUSIPCheckDigitTest extends invalid = new String[] {"0378#3100"}; } + private static String invalidCheckDigits[] = {"DUS0421CW", + "DUS0421CN", + "DUS0421CE" + }; + + public void testVALIDATOR_336_InvalidCheckDigits() { + for (int i = 0; i < invalidCheckDigits.length; i++) { + String invalidCheckDigit = invalidCheckDigits[i]; + assertFalse("Should fail: " + invalidCheckDigit, routine.isValid(invalidCheckDigit)); + } + } + + private static String validCheckDigits[] = {"DUS0421C5"}; + + public void testVALIDATOR_336_ValidCheckDigits() { + for (int i = 0; i < validCheckDigits.length; i++) { + String validCheckDigit = validCheckDigits[i]; + assertTrue("Should fail: " + validCheckDigit, routine.isValid(validCheckDigit)); + } + } }