Skip to content

Commit

Permalink
Merge pull request #6 from dm-drogeriemarkt/assert-no-more-log-messages
Browse files Browse the repository at this point in the history
assertNothingElseLogged(), dependency updates and test improvements
  • Loading branch information
waschmittel authored Jan 22, 2021
2 parents 4c23a65 + 17c4718 commit 7e24b5b
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 53 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Because this is a library, Checkstyle is used to make sure all public classes/me
**Table of Contents**

* [Changes](#changes)
* [3.1.0](#310)
* [3.0.0](#300)
* [2.0.1](#201)
* [Updating from Version 1.x.x to 2.x.x](#updating-from-version-1xx-to-2xx)
Expand All @@ -29,6 +30,10 @@ Because this is a library, Checkstyle is used to make sure all public classes/me

## Changes

### 3.1.0

Added `assertNothingElseLogged()`

### 3.0.0

Updated from JUnit 4 to JUnit 5.
Expand Down Expand Up @@ -94,7 +99,8 @@ public class MyUnitTest {

logCapture
.assertLogged(Level.INFO, "^something interesting$") //second parameter is a regular expression
.thenLogged(Level.ERROR, "terrible");
.thenLogged(Level.ERROR, "terrible")
.assertNothingElseLogged();
}
}
```
Expand Down Expand Up @@ -191,7 +197,6 @@ java.lang.AssertionError: Expected log message has occurred, but never with the
other_mdc_key: "this is the other MDC value"
```


## Usage with non-JUnit Runner

If you intend to use LogCapture outside of a JUnit test, you cannot rely on JUnit's `@Rule` annotation and must call LocCapture's `addAppenderAndSetLogLevelToDebug()` and `removeAppenderAndResetLogLevel()` methods manually.
Expand Down
3 changes: 1 addition & 2 deletions checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
<module name="MethodParamPad"/>
<module name="ParenPad"/>
<module name="OperatorWrap">
<property name="option" value="NL"/>
<property name="option" value="EOL"/>
<property name="tokens"
value="BAND, BOR, BSR, BXOR, DIV, EQUAL, GE, GT, LAND, LE, LITERAL_INSTANCEOF, LOR, LT, MINUS, MOD, NOT_EQUAL, PLUS, QUESTION, SL, SR, STAR, METHOD_REF "/>
</module>
Expand Down Expand Up @@ -187,7 +187,6 @@
<module name="ConstantName">
<property name="applyToPrivate" value="false"/>
</module>
<module name="AvoidStaticImport"/>
<module name="RedundantImport"/>
<module name="UnusedImports"/>
<module name="AnonInnerLength"/>
Expand Down
14 changes: 7 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>de.dm.infrastructure</groupId>
<artifactId>log-capture</artifactId>
<version>2.0.1-SNAPSHOT</version>
<version>3.1.0-SNAPSHOT</version>

<name>Log Capture</name>
<description>Makes it possible to capture logs and assert if things have been logged</description>
Expand All @@ -30,14 +30,14 @@

<properties>
<java.version>1.8</java.version>
<lombok.version>1.18.12</lombok.version>
<lombok.version>1.18.16</lombok.version>
<logback.version>1.2.3</logback.version>
<junit.version>5.6.0</junit.version>
<junit.version>5.7.0</junit.version>
<encoding>UTF-8</encoding>

<assertj-core.version>3.15.0</assertj-core.version>
<checkstyle.version>8.30</checkstyle.version>
<jacoco.version>0.8.5</jacoco.version>
<assertj-core.version>3.18.1</assertj-core.version>
<checkstyle.version>8.39</checkstyle.version>
<jacoco.version>0.8.6</jacoco.version>
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
<maven-checkstyle-plugin.version>3.1.1</maven-checkstyle-plugin.version>
<maven-source-plugin.version>3.2.1</maven-source-plugin.version>
Expand All @@ -46,7 +46,7 @@
<maven-release-plugin.version>2.5.3</maven-release-plugin.version>
<maven-gpg-plugin.version>1.6</maven-gpg-plugin.version>
<nexus-staging-maven-plugin.version>${maven-gpg-plugin.version}.8</nexus-staging-maven-plugin.version>
<versions-maven-plugin.version>2.7</versions-maven-plugin.version>
<versions-maven-plugin.version>2.8.1</versions-maven-plugin.version>
</properties>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
@Setter
class CapturingAppender extends ContextAwareBase implements Appender<ILoggingEvent> {

List<LoggedEvent> loggedEvents = new ArrayList<>();
final Set<String> capturedPackages;
private List<LoggedEvent> loggedEvents = new ArrayList<>();
private final Set<String> capturedPackages;

private String name;
private boolean started;
Expand Down Expand Up @@ -72,6 +72,10 @@ Integer whenCapturedNext(Level level, String regex, int startIndex, ExpectedMdcE
throw new AssertionError(String.format("Expected log message has not occurred: Level: %s, Regex: \"%s\"", level, regex));
}

int getNumberOfLoggedMessages() {
return loggedEvents.size();
}

private boolean eventMatchesWithoutMdc(LoggedEvent event, Level level, Pattern pattern) {
return eventHasLevel(event, level) && eventMatchesPattern(event, pattern);
}
Expand Down
38 changes: 31 additions & 7 deletions src/main/java/de/dm/infrastructure/logcapture/LogCapture.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@
import java.util.HashSet;
import java.util.Set;

import static org.slf4j.Logger.ROOT_LOGGER_NAME;

/**
* a JUnit 4 @Rule that can be used to capture log output. Use the appropriate constructor for unit/integration tests.
*/
public final class LogCapture implements BeforeEachCallback, AfterEachCallback { //should implement AfterEachCallback, BeforeEachCallback in JUnit 5

final Set<String> capturedPackages;
private CapturingAppender capturingAppender;
private Logger rootLogger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
private Logger rootLogger = (Logger) LoggerFactory.getLogger(ROOT_LOGGER_NAME);
private HashMap<String, Level> originalLogLevels = null;

/**
Expand Down Expand Up @@ -121,17 +123,25 @@ public void removeAppenderAndResetLogLevel() {
* @param expectedMdcEntries expected MDC entries, see @{@link ExpectedMdcEntry}
*
* @return a LastCapturedLogEvent from which .thenLogged(...) can be called to assert if things have been logged in a specific order
*
* @throws AssertionError if the expected log message has not been logged
*/
public LastCapturedLogEvent assertLogged(Level level, String regex, ExpectedMdcEntry... expectedMdcEntries) {
return assertLogged(level, regex, 0, expectedMdcEntries);
return assertLogged(level, regex, null, expectedMdcEntries);
}

private LastCapturedLogEvent assertLogged(Level level, String regex, int index, ExpectedMdcEntry... expectedMdcEntries) {
private LastCapturedLogEvent assertLogged(Level level, String regex, LastCapturedLogEvent lastCapturedLogEvent, ExpectedMdcEntry... expectedMdcEntries) {
if (capturingAppender == null) {
throw new IllegalStateException("capuringAppender is null. Please make sure that either LogCapture is used with a @Rule annotation or that addAppenderAndSetLogLevelToDebug is called manually.");
throw new IllegalStateException("capuringAppender is null. " +
"Please make sure that either LogCapture is used with a @Rule annotation or that addAppenderAndSetLogLevelToDebug is called manually.");
}
Integer foundAtIndex = capturingAppender.whenCapturedNext(level, regex, index, expectedMdcEntries);
return new LastCapturedLogEvent(foundAtIndex);

Integer startIndex = lastCapturedLogEvent == null ? 0 : lastCapturedLogEvent.index + 1;
int assertedLogMessages = lastCapturedLogEvent == null ? 1 : lastCapturedLogEvent.assertedLogMessages + 1;

Integer foundAtIndex = capturingAppender.whenCapturedNext(level, regex, startIndex, expectedMdcEntries);

return new LastCapturedLogEvent(foundAtIndex, assertedLogMessages);
}

/**
Expand All @@ -140,6 +150,7 @@ private LastCapturedLogEvent assertLogged(Level level, String regex, int index,
@RequiredArgsConstructor
public class LastCapturedLogEvent {
private final int index;
private final int assertedLogMessages;

/**
* assert that something has been logged after this event
Expand All @@ -149,9 +160,22 @@ public class LastCapturedLogEvent {
* @param expectedMdcEntries expected MDC entries, see @{@link ExpectedMdcEntry}
*
* @return another LastCapturedLogEvent - for obvious reasons
*
* @throws AssertionError if the expected log message has not been logged
*/
public LastCapturedLogEvent thenLogged(Level level, String regex, ExpectedMdcEntry... expectedMdcEntries) {
return assertLogged(level, regex, index + 1, expectedMdcEntries);
return assertLogged(level, regex, this, expectedMdcEntries);
}

/**
* assert that nothing else has been logged except for the asserted log messages
*
* @throws AssertionError if something else has been logged
*/
public void assertNothingElseLogged() {
if (capturingAppender.getNumberOfLoggedMessages() > assertedLogMessages) {
throw new AssertionError("There have been other log messages than the asserted ones.");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
/**
* Everything regarding LogCapture is here
* See README.md or https://github.com/dm-drogeriemarkt/structured-logging for more information on usage
*/
package de.dm.infrastructure.logcapture;
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
import java.util.HashMap;
import java.util.Map;

public class CapturingAppenderIntegrationTest {
class CapturingAppenderIntegrationTest {

private final String TEST_KEY = "test_key";
private final String OTHER_KEY = "test_key_2";

@Test
public void containsMdcEntries() {
void containsMdcEntries() {
ExpectedMdcEntry expectedMdcEntry1 = ExpectedMdcEntry.withMdc(TEST_KEY, "test value");
ExpectedMdcEntry expectedMdcEntry2 = ExpectedMdcEntry.withMdc(OTHER_KEY, "good value");
ExpectedMdcEntry[] expectedMdcEntries = new ExpectedMdcEntry[]{expectedMdcEntry1, expectedMdcEntry2};
Expand All @@ -25,7 +25,7 @@ public void containsMdcEntries() {
}

@Test
public void nullEntriesShouldNotThrowNullPointerException() {
void nullEntriesShouldNotThrowNullPointerException() {
Map<String, String> mdcContents = new HashMap<>();
Assertions.assertTrue(CapturingAppender.containsMdcEntries(mdcContents, null));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@
import java.util.HashMap;
import java.util.Map;

public class ExpectedMdcEntryUnitTest {
class ExpectedMdcEntryUnitTest {

private static final String TEST_KEY = "test_key";

@Test
public void isContainedIn() {
void isContainedIn() {
ExpectedMdcEntry expectedMdcEntry = ExpectedMdcEntry.withMdc(TEST_KEY, "test value");
Map<String, String> mdcContents = new HashMap<>();
mdcContents.put(TEST_KEY, "this is a test value, cool!");
Assertions.assertTrue(expectedMdcEntry.isContainedIn(mdcContents));
}

@Test
public void isNotContainedIn() {
void isNotContainedIn() {
ExpectedMdcEntry expectedMdcEntry = ExpectedMdcEntry.withMdc(TEST_KEY, "test value");
Map<String, String> mdcContents = new HashMap<>();
mdcContents.put(TEST_KEY, "this is a value, cool!");
Expand Down
Loading

0 comments on commit 7e24b5b

Please sign in to comment.