HostAndPort { public static HostAndPort from(String hostPortString) { Validations.validate(hostPortString, Strings::isEmptyOrNull, "hostPortString can't be null"); String endpointWithoutScheme = hostPortString.replaceAll(".*: String[] split = endpointWithoutScheme.split(":"); Validations.validate(split, arr -> arr.length != 2, String.format("%s should be of format host:port for example 192.168.99.100:2376", hostPortString)); return new HostAndPort(split[0], Integer.parseInt(split[1])); } HostAndPort(String host, int port); static HostAndPort using(String host, int port); static HostAndPort from(String hostPortString); String getHost(); int getPort();  }
@Test public void shouldThrowIllegalArgumentExceptionWhenHostPortStringIsInvalid() throws Exception { String hostPortString = "test"; expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage(equalTo("test should be of format host:port for example 192.168.99.100:2376")); HostAndPort.from(hostPortString); }