1616 */
1717package org .apache .logging .log4j .core .message ;
1818
19+ import static org .hamcrest .CoreMatchers .containsString ;
20+ import static org .hamcrest .MatcherAssert .assertThat ;
1921import static org .junit .jupiter .api .Assertions .assertTrue ;
22+ import static org .mockito .Mockito .mock ;
23+ import static org .mockito .Mockito .when ;
2024
25+ import java .lang .management .ThreadInfo ;
2126import org .apache .logging .log4j .message .ThreadDumpMessage ;
2227import org .junit .jupiter .api .Test ;
28+ import org .junit .jupiter .params .ParameterizedTest ;
29+ import org .junit .jupiter .params .provider .EnumSource ;
2330
2431/**
2532 * Tests that ThreadDumpMessage uses ExtendedThreadInformation when available.
@@ -33,4 +40,42 @@ void testMessage() {
3340 // System.out.print(message);
3441 assertTrue (message .contains (" Id=" ), "No header" );
3542 }
43+
44+ @ ParameterizedTest
45+ @ EnumSource (Thread .State .class )
46+ void testMessageWithNullStackTrace (final Thread .State state ) {
47+ obtainMessageWithMissingStackTrace (state , null );
48+ }
49+
50+ @ ParameterizedTest
51+ @ EnumSource (Thread .State .class )
52+ void testMessageWithEmptyStackTrace (final Thread .State state ) {
53+ obtainMessageWithMissingStackTrace (state , new StackTraceElement [0 ]);
54+ }
55+
56+ private void obtainMessageWithMissingStackTrace (final Thread .State state , final StackTraceElement [] stackTrace ) {
57+ // setup
58+ final String threadName = "the thread name" ;
59+ final long threadId = 23523L ;
60+
61+ final ThreadInfo threadInfo = mock (ThreadInfo .class );
62+ when (threadInfo .getStackTrace ()).thenReturn (stackTrace );
63+ when (threadInfo .getThreadName ()).thenReturn (threadName );
64+ when (threadInfo .getThreadId ()).thenReturn (threadId );
65+ when (threadInfo .isSuspended ()).thenReturn (true );
66+ when (threadInfo .isInNative ()).thenReturn (true );
67+ when (threadInfo .getThreadState ()).thenReturn (state );
68+
69+ // given
70+ final ExtendedThreadInformation sut = new ExtendedThreadInformation (threadInfo );
71+
72+ // when
73+ final StringBuilder result = new StringBuilder ();
74+ sut .printThreadInfo (result );
75+
76+ // then
77+ assertThat (result .toString (), containsString (threadName ));
78+ assertThat (result .toString (), containsString (state .name ()));
79+ assertThat (result .toString (), containsString (String .valueOf (threadId )));
80+ }
3681}
0 commit comments