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