String2Test.java revision ef7122278207e33b724c6360945f9eae1f9a5a58
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, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package org.apache.harmony.luni.tests.java.lang; 19 20import dalvik.annotation.BrokenTest; 21import dalvik.annotation.TestTargets; 22import dalvik.annotation.TestLevel; 23import dalvik.annotation.TestTargetNew; 24import dalvik.annotation.TestTargetClass; 25 26import java.io.UnsupportedEncodingException; 27import java.util.Arrays; 28import java.util.Calendar; 29import java.util.Date; 30import java.util.IllegalFormatException; 31import java.util.Locale; 32import java.util.regex.Pattern; 33import java.util.regex.PatternSyntaxException; 34 35@TestTargetClass(String.class) 36public class String2Test extends junit.framework.TestCase { 37 38 String hw1 = "HelloWorld"; 39 40 String hw2 = "HelloWorld"; 41 42 String hwlc = "helloworld"; 43 44 String hwuc = "HELLOWORLD"; 45 46 String hello1 = "Hello"; 47 48 String world1 = "World"; 49 50 String comp11 = "Test String"; 51 52 Object obj = new Object(); 53 54 char[] buf = { 'W', 'o', 'r', 'l', 'd' }; 55 56 char[] rbuf = new char[5]; 57 58 /** 59 * @tests java.lang.String#String() 60 */ 61 @TestTargetNew( 62 level = TestLevel.COMPLETE, 63 notes = "", 64 method = "String", 65 args = {java.lang.String.class} 66 ) 67 public void test_Constructor() { 68 // Test for method java.lang.String() 69 assertTrue("Created incorrect string", new String().equals("")); 70 } 71 72 /** 73 * @tests java.lang.String#String(byte[]) 74 */ 75 @TestTargetNew( 76 level = TestLevel.COMPLETE, 77 notes = "", 78 method = "String", 79 args = {byte[].class} 80 ) 81 public void test_Constructor$B() { 82 // Test for method java.lang.String(byte []) 83 assertTrue("Failed to create string", new String(hw1.getBytes()) 84 .equals(hw1)); 85 } 86 87 /** 88 * @tests java.lang.String#String(byte[], int) 89 */ 90 @TestTargetNew( 91 level = TestLevel.COMPLETE, 92 notes = "", 93 method = "String", 94 args = {byte[].class, int.class} 95 ) 96 @SuppressWarnings("deprecation") 97 public void test_Constructor$BI() { 98 // Test for method java.lang.String(byte [], int) 99 String s = new String(new byte[] { 65, 66, 67, 68, 69 }, 0); 100 assertTrue("Incorrect string returned: " + s, s.equals("ABCDE")); 101 s = new String(new byte[] { 65, 66, 67, 68, 69 }, 1); 102 assertTrue("Did not use nonzero hibyte", !s.equals("ABCDE")); 103 } 104 105 /** 106 * @tests java.lang.String#String(byte[], int, int) 107 */ 108 @TestTargetNew( 109 level = TestLevel.COMPLETE, 110 notes = "", 111 method = "String", 112 args = {byte[].class, int.class, int.class} 113 ) 114 public void test_Constructor$BII() { 115 // Test for method java.lang.String(byte [], int, int) 116 assertTrue("Failed to create string", new String(hw1.getBytes(), 0, hw1 117 .getBytes().length).equals(hw1)); 118 119 boolean exception = false; 120 try { 121 new String(new byte[0], 0, Integer.MAX_VALUE); 122 } catch (IndexOutOfBoundsException e) { 123 exception = true; 124 } 125 assertTrue("Did not throw exception", exception); 126 } 127 128 /** 129 * @tests java.lang.String#String(byte[], int, int, int) 130 */ 131 @TestTargetNew( 132 level = TestLevel.COMPLETE, 133 notes = "", 134 method = "String", 135 args = {byte[].class, int.class, int.class, int.class} 136 ) 137 @SuppressWarnings("deprecation") 138 public void test_Constructor$BIII() { 139 // Test for method java.lang.String(byte [], int, int, int) 140 String s = new String(new byte[] { 65, 66, 67, 68, 69 }, 0, 1, 3); 141 assertTrue("Incorrect string returned: " + s, s.equals("BCD")); 142 s = new String(new byte[] { 65, 66, 67, 68, 69 }, 1, 0, 5); 143 assertTrue("Did not use nonzero hibyte", !s.equals("ABCDE")); 144 } 145 146 /** 147 * @tests java.lang.String#String(byte[], int, int, java.lang.String) 148 */ 149 @TestTargetNew( 150 level = TestLevel.COMPLETE, 151 notes = "", 152 method = "String", 153 args = {byte[].class, int.class, int.class, java.lang.String.class} 154 ) 155 public void test_Constructor$BIILjava_lang_String() throws Exception { 156 // Test for method java.lang.String(byte [], int, int, java.lang.String) 157 String s = null; 158 try { 159 s = new String(new byte[] { 65, 66, 67, 68, 69 }, 0, 5, "8859_1"); 160 } catch (Exception e) { 161 fail("Threw exception : " + e.getMessage()); 162 } 163 assertTrue("Incorrect string returned: " + s, s.equals("ABCDE")); 164 //Regression for HARMONY-1111 165 assertNotNull(new String(new byte[] {(byte)0xC0}, 0, 1, "UTF-8")); 166 } 167 168 /** 169 * @tests java.lang.String#String(byte[], java.lang.String) 170 */ 171 @TestTargetNew( 172 level = TestLevel.COMPLETE, 173 notes = "", 174 method = "String", 175 args = {byte[].class, java.lang.String.class} 176 ) 177 public void test_Constructor$BLjava_lang_String() { 178 // Test for method java.lang.String(byte [], java.lang.String) 179 String s = null; 180 try { 181 s = new String(new byte[] { 65, 66, 67, 68, 69 }, "8859_1"); 182 } catch (Exception e) { 183 fail("Threw exception : " + e.getMessage()); 184 } 185 assertTrue("Incorrect string returned: " + s, s.equals("ABCDE")); 186 } 187 188 /** 189 * @tests java.lang.String#String(char[]) 190 */ 191 @TestTargetNew( 192 level = TestLevel.COMPLETE, 193 notes = "", 194 method = "String", 195 args = {char[].class} 196 ) 197 public void test_Constructor$C() { 198 // Test for method java.lang.String(char []) 199 assertEquals("Failed Constructor test", "World", new String(buf)); 200 } 201 202 /** 203 * @tests java.lang.String#String(char[], int, int) 204 */ 205 @TestTargetNew( 206 level = TestLevel.COMPLETE, 207 notes = "", 208 method = "String", 209 args = {char[].class, int.class, int.class} 210 ) 211 public void test_Constructor$CII() { 212 // Test for method java.lang.String(char [], int, int) 213 char[] buf = { 'H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd' }; 214 String s = new String(buf, 0, buf.length); 215 assertTrue("Incorrect string created", hw1.equals(s)); 216 217 boolean exception = false; 218 try { 219 new String(new char[0], 0, Integer.MAX_VALUE); 220 } catch (IndexOutOfBoundsException e) { 221 exception = true; 222 } 223 assertTrue("Did not throw exception", exception); 224 } 225 226 /** 227 * @tests java.lang.String#String(int[], int, int) 228 */ 229 @TestTargetNew( 230 level = TestLevel.COMPLETE, 231 notes = "", 232 method = "String", 233 args = {int[].class, int.class, int.class} 234 ) 235 public void test_Constructor$III() { 236 // Test for method java.lang.String(int [], int, int) 237 try { 238 new String(new int[0], 2, Integer.MAX_VALUE); 239 fail("Did not throw exception"); 240 } catch (IndexOutOfBoundsException e) { 241 //expected 242 } 243 } 244 245 /** 246 * @tests java.lang.String#String(java.lang.String) 247 */ 248 @TestTargetNew( 249 level = TestLevel.COMPLETE, 250 notes = "", 251 method = "String", 252 args = {java.lang.String.class} 253 ) 254 public void test_ConstructorLjava_lang_String() { 255 // Test for method java.lang.String(java.lang.String) 256 String s = new String("Hello World"); 257 assertEquals("Failed to construct correct string", "Hello World", s 258 ); 259 } 260 261 /** 262 * @tests java.lang.String#String(java.lang.StringBuffer) 263 */ 264 @TestTargetNew( 265 level = TestLevel.COMPLETE, 266 notes = "", 267 method = "String", 268 args = {java.lang.StringBuffer.class} 269 ) 270 public void test_ConstructorLjava_lang_StringBuffer() { 271 // Test for method java.lang.String(java.lang.StringBuffer) 272 StringBuffer sb = new StringBuffer(); 273 sb.append("HelloWorld"); 274 assertEquals("Created incorrect string", "HelloWorld", new String(sb) 275 ); 276 } 277 278 /** 279 * @tests java.lang.String#charAt(int) 280 */ 281 @TestTargetNew( 282 level = TestLevel.COMPLETE, 283 notes = "", 284 method = "charAt", 285 args = {int.class} 286 ) 287 public void test_charAtI() { 288 // Test for method char java.lang.String.charAt(int) 289 assertTrue("Incorrect character returned", hw1.charAt(5) == 'W' 290 && (hw1.charAt(1) != 'Z')); 291 292 String testString = "Test String"; 293 try { 294 testString.charAt(testString.length()); 295 fail("IndexOutOfBoundsException was not thrown."); 296 } catch(IndexOutOfBoundsException iobe) { 297 //expected 298 } 299 300 try { 301 testString.charAt(Integer.MAX_VALUE); 302 fail("IndexOutOfBoundsException was not thrown."); 303 } catch(IndexOutOfBoundsException iobe) { 304 //expected 305 } 306 307 try { 308 testString.charAt(-1); 309 fail("IndexOutOfBoundsException was not thrown."); 310 } catch(IndexOutOfBoundsException iobe) { 311 //expected 312 } 313 } 314 315 /** 316 * @tests java.lang.String#compareTo(java.lang.String) 317 */ 318 @TestTargetNew( 319 level = TestLevel.COMPLETE, 320 notes = "", 321 method = "compareTo", 322 args = {java.lang.String.class} 323 ) 324 public void test_compareToLjava_lang_String() { 325 // Test for method int java.lang.String.compareTo(java.lang.String) 326 assertTrue("Returned incorrect value for first < second", "aaaaab" 327 .compareTo("aaaaac") < 0); 328 assertEquals("Returned incorrect value for first = second", 0, "aaaaac" 329 .compareTo("aaaaac")); 330 assertTrue("Returned incorrect value for first > second", "aaaaac" 331 .compareTo("aaaaab") > 0); 332 assertTrue("Considered case to not be of importance", !("A" 333 .compareTo("a") == 0)); 334 335 try { 336 "fixture".compareTo(null); 337 fail("No NPE"); 338 } catch (NullPointerException e) { 339 } 340 } 341 342 /** 343 * @tests java.lang.String#compareToIgnoreCase(java.lang.String) 344 */ 345 @TestTargetNew( 346 level = TestLevel.COMPLETE, 347 notes = "", 348 method = "compareToIgnoreCase", 349 args = {java.lang.String.class} 350 ) 351 public void test_compareToIgnoreCaseLjava_lang_String() { 352 // Test for method int 353 // java.lang.String.compareToIgnoreCase(java.lang.String) 354 assertTrue("Returned incorrect value for first < second", "aaaaab" 355 .compareToIgnoreCase("aaaaac") < 0); 356 assertEquals("Returned incorrect value for first = second", 0, "aaaaac" 357 .compareToIgnoreCase("aaaaac")); 358 assertTrue("Returned incorrect value for first > second", "aaaaac" 359 .compareToIgnoreCase("aaaaab") > 0); 360 assertEquals("Considered case to not be of importance", 0, "A" 361 .compareToIgnoreCase("a")); 362 363 assertTrue("0xbf should not compare = to 'ss'", "\u00df" 364 .compareToIgnoreCase("ss") != 0); 365 assertEquals("0x130 should compare = to 'i'", 0, "\u0130" 366 .compareToIgnoreCase("i")); 367 assertEquals("0x131 should compare = to 'i'", 0, "\u0131" 368 .compareToIgnoreCase("i")); 369 370 Locale defLocale = Locale.getDefault(); 371 try { 372 Locale.setDefault(new Locale("tr", "")); 373 assertEquals("Locale tr: 0x130 should compare = to 'i'", 0, "\u0130" 374 .compareToIgnoreCase("i")); 375 assertEquals("Locale tr: 0x131 should compare = to 'i'", 0, "\u0131" 376 .compareToIgnoreCase("i")); 377 } finally { 378 Locale.setDefault(defLocale); 379 } 380 381 try { 382 "fixture".compareToIgnoreCase(null); 383 fail("No NPE"); 384 } catch (NullPointerException e) { 385 } 386 } 387 388 /** 389 * @tests java.lang.String#concat(java.lang.String) 390 */ 391 @TestTargetNew( 392 level = TestLevel.COMPLETE, 393 notes = "", 394 method = "concat", 395 args = {java.lang.String.class} 396 ) 397 public void test_concatLjava_lang_String() { 398 // Test for method java.lang.String 399 // java.lang.String.concat(java.lang.String) 400 assertTrue("Concatenation failed to produce correct string", hello1 401 .concat(world1).equals(hw1)); 402 boolean exception = false; 403 try { 404 String a = new String("test"); 405 String b = null; 406 a.concat(b); 407 } catch (NullPointerException e) { 408 exception = true; 409 } 410 assertTrue("Concatenation failed to throw NP exception (1)", exception); 411 exception = false; 412 try { 413 String a = new String(""); 414 String b = null; 415 a.concat(b); 416 } catch (NullPointerException e) { 417 exception = true; 418 } 419 assertTrue("Concatenation failed to throw NP exception (2)", exception); 420 421 String s1 = ""; 422 String s2 = "s2"; 423 String s3 = s1.concat(s2); 424 assertEquals(s2, s3); 425 426 s3 = s2.concat(s1); 427 assertSame(s2, s3); 428 } 429 430 /** 431 * @tests java.lang.String#copyValueOf(char[]) 432 */ 433 @TestTargetNew( 434 level = TestLevel.COMPLETE, 435 notes = "", 436 method = "copyValueOf", 437 args = {char[].class} 438 ) 439 public void test_copyValueOf$C() { 440 // Test for method java.lang.String java.lang.String.copyValueOf(char 441 // []) 442 char[] t = { 'H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd' }; 443 assertEquals("copyValueOf returned incorrect String", "HelloWorld", String.copyValueOf( 444 t)); 445 } 446 447 /** 448 * @tests java.lang.String#copyValueOf(char[], int, int) 449 */ 450 @TestTargetNew( 451 level = TestLevel.COMPLETE, 452 notes = "", 453 method = "copyValueOf", 454 args = {char[].class, int.class, int.class} 455 ) 456 public void test_copyValueOf$CII() { 457 // Test for method java.lang.String java.lang.String.copyValueOf(char 458 // [], int, int) 459 char[] t = { 'H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd' }; 460 assertEquals("copyValueOf returned incorrect String", "World", String.copyValueOf( 461 t, 5, 5)); 462 } 463 464 /** 465 * @tests java.lang.String#endsWith(java.lang.String) 466 */ 467 @TestTargetNew( 468 level = TestLevel.COMPLETE, 469 notes = "", 470 method = "endsWith", 471 args = {java.lang.String.class} 472 ) 473 public void test_endsWithLjava_lang_String() { 474 // Test for method boolean java.lang.String.endsWith(java.lang.String) 475 assertTrue("Failed to fine ending string", hw1.endsWith("ld")); 476 assertFalse("Doesn't return false value.", hw1.endsWith("ld ")); 477 assertFalse("Doesn't return false value.", hw1.endsWith(" ")); 478 assertTrue("Returned incorrect value for empty string.", hw1.endsWith("")); 479 try { 480 hw1.endsWith(null); 481 fail("NullPointerException is not thrown."); 482 } catch(NullPointerException npe) { 483 //expected 484 } 485 } 486 487 /** 488 * @tests java.lang.String#equals(java.lang.Object) 489 */ 490 @TestTargetNew( 491 level = TestLevel.COMPLETE, 492 notes = "", 493 method = "equals", 494 args = {java.lang.Object.class} 495 ) 496 public void test_equalsLjava_lang_Object() { 497 // Test for method boolean java.lang.String.equals(java.lang.Object) 498 assertTrue("String not equal", hw1.equals(hw2) && !(hw1.equals(comp11))); 499 } 500 501 /** 502 * @tests java.lang.String#equalsIgnoreCase(java.lang.String) 503 */ 504 @TestTargetNew( 505 level = TestLevel.COMPLETE, 506 notes = "", 507 method = "equalsIgnoreCase", 508 args = {java.lang.String.class} 509 ) 510 public void test_equalsIgnoreCaseLjava_lang_String() { 511 // Test for method boolean 512 // java.lang.String.equalsIgnoreCase(java.lang.String) 513 assertTrue("lc version returned unequal to uc", hwlc 514 .equalsIgnoreCase(hwuc)); 515 516 assertTrue("Returned false for equals strings.", hwlc 517 .equalsIgnoreCase(hwlc)); 518 519 assertFalse("Returned true for different strings.", hwlc 520 .equalsIgnoreCase(hwuc + " ")); 521 } 522 523 /** 524 * @tests java.lang.String#getBytes() 525 */ 526 @TestTargetNew( 527 level = TestLevel.COMPLETE, 528 notes = "", 529 method = "getBytes", 530 args = {} 531 ) 532 @BrokenTest("Takes too long for the CTS host") 533 public void test_getBytes() { 534 // Test for method byte [] java.lang.String.getBytes() 535 byte[] sbytes = hw1.getBytes(); 536 for (int i = 0; i < hw1.length(); i++) 537 assertTrue("Returned incorrect bytes", sbytes[i] == (byte) hw1 538 .charAt(i)); 539 540 char[] chars = new char[1]; 541 for (int i = 0; i < 65536; i++) { 542 // skip surrogates 543 if (i == 0xd800) 544 i = 0xe000; 545 byte[] result = null; 546 chars[0] = (char) i; 547 String string = new String(chars); 548 try { 549 result = string.getBytes("8859_1"); 550 if (i < 256) { 551 assertEquals((byte)i, result[0]); 552 } else { 553 /* 554 * Substitue character should be 0x1A [1], but may be 555 * '?' character. 556 * [1] http://en.wikipedia.org/wiki/Substitute_character 557 */ 558 assertTrue(result[0] == '?' || result[0] == 0x1a); 559 } 560 } catch (java.io.UnsupportedEncodingException e) { 561 } 562 try { 563 result = string.getBytes("UTF8"); 564 int length = i < 0x80 ? 1 : (i < 0x800 ? 2 : 3); 565 assertTrue("Wrong length UTF8: " + Integer.toHexString(i), 566 result.length == length); 567 assertTrue( 568 "Wrong bytes UTF8: " + Integer.toHexString(i), 569 (i < 0x80 && result[0] == i) 570 || (i >= 0x80 571 && i < 0x800 572 && result[0] == (byte) (0xc0 | ((i & 0x7c0) >> 6)) && result[1] == (byte) (0x80 | (i & 0x3f))) 573 || (i >= 0x800 574 && result[0] == (byte) (0xe0 | (i >> 12)) 575 && result[1] == (byte) (0x80 | ((i & 0xfc0) >> 6)) && result[2] == (byte) (0x80 | (i & 0x3f)))); 576 } catch (java.io.UnsupportedEncodingException e) { 577 } 578 579 String bytes = null; 580 try { 581 bytes = new String(result, "UTF8"); 582 assertTrue("Wrong UTF8 byte length: " + bytes.length() + "(" 583 + i + ")", bytes.length() == 1); 584 assertTrue( 585 "Wrong char UTF8: " 586 + Integer.toHexString(bytes.charAt(0)) + " (" 587 + i + ")", bytes.charAt(0) == i); 588 } catch (java.io.UnsupportedEncodingException e) { 589 } 590 } 591 592 byte[] bytes = new byte[1]; 593 for (int i = 0; i < 256; i++) { 594 bytes[0] = (byte) i; 595 String result = null; 596 try { 597 result = new String(bytes, "8859_1"); 598 assertEquals("Wrong char length", 1, result.length()); 599 assertTrue("Wrong char value", result.charAt(0) == (char) i); 600 } catch (java.io.UnsupportedEncodingException e) { 601 } 602 } 603 } 604 605 /** 606 * @tests java.lang.String#getBytes(int, int, byte[], int) 607 */ 608 @TestTargetNew( 609 level = TestLevel.COMPLETE, 610 notes = "", 611 method = "getBytes", 612 args = {int.class, int.class, byte[].class, int.class} 613 ) 614 @SuppressWarnings("deprecation") 615 public void test_getBytesII$BI() { 616 // Test for method void java.lang.String.getBytes(int, int, byte [], 617 // int) 618 byte[] buf = new byte[5]; 619 "Hello World".getBytes(6, 11, buf, 0); 620 assertEquals("Returned incorrect bytes", "World", new String(buf)); 621 622 try { 623 "Hello World".getBytes(-1, 1, null, 0); 624 fail("Expected IndexOutOfBoundsException"); 625 } catch (IndexOutOfBoundsException e) { 626 //expected 627 } catch (NullPointerException e) { 628 fail("Threw wrong exception"); 629 } 630 631 try { 632 "Hello World".getBytes(6, 2, null, 0); 633 fail("IndexOutOfBoundsException was not thrown."); 634 } catch (IndexOutOfBoundsException e) { 635 //expected 636 } 637 638 try { 639 "Hello World".getBytes(2, 10, new byte[10], 4); 640 fail("IndexOutOfBoundsException was not thrown."); 641 } catch (IndexOutOfBoundsException e) { 642 //expected 643 } 644 } 645 646 /** 647 * @tests java.lang.String#getBytes(java.lang.String) 648 */ 649 @TestTargetNew( 650 level = TestLevel.COMPLETE, 651 notes = "", 652 method = "getBytes", 653 args = {java.lang.String.class} 654 ) 655 public void test_getBytesLjava_lang_String() throws Exception { 656 // Test for method byte [] java.lang.String.getBytes(java.lang.String) 657 byte[] buf = "Hello World".getBytes(); 658 assertEquals("Returned incorrect bytes", "Hello World", new String(buf)); 659 660 try { 661 "string".getBytes("8849_1"); 662 fail("No UnsupportedEncodingException"); 663 } catch (UnsupportedEncodingException e) { 664 } 665 666 byte[] bytes = "\u3048".getBytes("UTF-8"); 667 byte[] expected = new byte[] {(byte)0xE3, (byte)0x81, (byte)0x88}; 668 assertEquals(expected[0], bytes[0]); 669 assertEquals(expected[1], bytes[1]); 670 assertEquals(expected[2], bytes[2]); 671 672 // Regression for HARMONY-663 673 try { 674 // BEGIN android-changed 675 // (replaced non-ASCII-printable characters with escapes) 676 "string".getBytes( 677 "?Q?D??_??_6ffa?+vG?_??\u00ef\u00bf\u00bd??\u0015"); 678 // END android-changed 679 fail("No UnsupportedEncodingException"); 680 } catch (UnsupportedEncodingException e) { 681 //expected 682 } 683 } 684 685 /** 686 * @tests java.lang.String#getChars(int, int, char[], int) 687 */ 688 @TestTargetNew( 689 level = TestLevel.COMPLETE, 690 notes = "", 691 method = "getChars", 692 args = {int.class, int.class, char[].class, int.class} 693 ) 694 public void test_getCharsII$CI() { 695 // Test for method void java.lang.String.getChars(int, int, char [], 696 // int) 697 hw1.getChars(5, hw1.length(), rbuf, 0); 698 699 for (int i = 0; i < rbuf.length; i++) 700 assertTrue("getChars returned incorrect char(s)", rbuf[i] == buf[i]); 701 702 try { 703 "Hello World".getChars(-1, 1, null, 0); 704 fail("Expected IndexOutOfBoundsException"); 705 } catch (IndexOutOfBoundsException e) { 706 //expected 707 } catch (NullPointerException e) { 708 fail("Threw wrong exception"); 709 } 710 711 try { 712 "Hello World".getChars(6, 2, null, 0); 713 fail("IndexOutOfBoundsException was not thrown."); 714 } catch (IndexOutOfBoundsException e) { 715 //expected 716 } 717 718 try { 719 "Hello World".getChars(2, 10, new char[10], 4); 720 fail("IndexOutOfBoundsException was not thrown."); 721 } catch (IndexOutOfBoundsException e) { 722 //expected 723 } 724 } 725 726 /** 727 * @tests java.lang.String#hashCode() 728 */ 729 @TestTargetNew( 730 level = TestLevel.COMPLETE, 731 notes = "", 732 method = "hashCode", 733 args = {} 734 ) 735 public void test_hashCode() { 736 // Test for method int java.lang.String.hashCode() 737 int hwHashCode = 0; 738 final int hwLength = hw1.length(); 739 int powerOfThirtyOne = 1; 740 for (int counter = hwLength - 1; counter >= 0; counter--) { 741 hwHashCode += hw1.charAt(counter) * powerOfThirtyOne; 742 powerOfThirtyOne *= 31; 743 } 744 assertTrue("String did not hash to correct value--got: " 745 + String.valueOf(hw1.hashCode()) + " but wanted: " 746 + String.valueOf(hwHashCode), hw1.hashCode() == hwHashCode); 747 assertTrue("The empty string \"\" did not hash to zero", 0 == "" 748 .hashCode()); 749 } 750 751 /** 752 * @tests java.lang.String#indexOf(int) 753 */ 754 @TestTargetNew( 755 level = TestLevel.COMPLETE, 756 notes = "", 757 method = "indexOf", 758 args = {int.class} 759 ) 760 public void test_indexOfI() { 761 // Test for method int java.lang.String.indexOf(int) 762 assertEquals("Invalid index returned", 1, hw1.indexOf('e')); 763 764 assertEquals("Doesn't return -1 if there is no such character.", -1, 765 hw1.indexOf('q')); 766 } 767 768 /** 769 * @tests java.lang.String#indexOf(int, int) 770 */ 771 @TestTargetNew( 772 level = TestLevel.COMPLETE, 773 notes = "", 774 method = "indexOf", 775 args = {int.class, int.class} 776 ) 777 public void test_indexOfII() { 778 // Test for method int java.lang.String.indexOf(int, int) 779 assertEquals("Invalid character index returned", 5, hw1.indexOf('W', 2)); 780 assertEquals("Doesn't return -1 if there is no such character.", -1, 781 hw1.indexOf('H', 2)); 782 783 } 784 785 /** 786 * @tests java.lang.String#indexOf(java.lang.String) 787 */ 788 @TestTargetNew( 789 level = TestLevel.COMPLETE, 790 notes = "", 791 method = "indexOf", 792 args = {java.lang.String.class} 793 ) 794 public void test_indexOfLjava_lang_String() { 795 // Test for method int java.lang.String.indexOf(java.lang.String) 796 assertTrue("Failed to find string", hw1.indexOf("World") > 0); 797 assertTrue("Failed to find string", !(hw1.indexOf("ZZ") > 0)); 798 assertEquals("Doesn't return -1 for unknown string.", 799 -1, hw1.indexOf("Heo")); 800 } 801 802 /** 803 * @tests java.lang.String#indexOf(java.lang.String, int) 804 */ 805 @TestTargetNew( 806 level = TestLevel.COMPLETE, 807 notes = "", 808 method = "indexOf", 809 args = {java.lang.String.class, int.class} 810 ) 811 public void test_indexOfLjava_lang_StringI() { 812 // Test for method int java.lang.String.indexOf(java.lang.String, int) 813 assertTrue("Failed to find string", hw1.indexOf("World", 0) > 0); 814 assertTrue("Found string outside index", !(hw1.indexOf("Hello", 6) > 0)); 815 assertEquals("Did not accept valid negative starting position", 0, hello1 816 .indexOf("", -5)); 817 assertEquals("Reported wrong error code", 5, hello1.indexOf("", 5)); 818 assertEquals("Wrong for empty in empty", 0, "".indexOf("", 0)); 819 } 820 821 /** 822 * @tests java.lang.String#intern() 823 */ 824 @TestTargetNew( 825 level = TestLevel.COMPLETE, 826 notes = "", 827 method = "intern", 828 args = {} 829 ) 830 public void test_intern() { 831 // Test for method java.lang.String java.lang.String.intern() 832 assertTrue("Intern returned incorrect result", hw1.intern() == hw2 833 .intern()); 834 } 835 836 /** 837 * @tests java.lang.String#lastIndexOf(int) 838 */ 839 @TestTargetNew( 840 level = TestLevel.COMPLETE, 841 notes = "", 842 method = "lastIndexOf", 843 args = {int.class} 844 ) 845 public void test_lastIndexOfI() { 846 // Test for method int java.lang.String.lastIndexOf(int) 847 assertEquals("Failed to return correct index", 5, hw1.lastIndexOf('W')); 848 assertEquals("Returned index for non-existent char", 849 -1, hw1.lastIndexOf('Z')); 850 851 } 852 853 /** 854 * @tests java.lang.String#lastIndexOf(int, int) 855 */ 856 @TestTargetNew( 857 level = TestLevel.COMPLETE, 858 notes = "", 859 method = "lastIndexOf", 860 args = {int.class, int.class} 861 ) 862 public void test_lastIndexOfII() { 863 // Test for method int java.lang.String.lastIndexOf(int, int) 864 assertEquals("Failed to return correct index", 865 5, hw1.lastIndexOf('W', 6)); 866 assertEquals("Returned index for char out of specified range", -1, hw1 867 .lastIndexOf('W', 4)); 868 assertEquals("Returned index for non-existent char", -1, hw1.lastIndexOf('Z', 869 9)); 870 871 } 872 873 /** 874 * @tests java.lang.String#lastIndexOf(java.lang.String) 875 */ 876 @TestTargetNew( 877 level = TestLevel.COMPLETE, 878 notes = "", 879 method = "lastIndexOf", 880 args = {java.lang.String.class} 881 ) 882 public void test_lastIndexOfLjava_lang_String() { 883 // Test for method int java.lang.String.lastIndexOf(java.lang.String) 884 assertEquals("Returned incorrect index", 5, hw1.lastIndexOf("World")); 885 assertEquals("Found String outside of index", -1, hw1 886 .lastIndexOf("HeKKKKKKKK")); 887 } 888 889 /** 890 * @tests java.lang.String#lastIndexOf(java.lang.String, int) 891 */ 892 @TestTargetNew( 893 level = TestLevel.COMPLETE, 894 notes = "", 895 method = "lastIndexOf", 896 args = {java.lang.String.class, int.class} 897 ) 898 public void test_lastIndexOfLjava_lang_StringI() { 899 // Test for method int java.lang.String.lastIndexOf(java.lang.String, 900 // int) 901 assertEquals("Returned incorrect index", 5, hw1.lastIndexOf("World", 9)); 902 int result = hw1.lastIndexOf("Hello", 2); 903 assertTrue("Found String outside of index: " + result, result == 0); 904 assertEquals("Reported wrong error code", 905 -1, hello1.lastIndexOf("", -5)); 906 assertEquals("Did not accept valid large starting position", 5, hello1 907 .lastIndexOf("", 5)); 908 } 909 910 /** 911 * @tests java.lang.String#length() 912 */ 913 @TestTargetNew( 914 level = TestLevel.COMPLETE, 915 notes = "", 916 method = "length", 917 args = {} 918 ) 919 public void test_length() { 920 // Test for method int java.lang.String.length() 921 assertEquals("Invalid length returned", 11, comp11.length()); 922 } 923 924 /** 925 * @tests java.lang.String#regionMatches(int, java.lang.String, int, int) 926 */ 927 @TestTargetNew( 928 level = TestLevel.COMPLETE, 929 notes = "", 930 method = "regionMatches", 931 args = {int.class, java.lang.String.class, int.class, int.class} 932 ) 933 public void test_regionMatchesILjava_lang_StringII() { 934 // Test for method boolean java.lang.String.regionMatches(int, 935 // java.lang.String, int, int) 936 String bogusString = "xxcedkedkleiorem lvvwr e''' 3r3r 23r"; 937 938 assertTrue("identical regions failed comparison", hw1.regionMatches(2, 939 hw2, 2, 5)); 940 assertTrue("Different regions returned true", !hw1.regionMatches(2, 941 bogusString, 2, 5)); 942 assertFalse("Returned true for negative offset.", hw1.regionMatches(-1, 943 hw2, 2, 5)); 944 assertFalse("Returned true for negative offset.", hw1.regionMatches(2, 945 hw2, -1, 5)); 946 assertFalse("Returned true for toffset+len is greater than the length.", 947 hw1.regionMatches(5, hw2, 2, 6)); 948 assertFalse("Returned true for ooffset+len is greater than the length.", 949 hw1.regionMatches(2, hw2, 5, 6)); 950 } 951 952 /** 953 * @tests java.lang.String#regionMatches(boolean, int, java.lang.String, 954 * int, int) 955 */ 956 @TestTargetNew( 957 level = TestLevel.COMPLETE, 958 notes = "", 959 method = "regionMatches", 960 args = {boolean.class, int.class, java.lang.String.class, int.class, int.class} 961 ) 962 public void test_regionMatchesZILjava_lang_StringII() { 963 // Test for method boolean java.lang.String.regionMatches(boolean, int, 964 // java.lang.String, int, int) 965 966 String bogusString = "xxcedkedkleiorem lvvwr e''' 3r3r 23r"; 967 968 assertTrue("identical regions failed comparison", hw1.regionMatches( 969 false, 2, hw2, 2, 5)); 970 assertTrue("identical regions failed comparison with different cases", 971 hw1.regionMatches(true, 2, hw2, 2, 5)); 972 assertTrue("Different regions returned true", !hw1.regionMatches(true, 973 2, bogusString, 2, 5)); 974 assertTrue("identical regions failed comparison with different cases", 975 hw1.regionMatches(false, 2, hw2, 2, 5)); 976 977 assertFalse("Returned true for negative offset.", hw1.regionMatches(true, 978 -1, hw2, 2, 5)); 979 assertFalse("Returned true for negative offset.", hw1.regionMatches(false, 980 2, hw2, -1, 5)); 981 assertFalse("Returned true for toffset+len is greater than the length.", 982 hw1.regionMatches(true, 5, hw2, 2, 6)); 983 assertFalse("Returned true for ooffset+len is greater than the length.", 984 hw1.regionMatches(false, 2, hw2, 5, 6)); 985 986 assertTrue("identical regions failed comparison", hwuc.regionMatches( 987 true, 0, hwlc, 0, hwuc.length())); 988 assertFalse("non identical regions failed comparison", hwuc.regionMatches( 989 false, 0, hwlc, 0, hwuc.length())); 990 } 991 992 /** 993 * @tests java.lang.String#replace(char, char) 994 */ 995 @TestTargetNew( 996 level = TestLevel.COMPLETE, 997 notes = "", 998 method = "replace", 999 args = {char.class, char.class} 1000 ) 1001 public void test_replaceCC() { 1002 // Test for method java.lang.String java.lang.String.replace(char, char) 1003 assertEquals("Failed replace", "HezzoWorzd", hw1.replace('l', 'z')); 1004 assertEquals("Returned incorrect string.", hw1, hw1.replace("!", ".")); 1005 } 1006 1007 @TestTargetNew( 1008 level = TestLevel.COMPLETE, 1009 notes = "", 1010 method = "replaceAll", 1011 args = {java.lang.String.class, java.lang.String.class} 1012 ) 1013 public void test_replaceAll() { 1014 String str = "!'123123.123HelloWorld!123123helloworld#"; 1015 String [] patterns = {"[hw\\p{Upper}]", "(o|l){2,}", "([\'\"]?)(\\d+)", 1016 "^!.*#$"}; 1017 1018 String [] results = {"!\'123123.123?ello?orld!123123?ello?orld#", 1019 "!\'123123.123He?World!123123he?world#", 1020 "!?.?HelloWorld!?helloworld#", "?"}; 1021 1022 for(int i = 0; i < patterns.length; i++) { 1023 assertEquals("Returned incorrect string", 1024 results[i], str.replaceAll(patterns[i], "?")); 1025 } 1026 1027 try { 1028 str.replaceAll("[abc*", "?"); 1029 fail("PatternSyntaxException is not thrown."); 1030 } catch(PatternSyntaxException pse) { 1031 //expected 1032 } 1033 } 1034 1035 @TestTargetNew( 1036 level = TestLevel.COMPLETE, 1037 notes = "", 1038 method = "replaceFirst", 1039 args = {java.lang.String.class, java.lang.String.class} 1040 ) 1041 public void test_replaceFirst() { 1042 String str = "!'123123.123HelloWorld!123123helloworld#"; 1043 String [] patterns = {"[hw\\p{Upper}]", "(o|l){2,}", "([\'\"]?)(\\d+)", 1044 "^!.*#$"}; 1045 1046 String [] results = {"!'123123.123?elloWorld!123123helloworld#", 1047 "!'123123.123He?World!123123helloworld#", 1048 "!?.123HelloWorld!123123helloworld#", "?"}; 1049 1050 for(int i = 0; i < patterns.length; i++) { 1051 assertEquals("Returned incorrect string", 1052 results[i], str.replaceFirst(patterns[i], "?")); 1053 } 1054 1055 try { 1056 str.replaceFirst("[abc*", "?"); 1057 fail("PatternSyntaxException is not thrown."); 1058 } catch(PatternSyntaxException pse) { 1059 //expected 1060 } 1061 } 1062 1063 @TestTargetNew( 1064 level = TestLevel.COMPLETE, 1065 notes = "", 1066 method = "split", 1067 args = {java.lang.String.class} 1068 ) 1069 public void test_splitLString() { 1070 String str = "!'123123.123HelloWorld!123123helloworld#"; 1071 String [] patterns = {"[!.1]", "(\\d+).*e(l+)o.*orld"}; 1072 String [][] results = {{"", "'","23", "23", "", "23HelloWorld", "", "23", 1073 "23helloworld#"}, 1074 {"!'", "#"}}; 1075 1076 for(int i = 0; i < patterns.length; i++) { 1077 assertTrue("Returned incorrect string array for pattern: " + 1078 patterns[i], Arrays.equals(results[i], str.split(patterns[i]))); 1079 } 1080 1081 try { 1082 str.split("[a}"); 1083 fail("PatternSyntaxException is not thrown."); 1084 } catch(PatternSyntaxException pse) { 1085 //expected 1086 } 1087 } 1088 1089 @TestTargetNew( 1090 level = TestLevel.COMPLETE, 1091 notes = "", 1092 method = "split", 1093 args = {java.lang.String.class, int.class} 1094 ) 1095 public void test_splitLStringLint() { 1096 String str = "!'123123.123HelloWorld!123123helloworld#"; 1097 String pattern = "[!.1]"; 1098 String [][] results = {{"", "'","23", "23.123HelloWorld!123123helloworld#"}, 1099 {"", "'","23", "23", "", "23HelloWorld", "", "23", 1100 "23helloworld#"}}; 1101 1102 assertTrue("Returned incorrect string array for limit 4", 1103 Arrays.equals(results[0], str.split(pattern, 4))); 1104 assertTrue("Returned incorrect string array for limit 9", 1105 Arrays.equals(results[1], str.split(pattern, 9))); 1106 assertTrue("Returned incorrect string array for limit 0", 1107 Arrays.equals(results[1], str.split(pattern, 0))); 1108 assertTrue("Returned incorrect string array for limit -1", 1109 Arrays.equals(results[1], str.split(pattern, -1))); 1110 assertTrue("Returned incorrect string array for limit 10", 1111 Arrays.equals(results[1], str.split(pattern, 10))); 1112 assertTrue("Returned incorrect string array for limit Integer.MAX_VALUE", 1113 Arrays.equals(results[1], str.split(pattern, Integer.MAX_VALUE))); 1114 1115 try { 1116 str.split("[a}", 0); 1117 fail("PatternSyntaxException is not thrown."); 1118 } catch(PatternSyntaxException pse) { 1119 //expected 1120 } 1121 } 1122 1123 1124 1125 /** 1126 * @tests java.lang.String#replace(CharSequence, CharSequence) 1127 */ 1128 @TestTargetNew( 1129 level = TestLevel.COMPLETE, 1130 notes = "", 1131 method = "replace", 1132 args = {java.lang.CharSequence.class, java.lang.CharSequence.class} 1133 ) 1134 public void test_replaceLjava_langCharSequenceLjava_langCharSequence() { 1135 assertEquals("Failed replace", "aaccdd", "aabbdd".replace( 1136 new StringBuffer("bb"), "cc")); 1137 assertEquals("Failed replace by bigger seq", "cccbccc", "aba".replace( 1138 "a", "ccc")); 1139 assertEquals("Failed replace by smaller seq", "$bba^", 1140 "$aaaaa^".replace(new StringBuilder("aa"), "b")); 1141 1142 try { 1143 "".replace((CharSequence) null, "123".subSequence(0, 1)); 1144 fail("NullPointerException is not thrown."); 1145 } catch(NullPointerException npe) { 1146 //expected 1147 } 1148 1149 try { 1150 "".replace("123".subSequence(0, 1), (CharSequence) null); 1151 fail("NullPointerException is not thrown."); 1152 } catch(NullPointerException npe) { 1153 //expected 1154 } 1155 } 1156 1157 /** 1158 * @tests java.lang.String#startsWith(java.lang.String) 1159 */ 1160 @TestTargetNew( 1161 level = TestLevel.COMPLETE, 1162 notes = "", 1163 method = "startsWith", 1164 args = {java.lang.String.class} 1165 ) 1166 public void test_startsWithLjava_lang_String() { 1167 // Test for method boolean java.lang.String.startsWith(java.lang.String) 1168 assertTrue("Failed to find string", hw1.startsWith("Hello")); 1169 assertTrue("Found incorrect string", !hw1.startsWith("T")); 1170 } 1171 1172 /** 1173 * @tests java.lang.String#startsWith(java.lang.String, int) 1174 */ 1175 @TestTargetNew( 1176 level = TestLevel.COMPLETE, 1177 notes = "", 1178 method = "startsWith", 1179 args = {java.lang.String.class, int.class} 1180 ) 1181 public void test_startsWithLjava_lang_StringI() { 1182 // Test for method boolean java.lang.String.startsWith(java.lang.String, 1183 // int) 1184 assertTrue("Failed to find string", hw1.startsWith("World", 5)); 1185 assertTrue("Found incorrect string", !hw1.startsWith("Hello", 5)); 1186 } 1187 1188 /** 1189 * @tests java.lang.String#substring(int) 1190 */ 1191 @TestTargetNew( 1192 level = TestLevel.COMPLETE, 1193 notes = "", 1194 method = "substring", 1195 args = {int.class} 1196 ) 1197 public void test_substringI() { 1198 // Test for method java.lang.String java.lang.String.substring(int) 1199 assertEquals("Incorrect substring returned", 1200 "World", hw1.substring(5)); 1201 assertTrue("not identical", hw1.substring(0) == hw1); 1202 1203 try { 1204 hw1.substring(-1); 1205 fail("IndexOutOfBoundsException was not thrown."); 1206 } catch(IndexOutOfBoundsException ioobe) { 1207 //expected 1208 } 1209 1210 try { 1211 hw1.substring(hw1.length() + 1); 1212 fail("IndexOutOfBoundsException was not thrown."); 1213 } catch(IndexOutOfBoundsException ioobe) { 1214 //expected 1215 } 1216 1217 try { 1218 hw1.substring(Integer.MAX_VALUE); 1219 fail("IndexOutOfBoundsException was not thrown."); 1220 } catch(IndexOutOfBoundsException ioobe) { 1221 //expected 1222 } 1223 } 1224 1225 /** 1226 * @tests java.lang.String#substring(int, int) 1227 */ 1228 @TestTargetNew( 1229 level = TestLevel.COMPLETE, 1230 notes = "", 1231 method = "substring", 1232 args = {int.class, int.class} 1233 ) 1234 public void test_substringII() { 1235 // Test for method java.lang.String java.lang.String.substring(int, int) 1236 assertTrue("Incorrect substring returned", hw1.substring(0, 5).equals( 1237 "Hello") 1238 && (hw1.substring(5, 10).equals("World"))); 1239 assertTrue("not identical", hw1.substring(0, hw1.length()) == hw1); 1240 1241 try { 1242 hw1.substring(-1, hw1.length()); 1243 fail("IndexOutOfBoundsException was not thrown."); 1244 } catch(IndexOutOfBoundsException ioobe) { 1245 //expected 1246 } 1247 1248 try { 1249 hw1.substring(Integer.MAX_VALUE, hw1.length()); 1250 fail("IndexOutOfBoundsException was not thrown."); 1251 } catch(IndexOutOfBoundsException ioobe) { 1252 //expected 1253 } 1254 1255 try { 1256 hw1.substring(0, Integer.MAX_VALUE); 1257 fail("IndexOutOfBoundsException was not thrown."); 1258 } catch(IndexOutOfBoundsException ioobe) { 1259 //expected 1260 } 1261 } 1262 1263 1264 @TestTargetNew( 1265 level = TestLevel.COMPLETE, 1266 notes = "", 1267 method = "subSequence", 1268 args = {int.class, int.class} 1269 ) 1270 public void test_subSequence() { 1271 // Test for method java.lang.String java.lang.String.substring(int, int) 1272 assertTrue("Incorrect substring returned", hw1.subSequence(0, 5).equals( 1273 "Hello") && (hw1.subSequence(5, 10).equals("World"))); 1274 assertTrue("not identical", hw1.subSequence(0, hw1.length()) == hw1); 1275 1276 try { 1277 hw1.subSequence(0, Integer.MAX_VALUE); 1278 fail("IndexOutOfBoundsException was not thrown."); 1279 } catch(IndexOutOfBoundsException ioobe) { 1280 //expected 1281 } 1282 1283 try { 1284 hw1.subSequence(Integer.MAX_VALUE, hw1.length()); 1285 fail("IndexOutOfBoundsException was not thrown."); 1286 } catch(IndexOutOfBoundsException ioobe) { 1287 //expected 1288 } 1289 1290 try { 1291 hw1.subSequence(-1, hw1.length()); 1292 fail("IndexOutOfBoundsException was not thrown."); 1293 } catch(IndexOutOfBoundsException ioobe) { 1294 //expected 1295 } 1296 } 1297 1298 /** 1299 * @tests java.lang.String#toCharArray() 1300 */ 1301 @TestTargetNew( 1302 level = TestLevel.COMPLETE, 1303 notes = "", 1304 method = "toCharArray", 1305 args = {} 1306 ) 1307 public void test_toCharArray() { 1308 // Test for method char [] java.lang.String.toCharArray() 1309 1310 String s = new String(buf, 0, buf.length); 1311 char[] schars = s.toCharArray(); 1312 for (int i = 0; i < s.length(); i++) 1313 assertTrue("Returned incorrect char aray", buf[i] == schars[i]); 1314 } 1315 1316 /** 1317 * @tests java.lang.String#toLowerCase() 1318 */ 1319 @TestTargetNew( 1320 level = TestLevel.COMPLETE, 1321 notes = "", 1322 method = "toLowerCase", 1323 args = {} 1324 ) 1325 public void test_toLowerCase() { 1326 // Test for method java.lang.String java.lang.String.toLowerCase() 1327 assertTrue("toLowerCase case conversion did not succeed", hwuc 1328 .toLowerCase().equals(hwlc)); 1329 1330 assertEquals("a) Sigma has same lower case value at end of word with Unicode 3.0", 1331 "\u03c3", "\u03a3".toLowerCase()); 1332 assertEquals("b) Sigma has same lower case value at end of word with Unicode 3.0", 1333 "a \u03c3", "a \u03a3".toLowerCase()); 1334 } 1335 1336 /** 1337 * @tests java.lang.String#toLowerCase(java.util.Locale) 1338 */ 1339 @TestTargetNew( 1340 level = TestLevel.COMPLETE, 1341 notes = "", 1342 method = "toLowerCase", 1343 args = {java.util.Locale.class} 1344 ) 1345 public void test_toLowerCaseLjava_util_Locale() { 1346 // Test for method java.lang.String 1347 // java.lang.String.toLowerCase(java.util.Locale) 1348 assertTrue("toLowerCase case conversion did not succeed", hwuc 1349 .toLowerCase(java.util.Locale.getDefault()).equals(hwlc)); 1350 assertEquals("Invalid \\u0049 for English", "\u0069", "\u0049".toLowerCase( 1351 Locale.ENGLISH)); 1352 assertEquals("Invalid \\u0049 for Turkish", "\u0131", "\u0049".toLowerCase( 1353 new Locale("tr", ""))); 1354 } 1355 1356 /** 1357 * @tests java.lang.String#toString() 1358 */ 1359 @TestTargetNew( 1360 level = TestLevel.COMPLETE, 1361 notes = "", 1362 method = "toString", 1363 args = {} 1364 ) 1365 public void test_toString() { 1366 // Test for method java.lang.String java.lang.String.toString() 1367 assertTrue("Incorrect string returned", hw1.toString().equals(hw1)); 1368 } 1369 1370 /** 1371 * @tests java.lang.String#toUpperCase() 1372 */ 1373 @TestTargetNew( 1374 level = TestLevel.COMPLETE, 1375 notes = "", 1376 method = "toUpperCase", 1377 args = {} 1378 ) 1379 public void test_toUpperCase() { 1380 // Test for method java.lang.String java.lang.String.toUpperCase() 1381 assertTrue("Returned string is not UpperCase", hwlc.toUpperCase() 1382 .equals(hwuc)); 1383 1384 assertEquals("Wrong conversion", "SS", "\u00df".toUpperCase()); 1385 1386 String s = "a\u00df\u1f56"; 1387 assertTrue("Invalid conversion", !s.toUpperCase().equals(s)); 1388 1389 } 1390 1391 /** 1392 * @tests java.lang.String#toUpperCase(java.util.Locale) 1393 */ 1394 @TestTargetNew( 1395 level = TestLevel.COMPLETE, 1396 notes = "", 1397 method = "toUpperCase", 1398 args = {java.util.Locale.class} 1399 ) 1400 public void test_toUpperCaseLjava_util_Locale() { 1401 // Test for method java.lang.String 1402 // java.lang.String.toUpperCase(java.util.Locale) 1403 assertTrue("Returned string is not UpperCase", hwlc.toUpperCase() 1404 .equals(hwuc)); 1405 assertEquals("Invalid \\u0069 for English", "\u0049", "\u0069".toUpperCase( 1406 Locale.ENGLISH)); 1407 assertEquals("Invalid \\u0069 for Turkish", "\u0130", "\u0069".toUpperCase( 1408 new Locale("tr", ""))); 1409 } 1410 1411 /** 1412 * @tests java.lang.String#trim() 1413 */ 1414 @TestTargetNew( 1415 level = TestLevel.COMPLETE, 1416 notes = "", 1417 method = "trim", 1418 args = {} 1419 ) 1420 public void test_trim() { 1421 // Test for method java.lang.String java.lang.String.trim() 1422 assertTrue("Incorrect string returned", " HelloWorld ".trim().equals( 1423 hw1)); 1424 assertEquals("Incorrect string returned", hw1, " HelloWorld ".trim()); 1425 assertTrue("Incorrect string returned", " ".trim().equals("")); 1426 } 1427 1428 /** 1429 * @tests java.lang.String#valueOf(char[]) 1430 */ 1431 @TestTargetNew( 1432 level = TestLevel.COMPLETE, 1433 notes = "", 1434 method = "valueOf", 1435 args = {char[].class} 1436 ) 1437 public void test_valueOf$C() { 1438 // Test for method java.lang.String java.lang.String.valueOf(char []) 1439 assertEquals("Returned incorrect String", 1440 "World", String.valueOf(buf)); 1441 assertEquals("Returned incorrect String", 1442 "", String.valueOf(new char[]{})); 1443 try { 1444 String.valueOf(null); 1445 fail("NullPointerException was not thrown."); 1446 } catch(NullPointerException npe) { 1447 //expected 1448 } 1449 } 1450 1451 /** 1452 * @tests java.lang.String#valueOf(char[], int, int) 1453 */ 1454 @TestTargetNew( 1455 level = TestLevel.COMPLETE, 1456 notes = "", 1457 method = "valueOf", 1458 args = {char[].class, int.class, int.class} 1459 ) 1460 public void test_valueOf$CII() { 1461 // Test for method java.lang.String java.lang.String.valueOf(char [], 1462 // int, int) 1463 char[] t = { 'H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd' }; 1464 assertEquals("copyValueOf returned incorrect String", "World", String.valueOf(t, 1465 5, 5)); 1466 1467 try { 1468 String.valueOf(t, 0, t.length + 1); 1469 fail("IndexOutOfBoundsException was not thrown."); 1470 } catch(IndexOutOfBoundsException ioobe) { 1471 //expected 1472 } 1473 1474 try { 1475 String.valueOf(t, 0, -1); 1476 fail("IndexOutOfBoundsException was not thrown."); 1477 } catch(IndexOutOfBoundsException ioobe) { 1478 //expected 1479 } 1480 1481 try { 1482 String.valueOf(t, 0, Integer.MAX_VALUE); 1483 fail("IndexOutOfBoundsException was not thrown."); 1484 } catch(IndexOutOfBoundsException ioobe) { 1485 //expected 1486 } 1487 } 1488 1489 /** 1490 * @tests java.lang.String#valueOf(char) 1491 */ 1492 @TestTargetNew( 1493 level = TestLevel.COMPLETE, 1494 notes = "", 1495 method = "valueOf", 1496 args = {char.class} 1497 ) 1498 public void test_valueOfC() { 1499 // Test for method java.lang.String java.lang.String.valueOf(char) 1500 for (int i = 0; i < 65536; i++) 1501 assertTrue("Incorrect valueOf(char) returned: " + i, String 1502 .valueOf((char) i).charAt(0) == (char) i); 1503 } 1504 1505 /** 1506 * @tests java.lang.String#valueOf(double) 1507 */ 1508 @TestTargetNew( 1509 level = TestLevel.COMPLETE, 1510 notes = "", 1511 method = "valueOf", 1512 args = {double.class} 1513 ) 1514 public void test_valueOfD() { 1515 // Test for method java.lang.String java.lang.String.valueOf(double) 1516 assertEquals("Incorrect double string returned", "1.7976931348623157E308", String.valueOf( 1517 Double.MAX_VALUE)); 1518 } 1519 1520 /** 1521 * @tests java.lang.String#valueOf(float) 1522 */ 1523 @TestTargetNew( 1524 level = TestLevel.COMPLETE, 1525 notes = "", 1526 method = "valueOf", 1527 args = {float.class} 1528 ) 1529 public void test_valueOfF() { 1530 // Test for method java.lang.String java.lang.String.valueOf(float) 1531 assertTrue("incorrect float string returned--got: " 1532 + String.valueOf(1.0F) + " wanted: 1.0", String.valueOf(1.0F) 1533 .equals("1.0")); 1534 assertTrue("incorrect float string returned--got: " 1535 + String.valueOf(0.9F) + " wanted: 0.9", String.valueOf(0.9F) 1536 .equals("0.9")); 1537 assertTrue("incorrect float string returned--got: " 1538 + String.valueOf(109.567F) + " wanted: 109.567", String 1539 .valueOf(109.567F).equals("109.567")); 1540 } 1541 1542 /** 1543 * @tests java.lang.String#valueOf(int) 1544 */ 1545 @TestTargetNew( 1546 level = TestLevel.COMPLETE, 1547 notes = "", 1548 method = "valueOf", 1549 args = {int.class} 1550 ) 1551 public void test_valueOfI() { 1552 // Test for method java.lang.String java.lang.String.valueOf(int) 1553 assertEquals("returned invalid int string", "1", String.valueOf(1)); 1554 } 1555 1556 /** 1557 * @tests java.lang.String#valueOf(long) 1558 */ 1559 @TestTargetNew( 1560 level = TestLevel.COMPLETE, 1561 notes = "", 1562 method = "valueOf", 1563 args = {long.class} 1564 ) 1565 public void test_valueOfJ() { 1566 // Test for method java.lang.String java.lang.String.valueOf(long) 1567 assertEquals("returned incorrect long string", "927654321098", String.valueOf( 1568 927654321098L)); 1569 } 1570 1571 /** 1572 * @tests java.lang.String#valueOf(java.lang.Object) 1573 */ 1574 @TestTargetNew( 1575 level = TestLevel.COMPLETE, 1576 notes = "", 1577 method = "valueOf", 1578 args = {java.lang.Object.class} 1579 ) 1580 public void test_valueOfLjava_lang_Object() { 1581 // Test for method java.lang.String 1582 // java.lang.String.valueOf(java.lang.Object) 1583 assertTrue("Incorrect Object string returned", obj.toString().equals( 1584 String.valueOf(obj))); 1585 assertEquals("Incorrect value was returned for null.", 1586 "null", String.valueOf((Object) null)); 1587 } 1588 1589 /** 1590 * @tests java.lang.String#valueOf(boolean) 1591 */ 1592 @TestTargetNew( 1593 level = TestLevel.COMPLETE, 1594 notes = "", 1595 method = "valueOf", 1596 args = {boolean.class} 1597 ) 1598 public void test_valueOfZ() { 1599 // Test for method java.lang.String java.lang.String.valueOf(boolean) 1600 assertTrue("Incorrect boolean string returned", String.valueOf(false) 1601 .equals("false") 1602 && (String.valueOf(true).equals("true"))); 1603 } 1604 1605 /** 1606 * @tests java.lang.String#contentEquals(CharSequence cs) 1607 */ 1608 @TestTargetNew( 1609 level = TestLevel.PARTIAL, 1610 notes = "NullPointerException is not verified.", 1611 method = "contentEquals", 1612 args = {java.lang.CharSequence.class} 1613 ) 1614 public void test_contentEqualsLjava_lang_CharSequence() { 1615 // Test for method java.lang.String java.lang.String.contentEquals(CharSequence cs) 1616 assertFalse("Incorrect result of compare", "qwerty".contentEquals("")); 1617 } 1618 1619 /** 1620 * @tests java.lang.String#format(Locale, String, Object[]) 1621 */ 1622 @TestTargetNew( 1623 level = TestLevel.COMPLETE, 1624 notes = "", 1625 method = "format", 1626 args = {java.lang.String.class, java.lang.Object[].class} 1627 ) 1628 @SuppressWarnings("boxing") 1629 public void test_format() { 1630 assertEquals("13% of sum is 0x11", 1631 String.format("%d%% of %s is 0x%x", 13, "sum", 17)); 1632 1633 assertEquals("3 2 1 4 3 2 1", String.format( 1634 "%3$d %2$d %1$d %4$d %3$d %2$d %1$d", 1, 2, 3, 4)); 1635 1636 assertEquals("empty format", "", String.format("", 123, this)); 1637 try { 1638 String.format(null); 1639 fail("NPE is expected on null format"); 1640 } catch (NullPointerException ok){} 1641 1642 try { 1643 String.format("%d%% of %s is 0x%x", "123"); 1644 fail("IllegalFormatException was not thrown."); 1645 } catch(IllegalFormatException ife) { 1646 //expected 1647 } 1648 1649 try { 1650 String.format("%tu", "123"); 1651 fail("IllegalFormatException was not thrown."); 1652 } catch(IllegalFormatException ife) { 1653 //expected 1654 } 1655 1656 } 1657 1658 /** 1659 * @tests java.lang.String#format(Locale, String, Object...) 1660 */ 1661 @TestTargetNew( 1662 level = TestLevel.COMPLETE, 1663 notes = "", 1664 method = "format", 1665 args = {java.util.Locale.class, java.lang.String.class, java.lang.Object[].class} 1666 ) 1667 @SuppressWarnings("boxing") 1668 public void test_format_Locale() { 1669 Locale l = new Locale("UK"); 1670 assertEquals("13% of sum is 0x11", 1671 String.format(l, "%d%% of %s is 0x%x", 13, "sum", 17)); 1672 assertEquals("empty format", "", String.format("", 123, this)); 1673 try { 1674 String.format(l, null, ""); 1675 fail("NPE is expected on null format"); 1676 } catch (NullPointerException ok){} 1677 1678 try { 1679 String.format(l, "%d", "test"); 1680 fail("IllegalFormatException wasn't thrown."); 1681 } catch(IllegalFormatException ife) { 1682 //expected 1683 } 1684 } 1685 1686 @TestTargetNew( 1687 level = TestLevel.COMPLETE, 1688 notes = "", 1689 method = "matches", 1690 args = {java.lang.String.class} 1691 ) 1692 public void test_matches() { 1693 String[] patterns = { 1694 "(a|b)*abb", 1695 "(1*2*3*4*)*567", 1696 "(a|b|c|d)*aab", 1697 "(1|2|3|4|5|6|7|8|9|0)(1|2|3|4|5|6|7|8|9|0)*", 1698 "(abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ)*", 1699 "(a|b)*(a|b)*A(a|b)*lice.*", 1700 "(a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z)(a|b|c|d|e|f|g|h|" 1701 + "i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z)*(1|2|3|4|5|6|7|8|9|0)*|while|for|struct|if|do", 1702 1703 }; 1704 1705 String[] patternsInc = { 1706 "(ac)*bb", 1707 "(1)*567", 1708 "(c)*ab", 1709 "(|8|9|0)(1|2|7|8|9|0)*", 1710 "(z)", 1711 "(a)*A(b)*lice.", 1712 "(a|b|c|d|e)", 1713 1714 }; 1715 1716 String[][] strings = { 1717 { "abb", "ababb", "abababbababb", "abababbababbabababbbbbabb" }, 1718 { "213567", "12324567", "1234567", "213213567", 1719 "21312312312567", "444444567" }, 1720 { "abcdaab", "aab", "abaab", "cdaab", "acbdadcbaab" }, 1721 { "213234567", "3458", "0987654", "7689546432", "0398576", 1722 "98432", "5" }, 1723 { 1724 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", 1725 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" 1726 + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" }, 1727 { "ababbaAabababblice", "ababbaAliceababab", "ababbAabliceaaa", 1728 "abbbAbbbliceaaa", "Alice" }, 1729 { "a123", "bnxnvgds156", "for", "while", "if", "struct" }, 1730 { "xy" }, { "xy" }, { "xcy" } 1731 1732 }; 1733 1734 for (int i = 0; i < patterns.length; i++) { 1735 for (int j = 0; j < strings[i].length; j++) { 1736 assertTrue("Incorrect match: " + patterns[i] + " vs " 1737 + strings[i][j], strings[i][j].matches(patterns[i])); 1738 assertFalse("" + i, strings[i][j].matches(patternsInc[i])); 1739 } 1740 } 1741 } 1742 1743} 1744