001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Evgueni V. Brevnov, Roman S. Bushmanov
020: * @version $Revision$
021: */package java.lang;
022:
023: import junit.framework.TestCase;
024:
025: /**
026: * tested class: java.lang.Class
027: * tested method: forName
028: */
029: @SuppressWarnings(value={"all"})
030: public class ClassTestForName extends TestCase {
031:
032: /**
033: * This test case checks two cases. First, a class must be sucessfully
034: * loaded by bootstrup class loader. Second, this class instance must be the
035: * same as previously loaded.
036: */
037: public void test1() {
038: try {
039: final String name = "java.lang.Object";
040: Class c = Class.forName(name);
041: assertSame("Objects differ", Object.class, c);
042: } catch (Exception e) {
043: fail(e.toString());
044: }
045: }
046:
047: /**
048: * This test case checks two cases. First, a class must be sucessfully
049: * loaded by user-defined class loader. Second, this class instance must be
050: * the same as previously loaded.
051: */
052: public void test2() {
053: try {
054: final String name = ClassTestForName.class.getName();
055: Class c = Class.forName(name);
056: assertSame("Objects differ", ClassTestForName.class, c);
057: } catch (Exception e) {
058: fail(e.toString());
059: }
060: }
061:
062: /**
063: * This test case checks two cases. First, an array class must be
064: * sucessfully loaded by bootstrup class loader. Second, this class instance
065: * must be the same as previously loaded.
066: */
067: public void test3() {
068: try {
069: Object[] array = new Object[0];
070: final String name = array.getClass().getName();
071: Class c = Class.forName(name);
072: assertSame("Objects differ", array.getClass(), c);
073: } catch (Exception e) {
074: fail(e.toString());
075: }
076: }
077:
078: /**
079: * This test case checks whether a class loaded by forName() method is the
080: * same as a class loaded by loadClass() method through the same class
081: * loader.
082: */
083: public void test4() {
084: try {
085: final String name = getClass().getName() + "$InnerHelper";
086: ClassLoader cLoader = ClassLoader.getSystemClassLoader();
087: Class c1 = Class.forName(name, true, cLoader);
088: Class c2 = cLoader.loadClass(name);
089: assertSame("Objects differ", c1, c2);
090: } catch (Exception e) {
091: fail(e.toString());
092: }
093: }
094:
095: /**
096: * Simple reference type and array type must be loaded by the same class
097: * loader.
098: */
099: public void test5() {
100: try {
101: final String name = getClass().getName() + "$InnerHelper";
102: Class c1 = Class.forName(name);
103: Class c2 = Class.forName("[[L" + name + ";");
104: assertSame("Objects differ", c1.getClassLoader(), c2
105: .getClassLoader());
106: } catch (Exception e) {
107: fail(e.toString());
108: }
109: }
110:
111: /**
112: * class names must be delimited by dotes not by any over delimiter.
113: */
114: public void test6() {
115: try {
116: final String name = "java/lang/Object";
117: Class.forName(name);
118: } catch (ClassNotFoundException e) {
119: return;
120: }
121: fail("ClassNotFoundException exception expected");
122: }
123:
124: /**
125: * primitive types can not be loaded by forName() method.
126: */
127: public void test7() {
128: try {
129: final String name = Integer.TYPE.getName();
130: Class.forName(name);
131: } catch (ClassNotFoundException e) {
132: return;
133: }
134: fail("ClassNotFoundException exception expected");
135: }
136:
137: /**
138: * primitive types can not be loaded by forName() method.
139: */
140: public void test8() {
141: try {
142: Class c = new int[0].getClass();
143: assertSame("Objects differ", c, Class.forName(c.getName()));
144: } catch (Exception e) {
145: fail(e.toString());
146: }
147: }
148:
149: /**
150: * Regression test for HARMONY-887
151: */
152: public void testHARMONY887() throws ClassNotFoundException {
153: try {
154: Class.forName(null, true, ClassLoader
155: .getSystemClassLoader());
156: } catch (NullPointerException npe) {
157: return;
158: }
159: fail("NullPointerException exception expected");
160: }
161:
162: /**
163: * Helper class.
164: */
165: private static class InnerHelper {
166: }
167: }
|