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