You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by gg...@apache.org on 2022/01/01 21:19:05 UTC

[logging-log4j2] branch release-2.x updated: Refactor out a new SystemPropertyTestRule out of AvailablePortSystemPropertyTestRule for use from log4j-tools.

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git


The following commit(s) were added to refs/heads/release-2.x by this push:
     new 8a90c5a  Refactor out a new SystemPropertyTestRule out of AvailablePortSystemPropertyTestRule for use from log4j-tools.
8a90c5a is described below

commit 8a90c5a5be29269515a03faf7850534968f19967
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jan 1 16:19:03 2022 -0500

    Refactor out a new SystemPropertyTestRule out of
    AvailablePortSystemPropertyTestRule for use from log4j-tools.
---
 .../test/AvailablePortSystemPropertyTestRule.java  | 51 +---------------------
 ...tyTestRule.java => SystemPropertyTestRule.java} | 47 +++++++++++---------
 2 files changed, 29 insertions(+), 69 deletions(-)

diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/test/AvailablePortSystemPropertyTestRule.java b/log4j-core/src/test/java/org/apache/logging/log4j/test/AvailablePortSystemPropertyTestRule.java
index 1bece7b..60815ac 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/test/AvailablePortSystemPropertyTestRule.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/test/AvailablePortSystemPropertyTestRule.java
@@ -17,65 +17,18 @@
 
 package org.apache.logging.log4j.test;
 
-import org.junit.rules.TestRule;
-import org.junit.runner.Description;
-import org.junit.runners.model.Statement;
-
 /**
  * A JUnit TestRule to discover an available port and save it in a system property. Useful for setting up tests using
  * Apache Active MQ.
  */
-public class AvailablePortSystemPropertyTestRule implements TestRule {
+public class AvailablePortSystemPropertyTestRule extends SystemPropertyTestRule {
 
     public static AvailablePortSystemPropertyTestRule create(final String name) {
         return new AvailablePortSystemPropertyTestRule(name);
     }
 
-    protected final String name;
-    protected int port;
-
     protected AvailablePortSystemPropertyTestRule(final String name) {
-        this.name = name;
-    }
-
-    @Override
-    public Statement apply(final Statement base, final Description description) {
-        return new Statement() {
-
-            @Override
-            public void evaluate() throws Throwable {
-                final String oldValue = System.getProperty(name);
-                try {
-                    port = AvailablePortFinder.getNextAvailable();
-                    System.setProperty(name, Integer.toString(port));
-                    base.evaluate();
-                } finally {
-                    // Restore if previously set
-                    if (oldValue != null) {
-                        System.setProperty(name, oldValue);
-                    }
-                }
-            }
-        };
-    }
-
-    public String getName() {
-        return name;
-    }
-
-    public int getPort() {
-        return port;
-    }
-
-    @Override
-    public String toString() {
-        final StringBuilder builder = new StringBuilder();
-        builder.append("AvailablePortSystemPropertyRule [name=");
-        builder.append(name);
-        builder.append(", port=");
-        builder.append(port);
-        builder.append("]");
-        return builder.toString();
+        super(name, () -> Integer.toString(AvailablePortFinder.getNextAvailable()));
     }
 
 }
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/test/AvailablePortSystemPropertyTestRule.java b/log4j-core/src/test/java/org/apache/logging/log4j/test/SystemPropertyTestRule.java
similarity index 60%
copy from log4j-core/src/test/java/org/apache/logging/log4j/test/AvailablePortSystemPropertyTestRule.java
copy to log4j-core/src/test/java/org/apache/logging/log4j/test/SystemPropertyTestRule.java
index 1bece7b..06e780e 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/test/AvailablePortSystemPropertyTestRule.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/test/SystemPropertyTestRule.java
@@ -17,25 +17,33 @@
 
 package org.apache.logging.log4j.test;
 
+import java.util.Objects;
+import java.util.function.Supplier;
+
 import org.junit.rules.TestRule;
 import org.junit.runner.Description;
 import org.junit.runners.model.Statement;
 
 /**
- * A JUnit TestRule to discover an available port and save it in a system property. Useful for setting up tests using
- * Apache Active MQ.
+ * A JUnit TestRule to set and reset a system property during a test.
  */
-public class AvailablePortSystemPropertyTestRule implements TestRule {
+public class SystemPropertyTestRule implements TestRule {
 
-    public static AvailablePortSystemPropertyTestRule create(final String name) {
-        return new AvailablePortSystemPropertyTestRule(name);
+    public static SystemPropertyTestRule create(final String name, final String value) {
+        return new SystemPropertyTestRule(name, value);
     }
 
-    protected final String name;
-    protected int port;
+    private final String name;
+    private final Supplier<String> valueSupplier;
+    private String value;
+
+    protected SystemPropertyTestRule(final String name, final String value) {
+        this(name, () -> value);
+    }
 
-    protected AvailablePortSystemPropertyTestRule(final String name) {
-        this.name = name;
+    protected SystemPropertyTestRule(final String name, final Supplier<String> value) {
+        this.name = Objects.requireNonNull(name, "name");
+        this.valueSupplier = value;
     }
 
     @Override
@@ -46,8 +54,8 @@ public class AvailablePortSystemPropertyTestRule implements TestRule {
             public void evaluate() throws Throwable {
                 final String oldValue = System.getProperty(name);
                 try {
-                    port = AvailablePortFinder.getNextAvailable();
-                    System.setProperty(name, Integer.toString(port));
+                    value = valueSupplier.get();
+                    System.setProperty(name, value);
                     base.evaluate();
                 } finally {
                     // Restore if previously set
@@ -63,19 +71,18 @@ public class AvailablePortSystemPropertyTestRule implements TestRule {
         return name;
     }
 
-    public int getPort() {
-        return port;
+    public String getValue() {
+        return value;
+    }
+
+    public Supplier<String> getValueSupplier() {
+        return valueSupplier;
     }
 
     @Override
     public String toString() {
-        final StringBuilder builder = new StringBuilder();
-        builder.append("AvailablePortSystemPropertyRule [name=");
-        builder.append(name);
-        builder.append(", port=");
-        builder.append(port);
-        builder.append("]");
-        return builder.toString();
+        // Value might be a secret...
+        return "SystemPropertyTestRule [name=" + name + "]";
     }
 
 }