BufferForwarding.java revision 12745:f068a4ffddd2
1209878Snwhitehorn/* 2209878Snwhitehorn * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. 3209878Snwhitehorn * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4209878Snwhitehorn * 5209878Snwhitehorn * This code is free software; you can redistribute it and/or modify it 6209878Snwhitehorn * under the terms of the GNU General Public License version 2 only, as 7209878Snwhitehorn * published by the Free Software Foundation. 8209878Snwhitehorn * 9209878Snwhitehorn * This code is distributed in the hope that it will be useful, but WITHOUT 10209878Snwhitehorn * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11209878Snwhitehorn * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12209878Snwhitehorn * version 2 for more details (a copy is included in the LICENSE file that 13209878Snwhitehorn * accompanied this code). 14209878Snwhitehorn * 15209878Snwhitehorn * You should have received a copy of the GNU General Public License version 16209878Snwhitehorn * 2 along with this work; if not, write to the Free Software Foundation, 17209878Snwhitehorn * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18209878Snwhitehorn * 19209878Snwhitehorn * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20209878Snwhitehorn * or visit www.oracle.com if you need additional information or have any 21209878Snwhitehorn * questions. 22209878Snwhitehorn */ 23209878Snwhitehorn 24209878Snwhitehorn/** 25209878Snwhitehorn * @test 26209878Snwhitehorn * @bug 6206780 27209878Snwhitehorn * @summary Test forwarding of methods to super in StringBuffer 28209878Snwhitehorn * @author Jim Gish <jim.gish@oracle.com> 29209878Snwhitehorn */ 30209878Snwhitehorn 31209878Snwhitehornimport java.util.ArrayList; 32209878Snwhitehornimport java.util.List; 33209878Snwhitehorn 34209878Snwhitehornpublic class BufferForwarding { 35209878Snwhitehorn private static final String A_STRING_BUFFER_VAL = "aStringBuffer"; 36209878Snwhitehorn private static final String A_STRING_BUILDER_VAL = "aStringBuilder"; 37209878Snwhitehorn private static final String A_STRING_VAL = "aString"; 38215147Sdim private static final String NON_EMPTY_VAL = "NonEmpty"; 39209878Snwhitehorn 40209878Snwhitehorn public BufferForwarding() { 41209878Snwhitehorn System.out.println( "Starting BufferForwarding"); 42209878Snwhitehorn } 43209878Snwhitehorn 44209878Snwhitehorn public static void main(String... args) { 45209878Snwhitehorn new BufferForwarding().executeTestMethods(); 46209878Snwhitehorn } 47209878Snwhitehorn 48209878Snwhitehorn public void executeTestMethods() { 49209878Snwhitehorn appendCharSequence(); 50209878Snwhitehorn indexOfString(); 51209878Snwhitehorn indexOfStringIntNull(); 52209878Snwhitehorn indexOfStringNull(); 53209878Snwhitehorn indexOfStringint(); 54209878Snwhitehorn insertintCharSequence(); 55209878Snwhitehorn insertintObject(); 56209878Snwhitehorn insertintboolean(); 57209878Snwhitehorn insertintchar(); 58209878Snwhitehorn insertintdouble(); 59209878Snwhitehorn insertintfloat(); 60209878Snwhitehorn insertintint(); 61209878Snwhitehorn insertintlong(); 62209878Snwhitehorn lastIndexOfString(); 63209878Snwhitehorn lastIndexOfStringint(); 64209878Snwhitehorn } 65209878Snwhitehorn 66218824Snwhitehorn public void appendCharSequence() { 67209878Snwhitehorn // three different flavors of CharSequence 68209878Snwhitehorn CharSequence aString = A_STRING_VAL; 69209878Snwhitehorn CharSequence aStringBuilder = new StringBuilder(A_STRING_BUILDER_VAL); 70209878Snwhitehorn CharSequence aStringBuffer = new StringBuffer(A_STRING_BUFFER_VAL); 71209878Snwhitehorn 72217398Skib assertEquals( /*actual*/ new StringBuilder().append(aString).toString(), /*expected*/ A_STRING_VAL ); 73217398Skib 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 void indexOfString() { 82 StringBuffer sb = new StringBuffer("xyz"); 83 assertEquals( sb.indexOf("y"), 1 ); 84 assertEquals( sb.indexOf("not found"), -1 ); 85 } 86 87 88 public void indexOfStringint() { 89 StringBuffer sb = new StringBuffer("xyyz"); 90 assertEquals( sb.indexOf("y",0), 1 ); 91 assertEquals( sb.indexOf("y",1), 1 ); 92 assertEquals( sb.indexOf("y",2), 2 ); 93 assertEquals( sb.indexOf("not found"), -1 ); 94 } 95 96 97 public void indexOfStringIntNull() { 98 StringBuffer sb = new StringBuffer(); 99 // should be NPE if null passed 100 try { 101 sb.indexOf(null,1); 102 throw new RuntimeException("Test failed: should have thrown NPE"); 103 } catch (NullPointerException npe) { 104 // expected: passed 105 } catch (Throwable t) { 106 throw new RuntimeException("Test failed: should have thrown NPE. Instead threw " 107 + t); 108 } 109 } 110 111 112 public void indexOfStringNull() { 113 StringBuffer sb = new StringBuffer(); 114 115 // should be NPE if null passed 116 try { 117 sb.indexOf(null); 118 throw new RuntimeException("Test failed: should have thrown NPE"); 119 } catch (NullPointerException npe) { 120 // expected: passed 121 } catch (Throwable t) { 122 throw new RuntimeException("Test failed: should have thrown NPE. Instead threw " 123 + t); 124 } 125 } 126 127 128 public void insertintboolean() { 129 boolean b = true; 130 StringBuffer sb = new StringBuffer("012345"); 131 assertEquals( sb.insert( 2, b).toString(), "01true2345"); 132 } 133 134 135 public void insertintchar() { 136 char c = 'C'; 137 StringBuffer sb = new StringBuffer("012345"); 138 assertEquals( sb.insert( 2, c ).toString(), "01C2345"); 139 } 140 141 142 public void insertintCharSequence() { 143 final String initString = "012345"; 144 // three different flavors of CharSequence 145 CharSequence aString = A_STRING_VAL; 146 CharSequence aStringBuilder = new StringBuilder(A_STRING_BUILDER_VAL); 147 CharSequence aStringBuffer = new StringBuffer(A_STRING_BUFFER_VAL); 148 149 assertEquals( new StringBuffer(initString).insert(2, aString).toString(), "01"+A_STRING_VAL+"2345" ); 150 151 assertEquals( new StringBuffer(initString).insert(2, aStringBuilder).toString(), "01"+A_STRING_BUILDER_VAL+"2345" ); 152 153 assertEquals( new StringBuffer(initString).insert(2, aStringBuffer).toString(), "01"+A_STRING_BUFFER_VAL+"2345" ); 154 155 try { 156 new StringBuffer(initString).insert(7, aString); 157 throw new RuntimeException("Test failed: should have thrown IndexOutOfBoundsException"); 158 } catch (IndexOutOfBoundsException soob) { 159 // expected: passed 160 } catch (Throwable t) { 161 throw new RuntimeException("Test failed: should have thrown IndexOutOfBoundsException, but instead threw " + t.getMessage()); 162 163 } 164 } 165 166 public void insertintdouble() { 167 double d = 99d; 168 StringBuffer sb = new StringBuffer("012345"); 169 assertEquals( sb.insert( 2, d ).toString(), "0199.02345"); } 170 171 172 public void insertintfloat() { 173 float f = 99.0f; 174 StringBuffer sb = new StringBuffer("012345"); 175 assertEquals( sb.insert( 2, f ).toString(), "0199.02345"); } 176 177 178 public void insertintint() { 179 int i = 99; 180 StringBuffer sb = new StringBuffer("012345"); 181 assertEquals( sb.insert( 2, i ).toString(), "01992345"); 182 } 183 184 185 public void insertintlong() { 186 long l = 99; 187 StringBuffer sb = new StringBuffer("012345"); 188 assertEquals( sb.insert( 2, l ).toString(), "01992345"); } 189 190 public void insertintObject() { 191 StringBuffer sb = new StringBuffer("012345"); 192 List<String> ls = new ArrayList<String>(); 193 ls.add("A"); ls.add("B"); 194 String lsString = ls.toString(); 195 assertEquals( sb.insert(2, ls).toString(), "01"+lsString+"2345"); 196 197 try { 198 sb.insert(sb.length()+1, ls); 199 throw new RuntimeException("Test failed: should have thrown StringIndexOutOfBoundsException"); 200 } catch (StringIndexOutOfBoundsException soob) { 201 // expected: passed 202 } catch (Throwable t) { 203 throw new RuntimeException("Test failed: should have thrown StringIndexOutOfBoundsException, but instead threw:" 204 + t); 205 206 } 207 } 208 209 210 211 public void lastIndexOfString() { 212 String xyz = "xyz"; 213 String xyz3 = "xyzxyzxyz"; 214 StringBuffer sb = new StringBuffer(xyz3); 215 int pos = sb.lastIndexOf("xyz"); 216 assertEquals( pos, 2*xyz.length() ); 217 } 218 219 public void lastIndexOfStringint() { 220 StringBuffer sb = new StringBuffer("xyzxyzxyz"); 221 int pos = sb.lastIndexOf("xyz",5); 222 assertEquals( pos, 3 ); 223 pos = sb.lastIndexOf("xyz", 6); 224 assertEquals( pos, 6 ); 225 } 226 227 public void assertEquals( String actual, String expected) { 228 if (!actual.equals( expected )) { 229 throw new RuntimeException( "Test failed: actual = '" + actual + 230 "', expected = '" + expected + "'"); 231 } 232 } 233 234 public void assertEquals( int actual, int expected) { 235 if (actual != expected) { 236 throw new RuntimeException( "Test failed: actual = '" + actual + 237 "', expected = '" + expected + "'"); 238 } 239 } 240} 241