Ensure that logging system tests do not leave log files open

See gh-17107
This commit is contained in:
Andy Wilkinson 2019-07-02 12:57:46 +01:00
parent 0b8247bd62
commit 73cf11535d
3 changed files with 18 additions and 10 deletions

View File

@ -28,10 +28,10 @@ import java.util.Properties;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.StreamSupport; import java.util.stream.StreamSupport;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.logging.log4j.Level; import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.core.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.LoggerConfig;
import org.assertj.core.api.Condition; import org.assertj.core.api.Condition;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -61,6 +61,7 @@ import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.ResourceLoader;
import org.springframework.test.context.support.TestPropertySourceUtils; import org.springframework.test.context.support.TestPropertySourceUtils;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -495,17 +496,15 @@ class ConfigFileApplicationListenerTests {
} }
private void withDebugLogging(Runnable runnable) { private void withDebugLogging(Runnable runnable) {
LoggerContext loggingContext = (LoggerContext) LogManager.getContext(false); Log log = LogFactory.getLog(ConfigFileApplicationListener.class);
org.apache.logging.log4j.core.config.Configuration configuration = loggingContext.getConfiguration(); Logger logger = (Logger) ReflectionTestUtils.getField(log, "logger");
configuration.addLogger(ConfigFileApplicationListener.class.getName(), Level previousLevel = logger.getLevel();
new LoggerConfig(ConfigFileApplicationListener.class.getName(), Level.DEBUG, true)); logger.setLevel(Level.DEBUG);
loggingContext.updateLoggers();
try { try {
runnable.run(); runnable.run();
} }
finally { finally {
configuration.removeLogger(ConfigFileApplicationListener.class.getName()); logger.setLevel(previousLevel);
loggingContext.updateLoggers();
} }
} }

View File

@ -70,6 +70,7 @@ class JavaLoggingSystemTests extends AbstractLoggingSystemTests {
@AfterEach @AfterEach
void resetLogger() { void resetLogger() {
this.logger.setLevel(Level.OFF); this.logger.setLevel(Level.OFF);
this.loggingSystem.getShutdownHandler().run();
} }
@AfterEach @AfterEach

View File

@ -29,6 +29,7 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext; import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration; import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.Reconfigurable;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -66,8 +67,12 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
private Logger logger; private Logger logger;
private Configuration configuration;
@BeforeEach @BeforeEach
void setup() { void setup() {
LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
this.configuration = loggerContext.getConfiguration();
this.loggingSystem.cleanUp(); this.loggingSystem.cleanUp();
this.logger = LogManager.getLogger(getClass()); this.logger = LogManager.getLogger(getClass());
} }
@ -75,6 +80,9 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
@AfterEach @AfterEach
void cleanUp() { void cleanUp() {
this.loggingSystem.cleanUp(); this.loggingSystem.cleanUp();
LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
loggerContext.stop();
loggerContext.start(((Reconfigurable) this.configuration).reconfigure());
} }
@Test @Test