private void closeMap() { for (final Iterator<Map.Entry<String, String>> it = originalValues.entrySet().iterator(); it.hasNext(); ) { final Map.Entry<String, String> entry = it.next(); final String key = entry.getKey(); final String originalValue = entry.getValue(); if (null == originalValue) { ThreadContext.remove(key); } else { ThreadContext.put(key, originalValue); } it.remove(); } }
@Override @SuppressWarnings("unchecked") // nothing we can do about this, restricted by SLF4J API public void setContextMap(@SuppressWarnings("rawtypes") final Map map) { ThreadContext.clearMap(); for (final Map.Entry<String, String> entry : ((Map<String, String>) map).entrySet()) { ThreadContext.put(entry.getKey(), entry.getValue()); } } }
public static void testGetImmutableContextReturnsImmutableMapIfNonEmpty() { ThreadContext.clearMap(); ThreadContext.put("key", "val"); final Map<String, String> immutable = ThreadContext.getImmutableContext(); immutable.put("otherkey", "otherval"); }
@Test public void mdc() { ThreadContext.put("TestYear", "2010"); logger.debug("Debug message"); ThreadContext.clearMap(); logger.debug("Debug message"); final List<LogEvent> events = app.getEvents(); assertEquals("Incorrect number of events. Expected 2, actual " + events.size(), 2, events.size()); }
protected void sendInfoStructuredMessage() { ThreadContext.put("loginId", "JohnDoe"); ThreadContext.put("ipAddress", "192.168.0.120"); ThreadContext.put("locale", Locale.US.getDisplayName()); final StructuredDataMessage msg = new StructuredDataMessage("Transfer@18060", "Transfer Complete", "Audit"); msg.put("ToAccount", "123456"); msg.put("FromAccount", "123457"); msg.put("Amount", "200.00"); // the msg.toString() doesn't contain the parameters of the ThreadContext, so we must use the line1 string final String str = msg.asString(null, null); sentMessages.add(str); root.info(MarkerManager.getMarker("EVENT"), msg); }
@Test public void mdc() { ThreadContext.put("TestYear", "2010"); logger.debug("Debug message"); ThreadContext.clearMap(); logger.debug("Debug message"); final List<LogEvent> events = app.getEvents(); assertEventCount(events, 2); }
private void prepareThreadContext(boolean isThreadContextMapInheritable) { System.setProperty("log4j2.isThreadContextMapInheritable", Boolean.toString(isThreadContextMapInheritable)); PropertiesUtil.getProperties().reload(); ThreadContextTest.reinitThreadContext(); ThreadContext.remove("baz"); ThreadContext.put("foo", "bar"); }
@Test public void putAllWillPutAllValues() throws Exception { final String oldValue = "oldValue"; ThreadContext.put(key, oldValue); final Map<String, String> valuesToPut = new HashMap<>(); valuesToPut.put(key, value); try (final CloseableThreadContext.Instance ignored = CloseableThreadContext.putAll(valuesToPut)) { assertThat(ThreadContext.get(key), is(value)); } assertThat(ThreadContext.get(key), is(oldValue)); }
@Test public void testNoop() { ThreadContext.put("Test", "Test"); final String value = ThreadContext.get("Test"); assertNull("value was saved", value); }
@Test public void testHeaderFooterThreadContext() throws Exception { final PatternLayout layout = PatternLayout.newBuilder().withPattern("%d{UNIX} %m") .withConfiguration(ctx.getConfiguration()).withHeader("${ctx:header}").withFooter("${ctx:footer}") .build(); ThreadContext.put("header", "Hello world Header"); ThreadContext.put("footer", "Hello world Footer"); final byte[] header = layout.getHeader(); assertNotNull("No header", header); assertTrue("expected \"Hello world Header\", actual " + Strings.dquote(new String(header)), new String(header).equals(new String("Hello world Header"))); }
@Test public void shouldPreserveOldEntriesFromTheMapWhenAutoClosed() throws Exception { final String oldValue = "oldValue"; ThreadContext.put(key, oldValue); try (final CloseableThreadContext.Instance ignored = CloseableThreadContext.put(key, value)) { assertThat(ThreadContext.get(key), is(value)); } assertThat(ThreadContext.get(key), is(oldValue)); }
@Test public void testDefault() { final Map<String, String> map = new HashMap<>(); map.put(TESTKEY, TESTVAL); final StrLookup lookup = new Interpolator(new MapLookup(map)); final StrSubstitutor subst = new StrSubstitutor(lookup); ThreadContext.put(TESTKEY, TESTVAL); //String value = subst.replace("${sys:TestKey1:-${ctx:TestKey}}"); final String value = subst.replace("${sys:TestKey1:-${ctx:TestKey}}"); assertEquals("TestValue", value); } }
@Test public void ifTheSameKeyIsAddedTwiceTheOriginalShouldBeUsed() throws Exception { final String oldValue = "oldValue"; final String secondValue = "innerValue"; ThreadContext.put(key, oldValue); try (final CloseableThreadContext.Instance ignored = CloseableThreadContext.put(key, value).put(key, secondValue)) { assertThat(ThreadContext.get(key), is(secondValue)); } assertThat(ThreadContext.get(key), is(oldValue)); }
public static void testPut() { ThreadContext.clearMap(); assertNull(ThreadContext.get("testKey")); ThreadContext.put("testKey", "testValue"); assertEquals("testValue", ThreadContext.get("testKey")); } }
@Test public void testLookup() { ThreadContext.put(TESTKEY, TESTVAL); final StrLookup lookup = new ContextMapLookup(); String value = lookup.lookup(TESTKEY); assertEquals(TESTVAL, value); value = lookup.lookup("BadKey"); assertNull(value); }
@Test public void testContainsKey() { ThreadContext.clearMap(); assertFalse(ThreadContext.containsKey("testKey")); ThreadContext.put("testKey", "testValue"); assertTrue(ThreadContext.containsKey("testKey")); ThreadContext.remove("testKey"); assertFalse(ThreadContext.containsKey("testKey")); }
@Test public void testContainsKey() { ThreadContext.clearMap(); assertFalse(ThreadContext.containsKey("testKey")); ThreadContext.put("testKey", "testValue"); assertTrue(ThreadContext.containsKey("testKey")); ThreadContext.remove("testKey"); assertFalse(ThreadContext.containsKey("testKey")); }
@Test public void testRemove() { ThreadContext.clearMap(); assertNull(ThreadContext.get("testKey")); ThreadContext.put("testKey", "testValue"); assertEquals("testValue", ThreadContext.get("testKey")); ThreadContext.remove("testKey"); assertNull(ThreadContext.get("testKey")); assertTrue(ThreadContext.isEmpty()); }
@Test public void testRemove() { ThreadContext.clearMap(); assertNull(ThreadContext.get("testKey")); ThreadContext.put("testKey", "testValue"); assertEquals("testValue", ThreadContext.get("testKey")); ThreadContext.remove("testKey"); assertNull(ThreadContext.get("testKey")); assertTrue(ThreadContext.isEmpty()); }