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: getDeclaredMethod
030: */
031: @SuppressWarnings(value={"all"})
032: public class ClassTestGetDeclaredMethod extends TestCase {
033:
034: /**
035: * Private method method1 should be reflected by getDeclaredMethod() method.
036: */
037: public void test1() {
038: try {
039: final String name = "method1";
040: Method m = A.class.getDeclaredMethod(name, (Class[]) null);
041: assertNotNull("null expected", m);
042: assertEquals("incorrect name", name, m.getName());
043: assertSame("objects differ", A.class, m.getDeclaringClass());
044: } catch (Exception e) {
045: fail(e.toString());
046: }
047: }
048:
049: /**
050: * Attempt to retrieve public method declared in the super class.
051: */
052: public void test2() {
053: try {
054: String methodName = "wait";
055: getClass().getDeclaredMethod(methodName, (Class[]) null);
056: } catch (NoSuchMethodException e) {
057: return;
058: }
059: fail("NoSuchMethodException exception expected");
060: }
061:
062: /**
063: * if name is null NullPointerException should be thrown.
064: */
065: public void test3() {
066: try {
067: String methodName = null;
068: getClass().getDeclaredMethod(methodName, (Class[]) null);
069: } catch (NullPointerException e) {
070: return;
071: } catch (NoSuchMethodException e) {
072: }
073: fail("NullPointerException exception expected");
074: }
075:
076: /**
077: * NoSuchMethodException should be thrown if name is equal to "<cinit>"
078: */
079: public void test4() {
080: try {
081: String methodName = "<cinit>";
082: A.class.getDeclaredMethod(methodName, (Class[]) null);
083: } catch (NoSuchMethodException e) {
084: return;
085: }
086: fail("NoSuchMethodException exception expected");
087: }
088:
089: /**
090: * NoSuchMethodException should be thrown if name is equal to "<init>"
091: */
092: public void test5() {
093: try {
094: String methodName = "<init>";
095: A.class.getDeclaredMethod(methodName, (Class[]) null);
096: } catch (NoSuchMethodException e) {
097: return;
098: }
099: fail("NoSuchMethodException exception expected");
100: }
101:
102: /**
103: * if a class contains the method with the same name and parameters as its
104: * super class then the method of this class should be reflected.
105: */
106: public void test6() {
107: try {
108: String methodName = "toString";
109: Method m = A.class.getMethod(methodName, new Class[] {});
110: assertEquals("incorrect name", methodName, m.getName());
111: assertSame("objects differ", A.class, m.getDeclaringClass());
112: } catch (Exception e) {
113: fail(e.toString());
114: }
115: }
116:
117: /**
118: * Helper inner class.
119: */
120: private static class A {
121:
122: static int i;
123:
124: static {
125: i = 0;
126: }
127:
128: public A() {
129: i = 0;
130: }
131:
132: private void method1() {
133: }
134:
135: public String toString() {
136: return null;
137: }
138: }
139: }
|