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 package jdk.vm.ci.hotspot.test;
  25 
  26 import jdk.vm.ci.meta.JavaConstant;
  27 import org.testng.annotations.DataProvider;
  28 
  29 import java.util.LinkedList;
  30 
  31 import static jdk.vm.ci.hotspot.test.TestHelper.ARRAYS_MAP;
  32 import static jdk.vm.ci.hotspot.test.TestHelper.ARRAY_ARRAYS_MAP;
  33 import static jdk.vm.ci.hotspot.test.TestHelper.CONSTANT_REFLECTION_PROVIDER;
  34 import static jdk.vm.ci.hotspot.test.TestHelper.DUMMY_CLASS_CONSTANT;
  35 import static jdk.vm.ci.hotspot.test.TestHelper.DUMMY_CLASS_INSTANCE;
  36 import static jdk.vm.ci.hotspot.test.TestHelper.INSTANCE_FIELDS_MAP;
  37 import static jdk.vm.ci.hotspot.test.TestHelper.INSTANCE_STABLE_FIELDS_MAP;
  38 import static jdk.vm.ci.hotspot.test.TestHelper.STABLE_ARRAYS_MAP;
  39 import static jdk.vm.ci.hotspot.test.TestHelper.STABLE_ARRAY_ARRAYS_MAP;
  40 import static jdk.vm.ci.hotspot.test.TestHelper.STATIC_FIELDS_MAP;
  41 import static jdk.vm.ci.hotspot.test.TestHelper.STATIC_STABLE_FIELDS_MAP;
  42 
  43 public class ReadFieldValueDataProvider {
  44 
  45     @DataProvider(name = "readFieldValueDataProvider")
  46     public static Object[][] readFieldValueDataProvider() {
  47         LinkedList<Object[]> cfgSet = new LinkedList<>();
  48         // Testing instance non-stable fields
  49         INSTANCE_FIELDS_MAP.entrySet().stream().forEach((instanceField) -> {
  50             cfgSet.add(new Object[]{instanceField.getKey(),
  51                             DUMMY_CLASS_CONSTANT,
  52                             instanceField.getValue()});
  53         });
  54         // Testing static non-stable fields with null as receiver
  55         STATIC_FIELDS_MAP.entrySet().stream().forEach((staticField) -> {
  56             cfgSet.add(new Object[]{staticField.getKey(), null, staticField.getValue()});
  57         });
  58         // Testing static non-stable fields with JavaConstant.NULL_POINTER as receiver
  59         STATIC_FIELDS_MAP.entrySet().stream().forEach((staticField) -> {
  60             cfgSet.add(new Object[]{staticField.getKey(),
  61                             JavaConstant.NULL_POINTER,
  62                             staticField.getValue()});
  63         });
  64         // Testing instance stable fields
  65         INSTANCE_STABLE_FIELDS_MAP.entrySet().stream().forEach((instanceField) -> {
  66             cfgSet.add(new Object[]{instanceField.getKey(),
  67                             DUMMY_CLASS_CONSTANT,
  68                             instanceField.getValue()});
  69         });
  70         // Testing static stable fields with null as receiver
  71         STATIC_STABLE_FIELDS_MAP.entrySet().stream().forEach((staticField) -> {
  72             cfgSet.add(new Object[]{staticField.getKey(), null, staticField.getValue()});
  73         });
  74         // Testing static stable fields with JavaConstant.NULL_POINTER as receiver
  75         STATIC_STABLE_FIELDS_MAP.entrySet().stream().forEach((staticField) -> {
  76             cfgSet.add(new Object[]{staticField.getKey(),
  77                             JavaConstant.NULL_POINTER,
  78                             staticField.getValue()});
  79         });
  80         // Testing instance non-stable array fields
  81         ARRAYS_MAP.entrySet().stream().forEach((instanceField) -> {
  82             cfgSet.add(new Object[]{instanceField.getKey(),
  83                             DUMMY_CLASS_CONSTANT,
  84                             instanceField.getValue()});
  85         });
  86         // Testing instance stable array fields
  87         STABLE_ARRAYS_MAP.entrySet().stream().forEach((instanceField) -> {
  88             cfgSet.add(new Object[]{instanceField.getKey(),
  89                             DUMMY_CLASS_CONSTANT,
  90                             instanceField.getValue()});
  91         });
  92         // Testing instance non-stable array-of-array fields
  93         ARRAY_ARRAYS_MAP.entrySet().stream().forEach((instanceField) -> {
  94             cfgSet.add(new Object[]{instanceField.getKey(),
  95                             DUMMY_CLASS_CONSTANT,
  96                             instanceField.getValue()});
  97         });
  98         // Testing instance stable array-of-array fields
  99         STABLE_ARRAY_ARRAYS_MAP.entrySet().stream().forEach((instanceField) -> {
 100             cfgSet.add(new Object[]{instanceField.getKey(),
 101                             DUMMY_CLASS_CONSTANT,
 102                             instanceField.getValue()});
 103         });
 104         // Testing instance fields with JavaConstant.NULL_POINTER as receiver
 105         INSTANCE_FIELDS_MAP.entrySet().stream().forEach((instanceField) -> {
 106             cfgSet.add(new Object[]{instanceField.getKey(), JavaConstant.NULL_POINTER, null});
 107         });
 108         // Testing instance fields with an object that does not have the field
 109         INSTANCE_FIELDS_MAP.entrySet().stream().forEach((instanceField) -> {
 110             cfgSet.add(new Object[]{instanceField.getKey(),
 111                             CONSTANT_REFLECTION_PROVIDER.forObject(DUMMY_CLASS_INSTANCE.objectField),
 112                             null});
 113         });
 114         return cfgSet.toArray(new Object[0][0]);
 115     }
 116 
 117     @DataProvider(name = "readFieldValueNegativeDataProvider")
 118     public static Object[][] readFieldValueNegativeDataProvider() {
 119         LinkedList<Object[]> cfgSet = new LinkedList<>();
 120         // Testing instance fields with null as receiver
 121         INSTANCE_FIELDS_MAP.keySet().stream().forEach((instanceField) -> {
 122             cfgSet.add(new Object[]{instanceField, null});
 123         });
 124         // Testing null as a field argument
 125         cfgSet.add(new Object[]{null, null});
 126         return cfgSet.toArray(new Object[0][0]);
 127     }
 128 }