Skip to content

Commit

Permalink
Initial log capture utility for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
kenfinnigan committed Jul 2, 2020
1 parent 6c16a57 commit 397f6b0
Show file tree
Hide file tree
Showing 7 changed files with 242 additions and 7 deletions.
6 changes: 5 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@

<dependencyManagement>
<dependencies>

<dependency>
<groupId>io.smallrye.common</groupId>
<artifactId>smallrye-common-constraint</artifactId>
<version>1.0.2</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down
28 changes: 22 additions & 6 deletions utilities/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,34 @@
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
</dependency>

<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging-annotations</artifactId>
<groupId>io.smallrye.common</groupId>
<artifactId>smallrye-common-constraint</artifactId>
</dependency>

<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging-processor</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${version.junit5}</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.jboss.logmanager</groupId>
<artifactId>jboss-logmanager</artifactId>
<version>2.1.17.Final</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.smallrye.testing.logging;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;

import io.smallrye.common.constraint.Assert;

public class InMemoryLogHandler extends Handler {

public InMemoryLogHandler(Predicate<LogRecord> predicate) {
Assert.checkNotNullParam("predicate", predicate);
setFilter(predicate::test);
setLevel(Level.FINE);
}

final List<LogRecord> records = new ArrayList<>();

@Override
public void publish(LogRecord record) {
if (!isLoggable(record)) {
return;
}

records.add(record);
}

@Override
public void flush() {
}

@Override
public void close() throws SecurityException {
records.clear();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.smallrye.testing.logging;

import java.util.List;
import java.util.function.Predicate;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;

public class LogCapture implements BeforeAllCallback {

private static final Logger rootLogger;

private InMemoryLogHandler inMemoryLogHandler = new InMemoryLogHandler((r) -> false);

static {
System.setProperty("java.util.logging.manager", "org.jboss.logmanager.LogManager");
rootLogger = LogManager.getLogManager().getLogger("");
}

public static LogCapture none() {
return new LogCapture();
}

public static LogCapture with(Predicate<LogRecord> predicate) {
return LogCapture.with(predicate, Level.INFO);
}

public static LogCapture with(Predicate<LogRecord> predicate, Level logLevel) {
LogCapture capture = new LogCapture(predicate);
return capture.setLevel(logLevel);
}

private LogCapture() {
// Capture nothing by default
}

private LogCapture(Predicate<LogRecord> predicate) {
inMemoryLogHandler = new InMemoryLogHandler(predicate);
}

@Override
public void beforeAll(ExtensionContext context) throws Exception {
rootLogger.addHandler(inMemoryLogHandler);
}

private LogCapture setLevel(Level newLevel) {
rootLogger.setLevel(newLevel);
inMemoryLogHandler.setLevel(newLevel);
return this;
}

public List<LogRecord> records() {
return inMemoryLogHandler.records;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.smallrye.testing.logging;

import static org.assertj.core.api.Assertions.assertThat;

import org.jboss.logging.Logger;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

public class LogEmptyTest {
@RegisterExtension
static LogCapture logCapture = LogCapture.none();

private final Logger logger = Logger.getLogger(LogEmptyTest.class);

@Test
void testEmptyLogContent() {
logger.debug("Writing a debug level message.");
logger.info("Writing an info level message.");
logger.warn("Writing a warning level message.");
logger.trace("Writing a trace level message.");

assertThat(LogEmptyTest.logCapture.records()).hasSize(0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.smallrye.testing.logging;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.List;
import java.util.logging.LogRecord;

import org.jboss.logging.Logger;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

public class LogFilteringTest {
@RegisterExtension
static LogCapture logCapture = LogCapture.with(r -> LogFilteringTest.class.getName().equals(r.getLoggerName()));

private final Logger logger = Logger.getLogger(LogFilteringTest.class);
private final Logger anotherLogger = Logger.getLogger("io.smallrye.testing.logging.AnotherLogger");

private static final String INFO = "Writing an info level message.";
private static final String WARNING = "Writing a warning level message.";

@Test
void testFilteredCategory() {
logger.debug("Writing a debug level message.");
logger.info(INFO);
logger.warn(WARNING);
logger.trace("Writing a trace level message.");

anotherLogger.debug("Another logger writing a debug message.");
anotherLogger.info(INFO);

List<LogRecord> records = LogFilteringTest.logCapture.records();
assertThat(records).hasSize(2);
assertThat(records)
.filteredOn(r -> r.getMessage().contains(INFO))
.hasOnlyOneElementSatisfying(r -> assertThat(r.getMessage()).contains(INFO));
assertThat(records)
.filteredOn(r -> r.getMessage().contains(WARNING))
.hasOnlyOneElementSatisfying(r -> assertThat(r.getMessage()).contains(WARNING));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.smallrye.testing.logging;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.LogRecord;

import org.jboss.logging.Logger;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

public class LogLevelSetTest {
@RegisterExtension
static LogCapture logCapture = LogCapture.with(r -> LogLevelSetTest.class.getName().equals(r.getLoggerName()),
Level.ALL);

private final Logger logger = Logger.getLogger(LogLevelSetTest.class);
private final Logger anotherLogger = Logger.getLogger("io.smallrye.testing.logging.AnotherLogger");

private static final String INFO = "Writing an info level message.";
private static final String WARNING = "Writing a warning level message.";
private static final String DEBUG = "Writing a debug level message.";
private static final String TRACE = "Writing a trace level message.";

@Test
void testLogLevelSetting() {
logger.debug(DEBUG);
logger.info(INFO);
logger.warn(WARNING);
logger.trace(TRACE);

anotherLogger.debug("Another logger writing a debug message.");
anotherLogger.info(INFO);
anotherLogger.trace(TRACE);

List<LogRecord> records = LogLevelSetTest.logCapture.records();
assertThat(records).hasSize(4);
assertThat(records)
.filteredOn(r -> r.getMessage().contains(INFO))
.hasOnlyOneElementSatisfying(r -> assertThat(r.getMessage()).contains(INFO));
assertThat(records)
.filteredOn(r -> r.getMessage().contains(WARNING))
.hasOnlyOneElementSatisfying(r -> assertThat(r.getMessage()).contains(WARNING));
assertThat(records)
.filteredOn(r -> r.getMessage().contains(DEBUG))
.hasOnlyOneElementSatisfying(r -> assertThat(r.getMessage()).contains(DEBUG));
assertThat(records)
.filteredOn(r -> r.getMessage().contains(TRACE))
.hasOnlyOneElementSatisfying(r -> assertThat(r.getMessage()).contains(TRACE));
}
}

0 comments on commit 397f6b0

Please sign in to comment.