Skip to content

Commit

Permalink
fixed unit tests relying on old SLF4J "Binder" mechanism
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Jäckle <thomas.jaeckle@beyonnex.io>
  • Loading branch information
thjaeckle committed Nov 30, 2023
1 parent 268c979 commit c3b5420
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public void onPut(final String key, final String value) {
allPutEntries.add(entry(key, value));
}

@Override
public void onPushByKey(final String key, final String value) {
super.onPushByKey(key, value);
}

@Override
public void onRemove(final String key) {
allRemovedKeys.add(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.assertj.core.api.JUnitSoftAssertions;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
Expand All @@ -28,6 +29,7 @@
import org.mockito.junit.MockitoJUnitRunner;
import org.slf4j.Logger;
import org.slf4j.impl.ObservableMdcAdapter;
import org.slf4j.impl.StaticMDCServiceProvider;

/**
* Unit test for {@link DefaultAutoCloseableSlf4jLogger}.
Expand All @@ -49,6 +51,11 @@ public final class DefaultAutoCloseableSlf4jLoggerTest {

private CapturingMdcAdapterObserver mdcObserver;

@BeforeClass
public static void configureProvider() {
System.setProperty("slf4j.provider", StaticMDCServiceProvider.class.getName());
}

@Before
public void setUp() {
mdcObserver = new CapturingMdcAdapterObserver();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.assertj.core.api.JUnitSoftAssertions;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
Expand All @@ -28,6 +29,7 @@
import org.mockito.junit.MockitoJUnitRunner;
import org.slf4j.Logger;
import org.slf4j.impl.ObservableMdcAdapter;
import org.slf4j.impl.StaticMDCServiceProvider;

/**
* Unit test for {@link DefaultDittoLogger}.
Expand All @@ -49,6 +51,11 @@ public final class DefaultDittoLoggerTest {

private CapturingMdcAdapterObserver mdcObserver;

@BeforeClass
public static void configureProvider() {
System.setProperty("slf4j.provider", StaticMDCServiceProvider.class.getName());
}

@Before
public void setUp() {
mdcObserver = new CapturingMdcAdapterObserver();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.assertj.core.data.MapEntry;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -37,6 +38,7 @@
import org.mockito.junit.MockitoJUnitRunner;
import org.slf4j.Logger;
import org.slf4j.impl.ObservableMdcAdapter;
import org.slf4j.impl.StaticMDCServiceProvider;

/**
* Unit test for {@link ImmutableDittoLogger}.
Expand All @@ -58,6 +60,11 @@ public final class ImmutableDittoLoggerTest {

private CapturingMdcAdapterObserver mdcObserver;

@BeforeClass
public static void configureProvider() {
System.setProperty("slf4j.provider", StaticMDCServiceProvider.class.getName());
}

@Before
public void setUp() {
mdcObserver = new CapturingMdcAdapterObserver();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,25 @@ public void setContextMap(final Map<String, String> contextMap) {

@Override
public void pushByKey(final String key, final String value) {
notifyAllObservers(observer -> observer.onPushByKey(key, value));
basicMdcAdapter.pushByKey(key, value);
}

@Override
public String popByKey(final String key) {
notifyAllObservers(observer -> observer.onPopByKey(key));
return basicMdcAdapter.popByKey(key);
}

@Override
public Deque<String> getCopyOfDequeByKey(final String key) {
notifyAllObservers(observer -> observer.onGetCopyOfDequeByKey(key));
return basicMdcAdapter.getCopyOfDequeByKey(key);
}

@Override
public void clearDequeByKey(final String key) {
notifyAllObservers(observer -> observer.onClearDequeByKey(key));
basicMdcAdapter.clearDequeByKey(key);
}

Expand All @@ -142,6 +146,13 @@ public interface MdcAdapterObserver {

void onSetContextMap(Map<String, String> contextMap);

void onPushByKey(String key, String value);

void onPopByKey(String key);

void onGetCopyOfDequeByKey(String key);

void onClearDequeByKey(String key);
}

/**
Expand Down Expand Up @@ -187,6 +198,25 @@ public void onSetContextMap(final Map<String, String> contextMap) {
// Does nothing by default.
}

@Override
public void onPushByKey(final String key, final String value) {
// Does nothing by default.
}

@Override
public void onPopByKey(final String key) {
// Does nothing by default.
}

@Override
public void onGetCopyOfDequeByKey(final String key) {
// Does nothing by default.
}

@Override
public void onClearDequeByKey(final String key) {
// Does nothing by default.
}
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.slf4j.impl;

import javax.annotation.concurrent.Immutable;

import org.slf4j.ILoggerFactory;
import org.slf4j.IMarkerFactory;
import org.slf4j.helpers.BasicMarkerFactory;
import org.slf4j.helpers.NOPLoggerFactory;
import org.slf4j.helpers.NOP_FallbackServiceProvider;
import org.slf4j.spi.MDCAdapter;
import org.slf4j.spi.SLF4JServiceProvider;

/**
* The purpose of this class is to use {@link ObservableMdcAdapter} as MDC adapter for logging.
*
* @since 1.3.0
*/
@Immutable
public final class StaticMDCServiceProvider implements SLF4JServiceProvider {

private final ILoggerFactory loggerFactory = new NOPLoggerFactory();
private final IMarkerFactory markerFactory = new BasicMarkerFactory();

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

@Override
public IMarkerFactory getMarkerFactory() {
return markerFactory;
}

@Override
public MDCAdapter getMDCAdapter() {
return ObservableMdcAdapter.getInstance();
}

@Override
public String getRequestedApiVersion() {
return NOP_FallbackServiceProvider.REQUESTED_API_VERSION;
}

@Override
public void initialize() {
// already initialized
}
}

0 comments on commit c3b5420

Please sign in to comment.