Skip to content

Commit

Permalink
Split project into multi module - prepare for slf4j 2.x
Browse files Browse the repository at this point in the history
- slf4j-mock-common - implementations of mocks for Mockito
- slf4j-mock-tests  - test templates for using in slf4 provider/bindings
- slf4j-mock        - static bindings for 1.7.x
  • Loading branch information
slawekjaranowski committed Sep 25, 2022
1 parent ed06d60 commit 86d6f4b
Show file tree
Hide file tree
Showing 50 changed files with 821 additions and 105 deletions.
116 changes: 47 additions & 69 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
~ limitations under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
Expand All @@ -24,9 +25,10 @@
<version>2.16.0</version>
</parent>

<artifactId>slf4j-mock</artifactId>
<artifactId>slf4j-mock-parent</artifactId>
<version>2.2.1-SNAPSHOT</version>
<name>SLF4J mock</name>
<name>SLF4J mock parent</name>
<packaging>pom</packaging>

<description>Library to easy mock request on sl4j-api</description>
<url>https://www.simplify4u.org/slf4j-mock</url>
Expand All @@ -40,7 +42,7 @@
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0</url>
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
Expand All @@ -60,6 +62,13 @@
<!-- Add yourself to list after contributions - if wish -->
</contributors>

<modules>
<module>slf4j-mock-common</module>
<module>slf4j-mock-tests</module>
<module>slf4j-mock</module>
<module>slf4j-mock-coverage-report</module>
</modules>

<scm>
<connection>scm:git:git@github.com:s4u/slf4j-mock.git</connection>
<developerConnection>${project.scm.connection}</developerConnection>
Expand All @@ -75,21 +84,18 @@
</distributionManagement>

<properties>
<slf4j.version>1.7.36</slf4j.version>
<slf4j-1.version>1.7.36</slf4j-1.version>
<junit.version>5.9.1</junit.version>
<mockito.version>4.6.1</mockito.version>
<assertj.version>3.23.1</assertj.version>
<sonar.coverage.exclusions>**/slf4jmock/SimpleLogger.java</sonar.coverage.exclusions>
<sonar.projectName>SLF4J mock</sonar.projectName>
<sonar.coverage.jacoco.xmlReportPaths>../target/site/jacoco/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>
<project.build.outputTimestamp>2022-04-20T19:28:53Z</project.build.outputTimestamp>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down Expand Up @@ -123,39 +129,6 @@
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>

<!-- testing -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
Expand Down Expand Up @@ -184,32 +157,6 @@
</dependency>
</dependencies>
</plugin>
<plugin>
<!-- copy source of SimpleLogger -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<classifier>sources</classifier>
<version>${slf4j.version}</version>
</artifactItem>
</artifactItems>
<includes>org/slf4j/impl/*.java</includes>
<excludes>**/Static*,**/SimpleLoggerFactory*</excludes>
<outputDirectory>${project.build.directory}/generated-sources/annotations</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
Expand All @@ -220,4 +167,35 @@
</plugins>
</build>

<profiles>
<profile>
<id>coverage</id>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<!-- disable default goals, execute only what need -->
<execution>
<id>default</id>
<phase>none</phase>
</execution>
<execution>
<id>jacoco-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
</executions>
<configuration>
<includes combine.children="append">
<include>org/slf4j/**</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
71 changes: 71 additions & 0 deletions slf4j-mock-common/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2022 Slawomir Jaranowski
~
~ 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="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.simplify4u</groupId>
<artifactId>slf4j-mock-parent</artifactId>
<version>2.2.1-SNAPSHOT</version>
</parent>

<artifactId>slf4j-mock-common</artifactId>
<version>2.2.1-SNAPSHOT</version>
<name>SLF4J mock commons</name>

<properties>
<maven.site.skip>true</maven.site.skip>
</properties>

<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j-1.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4j-1.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
*/
package org.simplify4u.slf4jmock;

import org.simplify4u.slf4jmock.internal.ProxyMock;
import org.slf4j.ILoggerFactory;
import org.slf4j.Logger;
import org.slf4j.spi.LoggerFactoryBinder;

import java.lang.reflect.Proxy;
import java.util.HashMap;
Expand All @@ -29,12 +29,16 @@
/**
* SLF4J Logger mock service.
*/
public class LoggerMock implements LoggerFactoryBinder {
public final class LoggerMock {

private static final ILoggerFactory loggerFactory = LoggerMock::getLoggerProxyByName;

private static final Map<String, Logger> loggers = new HashMap<>();

private LoggerMock(){
// Utility classes
}

private static Logger createNewLoggerProxy(String name) {
return (Logger) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
new Class<?>[]{Logger.class, ProxyMock.class},
Expand All @@ -50,20 +54,14 @@ private static Logger getLoggerProxyByName(String name) {
}
}

protected static ProxyMock getProxyByName(String name) {
private static ProxyMock getProxyByName(String name) {
return (ProxyMock) getLoggerProxyByName(name);
}

@Override
public ILoggerFactory getLoggerFactory() {
public static ILoggerFactory getLoggerFactory() {
return loggerFactory;
}

@Override
public String getLoggerFactoryClassStr() {
return LoggerMock.class.getName();
}

/**
* Set Mock for given Logger at current thread.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.simplify4u.slf4jmock;

import org.simplify4u.slf4jmock.internal.ProxyMock;
import org.slf4j.spi.MDCAdapter;

import java.lang.reflect.Proxy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import java.lang.reflect.Method;
import java.util.function.Supplier;

import org.simplify4u.slf4jmock.internal.ProxyMock;

class MockInvocationHandler implements InvocationHandler, ProxyMock {

private final String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
*/
package org.simplify4u.slf4jmock;

import java.util.Iterator;
import java.util.ServiceLoader;

import org.simplify4u.slf4jmock.internal.SimpleLoggerFactory;
import org.slf4j.Logger;
import org.slf4j.Marker;
import org.slf4j.impl.SimpleLoggerDelegate;

/**
* Access to SLF4J SimpleLogger by own implementation.
Expand All @@ -33,7 +36,13 @@ public class SimpleLogger implements Logger {
* @param name a name of Logger
*/
public SimpleLogger(String name) {
delegateLogger = new SimpleLoggerDelegate(name);
Iterator<SimpleLoggerFactory> delegateLoggerServiceIt =
ServiceLoader.load(SimpleLoggerFactory.class).iterator();
if (delegateLoggerServiceIt.hasNext()) {
delegateLogger = delegateLoggerServiceIt.next().createLogger(name);
} else {
throw new RuntimeException("No service for SimpleLoggerFactory");
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 Slawomir Jaranowski
* Copyright 2022 Slawomir Jaranowski
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.simplify4u.slf4jmock;
package org.simplify4u.slf4jmock.internal;

/**
* Interface to describe proxy for created Logger.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2022 Slawomir Jaranowski
*
* 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 org.simplify4u.slf4jmock.internal;

import org.slf4j.Logger;

/**
* Service for creating SimpleLogger by desired slf4j implementation.
*/
public interface SimpleLoggerFactory {
/**
* Create SimpleLogger instance.
*
* @param name a logger name
* @return instance of SimpleLogger
*/
Logger createLogger(String name);
}
Loading

0 comments on commit 86d6f4b

Please sign in to comment.