001/*
002 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package jdk.internal.jvmci.runtime.test;
024
025import static org.junit.Assert.*;
026
027import java.lang.annotation.*;
028import java.lang.reflect.*;
029import java.util.*;
030
031import jdk.internal.jvmci.meta.*;
032
033import org.junit.*;
034
035/**
036 * Tests for {@link ResolvedJavaField}.
037 */
038public class TestResolvedJavaField extends FieldUniverse {
039
040    public TestResolvedJavaField() {
041    }
042
043    @Test
044    public void getModifiersTest() {
045        for (Map.Entry<Field, ResolvedJavaField> e : fields.entrySet()) {
046            int expected = e.getKey().getModifiers();
047            int actual = e.getValue().getModifiers();
048            assertEquals(expected, actual);
049        }
050    }
051
052    @Test
053    public void isSyntheticTest() {
054        for (Map.Entry<Field, ResolvedJavaField> e : fields.entrySet()) {
055            boolean expected = e.getKey().isSynthetic();
056            boolean actual = e.getValue().isSynthetic();
057            assertEquals(expected, actual);
058        }
059    }
060
061    @Test
062    public void getAnnotationTest() {
063        for (Map.Entry<Field, ResolvedJavaField> e : fields.entrySet()) {
064            for (Annotation expected : e.getKey().getAnnotations()) {
065                if (expected != null) {
066                    Annotation actual = e.getValue().getAnnotation(expected.annotationType());
067                    assertEquals(expected, actual);
068                }
069            }
070        }
071    }
072
073    @Test
074    public void getLocationIdentityTest() {
075        for (Map.Entry<Field, ResolvedJavaField> e : fields.entrySet()) {
076            LocationIdentity identity = e.getValue().getLocationIdentity();
077            assertTrue(identity != null);
078        }
079    }
080
081    static class ReadConstantValueTestConstants {
082        String stringField = "field";
083        final String constantStringField = "constantField";
084
085        static final Object CONST1 = new ReadConstantValueTestConstants();
086        static final Object CONST2 = null;
087        static final Object CONST3 = new String();
088    }
089
090    @Test
091    public void readConstantValueTest() throws NoSuchFieldException {
092        ResolvedJavaField field = metaAccess.lookupJavaField(ReadConstantValueTestConstants.class.getDeclaredField("stringField"));
093        List<ConstantValue> receiverConstants = readConstants(ReadConstantValueTestConstants.class);
094        for (ConstantValue receiver : receiverConstants) {
095            JavaConstant value = constantReflection.readConstantFieldValue(field, receiver.value);
096            assertNull(value);
097        }
098
099        ResolvedJavaField constField = metaAccess.lookupJavaField(ReadConstantValueTestConstants.class.getDeclaredField("constantStringField"));
100        for (ConstantValue receiver : receiverConstants) {
101            JavaConstant value = constantReflection.readConstantFieldValue(constField, receiver.value);
102            if (value != null) {
103                Object expected = "constantField";
104                String actual = ((ReadConstantValueTestConstants) receiver.boxed).constantStringField;
105                assertTrue(actual + " != " + expected, actual == expected);
106            }
107        }
108    }
109
110    private Method findTestMethod(Method apiMethod) {
111        String testName = apiMethod.getName() + "Test";
112        for (Method m : getClass().getDeclaredMethods()) {
113            if (m.getName().equals(testName) && m.getAnnotation(Test.class) != null) {
114                return m;
115            }
116        }
117        return null;
118    }
119
120    // @formatter:off
121    private static final String[] untestedApiMethods = {
122        "getDeclaringClass",
123        "isInternal",
124        "isFinal"
125    };
126    // @formatter:on
127
128    /**
129     * Ensures that any new methods added to {@link ResolvedJavaMethod} either have a test written
130     * for them or are added to {@link #untestedApiMethods}.
131     */
132    @Test
133    public void testCoverage() {
134        Set<String> known = new HashSet<>(Arrays.asList(untestedApiMethods));
135        for (Method m : ResolvedJavaField.class.getDeclaredMethods()) {
136            if (m.isSynthetic()) {
137                continue;
138            }
139            if (findTestMethod(m) == null) {
140                assertTrue("test missing for " + m, known.contains(m.getName()));
141            } else {
142                assertFalse("test should be removed from untestedApiMethods" + m, known.contains(m.getName()));
143            }
144        }
145    }
146}