TestResolvedJavaField.java revision 12651:6ef01bd40ce2
1/*
2 * Copyright (c) 2012, 2016, 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 vm.jvmci
27 * @library ../../../../../
28 * @modules jdk.internal.vm.ci/jdk.vm.ci.meta
29 *          jdk.internal.vm.ci/jdk.vm.ci.runtime
30 *          java.base/jdk.internal.misc
31 * @run junit/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI jdk.vm.ci.runtime.test.TestResolvedJavaField
32 */
33
34package jdk.vm.ci.runtime.test;
35
36import jdk.vm.ci.meta.ResolvedJavaField;
37import jdk.vm.ci.meta.ResolvedJavaMethod;
38import org.junit.Test;
39
40import java.lang.annotation.Annotation;
41import java.lang.reflect.Field;
42import java.lang.reflect.Method;
43import java.util.Arrays;
44import java.util.HashSet;
45import java.util.Map;
46import java.util.Set;
47
48import static org.junit.Assert.assertArrayEquals;
49import static org.junit.Assert.assertEquals;
50import static org.junit.Assert.assertFalse;
51import static org.junit.Assert.assertTrue;
52
53/**
54 * Tests for {@link ResolvedJavaField}.
55 */
56public class TestResolvedJavaField extends FieldUniverse {
57
58    public TestResolvedJavaField() {
59    }
60
61    @Test
62    public void getModifiersTest() {
63        for (Map.Entry<Field, ResolvedJavaField> e : fields.entrySet()) {
64            int expected = e.getKey().getModifiers();
65            int actual = e.getValue().getModifiers();
66            assertEquals(expected, actual);
67        }
68    }
69
70    @Test
71    public void isSyntheticTest() {
72        for (Map.Entry<Field, ResolvedJavaField> e : fields.entrySet()) {
73            boolean expected = e.getKey().isSynthetic();
74            boolean actual = e.getValue().isSynthetic();
75            assertEquals(expected, actual);
76        }
77    }
78
79    @Test
80    public void getAnnotationsTest() {
81        for (Map.Entry<Field, ResolvedJavaField> e : fields.entrySet()) {
82            Annotation[] expected = e.getKey().getAnnotations();
83            Annotation[] actual = e.getValue().getAnnotations();
84            assertArrayEquals(expected, actual);
85        }
86    }
87
88    @Test
89    public void getAnnotationTest() {
90        for (Map.Entry<Field, ResolvedJavaField> e : fields.entrySet()) {
91            for (Annotation expected : e.getKey().getAnnotations()) {
92                if (expected != null) {
93                    Annotation actual = e.getValue().getAnnotation(expected.annotationType());
94                    assertEquals(expected, actual);
95                }
96            }
97        }
98    }
99
100    private Method findTestMethod(Method apiMethod) {
101        String testName = apiMethod.getName() + "Test";
102        for (Method m : getClass().getDeclaredMethods()) {
103            if (m.getName().equals(testName) && m.getAnnotation(Test.class) != null) {
104                return m;
105            }
106        }
107        return null;
108    }
109
110    // @formatter:off
111    private static final String[] untestedApiMethods = {
112        "getDeclaringClass",
113        "isInternal",
114        "isFinal"
115    };
116    // @formatter:on
117
118    /**
119     * Ensures that any new methods added to {@link ResolvedJavaMethod} either have a test written
120     * for them or are added to {@link #untestedApiMethods}.
121     */
122    @Test
123    public void testCoverage() {
124        Set<String> known = new HashSet<>(Arrays.asList(untestedApiMethods));
125        for (Method m : ResolvedJavaField.class.getDeclaredMethods()) {
126            if (m.isSynthetic()) {
127                continue;
128            }
129            if (findTestMethod(m) == null) {
130                assertTrue("test missing for " + m, known.contains(m.getName()));
131            } else {
132                assertFalse("test should be removed from untestedApiMethods" + m, known.contains(m.getName()));
133            }
134        }
135    }
136}
137