Skip to content

Commit af45d6c

Browse files
committed
feat(junit): Improve generated code readability
1 parent 7536862 commit af45d6c

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

utils/junit-utils/src/main/java/datadog/trace/junit/utils/config/WithConfigExtension.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,24 @@ public class WithConfigExtension
7575

7676
@Override
7777
public void beforeAll(ExtensionContext context) {
78+
/*
79+
* Patch config classes to make them modifiable.
80+
*/
81+
// Install config transformer error listener
7882
if (!configTransformerInstalled) {
7983
installConfigTransformer();
8084
configTransformerInstalled = true;
8185
}
86+
// Make config instance modifiable
8287
makeConfigInstanceModifiable();
88+
// Verify that config class transformation succeeded
8389
assertFalse(configModificationFailed, "Config class modification failed");
8490
if (isConfigInstanceModifiable) {
8591
checkConfigTransformation();
8692
}
93+
/*
94+
* Back up config and apply class-level config values.
95+
*/
8796
if (originalSystemProperties == null) {
8897
saveProperties();
8998
}
@@ -127,8 +136,8 @@ private static void applyDeclaredConfig(ExtensionContext context) {
127136
}
128137

129138
private static void applyClassLevelConfig(ExtensionContext context) {
130-
// Walk the entire class hierarchy so annotations on superclasses are applied
131-
// (topmost first, then subclass overrides)
139+
// Walk the entire class hierarchy so annotations on superclasses and apply topmost first, then
140+
// subclass overrides.
132141
Class<?> testClass = context.getRequiredTestClass();
133142
List<Class<?>> hierarchy = new ArrayList<>();
134143
for (Class<?> cls = testClass; cls != null; cls = cls.getSuperclass()) {
@@ -166,12 +175,12 @@ private static void applyConfig(WithConfig cfg) {
166175
}
167176

168177
private static void setSysProperty(String name, String value, boolean addPrefix) {
169-
String prefixedName = name.startsWith("dd.") || !addPrefix ? name : "dd." + name;
178+
String prefixedName = addPrefix && !name.startsWith("dd.") ? "dd." + name : name;
170179
System.setProperty(prefixedName, value);
171180
}
172181

173182
private static void setEnvVariable(String name, String value, boolean addPrefix) {
174-
String prefixedName = name.startsWith("DD_") || !addPrefix ? name : "DD_" + name;
183+
String prefixedName = addPrefix && !name.startsWith("DD_") ? "DD_" + name : name;
175184
environmentVariables.set(prefixedName, value);
176185
}
177186

@@ -193,7 +202,7 @@ public static void removeSysConfig(String name) {
193202
}
194203

195204
public static void removeSysConfig(String name, boolean addPrefix) {
196-
String prefixedName = name.startsWith("dd.") || !addPrefix ? name : "dd." + name;
205+
String prefixedName = addPrefix && !name.startsWith("dd.") ? "dd." + name : name;
197206
System.clearProperty(prefixedName);
198207
rebuildConfig();
199208
}
@@ -212,7 +221,7 @@ public static void removeEnvConfig(String name) {
212221
}
213222

214223
public static void removeEnvConfig(String name, boolean addPrefix) {
215-
String prefixedName = name.startsWith("DD_") || !addPrefix ? name : "DD_" + name;
224+
String prefixedName = addPrefix && !name.startsWith("DD_") ? "DD_" + name : name;
216225
environmentVariables.removePrefixed(prefixedName);
217226
rebuildConfig();
218227
}

0 commit comments

Comments
 (0)