Skip to content

Commit a3d54e9

Browse files
committed
feat(env): Improve null values handling
1 parent 44223a5 commit a3d54e9

File tree

4 files changed

+51
-9
lines changed

4 files changed

+51
-9
lines changed

components/environment/src/main/java/datadog/environment/EnvironmentVariables.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package datadog.environment;
22

3-
import javax.annotation.Nonnull;
43
import javax.annotation.Nullable;
54

65
/**
@@ -32,7 +31,7 @@ private EnvironmentVariables() {}
3231
* @return The environment variable value, {@code defaultValue} if missing, can't be retrieved or
3332
* the environment variable name is {@code null}.
3433
*/
35-
public static String getOrDefault(@Nonnull String name, String defaultValue) {
34+
public static String getOrDefault(String name, String defaultValue) {
3635
if (name == null) {
3736
return defaultValue;
3837
}

components/environment/src/main/java/datadog/environment/SystemProperties.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package datadog.environment;
22

3-
import javax.annotation.Nonnull;
3+
import javax.annotation.Nullable;
44

55
/**
66
* Safely queries system properties against security manager.
@@ -18,7 +18,7 @@ private SystemProperties() {}
1818
* @return The system property value, {@code null} if missing, can't be retrieved, or the system
1919
* property name is {@code null}.
2020
*/
21-
public static String get(String property) {
21+
public static @Nullable String get(String property) {
2222
return getOrDefault(property, null);
2323
}
2424

@@ -31,7 +31,7 @@ public static String get(String property) {
3131
* @return The system property value, {@code defaultValue} if missing, can't be retrieved, or the
3232
* system property name is {@code null}.
3333
*/
34-
public static String getOrDefault(@Nonnull String property, String defaultValue) {
34+
public static String getOrDefault(String property, String defaultValue) {
3535
if (property == null) {
3636
return defaultValue;
3737
}
@@ -50,11 +50,32 @@ public static String getOrDefault(@Nonnull String property, String defaultValue)
5050
* @return {@code true} if the system property was successfully set, {@code} false otherwise.
5151
*/
5252
public static boolean set(String property, String value) {
53+
if (property == null || value == null) {
54+
return false;
55+
}
5356
try {
5457
System.setProperty(property, value);
5558
return true;
5659
} catch (SecurityException ignored) {
5760
return false;
5861
}
5962
}
63+
64+
/**
65+
* Clears a system property.
66+
*
67+
* @param property The system property name to clear.
68+
* @return The previous value of the system property, {@code null} if there was no prior property
69+
* and property can't be cleared.
70+
*/
71+
public static @Nullable String clear(String property) {
72+
if (property == null) {
73+
return null;
74+
}
75+
try {
76+
return System.clearProperty(property);
77+
} catch (SecurityException ignored) {
78+
return null;
79+
}
80+
}
6081
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@ParametersAreNonnullByDefault
2+
package datadog.environment;
3+
4+
import javax.annotation.ParametersAreNonnullByDefault;

components/environment/src/test/java/datadog/environment/SystemPropertiesTest.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,29 @@ void testGetOrDefault() {
3838

3939
@Test
4040
void testSet() {
41-
String testProperty = "test.property";
42-
String testValue = "test-value";
43-
assumeTrue(SystemProperties.get(testProperty) == null);
44-
41+
String testProperty = "test.set.property";
42+
String testValue = "test.set.value";
43+
assertNull(SystemProperties.get(testProperty));
4544
assertTrue(SystemProperties.set(testProperty, testValue));
4645
assertEquals(testValue, SystemProperties.get(testProperty));
46+
// Null values
47+
assertDoesNotThrow(() -> SystemProperties.set(testProperty, null));
48+
assertFalse(SystemProperties.set(testProperty, null));
49+
assertDoesNotThrow(() -> SystemProperties.set(null, testValue));
50+
assertFalse(SystemProperties.set(null, testValue));
51+
}
52+
53+
@Test
54+
void testClear() {
55+
String testProperty = "test.clear.property";
56+
String testValue = "test.clear.value";
57+
assertNull(SystemProperties.get(testProperty));
58+
assertNull(SystemProperties.clear(testProperty));
59+
assumeTrue(SystemProperties.set(testProperty, testValue));
60+
assertEquals(testValue, SystemProperties.clear(testProperty));
61+
assertNull(SystemProperties.clear(testProperty));
62+
// Null values
63+
assertDoesNotThrow(() -> SystemProperties.clear(null));
64+
assertNull(SystemProperties.clear(null));
4765
}
4866
}

0 commit comments

Comments
 (0)