Skip to content

Commit

Permalink
Refactor to use logged instead of filter
Browse files Browse the repository at this point in the history
  • Loading branch information
jsalinaspolo committed May 22, 2023
1 parent 25dc5d5 commit 2829adf
Show file tree
Hide file tree
Showing 16 changed files with 33 additions and 67 deletions.
12 changes: 4 additions & 8 deletions logcapture-core/src/main/java/org/logcapture/LogCapture.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,12 @@ public LogCapture<T> logged(Matcher<List<ILoggingEvent>> expectedLoggingMessage)

public LogCapture<T> logged(Matcher<List<ILoggingEvent>> expectedLoggingMessage, Integer times) {
logged(expectedLoggingMessage);
if (events.size() != times) {
List<ILoggingEvent> matchingEvents = events.stream()
.filter(actual -> expectedLoggingMessage.matches(Collections.singletonList(actual)))
.collect(Collectors.toList());
if (matchingEvents.size() != times) {
throw VerificationException.forUnmatchedTimesLog(expectedLoggingMessage, events, times, events.size());
}
return this;
}

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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ public void match_n_times_filtering_others() {
LogCapture<ILoggingEvent> underTest = new LogCapture<>(Arrays.asList(log1, log1, log2));

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

@Test
Expand All @@ -45,7 +44,7 @@ public void match_n_times_multiples_messages() {

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

underTest.logged(aLog().withMessage("message"), 4);
underTest.logged(aLog().withMessage("message"), 2);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public LogCapture logged(Matcher<List<ILoggingEvent>> expectedLoggingMessage) {
return new LogCapture<>(logAppender.events()).logged(expectedLoggingMessage);
}

public LogCapture filter(Matcher<List<ILoggingEvent>> expectedLoggingMessage) {
return new LogCapture<>(logAppender.events()).filter(expectedLoggingMessage);
public LogCapture logged(Matcher<List<ILoggingEvent>> expectedLoggingMessage, Integer times) {
return new LogCapture<>(logAppender.events()).logged(expectedLoggingMessage, times);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

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;
Expand Down Expand Up @@ -86,12 +85,7 @@ public void verify_log_n_times() {
log.info("a message");
log.info("a message");

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

private Logger createLogger(String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public LogCapture logged(Matcher<List<ILoggingEvent>> expectedLoggingMessage) {
return new LogCapture<>(logAppender.events()).logged(expectedLoggingMessage);
}

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

ExpectedLoggingMessage expectedMessage = aLog()
.info()
.withMessage("a message");
logCaptureExtension
.filter(expectedMessage)
.logged(expectedMessage, 2);
logCaptureExtension.logged(aLog().info().withMessage("a message"), 2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,4 @@ class LogCaptureListener(private val loggerName: String = ROOT_LOGGER_NAME) : Te
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
Expand Up @@ -22,7 +22,6 @@ class LogCaptureListenerSpec : StringSpec({
log.info("a message")
log.info("a message")

val expectedMessage = aLog().info().withMessage("a message")
logCaptureListener.filter(expectedMessage).logged(expectedMessage, 2)
logCaptureListener.logged(aLog().info().withMessage("a message"), 2)
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ package org.logcapture.spock

import ch.qos.logback.classic.Logger
import ch.qos.logback.classic.spi.ILoggingEvent
import org.hamcrest.Matcher
import org.logcapture.LogCapture
import org.logcapture.logback.StubAppender
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 @@ -32,7 +30,7 @@ class LogCaptureSpec extends Specification {
return new LogCapture<>(logAppender.events()).logged(expectedLoggingMessage)
}

LogCapture filter(Matcher<List<ILoggingEvent>> expectedLoggingMessage) {
return new LogCapture<>(logAppender.events()).filter(expectedLoggingMessage)
LogCapture logged(Matcher<List<ILoggingEvent>> expectedLoggingMessage, Integer times) {
return new LogCapture<>(logAppender.events()).logged(expectedLoggingMessage, times)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package org.logcapture.spock

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.After
import org.junit.Before
import org.logcapture.LogCapture
import org.logcapture.logback.StubAppender
import org.slf4j.LoggerFactory

trait LogCaptureTrait {
Expand All @@ -31,7 +31,7 @@ trait LogCaptureTrait {
return new LogCapture<>(logAppender.events()).logged(expectedLoggingMessage)
}

LogCapture filter(Matcher<List<ILoggingEvent>> expectedLoggingMessage) {
return new LogCapture<>(logAppender.events()).filter(expectedLoggingMessage)
LogCapture logged(Matcher<List<ILoggingEvent>> expectedLoggingMessage, Integer times) {
return new LogCapture<>(logAppender.events()).logged(expectedLoggingMessage, times)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ import spock.lang.Shared

import static ch.qos.logback.classic.Level.DEBUG
import static ch.qos.logback.classic.Level.INFO
import static org.hamcrest.Matchers.*
import static org.logcapture.assertion.ExpectedLoggingMessage.aLog
import static org.hamcrest.Matchers.allOf
import static org.hamcrest.Matchers.equalTo
import static org.hamcrest.Matchers.not

class LogCaptureSpecShould extends LogCaptureSpec {

@Shared log = LoggerFactory.getLogger(getClass())
@Shared
log = LoggerFactory.getLogger(getClass())

def "verify missing events"() {
expect:
Expand Down Expand Up @@ -42,9 +41,6 @@ class LogCaptureSpecShould extends LogCaptureSpec {
log.info("a message")
log.info("a message")

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import spock.lang.Specification

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.hamcrest.Matchers.*
import static org.logcapture.assertion.ExpectedLoggingMessage.aLog

class LogCaptureTraitShould extends Specification implements LogCaptureTrait {

Expand Down Expand Up @@ -41,8 +41,6 @@ class LogCaptureTraitShould extends Specification implements LogCaptureTrait {
log.info("a message")
log.info("a message")

def expectedLog = aLog().info().withMessage("a message")
filter(expectedLog)
.logged(expectedLog, 2)
logged(aLog().info().withMessage("a message"), 2)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package org.logcapture.spock2

import ch.qos.logback.classic.Logger
import ch.qos.logback.classic.spi.ILoggingEvent
import org.hamcrest.Matcher
import org.logcapture.LogCapture
import org.logcapture.logback.StubAppender
import org.hamcrest.Matcher
import org.slf4j.LoggerFactory
import spock.lang.Specification

Expand All @@ -30,7 +30,7 @@ class LogCaptureSpec extends Specification {
return new LogCapture<>(logAppender.events()).logged(expectedLoggingMessage)
}

LogCapture filter(Matcher<List<ILoggingEvent>> expectedLoggingMessage) {
return new LogCapture<>(logAppender.events()).filter(expectedLoggingMessage)
LogCapture logged(Matcher<List<ILoggingEvent>> expectedLoggingMessage, Integer times) {
return new LogCapture<>(logAppender.events()).logged(expectedLoggingMessage, times)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ trait LogCaptureTrait {
return new LogCapture<>(logAppender.events()).logged(expectedLoggingMessage)
}

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


def expectedLog = aLog().info().withMessage("a message")
filter(expectedLog)
.logged(expectedLog, 2)
logged(aLog().info().withMessage("a message"), 2)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ class LogCaptureTraitShould extends Specification implements LogCaptureTrait {
log.info("a message")
log.info("a message")

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

0 comments on commit 2829adf

Please sign in to comment.