diff --git a/src/changes/changes.xml b/src/changes/changes.xml index d403fbb79..d311e529d 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -90,6 +90,7 @@ The type attribute can be add,update,fix,remove. Add StopWatch convenience APIs to format times and create a simple instance. Allow a StopWatch to carry an optional message. Add ComparableUtils #398. + Add org.apache.commons.lang3.SystemUtils.getUserName(). diff --git a/src/main/java/org/apache/commons/lang3/SystemUtils.java b/src/main/java/org/apache/commons/lang3/SystemUtils.java index a95422327..7d5c2cc4a 100644 --- a/src/main/java/org/apache/commons/lang3/SystemUtils.java +++ b/src/main/java/org/apache/commons/lang3/SystemUtils.java @@ -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); /** *

@@ -1687,6 +1692,37 @@ public static File getUserHome() { return new File(System.getProperty(USER_HOME_KEY)); } + /** + *

+ * Gets the user name. + *

+ * + * @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); + } + + /** + *

+ * Gets the user name. + *

+ * + * @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}. * diff --git a/src/test/java/org/apache/commons/lang3/SystemUtilsTest.java b/src/test/java/org/apache/commons/lang3/SystemUtilsTest.java index d05053253..c39ebd617 100644 --- a/src/test/java/org/apache/commons/lang3/SystemUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/SystemUtilsTest.java @@ -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() {