Add PropertiesUtil.reload() for unit tests
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/51f8e875 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/51f8e875 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/51f8e875 Branch: refs/heads/LOG4J2-1431 Commit: 51f8e875e528d538c2a9faf720051c4eafb5a396 Parents: 74ed4f6 Author: Matt Sicker <[email protected]> Authored: Sat Aug 26 15:02:49 2017 -0500 Committer: Matt Sicker <[email protected]> Committed: Sat Aug 26 15:50:57 2017 -0500 ---------------------------------------------------------------------- .../apache/logging/log4j/util/PropertiesUtil.java | 18 +++++++++++++++++- .../appender/db/jpa/AbstractJpaAppenderTest.java | 3 +++ .../async/AbstractAsyncThreadContextTestBase.java | 2 ++ .../async/AsyncQueueFullPolicyFactoryTest.java | 14 +++----------- 4 files changed, 25 insertions(+), 12 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/51f8e875/log4j-api/src/main/java/org/apache/logging/log4j/util/PropertiesUtil.java ---------------------------------------------------------------------- diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/util/PropertiesUtil.java b/log4j-api/src/main/java/org/apache/logging/log4j/util/PropertiesUtil.java index 5e87349..45438d2 100644 --- a/log4j-api/src/main/java/org/apache/logging/log4j/util/PropertiesUtil.java +++ b/log4j-api/src/main/java/org/apache/logging/log4j/util/PropertiesUtil.java @@ -273,6 +273,15 @@ public final class PropertiesUtil { } /** + * Reloads all properties. This is primarily useful for unit tests. + * + * @since 2.9.1 + */ + public void reload() { + environment.reload(); + } + + /** * Provides support for looking up global configuration properties via environment variables, property files, * and system properties, in three variations: * @@ -287,16 +296,23 @@ public final class PropertiesUtil { */ private static class Environment { + private final Set<PropertySource> sources = new TreeSet<>(new PropertySource.Comparator()); private final Map<CharSequence, String> literal = new ConcurrentHashMap<>(); private final Map<CharSequence, String> normalized = new ConcurrentHashMap<>(); private final Map<List<CharSequence>, String> tokenized = new ConcurrentHashMap<>(); private Environment(final PropertySource propertySource) { - final Set<PropertySource> sources = new TreeSet<>(new PropertySource.Comparator()); sources.add(propertySource); for (final PropertySource source : ServiceLoader.load(PropertySource.class)) { sources.add(source); } + reload(); + } + + private synchronized void reload() { + literal.clear(); + normalized.clear(); + tokenized.clear(); for (final PropertySource source : sources) { source.forEach(new BiConsumer<String, String>() { @Override http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/51f8e875/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/db/jpa/AbstractJpaAppenderTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/db/jpa/AbstractJpaAppenderTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/db/jpa/AbstractJpaAppenderTest.java index c61af57..517e98c 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/db/jpa/AbstractJpaAppenderTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/db/jpa/AbstractJpaAppenderTest.java @@ -31,6 +31,7 @@ import org.apache.logging.log4j.core.LoggerContext; import org.apache.logging.log4j.core.config.ConfigurationFactory; import org.apache.logging.log4j.core.config.DefaultConfiguration; import org.apache.logging.log4j.status.StatusLogger; +import org.apache.logging.log4j.util.PropertiesUtil; import org.junit.Test; import org.junit.experimental.categories.Category; @@ -52,6 +53,7 @@ public abstract class AbstractJpaAppenderTest { System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, "org/apache/logging/log4j/core/appender/db/jpa/" + configFileName); + PropertiesUtil.getProperties().reload(); final LoggerContext context = LoggerContext.getContext(false); if (context.getConfiguration() instanceof DefaultConfiguration) { context.reconfigure(); @@ -68,6 +70,7 @@ public abstract class AbstractJpaAppenderTest { ((JpaAppender) appender).getManager().close(); } finally { System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY); + PropertiesUtil.getProperties().reload(); context.reconfigure(); StatusLogger.getLogger().reset(); http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/51f8e875/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AbstractAsyncThreadContextTestBase.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AbstractAsyncThreadContextTestBase.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AbstractAsyncThreadContextTestBase.java index 00cad14..a590c2d 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AbstractAsyncThreadContextTestBase.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AbstractAsyncThreadContextTestBase.java @@ -32,6 +32,7 @@ import org.apache.logging.log4j.core.util.Constants; import org.apache.logging.log4j.spi.DefaultThreadContextMap; import org.apache.logging.log4j.spi.LoggerContext; import org.apache.logging.log4j.spi.ReadOnlyThreadContextMap; +import org.apache.logging.log4j.util.PropertiesUtil; import org.apache.logging.log4j.util.Unbox; import org.junit.AfterClass; import org.junit.BeforeClass; @@ -88,6 +89,7 @@ public abstract class AbstractAsyncThreadContextTestBase { System.clearProperty("log4j2.threadContextMap"); final String PACKAGE = "org.apache.logging.log4j.spi."; System.setProperty("log4j2.threadContextMap", PACKAGE + implClassSimpleName()); + PropertiesUtil.getProperties().reload(); ThreadContextTestAccess.init(); } http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/51f8e875/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncQueueFullPolicyFactoryTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncQueueFullPolicyFactoryTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncQueueFullPolicyFactoryTest.java index 52d97c5..964bf12 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncQueueFullPolicyFactoryTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncQueueFullPolicyFactoryTest.java @@ -18,7 +18,7 @@ package org.apache.logging.log4j.core.async; import org.apache.logging.log4j.Level; import org.apache.logging.log4j.categories.AsyncLoggers; -import org.junit.After; +import org.apache.logging.log4j.util.PropertiesUtil; import org.junit.Before; import org.junit.Test; import org.junit.experimental.categories.Category; @@ -31,19 +31,11 @@ import static org.junit.Assert.*; @Category(AsyncLoggers.class) public class AsyncQueueFullPolicyFactoryTest { - @After - public void after() { - clearProperties(); - } - @Before - public void before() { - clearProperties(); - } - - private void clearProperties() { + public void setUp() throws Exception { System.clearProperty(AsyncQueueFullPolicyFactory.PROPERTY_NAME_ASYNC_EVENT_ROUTER); System.clearProperty(AsyncQueueFullPolicyFactory.PROPERTY_NAME_DISCARDING_THRESHOLD_LEVEL); + PropertiesUtil.getProperties().reload(); } @Test
