-
Notifications
You must be signed in to change notification settings - Fork 338
Expand file tree
/
Copy pathConfigInversionExtension.java
More file actions
40 lines (34 loc) · 1.63 KB
/
ConfigInversionExtension.java
File metadata and controls
40 lines (34 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package datadog.trace.test;
import datadog.trace.config.inversion.ConfigHelper;
import datadog.trace.config.inversion.ConfigHelper.StrictnessPolicy;
import java.util.List;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
/**
* JUnit 5 extension that enforces {@link StrictnessPolicy#STRICT_TEST} mode on {@link ConfigHelper}
* during test execution. Any access to an unsupported configuration key will throw {@link
* IllegalArgumentException} and be collected for assertion in {@link #afterAll}.
*
* <p>Registered via {@code META-INF/services/org.junit.jupiter.api.extension.Extension} for
* automatic discovery. Requires {@code junit.jupiter.extensions.autodetection.enabled=true}.
*/
public class ConfigInversionExtension implements BeforeAllCallback, AfterAllCallback {
private StrictnessPolicy previousPolicy;
@Override
public void beforeAll(ExtensionContext ctx) {
previousPolicy = ConfigHelper.get().configInversionStrictFlag();
ConfigHelper.get().setConfigInversionStrict(StrictnessPolicy.STRICT_TEST);
}
@Override
public void afterAll(ExtensionContext ctx) {
List<String> unsupported = ConfigHelper.get().drainUnsupportedConfigs();
ConfigHelper.get().setConfigInversionStrict(previousPolicy);
if (!unsupported.isEmpty()) {
throw new AssertionError(
"Unsupported configurations found during test. "
+ "Add these to metadata/supported-configurations.json or opt out with StrictnessPolicy.TEST:\n "
+ String.join("\n ", unsupported));
}
}
}