1 /* 2 * Copyright (c) 2012, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 /** 27 * @test 28 * @bug 6206780 29 * @summary Test forwarding of methods to super in StringBuffer 30 * @author Jim Gish <jim.gish@oracle.com> 31 */ 32 33 import java.util.ArrayList; 34 import java.util.List; 35 36 public class BufferForwarding { 37 private final static String A_STRING_BUFFER_VAL = "aStringBuffer"; 38 private final static String A_STRING_BUILDER_VAL = "aStringBuilder"; 39 private final static String A_STRING_VAL = "aString"; 40 private final static String NON_EMPTY_VAL = "NonEmpty"; 41 42 public BufferForwarding() { 43 System.out.println( "Starting BufferForwarding"); 44 } 45 46 public static void main(String... args) { 47 new BufferForwarding().executeTestMethods(); 48 } 49 50 public void executeTestMethods() { 51 appendCharSequence(); 52 indexOfString(); 53 indexOfStringIntNull(); 54 indexOfStringNull(); 55 indexOfStringint(); 56 insertintCharSequence(); 57 insertintObject(); 58 insertintboolean(); 59 insertintchar(); 60 insertintdouble(); 61 insertintfloat(); 62 insertintint(); 63 insertintlong(); 64 lastIndexOfString(); 65 lastIndexOfStringint(); 66 } 67 68 public void appendCharSequence() { 69 // three different flavors of CharSequence 70 CharSequence aString = A_STRING_VAL; 71 CharSequence aStringBuilder = new StringBuilder(A_STRING_BUILDER_VAL); 72 CharSequence aStringBuffer = new StringBuffer(A_STRING_BUFFER_VAL); 73 74 assertEquals( /*actual*/ new StringBuilder().append(aString).toString(), /*expected*/ A_STRING_VAL ); 75 assertEquals( new StringBuilder().append(aStringBuilder).toString(), A_STRING_BUILDER_VAL ); 76 assertEquals( new StringBuilder().append(aStringBuffer).toString(), A_STRING_BUFFER_VAL ); 77 78 assertEquals( /*actual*/ new StringBuilder(NON_EMPTY_VAL).append(aString).toString(), NON_EMPTY_VAL+A_STRING_VAL ); 79 assertEquals( new StringBuilder(NON_EMPTY_VAL).append(aStringBuilder).toString(), NON_EMPTY_VAL+A_STRING_BUILDER_VAL ); 80 assertEquals( new StringBuilder(NON_EMPTY_VAL).append(aStringBuffer).toString(), NON_EMPTY_VAL+A_STRING_BUFFER_VAL ); 81 } 82 83 void indexOfString() { 84 StringBuffer sb = new StringBuffer("xyz"); 85 assertEquals( sb.indexOf("y"), 1 ); 86 assertEquals( sb.indexOf("not found"), -1 ); 87 } 88 89 90 public void indexOfStringint() { 91 StringBuffer sb = new StringBuffer("xyyz"); 92 assertEquals( sb.indexOf("y",0), 1 ); 93 assertEquals( sb.indexOf("y",1), 1 ); 94 assertEquals( sb.indexOf("y",2), 2 ); 95 assertEquals( sb.indexOf("not found"), -1 ); 96 } 97 98 99 public void indexOfStringIntNull() { 100 StringBuffer sb = new StringBuffer(); 101 // should be NPE if null passed 102 try { 103 sb.indexOf(null,1); 104 throw new RuntimeException("Test failed: should have thrown NPE"); 105 } catch (NullPointerException npe) { 106 // expected: passed 107 } catch (Throwable t) { 108 throw new RuntimeException("Test failed: should have thrown NPE. Instead threw " 109 + t); 110 } 111 } 112 113 114 public void indexOfStringNull() { 115 StringBuffer sb = new StringBuffer(); 116 117 // should be NPE if null passed 118 try { 119 sb.indexOf(null); 120 throw new RuntimeException("Test failed: should have thrown NPE"); 121 } catch (NullPointerException npe) { 122 // expected: passed 123 } catch (Throwable t) { 124 throw new RuntimeException("Test failed: should have thrown NPE. Instead threw " 125 + t); 126 } 127 } 128 129 130 public void insertintboolean() { 131 boolean b = true; 132 StringBuffer sb = new StringBuffer("012345"); 133 assertEquals( sb.insert( 2, b).toString(), "01true2345"); 134 } 135 136 137 public void insertintchar() { 138 char c = 'C'; 139 StringBuffer sb = new StringBuffer("012345"); 140 assertEquals( sb.insert( 2, c ).toString(), "01C2345"); 141 } 142 143 144 public void insertintCharSequence() { 145 final String initString = "012345"; 146 // three different flavors of CharSequence 147 CharSequence aString = A_STRING_VAL; 148 CharSequence aStringBuilder = new StringBuilder(A_STRING_BUILDER_VAL); 149 CharSequence aStringBuffer = new StringBuffer(A_STRING_BUFFER_VAL); 150 151 assertEquals( new StringBuffer(initString).insert(2, aString).toString(), "01"+A_STRING_VAL+"2345" ); 152 153 assertEquals( new StringBuffer(initString).insert(2, aStringBuilder).toString(), "01"+A_STRING_BUILDER_VAL+"2345" ); 154 155 assertEquals( new StringBuffer(initString).insert(2, aStringBuffer).toString(), "01"+A_STRING_BUFFER_VAL+"2345" ); 156 157 try { 158 new StringBuffer(initString).insert(7, aString); 159 throw new RuntimeException("Test failed: should have thrown IndexOutOfBoundsException"); 160 } catch (IndexOutOfBoundsException soob) { 161 // expected: passed 162 } catch (Throwable t) { 163 throw new RuntimeException("Test failed: should have thrown IndexOutOfBoundsException, but instead threw " + t.getMessage()); 164 165 } 166 } 167 168 public void insertintdouble() { 169 double d = 99d; 170 StringBuffer sb = new StringBuffer("012345"); 171 assertEquals( sb.insert( 2, d ).toString(), "0199.02345"); } 172 173 174 public void insertintfloat() { 175 float f = 99.0f; 176 StringBuffer sb = new StringBuffer("012345"); 177 assertEquals( sb.insert( 2, f ).toString(), "0199.02345"); } 178 179 180 public void insertintint() { 181 int i = 99; 182 StringBuffer sb = new StringBuffer("012345"); 183 assertEquals( sb.insert( 2, i ).toString(), "01992345"); 184 } 185 186 187 public void insertintlong() { 188 long l = 99; 189 StringBuffer sb = new StringBuffer("012345"); 190 assertEquals( sb.insert( 2, l ).toString(), "01992345"); } 191 192 public void insertintObject() { 193 StringBuffer sb = new StringBuffer("012345"); 194 List<String> ls = new ArrayList<String>(); 195 ls.add("A"); ls.add("B"); 196 String lsString = ls.toString(); 197 assertEquals( sb.insert(2, ls).toString(), "01"+lsString+"2345"); 198 199 try { 200 sb.insert(sb.length()+1, ls); 201 throw new RuntimeException("Test failed: should have thrown StringIndexOutOfBoundsException"); 202 } catch (StringIndexOutOfBoundsException soob) { 203 // expected: passed 204 } catch (Throwable t) { 205 throw new RuntimeException("Test failed: should have thrown StringIndexOutOfBoundsException, but instead threw:" 206 + t); 207 208 } 209 } 210 211 212 213 public void lastIndexOfString() { 214 String xyz = "xyz"; 215 String xyz3 = "xyzxyzxyz"; 216 StringBuffer sb = new StringBuffer(xyz3); 217 int pos = sb.lastIndexOf("xyz"); 218 assertEquals( pos, 2*xyz.length() ); 219 } 220 221 public void lastIndexOfStringint() { 222 StringBuffer sb = new StringBuffer("xyzxyzxyz"); 223 int pos = sb.lastIndexOf("xyz",5); 224 assertEquals( pos, 3 ); 225 pos = sb.lastIndexOf("xyz", 6); 226 assertEquals( pos, 6 ); 227 } 228 229 public void assertEquals( String actual, String expected) { 230 if (!actual.equals( expected )) { 231 throw new RuntimeException( "Test failed: actual = '" + actual + 232 "', expected = '" + expected + "'"); 233 } 234 } 235 236 public void assertEquals( int actual, int expected) { 237 if (actual != expected) { 238 throw new RuntimeException( "Test failed: actual = '" + actual + 239 "', expected = '" + expected + "'"); 240 } 241 } 242 }