/** * @return */ private DefaultThreadContextMap createMap() { final DefaultThreadContextMap map = new DefaultThreadContextMap(true); assertTrue(map.isEmpty()); map.put("key", "value"); map.put("key2", "value2"); assertEquals("value", map.get("key")); assertEquals("value2", map.get("key2")); return map; }
public DefaultThreadContextMap(final boolean useMap) { this.useMap = useMap; this.localMap = createThreadLocalMap(useMap); }
@Override public Map<String, String> toMap() { return getCopy(); }
@Before public void before() { // clear the thread-local map new DefaultThreadContextMap(true).clear(); }
@Test public void testToStringShowsMapContext() { final DefaultThreadContextMap map = new DefaultThreadContextMap(true); assertEquals("{}", map.toString()); map.put("key1", "value1"); assertEquals("{key1=value1}", map.toString()); map.remove("key1"); map.put("key2", "value2"); assertEquals("{key2=value2}", map.toString()); }
@Test public void testGetCopyReturnsMutableCopy() { final DefaultThreadContextMap map = new DefaultThreadContextMap(true); map.put("key1", "value1"); assertFalse(map.isEmpty()); final Map<String, String> copy = map.getCopy(); assertEquals("value1", copy.get("key1")); // copy has values too copy.put("key", "value"); // copy is mutable assertEquals("value", copy.get("key")); // thread context map not affected assertFalse(map.containsKey("key")); // clearing context map does not affect copy map.clear(); assertTrue(map.isEmpty()); assertFalse(copy.isEmpty()); }
@Test public void testPutAll() { final DefaultThreadContextMap map = new DefaultThreadContextMap(true); assertTrue(map.isEmpty()); assertFalse(map.containsKey("key")); final int mapSize = 10; final Map<String, String> newMap = new HashMap<>(mapSize); for (int i = 1; i <= mapSize; i++) { newMap.put("key" + i, "value" + i); } map.putAll(newMap); assertFalse(map.isEmpty()); for (int i = 1; i <= mapSize; i++) { assertTrue(map.containsKey("key" + i)); assertEquals("value" + i, map.get("key" + i)); } }
@Test(expected = UnsupportedOperationException.class) public void testGetImmutableMapReturnsImmutableMapIfNonEmpty() { final DefaultThreadContextMap map = new DefaultThreadContextMap(true); map.put("key1", "value1"); assertFalse(map.isEmpty()); final Map<String, String> immutable = map.getImmutableMapOrNull(); assertEquals("value1", immutable.get("key1")); // copy has values too // immutable immutable.put("key", "value"); // error }
@Test public void testGetCopyReturnsMutableMap() { final DefaultThreadContextMap map = new DefaultThreadContextMap(true); assertTrue(map.isEmpty()); final Map<String, String> copy = map.getCopy(); assertTrue(copy.isEmpty()); copy.put("key", "value"); // mutable assertEquals("value", copy.get("key")); // thread context map not affected assertTrue(map.isEmpty()); }
@Test public void testGetImmutableMapReturnsNullIfEmpty() { final DefaultThreadContextMap map = new DefaultThreadContextMap(true); assertTrue(map.isEmpty()); assertNull(map.getImmutableMapOrNull()); }
private static ThreadContextMap createDefaultThreadContextMap() { if (Constants.ENABLE_THREADLOCALS) { if (GcFreeThreadContextKey) { return new GarbageFreeSortedArrayThreadContextMap(); } return new CopyOnWriteSortedArrayThreadContextMap(); } return new DefaultThreadContextMap(true); } }
/** * Puts all given context map entries into the current thread's * context map. * * <p>If the current thread does not have a context map it is * created as a side effect.</p> * @param m The map. * @since 2.7 */ public static void putAll(final Map<String, String> m) { if (contextMap instanceof ThreadContextMap2) { ((ThreadContextMap2) contextMap).putAll(m); } else if (contextMap instanceof DefaultThreadContextMap) { ((DefaultThreadContextMap) contextMap).putAll(m); } else { for (final Map.Entry<String, String> entry: m.entrySet()) { contextMap.put(entry.getKey(), entry.getValue()); } } }
@Test public void testClear() { final DefaultThreadContextMap map = createMap(); map.clear(); assertTrue(map.isEmpty()); assertFalse(map.containsKey("key")); assertFalse(map.containsKey("key2")); }
/** * Test method for * {@link org.apache.logging.log4j.spi.DefaultThreadContextMap#remove(java.lang.String)} * . */ @Test public void testRemove() { final DefaultThreadContextMap map = createMap(); assertEquals("value", map.get("key")); assertEquals("value2", map.get("key2")); map.remove("key"); assertFalse(map.containsKey("key")); assertEquals("value2", map.get("key2")); }
/** * Removes the context values identified by the <code>keys</code> parameter. * * @param keys The keys to remove. * * @since 2.8 */ public static void removeAll(final Iterable<String> keys) { if (contextMap instanceof CleanableThreadContextMap) { ((CleanableThreadContextMap) contextMap).removeAll(keys); } else if (contextMap instanceof DefaultThreadContextMap) { ((DefaultThreadContextMap) contextMap).removeAll(keys); } else { for (final String key : keys) { contextMap.remove(key); } } }
@Test public void testGetImmutableMapCopyNotAffectdByContextMapChanges() { final DefaultThreadContextMap map = new DefaultThreadContextMap(true); map.put("key1", "value1"); assertFalse(map.isEmpty()); final Map<String, String> immutable = map.getImmutableMapOrNull(); assertEquals("value1", immutable.get("key1")); // copy has values too // clearing context map does not affect copy map.clear(); assertTrue(map.isEmpty()); assertFalse(immutable.isEmpty()); }
private static ThreadContextMap createDefaultThreadContextMap() { if (Constants.ENABLE_THREADLOCALS) { if (PropertiesUtil.getProperties().getBooleanProperty(GC_FREE_THREAD_CONTEXT_KEY)) { return new GarbageFreeSortedArrayThreadContextMap(); } return new CopyOnWriteSortedArrayThreadContextMap(); } return new DefaultThreadContextMap(true); } }
/** * Puts all given context map entries into the current thread's * context map. * * <p>If the current thread does not have a context map it is * created as a side effect.</p> * @param m The map. * @since 2.7 */ public static void putAll(final Map<String, String> m) { if (contextMap instanceof ThreadContextMap2) { ((ThreadContextMap2) contextMap).putAll(m); } else if (contextMap instanceof DefaultThreadContextMap) { ((DefaultThreadContextMap) contextMap).putAll(m); } else { for (final Map.Entry<String, String> entry: m.entrySet()) { contextMap.put(entry.getKey(), entry.getValue()); } } }
/** * Removes the context values identified by the <code>keys</code> parameter. * * @param keys The keys to remove. * * @since 2.8 */ public static void removeAll(final Iterable<String> keys) { if (contextMap instanceof CleanableThreadContextMap) { ((CleanableThreadContextMap) contextMap).removeAll(keys); } else if (contextMap instanceof DefaultThreadContextMap) { ((DefaultThreadContextMap) contextMap).removeAll(keys); } else { for (final String key : keys) { contextMap.remove(key); } } }
@Test public void testPut() { final DefaultThreadContextMap map = new DefaultThreadContextMap(true); assertTrue(map.isEmpty()); assertFalse(map.containsKey("key")); map.put("key", "value"); assertFalse(map.isEmpty()); assertTrue(map.containsKey("key")); assertEquals("value", map.get("key")); }