Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable assertions to be made on LoggingEvents from any thread. #207

Merged
merged 1 commit into from
Oct 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
package com.github.valfirst.slf4jtest;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.function.Supplier;
import org.assertj.core.api.AbstractAssert;
import uk.org.lidalia.slf4jext.Level;

abstract class AbstractTestLoggerAssert<C extends AbstractAssert<C, TestLogger>>
extends AbstractAssert<C, TestLogger> {
protected Supplier<List<LoggingEvent>> loggingEventsSupplier;

protected AbstractTestLoggerAssert(TestLogger testLogger, Class clazz) {
super(testLogger, clazz);
loggingEventsSupplier = testLogger::getLoggingEvents;
}

protected long getLogCount(Level level, Predicate<LoggingEvent> predicate) {
return actual.getLoggingEvents().stream()
return loggingEventsSupplier.get().stream()
.filter(event -> level.equals(event.getLevel()) && predicate.test(event))
.count();
}
Expand Down
23 changes: 19 additions & 4 deletions src/main/java/com/github/valfirst/slf4jtest/TestLoggerAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,27 @@
/**
* A set of assertions to validate that logs have been logged to a {@link TestLogger}.
*
* <p>Should be thread safe, as this uses <code>testLogger.getLoggingEvents()</code>.
* <p>Should be thread safe, as this uses <code>testLogger.getLoggingEvents()</code> by default. The
* assertion mode can be switched to use <code>testLogger.getAllLoggingEvents()</code> by calling
* {@link #anyThread()}.
*/
public class TestLoggerAssert extends AbstractTestLoggerAssert<TestLoggerAssert> {

protected TestLoggerAssert(TestLogger testLogger) {
super(testLogger, TestLoggerAssert.class);
}

/**
* Changes the assertion mode to verify that log messages have been logged regardless of which
* thread actually logged the message.
*
* @return a {@link TestLoggerAssert} for chaining
*/
public TestLoggerAssert anyThread() {
loggingEventsSupplier = actual::getAllLoggingEvents;
return this;
}

/**
* Verify that a log message, at a specific level, has been logged by the test logger.
*
Expand Down Expand Up @@ -47,7 +60,7 @@ public TestLoggerAssert hasLogged(
* @return a {@link TestLoggerAssert} for chaining
*/
public TestLoggerAssert hasLogged(LoggingEvent event) {
if (!actual.getLoggingEvents().contains(event)) {
if (!loggingEventsSupplier.get().contains(event)) {
failWithMessage("Failed to find %s", event);
}
return this;
Expand Down Expand Up @@ -87,7 +100,7 @@ public TestLoggerAssert hasNotLogged(
* @return a {@link TestLoggerAssert} for chaining
*/
public TestLoggerAssert hasNotLogged(LoggingEvent event) {
if (actual.getLoggingEvents().contains(event)) {
if (loggingEventsSupplier.get().contains(event)) {
failWithMessage("Found %s, even though we expected not to", event);
}
return this;
Expand All @@ -100,6 +113,8 @@ public TestLoggerAssert hasNotLogged(LoggingEvent event) {
* @return the {@link LevelAssert} bound to the given {@link Level}
*/
public LevelAssert hasLevel(Level level) {
return new LevelAssert(actual, level);
LevelAssert levelAssert = new LevelAssert(actual, level);
levelAssert.loggingEventsSupplier = loggingEventsSupplier;
return levelAssert;
}
}
Loading