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 MethodTest extends TestCase {
035:
036: /**
037: *
038: */
039: public void test_equals_Obj() {
040: class X {
041: public int m() {
042: return 777;
043: }
044: }
045: class Y {
046: public int m() {
047: return 777;
048: }
049: }
050: try {
051: Method m1 = X.class.getDeclaredMethod("m", (Class[]) null);
052: Method m2 = Y.class.getDeclaredMethod("m", (Class[]) null);
053: assertEquals("Error1: equal methods should coincide", m1,
054: m1);
055: assertTrue(
056: "Error2: coincidence of the unequal methods is detected",
057: !m1.equals(m2));
058: } catch (NoSuchMethodException _) {
059: fail("Error3: unfound method");
060: }
061: }
062:
063: /**
064: *
065: */
066: public void test_getDeclaringClass_V() {
067: class X {
068: public int m() {
069: return 777;
070: }
071: }
072: new X();
073: try {
074: Method m = X.class.getDeclaredMethod("m", (Class[]) null);
075: assertEquals("Error1", "java.lang.reflect.MethodTest$2X", m
076: .getDeclaringClass().getName());
077: } catch (NoSuchMethodException _) {
078: fail("Error2");
079: }
080: }
081:
082: /**
083: *
084: */
085: public void test_getExceptionTypes_V() {
086: class X {
087: class Y extends Throwable {
088: private static final long serialVersionUID = 0L;
089: };
090:
091: public int m() throws Throwable, Y {
092: return 777;
093: }
094:
095: public int m2() {
096: return 777;
097: }
098: }
099: new X();
100: try {
101: Method m = X.class.getDeclaredMethod("m", (Class[]) null);
102: assertTrue("Error1", (m.getExceptionTypes()[0].getName()
103: .equals("java.lang.reflect.MethodTest$3X$Y") || m
104: .getExceptionTypes()[0].getName().equals(
105: "java.lang.Throwable"))
106: && (m.getExceptionTypes()[1].getName().equals(
107: "java.lang.reflect.MethodTest$3X$Y") || m
108: .getExceptionTypes()[1].getName().equals(
109: "java.lang.Throwable")));
110: } catch (Exception e) {
111: fail("Error2" + e.toString());
112: }
113: try {
114: Method m = X.class.getDeclaredMethod("m2", (Class[]) null);
115: assertEquals("Error3", 0, m.getExceptionTypes().length);
116: } catch (Exception e) {
117: fail("Error4" + e.toString());
118: }
119: }
120:
121: /**
122: *
123: */
124: public void test_getModifiers_V() {
125: class X {
126: public int m() {
127: return 777;
128: }
129:
130: final int m2() {
131: return 777;
132: }
133: }
134: new X();
135: try {
136: Method m = X.class.getDeclaredMethod("m", (Class[]) null);
137: assertTrue("Error1", java.lang.reflect.Modifier.isPublic(m
138: .getModifiers()));
139: m = X.class.getDeclaredMethod("m2", (Class[]) null);
140: assertTrue("Error2", java.lang.reflect.Modifier.isFinal(m
141: .getModifiers()));
142: } catch (Exception e) {
143: fail("Error3" + e.toString());
144: }
145: }
146:
147: /**
148: *
149: */
150: public void test_getName_V() {
151: class X {
152: public int first() {
153: return 777;
154: }
155:
156: final int second() {
157: return 777;
158: }
159: }
160: new X();
161: try {
162: Method af[] = X.class.getDeclaredMethods();
163: int res = 0;
164: for (int i = 0; i < af.length; i++) {
165: if (af[i].getName().equals("first")
166: || af[i].getName().equals("second"))
167: res++;
168: }
169: assertTrue("Error1", res == 2);
170: } catch (Exception e) {
171: fail("Error2" + e.toString());
172: }
173: }
174:
175: /**
176: *
177: */
178: public void test_getParameterTypes_V() {
179: class X {
180: public int m(boolean a1, byte a2, char a3, double a4,
181: float a5, int a6, long a7, short a8, X a9,
182: MethodTest a10) {
183: return 777;
184: }
185: }
186: new X();
187: try {
188: Class ac[] = X.class.getDeclaredMethod(
189: "m",
190: new Class[] { boolean.class, byte.class,
191: char.class, double.class, float.class,
192: int.class, long.class, short.class,
193: X.class, MethodTest.class })
194: .getParameterTypes();
195: int res = 0;
196: for (int i = 0; i < ac.length; i++) {
197: if (ac[i].getName().equals("boolean"))
198: res += 1;
199: if (ac[i].getName().equals("byte"))
200: res += 10;
201: if (ac[i].getName().equals("char"))
202: res += 100;
203: if (ac[i].getName().equals("double"))
204: res += 1000;
205: if (ac[i].getName().equals("float"))
206: res += 10000;
207: if (ac[i].getName().equals("int"))
208: res += 100000;
209: if (ac[i].getName().equals("long"))
210: res += 1000000;
211: if (ac[i].getName().equals("short"))
212: res += 10000000;
213: if (ac[i].getName().equals(
214: "java.lang.reflect.MethodTest$6X"))
215: res += 100000000;
216: if (ac[i].getName().equals(
217: "java.lang.reflect.MethodTest"))
218: res += 1000000000;
219: }
220: assertEquals("Error1", 1111111111, res);
221: } catch (Exception e) {
222: fail("Error2: " + e.toString());
223: }
224: }
225:
226: /**
227: *
228: */
229: public void test_getReturnType_V() {
230: class X {
231: public X m(X a9) {
232: return a9;
233: }
234: }
235: new X();
236: try {
237: assertEquals("Error1", "java.lang.reflect.MethodTest$7X",
238: X.class.getDeclaredMethod("m",
239: new Class[] { X.class }).getReturnType()
240: .getName());
241: } catch (Exception e) {
242: fail("Error2: " + e.toString());
243: }
244: }
245:
246: /**
247: *
248: */
249: public void test_hashCode_V() {
250: class X {
251: public X first(X a9) {
252: return a9;
253: }
254: }
255: try {
256: Method m = X.class.getDeclaredMethod("first",
257: new Class[] { X.class });
258: assertEquals("Error1", m.getDeclaringClass().getName()
259: .hashCode()
260: ^ m.getName().hashCode(), m.hashCode());
261: } catch (NoSuchMethodException _) {
262: fail("Error2");
263: }
264: }
265:
266: /**
267: *
268: */
269: public void test_invoke_Obj_Obj() {
270:
271: class X {
272: public X first(X a9) {
273: return a9;
274: }
275: }
276: X x = new X();
277: try {
278: Method m = X.class.getDeclaredMethod("first",
279: new Class[] { X.class });
280: Object o = m.invoke(x, new Object[] { new X() });
281: assertTrue("Error1", o instanceof X);
282: } catch (Exception e) {
283: fail("Error2: " + e.toString());
284: }
285: }
286:
287: /**
288: *
289: */
290: public void test_invoke_Obj_Obj_2() {
291: int sz = 500;
292: Object obj = null;
293: Class cls = null;
294: Method m;
295: try {
296: cls = Class.forName("java.lang.reflect.AuxiliaryClass");
297: obj = cls.newInstance();
298: } catch (Throwable e) {
299: e.printStackTrace();
300: fail("Test failed during class creation: Unexpected error: "
301: + e);
302: }
303: int pp = 0;
304: for (int j = 0; j < sz; j++) {
305: try {
306: m = cls.getMethod("get", (Class[]) null);
307: int ans = ((Integer) (m.invoke(obj, (Object[]) null)))
308: .intValue();
309: fail("Expected InvocationTargetException was not thrown: ans = "
310: + ans
311: + " step: "
312: + j
313: + " method name: "
314: + m.getDeclaringClass().getName()
315: + "."
316: + m.getName());
317: } catch (InvocationTargetException e) {
318: // expected
319: } catch (Throwable e) {
320: e.printStackTrace();
321: fail("Test failed: Unexpected error: " + e);
322: }
323: }
324: }
325:
326: /**
327: *
328: */
329: public void test_toString_Obj() {
330: class X {
331: public X first(X a9) {
332: return a9;
333: }
334: }
335: new X();
336: try {
337: Method m = X.class.getDeclaredMethod("first",
338: new Class[] { X.class });
339: assertEquals("Error1 ",
340: "public java.lang.reflect.MethodTest$10X "
341: + "java.lang.reflect.MethodTest$10X.first("
342: + "java.lang.reflect.MethodTest$10X)", m
343: .toString());
344: } catch (Exception e) {
345: fail("Error2: " + e.toString());
346: }
347: }
348: }
|