InetSocketAddressConverter { public static List<InetSocketAddress> getInetSocketAddresses(String[] servers) { return getInetSocketAddresses(servers, 0); }  static InetSocketAddress getInetSocketAddress(String server); static InetSocketAddress getInetSocketAddress(String hostPort, int defaultPort); static List<InetSocketAddress> getInetSocketAddresses(String[] servers); static List<InetSocketAddress> getInetSocketAddresses(String[] servers, int defaultPort); static String toHostPort(InetSocketAddress address);  }
@Test public void validateHostPortList_ok_hostname() { List<InetSocketAddress> validHosts = InetSocketAddressConverter.getInetSocketAddresses( new String[]{ "some-host:9510", "some-host", "pif:9610", "piaf22:9710" }); InetSocketAddress server1 = InetSocketAddress.createUnresolved("some-host", 9510); InetSocketAddress server2 = InetSocketAddress.createUnresolved("some-host", 0); InetSocketAddress server3 = InetSocketAddress.createUnresolved("pif", 9610); InetSocketAddress server4 = InetSocketAddress.createUnresolved("piaf22", 9710); assertThat(validHosts, hasItems(server1, server2, server3, server4)); }
@Test public void validateHostPortList_ok_ipv4_lookalike_hostname() { List<InetSocketAddress> validHosts = InetSocketAddressConverter.getInetSocketAddresses( new String[]{ "10.10.10", "10.10.10.10.10", "999.10.10.400", "10-10-10-10" }); InetSocketAddress server1 = InetSocketAddress.createUnresolved("10.10.10", 0); InetSocketAddress server2 = InetSocketAddress.createUnresolved("10.10.10.10.10", 0); InetSocketAddress server3 = InetSocketAddress.createUnresolved("999.10.10.400", 0); InetSocketAddress server4 = InetSocketAddress.createUnresolved("10-10-10-10", 0); assertThat(validHosts, hasItems(server1, server2, server3, server4)); }
@Test public void validateHostPortList_ok_ipv4() { List<InetSocketAddress> validServers = InetSocketAddressConverter.getInetSocketAddresses( new String[]{ "10.12.14.43:9610", "10.12.14.43", "127.0.0.1:9510", "127.0.0.1" }); InetSocketAddress server1 = InetSocketAddress.createUnresolved("10.12.14.43", 9610); InetSocketAddress server2 = InetSocketAddress.createUnresolved("10.12.14.43", 0); InetSocketAddress server3 = InetSocketAddress.createUnresolved("127.0.0.1", 9510); InetSocketAddress server4 = InetSocketAddress.createUnresolved("127.0.0.1", 0); assertThat(validServers, hasItems(server1, server2, server3, server4)); }
@Test public void validateHostPortList_ok_ipv6() { List<InetSocketAddress> validServers = InetSocketAddressConverter.getInetSocketAddresses( new String[]{ "[2001:db8:a0b:12f0:0:0:0:1]:9510", "2001:db8:a0b:12f0:0:0:0:1", "[2001:db8:a0b:12f0:0:0:0:1]", "[2001:db8:a0b:12f0::1]:9510", "2001:db8:a0b:12f0::1", "[::1]:9510", "::1", "[::1]", "[2001:db8::]:9510", "2001:db8::" }); InetSocketAddress server1 = InetSocketAddress.createUnresolved("[2001:db8:a0b:12f0:0:0:0:1]", 9510); InetSocketAddress server2 = InetSocketAddress.createUnresolved("2001:db8:a0b:12f0:0:0:0:1", 0); InetSocketAddress server3 = InetSocketAddress.createUnresolved("[2001:db8:a0b:12f0:0:0:0:1]", 0); InetSocketAddress server4 = InetSocketAddress.createUnresolved("[2001:db8:a0b:12f0::1]", 9510); InetSocketAddress server5 = InetSocketAddress.createUnresolved("2001:db8:a0b:12f0::1", 0); InetSocketAddress server6 = InetSocketAddress.createUnresolved("[::1]", 9510); InetSocketAddress server7 = InetSocketAddress.createUnresolved("::1", 0); InetSocketAddress server8 = InetSocketAddress.createUnresolved("[::1]", 0); InetSocketAddress server9 = InetSocketAddress.createUnresolved("[2001:db8::]", 9510); InetSocketAddress server10 = InetSocketAddress.createUnresolved("2001:db8::", 0); assertThat(validServers, hasItems(server1, server2, server3, server4, server5, server6, server7, server8, server9, server10)); }