/** * Set up routine & valid codes. */ @Override protected void setUp() throws Exception { super.setUp(); routine = VerhoeffCheckDigit.VERHOEFF_CHECK_DIGIT; valid = new String[] { "15", "1428570", "12345678902" }; }
/** * 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); } } }
log.debug(" " + i + " Testing Invalid Check Digit, Code=[" + code + "]"); String expected = checkDigit(code); String actual = routine.calculate(removeCheckDigit(code)); fail("Expected mismatch for " + code + " expected " + expected + " actual " + actual); assertTrue("Invalid Character[" +i +"]=" + e.getMessage(), e.getMessage().startsWith("Invalid "));
/** * 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()); } }
/** * Returns an array of codes with invalid check digits. * * @param codes Codes with valid check digits * @return Codes with invalid check digits */ protected String[] createInvalidCodes(String[] codes) { List<String> list = new ArrayList<String>(); // create invalid check digit values for (String fullCode : codes) { String code = removeCheckDigit(fullCode); String check = checkDigit(fullCode); for (int j = 0; j < POSSIBLE_CHECK_DIGITS.length(); j++) { String curr = POSSIBLE_CHECK_DIGITS.substring(j, j + 1);//"" + Character.forDigit(j, 10); if (!curr.equals(check)) { list.add(code + curr); } } } return list.toArray(new String[list.size()]); }
/** * Test check digit serialization. */ public void testSerialization() { // Serialize the check digit routine ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(routine); oos.flush(); oos.close(); } catch (Exception e) { fail(routine.getClass().getName() + " error during serialization: " + e); } // Deserialize the test object Object result = null; try { ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bais); result = ois.readObject(); bais.close(); } catch (Exception e) { fail(routine.getClass().getName() + " error during deserialization: " + e); } assertNotNull(result); }
/** * 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 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])); } }
/** * 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()); } }
/** * Set up routine & valid codes. */ @Override protected void setUp() throws Exception { super.setUp(); routine = EAN13CheckDigit.EAN13_CHECK_DIGIT; valid = new String[] { "9780072129519", "9780764558313", "4025515373438", "0095673400332"}; }
/** * Set up routine & valid codes. */ @Override protected void setUp() throws Exception { super.setUp(); routine = LuhnCheckDigit.LUHN_CHECK_DIGIT; valid = new String[] { VALID_VISA, VALID_SHORT_VISA, VALID_AMEX, VALID_MASTERCARD, VALID_DISCOVER, VALID_DINERS}; } }
/** * Set up routine & valid codes. */ @Override protected void setUp() throws Exception { super.setUp(); routine = ISBN10CheckDigit.ISBN10_CHECK_DIGIT; valid = new String[] { "1930110995", "020163385X", "1932394354", "1590596277" }; }
/** * Set up routine & valid codes. */ @Override protected void setUp() throws Exception { super.setUp(); routine = ABANumberCheckDigit.ABAN_CHECK_DIGIT; valid = new String[] { "123456780", "123123123", "011000015", "111000038", "231381116", "121181976" }; }
/** * Set up routine & valid codes. */ @Override protected void setUp() throws Exception { super.setUp(); routine = ISINCheckDigit.ISIN_CHECK_DIGIT; valid = new String[] {"US0378331005", "BMG8571G1096", "AU0000XVGZA3", "GB0002634946", "FR0004026250", "DK0009763344" }; invalid = new String[] {"0378#3100"}; }
/** * Set up routine & valid codes. */ @Override protected void setUp() throws Exception { super.setUp(); routine = SedolCheckDigit.SEDOL_CHECK_DIGIT; valid = new String[] { "0263494", "0870612", "B06LQ97", "3437575", "B07LF55", }; invalid = new String[] {"123#567"}; zeroSum = "0000000"; }
/** * Set up routine & valid codes. */ @Override protected void setUp() throws Exception { super.setUp(); routine = CUSIPCheckDigit.CUSIP_CHECK_DIGIT; valid = new String[] {"037833100", "931142103", "837649128", "392690QT3", "594918104", "86770G101", "Y8295N109", "G8572F100" }; invalid = new String[] {"0378#3100"}; }
/** * Set up routine & valid codes. */ @Override protected void setUp() throws Exception { super.setUp(); routine = ISBNCheckDigit.ISBN_CHECK_DIGIT; valid = new String[] { "9780072129519", "9780764558313", "1930110995", "020163385X", "1590596277", // ISBN-10 Ubuntu Book "9781590596272" // ISBN-13 Ubuntu Book }; missingMessage = "ISBN Code is missing"; zeroSum = "000000000000"; }
super.setUp(); routine = ISSNCheckDigit.ISSN_CHECK_DIGIT; valid = new String[] {
super.setUp(); routine = IBANCheckDigit.IBAN_CHECK_DIGIT; valid = new String[] {
/** * Set up routine & valid codes. */ @Override protected void setUp() throws Exception { super.setUp(); routine = new ModulusTenCheckDigit(new int[] { 1, 3 }, true); valid = new String[] { "9780072129519", "9780764558313", "4025515373438", "0095673400332"}; }