Skip to content

Commit d81db18

Browse files
authored
fix: debugThreadPool & shouldCheckCRDAndValidateLocalModel should work (#1015)
Fixes #1014
1 parent e5ee911 commit d81db18

File tree

2 files changed

+75
-2
lines changed
  • operator-framework-core/src
    • main/java/io/javaoperatorsdk/operator/api/config
    • test/java/io/javaoperatorsdk/operator/api/config

2 files changed

+75
-2
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/Utils.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,28 @@ public static boolean isValidateCustomResourcesEnvVarSet() {
6464
}
6565

6666
public static boolean shouldCheckCRDAndValidateLocalModel() {
67-
return Boolean.getBoolean(System.getProperty(CHECK_CRD_ENV_KEY, "true"));
67+
return getBooleanFromSystemPropsOrDefault(CHECK_CRD_ENV_KEY, true);
6868
}
6969

7070
public static boolean debugThreadPool() {
71-
return Boolean.getBoolean(System.getProperty(DEBUG_THREAD_POOL_ENV_KEY, "false"));
71+
return getBooleanFromSystemPropsOrDefault(DEBUG_THREAD_POOL_ENV_KEY, false);
72+
}
73+
74+
static boolean getBooleanFromSystemPropsOrDefault(String propertyName, boolean defaultValue) {
75+
var property = System.getProperty(propertyName);
76+
if (property == null) {
77+
return defaultValue;
78+
} else {
79+
property = property.trim().toLowerCase();
80+
switch (property) {
81+
case "true":
82+
return true;
83+
case "false":
84+
return false;
85+
default:
86+
return defaultValue;
87+
}
88+
}
7289
}
7390

7491
public static Class<?> getFirstTypeArgumentFromExtendedClass(Class<?> clazz) {

operator-framework-core/src/test/java/io/javaoperatorsdk/operator/api/config/UtilsTest.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,62 @@
1212

1313
class UtilsTest {
1414

15+
@Test
16+
void shouldCheckCRDAndValidateLocalModelByDefault() {
17+
assertTrue(Utils.shouldCheckCRDAndValidateLocalModel());
18+
}
19+
20+
@Test
21+
void shouldNotDebugThreadPoolByDefault() {
22+
assertFalse(Utils.debugThreadPool());
23+
}
24+
25+
@Test
26+
void askingForNonexistentPropertyShouldReturnDefault() {
27+
final var key = "foo";
28+
assertNull(System.getProperty(key));
29+
assertFalse(Utils.getBooleanFromSystemPropsOrDefault(key, false));
30+
assertTrue(Utils.getBooleanFromSystemPropsOrDefault(key, true));
31+
}
32+
33+
@Test
34+
void askingForExistingPropertyShouldReturnItIfBoolean() {
35+
final var key = "foo";
36+
try {
37+
System.setProperty(key, "true");
38+
assertNotNull(System.getProperty(key));
39+
assertTrue(Utils.getBooleanFromSystemPropsOrDefault(key, false));
40+
assertTrue(Utils.getBooleanFromSystemPropsOrDefault(key, true));
41+
42+
System.setProperty(key, "TruE");
43+
assertTrue(Utils.getBooleanFromSystemPropsOrDefault(key, false));
44+
assertTrue(Utils.getBooleanFromSystemPropsOrDefault(key, true));
45+
46+
System.setProperty(key, " \tTRUE ");
47+
assertTrue(Utils.getBooleanFromSystemPropsOrDefault(key, false));
48+
assertTrue(Utils.getBooleanFromSystemPropsOrDefault(key, true));
49+
50+
System.setProperty(key, " \nFalSe \t ");
51+
assertFalse(Utils.getBooleanFromSystemPropsOrDefault(key, false));
52+
assertFalse(Utils.getBooleanFromSystemPropsOrDefault(key, true));
53+
} finally {
54+
System.clearProperty(key);
55+
}
56+
}
57+
58+
@Test
59+
void askingForExistingNonBooleanPropertyShouldReturnDefaultValue() {
60+
final var key = "foo";
61+
try {
62+
System.setProperty(key, "bar");
63+
assertNotNull(System.getProperty(key));
64+
assertFalse(Utils.getBooleanFromSystemPropsOrDefault(key, false));
65+
assertTrue(Utils.getBooleanFromSystemPropsOrDefault(key, true));
66+
} finally {
67+
System.clearProperty(key);
68+
}
69+
}
70+
1571
@Test
1672
void getsFirstTypeArgumentFromExtendedClass() {
1773
Class<?> res =

0 commit comments

Comments
 (0)