Path: blob/master/test/jdk/java/lang/invoke/ArrayConstructorTest.java
41149 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/* @test26* @bug 815510627* @run testng/othervm -ea -esa test.java.lang.invoke.ArrayConstructorTest28*/29package test.java.lang.invoke;3031import java.lang.invoke.MethodHandle;32import java.lang.invoke.MethodHandles;3334import static java.lang.invoke.MethodType.methodType;3536import static org.testng.AssertJUnit.*;3738import org.testng.annotations.*;394041public class ArrayConstructorTest {4243static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup();4445@Test46public static void testFindConstructorArray() {47boolean caught = false;48try {49MethodHandle h = LOOKUP.findConstructor(Object[].class, methodType(void.class));50} catch (NoSuchMethodException nsme) {51assertEquals("no constructor for array class: [Ljava.lang.Object;", nsme.getMessage());52caught = true;53} catch (Exception e) {54throw new AssertionError("unexpected exception: " + e);55}56assertTrue(caught);57}5859@DataProvider60static Object[][] arrayConstructorNegative() {61return new Object[][]{62{String.class, IllegalArgumentException.class, "not an array class: java.lang.String"},63{null, NullPointerException.class, null}64};65}6667@Test(dataProvider = "arrayConstructorNegative")68public static void testArrayConstructorNegative(Class<?> clazz, Class<?> exceptionClass, String message) {69boolean caught = false;70try {71MethodHandle h = MethodHandles.arrayConstructor(clazz);72} catch (Exception e) {73assertEquals(exceptionClass, e.getClass());74if (message != null) {75assertEquals(message, e.getMessage());76}77caught = true;78}79assertTrue(caught);80}8182@Test83public static void testArrayConstructor() throws Throwable {84MethodHandle h = MethodHandles.arrayConstructor(String[].class);85assertEquals(methodType(String[].class, int.class), h.type());86String[] a = (String[]) h.invoke(17);87assertEquals(17, a.length);88}8990@Test(expectedExceptions = {NegativeArraySizeException.class})91public static void testArrayConstructorNegativeIndex() throws Throwable {92MethodHandle h = MethodHandles.arrayConstructor(String[].class);93assertEquals(methodType(String[].class, int.class), h.type());94h.invoke(-1); // throws exception95}9697}9899100