Skip to content

Commit

Permalink
Return streams from LogRecordListener getLogRecords() variants
Browse files Browse the repository at this point in the history
Issue: #1047
  • Loading branch information
sbrannen committed Sep 23, 2017
1 parent d9ec28d commit 8e86ba6
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 34 deletions.
Expand Up @@ -36,7 +36,7 @@ void testAndRepeatedTest(LogRecordListener listener) {
discoverTests(request().selectors(selectClass(TestCase.class)).build());

// @formatter:off
assertThat(listener.getLogRecords().stream()
assertThat(listener.getLogRecords()
.filter(logRecord -> logRecord.getLevel() == Level.WARNING)
.map(LogRecord::getMessage)
.filter(m -> m.matches("Possible configuration error: method .+ resulted in multiple TestDescriptors .+"))
Expand Down
Expand Up @@ -10,13 +10,13 @@

package org.junit.platform.commons.logging;

import static java.util.stream.Collectors.toList;
import static org.apiguardian.api.API.Status.INTERNAL;

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

import org.apiguardian.api.API;
import org.junit.platform.commons.JUnitException;
Expand All @@ -41,73 +41,61 @@ public void logRecordSubmitted(LogRecord logRecord) {
}

/**
* Get the list of {@link LogRecord log records} that have been
* Get a stream of {@link LogRecord log records} that have been
* {@linkplain #logRecordSubmitted submitted} to this listener.
*
* <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
* the {@code LogRecords} in the returned stream should only be inspected for
* testing purposes and not modified in any way.
*/
public List<LogRecord> getLogRecords() {
return this.logRecords;
public Stream<LogRecord> getLogRecords() {
return this.logRecords.stream();
}

/**
* Get the list of {@link LogRecord log records} that have been
* {@linkplain #logRecordSubmitted submitted} to this listener
* for the given class.
* Get a stream of {@link LogRecord log records} that have been
* {@linkplain #logRecordSubmitted submitted} to this listener for the
* logger name equal to the name of the given class.
*
* <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
* the {@code LogRecords} in the returned stream 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}
*/
public List<LogRecord> getLogRecords(Class<?> clazz) {
public Stream<LogRecord> getLogRecords(Class<?> clazz) {
// 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");
}

// @formatter:off
return this.logRecords.stream()
.filter(logRecord -> logRecord.getLoggerName().equals(clazz.getName()))
.collect(toList());
// @formatter:on
return getLogRecords().filter(logRecord -> logRecord.getLoggerName().equals(clazz.getName()));
}

/**
* 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.
* Get a stream of {@link LogRecord log records} that have been
* {@linkplain #logRecordSubmitted submitted} to this listener for the
* logger name equal to the name of 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
* the {@code LogRecords} in the returned stream 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) {
public Stream<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());
// @formatter:on
return getLogRecords(clazz).filter(logRecord -> logRecord.getLevel() == level);
}

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

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

private void assertLoggedWarning(LogRecordListener listener, String expectedMessage) {
// @formatter:off
assertThat(listener.getLogRecords(UniqueIdSelectorResolver.class, Level.WARNING).stream()
assertThat(listener.getLogRecords(UniqueIdSelectorResolver.class, Level.WARNING)
.map(LogRecord::getMessage)
.filter(m -> m.equals(expectedMessage))
.count()
Expand Down
Expand Up @@ -55,7 +55,7 @@ void logsDebugMessageWhenFilterExcludesClass(LogRecordListener listener) {
assertThat(testDescriptor.getChildren()).hasSize(1);

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

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

0 comments on commit 8e86ba6

Please sign in to comment.