Skip to content

Commit 339b0a5

Browse files
committed
Add default matcher for mdc.
* DSL has been removed as does not add any functionality.
1 parent ac450d0 commit 339b0a5

23 files changed

+149
-498
lines changed

.editorconfig

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ indent_style = space
1414
indent_size = 2
1515
continuation_indent_size = 4
1616
wildcard_import_limit = 9999
17+
curly_bracket_next_line = false
1718

1819
[Makefile]
1920
indent_style = tab

README.md

+2-11
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,23 @@ LogCapture is a testing library for asserting logging messages.
88

99
## How it works
1010

11-
Using the DSL:
12-
13-
```java
14-
captureLogEvents(() -> log.info("a message"))
15-
.logged(aLog().info()
16-
.withMessage("a message"))
17-
1811
```
19-
2012
Using JUnit Rule:
2113
2214
```java
2315
@Rule
24-
public LogCaptureRule logCaptureRule = new LogCaptureRule();
16+
public LogCaptureRule logCapture = new LogCaptureRule();
2517
2618
@Test
2719
public void verify_logs_using_rule() {
2820
log.info("a message");
2921
30-
logCaptureRule.logged(aLog().info().withMessage("a message"));
22+
logCapture.logged(aLog().info().withMessage("a message"));
3123
}
3224
```
3325

3426
More example how to use the library at [ExampleShould.java](https://github.com/mustaine/logcapture/blob/master/src/test/java/com/logcapture/example/ExampleShould.java)
3527

36-
3728
## Binaries
3829

3930
Binaries and dependency information for Maven, Ivy, Gradle and others can be found at [http://search.maven.org](http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22com.jspcore%22%20AND%20a%3A%22logcapture%22).

build.gradle

+7-11
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ plugins {
1414
}
1515

1616
group 'com.jspcore'
17-
version '0.4.7-SNAPSHOT'
17+
version '0.5.0-SNAPSHOT'
1818
description = 'A testing library for assert logging messages.'
1919

2020
apply plugin: 'idea'
@@ -31,19 +31,19 @@ repositories {
3131
}
3232

3333
ext {
34-
junit5_version = "5.2.0"
34+
junit5_version = "5.5.0"
3535
}
3636

3737
dependencies {
3838
api "org.hamcrest:hamcrest-library:2.1"
3939
implementation "org.slf4j:slf4j-api:1.7.22"
40-
implementation "ch.qos.logback:logback-classic:1.1.8"
40+
implementation "ch.qos.logback:logback-classic:1.2.3"
4141
implementation "org.awaitility:awaitility:3.1.6"
4242
implementation "junit:junit:4.12"
4343
implementation "org.junit.jupiter:junit-jupiter-api:$junit5_version"
4444
implementation 'org.spockframework:spock-core:1.3-groovy-2.5'
4545

46-
testImplementation "org.assertj:assertj-core:3.6.1"
46+
testImplementation "org.assertj:assertj-core:3.12.2"
4747
testImplementation "org.junit.jupiter:junit-jupiter-engine:$junit5_version"
4848
testImplementation "org.junit.jupiter:junit-jupiter-params:$junit5_version"
4949
testImplementation "org.junit.vintage:junit-vintage-engine:$junit5_version"
@@ -100,17 +100,13 @@ task sourceJar(type: Jar) {
100100
from sourceSets.main.allJava
101101
}
102102

103-
wrapper {
104-
gradleVersion = '5.4'
105-
}
106-
107103
publishing {
108104
publications {
109105
MyPublication(MavenPublication) {
110106
from components.java
111107
groupId 'com.jspcore'
112108
artifactId 'logcapture'
113-
version '0.4.7'
109+
version '0.5.0'
114110

115111
artifacts {
116112
artifact sourceJar {
@@ -144,8 +140,8 @@ bintray {
144140
licenses = ['MIT']
145141
vcsUrl = 'https://github.com/mustaine/logcapture.git'
146142
version {
147-
name = '0.4.7'
148-
vcsTag = '0.4.7'
143+
name = '0.5.0'
144+
vcsTag = '0.5.0'
149145
released = new Date()
150146
}
151147
}

gradle/wrapper/gradle-wrapper.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4-bin.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-5.5.1-bin.zip

src/main/groovy/com/logcapture/spock/LogCaptureSpec.groovy

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class LogCaptureSpec extends Specification {
3131
}
3232

3333
LogCaptureSpec logged(Matcher<List<ILoggingEvent>> expectedLoggingMessage) {
34-
new LogCapture<>(logAppender.events(), null).logged(expectedLoggingMessage)
34+
new LogCapture<>(logAppender.events()).logged(expectedLoggingMessage)
3535
return this
3636
}
3737
}

src/main/groovy/com/logcapture/spock/LogCaptureTrait.groovy

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.logcapture.junit
1+
package com.logcapture.spock
22

33
import ch.qos.logback.classic.Logger
44
import ch.qos.logback.classic.spi.ILoggingEvent
@@ -28,7 +28,7 @@ trait LogCaptureTrait {
2828
}
2929

3030
LogCaptureTrait logged(Matcher<List<ILoggingEvent>> expectedLoggingMessage) {
31-
new LogCapture<>(logAppender.events(), null).logged(expectedLoggingMessage)
31+
new LogCapture<>(logAppender.events()).logged(expectedLoggingMessage)
3232
return this
3333
}
3434
}

src/main/java/com/logcapture/Await.java

-11
This file was deleted.
+3-37
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,24 @@
11
package com.logcapture;
22

33
import ch.qos.logback.classic.spi.ILoggingEvent;
4-
import com.logcapture.assertion.ExpectedLoggingMessage;
54
import com.logcapture.assertion.VerificationException;
65
import org.hamcrest.Matcher;
76

87
import java.util.List;
9-
import java.util.function.Consumer;
10-
import java.util.function.Supplier;
118

129
public class LogCapture<T> {
1310

1411
private final List<ILoggingEvent> events;
15-
private final T result;
1612

17-
public LogCapture(List<ILoggingEvent> events, T result) {
13+
public LogCapture(List<ILoggingEvent> events) {
1814
this.events = events;
19-
this.result = result;
2015
}
2116

2217
public LogCapture<T> logged(Matcher<List<ILoggingEvent>> expectedLoggingMessage) {
23-
if (expectedLoggingMessage.matches(events)) {
24-
return this;
18+
if (expectedLoggingMessage.matches(events)) {
19+
return this;
2520
}
2621

2722
throw VerificationException.forUnmatchedLog(expectedLoggingMessage, events);
2823
}
29-
30-
public LogCapture<T> assertions(Consumer<T> assertions) {
31-
assertions.accept(result);
32-
return this;
33-
}
34-
35-
public static LogCapture<Void> captureLogEvents(Runnable codeBlock) {
36-
return LogbackInterceptor.captureLogEvents(codeBlock);
37-
}
38-
39-
public static LogCapture<Void> captureLogEvents(Runnable codeBlock, String loggerName) {
40-
return LogbackInterceptor.captureLogEvents(codeBlock, loggerName);
41-
}
42-
43-
public static <T> LogCapture<T> captureLogEvents(Supplier<T> codeBlock) {
44-
return LogbackInterceptor.captureLogEvents(codeBlock);
45-
}
46-
47-
public static <T> LogCapture<T> captureLogEvents(Supplier<T> codeBlock, String loggerName) {
48-
return LogbackInterceptor.captureLogEvents(codeBlock, loggerName);
49-
}
50-
51-
public static Await<Void> captureLogEventsAsync(Runnable codeBlock) {
52-
return (duration, condition) -> LogbackInterceptor.captureLogEventsAsync(codeBlock, duration, condition);
53-
}
54-
55-
public static <T> Await<T> captureLogEventsAsync(Supplier<T> codeBlock) {
56-
return (duration, condition) -> LogbackInterceptor.captureLogEventsAsync(codeBlock, duration, condition);
57-
}
5824
}

src/main/java/com/logcapture/LogbackInterceptor.java

-84
This file was deleted.

src/main/java/com/logcapture/assertion/ExpectedLoggedException.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public boolean matches(ILoggingEvent event) {
2727
}
2828

2929
return expectedMessageMatcher.matches(event.getThrowableProxy().getMessage()) &&
30-
expectedException.matches(event.getThrowableProxy());
30+
expectedException.matches(event.getThrowableProxy());
3131
}
3232

3333
public ExpectedLoggedException withMessage(Matcher<String> expectedMessageMatcher) {
@@ -38,9 +38,9 @@ public ExpectedLoggedException withMessage(Matcher<String> expectedMessageMatche
3838
@Override
3939
public String toString() {
4040
return "ExpectedLoggedException{" +
41-
"expectedMessageMatcher=" + expectedMessageMatcher +
42-
", expectedException=" + expectedException +
43-
'}';
41+
"expectedMessageMatcher=" + expectedMessageMatcher +
42+
", expectedException=" + expectedException +
43+
'}';
4444
}
4545

4646
public static final ExpectedLoggedException ANYTHING = new ExpectedLoggedException() {

src/main/java/com/logcapture/assertion/ExpectedLoggingMessage.java

+5
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ public ExpectedLoggingMessage length(Matcher<Integer> expectedLengthMatcher) {
136136
return this;
137137
}
138138

139+
public ExpectedLoggingMessage withMdc(String mdcKey, String mdcValue) {
140+
withMdc(mdcKey, equalTo(mdcValue));
141+
return this;
142+
}
143+
139144
public ExpectedLoggingMessage withMdc(String mdcKey, Matcher<String> mdcValue) {
140145
mdcMatcher.put(mdcKey, mdcValue);
141146
return this;

src/main/java/com/logcapture/assertion/VerificationException.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@ private VerificationException(String message) {
1414

1515
public static VerificationException forUnmatchedLog(Matcher<List<ILoggingEvent>> expectedLogMessage, List<ILoggingEvent> logEvents) {
1616
return new VerificationException(String.format(
17-
"Expected matching: \n%s\nLogs received: \n%s",
18-
expectedLogMessage.toString(),
19-
logEvents.stream()
20-
.map(VerificationException::formatLogEvent)
21-
.collect(Collectors.joining("\n"))
17+
"Expected matching: \n%s\nLogs received: \n%s",
18+
expectedLogMessage.toString(),
19+
logEvents.stream()
20+
.map(VerificationException::formatLogEvent)
21+
.collect(Collectors.joining("\n"))
2222
));
2323
}
2424

2525
private static String formatLogEvent(ILoggingEvent log) {
2626
return String.format("level: %s marker: %s mdc: %s message: %s", log.getLevel(),
27-
log.getMarker(),
28-
log.getMDCPropertyMap(),
29-
log.getFormattedMessage());
27+
log.getMarker(),
28+
log.getMDCPropertyMap(),
29+
log.getFormattedMessage());
3030
}
3131
}

src/main/java/com/logcapture/junit/LogCaptureRule.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void evaluate() throws Throwable {
5353
}
5454

5555
public LogCaptureRule logged(Matcher<List<ILoggingEvent>> expectedLoggingMessage) {
56-
new LogCapture<>(logAppender.events(), null).logged(expectedLoggingMessage);
56+
new LogCapture<>(logAppender.events()).logged(expectedLoggingMessage);
5757
return this;
5858
}
5959
}

src/main/java/com/logcapture/junit5/LogCaptureExtension.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void afterEach(ExtensionContext context) {
4242
}
4343

4444
public LogCaptureExtension logged(Matcher<List<ILoggingEvent>> expectedLoggingMessage) {
45-
new LogCapture<>(logAppender.events(), null).logged(expectedLoggingMessage);
45+
new LogCapture<>(logAppender.events()).logged(expectedLoggingMessage);
4646
return this;
4747
}
4848

src/main/java/com/logcapture/matcher/TypedAnythingMatcher.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import org.hamcrest.BaseMatcher;
44
import org.hamcrest.Description;
55

6-
public class TypedAnythingMatcher<T> extends BaseMatcher<T> {
6+
public class TypedAnythingMatcher<T> extends BaseMatcher<T> {
77

88
@Override
99
public boolean matches(Object item) {

src/test/groovy/com/logcapture/spock/LogCaptureTraitShould.groovy

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.logcapture.spock
22

33
import com.logcapture.junit.LogCaptureRuleShould
4-
import com.logcapture.junit.LogCaptureTrait
54
import org.slf4j.LoggerFactory
65
import spock.lang.Shared
76
import spock.lang.Specification

0 commit comments

Comments
 (0)