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 assertEquals("abcde", buffer.toString()); 42 buffer.setLength(1); 43 buffer.append('f'); 44 assertEquals("af", buffer.toString()); 45 46 buffer = new StringBuffer("abcde"); 47 assertEquals("cde", buffer.substring(2)); 48 buffer.setLength(3); 49 buffer.append('f'); 50 assertEquals("abcf", buffer.toString()); 51 52 buffer = new StringBuffer("abcde"); 53 buffer.setLength(2); 54 try { 55 buffer.charAt(3); 56 fail("should throw IndexOutOfBoundsException"); 57 } catch (IndexOutOfBoundsException e) { 58 // Expected 59 } 60 61 buffer = new StringBuffer(); 62 buffer.append("abcdefg"); 63 buffer.setLength(2); 64 buffer.setLength(5); 65 for (int i = 2; i < 5; i++) { 66 assertEquals(0, buffer.charAt(i)); 67 } 68 69 buffer = new StringBuffer(); 70 buffer.append("abcdefg"); 71 buffer.delete(2, 4); 72 buffer.setLength(7); 73 assertEquals('a', buffer.charAt(0)); 74 assertEquals('b', buffer.charAt(1)); 75 assertEquals('e', buffer.charAt(2)); 76 assertEquals('f', buffer.charAt(3)); 77 assertEquals('g', buffer.charAt(4)); 78 for (int i = 5; i < 7; i++) { 79 assertEquals(0, buffer.charAt(i)); 80 } 81 82 buffer = new StringBuffer(); 83 buffer.append("abcdefg"); 84 buffer.replace(2, 5, "z"); 85 buffer.setLength(7); 86 for (int i = 5; i < 7; i++) { 87 assertEquals(0, buffer.charAt(i)); 88 } 89 } 90 91 /** 92 * @tests java.lang.StringBuffer#toString() 93 */ 94 public void test_toString() throws Exception { 95 StringBuffer buffer = new StringBuffer(); 96 assertEquals("", buffer.toString()); 97 98 buffer.append("abcde"); 99 assertEquals("abcde", buffer.toString()); 100 buffer.setLength(1000); 101 byte[] bytes = buffer.toString().getBytes("GB18030"); 102 for (int i = 5; i < bytes.length; i++) { 103 assertEquals(0, bytes[i]); 104 } 105 106 buffer.setLength(5); 107 buffer.append("fghij"); 108 assertEquals("abcdefghij", buffer.toString()); 109 } 110 111 /** 112 * @tests StringBuffer.StringBuffer(CharSequence); 113 */ 114 public void test_constructorLjava_lang_CharSequence() { 115 try { 116 new StringBuffer((CharSequence) null); 117 fail("Assert 0: NPE must be thrown."); 118 } catch (NullPointerException e) {} 119 120 assertEquals("Assert 1: must equal 'abc'.", "abc", new StringBuffer((CharSequence)"abc").toString()); 121 } 122 123 public void test_trimToSize() { 124 StringBuffer buffer = new StringBuffer(25); 125 buffer.append("abc"); 126 int origCapacity = buffer.capacity(); 127 buffer.trimToSize(); 128 int trimCapacity = buffer.capacity(); 129 assertTrue("Assert 0: capacity must be smaller.", trimCapacity < origCapacity); 130 assertEquals("Assert 1: length must still be 3", 3, buffer.length()); 131 assertEquals("Assert 2: value must still be 'abc'.", "abc", buffer.toString()); 132 } 133 134 /** 135 * @tests java.lang.StringBuffer.append(CharSequence) 136 */ 137 public void test_appendLjava_lang_CharSequence() { 138 StringBuffer sb = new StringBuffer(); 139 assertSame(sb, sb.append((CharSequence) "ab")); 140 assertEquals("ab", sb.toString()); 141 sb.setLength(0); 142 assertSame(sb, sb.append((CharSequence) "cd")); 143 assertEquals("cd", sb.toString()); 144 sb.setLength(0); 145 assertSame(sb, sb.append((CharSequence) null)); 146 assertEquals("null", sb.toString()); 147 } 148 149 /** 150 * @tests java.lang.StringBuffer.append(CharSequence, int, int) 151 */ 152 @SuppressWarnings("cast") 153 public void test_appendLjava_lang_CharSequenceII() { 154 StringBuffer sb = new StringBuffer(); 155 assertSame(sb, sb.append((CharSequence) "ab", 0, 2)); 156 assertEquals("ab", sb.toString()); 157 sb.setLength(0); 158 assertSame(sb, sb.append((CharSequence) "cd", 0, 2)); 159 assertEquals("cd", sb.toString()); 160 sb.setLength(0); 161 assertSame(sb, sb.append((CharSequence) "abcd", 0, 2)); 162 assertEquals("ab", sb.toString()); 163 sb.setLength(0); 164 assertSame(sb, sb.append((CharSequence) "abcd", 2, 4)); 165 assertEquals("cd", sb.toString()); 166 sb.setLength(0); 167 assertSame(sb, sb.append((CharSequence) null, 0, 2)); 168 assertEquals("nu", sb.toString()); 169 } 170 171 /** 172 * @tests java.lang.StringBuffer.append(char[], int, int) 173 */ 174 public void test_append$CII_2() { 175 StringBuffer obj = new StringBuffer(); 176 try { 177 obj.append(new char[0], -1, -1); 178 fail("ArrayIndexOutOfBoundsException expected"); 179 } catch (ArrayIndexOutOfBoundsException e) { 180 // expected 181 } 182 } 183 184 /** 185 * @tests java.lang.StringBuffer.append(char[], int, int) 186 */ 187 public void test_append$CII_3() throws Exception { 188 StringBuffer obj = new StringBuffer(); 189 try { 190 obj.append((char[]) null, -1, -1); 191 fail("NullPointerException expected"); 192 } catch (NullPointerException e) { 193 // expected 194 } 195 } 196 197 /** 198 * @tests java.lang.StringBuffer.insert(int, CharSequence) 199 */ 200 public void test_insertILjava_lang_CharSequence() { 201 final String fixture = "0000"; 202 StringBuffer sb = new StringBuffer(fixture); 203 assertSame(sb, sb.insert(0, (CharSequence) "ab")); 204 assertEquals("ab0000", sb.toString()); 205 assertEquals(6, sb.length()); 206 207 sb = new StringBuffer(fixture); 208 assertSame(sb, sb.insert(2, (CharSequence) "ab")); 209 assertEquals("00ab00", sb.toString()); 210 assertEquals(6, sb.length()); 211 212 sb = new StringBuffer(fixture); 213 assertSame(sb, sb.insert(4, (CharSequence) "ab")); 214 assertEquals("0000ab", sb.toString()); 215 assertEquals(6, sb.length()); 216 217 sb = new StringBuffer(fixture); 218 assertSame(sb, sb.insert(4, (CharSequence) null)); 219 assertEquals("0000null", sb.toString()); 220 assertEquals(8, sb.length()); 221 222 try { 223 sb = new StringBuffer(fixture); 224 sb.insert(-1, (CharSequence) "ab"); 225 fail("no IOOBE, negative index"); 226 } catch (IndexOutOfBoundsException e) { 227 // Expected 228 } 229 230 try { 231 sb = new StringBuffer(fixture); 232 sb.insert(5, (CharSequence) "ab"); 233 fail("no IOOBE, index too large index"); 234 } catch (IndexOutOfBoundsException e) { 235 // Expected 236 } 237 } 238 239 /** 240 * @tests java.lang.StringBuffer.insert(int, CharSequence, int, int) 241 */ 242 @SuppressWarnings("cast") 243 public void test_insertILjava_lang_CharSequenceII() { 244 final String fixture = "0000"; 245 StringBuffer sb = new StringBuffer(fixture); 246 assertSame(sb, sb.insert(0, (CharSequence) "ab", 0, 2)); 247 assertEquals("ab0000", sb.toString()); 248 assertEquals(6, sb.length()); 249 250 sb = new StringBuffer(fixture); 251 assertSame(sb, sb.insert(0, (CharSequence) "ab", 0, 1)); 252 assertEquals("a0000", sb.toString()); 253 assertEquals(5, sb.length()); 254 255 sb = new StringBuffer(fixture); 256 assertSame(sb, sb.insert(2, (CharSequence) "ab", 0, 2)); 257 assertEquals("00ab00", sb.toString()); 258 assertEquals(6, sb.length()); 259 260 sb = new StringBuffer(fixture); 261 assertSame(sb, sb.insert(2, (CharSequence) "ab", 0, 1)); 262 assertEquals("00a00", sb.toString()); 263 assertEquals(5, sb.length()); 264 265 sb = new StringBuffer(fixture); 266 assertSame(sb, sb.insert(4, (CharSequence) "ab", 0, 2)); 267 assertEquals("0000ab", sb.toString()); 268 assertEquals(6, sb.length()); 269 270 sb = new StringBuffer(fixture); 271 assertSame(sb, sb.insert(4, (CharSequence) "ab", 0, 1)); 272 assertEquals("0000a", sb.toString()); 273 assertEquals(5, sb.length()); 274 275 sb = new StringBuffer(fixture); 276 assertSame(sb, sb.insert(4, (CharSequence) null, 0, 2)); 277 assertEquals("0000nu", sb.toString()); 278 assertEquals(6, sb.length()); 279 280 try { 281 sb = new StringBuffer(fixture); 282 sb.insert(-1, (CharSequence) "ab", 0, 2); 283 fail("no IOOBE, negative index"); 284 } catch (IndexOutOfBoundsException e) { 285 // Expected 286 } 287 288 try { 289 sb = new StringBuffer(fixture); 290 sb.insert(5, (CharSequence) "ab", 0, 2); 291 fail("no IOOBE, index too large index"); 292 } catch (IndexOutOfBoundsException e) { 293 // Expected 294 } 295 296 try { 297 sb = new StringBuffer(fixture); 298 sb.insert(5, (CharSequence) "ab", -1, 2); 299 fail("no IOOBE, negative offset"); 300 } catch (IndexOutOfBoundsException e) { 301 // Expected 302 } 303 304 try { 305 sb = new StringBuffer(fixture); 306 sb.insert(5, new char[] { 'a', 'b' }, 0, -1); 307 fail("no IOOBE, negative length"); 308 } catch (IndexOutOfBoundsException e) { 309 // Expected 310 } 311 312 try { 313 sb = new StringBuffer(fixture); 314 sb.insert(5, new char[] { 'a', 'b' }, 0, 3); 315 fail("no IOOBE, too long"); 316 } catch (IndexOutOfBoundsException e) { 317 // Expected 318 } 319 } 320 321 /** 322 * @tests java.lang.StringBuffer.insert(int, char) 323 */ 324 public void test_insertIC() { 325 StringBuffer obj = new StringBuffer(); 326 try { 327 obj.insert(-1, ' '); 328 fail("ArrayIndexOutOfBoundsException expected"); 329 } catch (ArrayIndexOutOfBoundsException e) { 330 // expected 331 } 332 } 333 334 /** 335 * @tests java.lang.StringBuffer.appendCodePoint(int)' 336 */ 337 public void test_appendCodePointI() { 338 StringBuffer sb = new StringBuffer(); 339 sb.appendCodePoint(0x10000); 340 assertEquals("\uD800\uDC00", sb.toString()); 341 sb.append("fixture"); 342 assertEquals("\uD800\uDC00fixture", sb.toString()); 343 sb.appendCodePoint(0x00010FFFF); 344 assertEquals("\uD800\uDC00fixture\uDBFF\uDFFF", sb.toString()); 345 } 346 347 /** 348 * @tests java.lang.StringBuffer.codePointAt(int) 349 */ 350 public void test_codePointAtI() { 351 StringBuffer sb = new StringBuffer("abc"); 352 assertEquals('a', sb.codePointAt(0)); 353 assertEquals('b', sb.codePointAt(1)); 354 assertEquals('c', sb.codePointAt(2)); 355 356 sb = new StringBuffer("\uD800\uDC00"); 357 assertEquals(0x10000, sb.codePointAt(0)); 358 assertEquals('\uDC00', sb.codePointAt(1)); 359 360 try { 361 sb.codePointAt(-1); 362 fail("No IOOBE on negative index."); 363 } catch (IndexOutOfBoundsException e) { 364 365 } 366 367 try { 368 sb.codePointAt(sb.length()); 369 fail("No IOOBE on index equal to length."); 370 } catch (IndexOutOfBoundsException e) { 371 372 } 373 374 try { 375 sb.codePointAt(sb.length() + 1); 376 fail("No IOOBE on index greater than length."); 377 } catch (IndexOutOfBoundsException e) { 378 379 } 380 } 381 382 /** 383 * @tests java.lang.StringBuffer.codePointBefore(int) 384 */ 385 public void test_codePointBeforeI() { 386 StringBuffer sb = new StringBuffer("abc"); 387 assertEquals('a', sb.codePointBefore(1)); 388 assertEquals('b', sb.codePointBefore(2)); 389 assertEquals('c', sb.codePointBefore(3)); 390 391 sb = new StringBuffer("\uD800\uDC00"); 392 assertEquals(0x10000, sb.codePointBefore(2)); 393 assertEquals('\uD800', sb.codePointBefore(1)); 394 395 try { 396 sb.codePointBefore(0); 397 fail("No IOOBE on zero index."); 398 } catch (IndexOutOfBoundsException e) { 399 400 } 401 402 try { 403 sb.codePointBefore(-1); 404 fail("No IOOBE on negative index."); 405 } catch (IndexOutOfBoundsException e) { 406 407 } 408 409 try { 410 sb.codePointBefore(sb.length() + 1); 411 fail("No IOOBE on index greater than length."); 412 } catch (IndexOutOfBoundsException e) { 413 414 } 415 } 416 417 /** 418 * @tests java.lang.StringBuffer.codePointCount(int, int) 419 */ 420 public void test_codePointCountII() { 421 assertEquals(1, new StringBuffer("\uD800\uDC00").codePointCount(0, 2)); 422 assertEquals(1, new StringBuffer("\uD800\uDC01").codePointCount(0, 2)); 423 assertEquals(1, new StringBuffer("\uD801\uDC01").codePointCount(0, 2)); 424 assertEquals(1, new StringBuffer("\uDBFF\uDFFF").codePointCount(0, 2)); 425 426 assertEquals(3, new StringBuffer("a\uD800\uDC00b").codePointCount(0, 4)); 427 assertEquals(4, new StringBuffer("a\uD800\uDC00b\uD800").codePointCount(0, 5)); 428 429 StringBuffer sb = new StringBuffer("abc"); 430 try { 431 sb.codePointCount(-1, 2); 432 fail("No IOOBE for negative begin index."); 433 } catch (IndexOutOfBoundsException e) { 434 435 } 436 437 try { 438 sb.codePointCount(0, 4); 439 fail("No IOOBE for end index that's too large."); 440 } catch (IndexOutOfBoundsException e) { 441 442 } 443 444 try { 445 sb.codePointCount(3, 2); 446 fail("No IOOBE for begin index larger than end index."); 447 } catch (IndexOutOfBoundsException e) { 448 449 } 450 } 451 452 /** 453 * @tests java.lang.StringBuffer.getChars(int, int, char[], int) 454 */ 455 public void test_getCharsII$CI() { 456 StringBuffer obj = new StringBuffer(); 457 try { 458 obj.getChars(0, 0, new char[0], -1); 459 fail("ArrayIndexOutOfBoundsException expected"); 460 } catch (ArrayIndexOutOfBoundsException e) { 461 // expected 462 } 463 } 464 465 /** 466 * @tests java.lang.StringBuffer.offsetByCodePoints(int, int)' 467 */ 468 public void test_offsetByCodePointsII() { 469 int result = new StringBuffer("a\uD800\uDC00b").offsetByCodePoints(0, 2); 470 assertEquals(3, result); 471 472 result = new StringBuffer("abcd").offsetByCodePoints(3, -1); 473 assertEquals(2, result); 474 475 result = new StringBuffer("a\uD800\uDC00b").offsetByCodePoints(0, 3); 476 assertEquals(4, result); 477 478 result = new StringBuffer("a\uD800\uDC00b").offsetByCodePoints(3, -1); 479 assertEquals(1, result); 480 481 result = new StringBuffer("a\uD800\uDC00b").offsetByCodePoints(3, 0); 482 assertEquals(3, result); 483 484 result = new StringBuffer("\uD800\uDC00bc").offsetByCodePoints(3, 0); 485 assertEquals(3, result); 486 487 result = new StringBuffer("a\uDC00bc").offsetByCodePoints(3, -1); 488 assertEquals(2, result); 489 490 result = new StringBuffer("a\uD800bc").offsetByCodePoints(3, -1); 491 assertEquals(2, result); 492 493 StringBuffer sb = new StringBuffer("abc"); 494 try { 495 sb.offsetByCodePoints(-1, 1); 496 fail("No IOOBE for negative index."); 497 } catch (IndexOutOfBoundsException e) { 498 499 } 500 501 try { 502 sb.offsetByCodePoints(0, 4); 503 fail("No IOOBE for offset that's too large."); 504 } catch (IndexOutOfBoundsException e) { 505 506 } 507 508 try { 509 sb.offsetByCodePoints(3, -4); 510 fail("No IOOBE for offset that's too small."); 511 } catch (IndexOutOfBoundsException e) { 512 513 } 514 515 try { 516 sb.offsetByCodePoints(3, 1); 517 fail("No IOOBE for index that's too large."); 518 } catch (IndexOutOfBoundsException e) { 519 520 } 521 522 try { 523 sb.offsetByCodePoints(4, -1); 524 fail("No IOOBE for index that's too large."); 525 } catch (IndexOutOfBoundsException e) { 526 527 } 528 } 529 530 /** 531 * @tests {@link java.lang.StringBuffer#indexOf(String, int)} 532 */ 533 @SuppressWarnings("nls") 534 public void test_IndexOfStringInt() { 535 final String fixture = "0123456789"; 536 StringBuffer sb = new StringBuffer(fixture); 537 assertEquals(0, sb.indexOf("0")); 538 assertEquals(0, sb.indexOf("012")); 539 assertEquals(-1, sb.indexOf("02")); 540 assertEquals(8, sb.indexOf("89")); 541 542 assertEquals(0, sb.indexOf("0"), 0); 543 assertEquals(0, sb.indexOf("012"), 0); 544 assertEquals(-1, sb.indexOf("02"), 0); 545 assertEquals(8, sb.indexOf("89"), 0); 546 547 assertEquals(-1, sb.indexOf("0"), 5); 548 assertEquals(-1, sb.indexOf("012"), 5); 549 assertEquals(-1, sb.indexOf("02"), 0); 550 assertEquals(8, sb.indexOf("89"), 5); 551 552 try { 553 sb.indexOf(null, 0); 554 fail("Should throw a NullPointerExceptionE"); 555 } catch (NullPointerException e) { 556 // Expected 557 } 558 } 559 560 /** 561 * @tests {@link java.lang.StringBuffer#lastIndexOf(String, int)} 562 */ 563 @SuppressWarnings("nls") 564 public void test_lastIndexOfLjava_lang_StringI() { 565 final String fixture = "0123456789"; 566 StringBuffer sb = new StringBuffer(fixture); 567 assertEquals(0, sb.lastIndexOf("0")); 568 assertEquals(0, sb.lastIndexOf("012")); 569 assertEquals(-1, sb.lastIndexOf("02")); 570 assertEquals(8, sb.lastIndexOf("89")); 571 572 assertEquals(0, sb.lastIndexOf("0"), 0); 573 assertEquals(0, sb.lastIndexOf("012"), 0); 574 assertEquals(-1, sb.lastIndexOf("02"), 0); 575 assertEquals(8, sb.lastIndexOf("89"), 0); 576 577 assertEquals(-1, sb.lastIndexOf("0"), 5); 578 assertEquals(-1, sb.lastIndexOf("012"), 5); 579 assertEquals(-1, sb.lastIndexOf("02"), 0); 580 assertEquals(8, sb.lastIndexOf("89"), 5); 581 582 try { 583 sb.lastIndexOf(null, 0); 584 fail("Should throw a NullPointerException"); 585 } catch (NullPointerException e) { 586 // Expected 587 } 588 } 589 590 // comparator for StringBuffer objects 591 private static final SerializableAssert STRING_BUFFER_COMPARATOR = new SerializableAssert() { 592 public void assertDeserialized(Serializable initial, 593 Serializable deserialized) { 594 595 StringBuffer init = (StringBuffer) initial; 596 StringBuffer desr = (StringBuffer) deserialized; 597 598 // serializable fields are: 'count', 'shared', 'value' 599 // serialization of 'shared' is not verified 600 // 'count' + 'value' should result in required string 601 assertEquals("toString", init.toString(), desr.toString()); 602 } 603 }; 604 605 /** 606 * @tests serialization/deserialization. 607 */ 608 public void testSerializationSelf() throws Exception { 609 610 SerializationTest.verifySelf(new StringBuffer("0123456789"), 611 STRING_BUFFER_COMPARATOR); 612 } 613 614 /** 615 * @tests serialization/deserialization compatibility with RI. 616 */ 617 public void testSerializationCompatibility() throws Exception { 618 619 SerializationTest.verifyGolden(this, new StringBuffer("0123456789"), 620 STRING_BUFFER_COMPARATOR); 621 } 622} 623