Skip to content

Commit

Permalink
Add support for filter
Browse files Browse the repository at this point in the history
  • Loading branch information
jsalinaspolo committed May 22, 2023
1 parent ea157ea commit 25dc5d5
Show file tree
Hide file tree
Showing 18 changed files with 123 additions and 97 deletions.
42 changes: 23 additions & 19 deletions logcapture-core/src/main/java/org/logcapture/LogCapture.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,38 @@
import org.hamcrest.Matcher;
import org.logcapture.assertion.VerificationException;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class LogCapture<T> {

private final List<ILoggingEvent> events;
private final List<ILoggingEvent> events;

public LogCapture(List<ILoggingEvent> events) {
this.events = events;
}

public LogCapture<T> logged(Matcher<List<ILoggingEvent>> expectedLoggingMessage) {
if (expectedLoggingMessage.matches(events)) {
return this;
public LogCapture(List<ILoggingEvent> events) {
this.events = events;
}

throw VerificationException.forUnmatchedLog(expectedLoggingMessage, events);
}
public LogCapture<T> logged(Matcher<List<ILoggingEvent>> expectedLoggingMessage) {
if (expectedLoggingMessage.matches(events)) {
return this;
}

throw VerificationException.forUnmatchedLog(expectedLoggingMessage, events);
}

public LogCapture<T> logged(Matcher<List<ILoggingEvent>> expectedLoggingMessage, Integer times) {
logged(expectedLoggingMessage);
public LogCapture<T> logged(Matcher<List<ILoggingEvent>> expectedLoggingMessage, Integer times) {
logged(expectedLoggingMessage);
if (events.size() != times) {
throw VerificationException.forUnmatchedTimesLog(expectedLoggingMessage, events, times, events.size());
}
return this;
}

List<ILoggingEvent> result = events.stream()
.filter(actual -> !expectedLoggingMessage.matches(actual))
.collect(Collectors.toList());
if (result.size() != times) {
throw VerificationException.forUnmatchedTimesLog(expectedLoggingMessage, events, times, result.size());
public LogCapture<T> filter(Matcher<List<ILoggingEvent>> expectedLoggingMessage) {
List<ILoggingEvent> matchingEvents = events.stream()
.filter(actual -> expectedLoggingMessage.matches(Collections.singletonList(actual)))
.collect(Collectors.toList());
return new LogCapture<>(matchingEvents);
}
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import static ch.qos.logback.classic.Level.DEBUG;
Expand Down
13 changes: 13 additions & 0 deletions logcapture-core/src/test/java/org/logcapture/LogCaptureShould.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import ch.qos.logback.classic.spi.LoggingEvent;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.logcapture.assertion.ExpectedLoggingMessage;
import org.logcapture.assertion.VerificationException;
import org.slf4j.LoggerFactory;

Expand All @@ -25,6 +26,18 @@ public void match_n_times() {
underTest.logged(aLog().withMessage("message"), 2);
}

@Test
public void match_n_times_filtering_others() {
LoggingEvent log1 = aLoggingEventWith(INFO, "message");
LoggingEvent log2 = aLoggingEventWith(INFO, "another");

LogCapture<ILoggingEvent> underTest = new LogCapture<>(Arrays.asList(log1, log1, log2));

ExpectedLoggingMessage expectedLog = aLog().withMessage("message");
underTest.filter(expectedLog)
.logged(expectedLog, 2);
}

@Test
public void match_n_times_multiples_messages() {
LoggingEvent log1 = aLoggingEventWith(INFO, "message");
Expand Down
4 changes: 2 additions & 2 deletions logcapture-example/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

dependencies {
implementation "org.logcapture:logcapture-core:1.2.2"
implementation "org.logcapture:logcapture-junit4:1.2.2"
implementation "org.logcapture:logcapture-core:1.3.0"
implementation "org.logcapture:logcapture-junit4:1.3.0"
implementation "junit:junit:4.13.2"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.ILoggingEvent;
import org.logcapture.LogCapture;
import org.logcapture.logback.StubAppender;
import org.hamcrest.Matcher;
import org.junit.rules.MethodRule;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;
import org.logcapture.LogCapture;
import org.logcapture.logback.StubAppender;
import org.slf4j.LoggerFactory;

import java.util.List;
Expand Down Expand Up @@ -52,13 +52,11 @@ public void evaluate() throws Throwable {
};
}

public LogCaptureRule logged(Matcher<List<ILoggingEvent>> expectedLoggingMessage) {
new LogCapture<>(logAppender.events()).logged(expectedLoggingMessage);
return this;
public LogCapture logged(Matcher<List<ILoggingEvent>> expectedLoggingMessage) {
return new LogCapture<>(logAppender.events()).logged(expectedLoggingMessage);
}

public LogCaptureRule logged(Matcher<List<ILoggingEvent>> expectedLoggingMessage, Integer times) {
new LogCapture<>(logAppender.events()).logged(expectedLoggingMessage, times);
return this;
public LogCapture filter(Matcher<List<ILoggingEvent>> expectedLoggingMessage) {
return new LogCapture<>(logAppender.events()).filter(expectedLoggingMessage);
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package org.logcapture.junit4;

import org.logcapture.assertion.VerificationException;
import org.junit.Rule;
import org.junit.Test;
import org.logcapture.assertion.ExpectedLoggingMessage;
import org.logcapture.assertion.VerificationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.slf4j.MarkerFactory;

import static ch.qos.logback.classic.Level.DEBUG;
import static ch.qos.logback.classic.Level.INFO;
import static org.logcapture.assertion.ExpectedLoggingMessage.aLog;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.*;
import static org.logcapture.assertion.ExpectedLoggingMessage.aLog;

public class LogCaptureRuleShould {
private static final String LOG_NAME = "aLogNotAttachedToRoot";
Expand Down Expand Up @@ -87,9 +86,12 @@ public void verify_log_n_times() {
log.info("a message");
log.info("a message");

logCaptureRule.logged(aLog()
.info()
.withMessage("a message"), 2);
ExpectedLoggingMessage expectedMessage = aLog()
.info()
.withMessage("a message");
logCaptureRule
.filter(expectedMessage)
.logged(expectedMessage, 2);
}

private Logger createLogger(String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.ILoggingEvent;
import org.logcapture.LogCapture;
import org.logcapture.logback.StubAppender;
import org.hamcrest.Matcher;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.logcapture.LogCapture;
import org.logcapture.logback.StubAppender;
import org.slf4j.LoggerFactory;

import java.util.List;
Expand Down Expand Up @@ -41,13 +41,11 @@ public void afterEach(ExtensionContext context) {
root.detachAppender(logAppender);
}

public LogCaptureExtension logged(Matcher<List<ILoggingEvent>> expectedLoggingMessage) {
new LogCapture<>(logAppender.events()).logged(expectedLoggingMessage);
return this;
public LogCapture logged(Matcher<List<ILoggingEvent>> expectedLoggingMessage) {
return new LogCapture<>(logAppender.events()).logged(expectedLoggingMessage);
}

public LogCaptureExtension logged(Matcher<List<ILoggingEvent>> expectedLoggingMessage, Integer times) {
new LogCapture<>(logAppender.events()).logged(expectedLoggingMessage, times);
return this;
public LogCapture filter(Matcher<List<ILoggingEvent>> expectedLoggingMessage) {
return new LogCapture<>(logAppender.events()).filter(expectedLoggingMessage);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ void verify_log_n_times() {
log.info("a message");
log.info("a message");

logCaptureExtension.logged(aLog()
.info()
.withMessage("a message"), 2);
ExpectedLoggingMessage expectedMessage = aLog()
.info()
.withMessage("a message");
logCaptureExtension
.filter(expectedMessage)
.logged(expectedMessage, 2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ class LogCaptureListener(private val loggerName: String = ROOT_LOGGER_NAME) : Te
root.detachAppender(logAppender)
}

fun logged(expectedLoggingMessage: Matcher<List<ILoggingEvent>>): LogCaptureListener {
LogCapture<Any>(logAppender.events()).logged(expectedLoggingMessage)
return this
fun logged(expectedLoggingMessage: Matcher<List<ILoggingEvent>>): LogCapture<Any> {
return LogCapture<Any>(logAppender.events()).logged(expectedLoggingMessage)
}
fun logged(expectedLoggingMessage: Matcher<List<ILoggingEvent>>, times: Int): LogCaptureListener {
LogCapture<Any>(logAppender.events()).logged(expectedLoggingMessage, times)
return this

fun logged(expectedLoggingMessage: Matcher<List<ILoggingEvent>>, times: Int): LogCapture<Any> {
return LogCapture<Any>(logAppender.events()).logged(expectedLoggingMessage, times)
}

fun filter(expectedLoggingMessage: Matcher<List<ILoggingEvent>>): LogCapture<Any> {
return LogCapture<Any>(logAppender.events()).filter(expectedLoggingMessage)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.logcapture.kotest

import org.logcapture.assertion.ExpectedLoggingMessage.aLog
import io.kotest.core.spec.style.StringSpec
import org.logcapture.assertion.ExpectedLoggingMessage.aLog
import org.slf4j.Logger
import org.slf4j.LoggerFactory

Expand All @@ -22,6 +22,7 @@ class LogCaptureListenerSpec : StringSpec({
log.info("a message")
log.info("a message")

logCaptureListener.logged(aLog().info().withMessage("a message"), 2)
val expectedMessage = aLog().info().withMessage("a message")
logCaptureListener.filter(expectedMessage).logged(expectedMessage, 2)
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import org.hamcrest.Matcher
import org.slf4j.LoggerFactory
import spock.lang.Specification

import java.util.stream.Collectors

import static org.slf4j.Logger.ROOT_LOGGER_NAME

class LogCaptureSpec extends Specification {
Expand All @@ -26,14 +28,11 @@ class LogCaptureSpec extends Specification {
root.detachAppender(logAppender)
}

LogCaptureSpec logged(Matcher<List<ILoggingEvent>> expectedLoggingMessage) {
new LogCapture<>(logAppender.events()).logged(expectedLoggingMessage)
return this
LogCapture logged(Matcher<List<ILoggingEvent>> expectedLoggingMessage) {
return new LogCapture<>(logAppender.events()).logged(expectedLoggingMessage)
}

LogCaptureSpec logged(Matcher<List<ILoggingEvent>> expectedLoggingMessage, Integer times) {
new LogCapture<>(logAppender.events()).logged(expectedLoggingMessage, times)
return this
LogCapture filter(Matcher<List<ILoggingEvent>> expectedLoggingMessage) {
return new LogCapture<>(logAppender.events()).filter(expectedLoggingMessage)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@ trait LogCaptureTrait {
root.detachAppender(logAppender)
}

LogCaptureTrait logged(Matcher<List<ILoggingEvent>> expectedLoggingMessage) {
new LogCapture<>(logAppender.events()).logged(expectedLoggingMessage)
return this
LogCapture logged(Matcher<List<ILoggingEvent>> expectedLoggingMessage) {
return new LogCapture<>(logAppender.events()).logged(expectedLoggingMessage)
}

LogCaptureTrait logged(Matcher<List<ILoggingEvent>> expectedLoggingMessage, Integer times) {
new LogCapture<>(logAppender.events()).logged(expectedLoggingMessage, times)
return this
LogCapture filter(Matcher<List<ILoggingEvent>> expectedLoggingMessage) {
return new LogCapture<>(logAppender.events()).filter(expectedLoggingMessage)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class LogCaptureSpecShould extends LogCaptureSpec {
log.info("a message")
log.info("a message")

logged(aLog().info().withMessage("a message"), 2)
def expectedLog = aLog().info().withMessage("a message")
filter(expectedLog)
.logged(expectedLog, 2)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import static org.hamcrest.Matchers.*

class LogCaptureTraitShould extends Specification implements LogCaptureTrait {

@Shared log = LoggerFactory.getLogger(LogCaptureTraitShould.class)
@Shared
log = LoggerFactory.getLogger(LogCaptureTraitShould.class)

def "verify missing events"() {
expect:
Expand All @@ -24,10 +25,8 @@ class LogCaptureTraitShould extends Specification implements LogCaptureTrait {
expect:
log.info("first message")
log.debug("second message")
logged(allOf(
aLog().withLevel(INFO).withMessage("first message"),
aLog().withLevel(DEBUG).withMessage("second message"))
)
logged(allOf(aLog().withLevel(INFO).withMessage("first message"),
aLog().withLevel(DEBUG).withMessage("second message")))
}

def "verify sync logs using rule"() {
Expand All @@ -42,6 +41,8 @@ class LogCaptureTraitShould extends Specification implements LogCaptureTrait {
log.info("a message")
log.info("a message")

logged(aLog().info().withMessage("a message"), 2)
def expectedLog = aLog().info().withMessage("a message")
filter(expectedLog)
.logged(expectedLog, 2)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ class LogCaptureSpec extends Specification {
root.detachAppender(logAppender)
}

LogCaptureSpec logged(Matcher<List<ILoggingEvent>> expectedLoggingMessage) {
new LogCapture<>(logAppender.events()).logged(expectedLoggingMessage)
return this
LogCapture logged(Matcher<List<ILoggingEvent>> expectedLoggingMessage) {
return new LogCapture<>(logAppender.events()).logged(expectedLoggingMessage)
}
LogCaptureSpec logged(Matcher<List<ILoggingEvent>> expectedLoggingMessage, Integer times) {
new LogCapture<>(logAppender.events()).logged(expectedLoggingMessage, times)
return this

LogCapture filter(Matcher<List<ILoggingEvent>> expectedLoggingMessage) {
return new LogCapture<>(logAppender.events()).filter(expectedLoggingMessage)
}
}
Loading

0 comments on commit 25dc5d5

Please sign in to comment.