KickJava   Java API By Example, From Geeks To Geeks.

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


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

20 /**
21  * This is a raw test class, which produces AssertionErrors for all
22  * cases we want to catch. Make double-sure we don't refer to any
23  * JPF class in here, or we start to check JPF recursively.
24  * To turn this into a Junt test, you have to write a wrapper
25  * TestCase, which just calls the testXX() methods.
26  * The Junit test cases run JPF.main explicitly by means of specifying
27  * which test case to run, but be aware of this requiring proper
28  * state clean up in JPF !
29  *
30  * KEEP IT SIMPLE - it's already bad enough we have to mimic unit tests
31  * by means of system tests (use whole JPF to check if it works), we don't
32  * want to make the observer problem worse by means of enlarging the scope
33  * JPF has to look at
34  *
35  * Note that we don't use assert expressions, because those would already
36  * depend on working java.lang.Class APIs
37  */

38 package gov.nasa.jpf.jvm;
39
40 /**
41  * test of java.lang.Class API
42  */

43 public class TestJavaLangClass {
44   static String JavaDoc clsName = "gov.nasa.jpf.jvm.TestJavaLangClass";
45
46   int data = 42; // that creates a default ctor for our newInstance test
47

48   public static void main (String JavaDoc[] args) {
49     TestJavaLangClass t = new TestJavaLangClass();
50
51     if (args.length > 0) {
52       // just run the specified tests
53
for (int i = 0; i < args.length; i++) {
54         String JavaDoc func = args[i];
55
56         // note that we don't use reflection here because this would
57
// blow up execution/test scope under JPF
58
if ("testClassForName".equals(func)) {
59           t.testClassForName();
60         } else if ("testClassField".equals(func)) {
61           t.testClassField();
62         } else if ("testGetClass".equals(func)) {
63           t.testGetClass();
64         } else if ("testIdentity".equals(func)) {
65           t.testIdentity();
66         } else if ("testNewInstance".equals(func)) {
67           t.testNewInstance();
68          } else {
69           throw new IllegalArgumentException JavaDoc("unknown test function");
70         }
71       }
72     } else {
73       // that's mainly for our standalone test verification
74
t.testClassForName();
75       t.testClassField();
76       t.testGetClass();
77       t.testIdentity();
78       t.testNewInstance();
79     }
80   }
81
82   public void testClassField () {
83     Class JavaDoc clazz = TestJavaLangClass.class;
84
85     if (clazz == null) {
86       throw new RuntimeException JavaDoc("class field not set");
87     }
88
89     if (!clsName.equals(clazz.getName())) {
90       throw new RuntimeException JavaDoc("getName() wrong for class field");
91     }
92   }
93
94   /**************************** tests **********************************/
95   public void testClassForName () {
96     Class JavaDoc clazz = null;
97
98     try {
99       clazz = Class.forName(clsName);
100     } catch (Exception JavaDoc x) {
101     }
102
103     if (clazz == null) {
104       throw new RuntimeException JavaDoc("Class.forName() returned null object");
105     }
106
107     if (!clsName.equals(clazz.getName())) {
108       throw new RuntimeException JavaDoc(
109             "getName() wrong for Class.forName() acquired class");
110     }
111   }
112
113   public void testGetClass () {
114     Class JavaDoc clazz = this.getClass();
115
116     if (clazz == null) {
117       throw new RuntimeException JavaDoc("Object.getClass() failed");
118     }
119
120     if (!clsName.equals(clazz.getName())) {
121       throw new RuntimeException JavaDoc(
122             "getName() wrong for getClass() acquired class");
123     }
124   }
125
126   public void testIdentity () {
127     Class JavaDoc clazz1 = null;
128     Class JavaDoc clazz2 = TestJavaLangClass.class;
129     Class JavaDoc clazz3 = this.getClass();
130
131     try {
132       clazz1 = Class.forName(clsName);
133     } catch (Exception JavaDoc x) {
134     }
135
136     if (clazz1 != clazz2) {
137       throw new RuntimeException JavaDoc(
138             "Class.forName() and class field not identical");
139     }
140
141     if (clazz2 != clazz3) {
142       throw new RuntimeException JavaDoc(
143             "Object.getClass() and class field not identical");
144     }
145   }
146   
147   public void testNewInstance () {
148     try {
149       Class JavaDoc clazz = TestJavaLangClass.class;
150       TestJavaLangClass o = (TestJavaLangClass) clazz.newInstance();
151       
152       if (o.data != 42) {
153         throw new RuntimeException JavaDoc(
154           "Class.newInstance() failed to call default ctor");
155       }
156     } catch (Exception JavaDoc e) {
157       throw new RuntimeException JavaDoc(
158           "Class.newInstance() caused exception: " + e);
159     }
160   }
161 }
Popular Tags