HostsFileParser { public static Map<String, InetAddress> parse() throws IOException { return parse(locateHostsFile()); } private  HostsFileParser(); static Map<String, InetAddress> parseSilently(); static Map<String, InetAddress> parse(); static Map<String, InetAddress> parse(File file); static Map<String, InetAddress> parse(Reader reader);  }
@Test public void testParse() throws IOException { String hostsString = new StringBuilder() .append("127.0.0.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") .toString(); Map<String, InetAddress> entries = HostsFileParser.parse(new BufferedReader(new StringReader(hostsString))); assertEquals("Expected 6 entries", 6, entries.size()); assertEquals("127.0.0.1", entries.get("host1").getHostAddress()); assertEquals("192.168.0.1", entries.get("host2").getHostAddress()); assertEquals("192.168.0.2", entries.get("host3").getHostAddress()); assertEquals("192.168.0.3", entries.get("host4").getHostAddress()); assertEquals("192.168.0.3", entries.get("host5").getHostAddress()); assertEquals("192.168.0.3", entries.get("host6").getHostAddress()); }
@Test public void testParse() throws IOException { String hostsString = new StringBuilder() .append("127.0.0.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(); Map<String, InetAddress> entries = HostsFileParser.parse(new BufferedReader(new StringReader(hostsString))); assertEquals("Expected 7 entries", 7, entries.size()); assertEquals("127.0.0.1", entries.get("host1").getHostAddress()); assertEquals("192.168.0.1", entries.get("host2").getHostAddress()); assertEquals("192.168.0.2", entries.get("host3").getHostAddress()); assertEquals("192.168.0.3", entries.get("host4").getHostAddress()); assertEquals("192.168.0.3", entries.get("host5").getHostAddress()); assertEquals("192.168.0.3", entries.get("host6").getHostAddress()); assertNotNull("uppercase host doesn't resolve", entries.get("host7")); assertEquals("192.168.0.5", entries.get("host7").getHostAddress()); }