[LANG-1508] Add org.apache.commons.lang3.SystemUtils.getUserName().

This commit is contained in:
Gary Gregory 2019-12-31 09:07:14 -05:00
parent f9535b52a5
commit b46d417ef6
3 changed files with 48 additions and 1 deletions

View File

@ -90,6 +90,7 @@ The <action> type attribute can be add,update,fix,remove.
<action issue="LANG-1505" type="add" dev="ggregory" due-to="Gary Gregory">Add StopWatch convenience APIs to format times and create a simple instance.</action>
<action issue="LANG-1506" type="add" dev="ggregory" due-to="Gary Gregory">Allow a StopWatch to carry an optional message.</action>
<action issue="LANG-1507" type="add" dev="ggregory" due-to="Sam Kruglov, Mark Dacek, Marc Magon, Pascal Schumacher, Rob Tompkins, Bruno P. Kinoshita, Amey Jadiye, Gary Gregory">Add ComparableUtils #398.</action>
<action issue="LANG-1508" type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.lang3.SystemUtils.getUserName().</action>
</release>
<release version="3.9" date="2019-04-09" description="New features and bug fixes. Requires Java 8, supports Java 9, 10, 11.">

View File

@ -48,6 +48,11 @@ public class SystemUtils {
*/
private static final String USER_HOME_KEY = "user.home";
/**
* The System property key for the user name.
*/
private static final String USER_NAME_KEY = "user.name";
/**
* The System property key for the user directory.
*/
@ -837,7 +842,7 @@ public class SystemUtils {
*
* @since Java 1.1
*/
public static final String USER_NAME = getSystemProperty("user.name");
public static final String USER_NAME = getSystemProperty(USER_NAME_KEY);
/**
* <p>
@ -1687,6 +1692,37 @@ public static File getUserHome() {
return new File(System.getProperty(USER_HOME_KEY));
}
/**
* <p>
* Gets the user name.
* </p>
*
* @return a name
* @throws SecurityException if a security manager exists and its {@code checkPropertyAccess} method doesn't allow
* access to the specified system property.
* @see System#getProperty(String)
* @since 3.10
*/
public static String getUserName() {
return System.getProperty(USER_NAME_KEY);
}
/**
* <p>
* Gets the user name.
* </p>
*
* @param defaultValue A default value.
* @return a name
* @throws SecurityException if a security manager exists and its {@code checkPropertyAccess} method doesn't allow
* access to the specified system property.
* @see System#getProperty(String)
* @since 3.10
*/
public static String getUserName(final String defaultValue) {
return System.getProperty(USER_NAME_KEY, defaultValue);
}
/**
* Returns whether the {@link #JAVA_AWT_HEADLESS} value is {@code true}.
*

View File

@ -126,6 +126,16 @@ public void testGetUserHome() {
assertTrue(dir.exists());
}
/**
* Assumes no security manager exists.
*/
@Test
public void testGetUserName() {
assertEquals(System.getProperty("user.name"), SystemUtils.getUserName());
// Don't overwrite the system property in this test in case something goes awfully wrong.
assertEquals(System.getProperty("user.name", "foo"), SystemUtils.getUserName("foo"));
}
@Test
@SuppressWarnings("deprecation")
public void testIS_JAVA() {