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