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.util.Arrays;
024: import java.util.HashSet;
025:
026: import junit.framework.TestCase;
027:
028: /**
029: * tested class: java.lang.Class
030: * tested method: getDeclaredClasses
031: */
032: public class ClassTestGetDeclaredClasses extends TestCase {
033:
034: /**
035: * The getDeclaredClasses() method must return all inner classes and
036: * interfaces including protected, package private and private members.
037: *
038: */
039: public void test1() {
040: Class[] cs = ClassTestGetDeclaredClasses.class
041: .getDeclaredClasses();
042: HashSet<Class> set = new HashSet<Class>(Arrays.asList(cs));
043: assertTrue("Helper1 is not in the list", set
044: .contains(Helper1.class));
045: assertTrue("Helper2 is not in the list", set
046: .contains(Helper2.class));
047: assertTrue("Helper3 is not in the list", set
048: .contains(Helper3.class));
049: assertTrue("Helper4 is not in the list", set
050: .contains(Helper4.class));
051: assertFalse("Helper1.Inner1 is not in the list", set
052: .contains(Helper1.Inner1.class));
053: }
054:
055: /**
056: * The members declared in the super class must not be reflected by
057: * getDeclaredClasses() method.
058: *
059: */
060: public void test2() {
061: Class[] cs = Helper3.class.getDeclaredClasses();
062: assertNotNull("List of classes should not be null", cs);
063: assertEquals("There should be one class in the list", 1,
064: cs.length);
065: assertSame("Incorrect class returned", Helper3.Inner2.class,
066: cs[0]);
067: }
068:
069: /**
070: * An empty array must be returned for the classes that represent
071: * primitive types.
072: *
073: */
074: public void test3() {
075: Class[] cs = Void.TYPE.getDeclaredClasses();
076: assertNotNull("Array should not be null");
077: assertEquals("Array must be empty", 0, cs.length);
078: }
079:
080: /**
081: * An empty array must be returned for classes that represent arrays.
082: *
083: */
084: public void test4() {
085: Class[] cs = new Object[0].getClass().getDeclaredClasses();
086: assertNotNull("Array should not be null", cs);
087: assertEquals("Array must be empty", 0, cs.length);
088: }
089:
090: public class Helper1 {
091: class Inner1 {
092: }
093: }
094:
095: protected interface Helper2 {
096: }
097:
098: class Helper3 extends Helper1 {
099: class Inner2 {
100: }
101: }
102:
103: private class Helper4 {
104: }
105: }
|