/** * <p>Validate an ISBN-10 or ISBN-13 check digit, depending * on the length of the code.</p> * <p> * If the length of the code is 10, it is treated as an ISBN-10 * code or ff the length of the code is 13, it is treated as an ISBN-13 * code. * * @param code The ISBN code to validate (should have a length of * 10 or 13) * @return <code>true</code> if the code has a length of 10 and is * a valid ISBN-10 check digit or the code has a length of 13 and is * a valid ISBN-13 check digit - otherwise <code>false</code>. */ @Override public boolean isValid(String code) { if (code == null) { return false; } else if (code.length() == 10) { // CHECKSTYLE IGNORE MagicNumber return ISBN10_CHECK_DIGIT.isValid(code); } else if (code.length() == 13) { // CHECKSTYLE IGNORE MagicNumber return ISBN13_CHECK_DIGIT.isValid(code); } else { return false; } }
/** * Calculate an ISBN-10 or ISBN-13 check digit, depending * on the length of the code. * <p> * If the length of the code is 9, it is treated as an ISBN-10 * code or if the length of the code is 12, it is treated as an ISBN-13 * code. * * @param code The ISBN code to validate (should have a length of * 9 or 12) * @return The ISBN-10 check digit if the length is 9 or an ISBN-13 * check digit if the length is 12. * @throws CheckDigitException if the code is missing, or an invalid * length (i.e. not 9 or 12) or if there is an error calculating the * check digit. */ @Override public String calculate(String code) throws CheckDigitException { if (code == null || code.length() == 0) { throw new CheckDigitException("ISBN Code is missing"); } else if (code.length() == 9) { // CHECKSTYLE IGNORE MagicNumber return ISBN10_CHECK_DIGIT.calculate(code); } else if (code.length() == 12) { // CHECKSTYLE IGNORE MagicNumber return ISBN13_CHECK_DIGIT.calculate(code); } else { throw new CheckDigitException("Invalid ISBN Length = " + code.length()); } }
String ean13 = "977" + input.substring(0, input.length() -1) + suffix; try { String checkDigit = EAN13CheckDigit.EAN13_CHECK_DIGIT.calculate(ean13); ean13 += checkDigit; return ean13;
/** * Validate an IBAN Code * * @param code The value validation is being performed on * @return <code>true</code> if the value is valid */ public boolean isValid(String code) { Validator formatValidator = getValidator(code); if (formatValidator == null || code.length() != formatValidator.lengthOfIBAN || !formatValidator.validator.isValid(code)) { return false; } return IBANCheckDigit.IBAN_CHECK_DIGIT.isValid(code); }
/** * Test missing code */ public void testMissingCode() { // isValid() null assertFalse("isValid() Null", routine.isValid(null)); // isValid() zero length assertFalse("isValid() Zero Length", routine.isValid("")); // isValid() length 1 // Don't use 0, because that passes for Verhoef (not sure why yet) assertFalse("isValid() Length 1", routine.isValid("9")); // calculate() null try { routine.calculate(null); fail("calculate() Null - expected exception"); } catch (Exception e) { assertEquals("calculate() Null", missingMessage, e.getMessage()); } // calculate() zero length try { routine.calculate(""); fail("calculate() Zero Length - expected exception"); } catch (Exception e) { assertEquals("calculate() Zero Length", missingMessage, e.getMessage()); } }
/** * Convert an ISBN-10 code to an ISBN-13 code. * <p> * This method requires a valid ISBN-10 with NO formatting * characters. * * @param isbn10 The ISBN-10 code to convert * @return A converted ISBN-13 code or <code>null</code> * if the ISBN-10 code is not valid */ public String convertToISBN13(String isbn10) { if (isbn10 == null) { return null; } String input = isbn10.trim(); if (input.length() != ISBN_10_LEN) { throw new IllegalArgumentException("Invalid length " + input.length() + " for '" + input + "'"); } // Calculate the new ISBN-13 code (drop the original checkdigit) String isbn13 = "978" + input.substring(0, ISBN_10_LEN - 1); try { String checkDigit = isbn13Validator.getCheckDigit().calculate(isbn13); isbn13 += checkDigit; return isbn13; } catch (CheckDigitException e) { throw new IllegalArgumentException("Check digit error for '" + input + "' - " + e.getMessage()); } }
if (checkdigit != null && !checkdigit.isValid(code)) { return null;
assertFalse("isValid() Lth 9 ", routine.isValid("123456789")); assertFalse("isValid() Lth 11", routine.isValid("12345678901")); assertFalse("isValid() Lth 12", routine.isValid("123456789012")); assertFalse("isValid() Lth 14", routine.isValid("12345678901234")); routine.calculate("12345678"); fail("calculate() Lth 8 - expected exception"); } catch (Exception e) { routine.calculate("1234567890"); fail("calculate() Lth 10 - expected exception"); } catch (Exception e) { routine.calculate("12345678901"); fail("calculate() Lth 11 - expected exception"); } catch (Exception e) { routine.calculate("1234567890123"); fail("calculate() Lth 13 - expected exception"); } catch (Exception e) {
/** * Test calculate() for valid values. */ public void testCalculateValid() { if (log.isDebugEnabled()) { log.debug("testCalculateValid() for " + routine.getClass().getName()); } // test valid values for (int i = 0; i < valid.length; i++) { String code = removeCheckDigit(valid[i]); String expected = checkDigit(valid[i]); try { if (log.isDebugEnabled()) { log.debug(" " + i + " Testing Valid Check Digit, Code=[" + code + "] expected=[" + expected + "]"); } assertEquals("valid[" + i +"]: " + valid[i], expected, routine.calculate(code)); } catch (Exception e) { fail("valid[" + i +"]=" + valid[i] + " threw " + e); } } }
public void testVALIDATOR_336_InvalidCheckDigits() { for (int i = 0; i < invalidCheckDigits.length; i++) { String invalidCheckDigit = invalidCheckDigits[i]; assertFalse("Should fail: " + invalidCheckDigit, routine.isValid(invalidCheckDigit)); } }
String actual = routine.calculate(removeCheckDigit(code));
public void testVALIDATOR_346() { for (int i = 0; i < invalidCheckDigits.length; i++) { String invalidCheckDigit = invalidCheckDigits[i]; assertFalse("Should fail: " + invalidCheckDigit, routine.isValid(invalidCheckDigit)); } }
/** * Calculate an ISBN-10 or ISBN-13 check digit, depending * on the length of the code. * <p> * If the length of the code is 9, it is treated as an ISBN-10 * code or if the length of the code is 12, it is treated as an ISBN-13 * code. * * @param code The ISBN code to validate (should have a length of * 9 or 12) * @return The ISBN-10 check digit if the length is 9 or an ISBN-13 * check digit if the length is 12. * @throws CheckDigitException if the code is missing, or an invalid * length (i.e. not 9 or 12) or if there is an error calculating the * check digit. */ @Override public String calculate(String code) throws CheckDigitException { if (code == null || code.length() == 0) { throw new CheckDigitException("ISBN Code is missing"); } else if (code.length() == 9) { // CHECKSTYLE IGNORE MagicNumber return ISBN10_CHECK_DIGIT.calculate(code); } else if (code.length() == 12) { // CHECKSTYLE IGNORE MagicNumber return ISBN13_CHECK_DIGIT.calculate(code); } else { throw new CheckDigitException("Invalid ISBN Length = " + code.length()); } }
public void testVALIDATOR_336_ValidCheckDigits() { for (int i = 0; i < validCheckDigits.length; i++) { String validCheckDigit = validCheckDigits[i]; assertTrue("Should fail: " + validCheckDigit, routine.isValid(validCheckDigit)); } } }
/** * Convert an ISBN-10 code to an ISBN-13 code. * <p> * This method requires a valid ISBN-10 with NO formatting * characters. * * @param isbn10 The ISBN-10 code to convert * @return A converted ISBN-13 code or <code>null</code> * if the ISBN-10 code is not valid */ public String convertToISBN13(String isbn10) { if (isbn10 == null) { return null; } String input = isbn10.trim(); if (input.length() != ISBN_10_LEN) { throw new IllegalArgumentException("Invalid length " + input.length() + " for '" + input + "'"); } // Calculate the new ISBN-13 code (drop the original checkdigit) String isbn13 = "978" + input.substring(0, ISBN_10_LEN - 1); try { String checkDigit = isbn13Validator.getCheckDigit().calculate(isbn13); isbn13 += checkDigit; return isbn13; } catch (CheckDigitException e) { throw new IllegalArgumentException("Check digit error for '" + input + "' - " + e.getMessage()); } }
public void testVALIDATOR_345() { for (int i = 0; i < invalidCheckDigits.length; i++) { String invalidCheckDigit = invalidCheckDigits[i]; assertFalse("Should fail: " + invalidCheckDigit, routine.isValid(invalidCheckDigit)); } } }
String ean13 = "977" + input.substring(0, input.length() -1) + suffix; try { String checkDigit = EAN13CheckDigit.EAN13_CHECK_DIGIT.calculate(ean13); ean13 += checkDigit; return ean13;
public void testVALIDATOR_336_ValidCheckDigits() { for (int i = 0; i < validCheckDigits.length; i++) { String validCheckDigit = validCheckDigits[i]; assertTrue("Should fail: " + validCheckDigit, routine.isValid(validCheckDigit)); } } }
public void testVALIDATOR_346() { for (int i = 0; i < invalidCheckDigits.length; i++) { String invalidCheckDigit = invalidCheckDigits[i]; assertFalse("Should fail: " + invalidCheckDigit, routine.isValid(invalidCheckDigit)); } }