Path: blob/master/test/jdk/java/lang/constant/ConvertTest.java
41149 views
/*1* Copyright (c) 2020, 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.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @run testng ConvertTest26*/2728import org.testng.annotations.DataProvider;29import org.testng.annotations.Test;3031import java.lang.invoke.ConstantBootstraps;32import java.math.BigInteger;3334import static org.testng.Assert.assertEquals;3536public class ConvertTest {3738@DataProvider39public static Object[][] cceInputs() {40return new Object[][]{41{ void.class, null },42{ Integer.class, "a" },43{ int.class, BigInteger.ZERO },44};45}4647@Test(dataProvider = "cceInputs", expectedExceptions = ClassCastException.class)48public void testBadConversion(Class<?> dstType, Object value) {49ConstantBootstraps.explicitCast(null, null, dstType, value);50}5152@DataProvider53public static Object[][] goodInputs() {54Object o = new Object();55return new Object[][]{56{ Object.class, null, null },57{ Object.class, o, o },58{ String.class, "abc", "abc" },59{ short.class, 10, (short) 10 },60{ int.class, (short) 10, 10 },61{ boolean.class, 1, true },62{ boolean.class, 2, false },63{ int.class, true, 1 },64{ int.class, false, 0 },65{ int.class, 10, 10 },66{ Integer.class, 10, 10 },67{ Object.class, 10, 10 },68{ Number.class, 10, 10 },69};70}7172@Test(dataProvider = "goodInputs")73public void testSuccess(Class<?> dstType, Object value, Object expected) {74Object actual = ConstantBootstraps.explicitCast(null, null, dstType, value);75assertEquals(actual, expected);76}7778}798081