-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
357 additions
and
29 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
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,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> |
83 changes: 83 additions & 0 deletions
83
elf4j/src/main/java/io/github/vitalijr2/logging/mock/elf4j/MockLoggerProvider.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,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; | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
elf4j/src/main/resources/META-INF/services/elf4j.spi.LogServiceProvider
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 @@ | ||
io.github.vitalijr2.logging.mock.elf4j.MockLoggerProvider |
49 changes: 49 additions & 0 deletions
49
elf4j/src/test/java/io/github/vitalijr2/logging/mock/elf4j/MockLoggerProviderFastTest.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,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); | ||
} | ||
|
||
} |
74 changes: 74 additions & 0 deletions
74
elf4j/src/test/java/io/github/vitalijr2/logging/mock/elf4j/MockLoggerProviderSlowTest.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,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"); | ||
} | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
elf4j/src/test/resources/io/github/vitalijr2/logging/mock/elf4j/elf4j.csv
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,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 |
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
Oops, something went wrong.