source: josm/trunk/test/unit/org/openstreetmap/josm/tools/bugreport/ReportedExceptionTest.java@ 17360

Last change on this file since 17360 was 17275, checked in by Don-vip, 3 years ago

see #16567 - upgrade almost all tests to JUnit 5, except those depending on WiremockRule

See https://github.com/tomakehurst/wiremock/issues/684

  • Property svn:eol-style set to native
File size: 3.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools.bugreport;
3
4import static org.junit.jupiter.api.Assertions.assertEquals;
5
6import java.util.Arrays;
7
8import org.junit.jupiter.api.Test;
9
10/**
11 * Tests the {@link ReportedException} class.
12 * @author Michael Zangl
13 * @since 10285
14 */
15class ReportedExceptionTest {
16 private static final class CauseOverwriteException extends RuntimeException {
17 private Throwable myCause;
18
19 private CauseOverwriteException(String message) {
20 super(message);
21 }
22
23 @Override
24 public synchronized Throwable getCause() {
25 return myCause;
26 }
27 }
28
29 /**
30 * Tests that {@link ReportedException#put(String, Object)} handles null values
31 */
32 @Test
33 void testPutDoesHandleNull() {
34 ReportedException e = new ReportedException(new RuntimeException());
35 e.startSection("test");
36 Object[] a = new Object[] {
37 new Object(), null };
38 e.put("testObject", null);
39 e.put("testArray", a);
40 e.put("testList", Arrays.asList(a));
41 }
42
43 /**
44 * Tests that {@link ReportedException#put(String, Object)} handles exceptions during toString fine.
45 */
46 @Test
47 void testPutDoesNotThrow() {
48 ReportedException e = new ReportedException(new RuntimeException());
49 e.startSection("test");
50 Object o = new Object() {
51 @Override
52 public String toString() {
53 throw new IllegalArgumentException("");
54 }
55 };
56 Object[] a = new Object[] {
57 new Object(), o };
58 e.put("testObject", o);
59 e.put("testArray", a);
60 e.put("testList", Arrays.asList(a));
61 }
62
63 /**
64 * Tests that {@link ReportedException#isSame(ReportedException)} works as expected.
65 */
66 @Test
67 void testIsSame() {
68 // Do not break this line! All exceptions need to be created in the same line.
69 // CHECKSTYLE.OFF: LineLength
70 // @formatter:off
71 ReportedException[] testExceptions = new ReportedException[] {
72 /* 0 */ genException1(), /* 1, same as 0 */ genException1(), /* 2 */ genException2("x"), /* 3, same as 2 */ genException2("x"), /* 4, has different message than 2 */ genException2("y"), /* 5, has different stack trace than 2 */ genException3("x"), /* 6 */ genException4(true), /* 7, has different cause than 6 */ genException4(false), /* 8, has a cycle and should not crash */ genExceptionCycle() };
73 // @formatter:on
74 // CHECKSTYLE.ON: LineLength
75
76 for (int i = 0; i < testExceptions.length; i++) {
77 for (int j = 0; j < testExceptions.length; j++) {
78 boolean is01 = (i == 0 || i == 1) && (j == 0 || j == 1);
79 boolean is23 = (i == 2 || i == 3) && (j == 2 || j == 3);
80 assertEquals(is01 || is23 || i == j, testExceptions[i].isSame(testExceptions[j]), i + ", " + j);
81 }
82 }
83 }
84
85 private static ReportedException genException1() {
86 RuntimeException e = new RuntimeException();
87 return BugReport.intercept(e);
88 }
89
90 private static ReportedException genException2(String message) {
91 RuntimeException e = new RuntimeException(message);
92 RuntimeException e2 = new RuntimeException(e);
93 return BugReport.intercept(e2);
94 }
95
96 private static ReportedException genException3(String message) {
97 return genException2(message);
98 }
99
100 private static ReportedException genException4(boolean addCause) {
101 RuntimeException e = new RuntimeException("x");
102 RuntimeException e2 = new RuntimeException("x", addCause ? e : null);
103 return BugReport.intercept(e2);
104 }
105
106 private static ReportedException genExceptionCycle() {
107 CauseOverwriteException e = new CauseOverwriteException("x");
108 RuntimeException e2 = new RuntimeException("x", e);
109 e.myCause = e2;
110 return BugReport.intercept(e2);
111 }
112}
Note: See TracBrowser for help on using the repository browser.