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  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   * Test simple ExceptionHandler.
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  }