Fix failing logging listener tests

The logging listener tests started failing after
953a8a959b when the tests are run with
tests.es.logger.level set to any level other than debug. This is because
these tests were based around the assumption that the default logging
level was info, which was the case before that commit fixed setting the
default logging level via that system property. This commit fixes these
failing tests by adjusting this assumption to account for the fact that
the default logging level could be different.
This commit is contained in:
Jason Tedor 2016-09-30 08:09:35 +02:00
parent afcf683228
commit 3a4ffd7b86
1 changed files with 30 additions and 24 deletions

View File

@ -21,6 +21,7 @@ package org.elasticsearch.test.test;
import org.apache.logging.log4j.Level; import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.elasticsearch.common.logging.ESLoggerFactory;
import org.elasticsearch.common.logging.Loggers; import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.junit.annotations.TestLogging; import org.elasticsearch.test.junit.annotations.TestLogging;
@ -50,27 +51,28 @@ public class LoggingListenerTests extends ESTestCase {
Logger xyzLogger = Loggers.getLogger("xyz"); Logger xyzLogger = Loggers.getLogger("xyz");
Logger abcLogger = Loggers.getLogger("abc"); Logger abcLogger = Loggers.getLogger("abc");
assertEquals(Level.INFO, abcLogger.getLevel()); final Level level = ESLoggerFactory.getRootLogger().getLevel();
assertThat(xyzLogger.getLevel(), equalTo(Level.INFO));
assertThat(abcLogger.getLevel(), equalTo(Level.INFO)); assertThat(xyzLogger.getLevel(), equalTo(level));
assertThat(abcLogger.getLevel(), equalTo(level));
loggingListener.testRunStarted(suiteDescription); loggingListener.testRunStarted(suiteDescription);
assertThat(xyzLogger.getLevel(), equalTo(Level.INFO)); assertThat(xyzLogger.getLevel(), equalTo(level));
assertThat(abcLogger.getLevel(), equalTo(Level.INFO)); assertThat(abcLogger.getLevel(), equalTo(level));
Method method = TestClass.class.getMethod("annotatedTestMethod"); Method method = TestClass.class.getMethod("annotatedTestMethod");
TestLogging annotation = method.getAnnotation(TestLogging.class); TestLogging annotation = method.getAnnotation(TestLogging.class);
Description testDescription = Description.createTestDescription(LoggingListenerTests.class, "annotatedTestMethod", annotation); Description testDescription = Description.createTestDescription(LoggingListenerTests.class, "annotatedTestMethod", annotation);
loggingListener.testStarted(testDescription); loggingListener.testStarted(testDescription);
assertThat(xyzLogger.getLevel(), equalTo(Level.TRACE)); assertThat(xyzLogger.getLevel(), equalTo(Level.TRACE));
assertThat(abcLogger.getLevel(), equalTo(Level.INFO)); assertThat(abcLogger.getLevel(), equalTo(level));
loggingListener.testFinished(testDescription); loggingListener.testFinished(testDescription);
assertThat(xyzLogger.getLevel(), equalTo(Level.INFO)); assertThat(xyzLogger.getLevel(), equalTo(level));
assertThat(abcLogger.getLevel(), equalTo(Level.INFO)); assertThat(abcLogger.getLevel(), equalTo(level));
loggingListener.testRunFinished(new Result()); loggingListener.testRunFinished(new Result());
assertThat(xyzLogger.getLevel(), equalTo(Level.INFO)); assertThat(xyzLogger.getLevel(), equalTo(level));
assertThat(abcLogger.getLevel(), equalTo(Level.INFO)); assertThat(abcLogger.getLevel(), equalTo(level));
} }
public void testCustomLevelPerClass() throws Exception { public void testCustomLevelPerClass() throws Exception {
@ -81,24 +83,26 @@ public class LoggingListenerTests extends ESTestCase {
Logger abcLogger = Loggers.getLogger("abc"); Logger abcLogger = Loggers.getLogger("abc");
Logger xyzLogger = Loggers.getLogger("xyz"); Logger xyzLogger = Loggers.getLogger("xyz");
assertThat(xyzLogger.getLevel(), equalTo(Level.INFO)); final Level level = ESLoggerFactory.getRootLogger().getLevel();
assertThat(abcLogger.getLevel(), equalTo(Level.INFO));
assertThat(xyzLogger.getLevel(), equalTo(level));
assertThat(abcLogger.getLevel(), equalTo(level));
loggingListener.testRunStarted(suiteDescription); loggingListener.testRunStarted(suiteDescription);
assertThat(xyzLogger.getLevel(), equalTo(Level.INFO)); assertThat(xyzLogger.getLevel(), equalTo(level));
assertThat(abcLogger.getLevel(), equalTo(Level.WARN)); assertThat(abcLogger.getLevel(), equalTo(Level.WARN));
Description testDescription = Description.createTestDescription(LoggingListenerTests.class, "test"); Description testDescription = Description.createTestDescription(LoggingListenerTests.class, "test");
loggingListener.testStarted(testDescription); loggingListener.testStarted(testDescription);
assertThat(xyzLogger.getLevel(), equalTo(Level.INFO)); assertThat(xyzLogger.getLevel(), equalTo(level));
assertThat(abcLogger.getLevel(), equalTo(Level.WARN)); assertThat(abcLogger.getLevel(), equalTo(Level.WARN));
loggingListener.testFinished(testDescription); loggingListener.testFinished(testDescription);
assertThat(xyzLogger.getLevel(), equalTo(Level.INFO)); assertThat(xyzLogger.getLevel(), equalTo(level));
assertThat(abcLogger.getLevel(), equalTo(Level.WARN)); assertThat(abcLogger.getLevel(), equalTo(Level.WARN));
loggingListener.testRunFinished(new Result()); loggingListener.testRunFinished(new Result());
assertThat(xyzLogger.getLevel(), equalTo(Level.INFO)); assertThat(xyzLogger.getLevel(), equalTo(level));
assertThat(abcLogger.getLevel(), equalTo(Level.INFO)); assertThat(abcLogger.getLevel(), equalTo(level));
} }
public void testCustomLevelPerClassAndPerMethod() throws Exception { public void testCustomLevelPerClassAndPerMethod() throws Exception {
@ -109,10 +113,12 @@ public class LoggingListenerTests extends ESTestCase {
Logger abcLogger = Loggers.getLogger("abc"); Logger abcLogger = Loggers.getLogger("abc");
Logger xyzLogger = Loggers.getLogger("xyz"); Logger xyzLogger = Loggers.getLogger("xyz");
assertThat(xyzLogger.getLevel(), equalTo(Level.INFO)); final Level level = ESLoggerFactory.getRootLogger().getLevel();
assertThat(abcLogger.getLevel(), equalTo(Level.INFO));
assertThat(xyzLogger.getLevel(), equalTo(level));
assertThat(abcLogger.getLevel(), equalTo(level));
loggingListener.testRunStarted(suiteDescription); loggingListener.testRunStarted(suiteDescription);
assertThat(xyzLogger.getLevel(), equalTo(Level.INFO)); assertThat(xyzLogger.getLevel(), equalTo(level));
assertThat(abcLogger.getLevel(), equalTo(Level.WARN)); assertThat(abcLogger.getLevel(), equalTo(Level.WARN));
Method method = TestClass.class.getMethod("annotatedTestMethod"); Method method = TestClass.class.getMethod("annotatedTestMethod");
@ -123,7 +129,7 @@ public class LoggingListenerTests extends ESTestCase {
assertThat(abcLogger.getLevel(), equalTo(Level.WARN)); assertThat(abcLogger.getLevel(), equalTo(Level.WARN));
loggingListener.testFinished(testDescription); loggingListener.testFinished(testDescription);
assertThat(xyzLogger.getLevel(), equalTo(Level.INFO)); assertThat(xyzLogger.getLevel(), equalTo(level));
assertThat(abcLogger.getLevel(), equalTo(Level.WARN)); assertThat(abcLogger.getLevel(), equalTo(Level.WARN));
Method method2 = TestClass.class.getMethod("annotatedTestMethod2"); Method method2 = TestClass.class.getMethod("annotatedTestMethod2");
@ -134,12 +140,12 @@ public class LoggingListenerTests extends ESTestCase {
assertThat(abcLogger.getLevel(), equalTo(Level.TRACE)); assertThat(abcLogger.getLevel(), equalTo(Level.TRACE));
loggingListener.testFinished(testDescription2); loggingListener.testFinished(testDescription2);
assertThat(xyzLogger.getLevel(), equalTo(Level.INFO)); assertThat(xyzLogger.getLevel(), equalTo(level));
assertThat(abcLogger.getLevel(), equalTo(Level.WARN)); assertThat(abcLogger.getLevel(), equalTo(Level.WARN));
loggingListener.testRunFinished(new Result()); loggingListener.testRunFinished(new Result());
assertThat(xyzLogger.getLevel(), equalTo(Level.INFO)); assertThat(xyzLogger.getLevel(), equalTo(level));
assertThat(abcLogger.getLevel(), equalTo(Level.INFO)); assertThat(abcLogger.getLevel(), equalTo(level));
} }
/** /**