DNS { public static String getDefaultHost(@Nullable String strInterface, @Nullable String nameserver, boolean tryfallbackResolution) throws UnknownHostException { if (strInterface == null || "default".equals(strInterface)) { return cachedHostname; } if (nameserver != null && "default".equals(nameserver)) { nameserver = null; } String[] hosts = getHosts(strInterface, nameserver, tryfallbackResolution); return hosts[0]; }  static String reverseDns(InetAddress hostIp, @Nullable String ns); static String[] getIPs(String strInterface); static String[] getIPs(String strInterface,
      boolean returnSubinterfaces); static String getDefaultIP(String strInterface); static String[] getHosts(String strInterface,
                                  @Nullable String nameserver,
                                  boolean tryfallbackResolution); static String[] getHosts(String strInterface); static String getDefaultHost(@Nullable String strInterface,
                                      @Nullable String nameserver,
                                      boolean tryfallbackResolution); static String getDefaultHost(@Nullable String strInterface); static String getDefaultHost(@Nullable String strInterface,
                                      @Nullable String nameserver); static List<InetAddress> getIPsAsInetAddressList(String strInterface,
      boolean returnSubinterfaces);  }
@Test public void testGetLocalHost() throws Exception { String hostname = DNS.getDefaultHost(DEFAULT); assertNotNull(hostname); }
@Test public void testGetLocalHostIsFast() throws Exception { String hostname1 = DNS.getDefaultHost(DEFAULT); assertNotNull(hostname1); String hostname2 = DNS.getDefaultHost(DEFAULT); long t1 = Time.now(); String hostname3 = DNS.getDefaultHost(DEFAULT); long t2 = Time.now(); assertEquals(hostname3, hostname2); assertEquals(hostname2, hostname1); long interval = t2 - t1; assertTrue( "Took too long to determine local host - caching is not working", interval < 20000); }
@Test public void testNullDnsServer() throws Exception { String host = DNS.getDefaultHost(getLoopbackInterface(), null); assertThat(host, is(DNS.getDefaultHost(getLoopbackInterface()))); }
@Test public void testDefaultDnsServer() throws Exception { String host = DNS.getDefaultHost(getLoopbackInterface(), DEFAULT); assertThat(host, is(DNS.getDefaultHost(getLoopbackInterface()))); }
@Test (timeout=60000) public void testLookupWithHostsFallback() throws Exception { assumeNotWindows(); final String oldHostname = changeDnsCachedHostname(DUMMY_HOSTNAME); try { String hostname = DNS.getDefaultHost( getLoopbackInterface(), INVALID_DNS_SERVER, true); assertThat(hostname, not(DUMMY_HOSTNAME)); } finally { changeDnsCachedHostname(oldHostname); } }
@Test(timeout=60000) public void testLookupWithoutHostsFallback() throws Exception { final String oldHostname = changeDnsCachedHostname(DUMMY_HOSTNAME); try { String hostname = DNS.getDefaultHost( getLoopbackInterface(), INVALID_DNS_SERVER, false); assertThat(hostname, is(DUMMY_HOSTNAME)); } finally { changeDnsCachedHostname(oldHostname); } }