KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gov > nasa > jpf > jvm > TestException


1 //
2
// Copyright (C) 2005 United States Government as represented by the
3
// Administrator of the National Aeronautics and Space Administration
4
// (NASA). All Rights Reserved.
5
//
6
// This software is distributed under the NASA Open Source Agreement
7
// (NOSA), version 1.3. The NOSA has been approved by the Open Source
8
// Initiative. See the file NOSA-1.3-JPF at the top of the distribution
9
// directory tree for the complete NOSA document.
10
//
11
// THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
12
// KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
13
// LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
14
// SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
15
// A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
16
// THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
17
// DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
18
//
19
package gov.nasa.jpf.jvm;
20
21 /**
22  * JPF unit test for exception handling
23  */

24 public class TestException {
25   int data;
26
27   void foo () {
28   }
29   
30   static void bar () {
31     TestException o = null;
32     o.foo();
33   }
34   
35   public static void main (String JavaDoc[] args) {
36     if (args.length > 0) {
37       // just run the specified tests
38
for (int i = 0; i < args.length; i++) {
39         String JavaDoc func = args[i];
40
41         // note that we don't use reflection here because this would
42
// blow up execution/test scope under JPF
43
if ("testNPE".equals(func)) { testNPE(); }
44         else if ("testNPECall".equals(func)) { testNPECall(); }
45         else if ("testArrayIndexOutOfBoundsLow".equals(func)) { testArrayIndexOutOfBoundsLow(); }
46         else if ("testArrayIndexOutOfBoundsHigh".equals(func)) { testArrayIndexOutOfBoundsHigh(); }
47         else if ("testLocalHandler".equals(func)) { testLocalHandler(); }
48         else if ("testCallerHandler".equals(func)) { testCallerHandler(); }
49         else {
50           throw new IllegalArgumentException JavaDoc("unknown test function");
51         }
52       }
53     } else {
54       testNPE();
55       testNPECall();
56       testArrayIndexOutOfBoundsLow();
57       testArrayIndexOutOfBoundsHigh();
58       testLocalHandler();
59       testCallerHandler();
60     }
61   }
62
63   static void testNPE () {
64     TestException o = null;
65     o.data = -1;
66
67     assert false : "should never get here";
68   }
69   
70   static void testNPECall () {
71     TestException o = null;
72     o.foo();
73
74     assert false : "should never get here";
75   }
76
77   static void testArrayIndexOutOfBoundsLow () {
78     int[] a = new int[10];
79     a[-1] = 0;
80
81     assert false : "should never get here";
82   }
83
84   static void testArrayIndexOutOfBoundsHigh () {
85     int[] a = new int[10];
86     a[10] = 0;
87
88     assert false : "should never get here";
89   }
90
91   static void testLocalHandler () {
92     try {
93       TestException o = null;
94       o.data = 0;
95     } catch (IllegalArgumentException JavaDoc iax) {
96       assert false : "should never get here";
97     } catch (NullPointerException JavaDoc npe) {
98       return;
99     } catch (Exception JavaDoc x) {
100       assert false : "should never get here";
101     }
102     
103     assert false : "should never get here";
104   }
105
106   static void testCallerHandler () {
107     try {
108       bar();
109     } catch (Throwable JavaDoc t) {
110       return;
111     }
112     
113     assert false : "should never get here";
114   }
115   
116 }
117
118
Popular Tags