1 /*
   2  * Copyright (c) 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 jdk.vm.ci.hotspot.test.HotSpotConstantReflectionProviderTest
  26  * @requires (vm.arch == "x64" | vm.arch == "sparcv9" | vm.arch == "arm64")
  27  * @modules jdk.vm.ci/jdk.vm.ci.runtime
  28  *          jdk.vm.ci/jdk.vm.ci.meta
  29  *          jdk.vm.ci/jdk.vm.ci.hotspot
  30  *          java.base/jdk.internal.vm.annotation
  31  *          java.base/jdk.internal.misc
  32  * @library /testlibrary /test/lib /compiler/jvmci/jdk.vm.ci.hotspot.test/src
  33  * @build jdk.vm.ci.hotspot.test.DummyClass
  34  * @run driver ClassFileInstaller jdk.vm.ci.hotspot.test.DummyClass
  35  * @run testng/othervm/timeout=300 -Xbootclasspath/a:.
  36  *      -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
  37  *       jdk.vm.ci.hotspot.test.HotSpotConstantReflectionProviderTest
  38  */
  39 
  40 package jdk.vm.ci.hotspot.test;
  41 
  42 import static jdk.vm.ci.hotspot.test.TestHelper.CONSTANT_REFLECTION_PROVIDER;
  43 
  44 import java.lang.reflect.Method;
  45 import jdk.vm.ci.hotspot.HotSpotResolvedJavaField;
  46 import jdk.vm.ci.meta.JavaConstant;
  47 import jdk.vm.ci.meta.Constant;
  48 import jdk.vm.ci.meta.MemoryAccessProvider;
  49 import jdk.vm.ci.meta.MethodHandleAccessProvider;
  50 import jdk.vm.ci.meta.ResolvedJavaField;
  51 import jdk.vm.ci.meta.ResolvedJavaType;
  52 import org.testng.Assert;
  53 import org.testng.annotations.Test;
  54 
  55 public class HotSpotConstantReflectionProviderTest {
  56 
  57     @Test(dataProvider = "forObjectDataProvider", dataProviderClass = ForObjectDataProvider.class)
  58     public void testForObject(Object obj, String expected) {
  59         JavaConstant jConst = TestHelper.CONSTANT_REFLECTION_PROVIDER.forObject(obj);
  60         Assert.assertNotNull(jConst,
  61                         "An instance of JavaConstant returned by" + " \"forObject\" method should not be null");
  62         Assert.assertEquals(jConst.toString(), expected, "Unexpected result:");
  63     }
  64 
  65     @Test(dataProvider = "forStringDataProvider", dataProviderClass = ForStringDataProvider.class)
  66     public void testForString(String string, String expected) {
  67         JavaConstant jConst = CONSTANT_REFLECTION_PROVIDER.forString(string);
  68         Assert.assertNotNull(jConst,
  69                         "An instance of JavaConstant returned by" + " \"forString\" method should not be null");
  70         Assert.assertEquals(jConst.toString(), expected, "Unexpected result:");
  71     }
  72 
  73     @Test(dataProvider = "constantEqualsDataProvider", dataProviderClass = ConstantEqualsDataProvider.class)
  74     public void testConstantEquals(Constant const1, Constant const2, Boolean expected) {
  75         Assert.assertEquals(CONSTANT_REFLECTION_PROVIDER.constantEquals(const1, const2), expected,
  76                         "Unexpected result:");
  77     }
  78 
  79     @Test(dataProvider = "readArrayLengthDataProvider", dataProviderClass = ReadArrayLengthDataProvider.class)
  80     public void testReadArrayLength(JavaConstant array, Integer expected) {
  81         Assert.assertEquals(CONSTANT_REFLECTION_PROVIDER.readArrayLength(array), expected,
  82                         "Unexpected result:");
  83     }
  84 
  85     @Test(dataProvider = "readArrayElementDataProvider", dataProviderClass = ReadArrayElementDataProvider.class)
  86     public void testReadArrayElement(JavaConstant array, int index, Object expected) {
  87         Assert.assertEquals(CONSTANT_REFLECTION_PROVIDER.readArrayElement(array, index), expected,
  88                         "Unexpected result:");
  89     }
  90 
  91     @Test(dataProvider = "readFieldValueDataProvider", dataProviderClass = ReadFieldValueDataProvider.class)
  92     public void testReadFieldValue(ResolvedJavaField field, JavaConstant receiver, JavaConstant expected) {
  93         JavaConstant actual = CONSTANT_REFLECTION_PROVIDER.readFieldValue(field, receiver);
  94         Assert.assertEquals(actual == null ? "null" : actual.toString(),
  95                         expected == null ? "null" : expected.toString(), "Unexpected result:");
  96     }
  97 
  98     @Test(dataProvider = "readFieldValueNegativeDataProvider", dataProviderClass = ReadFieldValueDataProvider.class, expectedExceptions = {NullPointerException.class})
  99     public void testNegativeReadFieldValue(ResolvedJavaField field, JavaConstant receiver) {
 100         CONSTANT_REFLECTION_PROVIDER.readFieldValue(field, receiver);
 101     }
 102 
 103     @Test(dataProvider = "asJavaTypeDataProvider", dataProviderClass = AsJavaTypeDataProvider.class)
 104     public void testAsJavaType(JavaConstant constant, String expected) {
 105         ResolvedJavaType actual = CONSTANT_REFLECTION_PROVIDER.asJavaType(constant);
 106         Assert.assertEquals(actual == null ? "null" : actual.toJavaName(),
 107                         expected == null ? "null" : expected,
 108                         "Unexpected result, wrong type returned:");
 109     }
 110 
 111     @Test(dataProvider = "boxPrimitiveDataProvider", dataProviderClass = BoxPrimitiveDataProvider.class)
 112     public void testBoxPrimitive(JavaConstant constant, JavaConstant expected) {
 113         JavaConstant actual = CONSTANT_REFLECTION_PROVIDER.boxPrimitive(constant);
 114         Assert.assertEquals(actual, expected, "Unexpected result:");
 115     }
 116 
 117     @Test(dataProvider = "unboxPrimitiveDataProvider", dataProviderClass = UnboxPrimitiveDataProvider.class)
 118     public void testUnboxPrimitive(JavaConstant constant, JavaConstant expected) {
 119         JavaConstant actual = CONSTANT_REFLECTION_PROVIDER.unboxPrimitive(constant);
 120         Assert.assertEquals(actual, expected, "Unexpected result:");
 121     }
 122 
 123     @Test(dataProvider = "isEmbeddableDataProvider", dataProviderClass = IsEmbeddableDataProvider.class)
 124     public void testIsEmbeddable(JavaConstant constant, boolean expected) {
 125         boolean actual = CONSTANT_REFLECTION_PROVIDER.isEmbeddable(constant);
 126         Assert.assertEquals(actual, expected, "Unexpected result:");
 127     }
 128 
 129     @Test
 130     public void testGetMemoryAccessProvider() {
 131         MemoryAccessProvider actual = CONSTANT_REFLECTION_PROVIDER.getMemoryAccessProvider();
 132         Assert.assertNotNull(actual, "Returned MemoryAccessProvider instance should not be null");
 133     }
 134 
 135     @Test
 136     public void testGetMethodHandleAccess() {
 137         MethodHandleAccessProvider actual = CONSTANT_REFLECTION_PROVIDER.getMethodHandleAccess();
 138         Assert.assertNotNull(actual,
 139                         "Returned MethodHandleAccessProvider instance should not be null");
 140     }
 141 }