diff --git a/src/main/java/dev/comfast/util/Utils.java b/src/main/java/dev/comfast/util/Utils.java index 184fe3c..56e9d34 100644 --- a/src/main/java/dev/comfast/util/Utils.java +++ b/src/main/java/dev/comfast/util/Utils.java @@ -68,8 +68,9 @@ public static List> transposeMatrix(final List> matrix) { * e.g.
{@code
      *     // here "my.timeout" is 3000
      *     withSystemProp("my.timeout", () -> {
-     *         System.setProperty("my.timeout", "0");
-     *         doSomeTests();
+     *         System.setProperty("my.timeout", "456");
+     *         //or even System.clearProperty("my.timeout");
+     *         doSomeTestsWith456Timeout();
      *     })
      *     // here "my.timeout" is restored to 3000
      * }
@@ -77,8 +78,26 @@ public static  List> transposeMatrix(final List> matrix) {
      * }
      */
     public static void withSystemProp(String systemPropertyName, Runnable func) {
+        withSystemProp(systemPropertyName, null, func);
+    }
+
+    /**
+     * Restore system property after func is done.
+     * It's safe to edit it inside given function, for test purposes or any other.
+     * e.g. 
{@code
+     *     // here "my.timeout" is 3000
+     *     withSystemProp("my.timeout", "0" () -> {
+     *         doSomeTestsWithZeroTimeout();
+     *     })
+     *     // here "my.timeout" is restored to 3000
+     * }
+     * @param func within this function can edit system prop freely without side effects for the rest of the code
+     * }
+     */
+    public static void withSystemProp(String systemPropertyName, String systemPropertyValue, Runnable func) {
         final String restoreValue = getProperty(systemPropertyName);
         try {
+            if(systemPropertyValue != null) System.setProperty(systemPropertyName, systemPropertyValue);
             func.run();
         } finally {
             if(restoreValue == null) clearProperty(systemPropertyName);
diff --git a/src/test/java/dev/comfast/util/UtilsTest.java b/src/test/java/dev/comfast/util/UtilsTest.java
index b7bc336..a111ee9 100644
--- a/src/test/java/dev/comfast/util/UtilsTest.java
+++ b/src/test/java/dev/comfast/util/UtilsTest.java
@@ -40,18 +40,25 @@ void transposeMatrixTest() {
     @Test
     void withSystemPropTest() {
         //given
-        final String KEY = "xx.test", VALUE = "test value";
-        System.setProperty(KEY, VALUE);
+        final String KEY = "xx.test",
+            INITIAL_VALUE = "initial value",
+            CHANGED_VALUE = "xx is changed";
+        System.setProperty(KEY, INITIAL_VALUE);
 
-        //when
-        withSystemProp(KEY, () -> System.setProperty(KEY, "changed value"));
+        //when modify property in func
+        withSystemProp(KEY, () -> System.setProperty(KEY, CHANGED_VALUE));
         assertThat(System.getProperty(KEY))
-            .isEqualTo(VALUE);
+            .isEqualTo(INITIAL_VALUE);
 
-        //when
+        //when clear property
         withSystemProp(KEY, () -> System.clearProperty(KEY));
         assertThat(System.getProperty(KEY))
-            .isEqualTo(VALUE);
+            .isEqualTo(INITIAL_VALUE);
+
+        //when value passed
+        withSystemProp(KEY, CHANGED_VALUE, () -> assertThat(System.getProperty(KEY)).isEqualTo(CHANGED_VALUE));
+        assertThat(System.getProperty(KEY))
+            .isEqualTo(INITIAL_VALUE);
     }
 
     @Test