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