StringBufferTest.java revision 2ad60cfc28e14ee8f0bb038720836a4696c478ad
1/* Licensed to the Apache Software Foundation (ASF) under one or more 2 * contributor license agreements. See the NOTICE file distributed with 3 * this work for additional information regarding copyright ownership. 4 * The ASF licenses this file to You under the Apache License, Version 2.0 5 * (the "License"); you may not use this file except in compliance with 6 * the License. You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package org.apache.harmony.luni.tests.java.lang; 18 19import java.io.Serializable; 20 21import junit.framework.TestCase; 22 23import org.apache.harmony.testframework.serialization.SerializationTest; 24import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert; 25 26public class StringBufferTest extends TestCase { 27 28 /** 29 * @tests java.lang.StringBuffer#setLength(int) 30 */ 31 public void test_setLengthI() { 32 // Regression for HARMONY-90 33 StringBuffer buffer = new StringBuffer("abcde"); 34 try { 35 buffer.setLength(-1); 36 fail("Assert 0: IndexOutOfBoundsException must be thrown"); 37 } catch (IndexOutOfBoundsException e) { 38 // expected 39 } 40 } 41 42 /** 43 * @tests StringBuffer.StringBuffer(CharSequence); 44 */ 45 public void test_constructorLjava_lang_CharSequence() { 46 try { 47 new StringBuffer((CharSequence) null); 48 fail("Assert 0: NPE must be thrown."); 49 } catch (NullPointerException e) {} 50 51 assertEquals("Assert 1: must equal 'abc'.", "abc", new StringBuffer((CharSequence)"abc").toString()); 52 } 53 54 public void test_trimToSize() { 55 StringBuffer buffer = new StringBuffer(25); 56 buffer.append("abc"); 57 int origCapacity = buffer.capacity(); 58 buffer.trimToSize(); 59 int trimCapacity = buffer.capacity(); 60 assertTrue("Assert 0: capacity must be smaller.", trimCapacity < origCapacity); 61 assertEquals("Assert 1: length must still be 3", 3, buffer.length()); 62 assertEquals("Assert 2: value must still be 'abc'.", "abc", buffer.toString()); 63 } 64 65 /** 66 * @tests java.lang.StringBuffer.append(CharSequence) 67 */ 68 public void test_appendLjava_lang_CharSequence() { 69 StringBuffer sb = new StringBuffer(); 70 assertSame(sb, sb.append((CharSequence) "ab")); 71 assertEquals("ab", sb.toString()); 72 sb.setLength(0); 73 assertSame(sb, sb.append((CharSequence) "cd")); 74 assertEquals("cd", sb.toString()); 75 sb.setLength(0); 76 assertSame(sb, sb.append((CharSequence) null)); 77 assertEquals("null", sb.toString()); 78 } 79 80 /** 81 * @tests java.lang.StringBuffer.append(CharSequence, int, int) 82 */ 83 @SuppressWarnings("cast") 84 public void test_appendLjava_lang_CharSequenceII() { 85 StringBuffer sb = new StringBuffer(); 86 assertSame(sb, sb.append((CharSequence) "ab", 0, 2)); 87 assertEquals("ab", sb.toString()); 88 sb.setLength(0); 89 assertSame(sb, sb.append((CharSequence) "cd", 0, 2)); 90 assertEquals("cd", sb.toString()); 91 sb.setLength(0); 92 assertSame(sb, sb.append((CharSequence) "abcd", 0, 2)); 93 assertEquals("ab", sb.toString()); 94 sb.setLength(0); 95 assertSame(sb, sb.append((CharSequence) "abcd", 2, 4)); 96 assertEquals("cd", sb.toString()); 97 sb.setLength(0); 98 assertSame(sb, sb.append((CharSequence) null, 0, 2)); 99 assertEquals("nu", sb.toString()); 100 } 101 102 /** 103 * @tests java.lang.StringBuffer.append(char[], int, int) 104 */ 105 public void test_append$CII_2() { 106 StringBuffer obj = new StringBuffer(); 107 try { 108 obj.append(new char[0], -1, -1); 109 fail("ArrayIndexOutOfBoundsException expected"); 110 } catch (ArrayIndexOutOfBoundsException e) { 111 // expected 112 } 113 } 114 115 /** 116 * @tests java.lang.StringBuffer.append(char[], int, int) 117 */ 118 public void test_append$CII_3() throws Exception { 119 StringBuffer obj = new StringBuffer(); 120 try { 121 obj.append((char[]) null, -1, -1); 122 fail("NullPointerException expected"); 123 } catch (NullPointerException e) { 124 // expected 125 } 126 } 127 128 /** 129 * @tests java.lang.StringBuffer.insert(int, CharSequence) 130 */ 131 public void test_insertILjava_lang_CharSequence() { 132 final String fixture = "0000"; 133 StringBuffer sb = new StringBuffer(fixture); 134 assertSame(sb, sb.insert(0, (CharSequence) "ab")); 135 assertEquals("ab0000", sb.toString()); 136 assertEquals(6, sb.length()); 137 138 sb = new StringBuffer(fixture); 139 assertSame(sb, sb.insert(2, (CharSequence) "ab")); 140 assertEquals("00ab00", sb.toString()); 141 assertEquals(6, sb.length()); 142 143 sb = new StringBuffer(fixture); 144 assertSame(sb, sb.insert(4, (CharSequence) "ab")); 145 assertEquals("0000ab", sb.toString()); 146 assertEquals(6, sb.length()); 147 148 sb = new StringBuffer(fixture); 149 assertSame(sb, sb.insert(4, (CharSequence) null)); 150 assertEquals("0000null", sb.toString()); 151 assertEquals(8, sb.length()); 152 153 try { 154 sb = new StringBuffer(fixture); 155 sb.insert(-1, (CharSequence) "ab"); 156 fail("no IOOBE, negative index"); 157 } catch (IndexOutOfBoundsException e) { 158 // Expected 159 } 160 161 try { 162 sb = new StringBuffer(fixture); 163 sb.insert(5, (CharSequence) "ab"); 164 fail("no IOOBE, index too large index"); 165 } catch (IndexOutOfBoundsException e) { 166 // Expected 167 } 168 } 169 170 /** 171 * @tests java.lang.StringBuffer.insert(int, CharSequence, int, int) 172 */ 173 @SuppressWarnings("cast") 174 public void test_insertILjava_lang_CharSequenceII() { 175 final String fixture = "0000"; 176 StringBuffer sb = new StringBuffer(fixture); 177 assertSame(sb, sb.insert(0, (CharSequence) "ab", 0, 2)); 178 assertEquals("ab0000", sb.toString()); 179 assertEquals(6, sb.length()); 180 181 sb = new StringBuffer(fixture); 182 assertSame(sb, sb.insert(0, (CharSequence) "ab", 0, 1)); 183 assertEquals("a0000", sb.toString()); 184 assertEquals(5, sb.length()); 185 186 sb = new StringBuffer(fixture); 187 assertSame(sb, sb.insert(2, (CharSequence) "ab", 0, 2)); 188 assertEquals("00ab00", sb.toString()); 189 assertEquals(6, sb.length()); 190 191 sb = new StringBuffer(fixture); 192 assertSame(sb, sb.insert(2, (CharSequence) "ab", 0, 1)); 193 assertEquals("00a00", sb.toString()); 194 assertEquals(5, sb.length()); 195 196 sb = new StringBuffer(fixture); 197 assertSame(sb, sb.insert(4, (CharSequence) "ab", 0, 2)); 198 assertEquals("0000ab", sb.toString()); 199 assertEquals(6, sb.length()); 200 201 sb = new StringBuffer(fixture); 202 assertSame(sb, sb.insert(4, (CharSequence) "ab", 0, 1)); 203 assertEquals("0000a", sb.toString()); 204 assertEquals(5, sb.length()); 205 206 sb = new StringBuffer(fixture); 207 assertSame(sb, sb.insert(4, (CharSequence) null, 0, 2)); 208 assertEquals("0000nu", sb.toString()); 209 assertEquals(6, sb.length()); 210 211 try { 212 sb = new StringBuffer(fixture); 213 sb.insert(-1, (CharSequence) "ab", 0, 2); 214 fail("no IOOBE, negative index"); 215 } catch (IndexOutOfBoundsException e) { 216 // Expected 217 } 218 219 try { 220 sb = new StringBuffer(fixture); 221 sb.insert(5, (CharSequence) "ab", 0, 2); 222 fail("no IOOBE, index too large index"); 223 } catch (IndexOutOfBoundsException e) { 224 // Expected 225 } 226 227 try { 228 sb = new StringBuffer(fixture); 229 sb.insert(5, (CharSequence) "ab", -1, 2); 230 fail("no IOOBE, negative offset"); 231 } catch (IndexOutOfBoundsException e) { 232 // Expected 233 } 234 235 try { 236 sb = new StringBuffer(fixture); 237 sb.insert(5, new char[] { 'a', 'b' }, 0, -1); 238 fail("no IOOBE, negative length"); 239 } catch (IndexOutOfBoundsException e) { 240 // Expected 241 } 242 243 try { 244 sb = new StringBuffer(fixture); 245 sb.insert(5, new char[] { 'a', 'b' }, 0, 3); 246 fail("no IOOBE, too long"); 247 } catch (IndexOutOfBoundsException e) { 248 // Expected 249 } 250 } 251 252 /** 253 * @tests java.lang.StringBuffer.insert(int, char) 254 */ 255 public void test_insertIC() { 256 StringBuffer obj = new StringBuffer(); 257 try { 258 obj.insert(-1, ' '); 259 fail("ArrayIndexOutOfBoundsException expected"); 260 } catch (ArrayIndexOutOfBoundsException e) { 261 // expected 262 } 263 } 264 265 /** 266 * @tests java.lang.StringBuffer.appendCodePoint(int)' 267 */ 268 public void test_appendCodePointI() { 269 StringBuffer sb = new StringBuffer(); 270 sb.appendCodePoint(0x10000); 271 assertEquals("\uD800\uDC00", sb.toString()); 272 sb.append("fixture"); 273 assertEquals("\uD800\uDC00fixture", sb.toString()); 274 sb.appendCodePoint(0x00010FFFF); 275 assertEquals("\uD800\uDC00fixture\uDBFF\uDFFF", sb.toString()); 276 } 277 278 /** 279 * @tests java.lang.StringBuffer.codePointAt(int) 280 */ 281 public void test_codePointAtI() { 282 StringBuffer sb = new StringBuffer("abc"); 283 assertEquals('a', sb.codePointAt(0)); 284 assertEquals('b', sb.codePointAt(1)); 285 assertEquals('c', sb.codePointAt(2)); 286 287 sb = new StringBuffer("\uD800\uDC00"); 288 assertEquals(0x10000, sb.codePointAt(0)); 289 assertEquals('\uDC00', sb.codePointAt(1)); 290 291 try { 292 sb.codePointAt(-1); 293 fail("No IOOBE on negative index."); 294 } catch (IndexOutOfBoundsException e) { 295 296 } 297 298 try { 299 sb.codePointAt(sb.length()); 300 fail("No IOOBE on index equal to length."); 301 } catch (IndexOutOfBoundsException e) { 302 303 } 304 305 try { 306 sb.codePointAt(sb.length() + 1); 307 fail("No IOOBE on index greater than length."); 308 } catch (IndexOutOfBoundsException e) { 309 310 } 311 } 312 313 /** 314 * @tests java.lang.StringBuffer.codePointBefore(int) 315 */ 316 public void test_codePointBeforeI() { 317 StringBuffer sb = new StringBuffer("abc"); 318 assertEquals('a', sb.codePointBefore(1)); 319 assertEquals('b', sb.codePointBefore(2)); 320 assertEquals('c', sb.codePointBefore(3)); 321 322 sb = new StringBuffer("\uD800\uDC00"); 323 assertEquals(0x10000, sb.codePointBefore(2)); 324 assertEquals('\uD800', sb.codePointBefore(1)); 325 326 try { 327 sb.codePointBefore(0); 328 fail("No IOOBE on zero index."); 329 } catch (IndexOutOfBoundsException e) { 330 331 } 332 333 try { 334 sb.codePointBefore(-1); 335 fail("No IOOBE on negative index."); 336 } catch (IndexOutOfBoundsException e) { 337 338 } 339 340 try { 341 sb.codePointBefore(sb.length() + 1); 342 fail("No IOOBE on index greater than length."); 343 } catch (IndexOutOfBoundsException e) { 344 345 } 346 } 347 348 /** 349 * @tests java.lang.StringBuffer.codePointCount(int, int) 350 */ 351 public void test_codePointCountII() { 352 assertEquals(1, new StringBuffer("\uD800\uDC00").codePointCount(0, 2)); 353 assertEquals(1, new StringBuffer("\uD800\uDC01").codePointCount(0, 2)); 354 assertEquals(1, new StringBuffer("\uD801\uDC01").codePointCount(0, 2)); 355 assertEquals(1, new StringBuffer("\uDBFF\uDFFF").codePointCount(0, 2)); 356 357 assertEquals(3, new StringBuffer("a\uD800\uDC00b").codePointCount(0, 4)); 358 assertEquals(4, new StringBuffer("a\uD800\uDC00b\uD800").codePointCount(0, 5)); 359 360 StringBuffer sb = new StringBuffer("abc"); 361 try { 362 sb.codePointCount(-1, 2); 363 fail("No IOOBE for negative begin index."); 364 } catch (IndexOutOfBoundsException e) { 365 366 } 367 368 try { 369 sb.codePointCount(0, 4); 370 fail("No IOOBE for end index that's too large."); 371 } catch (IndexOutOfBoundsException e) { 372 373 } 374 375 try { 376 sb.codePointCount(3, 2); 377 fail("No IOOBE for begin index larger than end index."); 378 } catch (IndexOutOfBoundsException e) { 379 380 } 381 } 382 383 /** 384 * @tests java.lang.StringBuffer.getChars(int, int, char[], int) 385 */ 386 public void test_getCharsII$CI() { 387 StringBuffer obj = new StringBuffer(); 388 try { 389 obj.getChars(0, 0, new char[0], -1); 390// TODO(Fixed) According to spec this should be IndexOutOfBoundsException. 391// fail("ArrayIndexOutOfBoundsException expected"); 392// } catch (ArrayIndexOutOfBoundsException e) { 393 fail("IndexOutOfBoundsException expected"); 394 } catch (IndexOutOfBoundsException e) { 395 // expected 396 } 397 } 398 399 /** 400 * @tests java.lang.StringBuffer.offsetByCodePoints(int, int)' 401 */ 402 public void test_offsetByCodePointsII() { 403 int result = new StringBuffer("a\uD800\uDC00b").offsetByCodePoints(0, 2); 404 assertEquals(3, result); 405 406 result = new StringBuffer("abcd").offsetByCodePoints(3, -1); 407 assertEquals(2, result); 408 409 result = new StringBuffer("a\uD800\uDC00b").offsetByCodePoints(0, 3); 410 assertEquals(4, result); 411 412 result = new StringBuffer("a\uD800\uDC00b").offsetByCodePoints(3, -1); 413 assertEquals(1, result); 414 415 result = new StringBuffer("a\uD800\uDC00b").offsetByCodePoints(3, 0); 416 assertEquals(3, result); 417 418 result = new StringBuffer("\uD800\uDC00bc").offsetByCodePoints(3, 0); 419 assertEquals(3, result); 420 421 result = new StringBuffer("a\uDC00bc").offsetByCodePoints(3, -1); 422 assertEquals(2, result); 423 424 result = new StringBuffer("a\uD800bc").offsetByCodePoints(3, -1); 425 assertEquals(2, result); 426 427 StringBuffer sb = new StringBuffer("abc"); 428 try { 429 sb.offsetByCodePoints(-1, 1); 430 fail("No IOOBE for negative index."); 431 } catch (IndexOutOfBoundsException e) { 432 433 } 434 435 try { 436 sb.offsetByCodePoints(0, 4); 437 fail("No IOOBE for offset that's too large."); 438 } catch (IndexOutOfBoundsException e) { 439 440 } 441 442 try { 443 sb.offsetByCodePoints(3, -4); 444 fail("No IOOBE for offset that's too small."); 445 } catch (IndexOutOfBoundsException e) { 446 447 } 448 449 try { 450 sb.offsetByCodePoints(3, 1); 451 fail("No IOOBE for index that's too large."); 452 } catch (IndexOutOfBoundsException e) { 453 454 } 455 456 try { 457 sb.offsetByCodePoints(4, -1); 458 fail("No IOOBE for index that's too large."); 459 } catch (IndexOutOfBoundsException e) { 460 461 } 462 } 463 464 /** 465 * @tests {@link java.lang.StringBuffer#indexOf(String, int)} 466 */ 467 @SuppressWarnings("nls") 468 public void test_IndexOfStringInt() { 469 final String fixture = "0123456789"; 470 StringBuffer sb = new StringBuffer(fixture); 471 assertEquals(0, sb.indexOf("0")); 472 assertEquals(0, sb.indexOf("012")); 473 assertEquals(-1, sb.indexOf("02")); 474 assertEquals(8, sb.indexOf("89")); 475 476 assertEquals(0, sb.indexOf("0"), 0); 477 assertEquals(0, sb.indexOf("012"), 0); 478 assertEquals(-1, sb.indexOf("02"), 0); 479 assertEquals(8, sb.indexOf("89"), 0); 480 481 assertEquals(-1, sb.indexOf("0"), 5); 482 assertEquals(-1, sb.indexOf("012"), 5); 483 assertEquals(-1, sb.indexOf("02"), 0); 484 assertEquals(8, sb.indexOf("89"), 5); 485 486 try { 487 sb.indexOf(null, 0); 488 fail("Should throw a NullPointerExceptionE"); 489 } catch (NullPointerException e) { 490 // Expected 491 } 492 } 493 494 /** 495 * @tests {@link java.lang.StringBuffer#lastIndexOf(String, int)} 496 */ 497 @SuppressWarnings("nls") 498 public void test_lastIndexOfLjava_lang_StringI() { 499 final String fixture = "0123456789"; 500 StringBuffer sb = new StringBuffer(fixture); 501 assertEquals(0, sb.lastIndexOf("0")); 502 assertEquals(0, sb.lastIndexOf("012")); 503 assertEquals(-1, sb.lastIndexOf("02")); 504 assertEquals(8, sb.lastIndexOf("89")); 505 506 assertEquals(0, sb.lastIndexOf("0"), 0); 507 assertEquals(0, sb.lastIndexOf("012"), 0); 508 assertEquals(-1, sb.lastIndexOf("02"), 0); 509 assertEquals(8, sb.lastIndexOf("89"), 0); 510 511 assertEquals(-1, sb.lastIndexOf("0"), 5); 512 assertEquals(-1, sb.lastIndexOf("012"), 5); 513 assertEquals(-1, sb.lastIndexOf("02"), 0); 514 assertEquals(8, sb.lastIndexOf("89"), 5); 515 516 try { 517 sb.lastIndexOf(null, 0); 518 fail("Should throw a NullPointerException"); 519 } catch (NullPointerException e) { 520 // Expected 521 } 522 } 523 524 // comparator for StringBuffer objects 525 private static final SerializableAssert STRING_BUFFER_COMPARATOR = new SerializableAssert() { 526 public void assertDeserialized(Serializable initial, 527 Serializable deserialized) { 528 529 StringBuffer init = (StringBuffer) initial; 530 StringBuffer desr = (StringBuffer) deserialized; 531 532 // serializable fields are: 'count', 'shared', 'value' 533 // serialization of 'shared' is not verified 534 // 'count' + 'value' should result in required string 535 assertEquals("toString", init.toString(), desr.toString()); 536 } 537 }; 538 539 /** 540 * @tests serialization/deserialization. 541 */ 542 public void testSerializationSelf() throws Exception { 543 544 SerializationTest.verifySelf(new StringBuffer("0123456789"), 545 STRING_BUFFER_COMPARATOR); 546 } 547 548 /** 549 * @tests serialization/deserialization compatibility with RI. 550 */ 551 public void testSerializationCompatibility() throws Exception { 552 553 SerializationTest.verifyGolden(this, new StringBuffer("0123456789"), 554 STRING_BUFFER_COMPARATOR); 555 } 556} 557