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
commit 9b383f3fadccab020874aa5c69704790e03c4f4f Author: Gary Gregory <[email protected]> AuthorDate: Wed Jun 25 09:21:06 2025 -0400 Add org.apache.commons.lang3.SystemProperties.isPropertySet(String) --- src/changes/changes.xml | 1 + .../java/org/apache/commons/lang3/StringUtils.java | 5 ++++ .../org/apache/commons/lang3/SystemProperties.java | 17 ++++++++++++++ .../apache/commons/lang3/SystemPropertiesTest.java | 27 +++++++++++++++++----- 4 files changed, 44 insertions(+), 6 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index f3b27f0ae..cab8217d7 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -138,6 +138,7 @@ The <action> type attribute can be add,update,fix,remove. <action type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.lang3.concurrent.locks.LockingVisitors.ReentrantLockVisitor.</action> <action type="add" dev="ggregory" due-to="Gary Gregory">Add builders for LockingVisitors implementations.</action> <action type="add" dev="ggregory" due-to="Gary Gregory">Add EnumSet.stream(Class).</action> + <action type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.lang3.SystemProperties.isPropertySet(String).</action> <!-- UPDATE --> <action type="update" dev="ggregory" due-to="Gary Gregory, Dependabot">Bump org.apache.commons:commons-parent from 73 to 85 #1267, #1277, #1283, #1288, #1302, #1377.</action> <action type="update" dev="ggregory" due-to="Gary Gregory, Dependabot">[site] Bump org.codehaus.mojo:taglist-maven-plugin from 3.1.0 to 3.2.1 #1300.</action> diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java index 391335062..056656c06 100644 --- a/src/main/java/org/apache/commons/lang3/StringUtils.java +++ b/src/main/java/org/apache/commons/lang3/StringUtils.java @@ -158,6 +158,11 @@ public class StringUtils { */ public static final String EMPTY = ""; + /** + * The null String {@code null}. Package-private only. + */ + static final String NULL = null; + /** * A String for linefeed LF ("\n"). * diff --git a/src/main/java/org/apache/commons/lang3/SystemProperties.java b/src/main/java/org/apache/commons/lang3/SystemProperties.java index 0b38ebab4..72f3382f6 100644 --- a/src/main/java/org/apache/commons/lang3/SystemProperties.java +++ b/src/main/java/org/apache/commons/lang3/SystemProperties.java @@ -4076,6 +4076,23 @@ public static String getUserVariant() { return getProperty(USER_VARIANT); } + /** + * Tests whether the given property is set. + * <p> + * Short-hand for {@code getProperty(property) != null}. + * </p> + * <p> + * If a {@link SecurityException} is caught, the return value is {@code false}. + * </p> + * + * @param property the system property name. + * @return whether the given property is set. + * @since 3.18.0 + */ + public static boolean isPropertySet(final String property) { + return getProperty(property) != null; + } + /** * Make private in 4.0. * diff --git a/src/test/java/org/apache/commons/lang3/SystemPropertiesTest.java b/src/test/java/org/apache/commons/lang3/SystemPropertiesTest.java index 66fdd7f2e..086d2c0d1 100644 --- a/src/test/java/org/apache/commons/lang3/SystemPropertiesTest.java +++ b/src/test/java/org/apache/commons/lang3/SystemPropertiesTest.java @@ -22,12 +22,15 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assumptions.assumeTrue; import java.util.function.Supplier; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.function.ThrowingSupplier; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import org.junitpioneer.jupiter.SetSystemProperty; import org.junitpioneer.jupiter.SetSystemProperty.SetSystemProperties; @@ -36,8 +39,8 @@ @SetSystemProperty(key = SystemPropertiesTest.KEY_TAB_1, value = "value2") }) class SystemPropertiesTest { - private static final String KEY_SPACE_1 = " "; - private static final String KEY_TAB_1 = "\t"; + static final String KEY_SPACE_1 = " "; + static final String KEY_TAB_1 = "\t"; private void basicKeyCheck(final String key) { assertNotNull(key); @@ -715,10 +718,10 @@ void testGetProperty() { @Test void testGetPropertyStringString() { - assertNull(SystemProperties.getProperty(null, (String) null)); - assertNull(SystemProperties.getProperty(StringUtils.EMPTY, (String) null)); - assertEquals("value1", SystemProperties.getProperty(KEY_SPACE_1, (String) null)); - assertEquals("value2", SystemProperties.getProperty("\t", (String) null)); + assertNull(SystemProperties.getProperty(null, StringUtils.NULL)); + assertNull(SystemProperties.getProperty(StringUtils.EMPTY, StringUtils.NULL)); + assertEquals("value1", SystemProperties.getProperty(KEY_SPACE_1, StringUtils.NULL)); + assertEquals("value2", SystemProperties.getProperty("\t", StringUtils.NULL)); assertEquals("x", SystemProperties.getProperty(null, "x")); assertEquals("x", SystemProperties.getProperty(StringUtils.EMPTY, "x")); assertEquals("value1", SystemProperties.getProperty(KEY_SPACE_1, "v")); @@ -770,4 +773,16 @@ void testGetUserTimezone() { assertDoesNotThrow(SystemProperties::getUserTimezone); } + @ParameterizedTest + @ValueSource(strings = { KEY_SPACE_1, KEY_TAB_1 }) + void testIsPropertySet(final String property) { + assertTrue(SystemProperties.isPropertySet(property)); + } + + @Test + void testIsPropertySetEdges() { + assertFalse(SystemProperties.isPropertySet(StringUtils.NULL)); + assertFalse(SystemProperties.isPropertySet(StringUtils.EMPTY)); + } + }
