/** * @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; }
@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 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 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()); }
@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 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")); }
@Test public void testDoesNothingIfConstructedWithUseMapIsFalse() { final DefaultThreadContextMap map = new DefaultThreadContextMap(false); assertTrue(map.isEmpty()); assertFalse(map.containsKey("key")); map.put("key", "value"); assertTrue(map.isEmpty()); assertFalse(map.containsKey("key")); assertNull(map.get("key")); }