HostUtils { public static boolean isValidHostname(String hostname) { return (hostname != null) && REGEX_VALID_HOSTNAME.matcher(hostname).matches(); }  static boolean isValidHostname(String hostname);  }
@Test public void testIsValidHostname() throws Exception { Assert.assertTrue(HostUtils.isValidHostname("localhost")); Assert.assertTrue(HostUtils.isValidHostname("localhost.localdomain")); Assert.assertTrue(HostUtils.isValidHostname("host1.example.com")); Assert.assertTrue(HostUtils.isValidHostname("Host1.eXample.coM")); Assert.assertTrue(HostUtils.isValidHostname("host-name.example.com")); Assert.assertTrue(HostUtils.isValidHostname("123.456.789")); Assert.assertTrue(HostUtils.isValidHostname("host-123-name.ex4mpl3.c0m")); Assert.assertFalse(HostUtils.isValidHostname("host_name.example.com")); Assert.assertFalse(HostUtils.isValidHostname("host;name.example.com")); Assert.assertFalse(HostUtils.isValidHostname("host?name.example.com")); Assert.assertFalse(HostUtils.isValidHostname("host@name.example.com")); Assert.assertFalse(HostUtils.isValidHostname("host=name.example.com")); Assert.assertFalse(HostUtils.isValidHostname("host+name.example.com")); Assert.assertFalse(HostUtils.isValidHostname("host)name).example.com")); }