PortugueseNIFValidator extends StringValidator { @Override public void validate(IValidatable<String> validatable) { super.validate(validatable); final String value = validatable.getValue(); if(value==null || value.length()==0) return; try { if(!isNIFValid(value)) validatable.error(new ValidationError(this)); } catch(NumberFormatException e){ validatable.error(new ValidationError(this)); } }  @Override void validate(IValidatable<String> validatable); static boolean isNIFValid(String number);  }
@Test public void testValidNif() { IValidator<String> validator = new PortugueseNIFValidator(); Validatable<String> validatable = new Validatable<String>("241250609"); validator.validate(validatable); assertEquals(0, validatable.getErrors().size()); }
@Test public void testValidNifUsual() { IValidator<String> validator = new PortugueseNIFValidator(); Validatable<String> validatable = new Validatable<String>("123456789"); validator.validate(validatable); assertEquals(0, validatable.getErrors().size()); }
@Test public void testValidNif0term() { IValidator<String> validator = new PortugueseNIFValidator(); Validatable<String> validatable = new Validatable<String>("504426290"); validator.validate(validatable); assertEquals(0, validatable.getErrors().size()); }
@Test public void testValidNif0TermWithMod1() { IValidator<String> validator = new PortugueseNIFValidator(); Validatable<String> validatable = new Validatable<String>("504646680"); validator.validate(validatable); assertEquals(0, validatable.getErrors().size()); }
@Test public void testInvalidNif() { IValidator<String> validator = new PortugueseNIFValidator(); Validatable<String> validatable = new Validatable<String>("124456789"); validator.validate(validatable); assertEquals(1, validatable.getErrors().size()); }
@Test public void testAnotherInvalidNif() { IValidator<String> validator = new PortugueseNIFValidator(); Validatable<String> validatable = new Validatable<String>("505646780"); validator.validate(validatable); assertEquals(1, validatable.getErrors().size()); }
@Test public void testValidDoubleDigit() { IValidator<String> validator = new PortugueseNIFValidator(); Validatable<String> validatable = new Validatable<String>("451234561"); validator.validate(validatable); assertEquals(0, validatable.getErrors().size()); }