Path: blob/master/test/jdk/java/lang/invoke/ArrayLengthTest.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* @run testng/othervm -ea -esa test.java.lang.invoke.ArrayLengthTest27*/28package test.java.lang.invoke;2930import java.lang.invoke.MethodHandle;31import java.lang.invoke.MethodHandles;3233import static org.testng.AssertJUnit.*;3435import org.testng.annotations.*;3637public class ArrayLengthTest {3839@DataProvider40Object[][] arrayClasses() {41return new Object[][] {42{int[].class},43{long[].class},44{float[].class},45{double[].class},46{boolean[].class},47{byte[].class},48{short[].class},49{char[].class},50{Object[].class},51{StringBuffer[].class}52};53}5455@Test(dataProvider = "arrayClasses")56public void testArrayLength(Class<?> arrayClass) throws Throwable {57MethodHandle arrayLength = MethodHandles.arrayLength(arrayClass);58assertEquals(int.class, arrayLength.type().returnType());59assertEquals(arrayClass, arrayLength.type().parameterType(0));60Object array = MethodHandles.arrayConstructor(arrayClass).invoke(10);61assertEquals(10, arrayLength.invoke(array));62}6364@Test(dataProvider = "arrayClasses", expectedExceptions = NullPointerException.class)65public void testArrayLengthInvokeNPE(Class<?> arrayClass) throws Throwable {66MethodHandle arrayLength = MethodHandles.arrayLength(arrayClass);67arrayLength.invoke(null);68}6970@Test(expectedExceptions = IllegalArgumentException.class)71public void testArrayLengthNoArray() {72MethodHandles.arrayLength(String.class);73}7475@Test(expectedExceptions = NullPointerException.class)76public void testArrayLengthNPE() {77MethodHandles.arrayLength(null);78}7980@Test(expectedExceptions = NullPointerException.class)81public void testNullReference() throws Throwable {82MethodHandle arrayLength = MethodHandles.arrayLength(String[].class);83int len = (int)arrayLength.invokeExact((String[])null);84}85}868788