Skip to content

Commit

Permalink
Fix NPE caused by MDCContextMap
Browse files Browse the repository at this point in the history
  • Loading branch information
ppkarwasz committed Dec 20, 2023
1 parent bd08644 commit 611f417
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 3 deletions.
5 changes: 5 additions & 0 deletions log4j-to-slf4j/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@
<artifactId>log4j-api-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.logging.slf4j;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.logging.log4j.spi.CleanableThreadContextMap;
Expand Down Expand Up @@ -75,13 +76,12 @@ public boolean containsKey(final String key) {
}

@Override
@SuppressWarnings("unchecked") // nothing we can do about this, restricted by SLF4J API
public Map<String, String> getCopy() {
return MDC.getCopyOfContextMap();
final Map<String, String> contextMap = MDC.getCopyOfContextMap();
return contextMap != null ? contextMap : new HashMap<>();
}

@Override
@SuppressWarnings("unchecked") // nothing we can do about this, restricted by SLF4J API
public Map<String, String> getImmutableMapOrNull() {
return MDC.getCopyOfContextMap();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you 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.apache.logging.slf4j;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

import org.apache.logging.log4j.spi.ThreadContextMap;
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.Issue;
import org.slf4j.MDCTestHelper;
import org.slf4j.spi.MDCAdapter;

class MDCContextMapTest {

@Test
@Issue("https://github.com/apache/logging-log4j2/issues/1426")
void nonNullGetCopy() {
final ThreadContextMap contextMap = new MDCContextMap();
final MDCAdapter mockAdapter = mock(MDCAdapter.class);
when(mockAdapter.getCopyOfContextMap()).thenReturn(null);
final MDCAdapter adapter = MDCTestHelper.replaceMDCAdapter(mockAdapter);
try {
assertThat(contextMap.getImmutableMapOrNull()).isNull();
assertThat(contextMap.getCopy()).isNotNull();
verify(mockAdapter, times(2)).getCopyOfContextMap();
verifyNoMoreInteractions(mockAdapter);
} finally {
MDCTestHelper.replaceMDCAdapter(adapter);
}
}
}
28 changes: 28 additions & 0 deletions log4j-to-slf4j/src/test/java/org/slf4j/MDCTestHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you 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.slf4j;

import org.slf4j.spi.MDCAdapter;

public class MDCTestHelper {

public static MDCAdapter replaceMDCAdapter(final MDCAdapter adapter) {
final MDCAdapter old = MDC.mdcAdapter;
MDC.mdcAdapter = adapter;
return old;
}
}
10 changes: 10 additions & 0 deletions src/changelog/.2.x.x/fix_NPE_in_closeable_thread_context.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://logging.apache.org/log4j/changelog"
xsi:schemaLocation="http://logging.apache.org/log4j/changelog https://logging.apache.org/log4j/changelog-0.1.2.xsd"
type="fixed">
<issue id="1426" link="https://github.com/apache/logging-log4j2/pull/1426"/>
<description format="asciidoc">
Fix NPE in `CloseableThreadContext`.
</description>
</entry>

0 comments on commit 611f417

Please sign in to comment.