View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Test simple ExceptionHandler.
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  }