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 ClassTestGetDeclaredField 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.getDeclaredField(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().getDeclaredField(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.getDeclaredField(name);
069: assertEquals("incorrect name", name, f.getName());
070: } catch (Exception e) {
071: fail(e.toString());
072: }
073: }
074:
075: /**
076: * checks whether private field is reflected
077: */
078: public void test4() {
079: try {
080: final String name = "s";
081: Field f = A.class.getDeclaredField(name);
082: assertEquals("incorrect name", name, f.getName());
083: } catch (Exception e) {
084: fail(e.toString());
085: }
086: }
087:
088: /**
089: * Class B redefines field s of A class. Field of 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.getDeclaredField(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 i. So an NoSuchFieldException exception
105: * should be thrown even if its super class defines field with this name.
106: */
107: public void test6() {
108: try {
109: final String name = "i";
110: B.class.getDeclaredField(name);
111: } catch (NoSuchFieldException e) {
112: return;
113: }
114: fail("NoSuchFieldException exception expected");
115: }
116:
117: /**
118: * Class B does not define field j. So an NoSuchFieldException exception
119: * should be thrown even if its super interface defines field with this name.
120: */
121: public void test7() {
122: try {
123: final String name = "j";
124: B.class.getDeclaredField(name);
125: } catch (NoSuchFieldException e) {
126: return;
127: }
128: fail("NoSuchFieldException exception expected");
129: }
130:
131: private static class A {
132:
133: public final static int i = 0;
134:
135: private String s;
136: }
137:
138: interface I {
139:
140: public final static int i = 0;
141:
142: public final static int j = 1;
143: }
144:
145: private class B extends A implements I {
146:
147: String s;
148: }
149: }
|