-
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
5 changed files
with
421 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
Copyright 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. | ||
--> | ||
<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-tinylog-provider</artifactId> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-failsafe-plugin</artifactId> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
</plugin> | ||
<plugin> | ||
<artifactId>jacoco-maven-plugin</artifactId> | ||
<groupId>org.jacoco</groupId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
<dependencies> | ||
<!-- main --> | ||
<dependency> | ||
<artifactId>annotations</artifactId> | ||
<groupId>org.jetbrains</groupId> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<artifactId>mock-loggers-core</artifactId> | ||
<groupId>io.github.vitalijr2.logging</groupId> | ||
<version>1.0.0</version> | ||
</dependency> | ||
<dependency> | ||
<artifactId>mockito-core</artifactId> | ||
<groupId>org.mockito</groupId> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<artifactId>tinylog-api</artifactId> | ||
<groupId>org.tinylog</groupId> | ||
<scope>provided</scope> | ||
<version>2.7.0</version> | ||
</dependency> | ||
<!-- test --> | ||
<dependency> | ||
<artifactId>junit-jupiter-engine</artifactId> | ||
<groupId>org.junit.jupiter</groupId> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<artifactId>junit-jupiter-params</artifactId> | ||
<groupId>org.junit.jupiter</groupId> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<artifactId>mockito-junit-jupiter</artifactId> | ||
<groupId>org.mockito</groupId> | ||
<scope>provided</scope> | ||
</dependency> | ||
</dependencies> | ||
<description>Mock loggers for tinylog backed by Mockito.</description> | ||
<modelVersion>4.0.0</modelVersion> | ||
<name>Mock loggers for tinylog</name> | ||
<parent> | ||
<artifactId>mock-loggers</artifactId> | ||
<groupId>io.github.vitalijr2.logging</groupId> | ||
<version>1.0.0</version> | ||
</parent> | ||
<profiles> | ||
<profile> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-invoker-plugin</artifactId> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
<id>run-its</id> | ||
</profile> | ||
</profiles> | ||
</project> |
95 changes: 95 additions & 0 deletions
95
...g-provider/src/main/java/io/github/vitalijr2/logging/mock/tinylog/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,95 @@ | ||
/* | ||
* Copyright 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. | ||
*/ | ||
package io.github.vitalijr2.logging.mock.tinylog; | ||
|
||
import static org.mockito.Mockito.clearInvocations; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.reset; | ||
|
||
import io.github.vitalijr2.logging.mock.MockLoggerCleaner; | ||
import java.util.List; | ||
import org.tinylog.Level; | ||
import org.tinylog.format.MessageFormatter; | ||
import org.tinylog.provider.ContextProvider; | ||
import org.tinylog.provider.LoggingProvider; | ||
|
||
public class MockLoggerProvider implements LoggingProvider, MockLoggerCleaner { | ||
|
||
static final LoggingProvider MOCK_INSTANCE; | ||
|
||
static { | ||
MOCK_INSTANCE = mock(LoggingProvider.class); | ||
prepareMockInstance(); | ||
} | ||
|
||
public MockLoggerProvider() { | ||
subscribeToNotifications(); | ||
} | ||
|
||
private static void prepareMockInstance() { | ||
clearInvocations(MOCK_INSTANCE); | ||
reset(MOCK_INSTANCE); | ||
} | ||
|
||
@Override | ||
public List<String> cleanAndReset() { | ||
prepareMockInstance(); | ||
return List.of("tinylog"); | ||
} | ||
|
||
@Override | ||
public ContextProvider getContextProvider() { | ||
return MOCK_INSTANCE.getContextProvider(); | ||
} | ||
|
||
@Override | ||
public Level getMinimumLevel() { | ||
return MOCK_INSTANCE.getMinimumLevel(); | ||
} | ||
|
||
@Override | ||
public Level getMinimumLevel(String tag) { | ||
return MOCK_INSTANCE.getMinimumLevel(tag); | ||
} | ||
|
||
@Override | ||
public boolean isEnabled(int depth, String tag, Level level) { | ||
return MOCK_INSTANCE.isEnabled(depth, tag, level); | ||
} | ||
|
||
@Override | ||
public boolean isEnabled(String loggerClassName, String tag, Level level) { | ||
return MOCK_INSTANCE.isEnabled(loggerClassName, tag, level); | ||
} | ||
|
||
@Override | ||
public void log(int depth, String tag, Level level, Throwable exception, MessageFormatter formatter, Object obj, | ||
Object... arguments) { | ||
MOCK_INSTANCE.log(depth, tag, level, exception, formatter, obj, arguments); | ||
} | ||
|
||
@Override | ||
public void log(String loggerClassName, String tag, Level level, Throwable exception, MessageFormatter formatter, | ||
Object obj, Object... arguments) { | ||
MOCK_INSTANCE.log(loggerClassName, tag, level, exception, formatter, obj, arguments); | ||
} | ||
|
||
@Override | ||
public void shutdown() throws InterruptedException { | ||
MOCK_INSTANCE.shutdown(); | ||
} | ||
|
||
} |
185 changes: 185 additions & 0 deletions
185
...er/src/test/java/io/github/vitalijr2/logging/mock/tinylog/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,185 @@ | ||
package io.github.vitalijr2.logging.mock.tinylog; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.ArgumentMatchers.anyInt; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.ArgumentMatchers.isA; | ||
import static org.mockito.ArgumentMatchers.notNull; | ||
import static org.mockito.Mockito.clearInvocations; | ||
import static org.mockito.Mockito.doThrow; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.reset; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.verifyNoInteractions; | ||
import static org.mockito.Mockito.when; | ||
|
||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Tag; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.CsvSource; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
import org.tinylog.Level; | ||
import org.tinylog.format.MessageFormatter; | ||
import org.tinylog.provider.NopContextProvider; | ||
|
||
|
||
@Tag("fast") | ||
class MockLoggerProviderFastTest { | ||
|
||
private MockLoggerProvider provider; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
provider = new MockLoggerProvider(); | ||
} | ||
|
||
@AfterEach | ||
void tearDown() { | ||
clearInvocations(MockLoggerProvider.MOCK_INSTANCE); | ||
reset(MockLoggerProvider.MOCK_INSTANCE); | ||
} | ||
|
||
// Move to slow | ||
@DisplayName("Clear and reset the mock instance") | ||
@Test | ||
void cleanAndReset() { | ||
// when | ||
provider.cleanAndReset(); | ||
|
||
// then | ||
verifyNoInteractions(MockLoggerProvider.MOCK_INSTANCE); | ||
} | ||
|
||
@DisplayName("Get a context provider") | ||
@Test | ||
void getContextProvider() { | ||
// given | ||
when(MockLoggerProvider.MOCK_INSTANCE.getContextProvider()).thenReturn(new NopContextProvider()); | ||
|
||
// when and then | ||
assertNotNull(provider.getContextProvider()); | ||
|
||
verify(MockLoggerProvider.MOCK_INSTANCE).getContextProvider(); | ||
} | ||
|
||
@DisplayName("Get a minimum level") | ||
@ParameterizedTest | ||
@ValueSource(strings = {"TRACE", "DEBUG", "INFO", "WARN", "ERROR", "OFF"}) | ||
void getMinimumLevel(Level expectedlevel) { | ||
// given | ||
when(MockLoggerProvider.MOCK_INSTANCE.getMinimumLevel()).thenReturn(expectedlevel); | ||
|
||
// when | ||
var actualLevel = provider.getMinimumLevel(); | ||
|
||
// then | ||
verify(MockLoggerProvider.MOCK_INSTANCE).getMinimumLevel(); | ||
|
||
assertEquals(expectedlevel, actualLevel); | ||
} | ||
|
||
@DisplayName("Get a minimum level for a classname") | ||
@ParameterizedTest | ||
@ValueSource(strings = {"TRACE", "DEBUG", "INFO", "WARN", "ERROR", "OFF"}) | ||
void getMinimumLevelByTagWithCustomLevel(Level expectedlevel) { | ||
// given | ||
when(MockLoggerProvider.MOCK_INSTANCE.getMinimumLevel(anyString())).thenReturn(expectedlevel); | ||
|
||
// when | ||
var actualLevel = provider.getMinimumLevel("test tag"); | ||
|
||
// then | ||
verify(MockLoggerProvider.MOCK_INSTANCE).getMinimumLevel("test tag"); | ||
|
||
assertEquals(expectedlevel, actualLevel); | ||
} | ||
|
||
@DisplayName("Is it enabled?") | ||
@ParameterizedTest | ||
@CsvSource(value = {"TRACE,true", "TRACE,false", "DEBUG,true", "DEBUG,false", "INFO,true", "INFO,false", "WARN,true", | ||
"WARN,false", "ERROR,true", "ERROR,false", "OFF,true", "OFF,false"}) | ||
void isEnabled(Level level, boolean expectedResult) { | ||
// given | ||
when(MockLoggerProvider.MOCK_INSTANCE.isEnabled(anyInt(), anyString(), isA(Level.class))).thenReturn( | ||
expectedResult); | ||
|
||
// when | ||
var actualResult = provider.isEnabled(123, "test tag", level); | ||
|
||
// then | ||
verify(MockLoggerProvider.MOCK_INSTANCE).isEnabled(123, "test tag", level); | ||
|
||
assertEquals(expectedResult, actualResult); | ||
} | ||
|
||
@DisplayName("Is it enabled for a classname?") | ||
@ParameterizedTest | ||
@CsvSource(value = {"TRACE,true", "TRACE,false", "DEBUG,true", "DEBUG,false", "INFO,true", "INFO,false", "WARN,true", | ||
"WARN,false", "ERROR,true", "ERROR,false", "OFF,true", "OFF,false"}, nullValues = "N/A") | ||
void isEnabledForClassname(Level level, boolean expectedResult) { | ||
// given | ||
when(MockLoggerProvider.MOCK_INSTANCE.isEnabled(anyString(), anyString(), isA(Level.class))).thenReturn( | ||
expectedResult); | ||
|
||
// when | ||
var actualResult = provider.isEnabled("test.class", "test tag", level); | ||
|
||
// then | ||
verify(MockLoggerProvider.MOCK_INSTANCE).isEnabled("test.class", "test tag", level); | ||
|
||
assertEquals(expectedResult, actualResult); | ||
} | ||
|
||
@DisplayName("Log") | ||
@Test | ||
void log() { | ||
// when | ||
provider.log(123, "test tag", Level.TRACE, new Throwable("test throwable"), mock(MessageFormatter.class), this, "a", | ||
"b", "c"); | ||
|
||
// then | ||
verify(MockLoggerProvider.MOCK_INSTANCE).log(eq(123), eq("test tag"), eq(Level.TRACE), isA(Throwable.class), | ||
isA(MessageFormatter.class), notNull(), eq("a"), eq("b"), eq("c")); | ||
} | ||
|
||
@DisplayName("Log for a classname") | ||
@Test | ||
void logForClassname() { | ||
// when | ||
provider.log("test.class", "test tag", Level.TRACE, new Throwable("test throwable"), mock(MessageFormatter.class), | ||
this, "a", "b", "c"); | ||
|
||
// then | ||
verify(MockLoggerProvider.MOCK_INSTANCE).log(eq("test.class"), eq("test tag"), eq(Level.TRACE), | ||
isA(Throwable.class), isA(MessageFormatter.class), notNull(), eq("a"), eq("b"), eq("c")); | ||
} | ||
|
||
@DisplayName("Shutdown") | ||
@Test | ||
void shutdown() throws InterruptedException { | ||
// when | ||
provider.shutdown(); | ||
|
||
// then | ||
verify(MockLoggerProvider.MOCK_INSTANCE).shutdown(); | ||
} | ||
|
||
@DisplayName("Shutdown with an exception") | ||
@Test | ||
void shutdownWithException() throws InterruptedException { | ||
// given | ||
doThrow(new InterruptedException("test")).when(MockLoggerProvider.MOCK_INSTANCE).shutdown(); | ||
|
||
// when and then | ||
var exception = assertThrows(InterruptedException.class, () -> provider.shutdown()); | ||
|
||
assertEquals("test", exception.getMessage()); | ||
} | ||
|
||
} |
Oops, something went wrong.