SocketAddressFactory { public static SocketAddress createFrom(String value) { if (value.startsWith(UNIX_DOMAIN_SOCKET_PREFIX)) { String filePath = value.substring(UNIX_DOMAIN_SOCKET_PREFIX.length()); File file = new File(filePath); if (!file.isAbsolute()) { throw new IllegalArgumentException("File path must be absolute: " + filePath); } try { if (file.createNewFile()) { file.deleteOnExit(); } } catch (IOException ex) { throw new RuntimeException(ex); } return new DomainSocketAddress(file); } else { HostAndPort hostAndPort = HostAndPort.fromString(value); checkArgument( hostAndPort.hasPort(), "Address must be a unix: value); return new InetSocketAddress(hostAndPort.getHost(), hostAndPort.getPort()); } }  static SocketAddress createFrom(String value);  }
@Test public void testHostPortSocket() { SocketAddress socketAddress = SocketAddressFactory.createFrom("localhost:123"); assertThat(socketAddress, Matchers.instanceOf(InetSocketAddress.class)); assertEquals("localhost", ((InetSocketAddress) socketAddress).getHostString()); assertEquals(123, ((InetSocketAddress) socketAddress).getPort()); }
@Test public void testDomainSocket() throws Exception { File tmpFile = tmpFolder.newFile(); SocketAddress socketAddress = SocketAddressFactory.createFrom("unix: assertThat(socketAddress, Matchers.instanceOf(DomainSocketAddress.class)); assertEquals(tmpFile.getAbsolutePath(), ((DomainSocketAddress) socketAddress).path()); }
@Test public void testHostPortSocket() { SocketAddress socketAddress = SocketAddressFactory.createFrom("localhost:123"); assertThat(socketAddress, instanceOf(InetSocketAddress.class)); assertEquals("localhost", ((InetSocketAddress) socketAddress).getHostString()); assertEquals(123, ((InetSocketAddress) socketAddress).getPort()); }
@Test public void testDomainSocket() throws Exception { File tmpFile = tmpFolder.newFile(); SocketAddress socketAddress = SocketAddressFactory.createFrom("unix: assertThat(socketAddress, instanceOf(DomainSocketAddress.class)); assertEquals(tmpFile.getAbsolutePath(), ((DomainSocketAddress) socketAddress).path()); }