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: getFields
030: */
031: @SuppressWarnings(value={"all"})
032: public class ClassTestGetFields extends TestCase {
033:
034: /**
035: * Void.TYPE class does not declare fields.
036: */
037: public void test1() {
038: Field[] fs = Void.TYPE.getFields();
039: assertNotNull("unexpected null", fs);
040: assertEquals("array length", 0, fs.length);
041: }
042:
043: /**
044: * Arrays do not declare fields.
045: */
046: public void test2() {
047: Field[] fs = new int[0].getClass().getFields();
048: assertNotNull("unexpected null", fs);
049: assertEquals("array length", 0, fs.length);
050: }
051:
052: /**
053: * Every field of interface should be reflected.
054: */
055: public void test3() {
056: Field[] fs = I.class.getFields();
057: assertNotNull("unexpected null", fs);
058: assertEquals("array length", 1, fs.length);
059: }
060:
061: /**
062: * Every field of interface and its super interfaces should be reflected.
063: */
064: public void test4() {
065: Field[] fs = J.class.getFields();
066: assertNotNull("unexpected null", fs);
067: assertEquals("array length", 2, fs.length);
068: }
069:
070: /**
071: * Only public fields should be reflected.
072: */
073: public void test5() {
074: Field[] fs = A.class.getFields();
075: assertNotNull("unexpected null", fs);
076: assertEquals("array length", 3, fs.length);
077: }
078:
079: /**
080: * All fields of this class, its super interfaces and super classes should
081: * be included in resulting array. Each field should appear only once.
082: */
083: public void test6() {
084: Field[] fs = B.class.getFields();
085: assertNotNull("unexpected null", fs);
086: assertEquals("array length", 6, fs.length);
087: }
088:
089: private static class A implements I {
090:
091: public final static int i = 0;
092:
093: public final static int j = 1;
094:
095: Object o;
096:
097: private String s;
098: }
099:
100: interface I {
101:
102: int i = 0;
103: }
104:
105: interface J extends I {
106:
107: int k = 0;
108: }
109:
110: interface L extends I {
111: }
112:
113: private class B extends A implements L, J {
114:
115: public int k;
116:
117: public String s;
118: }
119: }
|