diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/DNS.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/DNS.java index ffe7ee5676..b737b0445b 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/DNS.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/DNS.java @@ -18,6 +18,7 @@ package org.apache.hadoop.net; +import org.apache.hadoop.thirdparty.com.google.common.annotations.VisibleForTesting; import org.apache.hadoop.thirdparty.com.google.common.net.InetAddresses; import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.classification.InterfaceStability; @@ -58,7 +59,7 @@ public class DNS { * The cached hostname -initially null. */ - private static final String cachedHostname = resolveLocalHostname(); + private static String cachedHostname = resolveLocalHostname(); private static final String cachedHostAddress = resolveLocalHostIPAddress(); private static final String LOCALHOST = "localhost"; @@ -448,4 +449,14 @@ public static List getIPsAsInetAddressList(String strInterface, } return new Vector(allAddrs); } + + @VisibleForTesting + static String getCachedHostname() { + return cachedHostname; + } + + @VisibleForTesting + static void setCachedHostname(String hostname) { + cachedHostname = hostname; + } } diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestDNS.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestDNS.java index c114c35651..0e1ac1deb9 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestDNS.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestDNS.java @@ -198,9 +198,9 @@ public void testRDNS() throws Exception { @Test (timeout=60000) public void testLookupWithHostsFallback() throws Exception { assumeNotWindows(); - final String oldHostname = changeDnsCachedHostname(DUMMY_HOSTNAME); - + final String oldHostname = DNS.getCachedHostname(); try { + DNS.setCachedHostname(DUMMY_HOSTNAME); String hostname = DNS.getDefaultHost( getLoopbackInterface(), INVALID_DNS_SERVER, true); @@ -208,7 +208,7 @@ public void testLookupWithHostsFallback() throws Exception { Assertions.assertThat(hostname).isNotEqualTo(DUMMY_HOSTNAME); } finally { // Restore DNS#cachedHostname for subsequent tests. - changeDnsCachedHostname(oldHostname); + DNS.setCachedHostname(oldHostname); } } @@ -220,9 +220,9 @@ public void testLookupWithHostsFallback() throws Exception { */ @Test(timeout=60000) public void testLookupWithoutHostsFallback() throws Exception { - final String oldHostname = changeDnsCachedHostname(DUMMY_HOSTNAME); - + final String oldHostname = DNS.getCachedHostname(); try { + DNS.setCachedHostname(DUMMY_HOSTNAME); String hostname = DNS.getDefaultHost( getLoopbackInterface(), INVALID_DNS_SERVER, false); @@ -231,7 +231,7 @@ public void testLookupWithoutHostsFallback() throws Exception { Assertions.assertThat(hostname).isEqualTo(DUMMY_HOSTNAME); } finally { // Restore DNS#cachedHostname for subsequent tests. - changeDnsCachedHostname(oldHostname); + DNS.setCachedHostname(oldHostname); } } @@ -240,22 +240,6 @@ private String getLoopbackInterface() throws SocketException { InetAddress.getLoopbackAddress()).getName(); } - /** - * Change DNS#cachedHostName to something which cannot be a real - * host name. Uses reflection since it is a 'private final' field. - */ - private String changeDnsCachedHostname(final String newHostname) - throws Exception { - final String oldCachedHostname = DNS.getDefaultHost(DEFAULT); - Field field = DNS.class.getDeclaredField("cachedHostname"); - field.setAccessible(true); - Field modifiersField = Field.class.getDeclaredField("modifiers"); - modifiersField.setAccessible(true); - modifiersField.set(field, field.getModifiers() & ~Modifier.FINAL); - field.set(null, newHostname); - return oldCachedHostname; - } - /** * Test that the name "localhost" resolves to something. *