Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ public void clear() {

@Override
public boolean containsKey(final String key) {
return MDC.getCopyOfContextMap().containsKey(key);
Map<String, String> map = MDC.getCopyOfContextMap();
return map != null && map.containsKey(key);
}

@Override
Expand All @@ -88,7 +89,8 @@ public Map<String, String> getImmutableMapOrNull() {

@Override
public boolean isEmpty() {
return MDC.getCopyOfContextMap().isEmpty();
Map<String, String> map = MDC.getCopyOfContextMap();
return map == null || map.isEmpty();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.slf4j.MDC;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
Expand Down Expand Up @@ -165,5 +166,43 @@ public void mdc() {
assertThat(list.strList, hasSize(2));
assertTrue("Incorrect year", list.strList.get(0).startsWith("2010"));
}
}

@Test
public void mdcNullBackedIsEmpty() {
assertNull("Setup wrong", MDC.getCopyOfContextMap());
assertTrue(ThreadContext.isEmpty());
}

@Test
public void mdcNullBackedContainsKey() {
assertNull("Setup wrong", MDC.getCopyOfContextMap());
assertFalse(ThreadContext.containsKey("something"));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would separate these into two tests, one for each API.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An edge case worth testing IMO is getting a null key.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the feedback! I split the tests and added some tests concerning null keys.


@Test
public void mdcNullBackedContainsNullKey() {
assertNull("Setup wrong", MDC.getCopyOfContextMap());
assertFalse(ThreadContext.containsKey(null));
}

@Test
public void mdcContainsNullKey() {
try {
ThreadContext.put("some", "thing");
assertNotNull("Setup wrong", MDC.getCopyOfContextMap());
assertFalse(ThreadContext.containsKey(null));
} finally {
ThreadContext.clearMap();
}
}

@Test
public void mdcCannotContainNullKey() {
try {
ThreadContext.put(null, "something");
fail("should throw");
} catch (IllegalArgumentException | NullPointerException e) {
// expected
}
}
}