BuilderForwarding.java revision 12745:f068a4ffddd2
1/* 2 * Copyright (c) 2012, 2013, 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 6206780 27 * @summary Test forwarding of methods to super in StringBuilder 28 * @author Jim Gish <jim.gish@oracle.com> 29 */ 30 31import java.util.ArrayList; 32import java.util.List; 33 34public class BuilderForwarding { 35 private static final String A_STRING_BUFFER_VAL = "aStringBuffer"; 36 private static final String A_STRING_BUILDER_VAL = "aStringBuilder"; 37 private static final String A_STRING_VAL = "aString"; 38 private static final String NON_EMPTY_VAL = "NonEmpty"; 39 40 public BuilderForwarding() { 41 System.out.println( "Starting BuilderForwarding"); 42 } 43 44 public static void main(String... args) { 45 new BuilderForwarding().executeTestMethods(); 46 } 47 48 public void executeTestMethods() { 49 appendCharSequence(); 50 indexOfString(); 51 indexOfStringIntNull(); 52 indexOfStringNull(); 53 indexOfStringint(); 54 insertintCharSequence(); 55 insertintObject(); 56 insertintboolean(); 57 insertintchar(); 58 insertintdouble(); 59 insertintfloat(); 60 insertintint(); 61 insertintlong(); 62 lastIndexOfString(); 63 lastIndexOfStringint(); 64 } 65 66 public void appendCharSequence() { 67 // three different flavors of CharSequence 68 CharSequence aString = A_STRING_VAL; 69 CharSequence aStringBuilder = new StringBuilder(A_STRING_BUILDER_VAL); 70 CharSequence aStringBuffer = new StringBuffer(A_STRING_BUFFER_VAL); 71 72 assertEquals( /*actual*/ new StringBuilder().append(aString).toString(), /*expected*/ A_STRING_VAL ); 73 assertEquals( new StringBuilder().append(aStringBuilder).toString(), A_STRING_BUILDER_VAL ); 74 assertEquals( new StringBuilder().append(aStringBuffer).toString(), A_STRING_BUFFER_VAL ); 75 76 assertEquals( /*actual*/ new StringBuilder(NON_EMPTY_VAL).append(aString).toString(), NON_EMPTY_VAL+A_STRING_VAL ); 77 assertEquals( new StringBuilder(NON_EMPTY_VAL).append(aStringBuilder).toString(), NON_EMPTY_VAL+A_STRING_BUILDER_VAL ); 78 assertEquals( new StringBuilder(NON_EMPTY_VAL).append(aStringBuffer).toString(), NON_EMPTY_VAL+A_STRING_BUFFER_VAL ); 79 } 80 81 82 public void indexOfString() { 83 StringBuilder sb = new StringBuilder(); 84 // should be NPE if null passed 85 try { 86 87 sb.indexOf(null); 88 throw new RuntimeException("Test failed: should have thrown NPE"); 89 90 91 } catch (NullPointerException npe) { 92 // expected: passed 93 } catch (Throwable t) { 94 throw new RuntimeException("Test failed: should have thrown NPE. Instead threw " 95 + t); 96 } 97 sb = new StringBuilder("xyz"); 98 assertEquals( sb.indexOf("y"), 1 ); 99 assertEquals( sb.indexOf("not found"), -1 ); 100 } 101 102 103 public void indexOfStringint() { 104 StringBuilder sb = new StringBuilder(); 105 // should be NPE if null passed 106 try { 107 sb.indexOf(null,1); 108 throw new RuntimeException("Test failed: should have thrown NPE"); 109 } catch (NullPointerException npe) { 110 // expected: passed 111 } catch (Throwable t) { 112 throw new RuntimeException("Test failed: should have thrown NPE"); 113 } 114 sb = new StringBuilder("xyyz"); 115 assertEquals( sb.indexOf("y",0), 1 ); 116 assertEquals( sb.indexOf("y",1), 1 ); 117 assertEquals( sb.indexOf("y",2), 2 ); 118 assertEquals( sb.indexOf("not found"), -1 ); 119 } 120 121 122 public void indexOfStringIntNull() { 123 StringBuffer sb = new StringBuffer(); 124 // should be NPE if null passed 125 try { 126 sb.indexOf(null,1); 127 throw new RuntimeException("Test failed: should have thrown NPE"); 128 } catch (NullPointerException npe) { 129 // expected: passed 130 } catch (Throwable t) { 131 throw new RuntimeException("Test failed: should have thrown NPE. Instead threw " 132 + t); 133 } 134 } 135 136 137 public void indexOfStringNull() { 138 StringBuilder sb = new StringBuilder(); 139 140 // should be NPE if null passed 141 try { 142 sb.indexOf(null); 143 throw new RuntimeException("Test failed: should have thrown NPE"); 144 } catch (NullPointerException npe) { 145 // expected: passed 146 } catch (Throwable t) { 147 throw new RuntimeException("Test failed: should have thrown NPE. Instead threw " 148 + t); 149 } 150 } 151 152 153 public void insertintboolean() { 154 boolean b = true; 155 StringBuilder sb = new StringBuilder("012345"); 156 assertEquals( sb.insert( 2, b).toString(), "01true2345"); 157 } 158 159 160 public void insertintchar() { 161 char c = 'C'; 162 StringBuilder sb = new StringBuilder("012345"); 163 assertEquals( sb.insert( 2, c ).toString(), "01C2345"); 164 } 165 166 167 public void insertintCharSequence() { 168 final String initString = "012345"; 169 // three different flavors of CharSequence 170 CharSequence aString = A_STRING_VAL; 171 CharSequence aStringBuilder = new StringBuilder(A_STRING_BUILDER_VAL); 172 CharSequence aStringBuffer = new StringBuffer(A_STRING_BUFFER_VAL); 173 174 assertEquals( new StringBuilder(initString).insert(2, aString).toString(), "01"+A_STRING_VAL+"2345" ); 175 176 assertEquals( new StringBuilder(initString).insert(2, aStringBuilder).toString(), "01"+A_STRING_BUILDER_VAL+"2345" ); 177 178 assertEquals( new StringBuilder(initString).insert(2, aStringBuffer).toString(), "01"+A_STRING_BUFFER_VAL+"2345" ); 179 180 try { 181 new StringBuilder(initString).insert(7, aString); 182 throw new RuntimeException("Test failed: should have thrown IndexOutOfBoundsException"); 183 } catch (IndexOutOfBoundsException soob) { 184 // expected: passed 185 } catch (Throwable t) { 186 throw new RuntimeException("Test failed: should have thrown IndexOutOfBoundsException, but instead threw " + t.getMessage()); 187 188 } 189 } 190 191 192 public void insertintdouble() { 193 double d = 99d; 194 StringBuilder sb = new StringBuilder("012345"); 195 assertEquals( sb.insert( 2, d ).toString(), "0199.02345"); } 196 197 198 public void insertintfloat() { 199 float f = 99.0f; 200 StringBuilder sb = new StringBuilder("012345"); 201 assertEquals( sb.insert( 2, f ).toString(), "0199.02345"); } 202 203 204 public void insertintint() { 205 int i = 99; 206 StringBuilder sb = new StringBuilder("012345"); 207 assertEquals( sb.insert( 2, i ).toString(), "01992345"); 208 } 209 210 211 public void insertintlong() { 212 long l = 99; 213 StringBuilder sb = new StringBuilder("012345"); 214 assertEquals( sb.insert( 2, l ).toString(), "01992345"); } 215 216 217 public void insertintObject() { 218 StringBuilder sb = new StringBuilder("012345"); 219 List<String> ls = new ArrayList<String>(); 220 ls.add("A"); ls.add("B"); 221 String lsString = ls.toString(); 222 assertEquals( sb.insert(2, ls).toString(), "01"+lsString+"2345"); 223 224 try { 225 sb.insert(sb.length()+1, ls); 226 throw new RuntimeException("Test failed: should have thrown StringIndexOutOfBoundsException"); 227 } catch (StringIndexOutOfBoundsException soob) { 228 // expected: passed 229 } catch (Throwable t) { 230 throw new RuntimeException("Test failed: should have thrown StringIndexOutOfBoundsException, but instead threw:" 231 + t); 232 } 233 } 234 235 236 public void lastIndexOfString() { 237 String xyz = "xyz"; 238 String xyz3 = "xyzxyzxyz"; 239 StringBuilder sb = new StringBuilder(xyz3); 240 int pos = sb.lastIndexOf("xyz"); 241 assertEquals( pos, 2*xyz.length() ); 242 } 243 244 245 public void lastIndexOfStringint() { 246 StringBuilder sb = new StringBuilder("xyzxyzxyz"); 247 int pos = sb.lastIndexOf("xyz",5); 248 assertEquals( pos, 3 ); 249 pos = sb.lastIndexOf("xyz", 6); 250 assertEquals( pos, 6 ); 251 } 252 253 public void assertEquals( String actual, String expected) { 254 if (!actual.equals( expected )) { 255 throw new RuntimeException( "Test failed: actual = '" + actual + 256 "', expected = '" + expected + "'"); 257 } 258 } 259 260 public void assertEquals( int actual, int expected) { 261 if (actual != expected) { 262 throw new RuntimeException( "Test failed: actual = '" + actual + 263 "', expected = '" + expected + "'"); 264 } 265 } 266} 267 268