1 /* 2 * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 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 8215510 27 * @compile NameValidationTest.java 28 * @run testng NameValidationTest 29 * @summary unit tests for verifying member names 30 */ 31 package test.java.lang.constant; 32 33 import java.lang.constant.*; 34 import java.lang.invoke.*; 35 36 import org.testng.annotations.Test; 37 38 import static java.lang.constant.DirectMethodHandleDesc.*; 39 import static java.lang.constant.ConstantDescs.*; 40 import static java.lang.constant.DirectMethodHandleDesc.Kind.VIRTUAL; 41 42 import static org.testng.Assert.fail; 43 44 @Test 45 public class NameValidationTest { 46 47 private static final String[] badMemberNames = new String[] {"xx.xx", "zz;zz", "[l", "aa/aa", "<cinit>"}; 48 private static final String[] goodMemberNames = new String[] {"<clinit>", "<init>", "3", "~", "$", "qq"}; 49 50 private static final String[] badClassNames = new String[] {"zz;zz", "[l", "aa/aa", ".", "a..b"}; 51 private static final String[] goodClassNames = new String[] {"3", "~", "$", "qq", "a.a"}; 52 testMemberNames()53 public void testMemberNames() { 54 DirectMethodHandleDesc mh = MethodHandleDesc.of(Kind.VIRTUAL, CD_String, "isEmpty", "()Z"); 55 for (String badName : badMemberNames) { 56 try { 57 memberNamesHelper(badName, mh, CD_int, null); 58 fail("Expected failure for name " + badName); 59 } catch (IllegalArgumentException iae) { 60 // expected 61 } 62 try { 63 memberNamesHelper(badName, mh, CD_int, new ConstantDesc[0]); 64 fail("Expected failure for name " + badName); 65 } catch (IllegalArgumentException iae) { 66 // expected 67 } 68 } 69 70 for (String badName : goodMemberNames) { 71 memberNamesHelper(badName, mh, CD_int, null); 72 memberNamesHelper(badName, mh, CD_int, new ConstantDesc[0]); 73 } 74 } 75 memberNamesHelper(String constantName, DirectMethodHandleDesc bootstrapMethod, ClassDesc constantType, ConstantDesc... bootstrapArgs)76 private void memberNamesHelper(String constantName, 77 DirectMethodHandleDesc bootstrapMethod, 78 ClassDesc constantType, 79 ConstantDesc... bootstrapArgs) { 80 if (bootstrapArgs == null) { 81 DynamicConstantDesc.ofNamed(bootstrapMethod, constantName, constantType); 82 } else { 83 DynamicConstantDesc.ofNamed(bootstrapMethod, constantName, constantType, bootstrapArgs); 84 } 85 } 86 testClassNames()87 public void testClassNames() { 88 for (String badName : badClassNames) { 89 try { 90 ClassDesc.of(badName); 91 fail("Expected failure for name " + badName); 92 } catch (IllegalArgumentException iae) { 93 // expected 94 } 95 } 96 97 for (String goodName : goodClassNames) { 98 ClassDesc.of(goodName); 99 } 100 } 101 } 102