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:30:05 UTC
[logging-log4j2] branch master updated: Port from release-2.x, can't cherry-pick thanks to jpms.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/master by this push:
new 69398f2 Port from release-2.x, can't cherry-pick thanks to jpms.
69398f2 is described below
commit 69398f29935bbe9dc17e5617576bbb5078602743
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jan 1 16:19:03 2022 -0500
Port from release-2.x, can't cherry-pick thanks to jpms.
---
.../test/AvailablePortSystemPropertyTestRule.java | 51 +---------------------
...tyTestRule.java => SystemPropertyTestRule.java} | 47 +++++++++++---------
2 files changed, 29 insertions(+), 69 deletions(-)
diff --git a/log4j-core-test/src/main/java/org/apache/logging/log4j/core/test/AvailablePortSystemPropertyTestRule.java b/log4j-core-test/src/main/java/org/apache/logging/log4j/core/test/AvailablePortSystemPropertyTestRule.java
index d62d180..1448f53 100644
--- a/log4j-core-test/src/main/java/org/apache/logging/log4j/core/test/AvailablePortSystemPropertyTestRule.java
+++ b/log4j-core-test/src/main/java/org/apache/logging/log4j/core/test/AvailablePortSystemPropertyTestRule.java
@@ -17,65 +17,18 @@
package org.apache.logging.log4j.core.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-test/src/main/java/org/apache/logging/log4j/core/test/AvailablePortSystemPropertyTestRule.java b/log4j-core-test/src/main/java/org/apache/logging/log4j/core/test/SystemPropertyTestRule.java
similarity index 60%
copy from log4j-core-test/src/main/java/org/apache/logging/log4j/core/test/AvailablePortSystemPropertyTestRule.java
copy to log4j-core-test/src/main/java/org/apache/logging/log4j/core/test/SystemPropertyTestRule.java
index d62d180..e1de101 100644
--- a/log4j-core-test/src/main/java/org/apache/logging/log4j/core/test/AvailablePortSystemPropertyTestRule.java
+++ b/log4j-core-test/src/main/java/org/apache/logging/log4j/core/test/SystemPropertyTestRule.java
@@ -17,25 +17,33 @@
package org.apache.logging.log4j.core.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 + "]";
}
}