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: getDeclaredConstructor
030: */
031: public class ClassTestGetDeclaredConstructor extends TestCase {
032:
033: /**
034: * Public constructor which takes an integer parameter must be reflected.
035: */
036: public void test1() {
037: try {
038: Constructor c = Integer.class
039: .getDeclaredConstructor(new Class[] { Integer.TYPE });
040: assertNotNull("unexpected null", c);
041: assertSame("objects differ:", Integer.TYPE, c
042: .getParameterTypes()[0]);
043: } catch (Exception e) {
044: fail(e.toString());
045: }
046: }
047:
048: /**
049: * Verify parametrized type
050: */
051: public void test1_java5() {
052: try {
053: Constructor<Integer> c = Integer.class
054: .getDeclaredConstructor(new Class[] { Integer.TYPE });
055: assertNotNull("unexpected null", c);
056: assertSame("objects differ:", Integer.TYPE, c
057: .getParameterTypes()[0]);
058: } catch (Exception e) {
059: fail(e.toString());
060: }
061: }
062:
063: /**
064: * Only default constructor which takes no parameters must be returned.
065: */
066: public void test2() {
067: try {
068: Constructor c = Inner.class
069: .getDeclaredConstructor(new Class[0]);
070: assertNotNull("unexpected null", c);
071: assertEquals("array length:", 0,
072: c.getParameterTypes().length);
073: } catch (Exception e) {
074: fail(e.toString());
075: }
076: }
077:
078: /**
079: * Public constructors of the super class must not be returned.
080: */
081: public void test3() {
082: try {
083: Inner.class
084: .getDeclaredConstructor(new Class[] { String.class });
085: } catch (NoSuchMethodException e) {
086: return;
087: }
088: fail("NoSuchMethodException exception expected");
089:
090: }
091:
092: /**
093: * Private constructor which takes no parameters must be reflected.
094: */
095: public void test4() {
096: try {
097: Constructor c = Inner2.class
098: .getDeclaredConstructor((Class[]) null);
099: assertNotNull("unexpected null", c);
100: assertEquals("array length:", 0,
101: c.getParameterTypes().length);
102: } catch (Exception e) {
103: fail(e.toString());
104: }
105: }
106:
107: /**
108: * Private constructor which takes no parameters must be reflected.
109: */
110: public void test5() {
111: try {
112: Constructor c = PrivateConstructor.class
113: .getDeclaredConstructor((Class[]) null);
114: assertNotNull("unexpected null", c);
115: assertEquals("array length:", 0,
116: c.getParameterTypes().length);
117: } catch (Exception e) {
118: fail(e.toString());
119: }
120: }
121:
122: private static class Inner extends Throwable {
123: private static final long serialVersionUID = 0L;
124: }
125:
126: public static class Inner2 {
127:
128: private Inner2() {
129: }
130: }
131:
132: }
|