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.Field;
024:
025: import junit.framework.TestCase;
026:
027: /**
028: * tested class: java.lang.Class
029: * tested method: getDeclaredField
030: */
031: @SuppressWarnings(value={"all"})
032: public class ClassTestGetField extends TestCase {
033:
034: /**
035: * the length field should not be reflected.
036: */
037: public void test1() {
038: try {
039: final String name = "length";
040: Class c = new int[0].getClass();
041: c.getField(name);
042: } catch (NoSuchFieldException e) {
043: return;
044: }
045: fail("NoSuchFieldException exception expected");
046: }
047:
048: /**
049: * if name is null NullPoinerexception exception should be thrown.
050: */
051: public void test2() {
052: try {
053: final String name = null;
054: getClass().getField(name);
055: } catch (NullPointerException e) {
056: return;
057: } catch (NoSuchFieldException e) {
058: }
059: fail("NullPointerException exception expected");
060: }
061:
062: /**
063: * checks whether public field is reflected
064: */
065: public void test3() {
066: try {
067: final String name = "i";
068: Field f = A.class.getField(name);
069: assertEquals("incorrect name", name, f.getName());
070: } catch (Exception e) {
071: fail(e.toString());
072: }
073: }
074:
075: /**
076: * Private field should not be reflected.
077: */
078: public void test4() {
079: try {
080: final String name = "s";
081: A.class.getField(name);
082: } catch (NoSuchFieldException e) {
083: return;
084: }
085: fail("NoSuchFieldException exception expected");
086: }
087:
088: /**
089: * Class B redefines field s of A class. Field of the B class should be
090: * reflected in this case.
091: */
092: public void test5() {
093: try {
094: final String name = "s";
095: Field f = B.class.getField(name);
096: assertEquals("incorrect name", name, f.getName());
097: assertSame("objects differ", B.class, f.getDeclaringClass());
098: } catch (Exception e) {
099: fail(e.toString());
100: }
101: }
102:
103: /**
104: * Class B does not define field k. But its super interface J defines field
105: * with such name. The field of the super interface should be returned.
106: */
107: public void test6() {
108: try {
109: final String name = "k";
110: Field f = B.class.getField(name);
111: assertNotNull("unexpected null", f);
112: assertEquals("incorrect name", name, f.getName());
113: assertSame("objects differ", J.class, f.getDeclaringClass());
114: } catch (Exception e) {
115: fail(e.toString());
116: }
117: }
118:
119: /**
120: * Class B does not define field i. But its super interface I and super
121: * class A define field with such name. The field of the super interface
122: * should be returned.
123: */
124: public void test7() {
125: try {
126: final String name = "i";
127: Field f = B.class.getField(name);
128: assertNotNull("unexpected null", f);
129: assertEquals("incorrect name", name, f.getName());
130: assertSame("objects differ", I.class, f.getDeclaringClass());
131: } catch (Exception e) {
132: fail(e.toString());
133: }
134: }
135:
136: /**
137: * Class B does not define field j. But its super class A defines field with
138: * such name. The field of the super class should be returned.
139: */
140: public void test8() {
141: try {
142: final String name = "j";
143: Field f = B.class.getField(name);
144: assertNotNull("unexpected null", f);
145: assertEquals("incorrect name", name, f.getName());
146: assertSame("objects differ", A.class, f.getDeclaringClass());
147: } catch (Exception e) {
148: fail(e.toString());
149: }
150: }
151:
152: /**
153: * Class B does not define field o. Its super class A defines field with
154: * such name. Sice this field has private accessibility an
155: * NoSuchFieldException exception should be thrown.
156: */
157: public void test9() {
158: try {
159: final String name = "o";
160: B.class.getField(name);
161: } catch (NoSuchFieldException e) {
162: return;
163: }
164: fail("NoSuchFieldException exception expected");
165: }
166:
167: private static class A {
168:
169: public final static int i = 0;
170:
171: public final static int j = 1;
172:
173: Object o;
174:
175: private String s;
176: }
177:
178: interface I {
179:
180: public final static int i = 0;
181: }
182:
183: interface J extends I {
184:
185: int k = 0;
186: }
187:
188: private class B extends A implements J {
189:
190: public String s;
191: }
192: }
|