[LANG-1297] SystemUtils.getHostName() API.

This commit is contained in:
Gary Gregory 2016-12-14 14:22:34 -08:00
parent cdfb2aa1e1
commit bd5cc81ed7
3 changed files with 20 additions and 0 deletions

View File

@ -72,6 +72,7 @@ The <action> type attribute can be add,update,fix,remove.
<action issue="LANG-1277" type="update" dev="pschumacher" due-to="yufcuy">StringUtils#getLevenshteinDistance reduce memory consumption</action>
<action issue="LANG-1279" type="update" dev="ggregory">Update Java requirement from Java 6 to 7.</action>
<action issue="LANG-1143" type="update" dev="pschumacher" due-to="sebb">StringUtils should use toXxxxCase(int) rather than toXxxxCase(char)</action>
<action issue="LANG-1297" type="update" dev="ggregory">Add SystemUtils.getHostName() API.</action>
</release>
<release version="3.5" date="2016-10-13" description="New features including Java 9 detection">

View File

@ -1493,6 +1493,16 @@ public class SystemUtils {
return new File(System.getProperty(JAVA_HOME_KEY));
}
/**
* Gets the host name.
*
* @return the host name.
* @since 3.6
*/
public static String getHostName() {
return SystemUtils.IS_OS_WINDOWS ? System.getenv("COMPUTERNAME") : System.getenv("HOSTNAME");
}
/**
* <p>
* Gets the Java IO temporary directory as a {@code File}.

View File

@ -58,6 +58,15 @@ public class SystemUtilsTest {
assertFalse(Modifier.isFinal(SystemUtils.class.getModifiers()));
}
@Test
public void testGetHostName() {
final String hostName = SystemUtils.getHostName();
Assert.assertNotNull(hostName);
Assert.assertFalse(hostName.isEmpty());
String expected = SystemUtils.IS_OS_WINDOWS ? System.getenv("COMPUTERNAME") : System.getenv("HOSTNAME");
Assert.assertEquals(expected, hostName);
}
/**
* Assumes no security manager exists.
*/