Path: blob/master/test/jdk/java/lang/Class/getFields/Sanity.java
41153 views
/*1* Copyright 2014 Google, Inc. All Rights Reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 806314725* @summary Tests for Class.getFields().26* @run testng Sanity27*/2829import java.lang.reflect.Field;30import java.util.ArrayList;31import java.util.Arrays;32import java.util.List;33import org.testng.annotations.Test;34import static org.testng.Assert.*;3536public class Sanity {37public interface EmptyInterface {}38class EmptyClass {}39interface BI1 {40public int i = 1;41int j = 2;42}43interface BI2 {44int k = 1;45}46public interface DI extends BI1, BI2, EmptyInterface {47int m = 5;48}49interface DDI extends DI {50int n = 6;51}5253public class D extends EmptyClass {54public int publicDField;55protected int protectedDField;56private int privateDField;57}5859class DD extends D {60public int publicDDField;61protected int protectedDDField;62private int privateDDField;63}6465public class Universe extends DD implements DDI {66public int publicUniverseField;67protected int protectedUniverseField;68private int privateUniverseField;69}7071void assertContainsNoFields(Class<?> clazz) {72assertEquals(clazz.getFields().length, 0);73}7475@Test76public void primitiveTypesHaveNoFields() throws Exception {77assertContainsNoFields(byte.class);78assertContainsNoFields(char.class);79assertContainsNoFields(short.class);80assertContainsNoFields(int.class);81assertContainsNoFields(long.class);82assertContainsNoFields(boolean.class);83assertContainsNoFields(void.class);84assertContainsNoFields(double.class);85assertContainsNoFields(float.class);86}8788@Test89public void arrayTypesHaveNoFields() throws Exception {90assertContainsNoFields(byte[].class);91assertContainsNoFields(Object[].class);92assertContainsNoFields(java.util.Map[].class);93assertContainsNoFields(java.util.HashMap[].class);94}9596@Test97public void emptyInterfacesHaveNoFields() throws Exception {98assertContainsNoFields(EmptyInterface.class);99}100101@Test102public void emptyClassesHaveNoFields() throws Exception {103assertContainsNoFields(EmptyClass.class);104class EmptyLocalClass {}105assertContainsNoFields(EmptyLocalClass.class);106}107108void assertContainsFields(Class<?> clazz, int count) {109assertEquals(clazz.getFields().length, count);110}111112@Test113public void checkFieldCounts() throws Exception {114assertContainsFields(BI1.class, 2);115assertContainsFields(BI2.class, 1);116assertContainsFields(DI.class, 4);117assertContainsFields(DDI.class, 5);118assertContainsFields(D.class, 1);119assertContainsFields(DD.class, 2);120assertContainsFields(Universe.class, 8);121}122123void assertContainsFields(Class<?> derived, Class<?> base) {124List<Field> derivedFields = Arrays.asList(derived.getFields());125List<Field> baseFields = Arrays.asList(base.getFields());126assertTrue(derivedFields.containsAll(baseFields));127}128129List<Class<?>> directSupers(Class<?> clazz) {130List<Class<?>> directSupers = new ArrayList<>();131directSupers.addAll(Arrays.asList(clazz.getInterfaces()));132if (clazz.getSuperclass() != null) {133directSupers.add(clazz.getSuperclass());134}135return directSupers;136}137138void assertContainsSuperFields(Class<?> clazz) {139for (Class<?> directSuper : directSupers(clazz)) {140assertContainsFields(clazz, directSuper);141}142}143144List<Class<?>> testClasses() {145List<Class<?>> testClasses = new ArrayList<>();146testClasses.add(Sanity.class);147testClasses.addAll(Arrays.asList(Sanity.class.getDeclaredClasses()));148assertEquals(testClasses.size(), 10);149return testClasses;150}151152@Test153public void fieldsAreInheritedFromSupers() throws Exception {154for (Class clazz : testClasses()) {155assertContainsSuperFields(clazz);156}157}158159@Test160public void getFieldIsConsistentWithGetFields() throws Exception {161for (Class clazz : testClasses()) {162for (Field field : clazz.getFields()) {163assertEquals(field, clazz.getField(field.getName()));164}165}166}167}168169170