Skip to content

Commit

Permalink
Implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalijr2 committed Feb 13, 2025
1 parent 0497ca5 commit 981cf3a
Show file tree
Hide file tree
Showing 14 changed files with 357 additions and 29 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
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
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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*-
* ---------------LICENSE_START-----------------
* Mock Loggers
* ---------------------------------------------
* Copyright (C) 2024 Vitalij Berdinskih
* ---------------------------------------------
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ---------------LICENSE_END-------------------
*/
package io.github.vitalijr2.logging.mock.elf4j;

import static org.mockito.Mockito.clearInvocations;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;

import elf4j.Logger;
import elf4j.spi.LogServiceProvider;
import io.github.vitalijr2.logging.mock.MockLoggerCleaner;
import java.util.List;
import org.jetbrains.annotations.VisibleForTesting;

/**
* Uses {@link org.mockito.Mockito#mock(Class, String)} to get a mock that is adapted for {@link Logger}.
* <p>
* Example:
* <pre><code class="language-java">
* {@literal @}Test
* void helloWorld() {
* var helloService = new HelloService();
*
* assertDoesNotThrow(helloService::sayHelloWorld);
*
* verify(LoggerFactory.getLogger("HelloService")).log(Level.INFO, "Hello World!");
* }
* </code></pre>
*
* @since 1.2.0
*/
public class MockLoggerProvider implements LogServiceProvider, MockLoggerCleaner {

private final Logger logger;

/**
* Create a logger provider with a common mock logger.
*/
public MockLoggerProvider() {
this(mock(Logger.class, "Mock logger"));
}

@VisibleForTesting
MockLoggerProvider(Logger logger) {
this.logger = logger;
subscribeToNotifications();
}

@Override
public List<String> cleanAndReset() {
clearInvocations(logger);
reset(logger);
return List.of("default");
}

/**
* Returns an instance of Logger.
*
* @return mock logger
*/
@Override
public Logger logger() {
return logger;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.github.vitalijr2.logging.mock.elf4j.MockLoggerProvider
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.github.vitalijr2.logging.mock.elf4j;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.verifyNoInteractions;

import elf4j.Logger;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@Tag("fast")
@ExtendWith(MockitoExtension.class)
class MockLoggerProviderFastTest {

@Mock
private Logger logger;

@DisplayName("Create provider")
@Test
void createAndInitializeProvider() {
// given
var provider = new MockLoggerProvider(logger);

// when and then
assertEquals(logger, provider.logger());
}

@DisplayName("Clean and reset the logger")
@Test
void cleanAndResetTheLogger() {
// given
var provider = new MockLoggerProvider(logger);

provider.logger().log("Test message");

// when
var loggerNames = provider.cleanAndReset();

// then
assertThat(loggerNames, contains("default"));
verifyNoInteractions(logger);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package io.github.vitalijr2.logging.mock.elf4j;

import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.Mockito.clearInvocations;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;

import elf4j.Logger;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;

@Tag("slow")
class MockLoggerProviderSlowTest {

private static Logger logger;

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

@AfterEach
void afterEach() {
clearInvocations(logger);
}

@DisplayName("Test")
@ParameterizedTest
@CsvFileSource(resources = "elf4j.csv", numLinesToSkip = 1)
void test(String level, String message, int traceCount, int debugCount, int infoCount, int warningCount,
int errorCount) {
// given
verifyNoInteractions(logger);
when(logger.atTrace()).thenReturn(logger);
when(logger.atDebug()).thenReturn(logger);
when(logger.atInfo()).thenReturn(logger);
when(logger.atWarn()).thenReturn(logger);
when(logger.atError()).thenReturn(logger);

// when
switch (level) {
case "TRACE":
logger.atTrace().log(message);
break;
case "DEBUG":
logger.atDebug().log(message);
break;
case "INFO":
logger.atInfo().log(message);
break;
case "WARNING":
logger.atWarn().log(message);
break;
case "ERROR":
logger.atError().log(message);
break;
default:
fail("Unknown level");
}

verify(logger, times(traceCount)).log("test trace message");
verify(logger, times(debugCount)).log("test debug message");
verify(logger, times(infoCount)).log("test info message");
verify(logger, times(warningCount)).log("test warning message");
verify(logger, times(errorCount)).log("test error message");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
level, message,trace count,debug count,info count,warning count,error count
TRACE, test trace message,1,0,0,0,0
DEBUG, test debug message,0,1,0,0,0
INFO, test info message,0,0,1,0,0
WARNING, test warning message,0,0,0,1,0
ERROR, test error message,0,0,0,0,1
4 changes: 0 additions & 4 deletions jdk-platform-logging/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,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 JDK Platform Logging backed by Mockito.</description>
<modelVersion>4.0.0</modelVersion>
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@
<module>bom</module>
<module>commons-logging</module>
<module>core</module>
<module>elf4j</module>
<module>jdk-platform-logging</module>
<module>slf4j</module>
<module>tinylog-provider</module>
Expand Down
Loading

0 comments on commit 981cf3a

Please sign in to comment.