AddressUtils { public static InetSocketAddress createResolved(String hostname, int port) { return createInetSocketAddress(hostname, port, true); } private  AddressUtils(); static InetSocketAddress createInetSocketAddress(String hostname, int port, boolean resolve); static InetSocketAddress createResolved(String hostname, int port); static InetSocketAddress createUnresolved(String hostname, int port); static InetSocketAddress parseAddress(String address, int defaultPort); static InetSocketAddress replaceUnresolvedNumericIp(InetSocketAddress inetSocketAddress); static InetSocketAddress replaceWithResolved(InetSocketAddress inetSocketAddress); static SocketAddress updateHost(@Nullable Supplier<? extends SocketAddress> address, String host); static SocketAddress updatePort(@Nullable Supplier<? extends SocketAddress> address, int port);  }
@Test public void shouldCreateResolvedNumericIPv4Address() { InetSocketAddress socketAddress = AddressUtils.createResolved("127.0.0.1", 8080); assertThat(socketAddress.isUnresolved()).isFalse(); assertThat(socketAddress.getAddress().getHostAddress()).isEqualTo("127.0.0.1"); assertThat(socketAddress.getPort()).isEqualTo(8080); assertThat(socketAddress.getHostString()).isEqualTo("127.0.0.1"); }
@Test public void shouldCreateResolvedNumericIPv6Address() { InetSocketAddress socketAddress = AddressUtils.createResolved("::1", 8080); assertThat(socketAddress.isUnresolved()).isFalse(); assertThat(socketAddress.getAddress().getHostAddress()).isEqualTo("0:0:0:0:0:0:0:1"); assertThat(socketAddress.getPort()).isEqualTo(8080); assertThat(socketAddress.getHostString()).isEqualTo("0:0:0:0:0:0:0:1"); }
@Test public void createResolvedBadValues() { assertThatExceptionOfType(NullPointerException.class) .isThrownBy(() -> AddressUtils.createResolved(null, 0)) .withMessage("hostname"); assertThatExceptionOfType(IllegalArgumentException.class) .isThrownBy(() -> AddressUtils.createResolved("hostname", -1)) .withMessage("port out of range:-1"); }