1 /* 2 * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * @test 26 * @requires (os.simpleArch == "x64" | os.simpleArch == "sparcv9" | os.simpleArch == "aarch64") 27 * @library ../../../../../ 28 * @modules jdk.vm.ci/jdk.vm.ci.meta 29 * jdk.vm.ci/jdk.vm.ci.runtime 30 * java.base/jdk.internal.misc 31 * @build jdk.vm.ci.runtime.test.TestResolvedJavaField 32 * @run junit/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI jdk.vm.ci.runtime.test.TestResolvedJavaField 33 */ 34 35 package jdk.vm.ci.runtime.test; 36 37 import static org.junit.Assert.assertArrayEquals; 38 import static org.junit.Assert.assertEquals; 39 import static org.junit.Assert.assertFalse; 40 import static org.junit.Assert.assertTrue; 41 42 import java.lang.annotation.Annotation; 43 import java.lang.reflect.Field; 44 import java.lang.reflect.Method; 45 import java.util.Arrays; 46 import java.util.HashSet; 47 import java.util.Map; 48 import java.util.Set; 49 50 import org.junit.Test; 51 52 import jdk.vm.ci.meta.LocationIdentity; 53 import jdk.vm.ci.meta.ResolvedJavaField; 54 import jdk.vm.ci.meta.ResolvedJavaMethod; 55 56 /** 57 * Tests for {@link ResolvedJavaField}. 58 */ 59 public class TestResolvedJavaField extends FieldUniverse { 60 61 public TestResolvedJavaField() { 62 } 63 64 @Test 65 public void getModifiersTest() { 66 for (Map.Entry<Field, ResolvedJavaField> e : fields.entrySet()) { 67 int expected = e.getKey().getModifiers(); 68 int actual = e.getValue().getModifiers(); 69 assertEquals(expected, actual); 70 } 71 } 72 73 @Test 74 public void isSyntheticTest() { 75 for (Map.Entry<Field, ResolvedJavaField> e : fields.entrySet()) { 76 boolean expected = e.getKey().isSynthetic(); 77 boolean actual = e.getValue().isSynthetic(); 78 assertEquals(expected, actual); 79 } 80 } 81 82 @Test 83 public void getAnnotationsTest() { 84 for (Map.Entry<Field, ResolvedJavaField> e : fields.entrySet()) { 85 Annotation[] expected = e.getKey().getAnnotations(); 86 Annotation[] actual = e.getValue().getAnnotations(); 87 assertArrayEquals(expected, actual); 88 } 89 } 90 91 @Test 92 public void getAnnotationTest() { 93 for (Map.Entry<Field, ResolvedJavaField> e : fields.entrySet()) { 94 for (Annotation expected : e.getKey().getAnnotations()) { 95 if (expected != null) { 96 Annotation actual = e.getValue().getAnnotation(expected.annotationType()); 97 assertEquals(expected, actual); 98 } 99 } 100 } 101 } 102 103 @Test 104 public void getLocationIdentityTest() { 105 for (Map.Entry<Field, ResolvedJavaField> e : fields.entrySet()) { 106 LocationIdentity identity = e.getValue().getLocationIdentity(); 107 assertTrue(identity != null); 108 } 109 } 110 111 private Method findTestMethod(Method apiMethod) { 112 String testName = apiMethod.getName() + "Test"; 113 for (Method m : getClass().getDeclaredMethods()) { 114 if (m.getName().equals(testName) && m.getAnnotation(Test.class) != null) { 115 return m; 116 } 117 } 118 return null; 119 } 120 121 // @formatter:off 122 private static final String[] untestedApiMethods = { 123 "getDeclaringClass", 124 "isInternal", 125 "isFinal" 126 }; 127 // @formatter:on 128 129 /** 130 * Ensures that any new methods added to {@link ResolvedJavaMethod} either have a test written 131 * for them or are added to {@link #untestedApiMethods}. 132 */ 133 @Test 134 public void testCoverage() { 135 Set<String> known = new HashSet<>(Arrays.asList(untestedApiMethods)); 136 for (Method m : ResolvedJavaField.class.getDeclaredMethods()) { 137 if (m.isSynthetic()) { 138 continue; 139 } 140 if (findTestMethod(m) == null) { 141 assertTrue("test missing for " + m, known.contains(m.getName())); 142 } else { 143 assertFalse("test should be removed from untestedApiMethods" + m, known.contains(m.getName())); 144 } 145 } 146 } 147 }