### Question:
ConfigUtils { public static String[] getHostAndPort(String s) throws ConfigException { if (s.startsWith("[")) { int i = s.indexOf("]"); if (i < 0) { throw new ConfigException(s + " starts with '[' but has no matching ']:'"); } if (i + 2 == s.length()) { throw new ConfigException(s + " doesn't have a port after colon"); } if (i + 2 < s.length()) { String[] sa = s.substring(i + 2).split(":"); String[] nsa = new String[sa.length + 1]; nsa[0] = s.substring(1, i); System.arraycopy(sa, 0, nsa, 1, sa.length); return nsa; } return new String[]{s.replaceAll("\\[|\\]", "")}; } else { return s.split(":"); } }  static String getClientConfigStr(String configData); static String[] getHostAndPort(String s);  }### Answer:
@Test public void testGetHostAndPortWithIPv6() throws ConfigException { String[] nsa = ConfigUtils.getHostAndPort("[2001:db8:85a3:8d3:1319:8a2e:370:7348]:443"); assertEquals(nsa[0], "2001:db8:85a3:8d3:1319:8a2e:370:7348"); assertEquals(nsa[1], "443"); nsa = ConfigUtils.getHostAndPort("[2001:db8:1::242:ac11:2]:2888:3888"); assertEquals(nsa[0], "2001:db8:1::242:ac11:2"); assertEquals(nsa[1], "2888"); assertEquals(nsa[2], "3888"); }
@Test public void testGetHostAndPortWithIPv4() throws ConfigException { String[] nsa = ConfigUtils.getHostAndPort("127.0.0.1:443"); assertEquals(nsa[0], "127.0.0.1"); assertEquals(nsa[1], "443"); nsa = ConfigUtils.getHostAndPort("127.0.0.1:2888:3888"); assertEquals(nsa[0], "127.0.0.1"); assertEquals(nsa[1], "2888"); assertEquals(nsa[2], "3888"); }
@Test public void testGetHostAndPortWithoutBracket() { assertThrows(ConfigException.class, () -> { String[] nsa = ConfigUtils.getHostAndPort("[2001:db8:85a3:8d3:1319:8a2e:370:7348"); }); }
@Test public void testGetHostAndPortWithoutPortAfterColon() { assertThrows(ConfigException.class, () -> { String[] nsa = ConfigUtils.getHostAndPort("[2001:db8:1::242:ac11:2]:"); }); }
@Test public void testGetHostAndPortWithoutPort() throws ConfigException { String[] nsa = ConfigUtils.getHostAndPort("127.0.0.1"); assertEquals(nsa[0], "127.0.0.1"); assertEquals(nsa.length, 1); nsa = ConfigUtils.getHostAndPort("[2001:db8:1::242:ac11:2]"); assertEquals(nsa[0], "2001:db8:1::242:ac11:2"); assertEquals(nsa.length, 1); }