2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0.
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package gov.nasa.jpf.test.vm.basic;
20 import gov.nasa.jpf.util.test.TestJPF;
22 import java.lang.reflect.Method;
24 import org.junit.Test;
27 * JPF unit test for exception handling
29 @SuppressWarnings("null")
30 public class ExceptionHandlingTest extends TestJPF {
37 ExceptionHandlingTest o = null;
41 @Test public void testNPE () {
42 if (verifyUnhandledException("java.lang.NullPointerException")){
43 ExceptionHandlingTest o = null;
46 assert false : "should never get here";
50 @Test public void testNPECall () {
51 if (verifyUnhandledException("java.lang.NullPointerException")){
52 ExceptionHandlingTest o = null;
55 assert false : "should never get here";
59 @Test public void testArrayIndexOutOfBoundsLow () {
60 if (verifyUnhandledException("java.lang.ArrayIndexOutOfBoundsException")){
61 int[] a = new int[10];
64 assert false : "should never get here";
68 @Test public void testArrayIndexOutOfBoundsHigh () {
69 if (verifyUnhandledException("java.lang.ArrayIndexOutOfBoundsException")){
70 int[] a = new int[10];
73 assert false : "should never get here";
77 @Test public void testLocalHandler () {
78 if (verifyNoPropertyViolation()){
80 ExceptionHandlingTest o = null;
82 } catch (IllegalArgumentException iax) {
83 assert false : "should never get here";
84 } catch (NullPointerException npe) {
86 } catch (Exception x) {
87 assert false : "should never get here";
90 assert false : "should never get here";
94 @Test public void testCallerHandler () {
95 if (verifyNoPropertyViolation()){
98 } catch (Throwable t) {
102 assert false : "should never get here";
106 @Test public void testEmptyHandler () {
107 if (verifyNoPropertyViolation()){
109 throw new RuntimeException("should be empty-handled");
110 } catch (Throwable t) {
116 @Test public void testEmptyTryBlock () {
117 if (verifyNoPropertyViolation()){
120 } catch (Throwable t) {
121 assert false : "should never get here";
126 @Test public void testStackTrace() {
127 if (verifyNoPropertyViolation()){
129 Throwable x = new Throwable();
130 StackTraceElement[] st = x.getStackTrace();
132 //x.printStackTrace();
133 for (int i=0; i<st.length; i++){
134 System.out.print("\t at ");
135 System.out.print(st[i].getClassName());
136 System.out.print('.');
137 System.out.print(st[i].getMethodName());
138 System.out.print('(');
139 System.out.print(st[i].getFileName());
140 System.out.print(':');
141 System.out.print(st[i].getLineNumber());
142 System.out.println(')');
145 // note - direct call stackframes should not show up here, they are JPF internal
146 assert st.length == 3 : "wrong stack trace depth";
148 assert st[0].getClassName().equals(ExceptionHandlingTest.class.getName());
149 assert st[0].getMethodName().equals("testStackTrace");
151 assert st[1].getClassName().equals(Method.class.getName());
152 assert st[1].getMethodName().equals("invoke");
154 assert st[2].getClassName().equals(TestJPF.class.getName());
155 assert st[2].getMethodName().equals("runTestMethod");