HostsFileParser { public static HostsFileEntries parse() throws IOException { return parse(locateHostsFile()); } private  HostsFileParser(); static HostsFileEntries parseSilently(); static HostsFileEntries parse(); static HostsFileEntries parse(File file); static HostsFileEntries parse(Reader reader);  }
@Test public void testParse() throws IOException { String hostsString = new StringBuilder() .append("127.0.0.1 host1").append("\n") .append("::1 host1").append("\n") .append("\n") .append("192.168.0.1\thost2").append("\n") .append("#comment").append("\n") .append(" #comment ").append("\n") .append("192.168.0.2 host3 #comment").append("\n") .append("192.168.0.3 host4 host5 host6").append("\n") .append("192.168.0.4 host4").append("\n") .append("192.168.0.5 HOST7").append("\n") .append("192.168.0.6 host7").append("\n") .toString(); HostsFileEntries entries = HostsFileParser.parse(new BufferedReader(new StringReader(hostsString))); Map<String, Inet4Address> inet4Entries = entries.inet4Entries(); Map<String, Inet6Address> inet6Entries = entries.inet6Entries(); assertEquals("Expected 7 IPv4 entries", 7, inet4Entries.size()); assertEquals("Expected 1 IPv6 entries", 1, inet6Entries.size()); assertEquals("127.0.0.1", inet4Entries.get("host1").getHostAddress()); assertEquals("192.168.0.1", inet4Entries.get("host2").getHostAddress()); assertEquals("192.168.0.2", inet4Entries.get("host3").getHostAddress()); assertEquals("192.168.0.3", inet4Entries.get("host4").getHostAddress()); assertEquals("192.168.0.3", inet4Entries.get("host5").getHostAddress()); assertEquals("192.168.0.3", inet4Entries.get("host6").getHostAddress()); assertNotNull("uppercase host doesn't resolve", inet4Entries.get("host7")); assertEquals("192.168.0.5", inet4Entries.get("host7").getHostAddress()); assertEquals("0:0:0:0:0:0:0:1", inet6Entries.get("host1").getHostAddress()); }