16
16
*/
17
17
package org .apache .logging .log4j .core .message ;
18
18
19
+ import static org .hamcrest .CoreMatchers .containsString ;
20
+ import static org .hamcrest .MatcherAssert .assertThat ;
19
21
import static org .junit .jupiter .api .Assertions .assertTrue ;
22
+ import static org .mockito .Mockito .mock ;
23
+ import static org .mockito .Mockito .when ;
20
24
25
+ import java .lang .management .ThreadInfo ;
21
26
import org .apache .logging .log4j .message .ThreadDumpMessage ;
22
27
import org .junit .jupiter .api .Test ;
28
+ import org .junit .jupiter .params .ParameterizedTest ;
29
+ import org .junit .jupiter .params .provider .EnumSource ;
23
30
24
31
/**
25
32
* Tests that ThreadDumpMessage uses ExtendedThreadInformation when available.
@@ -33,4 +40,42 @@ void testMessage() {
33
40
// System.out.print(message);
34
41
assertTrue (message .contains (" Id=" ), "No header" );
35
42
}
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
+ }
36
81
}
0 commit comments