You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2019/12/31 14:07:18 UTC

[commons-lang] branch master updated: [LANG-1508] Add org.apache.commons.lang3.SystemUtils.getUserName().

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git


The following commit(s) were added to refs/heads/master by this push:
     new b46d417  [LANG-1508] Add org.apache.commons.lang3.SystemUtils.getUserName().
b46d417 is described below

commit b46d417ef61b69f2e4562e596d55466907d6933d
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Dec 31 09:07:14 2019 -0500

    [LANG-1508] Add org.apache.commons.lang3.SystemUtils.getUserName().
---
 src/changes/changes.xml                            |  1 +
 .../java/org/apache/commons/lang3/SystemUtils.java | 38 +++++++++++++++++++++-
 .../org/apache/commons/lang3/SystemUtilsTest.java  | 10 ++++++
 3 files changed, 48 insertions(+), 1 deletion(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index d403fbb..d311e52 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -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.">
diff --git a/src/main/java/org/apache/commons/lang3/SystemUtils.java b/src/main/java/org/apache/commons/lang3/SystemUtils.java
index a954223..7d5c2cc 100644
--- a/src/main/java/org/apache/commons/lang3/SystemUtils.java
+++ b/src/main/java/org/apache/commons/lang3/SystemUtils.java
@@ -49,6 +49,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.
      */
     private static final String USER_DIR_KEY = "user.dir";
@@ -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>
@@ -1688,6 +1693,37 @@ public class SystemUtils {
     }
 
     /**
+     * <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}.
      *
      * @return {@code true} if {@code JAVA_AWT_HEADLESS} is {@code "true"}, {@code false} otherwise.
diff --git a/src/test/java/org/apache/commons/lang3/SystemUtilsTest.java b/src/test/java/org/apache/commons/lang3/SystemUtilsTest.java
index d050532..c39ebd6 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 class SystemUtilsTest {
         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() {