Skip to content

Commit

Permalink
elf4j implementation
Browse files Browse the repository at this point in the history
* Implementation

* Fix javadoc

* Update README

* Update README

* Update README

* Add the integration test

* Update README

* Update README

* Update README

* Add the package-info, TOC to README

* Impove integration tests for commons-logging, jdk-platform-logging and slf4j

* Add an integration test
  • Loading branch information
vitalijr2 authored Feb 21, 2025
1 parent 0497ca5 commit 677ea62
Show file tree
Hide file tree
Showing 27 changed files with 959 additions and 39 deletions.
7 changes: 4 additions & 3 deletions THIRD-PARTY.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@

Lists of 13 third-party dependencies.
(Apache License version 2.0) Apache Commons Logging (commons-logging:commons-logging:1.3.4 - https://commons.apache.org/proper/commons-logging/)
Lists of 14 third-party dependencies.
(Apache License version 2.0) Apache Commons Logging (commons-logging:commons-logging:1.3.5 - https://commons.apache.org/proper/commons-logging/)
(MIT License) elf4j (io.github.elf4j:elf4j:4.1.0 - https://github.com/elf4j/elf4j)
(Apache License version 2.0) Byte Buddy (without dependencies) (net.bytebuddy:byte-buddy:1.15.11 - https://bytebuddy.net/byte-buddy)
(Apache License version 2.0) Byte Buddy agent (net.bytebuddy:byte-buddy-agent:1.15.11 - https://bytebuddy.net/byte-buddy-agent)
(Apache License version 2.0) org.apiguardian:apiguardian-api (org.apiguardian:apiguardian-api:1.1.2 - https://github.com/apiguardian-team/apiguardian)
(Apache License version 2.0) JetBrains Java Annotations (org.jetbrains:annotations:26.0.1 - https://github.com/JetBrains/java-annotations)
(Apache License version 2.0) JetBrains Java Annotations (org.jetbrains:annotations:26.0.2 - https://github.com/JetBrains/java-annotations)
(Eclipse Public License - v 2.0) JUnit Jupiter API (org.junit.jupiter:junit-jupiter-api:5.11.4 - https://junit.org/junit5/)
(Eclipse Public License - v 2.0) JUnit Platform Commons (org.junit.platform:junit-platform-commons:1.11.4 - https://junit.org/junit5/)
(MIT License) mockito-core (org.mockito:mockito-core:5.15.2 - https://github.com/mockito/mockito)
Expand Down
6 changes: 6 additions & 0 deletions bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
<scope>test</scope>
<version>1.2.0-SNAPSHOT</version>
</dependency>
<dependency>
<artifactId>mock-loggers-elf4j</artifactId>
<groupId>io.github.vitalijr2.logging</groupId>
<scope>test</scope>
<version>1.2.0-SNAPSHOT</version>
</dependency>
<dependency>
<artifactId>mock-loggers-jdk-platform-logging</artifactId>
<groupId>io.github.vitalijr2.logging</groupId>
Expand Down
4 changes: 0 additions & 4 deletions commons-logging/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@
<groupId>org.mockito</groupId>
<scope>provided</scope>
</dependency>
<dependency>
<artifactId>mockito-junit-jupiter</artifactId>
<groupId>org.mockito</groupId>
</dependency>
</dependencies>
<description>Mock loggers for Apache Commons Logging backed by Mockito.</description>
<modelVersion>4.0.0</modelVersion>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.vitalijr2.logging.mock.commons;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.Mockito.clearInvocations;
import static org.mockito.Mockito.times;
Expand Down Expand Up @@ -77,14 +79,14 @@ void test(String level, String message, int traceCount, int debugCount, int info
@Test
void cleanAndResetMockLoggers() {
// when
var loggerFactory = new MockLoggerFactory();
log.info("test message");

loggerFactory.getInstance("test").info("test message");

MockLoggerKeeper.getInstance().cleanAndReset();
var names = MockLoggerKeeper.getInstance().cleanAndReset();

// then
verifyNoInteractions(loggerFactory.getInstance("test"));
assertThat(names, contains(MockLoggerFactorySlowTest.class.getName()));

verifyNoInteractions(log);
}

}
125 changes: 125 additions & 0 deletions elf4j/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<artifactId>mock-loggers-elf4j</artifactId>
<build>
<plugins>
<plugin>
<artifactId>jacoco-maven-plugin</artifactId>
<groupId>org.jacoco</groupId>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Automatic-Module-Name>mock.loggers.elf4j</Automatic-Module-Name>
</manifestEntries>
</archive>
</configuration>
<groupId>org.apache.maven.plugins</groupId>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<artifactId>annotations</artifactId>
<groupId>org.jetbrains</groupId>
</dependency>
<dependency>
<artifactId>elf4j</artifactId>
<groupId>io.github.elf4j</groupId>
<version>${elf4j.version}</version>
</dependency>
<dependency>
<artifactId>hamcrest</artifactId>
<groupId>org.hamcrest</groupId>
</dependency>
<dependency>
<artifactId>junit-jupiter-engine</artifactId>
<groupId>org.junit.jupiter</groupId>
</dependency>
<dependency>
<artifactId>junit-jupiter-params</artifactId>
<groupId>org.junit.jupiter</groupId>
</dependency>
<dependency>
<artifactId>mock-loggers-core</artifactId>
<groupId>io.github.vitalijr2.logging</groupId>
<version>1.2.0-SNAPSHOT</version>
</dependency>
<dependency>
<artifactId>mockito-core</artifactId>
<groupId>org.mockito</groupId>
<scope>provided</scope>
</dependency>
<dependency>
<artifactId>mockito-junit-jupiter</artifactId>
<groupId>org.mockito</groupId>
</dependency>
</dependencies>
<description>Mock loggers for Easy Logging Facade for Java backed by Mockito.</description>
<modelVersion>4.0.0</modelVersion>
<name>Mock loggers for elf4j</name>
<parent>
<artifactId>mock-loggers</artifactId>
<groupId>io.github.vitalijr2.logging</groupId>
<version>1.2.0-SNAPSHOT</version>
</parent>
<profiles>
<profile>
<build>
<plugins>
<plugin>
<artifactId>license-maven-plugin</artifactId>
<configuration>
<skipCheckLicense>false</skipCheckLicense>
</configuration>
<groupId>org.codehaus.mojo</groupId>
</plugin>
</plugins>
</build>
<id>prepare-and-check</id>
</profile>
<profile>
<build>
<plugins>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<links>
<link>https://javadoc.io/doc/io.github.elf4j/elf4j/${elf4j.version}</link>
</links>
</configuration>
<groupId>org.apache.maven.plugins</groupId>
</plugin>
</plugins>
</build>
<id>release</id>
</profile>
<profile>
<build>
<plugins>
<plugin>
<artifactId>maven-invoker-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
</plugin>
</plugins>
</build>
<id>run-its</id>
</profile>
</profiles>
<properties>
<elf4j.version>4.1.0</elf4j.version>
</properties>
<version>1.2.0-SNAPSHOT</version>
</project>
184 changes: 184 additions & 0 deletions elf4j/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
# Mock loggers for elf4j

[elf4j][] provider with mock loggers backed by [Mockito][].

> [!WARNING]
> This library does not support _parallel test execution_.
[![Java Version][java-version]][jdk-download]
![elf4j Version][elf4j-version]
![Mockito Version][mockito-version]
![Maven Central Last Update][maven-central-last-update]
[![Maven Central][maven-central]][maven-central-link]
[![Javadoc][javadoc]][javadoc-link]

## Table of Contents

<!--ts-->
* [How to use](#how-to-use)

<!-- Created by https://github.com/ekalinin/github-markdown-toc -->
<!-- Added by: r2, at: Fri Feb 21 08:39:41 AM EET 2025 -->

<!--te-->

## How to use

Just put a test dependency to your POM:

```xml
<dependency>
<artifactId>mock-loggers-elf4j</artifactId>
<groupId>io.github.vitalijr2.logging</groupId>
<scope>test</scope>
<version>1.1.1</version>
</dependency>
```

The simplest usage example looks like this:

```java
@Test
void helloWorld() {
var helloService = new HelloService();

assertDoesNotThrow(helloService::sayHelloWorld);

verify(Logger.instance()).atInfo();
verify(Logger.instance()).log("Hello World!");
}
```

See more details at [HelloServiceBasicTest.java](src/it/hello-elf4j-world/src/test/java/example/hello/HelloServiceBasicTest.java)

> [!IMPORTANT]
> Keep in mind that all loggers are initialized only once during the test run.
Therefore, a more complex example cleans the loggers after (or before)
each test:

```java
// the static logger instance
private static Logger logger;

// initialize the mock logger once
@BeforeAll
static void setUpClass() {
logger = Logger.instance();
}

// clean the mock logger after each test
@AfterEach
void tearDown() {
clearInvocations(logger);
}

// use the mock logger in a test
@DisplayName("Names")
@ParameterizedTest(name = "<{0}>")
@ValueSource(strings = {"John", "Jane"})
void names(String name) {
var helloService = new HelloService();

when(Logger.instance().atInfo()).thenReturn(logger);

assertDoesNotThrow(() -> helloService.sayHello(name));

verify(Logger.instance()).atInfo();
verify(Logger.instance()).log("Hello " + name + "!");
verifyNoMoreInteractions(Logger.instance());
}
```

See more details at [HelloServiceFullTest.java](src/it/hello-elf4j-world/src/test/java/example/hello/HelloServiceFullTest.java)

To avoid manual cleaning of mock loggers you can use
the [jUnit extension][junit-extension] for automation:

```java
@ExtendWith(MockLoggerExtension.class)
class HelloServiceExtensionTest {

private static Logger logger;

@BeforeAll
static void setUpClass() {
logger = Logger.instance();
}

@DisplayName("Names")
@ParameterizedTest(name = "<{0}>")
@ValueSource(strings = {"John", "Jane"})
void names(String name) {
var helloService = new HelloService();

when(Logger.instance().atInfo()).thenReturn(logger);

assertDoesNotThrow(() -> helloService.sayHello(name));

verify(Logger.instance()).atInfo();
verify(Logger.instance()).log("Hello " + name + "!");
verifyNoMoreInteractions(Logger.instance());
}

}
```

See more details at [HelloServiceExtensionTest.java](src/it/hello-elf4j-world/src/test/java/example/hello/HelloServiceExtensionTest.java)

Also you can use the annotation for automation:

```java
@MockLoggers
class HelloServiceAnnotationTest {

private static Logger logger;

@BeforeAll
static void setUpClass() {
logger = Logger.instance();
}

@DisplayName("Names")
@ParameterizedTest(name = "<{0}>")
@ValueSource(strings = {"John", "Jane"})
void names(String name) {
var helloService = new HelloService();

when(Logger.instance().atInfo()).thenReturn(logger);

assertDoesNotThrow(() -> helloService.sayHello(name));

verify(Logger.instance()).atInfo();
verify(Logger.instance()).log("Hello " + name + "!");
verifyNoMoreInteractions(Logger.instance());
}

}
```

See more details at [HelloServiceAnnotationTest.java](src/it/hello-elf4j-world/src/test/java/example/hello/HelloServiceAnnotationTest.java)

[elf4j]: https://github.com/elf4j/elf4j

[Mockito]: https://site.mockito.org

[java-version]: https://img.shields.io/static/v1?label=Java&message=11&color=blue&logoColor=E23D28

[jdk-download]: https://www.oracle.com/java/technologies/downloads/#java11

[elf4j-version]: https://img.shields.io/static/v1?label=elf4j&message=4.1.0&color=blue&logoColor=E23D28

[mockito-version]: https://img.shields.io/static/v1?label=Mockito&message=5.15.2&color=blue&logoColor=E23D28

[maven-central-last-update]: https://img.shields.io/maven-central/last-update/io.github.vitalijr2.logging/mock-loggers-elf4j

[maven-central]: https://img.shields.io/maven-central/v/io.github.vitalijr2.logging/mock-loggers-elf4j

[maven-central-link]: https://central.sonatype.com/artifact/io.github.vitalijr2.logging/mock-loggers-elf4j?smo=true

[javadoc]: https://javadoc.io/badge2/io.github.vitalijr2.logging/mock-loggers-elf4j/javadoc.svg

[javadoc-link]: https://javadoc.io/doc/io.github.vitalijr2.logging/mock-loggers-elf4j

[junit-extension]: ../core/
Loading

0 comments on commit 677ea62

Please sign in to comment.