URLValidator implements Validator<String> { @Override public ValidationResult apply(String value, ValueContext context) { if (value == null || "".equals(value)) { return ValidationResult.ok(); } String str = value; if (!str.contains(URL_PREFIX) && !str.contains(URL_PREFIX_SECURE)) { str = URL_PREFIX + str; } try { new java.net.URL(str); } catch (MalformedURLException ex) { return ValidationResult.error(message); } if (StringUtils.countMatches(str, ".") < 2) { return ValidationResult.error(message); } return ValidationResult.ok(); } URLValidator(String message); @Override ValidationResult apply(String value, ValueContext context);  }
@Test public void testFalse1() { URLValidator validator = new URLValidator("Not a valid URL"); ValidationResult result = validator.apply("test", new ValueContext()); assertTrue(result.isError()); }
@Test public void testFalse2() { URLValidator validator = new URLValidator("Not a valid URL"); ValidationResult result = validator.apply("44", new ValueContext()); assertTrue(result.isError()); }
@Test public void testCorrect1() { URLValidator validator = new URLValidator("Not a valid URL"); ValidationResult result = validator.apply("http: assertFalse(result.isError()); }
@Test public void testCorrect2() { URLValidator validator = new URLValidator("Not a valid URL"); ValidationResult result = validator.apply("www.google.nl", new ValueContext()); assertFalse(result.isError()); }
@Test public void testCorrect3() { URLValidator validator = new URLValidator("Not a valid URL"); ValidationResult result = validator.apply("mijn.site.nl", new ValueContext()); assertFalse(result.isError()); }