Reject invalid test logging annotations

Today we silently ignore invalid test logging annotations. This commit
rejects these annotations, failing the processing of the annotation and
aborting the test.
This commit is contained in:
Jason Tedor 2016-12-23 06:48:50 -05:00
parent 432ec54347
commit ddf4a463f3
2 changed files with 58 additions and 5 deletions

View File

@ -127,8 +127,10 @@ public class LoggingListener extends RunListener {
final String[] loggersAndLevels = testLogging.value().split(",");
for (final String loggerAndLevel : loggersAndLevels) {
final String[] loggerAndLevelArray = loggerAndLevel.split(":");
if (loggerAndLevelArray.length >= 2) {
if (loggerAndLevelArray.length == 2) {
map.put(loggerAndLevelArray[0], loggerAndLevelArray[1]);
} else {
throw new IllegalArgumentException("invalid test logging annotation [" + loggerAndLevel + "]");
}
}
return map;

View File

@ -163,8 +163,33 @@ public class LoggingListenerTests extends ESTestCase {
assertThat(abcLogger.getLevel(), equalTo(level));
}
public void testInvalidClassTestLoggingAnnotation() throws Exception {
final LoggingListener loggingListener = new LoggingListener();
final Description suiteDescription = Description.createSuiteDescription(InvalidClass.class);
final IllegalArgumentException e =
expectThrows(IllegalArgumentException.class, () -> loggingListener.testRunStarted(suiteDescription));
assertThat(e.getMessage(), equalTo("invalid test logging annotation [abc]"));
}
public void testInvalidMethodTestLoggingAnnotation() throws Exception {
final LoggingListener loggingListener = new LoggingListener();
final Description suiteDescription = Description.createSuiteDescription(InvalidMethod.class);
loggingListener.testRunStarted(suiteDescription);
final Method method = InvalidMethod.class.getMethod("invalidMethod");
final TestLogging annotation = method.getAnnotation(TestLogging.class);
Description testDescription = Description.createTestDescription(InvalidMethod.class, "invalidMethod", annotation);
final IllegalArgumentException e =
expectThrows(IllegalArgumentException.class, () -> loggingListener.testStarted(testDescription));
assertThat(e.getMessage(), equalTo("invalid test logging annotation [abc:INFO:WARN]"));
}
/**
* dummy class used to create a junit suite description that has the @TestLogging annotation
* Dummy class used to create a JUnit suite description that has the {@link TestLogging} annotation.
*/
@TestLogging("abc:WARN,foo:WARN,foo.bar:ERROR")
public static class AnnotatedTestClass {
@ -172,17 +197,43 @@ public class LoggingListenerTests extends ESTestCase {
}
/**
* dummy class used to create a junit suite description that doesn't have the @TestLogging annotation, but its test methods have it
* Dummy class used to create a JUnit suite description that doesn't have the {@link TestLogging} annotation, but its test methods have
* it.
*/
public static class TestClass {
@SuppressWarnings("unused")
@TestLogging("xyz:TRACE,foo:WARN,foo.bar:ERROR")
public void annotatedTestMethod() {}
public void annotatedTestMethod() {
}
@SuppressWarnings("unused")
@TestLogging("abc:TRACE,xyz:DEBUG")
public void annotatedTestMethod2() {}
public void annotatedTestMethod2() {
}
}
/**
* Dummy class with an invalid {@link TestLogging} annotation.
*/
@TestLogging("abc")
public static class InvalidClass {
}
/**
* Dummy class with an invalid {@link TestLogging} annotation on a method.
*/
public static class InvalidMethod {
@SuppressWarnings("unused")
@TestLogging("abc:INFO:WARN")
public void invalidMethod() {
}
}