From 83a9af76969c72a44c567fc442e74dc518b4b05b Mon Sep 17 00:00:00 2001 From: Javier Salinas Date: Mon, 1 Mar 2021 23:05:34 +0100 Subject: [PATCH] Add badge sonatype --- README.md | 16 ++-- logcapture-example/logcapture-example.gradle | 6 ++ .../org/logcapture/example/ExampleShould.java | 96 +++++++++++++++++++ settings.gradle | 3 +- 4 files changed, 112 insertions(+), 9 deletions(-) create mode 100644 logcapture-example/logcapture-example.gradle create mode 100644 logcapture-example/src/test/java/org/logcapture/example/ExampleShould.java diff --git a/README.md b/README.md index a2192dc..575e1cc 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ [![Build Status](https://travis-ci.org/jsalinaspolo/logcapture.svg?branch=master)](https://travis-ci.org/jsalinaspolo/logcapture) -[![Download](https://api.bintray.com/packages/jspcore/maven/logcapture-core/images/download.svg?version=1.2.1) ](https://bintray.com/jspcore/maven/logcapture-core/1.2.1/link) +[![Sonatype Nexus](https://img.shields.io/nexus/r/org.logcapture/logcapture-core?server=https%3A%2F%2Fs01.oss.sonatype.org)](https://repo1.maven.org/maven2/org/logcapture/) [![codecov](https://codecov.io/gh/jsalinaspolo/logcapture/branch/master/graph/badge.svg)](https://codecov.io/gh/jsalinaspolo/logcapture) [![Known Vulnerabilities](https://snyk.io/test/github/jsalinaspolo/logcapture/badge.svg?targetFile=build.gradle)](https://snyk.io/test/github/jsalinaspolo/logcapture?targetFile=build.gradle) @@ -75,21 +75,21 @@ More example how to use the library at [ExampleShould.java](https://github.com/j ## Binaries -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.logcapture%22%20AND%20a%3A%22logcapture%22). +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%22org.logcapture%22%20AND%20a%3A%22logcapture%22). Gradle ``` -testImplementation 'com.logcapture:logcapture-core:x.y.z' +testImplementation 'org.logcapture:logcapture-core:x.y.z' ``` add one of the test library dependency ``` -testImplementation 'com.logcapture:logcapture-junit4:x.y.z' -testImplementation 'com.logcapture:logcapture-junit5:x.y.z' -testImplementation 'com.logcapture:logcapture-spock:x.y.z' -testImplementation 'com.logcapture:logcapture-kotest:x.y.z' +testImplementation 'org.logcapture:logcapture-junit4:x.y.z' +testImplementation 'org.logcapture:logcapture-junit5:x.y.z' +testImplementation 'org.logcapture:logcapture-spock:x.y.z' +testImplementation 'org.logcapture:logcapture-kotest:x.y.z' ``` @@ -97,7 +97,7 @@ Maven: ```xml - com.logcapture + org.logcapture logcapture-core x.y.z diff --git a/logcapture-example/logcapture-example.gradle b/logcapture-example/logcapture-example.gradle new file mode 100644 index 0000000..c2c467a --- /dev/null +++ b/logcapture-example/logcapture-example.gradle @@ -0,0 +1,6 @@ + +dependencies { + implementation "org.logcapture:logcapture-core:1.2.2" + implementation "org.logcapture:logcapture-junit4:1.2.2" + implementation "junit:junit:4.12" +} diff --git a/logcapture-example/src/test/java/org/logcapture/example/ExampleShould.java b/logcapture-example/src/test/java/org/logcapture/example/ExampleShould.java new file mode 100644 index 0000000..a8d65a6 --- /dev/null +++ b/logcapture-example/src/test/java/org/logcapture/example/ExampleShould.java @@ -0,0 +1,96 @@ +package org.logcapture.example; + +import org.logcapture.junit4.LogCaptureRule; +import org.junit.Rule; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.slf4j.MDC; +import org.slf4j.MarkerFactory; + +import static ch.qos.logback.classic.Level.INFO; +import static org.logcapture.assertion.ExpectedLoggedException.logException; +import static org.logcapture.assertion.ExpectedLoggingMessage.aLog; +import static org.logcapture.matcher.exception.ExceptionCauseMatcher.causeOf; +import static org.logcapture.matcher.exception.ExceptionCauseMessageMatcher.whereCauseMessage; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.isA; + +public class ExampleShould { + private final Logger log = LoggerFactory.getLogger(ExampleShould.class); + + @Rule + public LogCaptureRule logCaptureRule = new LogCaptureRule(); + + private ServiceThatLogs underTest = new ServiceThatLogs(); + + @Test + public void verify_captured_events() { + underTest.methodThatLogsStuff(); + logCaptureRule.logged(aLog().info() + .withMessage("a message")); + } + + @Test + public void verify_captured_events_with_marker() { + log.info(MarkerFactory.getMarker("a_marker"), "a message"); + logCaptureRule.logged(aLog() + .withLevel(equalTo(INFO)) + .withMarker("a_marker") + .withMessage(equalTo("a message"))); + } + + @Test + public void verify_captured_events_with_exception() { + RuntimeException exception = new RuntimeException(); + + underTest.methodThatLogs(exception); + logCaptureRule.logged(aLog() + .havingException(logException() + .withException(isA(RuntimeException.class)) + )); + } + + @Test + public void verify_captured_events_with_exception_cause_message() { + RuntimeException exception = new RuntimeException(new IllegalStateException("Some state is invalid")); + underTest.methodThatLogs(exception); + logCaptureRule.logged(aLog() + .havingException(logException() + .withException(whereCauseMessage(containsString("state is invalid"))) + )); + } + + @Test + public void verify_captured_events_with_exception_cause() { + RuntimeException exception = new RuntimeException(new IllegalStateException("Some state is invalid")); + underTest.methodThatLogs(exception); + logCaptureRule.logged(aLog() + .havingException(logException() + .withException(causeOf(IllegalStateException.class)) + )); + } + + @Test + public void verify_mdc_keys() { + MDC.put("aKey", "someValue"); + + underTest.methodThatLogsStuff(); + logCaptureRule.logged(aLog().info() + .withMdc("aKey", equalTo("someValue")) + .withMessage("a message")); + } + + class ServiceThatLogs { + + void methodThatLogsStuff() { + log.info("a message"); + log.info("another message"); + } + + void methodThatLogs(Exception exception) { + log.error("message", exception); + } + } +} diff --git a/settings.gradle b/settings.gradle index c7f5546..de7d907 100644 --- a/settings.gradle +++ b/settings.gradle @@ -3,7 +3,8 @@ include 'logcapture-core', 'logcapture-junit5', 'logcapture-spock', 'logcapture-spock2', - 'logcapture-kotest' + 'logcapture-kotest', + 'logcapture-example' rootProject.name = 'logcapture' rootProject.children.each { child ->