### Question:
SocketAddressParser { public static InetSocketAddress parse(String str) { String address, port; if (str.startsWith("[")) { int addressEnd = str.indexOf(']'); if (addressEnd == -1) throw new IllegalArgumentException(); if ((addressEnd + 2) > str.length()) throw new IllegalArgumentException(); address = str.substring(1, addressEnd); if (str.charAt(addressEnd + 1) != ':') throw new IllegalArgumentException(); port = str.substring(addressEnd + 2); } else { String[] parts = str.split(":"); if (parts.length != 2) throw new IllegalArgumentException(); address = parts[0]; port = parts[1]; } return new InetSocketAddress(address, Integer.parseInt(port)); } private  SocketAddressParser(); static InetSocketAddress parse(String str);  }### Answer:
@Test public void testIpv4() { InetSocketAddress address = SocketAddressParser.parse("198.51.100.1:443"); assertEquals(new InetSocketAddress("198.51.100.1", 443), address); }
@Test public void testIpv6() { InetSocketAddress address = SocketAddressParser.parse("[2001:DB8::1]:443"); assertEquals(new InetSocketAddress("2001:DB8::1", 443), address); }
@Test public void testHostname() { InetSocketAddress address = SocketAddressParser.parse("example.com:443"); assertEquals(new InetSocketAddress("example.com", 443), address); }
@Test(expected = IllegalArgumentException.class) public void testIpv4WithoutPort() { SocketAddressParser.parse("198.51.100.1"); }
@Test(expected = IllegalArgumentException.class) public void testIpv6WithoutPort() { SocketAddressParser.parse("[2001:DB8::1]"); }
@Test(expected = IllegalArgumentException.class) public void testHostnameWithoutPort() { SocketAddressParser.parse("example.com"); }
@Test(expected = IllegalArgumentException.class) public void testInvalidPort() { SocketAddressParser.parse("198.51.100.1:hello"); }
@Test(expected = IllegalArgumentException.class) public void testMultipleColons() { SocketAddressParser.parse("example.com:a:b"); }