001: //
002: // Copyright (C) 2005 United States Government as represented by the
003: // Administrator of the National Aeronautics and Space Administration
004: // (NASA). All Rights Reserved.
005: //
006: // This software is distributed under the NASA Open Source Agreement
007: // (NOSA), version 1.3. The NOSA has been approved by the Open Source
008: // Initiative. See the file NOSA-1.3-JPF at the top of the distribution
009: // directory tree for the complete NOSA document.
010: //
011: // THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
012: // KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
013: // LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
014: // SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
015: // A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
016: // THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
017: // DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
018: //
019: package gov.nasa.jpf.jvm;
020:
021: /**
022: * JPF unit test for exception handling
023: */
024: public class TestException {
025: int data;
026:
027: void foo() {
028: }
029:
030: static void bar() {
031: TestException o = null;
032: o.foo();
033: }
034:
035: public static void main(String[] args) {
036: if (args.length > 0) {
037: // just run the specified tests
038: for (int i = 0; i < args.length; i++) {
039: String func = args[i];
040:
041: // note that we don't use reflection here because this would
042: // blow up execution/test scope under JPF
043: if ("testNPE".equals(func)) {
044: testNPE();
045: } else if ("testNPECall".equals(func)) {
046: testNPECall();
047: } else if ("testArrayIndexOutOfBoundsLow".equals(func)) {
048: testArrayIndexOutOfBoundsLow();
049: } else if ("testArrayIndexOutOfBoundsHigh".equals(func)) {
050: testArrayIndexOutOfBoundsHigh();
051: } else if ("testLocalHandler".equals(func)) {
052: testLocalHandler();
053: } else if ("testCallerHandler".equals(func)) {
054: testCallerHandler();
055: } else {
056: throw new IllegalArgumentException(
057: "unknown test function");
058: }
059: }
060: } else {
061: testNPE();
062: testNPECall();
063: testArrayIndexOutOfBoundsLow();
064: testArrayIndexOutOfBoundsHigh();
065: testLocalHandler();
066: testCallerHandler();
067: }
068: }
069:
070: static void testNPE() {
071: TestException o = null;
072: o.data = -1;
073:
074: assert false : "should never get here";
075: }
076:
077: static void testNPECall() {
078: TestException o = null;
079: o.foo();
080:
081: assert false : "should never get here";
082: }
083:
084: static void testArrayIndexOutOfBoundsLow() {
085: int[] a = new int[10];
086: a[-1] = 0;
087:
088: assert false : "should never get here";
089: }
090:
091: static void testArrayIndexOutOfBoundsHigh() {
092: int[] a = new int[10];
093: a[10] = 0;
094:
095: assert false : "should never get here";
096: }
097:
098: static void testLocalHandler() {
099: try {
100: TestException o = null;
101: o.data = 0;
102: } catch (IllegalArgumentException iax) {
103: assert false : "should never get here";
104: } catch (NullPointerException npe) {
105: return;
106: } catch (Exception x) {
107: assert false : "should never get here";
108: }
109:
110: assert false : "should never get here";
111: }
112:
113: static void testCallerHandler() {
114: try {
115: bar();
116: } catch (Throwable t) {
117: return;
118: }
119:
120: assert false : "should never get here";
121: }
122:
123: }
|