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 Serguei S.Zapreyev
020: * @version $Revision$
021: *
022: * This MethodTest class ("Software") is furnished under license and may only be
023: * used or copied in accordance with the terms of that license.
024: *
025: */package java.lang.reflect;
026:
027: import junit.framework.TestCase;
028:
029: /*
030: * Created on 01.28.2006
031: */
032:
033: @SuppressWarnings(value={"all"})
034: public class ConstructorTest extends TestCase {
035:
036: /**
037: *
038: */
039: public void test_equals_Obj() {
040: class X {
041: public X() {
042: return;
043: }
044: }
045: class Y {
046: public Y() {
047: return;
048: }
049: }
050: try {
051: Constructor m1 = X.class
052: .getDeclaredConstructor(new Class[] { java.lang.reflect.ConstructorTest.class });
053: Constructor m2 = Y.class
054: .getDeclaredConstructor(new Class[] { java.lang.reflect.ConstructorTest.class });
055: assertTrue(
056: "Error1: lack of coincidence of the equal constructors is detected",
057: m1.equals(m1));
058: assertTrue(
059: "Error2: coincidence of the unequal constructors is detected",
060: !m1.equals(m2));
061: } catch (NoSuchMethodException e) {
062: fail("Error3: unfound constructor" + e.toString());
063: }
064: }
065:
066: /**
067: *
068: */
069: public void test_getDeclaringClass_V() {
070: class X {
071: public X() {
072: return;
073: }
074: }
075: new X();
076: try {
077: Constructor m = X.class
078: .getDeclaredConstructor(new Class[] { java.lang.reflect.ConstructorTest.class });
079: assertTrue("Error1", m.getDeclaringClass().getName()
080: .equals("java.lang.reflect.ConstructorTest$2X"));
081: } catch (NoSuchMethodException _) {
082: fail("Error2");
083: }
084: }
085:
086: /**
087: *
088: */
089: public void test_getExceptionTypes_V() {
090: class X {
091: class Y extends Throwable {
092: private static final long serialVersionUID = 0L;
093: };
094:
095: public X() throws Throwable, Y {
096: return;
097: }
098:
099: public X(Y a1) {
100: return;
101: }
102: }
103: try {
104: Constructor m = X.class
105: .getDeclaredConstructor(new Class[] { java.lang.reflect.ConstructorTest.class });
106: assertTrue(
107: "Error1",
108: (m.getExceptionTypes()[0].getName().equals(
109: "java.lang.reflect.ConstructorTest$3X$Y") || m
110: .getExceptionTypes()[0].getName().equals(
111: "java.lang.Throwable"))
112: && (m.getExceptionTypes()[1]
113: .getName()
114: .equals(
115: "java.lang.reflect.ConstructorTest$3X$Y") || m
116: .getExceptionTypes()[1].getName()
117: .equals("java.lang.Throwable")));
118: } catch (Exception e) {
119: fail("Error2" + e.toString());
120: }
121: try {
122: Constructor m = X.class
123: .getDeclaredConstructor(new Class[] {
124: java.lang.reflect.ConstructorTest.class,
125: X.Y.class });
126: assertTrue("Error3", m.getExceptionTypes().length == 0);
127: } catch (Exception e) {
128: fail("Error4" + e.toString());
129: }
130: }
131:
132: /**
133: *
134: */
135: public void test_getModifiers_V() {
136: class X {
137: public X() {
138: return;
139: }
140: }
141: new X();
142: try {
143: Constructor m = X.class
144: .getDeclaredConstructor(new Class[] { java.lang.reflect.ConstructorTest.class });
145: assertTrue("Error1", java.lang.reflect.Modifier.isPublic(m
146: .getModifiers()));
147: } catch (Exception e) {
148: fail("Error2" + e.toString());
149: }
150: }
151:
152: /**
153: *
154: */
155: public void test_getName_V() {
156: class X {
157: public X() {
158: return;
159: }
160:
161: public X(X a1) {
162: return;
163: }
164: }
165: new X();
166: Constructor af[] = X.class.getDeclaredConstructors();
167: assertEquals("num of ctors", 2, af.length);
168: for (int i = 0; i < af.length; i++) {
169: assertEquals("name",
170: "java.lang.reflect.ConstructorTest$5X", af[i]
171: .getName());
172: }
173: }
174:
175: /**
176: *
177: */
178: public void test_getParameterTypes_V() {
179: class X {
180: public X(boolean a1, byte a2, char a3, double a4, float a5,
181: int a6, long a7, short a8, X a9, ConstructorTest a10) {
182: return;
183: }
184: }
185: try {
186: Class ac[] = X.class.getDeclaredConstructor(
187: new Class[] {
188: java.lang.reflect.ConstructorTest.class,
189: boolean.class, byte.class, char.class,
190: double.class, float.class, int.class,
191: long.class, short.class, X.class,
192: ConstructorTest.class })
193: .getParameterTypes();
194: int res = 0;
195: for (int i = 0; i < ac.length; i++) {
196: if (ac[i].getName().equals("boolean"))
197: res += 1;
198: if (ac[i].getName().equals("byte"))
199: res += 10;
200: if (ac[i].getName().equals("char"))
201: res += 100;
202: if (ac[i].getName().equals("double"))
203: res += 1000;
204: if (ac[i].getName().equals("float"))
205: res += 10000;
206: if (ac[i].getName().equals("int"))
207: res += 100000;
208: if (ac[i].getName().equals("long"))
209: res += 1000000;
210: if (ac[i].getName().equals("short"))
211: res += 10000000;
212: if (ac[i].getName().equals(
213: "java.lang.reflect.ConstructorTest$6X"))
214: res += 100000000;
215: if (ac[i].getName().equals(
216: "java.lang.reflect.ConstructorTest"))
217: res += 1000000000;
218: }
219: assertTrue("Error1", res == 2111111111);
220: } catch (Exception e) {
221: fail("Error2: " + e.toString());
222: }
223: }
224:
225: /**
226: *
227: */
228: public void test_hashCode_V() {
229: class X {
230: public X(X a9) {
231: return;
232: }
233: }
234: try {
235: Constructor m = X.class.getDeclaredConstructor(new Class[] {
236: java.lang.reflect.ConstructorTest.class, X.class });
237: assertTrue("Error1", m.hashCode() == m.getDeclaringClass()
238: .getName().hashCode());
239: } catch (NoSuchMethodException _) {
240: fail("Error2");
241: }
242: }
243:
244: /**
245: *
246: */
247: public void test_newInstance_Obj() {
248: class X1 {
249: public X1() {
250: return;
251: }
252:
253: public X1(X1 a9) {
254: return;
255: }
256: }
257: X1 x = new X1(new X1());
258: try {
259: Constructor m = X1.class
260: .getDeclaredConstructor(new Class[] {
261: java.lang.reflect.ConstructorTest.class,
262: X1.class });
263: Object o = m
264: .newInstance(new Object[] {
265: new java.lang.reflect.ConstructorTest(),
266: new X1() });
267: assertTrue("Error1", o instanceof X1);
268: } catch (Exception e) {
269: fail("Error2: " + e.toString());
270: }
271: }
272:
273: /**
274: *
275: */
276: public void test_toString_Obj() {
277: class X {
278: public X() {
279: return;
280: }
281:
282: public X(X a9) {
283: return;
284: }
285: }
286: try {
287: Constructor m = X.class.getDeclaredConstructor(new Class[] {
288: java.lang.reflect.ConstructorTest.class, X.class });
289: assertTrue(
290: "Error1 " + m.toString(),
291: m
292: .toString()
293: .equals(
294: "public java.lang.reflect.ConstructorTest$8X(java.lang.reflect.ConstructorTest,java.lang.reflect.ConstructorTest$8X)"));
295: } catch (Exception e) {
296: fail("Error2: " + e.toString());
297: }
298: }
299: }
|