Path: blob/master/test/jdk/java/lang/String/concat/ImplicitStringConcat.java
41153 views
/*1* Copyright (c) 2015, 2016, 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* @summary test implicit String concatenations26*27* @compile ImplicitStringConcat.java28* @run main/othervm -Xverify:all ImplicitStringConcat29*30* @compile -XDstringConcat=inline ImplicitStringConcat.java31* @run main/othervm -Xverify:all ImplicitStringConcat32*33* @compile -XDstringConcat=indy ImplicitStringConcat.java34* @run main/othervm -Xverify:all ImplicitStringConcat35*36* @compile -XDstringConcat=indyWithConstants ImplicitStringConcat.java37* @run main/othervm -Xverify:all ImplicitStringConcat38*/39import java.lang.StringBuilder;4041public class ImplicitStringConcat {4243static boolean b = true;44static byte by = 42;45static short sh = 42;46static char ch = 'a';47static int i = 42;48static float fl = 42.0f;49static long l = 42;50static double d = 42.0d;51static String s = "foo";52static String sNull = null;53static Object o = "bar";54static Object oNull = null;55static CharSequence cs = "bar";56static char[] chars = new char[] {'a'};5758static MyClass myCl = new MyClass();59static MyClassNull myClNull = new MyClassNull();60static Object myCl2 = new MyClass();61static Object[] myArr = new Object[] { myCl };62static final Object[] s_myArr = new Object[] { myCl };6364static StringBuffer sb = new StringBuffer("a");6566public static void main(String[] args) throws Exception {6768test("footrue", s + b);69test("foo42", s + by);70test("foo42", s + sh);71test("fooa", s + ch);72test("foo42", s + i);73test("foo42", s + l);74test("foo42.0", s + fl);75test("foo42.0", s + d);76test("foofoo", s + s);77test("foonull", s + sNull);78test("foobar", s + o);79test("foonull", s + oNull);80test("foobar", s + cs);8182{83StringBuilder sb = new StringBuilder();84sb.append("foo");85sb.append(myArr.toString());86test(sb.toString(), s + myArr);87}8889{90StringBuilder sb = new StringBuilder();91sb.append("foo");92sb.append(s_myArr.toString());93test(sb.toString(), s + s_myArr);94}9596{97StringBuilder sb = new StringBuilder();98sb.append("foo[C@");99sb.append(Integer.toHexString(System.identityHashCode(chars)));100test(sb.toString(), s + chars);101}102103test("fooa", s + ImplicitStringConcat.sb);104test("foonull", s + null);105test("fooMyClass", s + myCl);106test("foonull", s + myClNull);107test("fooMyClass", s + myCl2);108109s = "foo"; s += b; test("footrue", s);110s = "foo"; s += by; test("foo42", s);111s = "foo"; s += sh; test("foo42", s);112s = "foo"; s += ch; test("fooa", s);113s = "foo"; s += i; test("foo42", s);114s = "foo"; s += l; test("foo42", s);115s = "foo"; s += fl; test("foo42.0", s);116s = "foo"; s += d; test("foo42.0", s);117s = "foo"; s += s; test("foofoo", s);118s = "foo"; s += sNull; test("foonull", s);119s = "foo"; s += o; test("foobar", s);120s = "foo"; s += oNull; test("foonull", s);121s = "foo"; s += cs; test("foobar", s);122123{124StringBuilder sb = new StringBuilder();125sb.append("foo[C@");126sb.append(Integer.toHexString(System.identityHashCode(chars)));127s = "foo";128s += chars;129test(sb.toString(), s);130}131132s = "foo"; s += ImplicitStringConcat.sb; test("fooa", s);133s = "foo"; s += null; test("foonull", s);134s = "foo"; s += myCl; test("fooMyClass", s);135s = "foo"; s += myCl2; test("fooMyClass", s);136}137138public static void test(String expected, String actual) {139// Fingers crossed: String concat should work.140if (!expected.equals(actual)) {141StringBuilder sb = new StringBuilder();142sb.append("Expected = ");143sb.append(expected);144sb.append(", actual = ");145sb.append(actual);146throw new IllegalStateException(sb.toString());147}148}149150static class MyClass {151public String toString() {152return "MyClass";153}154}155156static class MyClassNull {157public String toString() {158return null;159}160}161}162163164