Path: blob/master/test/jdk/java/lang/constant/NameValidationTest.java
41149 views
/*1* Copyright (c) 2018, 2019, 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* @bug 821551026* @compile NameValidationTest.java27* @run testng NameValidationTest28* @summary unit tests for verifying member names29*/3031import java.lang.constant.*;32import java.lang.invoke.*;3334import org.testng.annotations.Test;3536import static java.lang.constant.DirectMethodHandleDesc.*;37import static java.lang.constant.ConstantDescs.*;38import static java.lang.constant.DirectMethodHandleDesc.Kind.VIRTUAL;3940import static org.testng.Assert.fail;4142@Test43public class NameValidationTest {4445private static final String[] badMemberNames = new String[] {"xx.xx", "zz;zz", "[l", "aa/aa", "<cinit>"};46private static final String[] goodMemberNames = new String[] {"<clinit>", "<init>", "3", "~", "$", "qq"};4748private static final String[] badClassNames = new String[] {"zz;zz", "[l", "aa/aa", ".", "a..b"};49private static final String[] goodClassNames = new String[] {"3", "~", "$", "qq", "a.a"};5051public void testMemberNames() {52DirectMethodHandleDesc mh = MethodHandleDesc.of(Kind.VIRTUAL, CD_String, "isEmpty", "()Z");53for (String badName : badMemberNames) {54try {55memberNamesHelper(badName, mh, CD_int, null);56fail("Expected failure for name " + badName);57} catch (IllegalArgumentException iae) {58// expected59}60try {61memberNamesHelper(badName, mh, CD_int, new ConstantDesc[0]);62fail("Expected failure for name " + badName);63} catch (IllegalArgumentException iae) {64// expected65}66}6768for (String badName : goodMemberNames) {69memberNamesHelper(badName, mh, CD_int, null);70memberNamesHelper(badName, mh, CD_int, new ConstantDesc[0]);71}72}7374private void memberNamesHelper(String constantName,75DirectMethodHandleDesc bootstrapMethod,76ClassDesc constantType,77ConstantDesc... bootstrapArgs) {78if (bootstrapArgs == null) {79DynamicConstantDesc.ofNamed(bootstrapMethod, constantName, constantType);80} else {81DynamicConstantDesc.ofNamed(bootstrapMethod, constantName, constantType, bootstrapArgs);82}83}8485public void testClassNames() {86for (String badName : badClassNames) {87try {88ClassDesc.of(badName);89fail("Expected failure for name " + badName);90} catch (IllegalArgumentException iae) {91// expected92}93}9495for (String goodName : goodClassNames) {96ClassDesc.of(goodName);97}98}99}100101102