ConnectionDescriptor { public static ConnectionDescriptor makeConnDesc(String address) { if (address == null || address.trim().length() == 0) { throw new IllegalArgumentException(String.format(ERR_MSG, "The given address is empty")); } address = address.replaceFirst("^tcp: if (address.startsWith("unix: final String path = address.substring("unix: if (path.isEmpty()) { throw new IllegalArgumentException( "Unix socket file name is empty."); } final File f = new File(path); if (!f.getAbsolutePath().matches("^[^*&%\\s]+$")) { throw new IllegalArgumentException(String.format(ERR_MSG,"Not a regular file name : " + f.getAbsolutePath())); } return new UnixSocketConnectionDescriptor(path); } final Matcher ipv4Matcher = PATTERN_HOSTNAME_PORT.matcher(address); final Matcher ipv6Matcher = PATTERN_IPV6_PORT.matcher(address); if (ipv4Matcher.matches()) { try { final InetAddress addr = InetAddress.getByName(ipv4Matcher .group(1)); final int port = Integer.parseInt(ipv4Matcher.group(2)); return new TcpConnectionDescriptor(addr, port); } catch (final UnknownHostException e) { throw new IllegalArgumentException(e); } } else if (ipv6Matcher.matches()) { try { final InetAddress addr = InetAddress.getByName(ipv6Matcher .group(1)); final int port = Integer.parseInt(ipv6Matcher.group(2)); return new TcpConnectionDescriptor(addr, port); } catch (final UnknownHostException e) { throw new IllegalArgumentException(e); } } else { throw new IllegalArgumentException(String.format(ERR_MSG, "Not a valid address")); } }  static ConnectionDescriptor makeConnDesc(String address); abstract ISocket makeSocket();  }
@Test(expected = IllegalArgumentException.class) public void testMakeConnDesc_unixSock_noFile() { ConnectionDescriptor.makeConnDesc("unix: }
@Test(expected = IllegalArgumentException.class) public void testMakeConnDesc_unixSock_invalidFile() { ConnectionDescriptor.makeConnDesc("unix: }
@Test public void testMakeConnDesc_localhost_port1() throws Exception { final TcpConnectionDescriptor desc = (TcpConnectionDescriptor) ConnectionDescriptor .makeConnDesc("localhost:1"); assertThat(desc).isNotNull(); assertThat(desc.getAddr().isReachable(1)).isTrue(); assertThat(desc.getPort()).isEqualTo(1); }
@Test public void testMakeConnDesc_127001_9000() throws Exception { final TcpConnectionDescriptor desc = (TcpConnectionDescriptor) ConnectionDescriptor .makeConnDesc("127.0.0.1:9000"); assertThat(desc).isNotNull(); assertThat(desc.getAddr().isReachable(1)).isTrue(); assertThat(desc.getPort()).isEqualTo(9000); }
@Test public void testMakeConnDesc_ipv6_fullAddress() throws Exception { final TcpConnectionDescriptor desc = (TcpConnectionDescriptor) ConnectionDescriptor .makeConnDesc("[0:0:0:0:0:0:0:1]:9000"); assertThat(desc).isNotNull(); if(!runsOnTravisCI()) { assertThat(desc.getAddr().isReachable(1)).isTrue(); } assertThat(desc.getPort()).isEqualTo(9000); }
@Test public void testMakeConnDesc_ipv6_localhostShortcut() throws Exception { final TcpConnectionDescriptor desc = (TcpConnectionDescriptor) ConnectionDescriptor .makeConnDesc("[::1]:9000"); assertThat(desc).isNotNull(); if(!runsOnTravisCI()){ assertThat(desc.getAddr().isReachable(1)).isTrue(); } assertThat(desc.getPort()).isEqualTo(9000); }
@Test public void testMakeConnDesc_ipv6_arbitraryAddress() throws Exception { final TcpConnectionDescriptor desc = (TcpConnectionDescriptor) ConnectionDescriptor .makeConnDesc("[0000:0000:0000:0000:0000:0acf:0000:0001]:9000"); assertThat(desc).isNotNull(); assertThat(desc.getAddr().getHostAddress()).isEqualToIgnoringCase( "0:0:0:0:0:acf:0:1"); assertThat(desc.getPort()).isEqualTo(9000); }
@Test(expected = IllegalArgumentException.class) public void testMakeConnDesc_ipv6_invalidAddress() throws Exception { ConnectionDescriptor .makeConnDesc("[0000:0Z00:0000:0000:0000:0acf:0000:0001]:9000"); }
@Test(expected = IllegalArgumentException.class) public void testMakeConnDesc_null() throws Exception { ConnectionDescriptor.makeConnDesc(null); }
@Test(expected = IllegalArgumentException.class) public void testMakeConnDesc_onlyHostname() throws Exception { ConnectionDescriptor.makeConnDesc("somehost"); }
@Test public void testMakeConnDesc_unixSock() { final ConnectionDescriptor desc = ConnectionDescriptor .makeConnDesc("unix: Assert.assertTrue(desc instanceof UnixSocketConnectionDescriptor); }