From 0497273648e77c58651fa8b3fe7fd1e82da8594a Mon Sep 17 00:00:00 2001 From: Ceki Gulcu Date: Thu, 7 Dec 2023 11:59:09 +0100 Subject: [PATCH] add more usage examples Signed-off-by: Ceki Gulcu --- .../test/java/org/slf4j/MDCHelperTest.java | 63 ++++++++++++++++++- 1 file changed, 60 insertions(+), 3 deletions(-) diff --git a/slf4j-api/src/test/java/org/slf4j/MDCHelperTest.java b/slf4j-api/src/test/java/org/slf4j/MDCHelperTest.java index 4dc166272..2d14b13f1 100644 --- a/slf4j-api/src/test/java/org/slf4j/MDCHelperTest.java +++ b/slf4j-api/src/test/java/org/slf4j/MDCHelperTest.java @@ -1,15 +1,72 @@ +/** + * Copyright (c) 2004-2023 QOS.ch + * 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; import org.junit.Test; +import org.slf4j.helpers.BasicMDCAdapter; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; public class MDCHelperTest { + BasicMDCAdapter mdcAdapter = new BasicMDCAdapter(); + MDCHelper mdch = new MDCHelper(mdcAdapter); + + String k0 = "k0"; + String v0 = "v0"; + String kUnused = "kUnused"; + + void throwRuntimeException() { + throw new RuntimeException(); + } + @Test public void smoke() { - MDCHelper mdch = new MDCHelper(); - - //mdch.addKey() + try { + mdch.put(k0, v0); + assertEquals(v0, mdcAdapter.get(k0)); + assertNull(mdcAdapter.get(kUnused)); + throwRuntimeException(); + } catch (RuntimeException e) { + assertEquals(v0, mdcAdapter.get(k0)); + } finally { + mdch.removeSet(); + } + assertNull(mdcAdapter.get(k0)); + } + @Test + public void empty() { + try { + assertNull(mdcAdapter.get(kUnused)); + } finally { + mdch.removeSet(); + } + assertNull(mdcAdapter.get(k0)); } }