@Override public boolean addAll(final Collection<? extends String> strings) { if (!useStack || strings.isEmpty()) { return false; } final MutableThreadContextStack copy = getNonNullStackCopy(); copy.addAll(strings); copy.freeze(); STACK.set(copy); return true; }
@Test public void testToStringShowsListContents() { final DefaultThreadContextStack stack = new DefaultThreadContextStack(true); stack.clear(); assertEquals("[]", stack.toString()); stack.push("msg1"); stack.add("msg2"); stack.push("msg3"); assertEquals("[msg1, msg2, msg3]", stack.toString()); stack.retainAll(Arrays.asList("msg1", "msg3")); assertEquals("[msg1, msg3]", stack.toString()); } }
contextData.putValue("MDC.A", "A_Value"); contextData.putValue("MDC.B", "B_Value"); final DefaultThreadContextStack contextStack = new DefaultThreadContextStack(true); contextStack.clear(); contextStack.push("stack_msg1"); contextStack.add("stack_msg2"); final Log4jLogEvent expected = Log4jLogEvent.newBuilder() // .setLoggerName("a.B") //
@Test public void testImmutableOrNullReturnsNullIfStackIsEmpty() { final DefaultThreadContextStack stack = new DefaultThreadContextStack(true); stack.clear(); assertTrue(stack.isEmpty()); assertEquals(null, stack.getImmutableStackOrNull()); }
@Test public void testImmutableOrNullReturnsNullIfUseStackIsFalse() { final DefaultThreadContextStack stack = new DefaultThreadContextStack(false); stack.clear(); assertEquals(null, stack.getImmutableStackOrNull()); }
@Test public void testCopy() { final DefaultThreadContextStack stack = createStack(); final ThreadContextStack copy = stack.copy(); assertEquals(3, copy.size()); assertTrue(copy.containsAll(Arrays.asList("msg1", "msg2", "msg3"))); // clearing stack does not affect copy stack.clear(); assertTrue(stack.isEmpty()); assertEquals(3, copy.size()); // not affected assertTrue(copy.containsAll(Arrays.asList("msg1", "msg2", "msg3"))); // adding to copy does not affect stack copy.add("other"); assertEquals(4, copy.size()); // not affected assertTrue(stack.isEmpty()); // adding to stack does not affect copy stack.push("newStackMsg"); assertEquals(1, stack.size()); assertEquals(4, copy.size()); // not affected // clearing copy does not affect stack copy.clear(); assertTrue(copy.isEmpty()); assertEquals(1, stack.size()); }
@Override public ThreadContext.ContextStack convertToEntityAttribute(final String s) { if (Strings.isEmpty(s)) { return null; } List<String> list; try { list = ContextMapJsonAttributeConverter.OBJECT_MAPPER.readValue(s, new TypeReference<List<String>>() { }); } catch (final IOException e) { throw new PersistenceException("Failed to convert JSON string to list for stack.", e); } final DefaultThreadContextStack result = new DefaultThreadContextStack(true); result.addAll(list); return result; } }
@Override public void push(final String message) { if (!useStack) { return; } add(message); }
@Test public void testAddAll() { final DefaultThreadContextStack stack = createStack(); stack.addAll(Arrays.asList("msg4", "msg5")); assertEquals(5, stack.size()); assertTrue(stack.contains("msg1")); assertTrue(stack.contains("msg2")); assertTrue(stack.contains("msg3")); assertTrue(stack.contains("msg4")); assertTrue(stack.contains("msg5")); }
/** * <em>Consider private, used for testing.</em> */ static void init() { ThreadContextMapFactory.init(); contextMap = null; final PropertiesUtil managerProps = PropertiesUtil.getProperties(); disableAll = managerProps.getBooleanProperty(DISABLE_ALL); useStack = !(managerProps.getBooleanProperty(DISABLE_STACK) || disableAll); useMap = !(managerProps.getBooleanProperty(DISABLE_MAP) || disableAll); contextStack = new DefaultThreadContextStack(useStack); if (!useMap) { contextMap = new NoOpThreadContextMap(); } else { contextMap = ThreadContextMapFactory.createThreadContextMap(); } if (contextMap instanceof ReadOnlyThreadContextMap) { readOnlyContextMap = (ReadOnlyThreadContextMap) contextMap; } else { readOnlyContextMap = null; } }
@Override public void push(final String message) { if (!useStack) { return; } add(message); }
@Override public boolean add(final String s) { if (!useStack) { return false; } final MutableThreadContextStack copy = getNonNullStackCopy(); copy.add(s); copy.freeze(); STACK.set(copy); return true; }
@Test public void testPeekReturnsLastAddedItem() { final DefaultThreadContextStack stack = new DefaultThreadContextStack(true); stack.clear(); assertTrue(stack.isEmpty()); stack.push("msg1"); stack.add("msg2"); assertEquals(2, stack.size()); assertEquals("msg2", stack.peek()); stack.push("msg3"); assertEquals("msg3", stack.peek()); }
@Override public boolean addAll(final Collection<? extends String> strings) { if (!useStack || strings.isEmpty()) { return false; } final MutableThreadContextStack copy = getNonNullStackCopy(); copy.addAll(strings); copy.freeze(); STACK.set(copy); return true; }
@Override public boolean add(final String s) { if (!useStack) { return false; } final MutableThreadContextStack copy = getNonNullStackCopy(); copy.add(s); copy.freeze(); STACK.set(copy); return true; }