Ticket #13434: patch-test-bug-report.patch
File patch-test-bug-report.patch, 7.5 KB (added by , 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 { 122 122 StringWriter stringWriter = new StringWriter(); 123 123 PrintWriter out = new PrintWriter(stringWriter); 124 124 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 } 126 130 } 127 131 if (isIncludeData()) { 128 132 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; 4 4 import java.awt.GraphicsEnvironment; 5 5 import java.util.ArrayList; 6 6 import java.util.LinkedList; 7 import java.util.concurrent.CopyOnWriteArrayList; 7 8 import java.util.function.BiFunction; 9 import java.util.function.Predicate; 8 10 9 11 import org.openstreetmap.josm.Main; 10 12 … … public class BugReportQueue { 22 24 private ArrayList<ReportedException> suppressFor = new ArrayList<>(); 23 25 private Thread displayThread; 24 26 private final BiFunction<ReportedException, Integer, SuppressionMode> bugReportHandler = getBestHandler(); 27 private final CopyOnWriteArrayList<Predicate<ReportedException>> handlers = new CopyOnWriteArrayList<>(); 25 28 private int displayedErrors; 26 29 27 30 private boolean inReportDialog; … … public class BugReportQueue { 96 99 } 97 100 98 101 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 } 99 106 return bugReportHandler.apply(e, getDisplayedErrors()); 100 107 } 101 108 … … public class BugReportQueue { 122 129 } 123 130 } 124 131 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 */ 125 144 public static BugReportQueue getInstance() { 126 145 return INSTANCE; 127 146 } -
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 1 1 // License: GPL. For details, see LICENSE file. 2 2 package org.openstreetmap.josm.tools.bugreport; 3 3 4 import static org.junit.Assert.assertFalse;4 import java.util.concurrent.CountDownLatch; 5 5 6 import org.junit. Before;6 import org.junit.Rule; 7 7 import org.junit.Test; 8 import org.openstreetmap.josm.JOSMFixture; 8 import org.openstreetmap.josm.testutils.JOSMTestRules; 9 10 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; 9 11 10 12 /** 11 13 * Unit tests of {@link BugReportExceptionHandler} class. 12 14 */ 13 15 public class BugReportExceptionHandlerTest { 14 15 16 /** 16 * Setup tests.17 * No dependcies 17 18 */ 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(); 22 22 23 23 /** 24 24 * Unit test for {@link BugReportExceptionHandler#handleException} method. 25 * @throws InterruptedException 25 26 */ 26 27 @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;}); 28 31 BugReportExceptionHandler.handleException(new Exception("testHandleException")); 29 assertFalse(BugReportExceptionHandler.exceptionHandlingInProgress());32 latch.await(); 30 33 } 31 34 } -
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 2 2 package org.openstreetmap.josm.tools.bugreport; 3 3 4 4 import static org.junit.Assert.assertEquals; 5 import static org.junit.Assert.assertSame; 6 import static org.junit.Assert.assertTrue; 5 7 8 import java.io.IOException; 9 import java.io.PrintWriter; 10 import java.io.StringWriter; 11 12 import org.junit.Rule; 6 13 import org.junit.Test; 14 import org.openstreetmap.josm.testutils.JOSMTestRules; 15 16 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; 7 17 8 18 /** 9 19 * Tests the bug report class. … … import org.junit.Test; 11 21 * @since 10285 12 22 */ 13 23 public 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 } 14 66 15 67 /** 16 68 * Test {@link BugReport#getCallingMethod(int)} … … public class BugReportTest { 19 71 public void testGetCallingMethod() { 20 72 assertEquals("BugReportTest#testGetCallingMethod", BugReport.getCallingMethod(1)); 21 73 assertEquals("BugReportTest#testGetCallingMethod", testGetCallingMethod2()); 74 assertEquals("?", BugReport.getCallingMethod(100)); 22 75 } 23 76 24 77 private String testGetCallingMethod2() {