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.arch == "x64" | vm.arch == "sparcv9" | vm.arch == "arm64")
  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.assertTrue;
  41 
  42 import java.lang.annotation.Annotation;
  43 import java.lang.reflect.Field;
  44 import java.lang.reflect.Method;
  45 import java.util.Arrays;
  46 import java.util.HashSet;
  47 import java.util.Map;
  48 import java.util.Set;
  49 
  50 import jdk.vm.ci.meta.ResolvedJavaField;
  51 import jdk.vm.ci.meta.ResolvedJavaMethod;
  52 
  53 import org.junit.Test;
  54 
  55 /**
  56  * Tests for {@link ResolvedJavaField}.
  57  */
  58 public 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     private Method findTestMethod(Method apiMethod) {
 103         String testName = apiMethod.getName() + "Test";
 104         for (Method m : getClass().getDeclaredMethods()) {
 105             if (m.getName().equals(testName) && m.getAnnotation(Test.class) != null) {
 106                 return m;
 107             }
 108         }
 109         return null;
 110     }
 111 
 112     // @formatter:off
 113     private static final String[] untestedApiMethods = {
 114         "getDeclaringClass",
 115         "isInternal",
 116         "isFinal"
 117     };
 118     // @formatter:on
 119 
 120     /**
 121      * Ensures that any new methods added to {@link ResolvedJavaMethod} either have a test written
 122      * for them or are added to {@link #untestedApiMethods}.
 123      */
 124     @Test
 125     public void testCoverage() {
 126         Set<String> known = new HashSet<>(Arrays.asList(untestedApiMethods));
 127         for (Method m : ResolvedJavaField.class.getDeclaredMethods()) {
 128             if (m.isSynthetic()) {
 129                 continue;
 130             }
 131             if (findTestMethod(m) == null) {
 132                 assertTrue("test missing for " + m, known.contains(m.getName()));
 133             } else {
 134                 assertFalse("test should be removed from untestedApiMethods" + m, known.contains(m.getName()));
 135             }
 136         }
 137     }
 138 }