Ticket #13434: patch-test-bug-report.patch

File patch-test-bug-report.patch, 7.5 KB (added by michael2402, 8 years ago)
  • src/org/openstreetmap/josm/tools/bugreport/BugReport.java

    diff --git a/src/org/openstreetmap/josm/tools/bugreport/BugReport.java b/src/org/openstreetmap/josm/tools/bugreport/BugReport.java
    index d47586f..2aa38e1 100644
    a b public final class BugReport implements Serializable {  
    122122        StringWriter stringWriter = new StringWriter();
    123123        PrintWriter out = new PrintWriter(stringWriter);
    124124        if (isIncludeStatusReport()) {
    125             out.println(ShowStatusReportAction.getReportHeader());
     125            try {
     126                out.println(ShowStatusReportAction.getReportHeader());
     127            } catch (RuntimeException e) {
     128                out.println("Could not generate status report: " + e.getMessage());
     129            }
    126130        }
    127131        if (isIncludeData()) {
    128132            exception.printReportDataTo(out);
  • src/org/openstreetmap/josm/tools/bugreport/BugReportQueue.java

    diff --git a/src/org/openstreetmap/josm/tools/bugreport/BugReportQueue.java b/src/org/openstreetmap/josm/tools/bugreport/BugReportQueue.java
    index e7a5e09..e1105de 100644
    a b package org.openstreetmap.josm.tools.bugreport;  
    44import java.awt.GraphicsEnvironment;
    55import java.util.ArrayList;
    66import java.util.LinkedList;
     7import java.util.concurrent.CopyOnWriteArrayList;
    78import java.util.function.BiFunction;
     9import java.util.function.Predicate;
    810
    911import org.openstreetmap.josm.Main;
    1012
    public class BugReportQueue {  
    2224    private ArrayList<ReportedException> suppressFor = new ArrayList<>();
    2325    private Thread displayThread;
    2426    private final BiFunction<ReportedException, Integer, SuppressionMode> bugReportHandler = getBestHandler();
     27    private final CopyOnWriteArrayList<Predicate<ReportedException>> handlers = new CopyOnWriteArrayList<>();
    2528    private int displayedErrors;
    2629
    2730    private boolean inReportDialog;
    public class BugReportQueue {  
    9699    }
    97100
    98101    private SuppressionMode displayFor(ReportedException e) {
     102        if (handlers.stream().anyMatch(p -> p.test(e))) {
     103            Main.trace("Intercepted by handler.");
     104            return SuppressionMode.NONE;
     105        }
    99106        return bugReportHandler.apply(e, getDisplayedErrors());
    100107    }
    101108
    public class BugReportQueue {  
    122129        }
    123130    }
    124131
     132    /**
     133     * Allows you to peek or even intersect the bug reports.
     134     * @param handler The handler. It can return false to stop all further handling of the exception.
     135     */
     136    public void addBugReportHandler(Predicate<ReportedException> handler) {
     137        handlers.add(handler);
     138    }
     139
     140    /**
     141     * Gets the global bug report queue
     142     * @return The queue
     143     */
    125144    public static BugReportQueue getInstance() {
    126145        return INSTANCE;
    127146    }
  • test/unit/org/openstreetmap/josm/tools/bugreport/BugReportExceptionHandlerTest.java

    diff --git a/test/unit/org/openstreetmap/josm/tools/bugreport/BugReportExceptionHandlerTest.java b/test/unit/org/openstreetmap/josm/tools/bugreport/BugReportExceptionHandlerTest.java
    index a030600..0440be2 100644
    a b  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.tools.bugreport;
    33
    4 import static org.junit.Assert.assertFalse;
     4import java.util.concurrent.CountDownLatch;
    55
    6 import org.junit.Before;
     6import org.junit.Rule;
    77import org.junit.Test;
    8 import org.openstreetmap.josm.JOSMFixture;
     8import org.openstreetmap.josm.testutils.JOSMTestRules;
     9
     10import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
    911
    1012/**
    1113 * Unit tests of {@link BugReportExceptionHandler} class.
    1214 */
    1315public class BugReportExceptionHandlerTest {
    14 
    1516    /**
    16      * Setup tests.
     17     * No dependcies
    1718     */
    18     @Before
    19     public void setUp() {
    20         JOSMFixture.createUnitTestFixture().init(true);
    21     }
     19    @Rule
     20    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
     21    public JOSMTestRules test = new JOSMTestRules();
    2222
    2323    /**
    2424     * Unit test for {@link BugReportExceptionHandler#handleException} method.
     25     * @throws InterruptedException
    2526     */
    2627    @Test
    27     public void testHandleException() {
     28    public void testHandleException() throws InterruptedException {
     29        CountDownLatch latch = new CountDownLatch(1);
     30        BugReportQueue.getInstance().addBugReportHandler(e -> {latch.countDown(); return false;});
    2831        BugReportExceptionHandler.handleException(new Exception("testHandleException"));
    29         assertFalse(BugReportExceptionHandler.exceptionHandlingInProgress());
     32        latch.await();
    3033    }
    3134}
  • test/unit/org/openstreetmap/josm/tools/bugreport/BugReportTest.java

    diff --git a/test/unit/org/openstreetmap/josm/tools/bugreport/BugReportTest.java b/test/unit/org/openstreetmap/josm/tools/bugreport/BugReportTest.java
    index ab9cf76..451910c 100644
    a b  
    22package org.openstreetmap.josm.tools.bugreport;
    33
    44import static org.junit.Assert.assertEquals;
     5import static org.junit.Assert.assertSame;
     6import static org.junit.Assert.assertTrue;
    57
     8import java.io.IOException;
     9import java.io.PrintWriter;
     10import java.io.StringWriter;
     11
     12import org.junit.Rule;
    613import org.junit.Test;
     14import org.openstreetmap.josm.testutils.JOSMTestRules;
     15
     16import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
    717
    818/**
    919 * Tests the bug report class.
    import org.junit.Test;  
    1121 * @since 10285
    1222 */
    1323public class BugReportTest {
     24    /**
     25     * Preferences for the report text
     26     */
     27    @Rule
     28    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
     29    public JOSMTestRules test = new JOSMTestRules().preferences();
     30
     31    /**
     32     * Test {@link BugReport#getReportText()}
     33     */
     34    @Test
     35    public void testReportText() {
     36        ReportedException e = interceptInChildMethod(new IOException("test-exception-message"));
     37        e.put("test-key", "test-value");
     38        String text = new BugReport(e).getReportText();
     39
     40        assertTrue(text.contains("test-exception-message"));
     41        assertTrue(text.contains("interceptInChildMethod"));
     42        assertTrue(text.contains("testReportText")); // stack trace
     43        assertTrue(text.contains("test-key: test-value"));
     44    }
     45
     46    /**
     47     * Test {@link BugReport#intercept(Throwable)}
     48     */
     49    @Test
     50    public void testIntercept() {
     51        IOException base = new IOException("test");
     52        ReportedException intercepted = interceptInChildMethod(base);
     53        assertEquals(intercepted.getCause(), base);
     54
     55        StringWriter out = new StringWriter();
     56        intercepted.printReportDataTo(new PrintWriter(out));
     57
     58        assertTrue(out.toString().contains("interceptInChildMethod")); // calling method.
     59
     60        assertSame(intercepted, BugReport.intercept(intercepted));
     61    }
     62
     63    private ReportedException interceptInChildMethod(IOException base) {
     64        return BugReport.intercept(base);
     65    }
    1466
    1567    /**
    1668     * Test {@link BugReport#getCallingMethod(int)}
    public class BugReportTest {  
    1971    public void testGetCallingMethod() {
    2072        assertEquals("BugReportTest#testGetCallingMethod", BugReport.getCallingMethod(1));
    2173        assertEquals("BugReportTest#testGetCallingMethod", testGetCallingMethod2());
     74        assertEquals("?", BugReport.getCallingMethod(100));
    2275    }
    2376
    2477    private String testGetCallingMethod2() {