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:
020: /**
021: * This is a raw test class, which produces AssertionErrors for all
022: * cases we want to catch. Make double-sure we don't refer to any
023: * JPF class in here, or we start to check JPF recursively.
024: * To turn this into a Junt test, you have to write a wrapper
025: * TestCase, which just calls the testXX() methods.
026: * The Junit test cases run JPF.main explicitly by means of specifying
027: * which test case to run, but be aware of this requiring proper
028: * state clean up in JPF !
029: *
030: * KEEP IT SIMPLE - it's already bad enough we have to mimic unit tests
031: * by means of system tests (use whole JPF to check if it works), we don't
032: * want to make the observer problem worse by means of enlarging the scope
033: * JPF has to look at
034: *
035: * Note that we don't use assert expressions, because those would already
036: * depend on working java.lang.Class APIs
037: */package gov.nasa.jpf.jvm;
038:
039: /**
040: * test of java.lang.Class API
041: */
042: public class TestJavaLangClass {
043: static String clsName = "gov.nasa.jpf.jvm.TestJavaLangClass";
044:
045: int data = 42; // that creates a default ctor for our newInstance test
046:
047: public static void main(String[] args) {
048: TestJavaLangClass t = new TestJavaLangClass();
049:
050: if (args.length > 0) {
051: // just run the specified tests
052: for (int i = 0; i < args.length; i++) {
053: String func = args[i];
054:
055: // note that we don't use reflection here because this would
056: // blow up execution/test scope under JPF
057: if ("testClassForName".equals(func)) {
058: t.testClassForName();
059: } else if ("testClassField".equals(func)) {
060: t.testClassField();
061: } else if ("testGetClass".equals(func)) {
062: t.testGetClass();
063: } else if ("testIdentity".equals(func)) {
064: t.testIdentity();
065: } else if ("testNewInstance".equals(func)) {
066: t.testNewInstance();
067: } else {
068: throw new IllegalArgumentException(
069: "unknown test function");
070: }
071: }
072: } else {
073: // that's mainly for our standalone test verification
074: t.testClassForName();
075: t.testClassField();
076: t.testGetClass();
077: t.testIdentity();
078: t.testNewInstance();
079: }
080: }
081:
082: public void testClassField() {
083: Class clazz = TestJavaLangClass.class;
084:
085: if (clazz == null) {
086: throw new RuntimeException("class field not set");
087: }
088:
089: if (!clsName.equals(clazz.getName())) {
090: throw new RuntimeException(
091: "getName() wrong for class field");
092: }
093: }
094:
095: /**************************** tests **********************************/
096: public void testClassForName() {
097: Class clazz = null;
098:
099: try {
100: clazz = Class.forName(clsName);
101: } catch (Exception x) {
102: }
103:
104: if (clazz == null) {
105: throw new RuntimeException(
106: "Class.forName() returned null object");
107: }
108:
109: if (!clsName.equals(clazz.getName())) {
110: throw new RuntimeException(
111: "getName() wrong for Class.forName() acquired class");
112: }
113: }
114:
115: public void testGetClass() {
116: Class clazz = this .getClass();
117:
118: if (clazz == null) {
119: throw new RuntimeException("Object.getClass() failed");
120: }
121:
122: if (!clsName.equals(clazz.getName())) {
123: throw new RuntimeException(
124: "getName() wrong for getClass() acquired class");
125: }
126: }
127:
128: public void testIdentity() {
129: Class clazz1 = null;
130: Class clazz2 = TestJavaLangClass.class;
131: Class clazz3 = this .getClass();
132:
133: try {
134: clazz1 = Class.forName(clsName);
135: } catch (Exception x) {
136: }
137:
138: if (clazz1 != clazz2) {
139: throw new RuntimeException(
140: "Class.forName() and class field not identical");
141: }
142:
143: if (clazz2 != clazz3) {
144: throw new RuntimeException(
145: "Object.getClass() and class field not identical");
146: }
147: }
148:
149: public void testNewInstance() {
150: try {
151: Class clazz = TestJavaLangClass.class;
152: TestJavaLangClass o = (TestJavaLangClass) clazz
153: .newInstance();
154:
155: if (o.data != 42) {
156: throw new RuntimeException(
157: "Class.newInstance() failed to call default ctor");
158: }
159: } catch (Exception e) {
160: throw new RuntimeException(
161: "Class.newInstance() caused exception: " + e);
162: }
163: }
164: }
|