Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Ceki Gulcu <ceki@qos.ch>
- Loading branch information
Showing
3 changed files
with
172 additions
and
145 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
154 changes: 154 additions & 0 deletions
154
slf4j-api/src/test/java/org/slf4j/helpers/MDCAdapterTestBase.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,154 @@ | ||
/** | ||
* Copyright (c) 2004-2013 QOS.ch, Copyright (C) 2015 Google Inc. | ||
* All rights reserved. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining | ||
* a copy of this software and associated documentation files (the | ||
* "Software"), to deal in the Software without restriction, including | ||
* without limitation the rights to use, copy, modify, merge, publish, | ||
* distribute, sublicense, and/or sell copies of the Software, and to | ||
* permit persons to whom the Software is furnished to do so, subject to | ||
* the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be | ||
* included in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package org.slf4j.helpers; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertNull; | ||
import static org.junit.Assert.fail; | ||
|
||
import java.lang.Thread.UncaughtExceptionHandler; | ||
import java.util.Map; | ||
|
||
import org.junit.After; | ||
import org.junit.Test; | ||
import org.slf4j.spi.MDCAdapter; | ||
|
||
/** | ||
* Tests for {@link BasicMDCAdapter} | ||
* | ||
* @author Lukasz Cwik | ||
*/ | ||
public class MDCAdapterTestBase { | ||
|
||
protected MDCAdapter mdc = instantiateMDC(); | ||
|
||
|
||
|
||
protected MDCAdapter instantiateMDC() { | ||
return new BasicMDCAdapter(); | ||
} | ||
|
||
// leave MDC clean | ||
@After | ||
public void tearDown() throws Exception { | ||
mdc.clear(); | ||
} | ||
|
||
@Test | ||
public void testSettingAndGettingWithMDC() { | ||
assertNull(mdc.get("testKey")); | ||
mdc.put("testKey", "testValue"); | ||
assertEquals(mdc.get("testKey"), "testValue"); | ||
} | ||
|
||
@Test | ||
public void testOverwritingAKeyInMDC() { | ||
assertNull(mdc.get("testKey")); | ||
mdc.put("testKey", "testValue"); | ||
mdc.put("testKey", "differentTestValue"); | ||
assertEquals(mdc.get("testKey"), "differentTestValue"); | ||
} | ||
|
||
@Test | ||
public void testGetCopyOfContextMapFromMDC() { | ||
mdc.put("testKey", "testValue"); | ||
Map<String, String> copy = mdc.getCopyOfContextMap(); | ||
mdc.put("anotherTestKey", "anotherTestValue"); | ||
assertFalse(copy.size() == mdc.getCopyOfContextMap().size()); | ||
} | ||
|
||
@Test | ||
public void testMDCInheritsValuesFromParentThread() throws Exception { | ||
mdc.put("parentKey", "parentValue"); | ||
runAndWait(new Runnable() { | ||
public void run() { | ||
mdc.put("childKey", "childValue"); | ||
assertEquals("parentValue", mdc.get("parentKey")); | ||
} | ||
}); | ||
} | ||
|
||
@Test | ||
public void testMDCDoesntGetValuesFromChildThread() throws Exception { | ||
mdc.put("parentKey", "parentValue"); | ||
runAndWait(new Runnable() { | ||
public void run() { | ||
mdc.put("childKey", "childValue"); | ||
} | ||
}); | ||
assertEquals("parentValue", mdc.get("parentKey")); | ||
assertNull(mdc.get("childKey")); | ||
} | ||
|
||
|
||
@Test | ||
public void testInvokingSetContextMap_WithANullMap_SLF4J_414() { | ||
mdc.setContextMap(null); | ||
} | ||
|
||
@Test | ||
public void testMDCChildThreadCanOverwriteParentThread() throws Exception { | ||
mdc.put("sharedKey", "parentValue"); | ||
runAndWait(new Runnable() { | ||
public void run() { | ||
assertEquals("parentValue", mdc.get("sharedKey")); | ||
mdc.put("sharedKey", "childValue"); | ||
assertEquals("childValue", mdc.get("sharedKey")); | ||
} | ||
}); | ||
assertEquals("parentValue", mdc.get("sharedKey")); | ||
} | ||
|
||
private void runAndWait(Runnable runnable) throws Exception { | ||
RecordingExceptionHandler handler = new RecordingExceptionHandler(); | ||
Thread thread = new Thread(runnable); | ||
thread.setUncaughtExceptionHandler(handler); | ||
thread.start(); | ||
try { | ||
thread.join(); | ||
} catch (Throwable t) { | ||
fail("Unexpected failure in child thread:" + t.getMessage()); | ||
} | ||
assertFalse(handler.getMessage(), handler.hadException()); | ||
} | ||
|
||
/** A {@link UncaughtExceptionHandler} that records whether the thread threw an exception. */ | ||
private static class RecordingExceptionHandler implements UncaughtExceptionHandler { | ||
private Throwable exception; | ||
|
||
public void uncaughtException(Thread t, Throwable e) { | ||
exception = e; | ||
} | ||
|
||
boolean hadException() { | ||
return exception != null; | ||
} | ||
|
||
String getMessage() { | ||
return exception != null ? exception.getMessage() : ""; | ||
} | ||
} | ||
} |
19 changes: 17 additions & 2 deletions
19
slf4j-log4j12/src/test/java/org/slf4j/log4j12/Log4jMDCAdapterTest.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 |
---|---|---|
@@ -1,13 +1,28 @@ | ||
package org.slf4j.log4j12; | ||
|
||
import org.slf4j.helpers.BasicMDCAdapterTest; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertNull; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import org.junit.Test; | ||
import org.slf4j.helpers.MDCAdapterTestBase; | ||
import org.slf4j.spi.MDCAdapter; | ||
|
||
public class Log4jMDCAdapterTest extends BasicMDCAdapterTest { | ||
public class Log4jMDCAdapterTest extends MDCAdapterTestBase { | ||
|
||
protected MDCAdapter instantiateMDC() { | ||
return new Log4jMDCAdapter(); | ||
} | ||
|
||
|
||
@Test | ||
public void testClearingMDC() { | ||
mdc.put("testKey", "testValue"); | ||
assertFalse(mdc.getCopyOfContextMap().isEmpty()); | ||
mdc.clear(); | ||
assertTrue(mdc.getCopyOfContextMap().isEmpty()); | ||
} | ||
|
||
|
||
|
||
} |