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.Method;
024:
025: import junit.framework.TestCase;
026:
027: /**
028: * tested class: java.lang.Class
029: * tested method: getMethod
030: */
031: public class ClassTestGetMethod extends TestCase {
032:
033: /**
034: * "method1" method has public accessibility so it should be accessible
035: * through Class.getMethod() method.
036: */
037: public void test1() {
038: try {
039: Method m = A.class.getMethod("method1", (Class[]) null);
040: assertEquals("incorrect name", "method1", m.getName());
041: assertSame("objects differ", A.class, m.getDeclaringClass());
042: } catch (Exception e) {
043: fail(e.toString());
044: }
045: }
046:
047: /**
048: * "method2" method has protected accessibility so it shouldn't be returned
049: * by Class.getMethod() method. NoSuchMethodException exception expected.
050: */
051: public void test2() {
052: try {
053: A.class.getMethod("method2", (Class[]) null);
054: } catch (NoSuchMethodException e) {
055: return;
056: }
057: fail("NoSuchMethodException exception expected");
058: }
059:
060: /**
061: * Attempt to retrieve public method declared in the super class.
062: */
063: public void test3() {
064: try {
065: String methodName = "wait";
066: Method m = getClass().getMethod(methodName, (Class[]) null);
067: assertEquals("incorrect name", methodName, m.getName());
068: assertSame("objects differ", Object.class, m
069: .getDeclaringClass());
070: } catch (Exception e) {
071: fail(e.toString());
072: }
073: }
074:
075: /**
076: * if name is null NullPointerException should be thrown.
077: */
078: public void test4() {
079: try {
080: String methodName = null;
081: getClass().getMethod(methodName, (Class[]) null);
082: } catch (NullPointerException e) {
083: return;
084: } catch (NoSuchMethodException e) {
085: }
086: fail("NullPointerException exception expected");
087: }
088:
089: /**
090: * NoSuchMethodException should be thrown if name is equal to " <cinit>"
091: */
092: public void test5() {
093: try {
094: String methodName = "<cinit>";
095: A.class.getMethod(methodName, (Class[]) null);
096: } catch (NoSuchMethodException e) {
097: return;
098: }
099: fail("NoSuchMethodException exception expected");
100: }
101:
102: /**
103: * NoSuchMethodException should be thrown if name is equal to " <init>"
104: */
105: public void test6() {
106: try {
107: String methodName = "<init>";
108: A.class.getMethod(methodName, (Class[]) null);
109: } catch (NoSuchMethodException e) {
110: return;
111: }
112: fail("NoSuchMethodException exception expected");
113: }
114:
115: /**
116: * if a class contains the method with the same name and parameters as its
117: * super class then the method of this class should be reflected.
118: */
119: public void test7() {
120: try {
121: String methodName = "toString";
122: Method m = A.class.getMethod(methodName, new Class[] {});
123: assertEquals("incorrect name", methodName, m.getName());
124: assertSame("objects differ", A.class, m.getDeclaringClass());
125: } catch (Exception e) {
126: fail(e.toString());
127: }
128: }
129:
130: /**
131: * if the super class of this class contains the method with the same
132: * descriptor as super interface of this class then the method of the super
133: * class should be reflected.
134: */
135: public void test8() {
136: try {
137: String methodName = "equals";
138: Method m = A.class.getMethod(methodName,
139: new Class[] { Object.class });
140: assertEquals("incorrect name", methodName, m.getName());
141: assertSame("objects differ", Object.class, m
142: .getDeclaringClass());
143: } catch (Exception e) {
144: fail(e.toString());
145: }
146: }
147:
148: /**
149: * the getMethod() method should thow the NoSuchMethodException even if the
150: * arguments contain nulls.
151: */
152: public void testBug537() {
153: final String methodName = "testBug537";
154: try {
155: getClass().getMethod(methodName, new Class[] { null });
156: } catch (NoSuchMethodException e) {
157: return;
158: }
159: fail("The NoSuchMethodException exception excpected");
160: }
161:
162: interface I {
163:
164: boolean equals(Object obj);
165: }
166:
167: /**
168: * Helper inner class.
169: */
170: private static class A implements I {
171:
172: static int i;
173:
174: static {
175: i = 0;
176: }
177:
178: public A() {
179: i = 0;
180: }
181:
182: public void method1() {
183: }
184:
185: protected void method2() {
186: }
187:
188: public String toString() {
189: return null;
190: }
191: }
192: }
|