StringConversion.java revision 553:9d9f26857129
1/* 2 * Copyright (c) 1997, 1998, 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 4030173 4071548 27 * @summary Verify correct conversion of non-string arguments in string concatenation. 28 * @author maddox 29 * 30 * @compile StringConversion.java 31 * @run main StringConversion 32 */ 33 34class FooBar { 35 public String toString(){ 36 return null; 37 } 38} 39 40public class StringConversion { 41 42 static void check(int testid, String have, String expect) 43 throws Exception { 44 if ((have == null && have != expect) || 45 (have != null && !have.equals(expect))) { 46 String msg = 47 "TEST " + testid + ": HAVE \"" + 48 have + "\" EXPECT \"" + expect + "\""; 49 System.out.println("StringConversion: " + msg); 50 throw new Exception(msg); 51 } 52 } 53 54 public static void main(String[] args) throws Exception { 55 56 String s; 57 String n = null; 58 Object o = null; 59 FooBar m = new FooBar(); 60 61 // Null reference must be converted to "null" 62 63 // Conversion will be done by 'StringBuffer.append'. 64 s = "foo" + n; 65 check(11, s, "foonull"); 66 s = n + "bar"; 67 check(12, s, "nullbar"); 68 s = "foo" + o; 69 check(13, s, "foonull"); 70 s = o + "bar"; 71 check(14, s, "nullbar"); 72 73 // Conversion will be done by 'String.valueOf'. 74 s = "" + n; 75 check(21, s, "null"); 76 s = n + ""; 77 check(22, s, "null"); 78 s = "" + o; 79 check(23, s, "null"); 80 s = o + ""; 81 check(24, s, "null"); 82 83 // Null 'toString' result must be converted to "null" 84 85 // Conversion will be done by 'StringBuffer.append'. 86 s = "foo" + m; 87 check(31, s, "foonull"); 88 s = m + "bar"; 89 check(32, s, "nullbar"); 90 91 // Conversion will be done by 'String.valueOf'. 92 s = "" + m; 93 check(43, s, "null"); 94 s = m + ""; 95 check(44, s, "null"); 96 97 // A character array must be converted as if by 98 // 'toString', i.e., it is treated as an 'Object'. 99 100 s = "polymorph"; 101 char[] ca = {'i', 's', 'm'}; 102 103 check(51, s + ca, s + ca.toString()); 104 check(52, ca + s, ca.toString() + s); 105 106 System.out.println("OK"); 107 } 108} 109