1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.jxpath.ri;
19
20 import static org.junit.jupiter.api.Assertions.assertThrows;
21 import static org.junit.jupiter.api.Assertions.fail;
22
23 import org.apache.commons.jxpath.AbstractJXPathTest;
24 import org.apache.commons.jxpath.JXPathContext;
25 import org.junit.jupiter.api.BeforeEach;
26 import org.junit.jupiter.api.Test;
27
28
29
30
31 public class ExceptionHandlerTest extends AbstractJXPathTest {
32
33 public static class Bar {
34
35 public Object getBaz() {
36 throw new IllegalStateException("baz unavailable");
37 }
38 }
39
40 private JXPathContext context;
41 private final Bar bar = new Bar();
42
43 public Bar getBar() {
44 return bar;
45 }
46
47 public Object getFoo() {
48 throw new IllegalStateException("foo unavailable");
49 }
50
51 @Override
52 @BeforeEach
53 public void setUp() throws Exception {
54 context = JXPathContext.newContext(this);
55 context.setExceptionHandler((t, ptr) -> {
56 if (t instanceof Error) {
57 throw (Error) t;
58 }
59 if (t instanceof RuntimeException) {
60 throw (RuntimeException) t;
61 }
62 throw new RuntimeException(t);
63 });
64 }
65
66 @Test
67 public void testHandleBarBaz() throws Exception {
68 Throwable t = assertThrows(Throwable.class, () -> context.getValue("bar/baz"), "expected Throwable");
69 while (t != null) {
70 if ("baz unavailable".equals(t.getMessage())) {
71 return;
72 }
73 t = t.getCause();
74 }
75 fail("expected \"baz unavailable\" in throwable chain");
76 }
77
78 @Test
79 public void testHandleFoo() throws Exception {
80 Throwable t = assertThrows(Throwable.class, () -> context.getValue("foo"), "expected Throwable");
81 while (t != null) {
82 if ("foo unavailable".equals(t.getMessage())) {
83 return;
84 }
85 t = t.getCause();
86 }
87 fail("expected \"foo unavailable\" in throwable chain");
88 }
89 }