1 /*
   2  * Copyright (c) 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 import java.lang.invoke.CallSite;
  25 import java.lang.invoke.MethodHandles;
  26 import java.lang.invoke.MethodType;
  27 import java.lang.invoke.StringConcatFactory;
  28 
  29 /**
  30  * @test
  31  * @summary StringConcatFactory allow recipes with repeated constants, but this
  32  *          is not expressible with java code and needs an explicit sanity test
  33  * @bug 8222852
  34  *
  35  * @compile StringConcatFactoryRepeatedConstants.java
  36  *
  37  * @run main/othervm -Xverify:all -Djava.lang.invoke.stringConcat=BC_SB                  StringConcatFactoryRepeatedConstants
  38  * @run main/othervm -Xverify:all -Djava.lang.invoke.stringConcat=BC_SB_SIZED            StringConcatFactoryRepeatedConstants
  39  * @run main/othervm -Xverify:all -Djava.lang.invoke.stringConcat=MH_SB_SIZED            StringConcatFactoryRepeatedConstants
  40  * @run main/othervm -Xverify:all -Djava.lang.invoke.stringConcat=BC_SB_SIZED_EXACT      StringConcatFactoryRepeatedConstants
  41  * @run main/othervm -Xverify:all -Djava.lang.invoke.stringConcat=MH_SB_SIZED_EXACT      StringConcatFactoryRepeatedConstants
  42  * @run main/othervm -Xverify:all -Djava.lang.invoke.stringConcat=MH_INLINE_SIZED_EXACT  StringConcatFactoryRepeatedConstants
  43  *
  44 */
  45 public class StringConcatFactoryRepeatedConstants {
  46 
  47     public static void main(String[] args) throws Throwable {
  48 
  49         CallSite site = StringConcatFactory.makeConcatWithConstants(
  50             MethodHandles.lookup(),
  51             "foo",
  52             MethodType.methodType(String.class),
  53             "\u0002\u0002",
  54             "foo", "bar"
  55         );
  56         String string = (String)site.dynamicInvoker().invoke();
  57         if (!"foobar".equals(string)) {
  58             throw new IllegalStateException("Expected: foobar, got: " + string);
  59         }
  60 
  61         site = StringConcatFactory.makeConcatWithConstants(
  62                 MethodHandles.lookup(),
  63                 "foo",
  64                 MethodType.methodType(String.class),
  65                 "\u0002\u0002\u0002\u0002",
  66                 "foo", 17.0f, 4711L, "bar"
  67         );
  68         string = (String)site.dynamicInvoker().invoke();
  69         StringBuilder sb = new StringBuilder();
  70         sb.append("foo").append(17.0f).append(4711L).append("bar");
  71         if (!sb.toString().equals(string)) {
  72             throw new IllegalStateException("Expected: " + sb.toString() + ", got: " + string);
  73         }
  74     }
  75 
  76 }