@@ -63,6 +63,7 @@ public class WithConfigExtension
6363 private static Field configInstanceField ;
6464 private static Constructor <?> configConstructor ;
6565
66+ private static volatile boolean configTransformerInstalled = false ;
6667 private static volatile boolean isConfigInstanceModifiable = false ;
6768 private static volatile boolean configModificationFailed = false ;
6869
@@ -74,9 +75,15 @@ public class WithConfigExtension
7475
7576 @ Override
7677 public void beforeAll (ExtensionContext context ) {
77- installConfigTransformer ();
78+ if (!configTransformerInstalled ) {
79+ installConfigTransformer ();
80+ configTransformerInstalled = true ;
81+ }
7882 makeConfigInstanceModifiable ();
7983 assertFalse (configModificationFailed , "Config class modification failed" );
84+ if (isConfigInstanceModifiable ) {
85+ checkConfigTransformation ();
86+ }
8087 if (originalSystemProperties == null ) {
8188 saveProperties ();
8289 }
@@ -86,10 +93,10 @@ public void beforeAll(ExtensionContext context) {
8693 public void beforeEach (ExtensionContext context ) {
8794 restoreProperties ();
8895 environmentVariables .clear ();
96+ applyDeclaredConfig (context );
8997 if (isConfigInstanceModifiable ) {
9098 rebuildConfig ();
9199 }
92- applyDeclaredConfig (context );
93100 }
94101
95102 @ Override
@@ -140,12 +147,22 @@ private void applyDeclaredConfig(ExtensionContext context) {
140147
141148 private static void applyConfig (WithConfig cfg ) {
142149 if (cfg .env ()) {
143- injectEnvConfig (cfg .key (), cfg .value (), cfg .addPrefix ());
150+ setEnvVariable (cfg .key (), cfg .value (), cfg .addPrefix ());
144151 } else {
145- injectSysConfig (cfg .key (), cfg .value (), cfg .addPrefix ());
152+ setSysProperty (cfg .key (), cfg .value (), cfg .addPrefix ());
146153 }
147154 }
148155
156+ private static void setSysProperty (String name , String value , boolean addPrefix ) {
157+ String prefixedName = name .startsWith ("dd." ) || !addPrefix ? name : "dd." + name ;
158+ System .setProperty (prefixedName , value );
159+ }
160+
161+ private static void setEnvVariable (String name , String value , boolean addPrefix ) {
162+ String prefixedName = name .startsWith ("DD_" ) || !addPrefix ? name : "DD_" + name ;
163+ environmentVariables .set (prefixedName , value );
164+ }
165+
149166 // endregion
150167
151168 // region Public static API for imperative config injection
@@ -155,9 +172,7 @@ public static void injectSysConfig(String name, String value) {
155172 }
156173
157174 public static void injectSysConfig (String name , String value , boolean addPrefix ) {
158- checkConfigTransformation ();
159- String prefixedName = name .startsWith ("dd." ) || !addPrefix ? name : "dd." + name ;
160- System .setProperty (prefixedName , value );
175+ setSysProperty (name , value , addPrefix );
161176 rebuildConfig ();
162177 }
163178
@@ -166,7 +181,6 @@ public static void removeSysConfig(String name) {
166181 }
167182
168183 public static void removeSysConfig (String name , boolean addPrefix ) {
169- checkConfigTransformation ();
170184 String prefixedName = name .startsWith ("dd." ) || !addPrefix ? name : "dd." + name ;
171185 System .clearProperty (prefixedName );
172186 rebuildConfig ();
@@ -177,9 +191,7 @@ public static void injectEnvConfig(String name, String value) {
177191 }
178192
179193 public static void injectEnvConfig (String name , String value , boolean addPrefix ) {
180- checkConfigTransformation ();
181- String prefixedName = name .startsWith ("DD_" ) || !addPrefix ? name : "DD_" + name ;
182- environmentVariables .set (prefixedName , value );
194+ setEnvVariable (name , value , addPrefix );
183195 rebuildConfig ();
184196 }
185197
@@ -188,7 +200,6 @@ public static void removeEnvConfig(String name) {
188200 }
189201
190202 public static void removeEnvConfig (String name , boolean addPrefix ) {
191- checkConfigTransformation ();
192203 String prefixedName = name .startsWith ("DD_" ) || !addPrefix ? name : "DD_" + name ;
193204 environmentVariables .removePrefixed (prefixedName );
194205 rebuildConfig ();
@@ -254,7 +265,6 @@ static void makeConfigInstanceModifiable() {
254265
255266 private static void rebuildConfig () {
256267 synchronized (WithConfigExtension .class ) {
257- checkConfigTransformation ();
258268 try {
259269 Object newInstConfig = instConfigConstructor .newInstance ();
260270 instConfigInstanceField .set (null , newInstConfig );
0 commit comments