UrlValidator implements ConstraintValidator<Url, String> { @Override public boolean isValid(String value, ConstraintValidatorContext context) { if (value == null) { return false; } if (!urlValidator.isValid(value)) { context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate(String.format("%s is invalid URL Address", value)) .addConstraintViolation(); return false; } return true; } @Inject  UrlValidator(); @Override void initialize(Url constraintAnnotation); @Override boolean isValid(String value, ConstraintValidatorContext context);  }
@Test public void testNull() { Assert.assertFalse(validator.isValid(null, context)); }
@Test public void testValidUrl() throws UnknownHostException { Assert.assertTrue(validator.isValid("http: }
@Test public void testInvalidHostName() { Assert.assertFalse(validator.isValid("fakename", context)); }