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