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 java.io.Serializable;
024:
025: import junit.framework.TestCase;
026:
027: /**
028: * tested class: java.lang.Class
029: * tested method: isAssignableFrom
030: */
031: public class ClassTestIsAssignableFrom extends TestCase {
032:
033: /**
034: * if argument is null an NullPoinerException should be thrown.
035: */
036: public void test1() {
037: try {
038: getClass().isAssignableFrom(null);
039: } catch (NullPointerException e) {
040: return;
041: }
042: fail("NullPointerException exception expected");
043: }
044:
045: /**
046: * checks identity primitive conversion.
047: */
048: public void test2() {
049: assertTrue("Assert 0:", Integer.TYPE
050: .isAssignableFrom(int.class));
051: assertTrue("Assert 1:", int.class
052: .isAssignableFrom(Integer.TYPE));
053: }
054:
055: /**
056: * checks widening refernce conversion for primitive type.
057: */
058: public void test3() {
059: assertFalse(Object.class.isAssignableFrom(Character.TYPE));
060: }
061:
062: /**
063: * Classes that represent primitive types aren't assignable from
064: * corresponding wrapper types as well as vice versa.
065: */
066: public void test4() {
067: assertFalse("Assert 0:", Integer.class
068: .isAssignableFrom(Integer.TYPE));
069: assertFalse("Assert 1:", Integer.TYPE
070: .isAssignableFrom(Integer.class));
071: }
072:
073: /**
074: * The Serializable interface is super interface of the Boolean class. So it
075: * should be assignable from the Boolean class. But not vice versa.
076: */
077: public void test5() {
078: assertTrue("Assert 0:", Serializable.class
079: .isAssignableFrom(Boolean.class));
080: assertFalse("Assert 1:", Boolean.class
081: .isAssignableFrom(Serializable.class));
082: }
083:
084: /**
085: * Each array has the Object class as its super class.
086: */
087: public void test6() {
088: assertTrue(Object.class.isAssignableFrom(new int[0].getClass()));
089: }
090:
091: /**
092: * The isAssignable() method should not perform widening primitive
093: * conversion.
094: */
095: public void test7() {
096: assertFalse("Assert 0:", double.class
097: .isAssignableFrom(int.class));
098: assertFalse("Assert 1:", int.class
099: .isAssignableFrom(double.class));
100: }
101:
102: /**
103: * if a class represents array the isAssignable() method should work with
104: * array's components. But this method should not perform widening primitive
105: * conversion.
106: */
107: public void test8() {
108: assertFalse("Assert 0:", new double[0].getClass()
109: .isAssignableFrom(new int[0].getClass()));
110: assertFalse("Assert 1:", new int[0].getClass()
111: .isAssignableFrom(new double[0].getClass()));
112: }
113:
114: /**
115: * if a class represents array the isAssignable() method should work with
116: * array's components. So the Object[] class is assignable from the String[]
117: * class as well as the Object class is assignable from the String class.
118: */
119: public void test9() {
120: assertTrue(new Object[0].getClass().isAssignableFrom(
121: new String[0].getClass()));
122: }
123:
124: /**
125: * The Object class is assignable from any reference type.
126: */
127: public void test10() {
128: assertTrue(Object.class.isAssignableFrom(getClass()));
129: }
130: }
|