@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()); }
/** * Clears the context map and stack. */ public static void clearAll() { clearMap(); clearStack(); }
public static void testGetImmutableContextReturnsImmutableMapIfNonEmpty() { ThreadContext.clearMap(); ThreadContext.put("key", "val"); final Map<String, String> immutable = ThreadContext.getImmutableContext(); immutable.put("otherkey", "otherval"); }
@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()); } } }
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(); } }
@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 closeIsIdempotent() throws Exception { final String originalMapValue = "map to keep"; final String originalStackValue = "stack to keep"; ThreadContext.put(key, originalMapValue); ThreadContext.push(originalStackValue); final String newMapValue = "temp map value"; final String newStackValue = "temp stack to keep"; final CloseableThreadContext.Instance ctc = CloseableThreadContext.push(newStackValue).put(key, newMapValue); ctc.close(); assertThat(ThreadContext.get(key), is(originalMapValue)); assertThat(ThreadContext.peek(), is(originalStackValue)); ctc.close(); assertThat(ThreadContext.get(key), is(originalMapValue)); assertThat(ThreadContext.peek(), is(originalStackValue)); }
@Test public void testRemoveAll() { ThreadContext.clearMap(); ThreadContext.put("testKey1", "testValue1"); ThreadContext.put("testKey2", "testValue2"); assertEquals("testValue1", ThreadContext.get("testKey1")); assertEquals("testValue2", ThreadContext.get("testKey2")); assertFalse(ThreadContext.isEmpty()); ThreadContext.removeAll(Arrays.asList("testKey1", "testKey2")); assertNull(ThreadContext.get("testKey1")); assertNull(ThreadContext.get("testKey2")); assertTrue(ThreadContext.isEmpty()); }
@Test public void testFilter() { ThreadContext.put("userid", "testuser"); ThreadContext.put("organization", "Apache"); final KeyValuePair[] pairs = new KeyValuePair[] { new KeyValuePair("userid", "JohnDoe"), new KeyValuePair("organization", "Apache")}; assertTrue(filter.isStarted()); assertSame(Filter.Result.DENY, filter.filter(null, Level.DEBUG, null, (Object) null, (Throwable) null)); ThreadContext.remove("userid"); assertSame(Filter.Result.DENY, filter.filter(null, Level.DEBUG, null, (Object) null, (Throwable) null)); ThreadContext.put("userid", "JohnDoe"); assertSame(Filter.Result.NEUTRAL, filter.filter(null, Level.ERROR, null, (Object) null, (Throwable) null)); ThreadContext.put("organization", "ASF"); assertSame(Filter.Result.DENY, filter.filter(null, Level.DEBUG, null, (Object) null, (Throwable) null)); ThreadContext.clearMap(); filter = ThreadContextMapFilter.createFilter(pairs, "or", null, null); filter.start(); assertTrue(filter.isStarted()); ThreadContext.put("userid", "testuser"); ThreadContext.put("organization", "Apache"); assertSame(Filter.Result.NEUTRAL, filter.filter(null, Level.DEBUG, null, (Object) null, (Throwable) null)); ThreadContext.put("organization", "ASF"); assertSame(Filter.Result.DENY, filter.filter(null, Level.DEBUG, null, (Object) null, (Throwable) null)); ThreadContext.remove("organization"); assertSame(Filter.Result.DENY, filter.filter(null, Level.DEBUG, null, (Object) null, (Throwable) null)); final KeyValuePair[] single = new KeyValuePair[] {new KeyValuePair("userid", "testuser")}; assertTrue(filter.isStarted()); assertSame(Filter.Result.NEUTRAL, filter.filter(null, Level.DEBUG, null, (Object) null, (Throwable) null));
@Test public void testAsyncLogWritesToLog() throws Exception { final File file = new File("target", "AsyncLoggerTest.log"); // System.out.println(f.getAbsolutePath()); file.delete(); ThreadContext.push("stackvalue"); ThreadContext.put("KEY", "mapvalue"); final Logger log = LogManager.getLogger("com.foo.Bar"); final String msg = "Async logger msg"; log.info(msg, new InternalError("this is not a real error")); CoreLoggerContexts.stopLoggerContext(false, file); // stop async thread final BufferedReader reader = new BufferedReader(new FileReader(file)); final String line1 = reader.readLine(); reader.close(); file.delete(); assertNotNull("line1", line1); assertTrue("line1 correct", line1.contains(msg)); assertTrue("ThreadContext.map", line1.contains("mapvalue")); assertTrue("ThreadContext.stack", line1.contains("stackvalue")); }
@Test public void pushAllWillPushAllValues() throws Exception { ThreadContext.push(key); final List<String> messages = ThreadContext.getImmutableStack().asList(); ThreadContext.pop(); try (final CloseableThreadContext.Instance ignored = CloseableThreadContext.pushAll(messages)) { assertThat(ThreadContext.peek(), is(key)); } assertThat(ThreadContext.peek(), is("")); }
@Test public void shouldAddEntriesToBothStackAndMap() throws Exception { final String stackValue = "something"; try (final CloseableThreadContext.Instance ignored = CloseableThreadContext.put(key, value).push(stackValue)) { assertThat(ThreadContext.get(key), is(value)); assertThat(ThreadContext.peek(), is(stackValue)); } assertThat(ThreadContext.containsKey(key), is(false)); assertThat(ThreadContext.peek(), is("")); }
context.put("user", "pass"); ThreadContext.push("message1"); ThreadContext.push("stack2"); final ThreadContext.ContextStack stack = ThreadContext.getImmutableStack(); ThreadContext.clearStack();
@Override public void removeMdc(String key) { ThreadContext.remove(key); }
private void closeStack() { for (int i = 0; i < pushCount; i++) { ThreadContext.pop(); } pushCount = 0; } }