KafkaBrokerAddress { public static KafkaBrokerAddress valueOf(String brokerAddress) throws IllegalArgumentException { if (brokerAddress == null) { badArg(null); } String str = brokerAddress.trim(); if (str.isEmpty()) { badArg(brokerAddress); } int lastColon = str.lastIndexOf(":"); if (lastColon == -1) { badArg(brokerAddress); } int portNumber = -1; try { portNumber = Integer.parseInt(str.substring(lastColon + 1).trim()); validatePortNumber(portNumber); } catch (NumberFormatException e) { badArg(brokerAddress); } String hostName = str.substring(0, lastColon); hostName = validateHostname(hostName); return new KafkaBrokerAddress(hostName, portNumber); } KafkaBrokerAddress(String hostName, int portNumber); String getHostName(); int getPortNumber(); @Override String toString(); @Override boolean equals(Object o); @Override int hashCode(); static KafkaBrokerAddress valueOf(String brokerAddress); static final Comparator<KafkaBrokerAddress> BY_URL; }
@Test(expectedExceptions = IllegalArgumentException.class) public void testParseNegativePort() { KafkaBrokerAddress.valueOf("somewhere:-666"); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testParseLargePort() { KafkaBrokerAddress.valueOf("somewhere:65536"); }
@Test public void testParseMaxPort() { Assert.assertEquals(new KafkaBrokerAddress("linkedin.com", 65535), KafkaBrokerAddress.valueOf("linkedin.com:65535")); }
@Test public void testParseWhitespaces() { Assert.assertEquals(new KafkaBrokerAddress("somewhere", 666), KafkaBrokerAddress.valueOf("\t \r\nsomewhere :\t666\n")); }
@Test public void testParseIp() { Assert.assertEquals(new KafkaBrokerAddress("192.168.0.1", 666), KafkaBrokerAddress.valueOf("192.168.0.1:666")); }
@Test public void testParseIpv6() { Assert.assertEquals(new KafkaBrokerAddress("::1", 666), KafkaBrokerAddress.valueOf(" ::1 :666")); Assert.assertEquals(new KafkaBrokerAddress("0:0:0:0:0:0:0:1", 666), KafkaBrokerAddress.valueOf("0:0:0:0:0:0:0:1:666")); Assert.assertEquals(new KafkaBrokerAddress("2001:0db8:85a3:0000:0000:8a2e:0370:7334", 666), KafkaBrokerAddress.valueOf("2001:0db8:85a3:0000:0000:8a2e:0370:7334:666")); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testParseNull() { KafkaBrokerAddress.valueOf(null); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testParseEmpty() { KafkaBrokerAddress.valueOf(""); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testParseNoHost() { KafkaBrokerAddress.valueOf(":666"); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testParseNoPort() { KafkaBrokerAddress.valueOf("somewhere"); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testParseBadPort() { KafkaBrokerAddress.valueOf("somewhere:notAPort"); }