1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools.bugreport;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertEquals;
|
---|
5 | import static org.junit.Assert.assertSame;
|
---|
6 | import static org.junit.Assert.assertTrue;
|
---|
7 |
|
---|
8 | import java.io.IOException;
|
---|
9 | import java.io.PrintWriter;
|
---|
10 | import java.io.StringWriter;
|
---|
11 |
|
---|
12 | import org.junit.Rule;
|
---|
13 | import org.junit.Test;
|
---|
14 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
15 |
|
---|
16 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Tests the bug report class.
|
---|
20 | * @author Michael Zangl
|
---|
21 | */
|
---|
22 | public class BugReportTest {
|
---|
23 | /**
|
---|
24 | * Preferences for the report text
|
---|
25 | */
|
---|
26 | @Rule
|
---|
27 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
28 | public JOSMTestRules test = new JOSMTestRules().preferences();
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Test {@link BugReport#getReportText()}
|
---|
32 | */
|
---|
33 | @Test
|
---|
34 | public void testReportText() {
|
---|
35 | ReportedException e = interceptInChildMethod(new IOException("test-exception-message"));
|
---|
36 | e.put("test-key", "test-value");
|
---|
37 | String text = new BugReport(e).getReportText();
|
---|
38 |
|
---|
39 | assertTrue(text.contains("test-exception-message"));
|
---|
40 | assertTrue(text.contains("interceptInChildMethod"));
|
---|
41 | assertTrue(text.contains("testReportText")); // stack trace
|
---|
42 | assertTrue(text.contains("test-key: test-value"));
|
---|
43 | }
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Test {@link BugReport#intercept(Throwable)}
|
---|
47 | */
|
---|
48 | @Test
|
---|
49 | public void testIntercept() {
|
---|
50 | IOException base = new IOException("test");
|
---|
51 | ReportedException intercepted = interceptInChildMethod(base);
|
---|
52 | assertEquals(intercepted.getCause(), base);
|
---|
53 |
|
---|
54 | StringWriter out = new StringWriter();
|
---|
55 | intercepted.printReportDataTo(new PrintWriter(out));
|
---|
56 |
|
---|
57 | assertTrue(out.toString().contains("interceptInChildMethod")); // calling method.
|
---|
58 |
|
---|
59 | assertSame(intercepted, BugReport.intercept(intercepted));
|
---|
60 | }
|
---|
61 |
|
---|
62 | private ReportedException interceptInChildMethod(IOException base) {
|
---|
63 | return BugReport.intercept(base);
|
---|
64 | }
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Test {@link BugReport#getCallingMethod(int)}
|
---|
68 | */
|
---|
69 | @Test
|
---|
70 | public void testGetCallingMethod() {
|
---|
71 | assertEquals("BugReportTest#testGetCallingMethod", BugReport.getCallingMethod(1));
|
---|
72 | assertEquals("BugReportTest#testGetCallingMethod", testGetCallingMethod2());
|
---|
73 | assertEquals("?", BugReport.getCallingMethod(100));
|
---|
74 | }
|
---|
75 |
|
---|
76 | private String testGetCallingMethod2() {
|
---|
77 | return BugReport.getCallingMethod(2);
|
---|
78 | }
|
---|
79 | }
|
---|