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.assertNull; 41 import static org.junit.Assert.assertTrue; 42 43 import java.lang.annotation.Annotation; 44 import java.lang.reflect.Field; 45 import java.lang.reflect.Method; 46 import java.util.Arrays; 47 import java.util.HashSet; 48 import java.util.List; 49 import java.util.Map; 50 import java.util.Set; 51 52 import jdk.vm.ci.meta.JavaConstant; 53 import jdk.vm.ci.meta.LocationIdentity; 54 import jdk.vm.ci.meta.ResolvedJavaField; 55 import jdk.vm.ci.meta.ResolvedJavaMethod; 56 57 import org.junit.Test; 58 59 /** 60 * Tests for {@link ResolvedJavaField}. 61 */ 62 public class TestResolvedJavaField extends FieldUniverse { 63 64 public TestResolvedJavaField() { 65 } 66 67 @Test 68 public void getModifiersTest() { 69 for (Map.Entry<Field, ResolvedJavaField> e : fields.entrySet()) { 70 int expected = e.getKey().getModifiers(); 71 int actual = e.getValue().getModifiers(); 72 assertEquals(expected, actual); 73 } 74 } 75 76 @Test 77 public void isSyntheticTest() { 78 for (Map.Entry<Field, ResolvedJavaField> e : fields.entrySet()) { 79 boolean expected = e.getKey().isSynthetic(); 80 boolean actual = e.getValue().isSynthetic(); 81 assertEquals(expected, actual); 82 } 83 } 84 85 @Test 86 public void getAnnotationsTest() { 87 for (Map.Entry<Field, ResolvedJavaField> e : fields.entrySet()) { 88 Annotation[] expected = e.getKey().getAnnotations(); 89 Annotation[] actual = e.getValue().getAnnotations(); 90 assertArrayEquals(expected, actual); 91 } 92 } 93 94 @Test 95 public void getAnnotationTest() { 96 for (Map.Entry<Field, ResolvedJavaField> e : fields.entrySet()) { 97 for (Annotation expected : e.getKey().getAnnotations()) { 98 if (expected != null) { 99 Annotation actual = e.getValue().getAnnotation(expected.annotationType()); 100 assertEquals(expected, actual); 101 } 102 } 103 } 104 } 105 106 @Test 107 public void getLocationIdentityTest() { 108 for (Map.Entry<Field, ResolvedJavaField> e : fields.entrySet()) { 109 LocationIdentity identity = e.getValue().getLocationIdentity(); 110 assertTrue(identity != null); 111 } 112 } 113 114 static class ReadConstantValueTestConstants { 115 String stringField = "field"; 116 final String constantStringField = "constantField"; 117 118 static final Object CONST1 = new ReadConstantValueTestConstants(); 119 static final Object CONST2 = null; 120 static final Object CONST3 = new String(); 121 } 122 123 @Test 124 public void readConstantValueTest() throws NoSuchFieldException { 125 ResolvedJavaField field = metaAccess.lookupJavaField(ReadConstantValueTestConstants.class.getDeclaredField("stringField")); 126 List<ConstantValue> receiverConstants = readConstants(ReadConstantValueTestConstants.class); 127 for (ConstantValue receiver : receiverConstants) { 128 JavaConstant value = constantReflection.readConstantFieldValue(field, receiver.value); 129 assertNull(value); 130 } 131 132 ResolvedJavaField constField = metaAccess.lookupJavaField(ReadConstantValueTestConstants.class.getDeclaredField("constantStringField")); 133 for (ConstantValue receiver : receiverConstants) { 134 JavaConstant value = constantReflection.readConstantFieldValue(constField, receiver.value); 135 if (value != null) { 136 Object expected = "constantField"; 137 String actual = ((ReadConstantValueTestConstants) receiver.boxed).constantStringField; 138 assertTrue(actual + " != " + expected, actual == expected); 139 } 140 } 141 } 142 143 private Method findTestMethod(Method apiMethod) { 144 String testName = apiMethod.getName() + "Test"; 145 for (Method m : getClass().getDeclaredMethods()) { 146 if (m.getName().equals(testName) && m.getAnnotation(Test.class) != null) { 147 return m; 148 } 149 } 150 return null; 151 } 152 153 // @formatter:off 154 private static final String[] untestedApiMethods = { 155 "getDeclaringClass", 156 "isInternal", 157 "isFinal" 158 }; 159 // @formatter:on 160 161 /** 162 * Ensures that any new methods added to {@link ResolvedJavaMethod} either have a test written 163 * for them or are added to {@link #untestedApiMethods}. 164 */ 165 @Test 166 public void testCoverage() { 167 Set<String> known = new HashSet<>(Arrays.asList(untestedApiMethods)); 168 for (Method m : ResolvedJavaField.class.getDeclaredMethods()) { 169 if (m.isSynthetic()) { 170 continue; 171 } 172 if (findTestMethod(m) == null) { 173 assertTrue("test missing for " + m, known.contains(m.getName())); 174 } else { 175 assertFalse("test should be removed from untestedApiMethods" + m, known.contains(m.getName())); 176 } 177 } 178 } 179 }