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 ClassTestGetDeclaresClasses 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 = ClassTestGetDeclaresClasses.class
041: .getDeclaredClasses();
042: HashSet<Class> set = new HashSet<Class>(Arrays.asList(cs));
043: assertTrue("Helper1 has not been found", set
044: .contains(Helper1.class));
045: assertTrue("Helper2 has not been found", set
046: .contains(Helper2.class));
047: assertTrue("Helper3 has not been found", set
048: .contains(Helper3.class));
049: assertTrue("Helper4 has not been found", set
050: .contains(Helper4.class));
051: assertFalse("Helper1.Inner1 has not been found", 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("null expected", cs);
063: assertEquals("array length:", 1, cs.length);
064: assertSame("objects differ", Helper3.Inner2.class, cs[0]);
065: }
066:
067: /**
068: * An empty array must be returned for the classes that represent
069: * primitive types.
070: *
071: */
072: public void test3() {
073: Class[] cs = Void.TYPE.getDeclaredClasses();
074: assertNotNull("null expected", cs);
075: assertEquals("array length:", 0, cs.length);
076: }
077:
078: /**
079: * An empty array must be returned for classes that represent arrays.
080: *
081: */
082: public void test4() {
083: Class[] cs = new Object[0].getClass().getDeclaredClasses();
084: assertNotNull("null expected", cs);
085: assertEquals("array length:", 0, cs.length);
086: }
087:
088: public class Helper1 {
089: class Inner1 {
090: }
091: }
092:
093: protected interface Helper2 {
094: }
095:
096: class Helper3 extends Helper1 {
097: class Inner2 {
098: }
099: }
100:
101: private class Helper4 {
102: }
103: }
|