DnsCacheManipulator { public static void setDnsCache(String host, String... ips) { try { InetAddressCacheUtil.setInetAddressCache(host, ips, NEVER_EXPIRATION); } catch (Exception e) { final String message = String.format("Fail to setDnsCache for host %s ip %s, cause: %s", host, Arrays.toString(ips), e.toString()); throw new DnsCacheManipulatorException(message, e); } } private  DnsCacheManipulator(); static void setDnsCache(String host, String... ips); static void setDnsCache(long expireMillis, String host, String... ips); static void setDnsCache(Properties properties); static void loadDnsCacheConfig(); static void loadDnsCacheConfig(String propertiesFileName); @Nullable static DnsCacheEntry getDnsCache(String host); @Deprecated static List<DnsCacheEntry> getAllDnsCache(); static List<DnsCacheEntry> listDnsCache(); static DnsCache getWholeDnsCache(); static void removeDnsCache(String host); static void clearDnsCache(); static int getDnsCachePolicy(); static void setDnsCachePolicy(int cacheSeconds); static int getDnsNegativeCachePolicy(); static void setDnsNegativeCachePolicy(int negativeCacheSeconds);  }
@Test public void test_setMultiIp() throws Exception { DnsCacheManipulator.setDnsCache("multi.ip.com", "1.1.1.1", "2.2.2.2"); String ip = InetAddress.getByName("multi.ip.com").getHostAddress(); assertEquals("1.1.1.1", ip); InetAddress[] all = InetAddress.getAllByName("multi.ip.com"); assertEquals(2, all.length); String[] ips = {all[0].getHostAddress(), all[1].getHostAddress()}; assertArrayEquals(new String[]{"1.1.1.1", "2.2.2.2"}, ips); }
@Test public void test_canSetExistedDomain_canExpire_thenReLookupBack() throws Exception { final String domain = "github.com"; Set<String> expected = getAllHostAddresses(domain); DnsCacheManipulator.setDnsCache(30, domain, IP3); assertEquals(IP3, InetAddress.getByName(domain).getHostAddress()); sleep(32); assertEquals(expected, getAllHostAddresses(domain)); }