< prev index next >

test/jdk/java/lang/reflect/records/RecordReflectionTest.java

Print this page




   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  * @summary reflection test for records
  27  * @compile --enable-preview -source ${jdk.version} RecordReflectionTest.java
  28  * @run testng/othervm --enable-preview RecordReflectionTest
  29  */
  30 
  31 import java.lang.annotation.*;
  32 import java.lang.reflect.*;
  33 import java.util.List;
  34 
  35 import org.testng.annotations.*;
  36 import static org.testng.Assert.*;
  37 
  38 @Test
  39 public class RecordReflectionTest {
  40 
  41     class NoRecord {}
  42 
  43     record R1() {}
  44 
  45     record R2(int i, int j) {}
  46 
  47     record R3(List<String> ls) {}
  48 
  49     record R4(R1 r1, R2 r2, R3 r3) {}
  50 
  51     public void testIsRecord() {
  52         assertFalse(NoRecord.class.isRecord());
  53 
  54         for (Class<?> c : List.of(R1.class, R2.class, R3.class))
  55             assertTrue(c.isRecord());



  56     }
  57 
  58     public void testGetComponentsNoRecord() {
  59         assertTrue(NoRecord.class.getRecordComponents().length == 0);
  60     }
  61 
  62     @DataProvider(name = "reflectionData")
  63     public Object[][] reflectionData() {
  64         return new Object[][] {
  65             new Object[] { new R1(),
  66                            0,
  67                            null,
  68                            null,
  69                            null },
  70             new Object[] { new R2(1, 2),
  71                            2,
  72                            new Object[]{ 1, 2 },
  73                            new String[]{ "i", "j" },
  74                            new String[]{ "int", "int"} },
  75             new Object[] { new R3(List.of("1")),




   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  * @bug 8235369
  27  * @summary reflection test for records
  28  * @compile --enable-preview -source ${jdk.version} RecordReflectionTest.java
  29  * @run testng/othervm --enable-preview RecordReflectionTest
  30  */
  31 
  32 import java.lang.annotation.*;
  33 import java.lang.reflect.*;
  34 import java.util.List;
  35 
  36 import org.testng.annotations.*;
  37 import static org.testng.Assert.*;
  38 
  39 @Test
  40 public class RecordReflectionTest {
  41 
  42     class NoRecord {}
  43 
  44     record R1() {}
  45 
  46     record R2(int i, int j) {}
  47 
  48     record R3(List<String> ls) {}
  49 
  50     record R4(R1 r1, R2 r2, R3 r3) {}
  51 
  52     public void testIsRecord() {
  53         assertFalse(NoRecord.class.isRecord());
  54 
  55         for (Class<?> c : List.of(R1.class, R2.class, R3.class)) {
  56             String message = c.toGenericString();
  57             assertTrue(c.isRecord(), message);
  58             assertTrue(message.contains("record") , message);
  59         }
  60     }
  61 
  62     public void testGetComponentsNoRecord() {
  63         assertTrue(NoRecord.class.getRecordComponents().length == 0);
  64     }
  65 
  66     @DataProvider(name = "reflectionData")
  67     public Object[][] reflectionData() {
  68         return new Object[][] {
  69             new Object[] { new R1(),
  70                            0,
  71                            null,
  72                            null,
  73                            null },
  74             new Object[] { new R2(1, 2),
  75                            2,
  76                            new Object[]{ 1, 2 },
  77                            new String[]{ "i", "j" },
  78                            new String[]{ "int", "int"} },
  79             new Object[] { new R3(List.of("1")),


< prev index next >