From 0ae8b3e3b01b2fcaad06cd964af47436c127b5fd Mon Sep 17 00:00:00 2001
From: Robert Scott <code@humanleg.org.uk>
Date: Sat, 27 Oct 2018 14:51:49 +0100
Subject: [PATCH v1 1/2] JOSMTestRules: use ReportedException (with now-public
 constructors) to format timeout stack traces properly

---
 .../openstreetmap/josm/tools/bugreport/ReportedException.java  |  4 ++--
 test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java  | 10 +++++++++-
 2 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/src/org/openstreetmap/josm/tools/bugreport/ReportedException.java b/src/org/openstreetmap/josm/tools/bugreport/ReportedException.java
index cf990ac6f..0ca78e5f2 100644
--- a/src/org/openstreetmap/josm/tools/bugreport/ReportedException.java
+++ b/src/org/openstreetmap/josm/tools/bugreport/ReportedException.java
@@ -48,11 +48,11 @@ public class ReportedException extends RuntimeException {
     private final transient Thread caughtOnThread;
     private String methodWarningFrom;
 
-    ReportedException(Throwable exception) {
+    public ReportedException(Throwable exception) {
         this(exception, Thread.currentThread());
     }
 
-    ReportedException(Throwable exception, Thread caughtOnThread) {
+    public ReportedException(Throwable exception, Thread caughtOnThread) {
         super(exception);
 
         try {
diff --git a/test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java b/test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java
index eed522f58..7d41bbd13 100644
--- a/test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java
+++ b/test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java
@@ -7,6 +7,8 @@ import java.awt.event.WindowEvent;
 import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.StringWriter;
 import java.lang.annotation.Documented;
 import java.lang.annotation.ElementType;
 import java.lang.annotation.Retention;
@@ -60,6 +62,7 @@ import org.openstreetmap.josm.tools.Logging;
 import org.openstreetmap.josm.tools.MemoryManagerTest;
 import org.openstreetmap.josm.tools.RightAndLefthandTraffic;
 import org.openstreetmap.josm.tools.Territories;
+import org.openstreetmap.josm.tools.bugreport.ReportedException;
 import org.openstreetmap.josm.tools.date.DateUtils;
 
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
@@ -673,7 +676,12 @@ public class JOSMTestRules implements TestRule {
                 if (exception != null) {
                     throw exception;
                 } else {
-                    Logging.debug("Thread state at timeout: {0}", Thread.getAllStackTraces());
+                    if (Logging.isLoggingEnabled(Logging.LEVEL_DEBUG)) {
+                        // i.e. skip expensive formatting of stack trace if it won't be shown
+                        final StringWriter sw = new StringWriter();
+                        new ReportedException(exception).printReportThreadsTo(new PrintWriter(sw));
+                        Logging.debug("Thread state at timeout: {0}", sw);
+                    }
                     throw new Exception(MessageFormat.format("Test timed out after {0}ms", timeout));
                 }
             }
-- 
2.11.0

