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.lang.reflect.Constructor;
024:
025: import junit.framework.TestCase;
026:
027: /**
028: * tested class: java.lang.Class
029: * tested method: getDeclaredConstructors
030: */
031: public class ClassTestGetDeclaredConstructors extends TestCase {
032:
033: /**
034: * The primitive types don't declare constructors.
035: */
036: public void test1() {
037: Constructor[] cs = Integer.TYPE.getDeclaredConstructors();
038: assertNotNull("unexpected null", cs);
039: assertEquals("array length:", 0, cs.length);
040: }
041:
042: /**
043: * The arrays don't declare constructors.
044: */
045: public void test2() {
046: Constructor[] cs = new int[0].getClass()
047: .getDeclaredConstructors();
048: assertNotNull("unexpected null", cs);
049: assertEquals("array length:", 0, cs.length);
050: }
051:
052: /**
053: * Only default constructor which takes no parameters must be returned.
054: * Constructors of the super class must not be returned.
055: */
056: public void test3() {
057: Constructor[] cs = ClassTestGetDeclaredConstructors.class
058: .getDeclaredConstructors();
059: assertNotNull("unexpected null", cs);
060: assertEquals("array length:", 1, cs.length);
061: assertEquals("array length:", 0,
062: cs[0].getParameterTypes().length);
063: }
064:
065: /**
066: * Only default constructor which takes no parameters must be returned.
067: * Constructors of the super class must not be returned.
068: */
069: public void test4() {
070: Constructor[] cs = Inner.class.getDeclaredConstructors();
071: assertNotNull("unexpected null", cs);
072: assertEquals("Constructors length:", 1, cs.length);
073: assertEquals("Parameters length:", 0,
074: cs[0].getParameterTypes().length);
075: }
076:
077: /**
078: * The getDeclaredConstructors() method must return protected, package
079: * private and private constructors.
080: */
081: public void test5() {
082: Constructor[] cs = Inner2.class.getDeclaredConstructors();
083: assertEquals("Constructors length:", 2, cs.length);
084: Class[] args = cs[0].getParameterTypes();
085: if (args.length == 0) {
086: args = cs[1].getParameterTypes();
087: assertEquals("Assert 0: args length:", 1, args.length);
088: assertSame("objects differ:", String.class, args[0]);
089: } else {
090: assertEquals("Assert 1: args length:", 1, args.length);
091: assertSame("objects differ:", String.class, args[0]);
092: args = cs[1].getParameterTypes();
093: assertEquals("Assert 2: args length:", 0, args.length);
094: }
095: }
096:
097: /**
098: * The interfaces can not define constructors.
099: */
100: public void test6() {
101: Constructor[] cs = Inner3.class.getDeclaredConstructors();
102: assertNotNull("unexpected null", cs);
103: assertEquals("array length:", 0, cs.length);
104: }
105:
106: static class Inner extends Throwable {
107: private static final long serialVersionUID = 0L;
108: }
109:
110: public static class Inner2 {
111:
112: public Inner2(String s) {
113: }
114:
115: private Inner2() {
116: }
117: }
118:
119: public static interface Inner3 {
120: }
121: }
|