-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial log capture utility for testing
- Loading branch information
1 parent
6c16a57
commit 397f6b0
Showing
7 changed files
with
242 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
utilities/src/main/java/io/smallrye/testing/logging/InMemoryLogHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
utilities/src/main/java/io/smallrye/testing/logging/LogCapture.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
utilities/src/test/java/io/smallrye/testing/logging/LogEmptyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
utilities/src/test/java/io/smallrye/testing/logging/LogFilteringTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
utilities/src/test/java/io/smallrye/testing/logging/LogLevelSetTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |