Skip to content

Commit

Permalink
Introduce log level filtering support in LogRecordListener
Browse files Browse the repository at this point in the history
Issue: #1047
  • Loading branch information
sbrannen committed Sep 23, 2017
1 parent 5c18778 commit d9ec28d
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 9 deletions.
Expand Up @@ -15,6 +15,7 @@


import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.logging.Level;
import java.util.logging.LogRecord; import java.util.logging.LogRecord;


import org.apiguardian.api.API; import org.apiguardian.api.API;
Expand Down Expand Up @@ -78,4 +79,35 @@ public List<LogRecord> getLogRecords(Class<?> clazz) {
// @formatter:on // @formatter:on
} }


/**
* Get the list of {@link LogRecord log records} that have been
* {@linkplain #logRecordSubmitted submitted} to this listener
* for the given class at the given log level.
*
* <p>As stated in the JavaDoc for {@code LogRecord}, a submitted
* {@code LogRecord} should not be updated by the client application. Thus,
* the {@code LogRecords} in the returned list should only be inspected for
* testing purposes and not modified in any way.
*
* @param clazz the class for which to get the log records; never {@code null}
* @param level the log level for which to get the log records; never {@code null}
*/
public List<LogRecord> getLogRecords(Class<?> clazz, Level level) {
// NOTE: we cannot use org.junit.platform.commons.util.Preconditions here
// since that would introduce a package cycle.
if (clazz == null) {
throw new JUnitException("Class must not be null");
}
if (level == null) {
throw new JUnitException("Level must not be null");
}

// @formatter:off
return this.logRecords.stream()
.filter(logRecord -> logRecord.getLoggerName().equals(clazz.getName()))
.filter(logRecord -> logRecord.getLevel() == level)
.collect(toList());

This comment has been minimized.

Copy link
@jbduncan

jbduncan Sep 23, 2017

Contributor

@sbrannen I think the list returned here (as well as the list returned by getLogRecords(Class)) should explicitly be made either mutable (.collect(toCollection(ArrayList::new))) or unmodifiable (.collect(CollectionUtils.toUnmodifiableList())) to ensure that its mutability is consistent across all JDKs.

This comment has been minimized.

Copy link
@jbduncan

jbduncan Sep 23, 2017

Contributor

Oops, never mind! Commit 8e86ba6 makes these methods return Streams now, so nothing needs to be done. 😛

// @formatter:on
}

} }
Expand Up @@ -67,8 +67,7 @@ void logsWarningOnNonFilterableRunner(LogRecordListener listener) {
resolve(new TestClassRequest(testClass, asList(filter))); resolve(new TestClassRequest(testClass, asList(filter)));


// @formatter:off // @formatter:off
assertThat(listener.getLogRecords(TestClassRequestResolver.class).stream() assertThat(listener.getLogRecords(TestClassRequestResolver.class, Level.WARNING).stream()
.filter(logRecord -> logRecord.getLevel() == Level.WARNING)
.map(LogRecord::getMessage) .map(LogRecord::getMessage)
.filter(m -> m.equals("Runner " + IgnoredClassRunner.class.getName() // .filter(m -> m.equals("Runner " + IgnoredClassRunner.class.getName() //
+ " (used on " + testClass.getName() + ") does not support filtering" // + " (used on " + testClass.getName() + ") does not support filtering" //
Expand Down
Expand Up @@ -91,8 +91,7 @@ void logsWarningOnUnexpectedTestDescriptor(LogRecordListener listener) {


private void assertLoggedWarning(LogRecordListener listener, String expectedMessage) { private void assertLoggedWarning(LogRecordListener listener, String expectedMessage) {
// @formatter:off // @formatter:off
assertThat(listener.getLogRecords(UniqueIdSelectorResolver.class).stream() assertThat(listener.getLogRecords(UniqueIdSelectorResolver.class, Level.WARNING).stream()
.filter(logRecord -> logRecord.getLevel() == Level.WARNING)
.map(LogRecord::getMessage) .map(LogRecord::getMessage)
.filter(m -> m.equals(expectedMessage)) .filter(m -> m.equals(expectedMessage))
.count() .count()
Expand Down
Expand Up @@ -38,7 +38,7 @@
class VintageDiscovererTests { class VintageDiscovererTests {


@Test @Test
void logsWarningWhenFilterExcludesClass(LogRecordListener listener) { void logsDebugMessageWhenFilterExcludesClass(LogRecordListener listener) {
ClassNameFilter fooFilter = className -> includedIf(Foo.class.getName().equals(className), () -> "match", ClassNameFilter fooFilter = className -> includedIf(Foo.class.getName().equals(className), () -> "match",
() -> "no match"); () -> "no match");


Expand All @@ -55,8 +55,7 @@ void logsWarningWhenFilterExcludesClass(LogRecordListener listener) {
assertThat(testDescriptor.getChildren()).hasSize(1); assertThat(testDescriptor.getChildren()).hasSize(1);


// @formatter:off // @formatter:off
assertThat(listener.getLogRecords(VintageDiscoverer.class).stream() assertThat(listener.getLogRecords(VintageDiscoverer.class, Level.FINE).stream()
.filter(logRecord -> logRecord.getLevel() == Level.FINE)
.map(LogRecord::getMessage) .map(LogRecord::getMessage)
.filter(m -> m.equals("Class " + Bar.class.getName() + " was excluded by a class filter: no match")) .filter(m -> m.equals("Class " + Bar.class.getName() + " was excluded by a class filter: no match"))
.count() .count()
Expand Down
Expand Up @@ -51,8 +51,7 @@ void returnsDisplayNameWhenUniqueIdCannotBeRead(LogRecordListener listener) {
assertEquals(description.getDisplayName(), uniqueId); assertEquals(description.getDisplayName(), uniqueId);


// @formatter:off // @formatter:off
assertThat(listener.getLogRecords(UniqueIdReader.class).stream() assertThat(listener.getLogRecords(UniqueIdReader.class, Level.WARNING).stream()
.filter(logRecord -> logRecord.getLevel() == Level.WARNING)
.map(LogRecord::getMessage) .map(LogRecord::getMessage)
.filter(m -> m.equals("Could not read unique ID for Description; using display name instead: " .filter(m -> m.equals("Could not read unique ID for Description; using display name instead: "
+ description.getDisplayName())) + description.getDisplayName()))
Expand Down

0 comments on commit d9ec28d

Please sign in to comment.