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  * @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.assertNull;
  41 import static org.junit.Assert.assertTrue;
  42 
  43 import java.lang.annotation.Annotation;
  44 import java.lang.reflect.Field;
  45 import java.lang.reflect.Method;
  46 import java.util.Arrays;
  47 import java.util.HashSet;
  48 import java.util.List;
  49 import java.util.Map;
  50 import java.util.Set;
  51 
  52 import jdk.vm.ci.meta.JavaConstant;

  53 import jdk.vm.ci.meta.ResolvedJavaField;
  54 import jdk.vm.ci.meta.ResolvedJavaMethod;
  55 
  56 import org.junit.Test;
  57 
  58 /**
  59  * Tests for {@link ResolvedJavaField}.
  60  */
  61 public class TestResolvedJavaField extends FieldUniverse {
  62 
  63     public TestResolvedJavaField() {
  64     }
  65 
  66     @Test
  67     public void getModifiersTest() {
  68         for (Map.Entry<Field, ResolvedJavaField> e : fields.entrySet()) {
  69             int expected = e.getKey().getModifiers();
  70             int actual = e.getValue().getModifiers();
  71             assertEquals(expected, actual);
  72         }
  73     }
  74 
  75     @Test
  76     public void isSyntheticTest() {
  77         for (Map.Entry<Field, ResolvedJavaField> e : fields.entrySet()) {
  78             boolean expected = e.getKey().isSynthetic();
  79             boolean actual = e.getValue().isSynthetic();
  80             assertEquals(expected, actual);
  81         }
  82     }
  83 
  84     @Test
  85     public void getAnnotationsTest() {
  86         for (Map.Entry<Field, ResolvedJavaField> e : fields.entrySet()) {
  87             Annotation[] expected = e.getKey().getAnnotations();
  88             Annotation[] actual = e.getValue().getAnnotations();
  89             assertArrayEquals(expected, actual);
  90         }
  91     }
  92 
  93     @Test
  94     public void getAnnotationTest() {
  95         for (Map.Entry<Field, ResolvedJavaField> e : fields.entrySet()) {
  96             for (Annotation expected : e.getKey().getAnnotations()) {
  97                 if (expected != null) {
  98                     Annotation actual = e.getValue().getAnnotation(expected.annotationType());
  99                     assertEquals(expected, actual);
 100                 }
 101             }








 102         }
 103     }
 104 
 105     static class ReadConstantValueTestConstants {
 106         String stringField = "field";
 107         final String constantStringField = "constantField";
 108 
 109         static final Object CONST1 = new ReadConstantValueTestConstants();
 110         static final Object CONST2 = null;
 111         static final Object CONST3 = new String();
 112     }
 113 
 114     @Test
 115     public void readConstantValueTest() throws NoSuchFieldException {
 116         ResolvedJavaField field = metaAccess.lookupJavaField(ReadConstantValueTestConstants.class.getDeclaredField("stringField"));
 117         List<ConstantValue> receiverConstants = readConstants(ReadConstantValueTestConstants.class);
 118         for (ConstantValue receiver : receiverConstants) {
 119             JavaConstant value = constantReflection.readConstantFieldValue(field, receiver.value);
 120             assertNull(value);
 121         }
 122 
 123         ResolvedJavaField constField = metaAccess.lookupJavaField(ReadConstantValueTestConstants.class.getDeclaredField("constantStringField"));
 124         for (ConstantValue receiver : receiverConstants) {
 125             JavaConstant value = constantReflection.readConstantFieldValue(constField, receiver.value);
 126             if (value != null) {
 127                 Object expected = "constantField";
 128                 String actual = ((ReadConstantValueTestConstants) receiver.boxed).constantStringField;
 129                 assertTrue(actual + " != " + expected, actual == expected);
 130             }
 131         }
 132     }
 133 
 134     private Method findTestMethod(Method apiMethod) {
 135         String testName = apiMethod.getName() + "Test";
 136         for (Method m : getClass().getDeclaredMethods()) {
 137             if (m.getName().equals(testName) && m.getAnnotation(Test.class) != null) {
 138                 return m;
 139             }
 140         }
 141         return null;
 142     }
 143 
 144     // @formatter:off
 145     private static final String[] untestedApiMethods = {
 146         "getDeclaringClass",
 147         "isInternal",
 148         "isFinal"
 149     };
 150     // @formatter:on
 151 
 152     /**
 153      * Ensures that any new methods added to {@link ResolvedJavaMethod} either have a test written
 154      * for them or are added to {@link #untestedApiMethods}.
 155      */
 156     @Test
 157     public void testCoverage() {
 158         Set<String> known = new HashSet<>(Arrays.asList(untestedApiMethods));
 159         for (Method m : ResolvedJavaField.class.getDeclaredMethods()) {
 160             if (m.isSynthetic()) {
 161                 continue;
 162             }
 163             if (findTestMethod(m) == null) {
 164                 assertTrue("test missing for " + m, known.contains(m.getName()));
 165             } else {
 166                 assertFalse("test should be removed from untestedApiMethods" + m, known.contains(m.getName()));
 167             }
 168         }
 169     }
 170 }
--- EOF ---