From 62d6497f6ab82b450d24f13d95bd6e7363a5ab1f Mon Sep 17 00:00:00 2001 From: Ceki Gulcu Date: Fri, 2 Jul 2021 16:23:17 +0200 Subject: [PATCH] fix unit tests Signed-off-by: Ceki Gulcu --- .../slf4j/helpers/BasicMDCAdapterTest.java | 144 +--------------- .../org/slf4j/helpers/MDCAdapterTestBase.java | 154 ++++++++++++++++++ .../slf4j/log4j12/Log4jMDCAdapterTest.java | 19 ++- 3 files changed, 172 insertions(+), 145 deletions(-) mode change 100644 => 100755 slf4j-api/src/test/java/org/slf4j/helpers/BasicMDCAdapterTest.java create mode 100644 slf4j-api/src/test/java/org/slf4j/helpers/MDCAdapterTestBase.java diff --git a/slf4j-api/src/test/java/org/slf4j/helpers/BasicMDCAdapterTest.java b/slf4j-api/src/test/java/org/slf4j/helpers/BasicMDCAdapterTest.java old mode 100644 new mode 100755 index 5f2c4b9f..f73a6e06 --- a/slf4j-api/src/test/java/org/slf4j/helpers/BasicMDCAdapterTest.java +++ b/slf4j-api/src/test/java/org/slf4j/helpers/BasicMDCAdapterTest.java @@ -1,73 +1,11 @@ -/** - * 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 BasicMDCAdapterTest { - 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"); - } +public class BasicMDCAdapterTest extends MDCAdapterTestBase { @Test public void testClearingMDC() { @@ -76,84 +14,4 @@ public class BasicMDCAdapterTest { mdc.clear(); assertNull(mdc.getCopyOfContextMap()); } - - @Test - public void testGetCopyOfContextMapFromMDC() { - mdc.put("testKey", "testValue"); - Map 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() : ""; - } - } } diff --git a/slf4j-api/src/test/java/org/slf4j/helpers/MDCAdapterTestBase.java b/slf4j-api/src/test/java/org/slf4j/helpers/MDCAdapterTestBase.java new file mode 100644 index 00000000..fe6b7169 --- /dev/null +++ b/slf4j-api/src/test/java/org/slf4j/helpers/MDCAdapterTestBase.java @@ -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 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() : ""; + } + } +} diff --git a/slf4j-log4j12/src/test/java/org/slf4j/log4j12/Log4jMDCAdapterTest.java b/slf4j-log4j12/src/test/java/org/slf4j/log4j12/Log4jMDCAdapterTest.java index 7d2fdbaa..8b92208f 100755 --- a/slf4j-log4j12/src/test/java/org/slf4j/log4j12/Log4jMDCAdapterTest.java +++ b/slf4j-log4j12/src/test/java/org/slf4j/log4j12/Log4jMDCAdapterTest.java @@ -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()); + } + + }