Skip to content

Commit

Permalink
Added backup&restore of system properties used in log4j unite tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andsel committed Sep 4, 2019
1 parent 8e9ac4b commit 3ab931e
Showing 1 changed file with 39 additions and 0 deletions.
Expand Up @@ -9,10 +9,14 @@

import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.test.appender.ListAppender;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static junit.framework.TestCase.assertNull;
import static junit.framework.TestCase.assertEquals;
Expand All @@ -21,6 +25,41 @@ public class LogstashConfigurationFactoryTest {

private static final String CONFIG = "log4j2-log-pipeline-test.properties";

private static Map<String, String> systemPropertiesDump = new HashMap<>();
private static Map<String, String> dumpedLog4jThreadContext;

@BeforeClass
public static void beforeClass() {
dumpSystemProperty("log4j.configurationFile");
dumpSystemProperty("ls.log.format");
dumpSystemProperty("ls.logs");
dumpSystemProperty(LogstashConfigurationFactory.PIPELINE_SEPARATE_LOGS);

dumpedLog4jThreadContext = ThreadContext.getImmutableContext();
}

private static void dumpSystemProperty(String propertyName) {
systemPropertiesDump.put(propertyName, System.getProperty(propertyName));
}

@AfterClass
public static void afterClass() {
restoreSystemProperty("log4j.configurationFile");
restoreSystemProperty("ls.log.format");
restoreSystemProperty("ls.logs");
restoreSystemProperty(LogstashConfigurationFactory.PIPELINE_SEPARATE_LOGS);

ThreadContext.putAll(dumpedLog4jThreadContext);
}

private static void restoreSystemProperty(String propertyName) {
if (systemPropertiesDump.get(propertyName) == null) {
System.clearProperty(propertyName);
} else {
System.setProperty(propertyName, systemPropertiesDump.get(propertyName));
}
}

@Before
public void setUp() {
System.setProperty("log4j.configurationFile", CONFIG);
Expand Down

0 comments on commit 3ab931e

Please sign in to comment.