URLValidator implements ConstraintValidator<URL, String> { @Override public boolean isValid(String value, ConstraintValidatorContext context) { if (StringUtils.isEmpty(value)) { return true; } if (!value.contains(URL_PREFIX) && !value.contains(URL_PREFIX_SECURE)) { value = URL_PREFIX + value; } try { new java.net.URL(value); int matches = StringUtils.countMatches(value, "."); if (matches < 2) { return false; } } catch (MalformedURLException ex) { return false; } return true; }  @Override void initialize(URL constraintAnnotation); @Override boolean isValid(String value, ConstraintValidatorContext context);  }
@Test public void testNull() { assertTrue(validator.isValid(null, null)); }
@Test public void testValid() { assertTrue(validator.isValid("http: assertTrue(validator.isValid("www.google.nl", null)); assertTrue(validator.isValid("mijn.site.nl", null)); assertFalse(validator.isValid("mijn.site", null)); }
@Test public void testValid_basic() { assertFalse(validator.isValid("nowwwnonothing", null)); assertFalse(validator.isValid("no-www-no-nothing", null)); assertFalse(validator.isValid("no/www/no/nothing", null)); assertFalse(validator.isValid("no$www$no$nothing", null)); assertFalse(validator.isValid("no@www@no@nothing", null)); assertFalse(validator.isValid("no www no nothing", null)); }
@Test public void testValid_exoticCharacters() { assertTrue(validator.isValid("http: }