Skip to content

Commit f0fe513

Browse files
committed
feat(env): Improve null values handling
1 parent 2fe6aa2 commit f0fe513

File tree

4 files changed

+50
-16
lines changed

4 files changed

+50
-16
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ private EnvironmentVariables() {}
1616
* Gets an environment variable value.
1717
*
1818
* @param name The environment variable name.
19-
* @return The environment variable value, {@code null} if missing or can't be retrieved.
19+
* @return The environment variable value, {@code null} if missing, can't be retrieved, or the
20+
* environment variable name is {@code null}.
2021
*/
2122
public static @Nullable String get(String name) {
2223
return getOrDefault(name, null);
@@ -28,9 +29,13 @@ private EnvironmentVariables() {}
2829
* @param name The environment variable name.
2930
* @param defaultValue The default value to return if the environment variable is missing or can't
3031
* be retrieved.
31-
* @return The environment variable value, {@code defaultValue} if missing or can't be retrieved.
32+
* @return The environment variable value, {@code defaultValue} if missing, can't be retrieved or
33+
* the environment variable name is {@code null}.
3234
*/
3335
public static String getOrDefault(@Nonnull String name, String defaultValue) {
36+
if (name == null) {
37+
return defaultValue;
38+
}
3439
try {
3540
String value = System.getenv(name);
3641
return value == null ? defaultValue : value;

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

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

3+
import javax.annotation.Nonnull;
4+
35
/**
46
* Safely queries system properties against security manager.
57
*
@@ -13,7 +15,8 @@ private SystemProperties() {}
1315
* Gets a system property value.
1416
*
1517
* @param property The system property name.
16-
* @return The system property value, {@code null} if missing or can't be retrieved.
18+
* @return The system property value, {@code null} if missing, can't be retrieved, or the system
19+
* property name is {@code null}.
1720
*/
1821
public static String get(String property) {
1922
return getOrDefault(property, null);
@@ -25,9 +28,13 @@ public static String get(String property) {
2528
* @param property The system property name.
2629
* @param defaultValue The default value to return if the system property is missing or can't be
2730
* retrieved.
28-
* @return The system property value, {@code defaultValue} if missing or can't be retrieved.
31+
* @return The system property value, {@code defaultValue} if missing, can't be retrieved, or the
32+
* system property name is {@code null}.
2933
*/
30-
public static String getOrDefault(String property, String defaultValue) {
34+
public static String getOrDefault(@Nonnull String property, String defaultValue) {
35+
if (property == null) {
36+
return defaultValue;
37+
}
3138
try {
3239
return System.getProperty(property, defaultValue);
3340
} catch (SecurityException ignored) {
Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,39 @@
11
package datadog.environment;
22

3-
import static org.junit.jupiter.api.Assertions.*;
3+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertNotNull;
6+
import static org.junit.jupiter.api.Assertions.assertNull;
47

58
import org.junit.jupiter.api.Test;
69

710
class EnvironmentVariablesTest {
811
private static final String EXISTING_ENV_VAR = "JAVA_8_HOME";
912
private static final String MISSING_ENV_VAR = "UNDEFINED_ENV_VAR";
13+
private static final String DEFAULT_VALUE = "DEFAULT";
1014

1115
@Test
1216
void testGet() {
17+
// Existing environment variable
1318
assertNotNull(EnvironmentVariables.get(EXISTING_ENV_VAR));
19+
// Missing environment variable
1420
assertNull(EnvironmentVariables.get(MISSING_ENV_VAR));
15-
assertThrows(NullPointerException.class, () -> EnvironmentVariables.get(null));
21+
// Null values
22+
assertDoesNotThrow(() -> EnvironmentVariables.get(null));
23+
assertNull(EnvironmentVariables.get(null));
1624
}
1725

1826
@Test
1927
void testGetOrDefault() {
28+
// Existing environment variable
2029
assertNotNull(EnvironmentVariables.getOrDefault(EXISTING_ENV_VAR, null));
21-
22-
assertEquals("", EnvironmentVariables.getOrDefault(MISSING_ENV_VAR, ""));
30+
// Missing environment variable
31+
assertEquals(DEFAULT_VALUE, EnvironmentVariables.getOrDefault(MISSING_ENV_VAR, DEFAULT_VALUE));
32+
assertNull(EnvironmentVariables.getOrDefault(MISSING_ENV_VAR, null));
33+
// Null values
34+
assertDoesNotThrow(() -> EnvironmentVariables.getOrDefault(null, DEFAULT_VALUE));
35+
assertEquals(DEFAULT_VALUE, EnvironmentVariables.getOrDefault(null, DEFAULT_VALUE));
36+
assertDoesNotThrow(() -> EnvironmentVariables.getOrDefault(MISSING_ENV_VAR, null));
2337
assertNull(EnvironmentVariables.getOrDefault(MISSING_ENV_VAR, null));
24-
25-
assertThrows(NullPointerException.class, () -> EnvironmentVariables.getOrDefault(null, ""));
2638
}
2739
}

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,32 @@
88
class SystemPropertiesTest {
99
private static final String EXISTING_SYSTEM_PROPERTY = "java.home";
1010
private static final String MISSING_SYSTEM_PROPERTY = "undefined.system.property";
11+
private static final String DEFAULT_VALUE = "DEFAULT";
1112

1213
@Test
1314
void testGet() {
15+
// Existing system properties
1416
assertNotNull(SystemProperties.get(EXISTING_SYSTEM_PROPERTY));
17+
// Missing system properties
1518
assertNull(SystemProperties.get(MISSING_SYSTEM_PROPERTY));
16-
assertThrows(NullPointerException.class, () -> SystemProperties.get(null));
19+
// Null values
20+
assertDoesNotThrow(() -> SystemProperties.get(null));
21+
assertNull(SystemProperties.get(null));
1722
}
1823

1924
@Test
2025
void testGetOrDefault() {
26+
// Existing system properties
2127
assertNotNull(SystemProperties.getOrDefault(EXISTING_SYSTEM_PROPERTY, null));
22-
23-
assertEquals("", SystemProperties.getOrDefault(MISSING_SYSTEM_PROPERTY, ""));
28+
// Missing system properties
29+
assertEquals(
30+
DEFAULT_VALUE, SystemProperties.getOrDefault(MISSING_SYSTEM_PROPERTY, DEFAULT_VALUE));
31+
assertNull(SystemProperties.getOrDefault(MISSING_SYSTEM_PROPERTY, null));
32+
// Null values
33+
assertDoesNotThrow(() -> SystemProperties.getOrDefault(null, DEFAULT_VALUE));
34+
assertEquals(DEFAULT_VALUE, SystemProperties.getOrDefault(null, DEFAULT_VALUE));
35+
assertDoesNotThrow(() -> SystemProperties.getOrDefault(MISSING_SYSTEM_PROPERTY, null));
2436
assertNull(SystemProperties.getOrDefault(MISSING_SYSTEM_PROPERTY, null));
25-
26-
assertThrows(NullPointerException.class, () -> SystemProperties.getOrDefault(null, ""));
2737
}
2838

2939
@Test

0 commit comments

Comments
 (0)