1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools.bugreport;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
---|
5 | import static org.junit.jupiter.api.Assertions.assertSame;
|
---|
6 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
---|
7 |
|
---|
8 | import java.io.IOException;
|
---|
9 | import java.io.PrintWriter;
|
---|
10 | import java.io.StringWriter;
|
---|
11 |
|
---|
12 | import org.junit.jupiter.api.extension.RegisterExtension;
|
---|
13 | import org.junit.jupiter.api.Test;
|
---|
14 | import org.openstreetmap.josm.actions.ShowStatusReportAction;
|
---|
15 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
16 |
|
---|
17 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Tests the bug report class.
|
---|
21 | * @author Michael Zangl
|
---|
22 | */
|
---|
23 | class BugReportTest {
|
---|
24 | /**
|
---|
25 | * Preferences for the report text
|
---|
26 | */
|
---|
27 | @RegisterExtension
|
---|
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 | 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(ShowStatusReportAction.getReportHeader());
|
---|
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 | 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 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Test {@link BugReport#getCallingMethod(int)}
|
---|
69 | */
|
---|
70 | @Test
|
---|
71 | void testGetCallingMethod() {
|
---|
72 | assertEquals("BugReportTest#testGetCallingMethod", BugReport.getCallingMethod(1));
|
---|
73 | assertEquals("BugReportTest#testGetCallingMethod", testGetCallingMethod2());
|
---|
74 | assertEquals("?", BugReport.getCallingMethod(100));
|
---|
75 | }
|
---|
76 |
|
---|
77 | private String testGetCallingMethod2() {
|
---|
78 | return BugReport.getCallingMethod(2);
|
---|
79 | }
|
---|
80 | }
|
---|