CSVURIValidator extends AbstractValidator<String> { @Override public void validate(String str) { if(str != null && str != "") { String[] pairs = str.split("\\s*,\\s*"); for (String pair: pairs) { String[] parts = pair.split("\\s*:\\s*"); if (parts.length != 2) { addMessage(Status.ERROR,"can't parse into host:port pairs"); } else { String[] rightParts = parts[1].split("/"); try { int port = Integer.parseInt(rightParts[0]); if (port < 0) { addMessage(Status.ERROR, "Can't parse port less than 0"); } else if (port > 65535) { addMessage(Status.ERROR, "Can't parse port greater than 65535"); } } catch(NumberFormatException e) { addMessage(Status.ERROR, "Can't parse port"); } } } } else { addMessage(Status.ERROR, "Can't be null nor empty"); } }  @Override void validate(String str);  }
@Test public void test() { assertEquals(0, validator.getMessages().size()); validator.validate("host1:1,host2:2"); assertEquals(Status.OK, validator.getStatus()); assertEquals(0, validator.getMessages().size()); }
@Test public void testBad() { assertEquals(0, validator.getMessages().size()); validator.validate("host1"); assertEquals(Status.ERROR, validator.getStatus()); assertEquals(1, validator.getMessages().size()); validator.reset(); validator.validate(""); assertEquals(Status.ERROR, validator.getStatus()); assertEquals(1, validator.getMessages().size()); validator.reset(); validator.validate(null); assertEquals(Status.ERROR, validator.getStatus()); assertEquals(1, validator.getMessages().size()); validator.reset(); validator.validate("host1:65536"); assertEquals(Status.ERROR, validator.getStatus()); assertEquals(1, validator.getMessages().size()); validator.reset(); validator.validate("host1:-1"); assertEquals(Status.ERROR, validator.getStatus()); assertEquals(1, validator.getMessages().size()); }