18
18
import org .junit .jupiter .engine .Constants ;
19
19
20
20
/**
21
- * The default implementation for {@link PreInterruptCallback},
22
- * which will print the stacks of all {@link Thread}s to {@code System.out}.
21
+ * Default implementation of {@link PreInterruptCallback}, which prints the stacks
22
+ * of all {@link Thread}s to {@code System.out}.
23
23
*
24
- * <p>Note: This is disabled by default, and must be enabled with
25
- * {@link Constants#EXTENSIONS_TIMEOUT_THREAD_DUMP_ENABLED_PROPERTY_NAME}
24
+ * <p>Note: This is disabled by default and must be enabled via
25
+ * {@link Constants#EXTENSIONS_TIMEOUT_THREAD_DUMP_ENABLED_PROPERTY_NAME}.
26
26
*
27
27
* @since 5.12
28
28
*/
29
29
final class PreInterruptThreadDumpPrinter implements PreInterruptCallback {
30
+
30
31
private static final String NL = "\n " ;
31
32
32
33
@ Override
33
34
public void beforeThreadInterrupt (PreInterruptContext preInterruptContext , ExtensionContext extensionContext ) {
34
-
35
35
Map <Thread , StackTraceElement []> stackTraces = Thread .getAllStackTraces ();
36
36
37
- StringBuilder sb = new StringBuilder ();
38
- sb .append ("Thread " );
37
+ StringBuilder sb = new StringBuilder ("Thread " );
39
38
appendThreadName (sb , preInterruptContext .getThreadToInterrupt ());
40
39
sb .append (" will be interrupted." );
41
40
sb .append (NL );
@@ -48,10 +47,9 @@ public void beforeThreadInterrupt(PreInterruptContext preInterruptContext, Exten
48
47
appendThreadName (sb , thread );
49
48
for (StackTraceElement stackTraceElement : stack ) {
50
49
sb .append (NL );
51
- //Do the same prefix as java.lang.Throwable.printStackTrace(java.lang.Throwable. PrintStreamOrWriter)
50
+ // Use the same prefix as java.lang.Throwable.printStackTrace(PrintStreamOrWriter)
52
51
sb .append ("\t at " );
53
52
sb .append (stackTraceElement .toString ());
54
-
55
53
}
56
54
sb .append (NL );
57
55
}
@@ -61,23 +59,25 @@ public void beforeThreadInterrupt(PreInterruptContext preInterruptContext, Exten
61
59
}
62
60
63
61
/**
64
- * Appends the {@link Thread} name and ID in a similar fashion as {@code jstack}.
65
- * @param sb the buffer
66
- * @param th the thread to append
62
+ * Append the {@link Thread} name and ID in a similar fashion as {@code jstack}.
63
+ * @param builder the builder to append to
64
+ * @param thread the thread whose information should be appended
67
65
*/
68
- private void appendThreadName (StringBuilder sb , Thread th ) {
69
- // Use same format as java.lang.management.ThreadInfo.toString
70
- sb .append ("\" " );
71
- sb .append (th .getName ());
72
- sb .append ("\" " );
73
- if (th .isDaemon ()) {
74
- sb .append (" daemon" );
66
+ @ SuppressWarnings ("deprecation" ) // Thread.getId() is deprecated on JDK 19+
67
+ private static void appendThreadName (StringBuilder builder , Thread thread ) {
68
+ // Use same format as java.lang.management.ThreadInfo.toString()
69
+ builder .append ("\" " );
70
+ builder .append (thread .getName ());
71
+ builder .append ("\" " );
72
+ if (thread .isDaemon ()) {
73
+ builder .append (" daemon" );
75
74
}
76
- sb .append (" prio=" );
77
- sb .append (th .getPriority ());
78
- sb .append (" Id=" );
79
- sb .append (th .getId ());
80
- sb .append (" " );
81
- sb .append (th .getState ());
75
+ builder .append (" prio=" );
76
+ builder .append (thread .getPriority ());
77
+ builder .append (" Id=" );
78
+ builder .append (thread .getId ());
79
+ builder .append (" " );
80
+ builder .append (thread .getState ());
82
81
}
82
+
83
83
}
0 commit comments