1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 14 * License for the specific language governing permissions and limitations under 15 * the License. 16 */ 17 18package org.apache.harmony.luni.tests.java.lang; 19 20import java.io.UnsupportedEncodingException; 21import java.lang.reflect.Constructor; 22import java.nio.charset.Charset; 23import java.util.Arrays; 24import java.util.SortedMap; 25 26import junit.framework.TestCase; 27 28public class StringTest extends TestCase { 29 30 private static final Constructor<String> UNSAFE_CONSTRUCTOR; 31 static { 32 Constructor<String> uc; 33 try { 34 uc = String.class.getDeclaredConstructor(new Class[] { int.class, 35 int.class, char[].class }); 36 uc.setAccessible(true); 37 } catch (Exception e) { 38 uc = null; 39 } 40 UNSAFE_CONSTRUCTOR = uc; 41 } 42 43 private static String newString(int start, int len, char[] data) throws Exception { 44 if (UNSAFE_CONSTRUCTOR == null) { 45 return new String(data, start, len); 46 } 47 48 return UNSAFE_CONSTRUCTOR.newInstance(Integer.valueOf(start), Integer.valueOf(len), 49 data); 50 } 51 52 /** 53 * @tests java.lang.String#String() 54 */ 55 public void test_Constructor() { 56 assertEquals("Created incorrect string", "", new String()); 57 } 58 59 /** 60 * @tests java.lang.String#String(byte[]) 61 */ 62 public void test_Constructor$B() { 63 assertEquals("Failed to create string", "HelloWorld", new String( 64 "HelloWorld".getBytes())); 65 } 66 67 /** 68 * @tests java.lang.String#String(byte[], int) 69 */ 70 @SuppressWarnings("deprecation") 71 public void test_Constructor$BI() { 72 String s = new String(new byte[] { 65, 66, 67, 68, 69 }, 0); 73 assertEquals("Incorrect string returned: " + s, "ABCDE", s); 74 s = new String(new byte[] { 65, 66, 67, 68, 69 }, 1); 75 assertFalse("Did not use nonzero hibyte", s.equals("ABCDE")); 76 } 77 78 /** 79 * @tests java.lang.String#String(byte[], int, int) 80 */ 81 public void test_Constructor$BII() { 82 byte[] hwba = "HelloWorld".getBytes(); 83 assertEquals("Failed to create string", "HelloWorld", new String(hwba, 84 0, hwba.length)); 85 86 try { 87 new String(new byte[0], 0, Integer.MAX_VALUE); 88 fail("No IndexOutOfBoundsException"); 89 } catch (IndexOutOfBoundsException e) { 90 } 91 } 92 93 /** 94 * @tests java.lang.String#String(byte[], int, int, int) 95 */ 96 @SuppressWarnings("deprecation") 97 public void test_Constructor$BIII() { 98 String s = new String(new byte[] { 65, 66, 67, 68, 69 }, 0, 1, 3); 99 assertEquals("Incorrect string returned: " + s, "BCD", s); 100 s = new String(new byte[] { 65, 66, 67, 68, 69 }, 1, 0, 5); 101 assertFalse("Did not use nonzero hibyte", s.equals("ABCDE")); 102 } 103 104 /** 105 * @tests java.lang.String#String(byte[], int, int, java.lang.String) 106 */ 107 public void test_Constructor$BIILjava_lang_String() throws Exception { 108 String s = new String(new byte[] { 65, 66, 67, 68, 69 }, 0, 5, "8859_1"); 109 assertEquals("Incorrect string returned: " + s, "ABCDE", s); 110 111 try { 112 new String(new byte[] { 65, 66, 67, 68, 69 }, 0, 5, ""); 113 fail("Should throw UnsupportedEncodingException"); 114 } catch (UnsupportedEncodingException e) { 115 //expected 116 } 117 } 118 119 /** 120 * @tests java.lang.String#String(byte[], java.lang.String) 121 */ 122 public void test_Constructor$BLjava_lang_String() throws Exception { 123 String s = new String(new byte[] { 65, 66, 67, 68, 69 }, "8859_1"); 124 assertEquals("Incorrect string returned: " + s, "ABCDE", s); 125 } 126 127 /** 128 * @tests java.lang.String#String(char[]) 129 */ 130 public void test_Constructor$C() { 131 assertEquals("Failed Constructor test", "World", new String(new char[] { 132 'W', 'o', 'r', 'l', 'd' })); 133 } 134 135 /** 136 * @tests java.lang.String#String(char[], int, int) 137 */ 138 public void test_Constructor$CII() throws Exception { 139 char[] buf = { 'H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd' }; 140 String s = new String(buf, 0, buf.length); 141 assertEquals("Incorrect string created", "HelloWorld", s); 142 143 try { 144 new String(new char[0], 0, Integer.MAX_VALUE); 145 fail("No IndexOutOfBoundsException"); 146 } catch (IndexOutOfBoundsException e) { 147 } 148 } 149 150 /** 151 * @tests java.lang.String#String(java.lang.String) 152 */ 153 public void test_ConstructorLjava_lang_String() { 154 String s = new String("Hello World"); 155 assertEquals("Failed to construct correct string", "Hello World", s); 156 } 157 158 /** 159 * @tests java.lang.String#String(java.lang.StringBuffer) 160 */ 161 public void test_ConstructorLjava_lang_StringBuffer() { 162 StringBuffer sb = new StringBuffer(); 163 sb.append("HelloWorld"); 164 assertEquals("Created incorrect string", "HelloWorld", new String(sb)); 165 } 166 167 /** 168 * @tests java.lang.String#String(java.lang.StringBuilder) 169 */ 170 public void test_ConstructorLjava_lang_StringBuilder() { 171 StringBuilder sb = new StringBuilder(32); 172 sb.append("HelloWorld"); 173 assertEquals("HelloWorld", new String(sb)); 174 175 try { 176 new String((StringBuilder) null); 177 fail("No NPE"); 178 } catch (NullPointerException e) { 179 } 180 } 181 182 /** 183 * @tests java.lang.String#String(int[],int,int) 184 */ 185 public void test_Constructor$III() { 186 assertEquals("HelloWorld", new String(new int[] { 'H', 'e', 'l', 'l', 187 'o', 'W', 'o', 'r', 'l', 'd' }, 0, 10)); 188 assertEquals("Hello", new String(new int[] { 'H', 'e', 'l', 'l', 'o', 189 'W', 'o', 'r', 'l', 'd' }, 0, 5)); 190 assertEquals("World", new String(new int[] { 'H', 'e', 'l', 'l', 'o', 191 'W', 'o', 'r', 'l', 'd' }, 5, 5)); 192 assertEquals("", new String(new int[] { 'H', 'e', 'l', 'l', 'o', 'W', 193 'o', 'r', 'l', 'd' }, 5, 0)); 194 195 assertEquals("\uD800\uDC00", new String(new int[] { 0x010000 }, 0, 1)); 196 assertEquals("\uD800\uDC00a\uDBFF\uDFFF", new String(new int[] { 197 0x010000, 'a', 0x010FFFF }, 0, 3)); 198 199 try { 200 new String((int[]) null, 0, 1); 201 fail("No NPE"); 202 } catch (NullPointerException e) { 203 } 204 205 try { 206 new String(new int[] { 'a', 'b' }, -1, 2); 207 fail("No IOOBE, negative offset"); 208 } catch (IndexOutOfBoundsException e) { 209 } 210 211 try { 212 new String(new int[] { 'a', 'b' }, 0, -1); 213 fail("No IOOBE, negative count"); 214 } catch (IndexOutOfBoundsException e) { 215 } 216 217 try { 218 new String(new int[] { 'a', 'b' }, 0, -1); 219 fail("No IOOBE, negative count"); 220 } catch (IndexOutOfBoundsException e) { 221 } 222 223 try { 224 new String(new int[] { 'a', 'b' }, 0, 3); 225 fail("No IOOBE, too large"); 226 } catch (IndexOutOfBoundsException e) { 227 } 228 } 229 230 /** 231 * @tests java.lang.String#contentEquals(CharSequence) 232 */ 233 public void test_contentEqualsLjava_lang_CharSequence() throws Exception { 234 String s = "abc"; 235 assertTrue(s.contentEquals((CharSequence) new StringBuffer("abc"))); 236 assertFalse(s.contentEquals((CharSequence) new StringBuffer("def"))); 237 assertFalse(s.contentEquals((CharSequence) new StringBuffer("ghij"))); 238 239 s = newString(1, 3, "_abc_".toCharArray()); 240 assertTrue(s.contentEquals((CharSequence) new StringBuffer("abc"))); 241 assertFalse(s.contentEquals((CharSequence) new StringBuffer("def"))); 242 assertFalse(s.contentEquals((CharSequence) new StringBuffer("ghij"))); 243 244 try { 245 s.contentEquals((CharSequence) null); 246 fail("No NPE"); 247 } catch (NullPointerException e) { 248 } 249 } 250 251 /** 252 * @tests java.lang.String#contentEquals(StringBuffer) 253 */ 254 @SuppressWarnings("nls") 255 public void test_boolean_contentEquals_StringBuffer() throws Exception { 256 String s = "abc"; 257 assertTrue(s.contentEquals(new StringBuffer("abc"))); 258 assertFalse(s.contentEquals(new StringBuffer("def"))); 259 assertFalse(s.contentEquals(new StringBuffer("ghij"))); 260 261 s = newString(1, 3, "_abc_".toCharArray()); 262 assertTrue(s.contentEquals(new StringBuffer("abc"))); 263 assertFalse(s.contentEquals(new StringBuffer("def"))); 264 assertFalse(s.contentEquals(new StringBuffer("ghij"))); 265 266 try { 267 s.contentEquals((StringBuffer) null); 268 fail("Should throw a NullPointerException"); 269 } catch (NullPointerException e) { 270 // expected 271 } 272 } 273 274 /** 275 * @tests java.lang.String#contains(CharSequence) 276 */ 277 @SuppressWarnings("cast") 278 public void test_containsLjava_lang_CharSequence() throws Exception { 279 String s = "abcdefghijklmnopqrstuvwxyz"; 280 assertTrue(s.contains((CharSequence) new StringBuffer("abc"))); 281 assertTrue(s.contains((CharSequence) new StringBuffer("def"))); 282 assertFalse(s.contains((CharSequence) new StringBuffer("ac"))); 283 284 s = newString(1, 26, "_abcdefghijklmnopqrstuvwxyz_".toCharArray()); 285 assertTrue(s.contains((CharSequence) new StringBuffer("abc"))); 286 assertTrue(s.contains((CharSequence) new StringBuffer("def"))); 287 assertFalse(s.contains((CharSequence) new StringBuffer("ac"))); 288 289 try { 290 s.contentEquals((CharSequence) null); 291 fail("No NPE"); 292 } catch (NullPointerException e) { 293 } 294 } 295 296 /** 297 * @tests java.lang.String.offsetByCodePoints(int, int)' 298 */ 299 public void test_offsetByCodePoints_II() throws Exception { 300 int result = new String("a\uD800\uDC00b").offsetByCodePoints(0, 2); 301 assertEquals(3, result); 302 303 result = new String("abcd").offsetByCodePoints(3, -1); 304 assertEquals(2, result); 305 306 result = new String("a\uD800\uDC00b").offsetByCodePoints(0, 3); 307 assertEquals(4, result); 308 309 result = new String("a\uD800\uDC00b").offsetByCodePoints(3, -1); 310 assertEquals(1, result); 311 312 result = new String("a\uD800\uDC00b").offsetByCodePoints(3, 0); 313 assertEquals(3, result); 314 315 result = new String("\uD800\uDC00bc").offsetByCodePoints(3, 0); 316 assertEquals(3, result); 317 318 result = new String("a\uDC00bc").offsetByCodePoints(3, -1); 319 assertEquals(2, result); 320 321 result = new String("a\uD800bc").offsetByCodePoints(3, -1); 322 assertEquals(2, result); 323 324 result = newString(2, 4, "__a\uD800\uDC00b__".toCharArray()) 325 .offsetByCodePoints(0, 2); 326 assertEquals(3, result); 327 328 result = newString(2, 4, "__abcd__".toCharArray()).offsetByCodePoints( 329 3, -1); 330 assertEquals(2, result); 331 332 result = newString(2, 4, "__a\uD800\uDC00b__".toCharArray()) 333 .offsetByCodePoints(0, 3); 334 assertEquals(4, result); 335 336 result = newString(2, 4, "__a\uD800\uDC00b__".toCharArray()) 337 .offsetByCodePoints(3, -1); 338 assertEquals(1, result); 339 340 result = newString(2, 4, "__a\uD800\uDC00b__".toCharArray()) 341 .offsetByCodePoints(3, 0); 342 assertEquals(3, result); 343 344 result = newString(2, 4, "__\uD800\uDC00bc__".toCharArray()) 345 .offsetByCodePoints(3, 0); 346 assertEquals(3, result); 347 348 result = newString(2, 4, "__a\uDC00bc__".toCharArray()) 349 .offsetByCodePoints(3, -1); 350 assertEquals(2, result); 351 352 result = newString(2, 4, "__a\uD800bc__".toCharArray()) 353 .offsetByCodePoints(3, -1); 354 assertEquals(2, result); 355 356 String s = "abc"; 357 try { 358 s.offsetByCodePoints(-1, 1); 359 fail("No IOOBE for negative index."); 360 } catch (IndexOutOfBoundsException e) { 361 } 362 363 try { 364 s.offsetByCodePoints(0, 4); 365 fail("No IOOBE for offset that's too large."); 366 } catch (IndexOutOfBoundsException e) { 367 } 368 369 try { 370 s.offsetByCodePoints(3, -4); 371 fail("No IOOBE for offset that's too small."); 372 } catch (IndexOutOfBoundsException e) { 373 } 374 375 try { 376 s.offsetByCodePoints(3, 1); 377 fail("No IOOBE for index that's too large."); 378 } catch (IndexOutOfBoundsException e) { 379 } 380 381 try { 382 s.offsetByCodePoints(4, -1); 383 fail("No IOOBE for index that's too large."); 384 } catch (IndexOutOfBoundsException e) { 385 } 386 387 s = newString(2,3,"__abc__".toCharArray()); 388 try { 389 s.offsetByCodePoints(-1, 1); 390 fail("No IOOBE for negative index."); 391 } catch (IndexOutOfBoundsException e) { 392 } 393 394 try { 395 s.offsetByCodePoints(0, 4); 396 fail("No IOOBE for offset that's too large."); 397 } catch (IndexOutOfBoundsException e) { 398 } 399 400 try { 401 s.offsetByCodePoints(3, -4); 402 fail("No IOOBE for offset that's too small."); 403 } catch (IndexOutOfBoundsException e) { 404 } 405 406 try { 407 s.offsetByCodePoints(3, 1); 408 fail("No IOOBE for index that's too large."); 409 } catch (IndexOutOfBoundsException e) { 410 } 411 412 try { 413 s.offsetByCodePoints(4, -1); 414 fail("No IOOBE for index that's too large."); 415 } catch (IndexOutOfBoundsException e) { 416 } 417 } 418 419 /** 420 * @tests java.lang.StringBuilder.codePointAt(int) 421 */ 422 public void test_codePointAtI() throws Exception { 423 String s = "abc"; 424 assertEquals('a', s.codePointAt(0)); 425 assertEquals('b', s.codePointAt(1)); 426 assertEquals('c', s.codePointAt(2)); 427 428 s = newString(2,3,"__abc__".toCharArray()); 429 assertEquals('a', s.codePointAt(0)); 430 assertEquals('b', s.codePointAt(1)); 431 assertEquals('c', s.codePointAt(2)); 432 433 s = "\uD800\uDC00"; 434 assertEquals(0x10000, s.codePointAt(0)); 435 assertEquals('\uDC00', s.codePointAt(1)); 436 437 s = newString(2,2,"__\uD800\uDC00__".toCharArray()); 438 assertEquals(0x10000, s.codePointAt(0)); 439 assertEquals('\uDC00', s.codePointAt(1)); 440 441 s = "abc"; 442 try { 443 s.codePointAt(-1); 444 fail("No IOOBE on negative index."); 445 } catch (IndexOutOfBoundsException e) { 446 } 447 448 try { 449 s.codePointAt(s.length()); 450 fail("No IOOBE on index equal to length."); 451 } catch (IndexOutOfBoundsException e) { 452 } 453 454 try { 455 s.codePointAt(s.length() + 1); 456 fail("No IOOBE on index greater than length."); 457 } catch (IndexOutOfBoundsException e) { 458 } 459 460 s = newString(2,3,"__abc__".toCharArray()); 461 try { 462 s.codePointAt(-1); 463 fail("No IOOBE on negative index."); 464 } catch (IndexOutOfBoundsException e) { 465 } 466 467 try { 468 s.codePointAt(s.length()); 469 fail("No IOOBE on index equal to length."); 470 } catch (IndexOutOfBoundsException e) { 471 } 472 473 try { 474 s.codePointAt(s.length() + 1); 475 fail("No IOOBE on index greater than length."); 476 } catch (IndexOutOfBoundsException e) { 477 } 478 } 479 480 /** 481 * @tests java.lang.StringBuilder.codePointBefore(int) 482 */ 483 public void test_codePointBeforeI() throws Exception { 484 String s = "abc"; 485 assertEquals('a', s.codePointBefore(1)); 486 assertEquals('b', s.codePointBefore(2)); 487 assertEquals('c', s.codePointBefore(3)); 488 489 s = newString(2,3,"__abc__".toCharArray()); 490 assertEquals('a', s.codePointBefore(1)); 491 assertEquals('b', s.codePointBefore(2)); 492 assertEquals('c', s.codePointBefore(3)); 493 494 s = "\uD800\uDC00"; 495 assertEquals(0x10000, s.codePointBefore(2)); 496 assertEquals('\uD800', s.codePointBefore(1)); 497 498 s = newString(2,2,"__\uD800\uDC00__".toCharArray()); 499 assertEquals(0x10000, s.codePointBefore(2)); 500 assertEquals('\uD800', s.codePointBefore(1)); 501 502 s = "abc"; 503 try { 504 s.codePointBefore(0); 505 fail("No IOOBE on zero index."); 506 } catch (IndexOutOfBoundsException e) { 507 } 508 509 try { 510 s.codePointBefore(-1); 511 fail("No IOOBE on negative index."); 512 } catch (IndexOutOfBoundsException e) { 513 } 514 515 try { 516 s.codePointBefore(s.length() + 1); 517 fail("No IOOBE on index greater than length."); 518 } catch (IndexOutOfBoundsException e) { 519 } 520 521 s = newString(2,3,"__abc__".toCharArray()); 522 try { 523 s.codePointBefore(0); 524 fail("No IOOBE on zero index."); 525 } catch (IndexOutOfBoundsException e) { 526 } 527 528 try { 529 s.codePointBefore(-1); 530 fail("No IOOBE on negative index."); 531 } catch (IndexOutOfBoundsException e) { 532 } 533 534 try { 535 s.codePointBefore(s.length() + 1); 536 fail("No IOOBE on index greater than length."); 537 } catch (IndexOutOfBoundsException e) { 538 } 539 } 540 541 /** 542 * @tests java.lang.StringBuilder.codePointCount(int, int) 543 */ 544 public void test_codePointCountII() throws Exception { 545 assertEquals(1, "\uD800\uDC00".codePointCount(0, 2)); 546 assertEquals(1, "\uD800\uDC01".codePointCount(0, 2)); 547 assertEquals(1, "\uD801\uDC01".codePointCount(0, 2)); 548 assertEquals(1, "\uDBFF\uDFFF".codePointCount(0, 2)); 549 550 assertEquals(3, "a\uD800\uDC00b".codePointCount(0, 4)); 551 assertEquals(4, "a\uD800\uDC00b\uD800".codePointCount(0, 5)); 552 553 assertEquals(1, newString(2,2,"__\uD800\uDC00__".toCharArray()).codePointCount(0, 2)); 554 assertEquals(1, newString(2,2,"__\uD800\uDC01__".toCharArray()).codePointCount(0, 2)); 555 assertEquals(1, newString(2,2,"__\uD801\uDC01__".toCharArray()).codePointCount(0, 2)); 556 assertEquals(1, newString(2,2,"__\uDBFF\uDFFF__".toCharArray()).codePointCount(0, 2)); 557 558 assertEquals(3, newString(2,4,"__a\uD800\uDC00b__".toCharArray()).codePointCount(0, 4)); 559 assertEquals(4, newString(2,5,"__a\uD800\uDC00b\uD800__".toCharArray()).codePointCount(0, 5)); 560 561 String s = "abc"; 562 try { 563 s.codePointCount(-1, 2); 564 fail("No IOOBE for negative begin index."); 565 } catch (IndexOutOfBoundsException e) { 566 } 567 568 try { 569 s.codePointCount(0, 4); 570 fail("No IOOBE for end index that's too large."); 571 } catch (IndexOutOfBoundsException e) { 572 } 573 574 try { 575 s.codePointCount(3, 2); 576 fail("No IOOBE for begin index larger than end index."); 577 } catch (IndexOutOfBoundsException e) { 578 } 579 580 s = newString(2, 3, "__abc__".toCharArray()); 581 try { 582 s.codePointCount(-1, 2); 583 fail("No IOOBE for negative begin index."); 584 } catch (IndexOutOfBoundsException e) { 585 } 586 587 try { 588 s.codePointCount(0, 4); 589 fail("No IOOBE for end index that's too large."); 590 } catch (IndexOutOfBoundsException e) { 591 } 592 593 try { 594 s.codePointCount(3, 2); 595 fail("No IOOBE for begin index larger than end index."); 596 } catch (IndexOutOfBoundsException e) { 597 } 598 } 599 600 /** 601 * @tests {@link java.lang.String#String(byte[], int, int, Charset)} 602 * 603 * @since 1.6 604 */ 605 public void test_ConstructorBIIL() throws Exception { 606 // can construct normally 607 new String(new byte[8], 0, 4, Charset.defaultCharset()); 608 new String(new byte[8], 8, 0, Charset.defaultCharset()); 609 new String(new byte[0], 0, 0, Charset.defaultCharset()); 610 // throws exceptions 611 try { 612 new String(new byte[8], 0, 9, Charset.defaultCharset()); 613 fail("should throw StringIndexOutOfBoundsException"); 614 } catch (StringIndexOutOfBoundsException e) { 615 // expected 616 } 617 try { 618 new String(new byte[8], 9, 0, Charset.defaultCharset()); 619 fail("should throw StringIndexOutOfBoundsException"); 620 } catch (StringIndexOutOfBoundsException e) { 621 // expected 622 } 623 try { 624 new String(new byte[8], -1, 0, Charset.defaultCharset()); 625 fail("should throw StringIndexOutOfBoundsException"); 626 } catch (StringIndexOutOfBoundsException e) { 627 // expected 628 } 629 try { 630 new String(new byte[8], 9, -1, Charset.defaultCharset()); 631 fail("should throw StringIndexOutOfBoundsException"); 632 } catch (StringIndexOutOfBoundsException e) { 633 // expected 634 } 635 try { 636 new String(null, -1, 0, Charset.defaultCharset()); 637 fail(); 638 } catch (NullPointerException expected) { 639 } catch (StringIndexOutOfBoundsException expected) { 640 } 641 try { 642 new String(null, 0, -1, Charset.defaultCharset()); 643 fail(); 644 } catch (NullPointerException expected) { 645 } catch (StringIndexOutOfBoundsException expected) { 646 } 647 try { 648 new String(null, 0, 9, Charset.defaultCharset()); 649 fail("should throw NullPointerException"); 650 } catch (NullPointerException e) { 651 // expected 652 } 653 try { 654 new String(null, 0, 0, Charset.defaultCharset()); 655 fail("should throw NullPointerException"); 656 } catch (NullPointerException e) { 657 // expected 658 } 659 try { 660 new String(null, -1, 0, (Charset)null); 661 fail("should throw NullPointerException"); 662 } catch (NullPointerException e) { 663 // expected 664 } 665 try { 666 new String(new byte[8], -1, 0, (Charset)null); 667 fail(); 668 } catch (NullPointerException expected) { 669 } catch (StringIndexOutOfBoundsException expected) { 670 } 671 try { 672 new String(new byte[8], 0, 9, (Charset)null); 673 fail(); 674 } catch (NullPointerException expected) { 675 } catch (StringIndexOutOfBoundsException expected) { 676 } 677 try { 678 new String(new byte[8], 0, 4, (Charset)null); 679 fail("should throw NullPointerException"); 680 } catch (NullPointerException e) { 681 // expected 682 } 683 } 684 685 /** 686 * @tests {@link java.lang.String#String(byte[], Charset)} 687 * 688 * @since 1.6 689 */ 690 public void test_ConstructorBL() throws Exception { 691 new String(new byte[8], Charset.defaultCharset()); 692 try { 693 new String(new byte[8],(Charset)null); 694 fail("should throw NullPointerException"); 695 } catch (NullPointerException e) { 696 // expected 697 } 698 try { 699 new String(new byte[0],(Charset)null); 700 fail("should throw NullPointerException"); 701 } catch (NullPointerException e) { 702 // expected 703 } 704 try { 705 new String(null,Charset.defaultCharset()); 706 fail("should throw NullPointerException"); 707 } catch (NullPointerException e) { 708 // expected 709 } 710 new String(new byte[0], Charset.defaultCharset()); 711 } 712 713 /** 714 * @tests {@link java.lang.String#isEmpty()} 715 * 716 * @since 1.6 717 */ 718 public void test_isEmpty() throws Exception { 719 assertTrue(new String(new byte[0], Charset.defaultCharset()).isEmpty()); 720 assertTrue(new String(new byte[8], Charset.defaultCharset()).substring(0, 0).isEmpty()); 721 } 722 723 /** 724 * @tests {@link java.lang.String#getBytes(Charset)} 725 * 726 * @since 1.6 727 */ 728 public void test_getBytesLCharset() throws Exception { 729 byte[] emptyBytes = new byte[0]; 730 byte[] someBytes = new byte[]{'T','h','i','s',' ',' ','i','s',' ','t','e','s','t',' ','b','y','t','e','s'}; 731 assertEquals(0, new String(emptyBytes, Charset.defaultCharset()).getBytes(Charset.defaultCharset()).length); 732 try{ 733 new String(emptyBytes, Charset.defaultCharset()).getBytes((Charset)null); 734 fail("should throw NPE"); 735 } catch (NullPointerException e){ 736 // correct 737 } 738 assertTrue(bytesEquals(someBytes,new String(someBytes, Charset.defaultCharset()).getBytes(Charset.defaultCharset()))); 739 SortedMap<String, Charset> charsets = Charset.availableCharsets(); 740 } 741 742 boolean bytesEquals(byte[] bytes1, byte[] bytes2){ 743 return Arrays.toString(bytes1).equals(Arrays.toString(bytes2)); 744 } 745} 746