Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SLF4J module #7

Merged
merged 10 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .github/workflows/check-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: "Check release"

on:
push:
tags:
- v*

permissions: read-all

jobs:
build:
name: Maven build
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup Java JDK
uses: actions/setup-java@v4
with:
distribution: 'corretto'
java-version: 11
- name: Build with Maven
env:
SIGN_KEY: ${{ secrets.SIGN_KEY }}
SIGN_KEY_PASS: ${{ secrets.SIGN_KEY_PASS }}
run: ./mvnw --batch-mode -s .mvn/ci_settings.xml -ntp -DskipTests -DskipPublishing -Prun-its,release
28 changes: 28 additions & 0 deletions .github/workflows/sonatype.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: "Publish artifacts"

on:
release:
types: [ created ]

permissions: read-all

jobs:
publish:
name: Publish to Maven Central
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup Java JDK
uses: actions/setup-java@v4
with:
distribution: 'corretto'
java-version: 11
- name: Build and publish with Maven
env:
SIGN_KEY: ${{ secrets.SIGN_KEY }}
SIGN_KEY_PASS: ${{ secrets.SIGN_KEY_PASS }}
SONATYPE_TOKEN_USERNAME: ${{ secrets.SONATYPE_TOKEN_USERNAME }}
SONATYPE_TOKEN_PASSWORD: ${{ secrets.SONATYPE_TOKEN_PASSWORD }}
run: ./mvnw --batch-mode -s .mvn/ci_settings.xml -ntp -DskipTests -Prelease
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Unreleased

- Rename to Mock Loggers
- Add SLF4J and Apache Commons Logging modules

## 1.1.3 - 2024-11-09

Expand Down
4 changes: 2 additions & 2 deletions commons-logging/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@
</dependency>
</dependencies>
<description>Mock loggers for Apache Commons Logging backed by Mockito.</description>
<name>Mock loggers for Apache Commons Logging</name>
<modelVersion>4.0.0</modelVersion>
<name>Mock loggers for Apache Commons Logging</name>
<parent>
<artifactId>mock-loggers</artifactId>
<groupId>io.github.vitalijr2.logging</groupId>
Expand All @@ -100,4 +100,4 @@
<id>run-its</id>
</profile>
</profiles>
</project>
</project>
7 changes: 4 additions & 3 deletions commons-logging/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
> This library does not support _parallel test execution_.

[![Java Version][java-version]][jdk-download]
![Commons Logging Version][commons-logging-version]
![Mockito Version][mockito-version]
![Maven Central Last Update][maven-central-last-update]
[![Maven Central][maven-central]][maven-central-link]
Expand All @@ -25,8 +26,6 @@ Just put a test dependency to your POM:

The simplest usage example looks like this:
```java
import org.apache.commons.logging.LogFactory;

@Test
void helloWorld() {
var helloService = new HelloService();
Expand Down Expand Up @@ -55,7 +54,7 @@ static void setUpClass() {
// clean the mock logger after each test
@AfterEach
void tearDown() {
clearInvocations(logger);
clearInvocations(log);
}

// use the mock logger in a test
Expand Down Expand Up @@ -143,6 +142,8 @@ See more details at [HelloServiceAnnotationTest.java](src/it/hello-commons-loggi

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

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

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

[maven-central-last-update]: https://img.shields.io/maven-central/last-update/io.github.vitalijr2.logging/mock-loggers-commons-logging
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,30 @@
import org.apache.commons.logging.LogFactory;
import org.jetbrains.annotations.VisibleForTesting;

/**
* Uses {@link org.mockito.Mockito#mock(Class, String)} to get a mock that is adapted for {@link System.Logger}.
* <p>
* Example:
* <pre><code class="language-java">
* {@literal @}Test
* void helloWorld() {
* var helloService = new HelloService();
*
* assertDoesNotThrow(helloService::sayHelloWorld);
*
* verify(LogFactory.getLog(helloService.getClass())).info("Hello World!");
* }
* </code></pre>
*
* @since 1.0.0
*/
public class MockLoggerFactory extends LogFactory implements MockLoggerCleaner {

private final Map<String, Log> loggers;

/**
* Create a map-based logger finder. The finder uses a concurrent map: a logger name is a key.
*/
public MockLoggerFactory() {
this(new ConcurrentHashMap<>());
}
Expand Down Expand Up @@ -71,6 +91,13 @@ public Log getInstance(Class<?> clazz) throws LogConfigurationException {
return getInstance(clazz.getName());
}

/**
* Returns an instance of Logger for the given name.
*
* @param name logging name
* @return mock logger
* @throws LogConfigurationException this is never thrown
*/
@Override
public Log getInstance(String name) throws LogConfigurationException {
return loggers.computeIfAbsent(name, key -> mock(Log.class, "Mock for logger " + key));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@ void clearLoggers() {
assertThat(loggers.entrySet(), emptyIterable());
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,4 @@ void cleanAndResetMockLoggers() {
verifyNoInteractions(loggerFactory.getInstance("test"));
}

}
}
2 changes: 1 addition & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,4 @@
<groupId>io.github.vitalijr2.logging</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ void cleanerSubscribesToNotifications() {
verify(cleaner).cleanAndReset();
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ void happyPath() {
assertThat(loggerNames, contains("a", "b", "c", "x", "y", "z"));
}

}
}
4 changes: 2 additions & 2 deletions jdk-platform-logging/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@
</dependency>
</dependencies>
<description>Mock loggers for JDK Platform Logging backed by Mockito.</description>
<name>Mock loggers for JDK Platform Logging</name>
<modelVersion>4.0.0</modelVersion>
<name>Mock loggers for JDK Platform Logging</name>
<parent>
<artifactId>mock-loggers</artifactId>
<groupId>io.github.vitalijr2.logging</groupId>
Expand All @@ -95,4 +95,4 @@
<id>run-its</id>
</profile>
</profiles>
</project>
</project>
7 changes: 4 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@
<version>3.5.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
<version>3.1.3</version>
</plugin>
<plugin>
Expand Down Expand Up @@ -387,6 +387,7 @@
<module>commons-logging</module>
<module>core</module>
<module>jdk-platform-logging</module>
<module>slf4j</module>
</modules>
<name>Mock Loggers</name>
<packaging>pom</packaging>
Expand Down Expand Up @@ -448,10 +449,10 @@
<plugins>
<plugin>
<artifactId>jacoco-maven-plugin</artifactId>
<groupId>org.jacoco</groupId>
<configuration>
<skip>true</skip>
</configuration>
<groupId>org.jacoco</groupId>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
Expand All @@ -462,10 +463,10 @@
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
<configuration>
<installAtEnd>false</installAtEnd>
</configuration>
<groupId>org.apache.maven.plugins</groupId>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
Expand Down
18 changes: 15 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,19 @@ void helloWorld() {
}
```

Now this library implements services for [JDK Platform Logging][jdk-logging]
and [Apache Commons Logging][commons-logging].
Now this library implements services for [JDK Platform Logging][jdk-logging],
[Apache Commons Logging][commons-logging] and [SLF4J][slf4j].

See more examples in the relevant modules of this project:

- for JDK Platform Logging in [mock-loggers-jdk-platform-logging](jdk-platform-logging)
- for Apache Commons Logging in [mock-loggers-commons-logging](commons-logging)
- for JDK Platform Logging in [mock-loggers-jdk-platform-logging](jdk-platform-logging)
- for SLF4J in [mock-loggers-slf4j](slf4j)

## Other logging libraries and frameworks

- [Apache Log4j: Unit Testing in Maven][log4j-unit-testing-in-maven]
- [Unit Test logback Using JUnit][logback-unit-test-using-junit], it's old but gold

## Credits

Expand Down Expand Up @@ -76,6 +82,8 @@ limitations under the License.

[commons-logging]: https://commons.apache.org/proper/commons-logging/

[slf4j]: https://www.slf4j.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
Expand All @@ -102,6 +110,10 @@ limitations under the License.

[today-hits-link]: https://hits.sh/github.com/vitalijr2/mock-loggers/

[log4j-unit-testing-in-maven]: https://logging.apache.org/log4j/2.3.x/manual/configuration.html#UnitTestingInMaven

[logback-unit-test-using-junit]: https://www.iamninad.com/posts/unit-test-logback-using-junit/

[slf4j-mock]: https://github.com/s4u/slf4j-mock

[mock-slf4j-impl]: https://github.com/ocarlsen/mock-slf4j-impl
Loading