Host { public static ImmutableList<Host> parseHosts(String hostsString) { return parseHosts(hostsString, Optional.empty()); } @VisibleForTesting  Host(String hostName, int port); static ImmutableList<Host> parseHosts(String hostsString); static ImmutableList<Host> parseHosts(String hostsString, int defaultPort); static Host from(String hostname, int port); static Host parseConfString(String ipAndPort, int defaultPort); static Host parseConfString(String ipAndPort); static Host parse(String ipAndPort, Optional<Integer> defaultPort); String getHostName(); int getPort(); @Override final int hashCode(); @Override final boolean equals(Object object); String asString(); @Override String toString();  }
@Test void parseHostsShouldParseEmptyString() { assertThat(Host.parseHosts("")) .isEmpty(); }
@Test void parseHostsShouldParseMonoHost() { assertThat(Host.parseHosts("localhost:9200")) .containsOnly(new Host("localhost", 9200)); }
@Test void parseHostsShouldParseMultiHosts() { assertThat(Host.parseHosts("localhost:9200,server:9155")) .containsOnly( new Host("localhost", 9200), new Host("server", 9155)); }
@Test void parseHostsShouldNotFailOnMultiComma() { assertThat(Host.parseHosts("localhost:9200,,server:9155")) .containsOnly( new Host("localhost", 9200), new Host("server", 9155)); }
@Test void parseHostsShouldFailOnInvalidHost() { assertThatThrownBy(() -> Host.parseHosts("localhost:invalid,,server:9155")) .isInstanceOf(NumberFormatException.class); }
@Test void parseHostsShouldSwallowDuplicates() { assertThat(Host.parseHosts("localhost:9200,localhost:9200")) .containsOnly( new Host("localhost", 9200)); }
@Test void parseHostsShouldNotSwallowSameAddressDifferentPort() { assertThat(Host.parseHosts("localhost:9200,localhost:9155")) .containsOnly( new Host("localhost", 9200), new Host("localhost", 9155)); }
@Test void parseHostsShouldNotSwallowSamePortDifferentAddress() { assertThat(Host.parseHosts("localhost:9200,abcd:9200")) .containsOnly( new Host("localhost", 9200), new Host("abcd", 9200)); }
@Test void parseHostsShouldHandleDefaultPort() { int defaultPort = 155; assertThat(Host.parseHosts("localhost:9200,abcd", defaultPort)) .containsOnly( new Host("localhost", 9200), new Host("abcd", 155)); }
@Test void parseHostsShouldThrowOnAbsentPortWhenNoDefaultPort() { assertThatIllegalArgumentException() .isThrownBy(() -> Host.parseHosts("localhost:9200,abcd")); }