/** * <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; } }
/** * 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); }
if (checkdigit != null && !checkdigit.isValid(code)) { return null;
public void testVALIDATOR_336_InvalidCheckDigits() { for (int i = 0; i < invalidCheckDigits.length; i++) { String invalidCheckDigit = invalidCheckDigits[i]; assertFalse("Should fail: " + invalidCheckDigit, routine.isValid(invalidCheckDigit)); } }
public void testVALIDATOR_346() { for (int i = 0; i < invalidCheckDigits.length; i++) { String invalidCheckDigit = invalidCheckDigits[i]; assertFalse("Should fail: " + invalidCheckDigit, routine.isValid(invalidCheckDigit)); } }
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_345() { for (int i = 0; i < invalidCheckDigits.length; i++) { String invalidCheckDigit = invalidCheckDigits[i]; assertFalse("Should fail: " + invalidCheckDigit, routine.isValid(invalidCheckDigit)); } } }
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)); } }
public void testVALIDATOR_336_InvalidCheckDigits() { for (int i = 0; i < invalidCheckDigits.length; i++) { String invalidCheckDigit = invalidCheckDigits[i]; assertFalse("Should fail: " + invalidCheckDigit, routine.isValid(invalidCheckDigit)); } }
/** * Test isValid() for valid values. */ public void testIsValidTrue() { if (log.isDebugEnabled()) { log.debug("testIsValidTrue() for " + routine.getClass().getName()); } // test valid values for (int i = 0; i < valid.length; i++) { if (log.isDebugEnabled()) { log.debug(" " + i + " Testing Valid Code=[" + valid[i] + "]"); } assertTrue("valid[" + i +"]: " + valid[i], routine.isValid(valid[i])); } }
public void testOther() throws Exception { BufferedReader rdr = null; try { rdr = new BufferedReader( new InputStreamReader( this.getClass().getResourceAsStream("IBANtests.txt"),"ASCII")); String line; while((line=rdr.readLine()) != null) { if (!line.startsWith("#") && line.length() > 0) { if (line.startsWith("-")) { line = line.substring(1); Assert.assertFalse(line, routine.isValid(line.replaceAll(" ", ""))); } else { Assert.assertTrue(line, routine.isValid(line.replaceAll(" ", ""))); } } } } finally { if (rdr != null) { rdr.close(); } } } }
/** * Test isValid() for invalid values. */ public void testIsValidFalse() { if (log.isDebugEnabled()) { log.debug("testIsValidFalse() for " + routine.getClass().getName()); } // test invalid code values for (int i = 0; i < invalid.length; i++) { if (log.isDebugEnabled()) { log.debug(" " + i + " Testing Invalid Code=[" + invalid[i] + "]"); } assertFalse("invalid[" + i +"]: " + invalid[i], routine.isValid(invalid[i])); } // test invalid check digit values String[] invalidCheckDigits = createInvalidCodes(valid); for (int i = 0; i < invalidCheckDigits.length; i++) { if (log.isDebugEnabled()) { log.debug(" " + i + " Testing Invalid Check Digit, Code=[" + invalidCheckDigits[i] + "]"); } assertFalse("invalid check digit[" + i +"]: " + invalidCheckDigits[i], routine.isValid(invalidCheckDigits[i])); } }
/** * Test zero sum */ public void testZeroSum() { assertFalse("isValid() Zero Sum", routine.isValid(zeroSum)); try { routine.calculate(zeroSum); fail("Zero Sum - expected exception"); } catch (Exception e) { assertEquals("isValid() Zero Sum", "Invalid code, sum is zero", e.getMessage()); } }
/** * 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()); } }
/** * Test isValid() ISSN codes and convert them */ public void testIsValidISSNConvert() { CheckDigit ean13cd = EAN13CheckDigit.EAN13_CHECK_DIGIT; Random r = new Random(); for(String f : validFormat) { String suffix = String.format("%02d", r.nextInt(100)); String ean13 = VALIDATOR.convertToEAN13(f, suffix); assertTrue(ean13, ean13cd.isValid(ean13)); } // internet samples assertEquals("9771144875007", VALIDATOR.convertToEAN13("1144-875X", "00")); assertEquals("9770264359008", VALIDATOR.convertToEAN13("0264-3596", "00")); assertEquals("9771234567003", VALIDATOR.convertToEAN13("1234-5679", "00")); }
@Test public void testValid() { for(String f : validIBANFormat) { assertTrue("Checksum fail: "+f, IBANCheckDigit.IBAN_CHECK_DIGIT.isValid(f)); assertTrue("Missing validator: "+f, VALIDATOR.hasValidator(f)); assertTrue(f, VALIDATOR.isValid(f)); } }
/** * <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; } }
/** * 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); }