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: getConstructor
030: */
031: public class ClassTestGetConstructor extends TestCase {
032:
033: /**
034: * The java.lang.Class class has no public constructotrs.
035: * NoSuchMethodException exception must be thrown.
036: *
037: */
038: public void test1() {
039: try {
040: Class.class.getConstructor(new Class[] { null });
041: } catch (NoSuchMethodException e) {
042: return;
043: }
044: fail("NoSuchMethodException exception expected");
045: }
046:
047: /**
048: * Public constructor which takes an integer parameter must be reflected.
049: *
050: */
051: public void test2() {
052: try {
053: Constructor c = Integer.class
054: .getConstructor(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: * Public constructor which takes a parameter of the java.lang.String type
065: * must be reflected.
066: *
067: */
068: public void test3() {
069: try {
070: Constructor c = Integer.class
071: .getConstructor(new Class[] { String.class });
072: assertNotNull("Unexpected null", c);
073: assertSame("Objects differ", String.class, c
074: .getParameterTypes()[0]);
075: } catch (Exception e) {
076: fail(e.toString());
077: }
078: }
079:
080: /**
081: * Verify parametrized type
082: */
083: public void test3_java5() {
084: try {
085: Constructor<Integer> c = Integer.class
086: .getConstructor(new Class[] { String.class });
087: assertNotNull("Unexpected null", c);
088: assertSame("Objects differ", String.class, c
089: .getParameterTypes()[0]);
090: } catch (Exception e) {
091: fail(e.toString());
092: }
093: }
094:
095: /**
096: * Default constructor which takes no parameters must be returned.
097: *
098: */
099: public void test4() {
100: try {
101: Constructor c = getClass().getConstructor(new Class[0]);
102: assertNotNull("Unexpected null", c);
103: assertEquals("array length:", 0,
104: c.getParameterTypes().length);
105: } catch (Exception e) {
106: fail(e.toString());
107: }
108: }
109:
110: /**
111: * Default constructor which takes no parameters must be returned.
112: *
113: */
114: public void test5() {
115: try {
116: Constructor c = Inner.class.getConstructor(new Class[0]);
117: assertNotNull("Unexpected null", c);
118: assertEquals("array length:", 0,
119: c.getParameterTypes().length);
120: } catch (Exception e) {
121: fail(e.toString());
122: }
123: }
124:
125: /**
126: * Public constructors of the super class must not be returned.
127: *
128: */
129: public void test6() {
130: try {
131: Inner.class.getConstructor(new Class[] { String.class });
132: } catch (NoSuchMethodException e) {
133: return;
134: }
135: fail("NoSuchMethodException exception expected");
136: }
137:
138: /**
139: * Only public constructors must be returned.
140: *
141: */
142: public void test7() {
143: try {
144: Inner2.class.getConstructor(new Class[] { null });
145: } catch (NoSuchMethodException e) {
146: return;
147: }
148: fail("NoSuchMethodException exception expected");
149: }
150:
151: public static class Inner extends Throwable {
152: private static final long serialVersionUID = 0L;
153: }
154:
155: private static class Inner2 {
156:
157: Inner2() {
158: }
159: }
160: }
|