ScannerTest.java revision 743fc438ecba5ee39e44e4e8b36dfbe9381340bd
1/* Licensed to the Apache Software Foundation (ASF) under one or more 2 * contributor license agreements. See the NOTICE file distributed with 3 * this work for additional information regarding copyright ownership. 4 * The ASF licenses this file to You under the Apache License, Version 2.0 5 * (the "License"); you may not use this file except in compliance with 6 * the License. You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16package tests.api.java.util; 17 18import dalvik.annotation.TestTargetNew; 19import dalvik.annotation.TestTargets; 20import dalvik.annotation.TestLevel; 21import dalvik.annotation.TestTargetClass; 22import dalvik.annotation.KnownFailure; 23 24import java.io.Closeable; 25import java.io.EOFException; 26import java.io.File; 27import java.io.FileNotFoundException; 28import java.io.FileOutputStream; 29import java.io.IOException; 30import java.io.InputStream; 31import java.io.OutputStream; 32import java.io.PipedInputStream; 33import java.io.StringReader; 34import java.math.BigDecimal; 35import java.math.BigInteger; 36import java.net.InetSocketAddress; 37import java.net.ServerSocket; 38import java.net.Socket; 39import java.net.SocketAddress; 40import java.nio.CharBuffer; 41import java.nio.channels.FileChannel; 42import java.nio.channels.ReadableByteChannel; 43import java.nio.channels.ServerSocketChannel; 44import java.nio.channels.SocketChannel; 45import java.nio.charset.Charset; 46import java.util.Arrays; 47import java.util.InputMismatchException; 48import java.util.Locale; 49import java.util.NoSuchElementException; 50import java.util.Scanner; 51import java.util.regex.MatchResult; 52import java.util.regex.Pattern; 53 54import tests.support.Support_PortManager; 55 56import junit.framework.TestCase; 57 58@TestTargetClass(Scanner.class) 59public class ScannerTest extends TestCase { 60 static final boolean disableRIBugs = true; 61 62 private Scanner s; 63 64 private ServerSocket server; 65 66 private SocketAddress address; 67 68 private SocketChannel client; 69 70 private Socket serverSocket; 71 72 private OutputStream os; 73 74 private static class MockCloseable implements Closeable, Readable { 75 76 public void close() throws IOException { 77 throw new IOException(); 78 } 79 80 public int read(CharBuffer cb) throws IOException { 81 throw new EOFException(); 82 } 83 84 } 85 86 /** 87 * @tests java.util.Scanner#Scanner(File) 88 */ 89 @TestTargetNew( 90 level = TestLevel.COMPLETE, 91 notes = "", 92 method = "Scanner", 93 args = {java.io.File.class} 94 ) 95 public void test_ConstructorLjava_io_File() throws IOException { 96 File tmpFile = File.createTempFile("TestFileForScanner", ".tmp"); 97 s = new Scanner(tmpFile); 98 assertNotNull(s); 99 s.close(); 100 assertTrue(tmpFile.delete()); 101 102 try { 103 s = new Scanner(tmpFile); 104 fail("should throw FileNotFoundException"); 105 } catch (FileNotFoundException e) { 106 // expected 107 } 108 109 tmpFile = File.createTempFile("TestFileForScanner", ".tmp"); 110 FileOutputStream fos = new FileOutputStream(tmpFile); 111 fos.write("test".getBytes()); 112 113 s = new Scanner(tmpFile); 114 tmpFile.delete(); 115 116 // Scanner(File = null) 117 try { 118 s = new Scanner((File) null); 119 fail("Should throw NullPointerException"); 120 } catch (NullPointerException e) { 121 // expected 122 } 123 124 // TODO: test if the default charset is used. 125 } 126 127 /** 128 * @tests java.util.Scanner#Scanner(File, String) 129 */ 130 @TestTargetNew( 131 level = TestLevel.COMPLETE, 132 notes = "", 133 method = "Scanner", 134 args = {java.io.File.class, java.lang.String.class} 135 ) 136 public void test_ConstructorLjava_io_FileLjava_lang_String() 137 throws IOException { 138 File tmpFile = File.createTempFile("TestFileForScanner", ".tmp"); 139 s = new Scanner(tmpFile, Charset.defaultCharset().name()); 140 assertNotNull(s); 141 s.close(); 142 assertTrue(tmpFile.delete()); 143 144 try { 145 s = new Scanner(tmpFile, Charset.defaultCharset().name()); 146 fail("should throw FileNotFoundException"); 147 } catch (FileNotFoundException e) { 148 // expected 149 } 150 151 try { 152 s = new Scanner(tmpFile, null); 153 fail("should throw FileNotFoundException"); 154 } catch (FileNotFoundException e) { 155 // expected 156 } 157 158 tmpFile = File.createTempFile("TestFileForScanner", ".tmp"); 159 try { 160 s = new Scanner(tmpFile, "invalid charset"); 161 fail("should throw IllegalArgumentException"); 162 } catch (IllegalArgumentException e) { 163 // expected 164 } 165 166 //fail on RI. File is opened but not closed when exception is thrown on 167 // RI. 168 assertTrue(tmpFile.delete()); 169 170 // Scanner(File = null, Charset = null) 171 try { 172 s = new Scanner((File) null, null); 173 fail("Should throw NullPointerException"); 174 } catch (NullPointerException e) { 175 // expected 176 } 177 178 // Scanner(File = null, Charset = UTF-8) 179 try { 180 s = new Scanner((File) null, "UTF-8"); 181 fail("Should throw NullPointerException"); 182 } catch (NullPointerException e) { 183 // expected 184 } 185 186 // Scanner(File = null, Charset = invalid) 187 try { 188 s = new Scanner((File) null, "invalid"); 189 fail("Should throw NullPointerException"); 190 } catch (NullPointerException e) { 191 // expected 192 } 193 194 // Scanner(File, Charset = null) 195 try { 196 File f = File.createTempFile("test", ".tmp"); 197 s = new Scanner(f, null); 198 fail("Should throw IllegalArgumentException"); 199 } catch (IllegalArgumentException e) { 200 // expected 201 } 202 203 // TODO: test if the specified charset is used. 204 } 205 206 /** 207 * @tests java.util.Scanner#Scanner(InputStream) 208 */ 209 @TestTargetNew( 210 level = TestLevel.COMPLETE, 211 notes = "", 212 method = "Scanner", 213 args = {java.io.InputStream.class} 214 ) 215 public void test_ConstructorLjava_io_InputStream() { 216 s = new Scanner(new PipedInputStream()); 217 assertNotNull(s); 218 s.close(); 219 220 // Scanner(InputStream) 221 try { 222 s = new Scanner((InputStream) null); 223 fail("Should throw NullPointerException"); 224 } catch (NullPointerException e) { 225 // expected 226 } 227 228 // TODO: test if the default charset is used. 229 } 230 231 /** 232 * @tests java.util.Scanner#Scanner(InputStream, String) 233 */ 234 @TestTargetNew( 235 level = TestLevel.COMPLETE, 236 notes = "", 237 method = "Scanner", 238 args = {java.io.InputStream.class, java.lang.String.class} 239 ) 240 public void test_ConstructorLjava_io_InputStreamLjava_lang_String() { 241 s = new Scanner(new PipedInputStream(), Charset.defaultCharset().name()); 242 assertNotNull(s); 243 s.close(); 244 245 try { 246 s = new Scanner((PipedInputStream) null, "invalid charset"); 247 fail("should throw NullPointerException"); 248 } catch (NullPointerException e) { 249 // expected 250 } 251 252 try { 253 s = new Scanner(new PipedInputStream(), null); 254 fail("should throw NullPointerException"); 255 } catch (NullPointerException e) { 256 // expected 257 } 258 259 try { 260 s = new Scanner(new PipedInputStream(), "invalid charset"); 261 fail("should throw IllegalArgumentException"); 262 } catch (IllegalArgumentException e) { 263 // expected 264 } 265 266 // TODO: test if the specified charset is used. 267 } 268 269 /** 270 * @tests java.util.Scanner#Scanner(Readable) 271 */ 272 @TestTargetNew( 273 level = TestLevel.COMPLETE, 274 notes = "", 275 method = "Scanner", 276 args = {java.lang.Readable.class} 277 ) 278 public void test_ConstructorLjava_lang_Readable() { 279 s = new Scanner(new StringReader("test string")); 280 assertNotNull(s); 281 s.close(); 282 283 // Scanner(Readable) 284 try { 285 s = new Scanner((Readable) null); 286 fail("Should throw NullPointerException"); 287 } catch (NullPointerException e) { 288 // expected 289 } 290 } 291 292 /** 293 * @tests java.util.Scanner#Scanner(ReadableByteChannel) 294 */ 295 @TestTargetNew( 296 level = TestLevel.COMPLETE, 297 notes = "", 298 method = "Scanner", 299 args = {java.nio.channels.ReadableByteChannel.class} 300 ) 301 public void test_ConstructorLjava_nio_channels_ReadableByteChannel() 302 throws IOException { 303 File tmpFile = File.createTempFile("TestFileForScanner", ".tmp"); 304 FileChannel fc = new FileOutputStream(tmpFile).getChannel(); 305 s = new Scanner(fc); 306 assertNotNull(s); 307 s.close(); 308 assertTrue(tmpFile.delete()); 309 310 // Scanner(ReadableByteChannel) 311 try { 312 s = new Scanner((ReadableByteChannel) null); 313 fail("Should throw NullPointerException"); 314 } catch (NullPointerException e) { 315 // expected 316 } 317 318 // TODO: test if the default charset is used. 319 } 320 321 /** 322 * @tests java.util.Scanner#Scanner(ReadableByteChannel, String) 323 */ 324 @TestTargetNew( 325 level = TestLevel.COMPLETE, 326 notes = "", 327 method = "Scanner", 328 args = {java.nio.channels.ReadableByteChannel.class, java.lang.String.class} 329 ) 330 public void test_ConstructorLjava_nio_channels_ReadableByteChannelLjava_lang_String() 331 throws IOException { 332 File tmpFile = File.createTempFile("TestFileForScanner", ".tmp"); 333 FileChannel fc = new FileOutputStream(tmpFile).getChannel(); 334 s = new Scanner(fc, Charset.defaultCharset().name()); 335 assertNotNull(s); 336 s.close(); 337 338 fc = new FileOutputStream(tmpFile).getChannel(); 339 try { 340 s = new Scanner(fc, "invalid charset"); 341 fail("should throw IllegalArgumentException"); 342 } catch (IllegalArgumentException e) { 343 // expected 344 } 345 fc.close(); 346 assertTrue(tmpFile.delete()); 347 348 // Scanner(ReadableByteChannel = null, Charset = null) 349 try { 350 s = new Scanner((ReadableByteChannel) null, null); 351 fail("Should throw NullPointerException"); 352 } catch (NullPointerException e) { 353 // expected 354 } 355 356 // Scanner(ReadableByteChannel = null, Charset = invalid) 357 try { 358 s = new Scanner((ReadableByteChannel) null, "invalid"); 359 fail("Should throw NullPointerException"); 360 } catch (NullPointerException e) { 361 // expected 362 } 363 364 // Scanner(ReadableByteChannel, Charset = null) 365 try { 366 s = new Scanner(fc, null); 367 fail("Should throw IllegalArgumentException"); 368 } catch (IllegalArgumentException e) { 369 // expected 370 } 371 // TODO: test if the specified charset is used. 372 } 373 374 /** 375 * @tests java.util.Scanner#Scanner(String) 376 */ 377 @TestTargetNew( 378 level = TestLevel.COMPLETE, 379 notes = "", 380 method = "Scanner", 381 args = {java.lang.String.class} 382 ) 383 public void test_ConstructorLjava_lang_String() { 384 s = new Scanner("test string"); 385 assertNotNull(s); 386 s.close(); 387 388 // Scanner(String) 389 try { 390 s = new Scanner((String) null); 391 fail("Should throw NullPointerException"); 392 } catch (NullPointerException e) { 393 // expected 394 } 395 } 396 397 /** 398 * @tests java.util.Scanner#close() 399 */ 400 @TestTargetNew( 401 level = TestLevel.COMPLETE, 402 notes = "", 403 method = "close", 404 args = {} 405 ) 406 public void test_close() throws IOException { 407 File tmpFile = File.createTempFile("TestFileForScanner", ".tmp"); 408 FileOutputStream fos = new FileOutputStream(tmpFile); 409 FileChannel fc = fos.getChannel(); 410 s = new Scanner(fc); 411 412 // Write out a int before the scanner is closed, should be OK. 413 fos.write(12); 414 415 s.close(); 416 assertFalse(fc.isOpen()); 417 418 // Write out a int after the scanner is closed, IOException should be 419 // thrown out. 420 try { 421 fos.write(12); 422 fail("Should throw IOException"); 423 } catch (IOException e) { 424 // expected 425 } 426 427 s.close(); // no exception should be thrown 428 assertTrue(tmpFile.delete()); 429 } 430 431 /** 432 * @tests java.util.Scanner#ioException() 433 */ 434 @TestTargetNew( 435 level = TestLevel.COMPLETE, 436 notes = "", 437 method = "ioException", 438 args = {} 439 ) 440 public void test_ioException() throws IOException { 441 MockCloseable mc = new MockCloseable(); 442 s = new Scanner(mc); 443 assertNull(s.ioException()); // No operation, no exception 444 445 s.close(); // IOException should be cached 446 assertNotNull(s.ioException()); 447 assertTrue(s.ioException() instanceof IOException); 448 } 449 450 /** 451 * @tests java.util.Scanner#delimiter() 452 */ 453 @TestTargetNew( 454 level = TestLevel.COMPLETE, 455 notes = "", 456 method = "delimiter", 457 args = {} 458 ) 459 public void test_delimiter() { 460 s = new Scanner("test"); 461 Pattern pattern = s.delimiter(); 462 assertEquals("\\p{javaWhitespace}+", pattern.toString()); 463 } 464 465 /** 466 * @tests java.util.Scanner#useDelimiter(Pattern) 467 */ 468 @TestTargetNew( 469 level = TestLevel.COMPLETE, 470 notes = "", 471 method = "useDelimiter", 472 args = {java.util.regex.Pattern.class} 473 ) 474 public void test_useDelimiter_LPattern() { 475 s = new Scanner("test"); 476 s.useDelimiter(Pattern.compile("\\w+")); 477 assertEquals("\\w+", s.delimiter().toString()); 478 479 s = new Scanner("test"); 480 s.useDelimiter((Pattern) null); 481 assertNull(s.delimiter()); 482 } 483 484 /** 485 * @tests java.util.Scanner#useDelimiter(String) 486 */ 487 @TestTargetNew( 488 level = TestLevel.COMPLETE, 489 notes = "", 490 method = "useDelimiter", 491 args = {java.lang.String.class} 492 ) 493 public void test_useDelimiter_String() { 494 s = new Scanner("test"); 495 try { 496 s.useDelimiter((String) null); 497 fail("Should throw NullPointerException"); 498 } catch (NullPointerException e) { 499 // expected 500 } 501 502 s = new Scanner("test"); 503 s.useDelimiter("\\w+"); 504 assertEquals("\\w+", s.delimiter().toString()); 505 } 506 507 /** 508 * @tests java.util.Scanner#locale() 509 */ 510 @TestTargetNew( 511 level = TestLevel.COMPLETE, 512 notes = "", 513 method = "locale", 514 args = {} 515 ) 516 public void test_locale() { 517 s = new Scanner("test"); 518 assertEquals(Locale.getDefault(), s.locale()); 519 } 520 521 /** 522 * @tests java.util.Scanner#useLocale(Locale) 523 */ 524 @TestTargetNew( 525 level = TestLevel.COMPLETE, 526 notes = "", 527 method = "useLocale", 528 args = {java.util.Locale.class} 529 ) 530 public void test_useLocale_LLocale() { 531 s = new Scanner("test"); 532 try { 533 s.useLocale(null); 534 fail("Should throw NullPointerException"); 535 } catch (NullPointerException e) { 536 // expected 537 } 538 539 s.useLocale(new Locale("test", "test")); 540 assertEquals(new Locale("test", "test"), s.locale()); 541 } 542 543 /** 544 * @tests java.util.Scanner#radix() 545 */ 546 @TestTargetNew( 547 level = TestLevel.COMPLETE, 548 notes = "", 549 method = "radix", 550 args = {} 551 ) 552 public void test_radix() { 553 s = new Scanner("test"); 554 assertEquals(10, s.radix()); 555 } 556 557 /** 558 * @tests java.util.Scanner#useRadix() 559 */ 560 @TestTargetNew( 561 level = TestLevel.COMPLETE, 562 notes = "", 563 method = "useRadix", 564 args = {int.class} 565 ) 566 public void test_useRadix_I() { 567 s = new Scanner("test"); 568 try { 569 s.useRadix(Character.MIN_RADIX - 1); 570 fail("Should throw IllegalArgumentException"); 571 } catch (IllegalArgumentException e) { 572 // expected 573 } 574 try { 575 s.useRadix(Character.MAX_RADIX + 1); 576 fail("Should throw IllegalArgumentException"); 577 } catch (IllegalArgumentException e) { 578 // expected 579 } 580 s.useRadix(11); 581 assertEquals(11, s.radix()); 582 } 583 584 /** 585 * @tests java.util.Scanner#remove() 586 */ 587 @TestTargetNew( 588 level = TestLevel.COMPLETE, 589 notes = "", 590 method = "remove", 591 args = {} 592 ) 593 public void test_remove() { 594 s = new Scanner("aab*b*").useDelimiter("\\*"); 595 try { 596 s.remove(); 597 fail("should throw UnsupportedOperationException"); 598 } catch (UnsupportedOperationException e) { 599 //Expected 600 } 601 } 602 603 /** 604 * @tests java.util.Scanner#match() 605 */ 606 @TestTargetNew( 607 level = TestLevel.COMPLETE, 608 notes = "", 609 method = "match", 610 args = {} 611 ) 612 public void test_match() { 613 MatchResult result ; 614 s = new Scanner("1 2 "); 615 try { 616 s.match(); 617 fail("should throw IllegalStateException"); 618 } catch (IllegalStateException e) { 619 // Expected 620 } 621 assertEquals("1", s.next()); 622 assertEquals("2", s.next()); 623 result = s.match(); 624 assertEquals(2, result.start()); 625 assertEquals(3, result.end()); 626 assertEquals(2, result.start(0)); 627 assertEquals(3, result.end(0)); 628 assertEquals("2", result.group()); 629 assertEquals("2", result.group(0)); 630 assertEquals(0, result.groupCount()); 631 try { 632 result.start(1); 633 fail("should throw IndexOutOfBoundsException"); 634 } catch (IndexOutOfBoundsException e) { 635 // Expected 636 } 637 try { 638 s.next(); 639 fail("should throw NoSuchElementException"); 640 } catch (NoSuchElementException e) { 641 // Expected 642 } 643 try { 644 s.match(); 645 fail("should throw IllegalStateException"); 646 } catch (IllegalStateException e) { 647 // Expected 648 } 649 650 s = new Scanner("True faLse"); 651 try { 652 s.match(); 653 fail("should throw IllegalStateException"); 654 } catch (IllegalStateException e) { 655 // Expected 656 } 657 assertTrue(s.nextBoolean()); 658 result = s.match(); 659 assertEquals(0, result.start()); 660 assertEquals(4, result.end()); 661 assertEquals(0, result.start(0)); 662 assertEquals(4, result.end(0)); 663 assertEquals("True", result.group()); 664 assertEquals(0, result.groupCount()); 665 assertFalse(s.nextBoolean()); 666 try { 667 s.nextBoolean(); 668 fail("should throw NoSuchElementException"); 669 } catch (NoSuchElementException e) { 670 // Expected 671 } 672 try { 673 s.match(); 674 fail("should throw IllegalStateException"); 675 } catch (IllegalStateException e) { 676 // Expected 677 } 678 679 s = new Scanner("True faLse"); 680 assertTrue(s.nextBoolean()); 681 result = s.match(); 682 assertEquals(0, result.start()); 683 assertEquals(4, result.end()); 684 assertEquals(0, result.start(0)); 685 assertEquals(4, result.end(0)); 686 assertEquals("True", result.group()); 687 assertEquals(0, result.groupCount()); 688 s.close(); 689 try { 690 s.nextBoolean(); 691 fail("should throw IllegalStateException"); 692 } catch (IllegalStateException e) { 693 // Expected 694 } 695 result = s.match(); 696 assertEquals(0, result.start()); 697 assertEquals(4, result.end()); 698 assertEquals(0, result.start(0)); 699 assertEquals(4, result.end(0)); 700 assertEquals("True", result.group()); 701 assertEquals(0, result.groupCount()); 702 703 s = new Scanner("True fase"); 704 assertTrue(s.nextBoolean()); 705 assertEquals(0, result.groupCount()); 706 try { 707 s.nextBoolean(); 708 fail("Should throw InputMismatchException"); 709 } catch (InputMismatchException e) { 710 // expected 711 } 712 try { 713 s.match(); 714 fail("should throw IllegalStateException"); 715 } catch (IllegalStateException e) { 716 // Expected 717 } 718 719 s = new Scanner("True fase"); 720 assertTrue(s.nextBoolean()); 721 try { 722 s.next((Pattern)null); 723 fail("Should throw NullPointerException"); 724 } catch (NullPointerException e) { 725 // Expected 726 } 727 result = s.match(); 728 assertEquals(0, result.start()); 729 assertEquals(4, result.end()); 730 assertEquals(0, result.start(0)); 731 assertEquals(4, result.end(0)); 732 assertEquals("True", result.group()); 733 assertEquals(0, result.groupCount()); 734 735 } 736 737 /** 738 * @throws IOException 739 * @tests java.util.Scanner#next() 740 */ 741 @TestTargetNew( 742 level = TestLevel.COMPLETE, 743 notes = "", 744 method = "next", 745 args = {} 746 ) 747 public void test_next() throws IOException { 748 // use special delimiter 749 s = new Scanner("1**2").useDelimiter("\\*"); 750 assertEquals("1", s.next()); 751 assertEquals("", s.next()); 752 assertEquals("2", s.next()); 753 754 s = new Scanner(" \t 1 \t 2").useDelimiter("\\s*"); 755 assertEquals("1", s.next()); 756 assertEquals("2", s.next()); 757 try { 758 s.next(); 759 fail("should throw NoSuchElementException"); 760 } catch (NoSuchElementException e) { 761 // Expected 762 } 763 764 s = new Scanner("a").useDelimiter("a?"); 765 try { 766 s.next(); 767 fail("should throw NoSuchElementException"); 768 } catch (NoSuchElementException e) { 769 // Expected 770 } 771 772 s = new Scanner("aa").useDelimiter("a?"); 773 assertEquals("", s.next()); 774 try { 775 s.next(); 776 fail("should throw NoSuchElementException"); 777 } catch (NoSuchElementException e) { 778 // Expected 779 } 780 781 782 s = new Scanner("word( )test( )").useDelimiter("\\( \\)"); 783 assertEquals("word", s.next()); 784 assertEquals("test", s.next()); 785 786 s = new Scanner("? next ").useDelimiter("( )"); 787 assertEquals("?", s.next()); 788 assertEquals("next", s.next()); 789 assertEquals("", s.next()); 790 791 s = new Scanner("word1 word2 "); 792 assertEquals("word1", s.next()); 793 assertEquals("word2", s.next()); 794 // test boundary case 795 try { 796 s.next(); 797 fail("should throw NoSuchElementException"); 798 } catch (NoSuchElementException e) { 799 // Expected 800 } 801 802 // just delimiter exists in this scanner 803 s = new Scanner(" "); 804 try { 805 s.next(); 806 fail("Should throw NoSuchElementException"); 807 } catch (NoSuchElementException e) { 808 // Expected 809 } 810 811 // nothing exists in this scanner 812 s = new Scanner(""); 813 try { 814 s.next(); 815 fail("Should throw NoSuchElementException"); 816 } catch (NoSuchElementException e) { 817 // Expected 818 } 819 820 // no delimiter exists in this scanner 821 s = new Scanner("test"); 822 assertEquals("test", s.next()); 823 824 // input resourse starts with delimiter 825 s = new Scanner(" test"); 826 assertEquals("test", s.next()); 827 828 // input resource ends with delimiter 829 s = new Scanner(" test "); 830 assertEquals("test", s.next()); 831 832 // Harmony uses 1024 as default buffer size, 833 // What if a sentence can not be read in all in once. 834 StringBuilder longSentence = new StringBuilder(1025); 835 for (int i = 0; i < 11; i++) { 836 longSentence.append(" "); 837 } 838 for (int i = 11; i < 1026; i++) { 839 longSentence.append("a"); 840 } 841 s = new Scanner(longSentence.toString()); 842 assertEquals(longSentence.toString().trim(), s.next()); 843 844 s = new Scanner(" test test"); 845 assertEquals("test", s.next()); 846 assertEquals("test", s.next()); 847 848 // What if use a delimiter of length 0. 849 s = new Scanner("test\ntest").useDelimiter(Pattern.compile("^", 850 Pattern.MULTILINE)); 851 assertEquals("test\n", s.next()); 852 assertEquals("test", s.next()); 853 854 s = new Scanner("").useDelimiter(Pattern.compile("^", 855 Pattern.MULTILINE)); 856 try { 857 s.next(); 858 fail("should throw NoSuchElementException"); 859 } catch (NoSuchElementException e) { 860 // Expected 861 } 862 863 s = new Scanner("").useDelimiter(Pattern.compile("^*", 864 Pattern.MULTILINE)); 865 try { 866 s.next(); 867 fail("should throw NoSuchElementException"); 868 } catch (NoSuchElementException e) { 869 // Expected 870 } 871 872 s = new Scanner("test\ntest").useDelimiter(Pattern.compile("^*", 873 Pattern.MULTILINE)); 874 assertEquals("t", s.next()); 875 assertEquals("e", s.next()); 876 877 s = new Scanner("\ntest\ntest").useDelimiter(Pattern.compile("$", 878 Pattern.MULTILINE)); 879 assertEquals("\ntest", s.next()); 880 assertEquals("\ntest", s.next()); 881 882 // test socket inputStream 883 // Harmony uses 1024 as default buffer size, 884 // what if the leading delimiter is larger than 1023 885 for (int i = 0; i < 1024; i++) { 886 os.write(" ".getBytes()); 887 } 888 os.write(" 1 2 ".getBytes()); 889 s = new Scanner(client); 890 assertEquals("1", s.next()); 891 assertEquals("2", s.next()); 892 os.write(" 1 2".getBytes()); 893 serverSocket.close(); 894 assertEquals("1", s.next()); 895 assertEquals("2", s.next()); 896 try { 897 s.next(); 898 fail("should throw NoSuchElementException"); 899 } catch (NoSuchElementException e) { 900 // Expected 901 } 902 903 s.close(); 904 try { 905 s.next(); 906 fail("IllegalStateException expected"); 907 } catch (IllegalStateException e) { 908 //expected 909 } 910 } 911 912 /** 913 * @throws IOException 914 * @tests java.util.Scanner#next(Pattern) 915 */ 916 @TestTargetNew( 917 level = TestLevel.COMPLETE, 918 notes = "", 919 method = "next", 920 args = {java.util.regex.Pattern.class} 921 ) 922 public void test_nextLPattern() throws IOException { 923 Pattern pattern; 924 s = new Scanner("aab*2*").useDelimiter("\\*"); 925 pattern = Pattern.compile("a*b"); 926 assertEquals("aab", s.next(pattern)); 927 try { 928 s.next(pattern); 929 fail("should throw InputMismatchException"); 930 } catch (InputMismatchException e) { 931 // Expected 932 } 933 934 s = new Scanner("word ? "); 935 pattern = Pattern.compile("\\w+"); 936 assertEquals("word", s.next(pattern)); 937 try { 938 s.next(pattern); 939 fail("should throw InputMismatchException"); 940 } catch (InputMismatchException e) { 941 // Expected 942 } 943 944 s = new Scanner("word1 word2 "); 945 pattern = Pattern.compile("\\w+"); 946 assertEquals("word1", s.next(pattern)); 947 assertEquals("word2", s.next(pattern)); 948 // test boundary case 949 try { 950 s.next(pattern); 951 fail("should throw NoSuchElementException"); 952 } catch (NoSuchElementException e) { 953 // Expected 954 } 955 956 // test socket inputStream 957 958 os.write("aab 2".getBytes()); 959 serverSocket.close(); 960 961 s = new Scanner(client); 962 pattern = Pattern.compile("a*b"); 963 assertEquals("aab", s.next(pattern)); 964 try { 965 s.next(pattern); 966 fail("should throw InputMismatchException"); 967 } catch (InputMismatchException e) { 968 // Expected 969 } 970 971 s.close(); 972 try { 973 s.next(pattern); 974 fail("IllegalStateException expected"); 975 } catch (IllegalStateException e) { 976 //expected 977 } 978 } 979 980 /** 981 * @throws IOException 982 * @tests java.util.Scanner#next(String) 983 */ 984 @TestTargetNew( 985 level = TestLevel.COMPLETE, 986 notes = "", 987 method = "next", 988 args = {java.lang.String.class} 989 ) 990 public void test_nextLString() throws IOException { 991 s = new Scanner("b*a*").useDelimiter("\\*"); 992 assertEquals("b", s.next("a*b")); 993 try { 994 s.next("a*b"); 995 fail("should throw InputMismatchException"); 996 } catch (InputMismatchException e) { 997 // Expected 998 } 999 1000 s = new Scanner("word ? "); 1001 assertEquals("word", s.next("\\w+")); 1002 try { 1003 s.next("\\w+"); 1004 fail("should throw InputMismatchException"); 1005 } catch (InputMismatchException e) { 1006 // Expected 1007 } 1008 1009 s = new Scanner("word1 next "); 1010 assertEquals("word1", s.next("\\w+")); 1011 assertEquals("next", s.next("\\w+")); 1012 // test boundary case 1013 try { 1014 s.next("\\w+"); 1015 fail("should throw NoSuchElementException"); 1016 } catch (NoSuchElementException e) { 1017 // Expected 1018 } 1019 1020 // test socket inputStream 1021 os.write("aab 2".getBytes()); 1022 serverSocket.close(); 1023 1024 s = new Scanner(client); 1025 assertEquals("aab", s.next("a*b")); 1026 try { 1027 s.next("a*b"); 1028 fail("should throw InputMismatchException"); 1029 } catch (InputMismatchException e) { 1030 // Expected 1031 } 1032 1033 s.close(); 1034 try { 1035 s.next("a*b"); 1036 fail("IllegalStateException expected"); 1037 } catch (IllegalStateException e) { 1038 //expected 1039 } 1040 } 1041 1042 /** 1043 * @throws IOException 1044 * @tests java.util.Scanner#nextBoolean() 1045 */ 1046 @TestTargetNew( 1047 level = TestLevel.COMPLETE, 1048 notes = "", 1049 method = "nextBoolean", 1050 args = {} 1051 ) 1052 public void test_nextBoolean() throws IOException { 1053 // case insensitive 1054 s = new Scanner("TRue"); 1055 assertTrue(s.nextBoolean()); 1056 1057 s = new Scanner("tRue false"); 1058 assertTrue(s.nextBoolean()); 1059 assertFalse(s.nextBoolean()); 1060 try { 1061 s.nextBoolean(); 1062 fail("Should throw NoSuchElementException"); 1063 } catch (NoSuchElementException e) { 1064 // Expected 1065 } 1066 1067 s = new Scanner("true1"); 1068 try { 1069 s.nextBoolean(); 1070 fail("Should throw InputMismatchException"); 1071 } catch (InputMismatchException e) { 1072 // Expected 1073 } 1074 1075 try { 1076 s = new Scanner(""); 1077 s.nextBoolean(); 1078 fail("Should throw NoSuchElementException"); 1079 } catch (NoSuchElementException e) { 1080 // Expected 1081 } 1082 1083 // test socket inputStream 1084 os.write("true false".getBytes()); 1085 serverSocket.close(); 1086 1087 s = new Scanner(client); 1088 assertTrue(s.nextBoolean()); 1089 assertFalse(s.nextBoolean()); 1090 1091 // ues '*' as delimiter 1092 s = new Scanner("true**false").useDelimiter("\\*"); 1093 assertTrue(s.nextBoolean()); 1094 try { 1095 s.nextBoolean(); 1096 fail("should throw NoSuchElementException"); 1097 } catch (NoSuchElementException e) { 1098 // Expected 1099 } 1100 1101 s = new Scanner("false( )").useDelimiter("\\( \\)"); 1102 assertFalse(s.nextBoolean()); 1103 1104 s.close(); 1105 try { 1106 s.nextBoolean(); 1107 fail("IllegalStateException expected"); 1108 } catch (IllegalStateException e) { 1109 //expected 1110 } 1111 } 1112 1113 /** 1114 * @throws IOException 1115 * @tests java.util.Scanner#nextInt(int) 1116 */ 1117 @TestTargetNew( 1118 level = TestLevel.COMPLETE, 1119 notes = "", 1120 method = "nextInt", 1121 args = {int.class} 1122 ) 1123 @KnownFailure("Some locales were removed last minute in cupcake") 1124 public void test_nextIntI() throws IOException { 1125 s = new Scanner("123 456"); 1126 assertEquals(123, s.nextInt(10)); 1127 assertEquals(456, s.nextInt(10)); 1128 try { 1129 s.nextInt(10); 1130 fail("Should throw NoSuchElementException"); 1131 } catch (NoSuchElementException e) { 1132 // Expected 1133 } 1134 1135 // If the radix is different from 10 1136 s = new Scanner("123 456"); 1137 assertEquals(38, s.nextInt(5)); 1138 try { 1139 s.nextInt(5); 1140 fail("Should throw InputMismatchException"); 1141 } catch (InputMismatchException e) { 1142 // Expected 1143 } 1144 1145 // If the number is out of range 1146 s = new Scanner("123456789123456789123456789123456789"); 1147 try { 1148 s.nextInt(10); 1149 fail("Should throw InputMismatchException"); 1150 } catch (InputMismatchException e) { 1151 // Expected 1152 } 1153 1154 /* 1155 * Different locale can only recognize corresponding locale sensitive 1156 * string. ',' is used in many locales as group separator. 1157 */ 1158 s = new Scanner("23,456 23,456"); 1159 s.useLocale(Locale.GERMANY); 1160 try { 1161 s.nextInt(10); 1162 fail("Should throw InputMismatchException"); 1163 } catch (InputMismatchException e) { 1164 // expected 1165 } 1166 s.useLocale(Locale.ENGLISH); 1167 // If exception is thrown out, input will not be advanced. 1168 assertEquals(23456, s.nextInt(10)); 1169 assertEquals(23456, s.nextInt(10)); 1170 1171 /* 1172 * ''' is used in many locales as group separator. 1173 */ 1174 s = new Scanner("23'456 23'456"); 1175 s.useLocale(Locale.GERMANY); 1176 try { 1177 s.nextInt(10); 1178 fail("Should throw InputMismatchException"); 1179 } catch (InputMismatchException e) { 1180 // expected 1181 } 1182 s.useLocale(new Locale("de", "CH")); 1183 // If exception is thrown out, input will not be advanced. 1184 assertEquals(23456, s.nextInt(10)); 1185 assertEquals(23456, s.nextInt(10)); 1186 1187 /* 1188 * The input string has Arabic-Indic digits. 1189 */ 1190 s = new Scanner("1\u06602 1\u06662"); 1191 assertEquals(102, s.nextInt(10)); 1192 try { 1193 s.nextInt(5); 1194 fail("Should throw InputMismatchException"); 1195 } catch (InputMismatchException e) { 1196 // Expected 1197 } 1198 assertEquals(162, s.nextInt(10)); 1199 1200 /* 1201 * '.' is used in many locales as group separator. The input string 1202 * has Arabic-Indic digits . 1203 */ 1204 s = new Scanner("23.45\u0666 23.456"); 1205 s.useLocale(Locale.CHINESE); 1206 try { 1207 s.nextInt(10); 1208 fail("Should throw InputMismatchException"); 1209 } catch (InputMismatchException e) { 1210 // expected 1211 } 1212 s.useLocale(Locale.GERMANY); 1213 // If exception is thrown out, input will not be advanced. 1214 assertEquals(23456, s.nextInt(10)); 1215 assertEquals(23456, s.nextInt(10)); 1216 1217 // The input string starts with zero 1218 s = new Scanner("03,456"); 1219 s.useLocale(Locale.ENGLISH); 1220 try { 1221 s.nextInt(10); 1222 fail("Should throw InputMismatchException"); 1223 } catch (InputMismatchException e) { 1224 // expected 1225 } 1226 1227 s = new Scanner("03456"); 1228 assertEquals(3456, s.nextInt(10)); 1229 1230 s = new Scanner("\u06603,456"); 1231 s.useLocale(Locale.ENGLISH); 1232 assertEquals(3456, s.nextInt(10)); 1233 1234 s = new Scanner("E3456"); 1235 assertEquals(930902, s.nextInt(16)); 1236 // The following test case fails on RI, because RI does not support 1237 // letter as leading digit 1238 if (!disableRIBugs) { 1239 s = new Scanner("E3,456"); 1240 s.useLocale(Locale.ENGLISH); 1241 assertEquals(930902, s.nextInt(16)); 1242 } 1243 1244 /* 1245 * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50' 1246 * respectively, but they are not differentiated. 1247 */ 1248 s = new Scanner("12300"); 1249 s.useLocale(Locale.CHINESE); 1250 assertEquals(12300, s.nextInt(10)); 1251 1252 s = new Scanner("123\u0966\u0966"); 1253 s.useLocale(Locale.CHINESE); 1254 assertEquals(12300, s.nextInt(10)); 1255 1256 s = new Scanner("123\u0e50\u0e50"); 1257 s.useLocale(Locale.CHINESE); 1258 assertEquals(12300, s.nextInt(10)); 1259 1260 /* 1261 * There are three types of negative prefix all in all. '' '-' '(' There 1262 * are three types of negative suffix all in all. '' '-' ')' '(' and ')' 1263 * must be used togethor. Prefix '-' and suffix '-' must be used 1264 * exclusively. 1265 */ 1266 1267 /* 1268 * According to Integer regular expression: Integer :: = ( [-+]? (* 1269 * Numeral ) ) | LocalPositivePrefix Numeral LocalPositiveSuffix | 1270 * LocalNegativePrefix Numeral LocalNegativeSuffix 123- should be 1271 * recognized by scanner with locale ar_AE, (123) shouble be recognized 1272 * by scanner with locale mk_MK. But this is not the case on RI. 1273 */ 1274 s = new Scanner("-123 123- -123-"); 1275 s.useLocale(new Locale("ar", "AE")); 1276 assertEquals(-123, s.nextInt(10)); 1277 // The following test case fails on RI 1278 if (!disableRIBugs) { 1279 assertEquals(-123, s.nextInt(10)); 1280 } 1281 try { 1282 s.nextInt(10); 1283 fail("Should throw InputMismatchException"); 1284 } catch (InputMismatchException e) { 1285 // expected 1286 } 1287 1288 s = new Scanner("-123 123- (123)"); 1289 s.useLocale(new Locale("mk", "MK")); 1290 assertEquals(-123, s.nextInt(10)); 1291 try { 1292 s.nextInt(); 1293 fail("Should throw InputMismatchException"); 1294 } catch (InputMismatchException e) { 1295 // expected 1296 } 1297 // Skip the un-recognizable token 123-. 1298 assertEquals("123-", s.next()); 1299 // The following test case fails on RI 1300 if (!disableRIBugs) { 1301 assertEquals(-123, s.nextInt(10)); 1302 1303 // If the parameter radix is illegal, the following test cases fail on 1304 // RI 1305 try { 1306 s.nextInt(Character.MIN_RADIX - 1); 1307 fail("Should throw IllegalArgumentException"); 1308 } catch (IllegalArgumentException e) { 1309 // Expected 1310 } 1311 try { 1312 s.nextInt(Character.MAX_RADIX + 1); 1313 fail("Should throw IllegalArgumentException"); 1314 } catch (IllegalArgumentException e) { 1315 // Expected 1316 } 1317 } 1318 1319 s.close(); 1320 try { 1321 s.nextInt(10); 1322 fail("IllegalStateException expected"); 1323 } catch (IllegalStateException e) { 1324 //expected 1325 } 1326 } 1327 1328 /** 1329 * @throws IOException 1330 * @tests java.util.Scanner#nextInt() 1331 */ 1332 @TestTargetNew( 1333 level = TestLevel.COMPLETE, 1334 notes = "", 1335 method = "nextInt", 1336 args = {} 1337 ) 1338 @KnownFailure("Some locales were removed last minute in cupcake") 1339 public void test_nextInt() throws IOException { 1340 s = new Scanner("123 456"); 1341 assertEquals(123, s.nextInt()); 1342 assertEquals(456, s.nextInt()); 1343 try { 1344 s.nextInt(); 1345 fail("Should throw NoSuchElementException"); 1346 } catch (NoSuchElementException e) { 1347 // Expected 1348 } 1349 1350 // If the radix is different from 10 1351 s = new Scanner("123 456"); 1352 s.useRadix(5); 1353 assertEquals(38, s.nextInt()); 1354 try { 1355 s.nextInt(); 1356 fail("Should throw InputMismatchException"); 1357 } catch (InputMismatchException e) { 1358 // Expected 1359 } 1360 1361 // If the number is out of range 1362 s = new Scanner("123456789123456789123456789123456789"); 1363 try { 1364 s.nextInt(); 1365 fail("Should throw InputMismatchException"); 1366 } catch (InputMismatchException e) { 1367 // Expected 1368 } 1369 1370 /* 1371 * Different locale can only recognize corresponding locale sensitive 1372 * string. ',' is used in many locales as group separator. 1373 */ 1374 s = new Scanner("23,456 23,456"); 1375 s.useLocale(Locale.GERMANY); 1376 try { 1377 s.nextInt(); 1378 fail("Should throw InputMismatchException"); 1379 } catch (InputMismatchException e) { 1380 // expected 1381 } 1382 s.useLocale(Locale.ENGLISH); 1383 // If exception is thrown out, input will not be advanced. 1384 assertEquals(23456, s.nextInt()); 1385 assertEquals(23456, s.nextInt()); 1386 1387 /* 1388 * ''' is used in many locales as group separator. 1389 */ 1390 s = new Scanner("23'456 23'456"); 1391 s.useLocale(Locale.GERMANY); 1392 try { 1393 s.nextInt(); 1394 fail("Should throw InputMismatchException"); 1395 } catch (InputMismatchException e) { 1396 // expected 1397 } 1398 s.useLocale(new Locale("de", "CH")); 1399 // If exception is thrown out, input will not be advanced. 1400 assertEquals(23456, s.nextInt()); 1401 assertEquals(23456, s.nextInt()); 1402 1403 /* 1404 * The input string has Arabic-Indic digits. 1405 */ 1406 s = new Scanner("1\u06602 1\u06662"); 1407 assertEquals(102, s.nextInt()); 1408 s.useRadix(5); 1409 try { 1410 s.nextInt(); 1411 fail("Should throw InputMismatchException"); 1412 } catch (InputMismatchException e) { 1413 // Expected 1414 } 1415 s.useRadix(10); 1416 assertEquals(162, s.nextInt()); 1417 1418 /* 1419 * '.' is used in many locales as group separator. The input string 1420 * has Arabic-Indic digits . 1421 */ 1422 s = new Scanner("23.45\u0666 23.456"); 1423 s.useLocale(Locale.CHINESE); 1424 try { 1425 s.nextInt(); 1426 fail("Should throw InputMismatchException"); 1427 } catch (InputMismatchException e) { 1428 // expected 1429 } 1430 s.useLocale(Locale.GERMANY); 1431 // If exception is thrown out, input will not be advanced. 1432 assertEquals(23456, s.nextInt()); 1433 assertEquals(23456, s.nextInt()); 1434 1435 // The input string starts with zero 1436 s = new Scanner("03,456"); 1437 s.useLocale(Locale.ENGLISH); 1438 try { 1439 s.nextInt(); 1440 fail("Should throw InputMismatchException"); 1441 } catch (InputMismatchException e) { 1442 // expected 1443 } 1444 1445 s = new Scanner("03456"); 1446 assertEquals(3456, s.nextInt()); 1447 1448 s = new Scanner("\u06603,456"); 1449 s.useLocale(Locale.ENGLISH); 1450 assertEquals(3456, s.nextInt()); 1451 1452 s = new Scanner("E3456"); 1453 s.useRadix(16); 1454 assertEquals(930902, s.nextInt()); 1455 1456 // The following test case fails on RI, because RI does not support 1457 // letter as leading digit 1458 s = new Scanner("E3,456"); 1459 s.useLocale(Locale.ENGLISH); 1460 s.useRadix(16); 1461 if (!disableRIBugs) { 1462 assertEquals(930902, s.nextInt()); 1463 } 1464 1465 /* 1466 * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50' 1467 * respectively, but they are not differentiated. 1468 */ 1469 s = new Scanner("12300"); 1470 s.useLocale(Locale.CHINESE); 1471 assertEquals(12300, s.nextInt()); 1472 1473 s = new Scanner("123\u0966\u0966"); 1474 s.useLocale(Locale.CHINESE); 1475 assertEquals(12300, s.nextInt()); 1476 1477 s = new Scanner("123\u0e50\u0e50"); 1478 s.useLocale(Locale.CHINESE); 1479 assertEquals(12300, s.nextInt()); 1480 1481 /* 1482 * There are three types of negative prefix all in all. '' '-' '(' There 1483 * are three types of negative suffix all in all. '' '-' ')' '(' and ')' 1484 * must be used togethor. Prefix '-' and suffix '-' must be used 1485 * exclusively. 1486 */ 1487 1488 /* 1489 * According to Integer regular expression: Integer :: = ( [-+]? (* 1490 * Numeral ) ) | LocalPositivePrefix Numeral LocalPositiveSuffix | 1491 * LocalNegativePrefix Numeral LocalNegativeSuffix 123- should be 1492 * recognized by scanner with locale ar_AE, (123) shouble be recognized 1493 * by scanner with locale mk_MK. But this is not the case on RI. 1494 */ 1495 s = new Scanner("-123 123- -123-"); 1496 s.useLocale(new Locale("ar", "AE")); 1497 assertEquals(-123, s.nextInt()); 1498 // The following test case fails on RI 1499 if (!disableRIBugs) { 1500 assertEquals(-123, s.nextInt()); 1501 } 1502 try { 1503 s.nextInt(); 1504 fail("Should throw InputMismatchException"); 1505 } catch (InputMismatchException e) { 1506 // expected 1507 } 1508 1509 s = new Scanner("-123 123- (123)"); 1510 s.useLocale(new Locale("mk", "MK")); 1511 assertEquals(-123, s.nextInt()); 1512 try { 1513 s.nextInt(); 1514 fail("Should throw InputMismatchException"); 1515 } catch (InputMismatchException e) { 1516 // expected 1517 } 1518 // Skip the un-recognizable token 123-. 1519 assertEquals("123-", s.next()); 1520 // The following test case fails on RI 1521 if (!disableRIBugs) { 1522 assertEquals(-123, s.nextInt()); 1523 } 1524 1525 s.close(); 1526 try { 1527 s.nextInt(); 1528 fail("IllegalStateException expected"); 1529 } catch (IllegalStateException e) { 1530 //expected 1531 } 1532 } 1533 1534 /** 1535 * @throws IOException 1536 * @tests java.util.Scanner#nextByte(int) 1537 */ 1538 @TestTargetNew( 1539 level = TestLevel.COMPLETE, 1540 notes = "", 1541 method = "nextByte", 1542 args = {int.class} 1543 ) 1544 public void test_nextByteI() throws IOException { 1545 s = new Scanner("123 126"); 1546 assertEquals(123, s.nextByte(10)); 1547 assertEquals(126, s.nextByte(10)); 1548 try { 1549 s.nextByte(10); 1550 fail("Should throw NoSuchElementException"); 1551 } catch (NoSuchElementException e) { 1552 // Expected 1553 } 1554 1555 // If the radix is different from 10 1556 s = new Scanner("123 126"); 1557 assertEquals(38, s.nextByte(5)); 1558 try { 1559 s.nextByte(5); 1560 fail("Should throw InputMismatchException"); 1561 } catch (InputMismatchException e) { 1562 // Expected 1563 } 1564 1565 // If the number is out of range 1566 s = new Scanner("1234"); 1567 try { 1568 s.nextByte(10); 1569 fail("Should throw InputMismatchException"); 1570 } catch (InputMismatchException e) { 1571 // Expected 1572 } 1573 1574 /* 1575 * The input string has Arabic-Indic digits. 1576 */ 1577 s = new Scanner("1\u06602 12\u0666"); 1578 assertEquals(102, s.nextByte(10)); 1579 try { 1580 s.nextByte(5); 1581 fail("Should throw InputMismatchException"); 1582 } catch (InputMismatchException e) { 1583 // Expected 1584 } 1585 assertEquals(126, s.nextByte(10)); 1586 1587 s = new Scanner("012"); 1588 assertEquals(12, s.nextByte(10)); 1589 1590 s = new Scanner("E"); 1591 assertEquals(14, s.nextByte(16)); 1592 1593 /* 1594 * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50' 1595 * respectively, but they are not differentiated. 1596 */ 1597 s = new Scanner("100"); 1598 s.useLocale(Locale.CHINESE); 1599 assertEquals(100, s.nextByte(10)); 1600 1601 s = new Scanner("1\u0966\u0966"); 1602 s.useLocale(Locale.CHINESE); 1603 assertEquals(100, s.nextByte(10)); 1604 1605 s = new Scanner("1\u0e50\u0e50"); 1606 s.useLocale(Locale.CHINESE); 1607 assertEquals(100, s.nextByte(10)); 1608 1609 s = new Scanner("-123"); 1610 s.useLocale(new Locale("ar", "AE")); 1611 assertEquals(-123, s.nextByte(10)); 1612 1613 1614 s = new Scanner("-123"); 1615 s.useLocale(new Locale("mk", "MK")); 1616 assertEquals(-123, s.nextByte(10)); 1617 1618 s.close(); 1619 try { 1620 s.nextByte(10); 1621 fail("IllegalStateException expected"); 1622 } catch (IllegalStateException e) { 1623 //expected 1624 } 1625 } 1626 1627 /** 1628 * @throws IOException 1629 * @tests java.util.Scanner#nextByte() 1630 */ 1631 @TestTargetNew( 1632 level = TestLevel.COMPLETE, 1633 notes = "", 1634 method = "nextByte", 1635 args = {} 1636 ) 1637 public void test_nextByte() throws IOException { 1638 s = new Scanner("123 126"); 1639 assertEquals(123, s.nextByte()); 1640 assertEquals(126, s.nextByte()); 1641 try { 1642 s.nextByte(); 1643 fail("Should throw NoSuchElementException"); 1644 } catch (NoSuchElementException e) { 1645 // Expected 1646 } 1647 1648 // If the radix is different from 10 1649 s = new Scanner("123 126"); 1650 s.useRadix(5); 1651 assertEquals(38, s.nextByte()); 1652 try { 1653 s.nextByte(); 1654 fail("Should throw InputMismatchException"); 1655 } catch (InputMismatchException e) { 1656 // Expected 1657 } 1658 1659 // If the number is out of range 1660 s = new Scanner("1234"); 1661 try { 1662 s.nextByte(); 1663 fail("Should throw InputMismatchException"); 1664 } catch (InputMismatchException e) { 1665 // Expected 1666 } 1667 1668 /* 1669 * The input string has Arabic-Indic digits. 1670 */ 1671 s = new Scanner("1\u06602 12\u0666"); 1672 assertEquals(102, s.nextByte()); 1673 s.useRadix(5); 1674 try { 1675 s.nextByte(); 1676 fail("Should throw InputMismatchException"); 1677 } catch (InputMismatchException e) { 1678 // Expected 1679 } 1680 s.useRadix(10); 1681 assertEquals(126, s.nextByte()); 1682 1683 s = new Scanner("012"); 1684 assertEquals(12, s.nextByte()); 1685 1686 s = new Scanner("E"); 1687 s.useRadix(16); 1688 assertEquals(14, s.nextByte()); 1689 1690 /* 1691 * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50' 1692 * respectively, but they are not differentiated. 1693 */ 1694 s = new Scanner("100"); 1695 s.useLocale(Locale.CHINESE); 1696 assertEquals(100, s.nextByte()); 1697 1698 s = new Scanner("1\u0966\u0966"); 1699 s.useLocale(Locale.CHINESE); 1700 assertEquals(100, s.nextByte()); 1701 1702 s = new Scanner("1\u0e50\u0e50"); 1703 s.useLocale(Locale.CHINESE); 1704 assertEquals(100, s.nextByte()); 1705 1706 s = new Scanner("-123"); 1707 s.useLocale(new Locale("ar", "AE")); 1708 assertEquals(-123, s.nextByte()); 1709 1710 s = new Scanner("-123"); 1711 s.useLocale(new Locale("mk", "MK")); 1712 assertEquals(-123, s.nextByte()); 1713 1714 s.close(); 1715 try { 1716 s.nextByte(); 1717 fail("IllegalStateException expected"); 1718 } catch (IllegalStateException e) { 1719 //expected 1720 } 1721 } 1722 1723 /** 1724 * @throws IOException 1725 * @tests java.util.Scanner#nextFloat() 1726 */ 1727 @TestTargetNew( 1728 level = TestLevel.COMPLETE, 1729 notes = "", 1730 method = "nextFloat", 1731 args = {} 1732 ) 1733 @KnownFailure("Some locales were removed last minute in cupcake") 1734 public void test_nextFloat() throws IOException { 1735 s = new Scanner("123 45\u0666. 123.4 .123 "); 1736 s.useLocale(Locale.ENGLISH); 1737 assertEquals((float)123.0, s.nextFloat()); 1738 assertEquals((float)456.0, s.nextFloat()); 1739 assertEquals((float)123.4, s.nextFloat()); 1740 assertEquals((float)0.123, s.nextFloat()); 1741 try { 1742 s.nextFloat(); 1743 fail("Should throw NoSuchElementException"); 1744 } catch (NoSuchElementException e) { 1745 // Expected 1746 } 1747 1748 s = new Scanner("+123.4 -456.7 123,456.789 0.1\u06623,4"); 1749 s.useLocale(Locale.ENGLISH); 1750 assertEquals((float)123.4, s.nextFloat()); 1751 assertEquals((float)-456.7, s.nextFloat()); 1752 assertEquals((float)123456.789, s.nextFloat()); 1753 try { 1754 s.nextFloat(); 1755 fail("Should throw InputMismatchException"); 1756 } catch (InputMismatchException e) { 1757 // Expected 1758 } 1759 1760 // Scientific notation 1761 s = new Scanner("+123.4E10 -456.7e+12 123,456.789E-10"); 1762 s.useLocale(Locale.ENGLISH); 1763 assertEquals((float)1.234E12, s.nextFloat()); 1764 assertEquals((float)-4.567E14, s.nextFloat()); 1765 assertEquals((float)1.23456789E-5, s.nextFloat()); 1766 1767 s = new Scanner("NaN Infinity -Infinity"); 1768 assertEquals(Float.NaN, s.nextFloat()); 1769 assertEquals(Float.POSITIVE_INFINITY, s.nextFloat()); 1770 assertEquals(Float.NEGATIVE_INFINITY, s.nextFloat()); 1771 1772 String str=String.valueOf(Float.MAX_VALUE*2); 1773 s=new Scanner(str); 1774 assertEquals(Float.POSITIVE_INFINITY,s.nextFloat()); 1775 1776 /* 1777 * Different locale can only recognize corresponding locale sensitive 1778 * string. ',' is used in many locales as group separator. 1779 */ 1780 s = new Scanner("23,456 23,456"); 1781 s.useLocale(Locale.ENGLISH); 1782 assertEquals((float)23456.0, s.nextFloat()); 1783 s.useLocale(Locale.GERMANY); 1784 assertEquals((float)23.456, s.nextFloat()); 1785 1786 s = new Scanner("23.456 23.456"); 1787 s.useLocale(Locale.ENGLISH); 1788 assertEquals((float)23.456, s.nextFloat()); 1789 s.useLocale(Locale.GERMANY); 1790 assertEquals((float)23456.0, s.nextFloat()); 1791 1792 s = new Scanner("23,456.7 23.456,7"); 1793 s.useLocale(Locale.ENGLISH); 1794 assertEquals((float)23456.7, s.nextFloat()); 1795 s.useLocale(Locale.GERMANY); 1796 assertEquals((float)23456.7, s.nextFloat()); 1797 1798 s = new Scanner("-123.4 123.4- -123.4-"); 1799 s.useLocale(new Locale("ar", "AE")); 1800 assertEquals((float)-123.4, s.nextFloat()); 1801 //The following test case fails on RI 1802 if (!disableRIBugs) { 1803 assertEquals((float)-123.4, s.nextFloat()); 1804 } 1805 try { 1806 s.nextFloat(); 1807 fail("Should throw InputMismatchException"); 1808 } catch (InputMismatchException e) { 1809 // Expected 1810 } 1811 1812 s = new Scanner("(123) 123- -123"); 1813 s.useLocale(new Locale("mk", "MK")); 1814 if (!disableRIBugs) { 1815 assertEquals((float)-123.0, s.nextFloat()); 1816 } 1817 try { 1818 s.nextFloat(); 1819 fail("Should throw InputMismatchException"); 1820 } catch (InputMismatchException e) { 1821 // Expected 1822 } 1823 // Skip the un-recognizable token 123-. 1824 if (!disableRIBugs) { 1825 assertEquals("123-", s.next()); 1826 assertEquals((float)-123.0, s.nextFloat()); 1827 } 1828 1829 s.close(); 1830 try { 1831 s.nextFloat(); 1832 fail("IllegalStateException expected"); 1833 } catch (IllegalStateException e) { 1834 //expected 1835 } 1836 } 1837 1838 /** 1839 * @throws IOException 1840 * @tests java.util.Scanner#nextBigInteger(int) 1841 */ 1842 @TestTargetNew( 1843 level = TestLevel.COMPLETE, 1844 notes = "", 1845 method = "nextBigInteger", 1846 args = {int.class} 1847 ) 1848 @KnownFailure("nextBigInteger method doesn't work properly if input string has Arabic-Indic digits") 1849 public void test_nextBigIntegerI() throws IOException { 1850 s = new Scanner("123 456"); 1851 assertEquals(new BigInteger("123"), s.nextBigInteger(10)); 1852 assertEquals(new BigInteger("456"), s.nextBigInteger(10)); 1853 try { 1854 s.nextBigInteger(10); 1855 fail("Should throw NoSuchElementException"); 1856 } catch (NoSuchElementException e) { 1857 // Expected 1858 } 1859 1860 // If the radix is different from 10 1861 s = new Scanner("123 456"); 1862 assertEquals(new BigInteger("38"), s.nextBigInteger(5)); 1863 try { 1864 s.nextBigInteger(5); 1865 fail("Should throw InputMismatchException"); 1866 } catch (InputMismatchException e) { 1867 // Expected 1868 } 1869 1870 /* 1871 * Different locale can only recognize corresponding locale sensitive 1872 * string. ',' is used in many locales as group separator. 1873 */ 1874 s = new Scanner("23,456 23,456"); 1875 s.useLocale(Locale.GERMANY); 1876 try { 1877 s.nextBigInteger(10); 1878 fail("Should throw InputMismatchException"); 1879 } catch (InputMismatchException e) { 1880 // Expected 1881 } 1882 s.useLocale(Locale.ENGLISH); 1883 // If exception is thrown out, input will not be advanced. 1884 assertEquals(new BigInteger("23456"), s.nextBigInteger(10)); 1885 assertEquals(new BigInteger("23456"), s.nextBigInteger(10)); 1886 1887 /* 1888 * ''' is used in many locales as group separator. 1889 */ 1890 s = new Scanner("23'456 23'456"); 1891 s.useLocale(Locale.GERMANY); 1892 try { 1893 s.nextBigInteger(10); 1894 fail("Should throw InputMismatchException"); 1895 } catch (InputMismatchException e) { 1896 // Expected 1897 } 1898 s.useLocale(new Locale("de", "CH")); 1899 // If exception is thrown out, input will not be advanced. 1900 assertEquals(new BigInteger("23456"), s.nextBigInteger(10)); 1901 assertEquals(new BigInteger("23456"), s.nextBigInteger(10)); 1902 1903 /* 1904 * The input string has Arabic-Indic digits. 1905 */ 1906 s = new Scanner("1\u06602 1\u06662"); 1907 assertEquals(new BigInteger("102"), s.nextBigInteger(10)); 1908 try { 1909 s.nextBigInteger(5); 1910 fail("Should throw InputMismatchException"); 1911 } catch (InputMismatchException e) { 1912 // Expected 1913 } 1914 assertEquals(new BigInteger("162"), s.nextBigInteger(10)); 1915 1916 /* 1917 * '.' is used in many locales as group separator. The input string 1918 * has Arabic-Indic digits . 1919 */ 1920 s = new Scanner("23.45\u0666 23.456"); 1921 s.useLocale(Locale.CHINESE); 1922 try { 1923 s.nextBigInteger(10); 1924 fail("Should throw InputMismatchException"); 1925 } catch (InputMismatchException e) { 1926 // Expected 1927 } 1928 s.useLocale(Locale.GERMANY); 1929 // If exception is thrown out, input will not be advanced. 1930 assertEquals(new BigInteger("23456"), s.nextBigInteger(10)); 1931 assertEquals(new BigInteger("23456"), s.nextBigInteger(10)); 1932 1933 // The input string starts with zero 1934 s = new Scanner("03,456"); 1935 s.useLocale(Locale.ENGLISH); 1936 try { 1937 s.nextBigInteger(10); 1938 fail("Should throw InputMismatchException"); 1939 } catch (InputMismatchException e) { 1940 // Expected 1941 } 1942 1943 s = new Scanner("03456"); 1944 assertEquals(new BigInteger("3456"), s.nextBigInteger(10)); 1945 1946 s = new Scanner("\u06603,456"); 1947 s.useLocale(Locale.ENGLISH); 1948 assertEquals(new BigInteger("3456"), s.nextBigInteger(10)); 1949 1950 s = new Scanner("E34"); 1951 assertEquals(new BigInteger("3636"), s.nextBigInteger(16)); 1952 1953 /* 1954 * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50' 1955 * respectively, but they are not differentiated. 1956 */ 1957 s = new Scanner("12300"); 1958 s.useLocale(Locale.CHINESE); 1959 assertEquals(new BigInteger("12300"), s.nextBigInteger(10)); 1960 1961 s = new Scanner("123\u0966\u0966"); 1962 s.useLocale(Locale.CHINESE); 1963 assertEquals(new BigInteger("12300"), s.nextBigInteger(10)); 1964 1965 s = new Scanner("123\u0e50\u0e50"); 1966 s.useLocale(Locale.CHINESE); 1967 assertEquals(new BigInteger("12300"), s.nextBigInteger(10)); 1968 1969 s = new Scanner("-123"); 1970 s.useLocale(new Locale("ar", "AE")); 1971 assertEquals(new BigInteger("-123"), s.nextBigInteger(10)); 1972 1973 1974 s = new Scanner("-123"); 1975 s.useLocale(new Locale("mk", "MK")); 1976 assertEquals(new BigInteger("-123"), s.nextBigInteger(10)); 1977 1978 s.close(); 1979 try { 1980 s.nextBigInteger(10); 1981 fail("IllegalStateException expected"); 1982 } catch (IllegalStateException e) { 1983 //expected 1984 } 1985 } 1986 1987 /** 1988 * @throws IOException 1989 * @tests java.util.Scanner#nextBigInteger() 1990 */ 1991 @TestTargetNew( 1992 level = TestLevel.COMPLETE, 1993 notes = "", 1994 method = "nextBigInteger", 1995 args = {} 1996 ) 1997 @KnownFailure("nextBigInteger method doesn't work properly if input string has Arabic-Indic digits") 1998 public void test_nextBigInteger() throws IOException { 1999 s = new Scanner("123 456"); 2000 assertEquals(new BigInteger("123"), s.nextBigInteger()); 2001 assertEquals(new BigInteger("456"), s.nextBigInteger()); 2002 try { 2003 s.nextBigInteger(); 2004 fail("Should throw NoSuchElementException"); 2005 } catch (NoSuchElementException e) { 2006 // Expected 2007 } 2008 2009 // If the radix is different from 10 2010 s = new Scanner("123 456"); 2011 s.useRadix(5); 2012 assertEquals(new BigInteger("38"), s.nextBigInteger()); 2013 try { 2014 s.nextBigInteger(); 2015 fail("Should throw InputMismatchException"); 2016 } catch (InputMismatchException e) { 2017 // Expected 2018 } 2019 2020 /* 2021 * Different locale can only recognize corresponding locale sensitive 2022 * string. ',' is used in many locales as group separator. 2023 */ 2024 s = new Scanner("23,456 23,456"); 2025 s.useLocale(Locale.GERMANY); 2026 try { 2027 s.nextBigInteger(); 2028 fail("Should throw InputMismatchException"); 2029 } catch (InputMismatchException e) { 2030 // Expected 2031 } 2032 s.useLocale(Locale.ENGLISH); 2033 // If exception is thrown out, input will not be advanced. 2034 assertEquals(new BigInteger("23456"), s.nextBigInteger()); 2035 assertEquals(new BigInteger("23456"), s.nextBigInteger()); 2036 2037 /* 2038 * ''' is used in many locales as group separator. 2039 */ 2040 s = new Scanner("23'456 23'456"); 2041 s.useLocale(Locale.GERMANY); 2042 try { 2043 s.nextBigInteger(); 2044 fail("Should throw InputMismatchException"); 2045 } catch (InputMismatchException e) { 2046 // Expected 2047 } 2048 s.useLocale(new Locale("de", "CH")); 2049 // If exception is thrown out, input will not be advanced. 2050 assertEquals(new BigInteger("23456"), s.nextBigInteger()); 2051 assertEquals(new BigInteger("23456"), s.nextBigInteger()); 2052 2053 /* 2054 * The input string has Arabic-Indic digits. 2055 */ 2056 s = new Scanner("1\u06602 1\u06662"); 2057 assertEquals(new BigInteger("102"), s.nextBigInteger()); 2058 s.useRadix(5); 2059 try { 2060 s.nextBigInteger(); 2061 fail("Should throw InputMismatchException"); 2062 } catch (InputMismatchException e) { 2063 // Expected 2064 } 2065 s.useRadix(10); 2066 assertEquals(new BigInteger("162"), s.nextBigInteger()); 2067 2068 /* 2069 * '.' is used in many locales as group separator. The input string 2070 * has Arabic-Indic digits . 2071 */ 2072 s = new Scanner("23.45\u0666 23.456"); 2073 s.useLocale(Locale.CHINESE); 2074 try { 2075 s.nextBigInteger(); 2076 fail("Should throw InputMismatchException"); 2077 } catch (InputMismatchException e) { 2078 // Expected 2079 } 2080 s.useLocale(Locale.GERMANY); 2081 // If exception is thrown out, input will not be advanced. 2082 assertEquals(new BigInteger("23456"), s.nextBigInteger()); 2083 assertEquals(new BigInteger("23456"), s.nextBigInteger()); 2084 2085 // The input string starts with zero 2086 s = new Scanner("03,456"); 2087 s.useLocale(Locale.ENGLISH); 2088 try { 2089 s.nextBigInteger(); 2090 fail("Should throw InputMismatchException"); 2091 } catch (InputMismatchException e) { 2092 // Expected 2093 } 2094 2095 s = new Scanner("03456"); 2096 assertEquals(new BigInteger("3456"), s.nextBigInteger()); 2097 2098 s = new Scanner("\u06603,456"); 2099 s.useLocale(Locale.ENGLISH); 2100 assertEquals(new BigInteger("3456"), s.nextBigInteger()); 2101 2102 s = new Scanner("E34"); 2103 s.useRadix(16); 2104 assertEquals(new BigInteger("3636"), s.nextBigInteger()); 2105 2106 /* 2107 * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50' 2108 * respectively, but they are not differentiated. 2109 */ 2110 s = new Scanner("12300"); 2111 s.useLocale(Locale.CHINESE); 2112 assertEquals(new BigInteger("12300"), s.nextBigInteger()); 2113 2114 s = new Scanner("123\u0966\u0966"); 2115 s.useLocale(Locale.CHINESE); 2116 assertEquals(new BigInteger("12300"), s.nextBigInteger()); 2117 2118 s = new Scanner("123\u0e50\u0e50"); 2119 s.useLocale(Locale.CHINESE); 2120 assertEquals(new BigInteger("12300"), s.nextBigInteger()); 2121 2122 s = new Scanner("-123"); 2123 s.useLocale(new Locale("ar", "AE")); 2124 assertEquals(new BigInteger("-123"), s.nextBigInteger()); 2125 2126 s = new Scanner("-123"); 2127 s.useLocale(new Locale("mk", "MK")); 2128 assertEquals(new BigInteger("-123"), s.nextBigInteger()); 2129 2130 s.close(); 2131 try { 2132 s.nextBigInteger(); 2133 fail("IllegalStateException expected"); 2134 } catch (IllegalStateException e) { 2135 //expected 2136 } 2137 } 2138 2139 /** 2140 * @throws IOException 2141 * @tests java.util.Scanner#nextShort(int) 2142 */ 2143 @TestTargetNew( 2144 level = TestLevel.COMPLETE, 2145 notes = "", 2146 method = "nextShort", 2147 args = {int.class} 2148 ) 2149 @KnownFailure("Some locales were removed last minute in cupcake") 2150 public void test_nextShortI() throws IOException { 2151 s = new Scanner("123 456"); 2152 assertEquals(123, s.nextShort(10)); 2153 assertEquals(456, s.nextShort(10)); 2154 try { 2155 s.nextShort(10); 2156 fail("Should throw NoSuchElementException"); 2157 } catch (NoSuchElementException e) { 2158 // Expected 2159 } 2160 2161 // If the radix is different from 10 2162 s = new Scanner("123 456"); 2163 assertEquals(38, s.nextShort(5)); 2164 try { 2165 s.nextShort(5); 2166 fail("Should throw InputMismatchException"); 2167 } catch (InputMismatchException e) { 2168 // Expected 2169 } 2170 2171 // If the number is out of range 2172 s = new Scanner("123456789"); 2173 try { 2174 s.nextShort(10); 2175 fail("Should throw InputMismatchException"); 2176 } catch (InputMismatchException e) { 2177 // Expected 2178 } 2179 2180 /* 2181 * Different locale can only recognize corresponding locale sensitive 2182 * string. ',' is used in many locales as group separator. 2183 */ 2184 s = new Scanner("23,456 23,456"); 2185 s.useLocale(Locale.GERMANY); 2186 try { 2187 s.nextShort(10); 2188 fail("Should throw InputMismatchException"); 2189 } catch (InputMismatchException e) { 2190 // Expected 2191 } 2192 s.useLocale(Locale.ENGLISH); 2193 // If exception is thrown out, input will not be advanced. 2194 assertEquals(23456, s.nextShort(10)); 2195 assertEquals(23456, s.nextShort(10)); 2196 2197 /* 2198 * ''' is used in many locales as group separator. 2199 */ 2200 s = new Scanner("23'456 23'456"); 2201 s.useLocale(Locale.GERMANY); 2202 try { 2203 s.nextShort(10); 2204 fail("Should throw InputMismatchException"); 2205 } catch (InputMismatchException e) { 2206 // Expected 2207 } 2208 s.useLocale(new Locale("de", "CH")); 2209 // If exception is thrown out, input will not be advanced. 2210 assertEquals(23456, s.nextShort(10)); 2211 assertEquals(23456, s.nextShort(10)); 2212 2213 /* 2214 * The input string has Arabic-Indic digits. 2215 */ 2216 s = new Scanner("1\u06602 1\u06662"); 2217 assertEquals(102, s.nextShort(10)); 2218 try { 2219 s.nextShort(5); 2220 fail("Should throw InputMismatchException"); 2221 } catch (InputMismatchException e) { 2222 // Expected 2223 } 2224 assertEquals(162, s.nextShort(10)); 2225 2226 /* 2227 * '.' is used in many locales as group separator. The input string 2228 * has Arabic-Indic digits . 2229 */ 2230 s = new Scanner("23.45\u0666 23.456"); 2231 s.useLocale(Locale.CHINESE); 2232 try { 2233 s.nextShort(10); 2234 fail("Should throw InputMismatchException"); 2235 } catch (InputMismatchException e) { 2236 // Expected 2237 } 2238 s.useLocale(Locale.GERMANY); 2239 // If exception is thrown out, input will not be advanced. 2240 assertEquals(23456, s.nextShort(10)); 2241 assertEquals(23456, s.nextShort(10)); 2242 2243 // The input string starts with zero 2244 s = new Scanner("03,456"); 2245 s.useLocale(Locale.ENGLISH); 2246 try { 2247 s.nextShort(10); 2248 fail("Should throw InputMismatchException"); 2249 } catch (InputMismatchException e) { 2250 // Expected 2251 } 2252 2253 s = new Scanner("03456"); 2254 assertEquals(3456, s.nextShort(10)); 2255 2256 s = new Scanner("\u06603,456"); 2257 s.useLocale(Locale.ENGLISH); 2258 assertEquals(3456, s.nextShort(10)); 2259 2260 s = new Scanner("E34"); 2261 assertEquals(3636, s.nextShort(16)); 2262 2263 /* 2264 * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50' 2265 * respectively, but they are not differentiated. 2266 */ 2267 s = new Scanner("12300"); 2268 s.useLocale(Locale.CHINESE); 2269 assertEquals(12300, s.nextShort(10)); 2270 2271 s = new Scanner("123\u0966\u0966"); 2272 s.useLocale(Locale.CHINESE); 2273 assertEquals(12300, s.nextShort(10)); 2274 2275 s = new Scanner("123\u0e50\u0e50"); 2276 s.useLocale(Locale.CHINESE); 2277 assertEquals(12300, s.nextShort(10)); 2278 2279 s = new Scanner("-123"); 2280 s.useLocale(new Locale("ar", "AE")); 2281 assertEquals(-123, s.nextShort(10)); 2282 2283 2284 s = new Scanner("-123"); 2285 s.useLocale(new Locale("mk", "MK")); 2286 assertEquals(-123, s.nextShort(10)); 2287 2288 s.close(); 2289 try { 2290 s.nextShort(10); 2291 fail("IllegalStateException expected"); 2292 } catch (IllegalStateException e) { 2293 //expected 2294 } 2295 } 2296 2297 /** 2298 * @throws IOException 2299 * @tests java.util.Scanner#nextShort() 2300 */ 2301 @TestTargetNew( 2302 level = TestLevel.COMPLETE, 2303 notes = "", 2304 method = "nextShort", 2305 args = {} 2306 ) 2307 @KnownFailure("Some locales were removed last minute in cupcake") 2308 public void test_nextShort() throws IOException { 2309 s = new Scanner("123 456"); 2310 assertEquals(123, s.nextShort()); 2311 assertEquals(456, s.nextShort()); 2312 try { 2313 s.nextShort(); 2314 fail("Should throw NoSuchElementException"); 2315 } catch (NoSuchElementException e) { 2316 // Expected 2317 } 2318 2319 // If the radix is different from 10 2320 s = new Scanner("123 456"); 2321 s.useRadix(5); 2322 assertEquals(38, s.nextShort()); 2323 try { 2324 s.nextShort(); 2325 fail("Should throw InputMismatchException"); 2326 } catch (InputMismatchException e) { 2327 // Expected 2328 } 2329 2330 // If the number is out of range 2331 s = new Scanner("123456789"); 2332 try { 2333 s.nextShort(); 2334 fail("Should throw InputMismatchException"); 2335 } catch (InputMismatchException e) { 2336 // Expected 2337 } 2338 2339 /* 2340 * Different locale can only recognize corresponding locale sensitive 2341 * string. ',' is used in many locales as group separator. 2342 */ 2343 s = new Scanner("23,456 23,456"); 2344 s.useLocale(Locale.GERMANY); 2345 try { 2346 s.nextShort(); 2347 fail("Should throw InputMismatchException"); 2348 } catch (InputMismatchException e) { 2349 // Expected 2350 } 2351 s.useLocale(Locale.ENGLISH); 2352 // If exception is thrown out, input will not be advanced. 2353 assertEquals(23456, s.nextShort()); 2354 assertEquals(23456, s.nextShort()); 2355 2356 /* 2357 * ''' is used in many locales as group separator. 2358 */ 2359 s = new Scanner("23'456 23'456"); 2360 s.useLocale(Locale.GERMANY); 2361 try { 2362 s.nextShort(); 2363 fail("Should throw InputMismatchException"); 2364 } catch (InputMismatchException e) { 2365 // Expected 2366 } 2367 s.useLocale(new Locale("de", "CH")); 2368 // If exception is thrown out, input will not be advanced. 2369 assertEquals(23456, s.nextShort()); 2370 assertEquals(23456, s.nextShort()); 2371 2372 /* 2373 * The input string has Arabic-Indic digits. 2374 */ 2375 s = new Scanner("1\u06602 1\u06662"); 2376 assertEquals(102, s.nextShort()); 2377 s.useRadix(5); 2378 try { 2379 s.nextShort(); 2380 fail("Should throw InputMismatchException"); 2381 } catch (InputMismatchException e) { 2382 // Expected 2383 } 2384 s.useRadix(10); 2385 assertEquals(162, s.nextShort()); 2386 2387 /* 2388 * '.' is used in many locales as group separator. The input string 2389 * has Arabic-Indic digits . 2390 */ 2391 s = new Scanner("23.45\u0666 23.456"); 2392 s.useLocale(Locale.CHINESE); 2393 try { 2394 s.nextShort(); 2395 fail("Should throw InputMismatchException"); 2396 } catch (InputMismatchException e) { 2397 // Expected 2398 } 2399 s.useLocale(Locale.GERMANY); 2400 // If exception is thrown out, input will not be advanced. 2401 assertEquals(23456, s.nextShort()); 2402 assertEquals(23456, s.nextShort()); 2403 2404 // The input string starts with zero 2405 s = new Scanner("03,456"); 2406 s.useLocale(Locale.ENGLISH); 2407 try { 2408 s.nextShort(); 2409 fail("Should throw InputMismatchException"); 2410 } catch (InputMismatchException e) { 2411 // Expected 2412 } 2413 2414 s = new Scanner("03456"); 2415 assertEquals(3456, s.nextShort()); 2416 2417 s = new Scanner("\u06603,456"); 2418 s.useLocale(Locale.ENGLISH); 2419 assertEquals(3456, s.nextShort()); 2420 2421 s = new Scanner("E34"); 2422 s.useRadix(16); 2423 assertEquals(3636, s.nextShort()); 2424 2425 /* 2426 * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50' 2427 * respectively, but they are not differentiated. 2428 */ 2429 s = new Scanner("12300"); 2430 s.useLocale(Locale.CHINESE); 2431 assertEquals(12300, s.nextShort()); 2432 2433 s = new Scanner("123\u0966\u0966"); 2434 s.useLocale(Locale.CHINESE); 2435 assertEquals(12300, s.nextShort()); 2436 2437 s = new Scanner("123\u0e50\u0e50"); 2438 s.useLocale(Locale.CHINESE); 2439 assertEquals(12300, s.nextShort()); 2440 2441 s = new Scanner("-123"); 2442 s.useLocale(new Locale("ar", "AE")); 2443 assertEquals(-123, s.nextShort()); 2444 2445 s = new Scanner("-123"); 2446 s.useLocale(new Locale("mk", "MK")); 2447 assertEquals(-123, s.nextShort()); 2448 2449 s.close(); 2450 try { 2451 s.nextShort(); 2452 fail("IllegalStateException expected"); 2453 } catch (IllegalStateException e) { 2454 //expected 2455 } 2456 } 2457 2458 /** 2459 * @throws IOException 2460 * @tests java.util.Scanner#nextLong(int) 2461 */ 2462 @TestTargetNew( 2463 level = TestLevel.COMPLETE, 2464 notes = "", 2465 method = "nextLong", 2466 args = {int.class} 2467 ) 2468 @KnownFailure("Some locales were removed last minute in cupcake") 2469 public void test_nextLongI() throws IOException { 2470 s = new Scanner("123 456"); 2471 assertEquals(123, s.nextLong(10)); 2472 assertEquals(456, s.nextLong(10)); 2473 try { 2474 s.nextLong(10); 2475 fail("Should throw NoSuchElementException"); 2476 } catch (NoSuchElementException e) { 2477 // Expected 2478 } 2479 2480 // If the radix is different from 10 2481 s = new Scanner("123 456"); 2482 assertEquals(38, s.nextLong(5)); 2483 try { 2484 s.nextLong(5); 2485 fail("Should throw InputMismatchException"); 2486 } catch (InputMismatchException e) { 2487 // Expected 2488 } 2489 2490 // If the number is out of range 2491 s = new Scanner("123456789123456789123456789123456789"); 2492 try { 2493 s.nextLong(10); 2494 fail("Should throw InputMismatchException"); 2495 } catch (InputMismatchException e) { 2496 // Expected 2497 } 2498 2499 /* 2500 * Different locale can only recognize corresponding locale sensitive 2501 * string. ',' is used in many locales as group separator. 2502 */ 2503 s = new Scanner("23,456 23,456"); 2504 s.useLocale(Locale.GERMANY); 2505 try { 2506 s.nextLong(10); 2507 fail("Should throw InputMismatchException"); 2508 } catch (InputMismatchException e) { 2509 // Expected 2510 } 2511 s.useLocale(Locale.ENGLISH); 2512 // If exception is thrown out, input will not be advanced. 2513 assertEquals(23456, s.nextLong(10)); 2514 assertEquals(23456, s.nextLong(10)); 2515 2516 /* 2517 * ''' is used in many locales as group separator. 2518 */ 2519 s = new Scanner("23'456 23'456"); 2520 s.useLocale(Locale.GERMANY); 2521 try { 2522 s.nextLong(10); 2523 fail("Should throw InputMismatchException"); 2524 } catch (InputMismatchException e) { 2525 // Expected 2526 } 2527 s.useLocale(new Locale("de", "CH")); 2528 // If exception is thrown out, input will not be advanced. 2529 assertEquals(23456, s.nextLong(10)); 2530 assertEquals(23456, s.nextLong(10)); 2531 2532 /* 2533 * The input string has Arabic-Indic digits. 2534 */ 2535 s = new Scanner("1\u06602 1\u06662"); 2536 assertEquals(102, s.nextLong(10)); 2537 try { 2538 s.nextLong(5); 2539 fail("Should throw InputMismatchException"); 2540 } catch (InputMismatchException e) { 2541 // Expected 2542 } 2543 assertEquals(162, s.nextLong(10)); 2544 2545 /* 2546 * '.' is used in many locales as group separator. The input string 2547 * has Arabic-Indic digits . 2548 */ 2549 s = new Scanner("23.45\u0666 23.456"); 2550 s.useLocale(Locale.CHINESE); 2551 try { 2552 s.nextLong(10); 2553 fail("Should throw InputMismatchException"); 2554 } catch (InputMismatchException e) { 2555 // Expected 2556 } 2557 s.useLocale(Locale.GERMANY); 2558 // If exception is thrown out, input will not be advanced. 2559 assertEquals(23456, s.nextLong(10)); 2560 assertEquals(23456, s.nextLong(10)); 2561 2562 // The input string starts with zero 2563 s = new Scanner("03,456"); 2564 s.useLocale(Locale.ENGLISH); 2565 try { 2566 s.nextLong(10); 2567 fail("Should throw InputMismatchException"); 2568 } catch (InputMismatchException e) { 2569 // Expected 2570 } 2571 2572 s = new Scanner("03456"); 2573 assertEquals(3456, s.nextLong(10)); 2574 2575 s = new Scanner("\u06603,456"); 2576 s.useLocale(Locale.ENGLISH); 2577 assertEquals(3456, s.nextLong(10)); 2578 2579 s = new Scanner("E34"); 2580 assertEquals(3636, s.nextLong(16)); 2581 2582 /* 2583 * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50' 2584 * respectively, but they are not differentiated. 2585 */ 2586 s = new Scanner("12300"); 2587 s.useLocale(Locale.CHINESE); 2588 assertEquals(12300, s.nextLong(10)); 2589 2590 s = new Scanner("123\u0966\u0966"); 2591 s.useLocale(Locale.CHINESE); 2592 assertEquals(12300, s.nextLong(10)); 2593 2594 s = new Scanner("123\u0e50\u0e50"); 2595 s.useLocale(Locale.CHINESE); 2596 assertEquals(12300, s.nextLong(10)); 2597 2598 s = new Scanner("-123"); 2599 s.useLocale(new Locale("ar", "AE")); 2600 assertEquals(-123, s.nextLong(10)); 2601 2602 2603 s = new Scanner("-123"); 2604 s.useLocale(new Locale("mk", "MK")); 2605 assertEquals(-123, s.nextLong(10)); 2606 2607 s.close(); 2608 try { 2609 s.nextLong(10); 2610 fail("IllegalStateException expected"); 2611 } catch (IllegalStateException e) { 2612 //expected 2613 } 2614 } 2615 2616 /** 2617 * @throws IOException 2618 * @tests java.util.Scanner#nextLong() 2619 */ 2620 @TestTargetNew( 2621 level = TestLevel.COMPLETE, 2622 notes = "", 2623 method = "nextLong", 2624 args = {} 2625 ) 2626 @KnownFailure("Some locales were removed last minute in cupcake") 2627 public void test_nextLong() throws IOException { 2628 s = new Scanner("123 456"); 2629 assertEquals(123, s.nextLong()); 2630 assertEquals(456, s.nextLong()); 2631 try { 2632 s.nextLong(); 2633 fail("Should throw NoSuchElementException"); 2634 } catch (NoSuchElementException e) { 2635 // Expected 2636 } 2637 2638 // If the radix is different from 10 2639 s = new Scanner("123 456"); 2640 s.useRadix(5); 2641 assertEquals(38, s.nextLong()); 2642 try { 2643 s.nextLong(); 2644 fail("Should throw InputMismatchException"); 2645 } catch (InputMismatchException e) { 2646 // Expected 2647 } 2648 2649 // If the number is out of range 2650 s = new Scanner("123456789123456789123456789123456789"); 2651 try { 2652 s.nextLong(); 2653 fail("Should throw InputMismatchException"); 2654 } catch (InputMismatchException e) { 2655 // Expected 2656 } 2657 2658 /* 2659 * Different locale can only recognize corresponding locale sensitive 2660 * string. ',' is used in many locales as group separator. 2661 */ 2662 s = new Scanner("23,456 23,456"); 2663 s.useLocale(Locale.GERMANY); 2664 try { 2665 s.nextLong(); 2666 fail("Should throw InputMismatchException"); 2667 } catch (InputMismatchException e) { 2668 // Expected 2669 } 2670 s.useLocale(Locale.ENGLISH); 2671 // If exception is thrown out, input will not be advanced. 2672 assertEquals(23456, s.nextLong()); 2673 assertEquals(23456, s.nextLong()); 2674 2675 /* 2676 * ''' is used in many locales as group separator. 2677 */ 2678 s = new Scanner("23'456 23'456"); 2679 s.useLocale(Locale.GERMANY); 2680 try { 2681 s.nextLong(); 2682 fail("Should throw InputMismatchException"); 2683 } catch (InputMismatchException e) { 2684 // Expected 2685 } 2686 s.useLocale(new Locale("de", "CH")); 2687 // If exception is thrown out, input will not be advanced. 2688 assertEquals(23456, s.nextLong()); 2689 assertEquals(23456, s.nextLong()); 2690 2691 /* 2692 * The input string has Arabic-Indic digits. 2693 */ 2694 s = new Scanner("1\u06602 1\u06662"); 2695 assertEquals(102, s.nextLong()); 2696 s.useRadix(5); 2697 try { 2698 s.nextLong(); 2699 fail("Should throw InputMismatchException"); 2700 } catch (InputMismatchException e) { 2701 // Expected 2702 } 2703 s.useRadix(10); 2704 assertEquals(162, s.nextLong()); 2705 2706 /* 2707 * '.' is used in many locales as group separator. The input string 2708 * has Arabic-Indic digits . 2709 */ 2710 s = new Scanner("23.45\u0666 23.456"); 2711 s.useLocale(Locale.CHINESE); 2712 try { 2713 s.nextLong(); 2714 fail("Should throw InputMismatchException"); 2715 } catch (InputMismatchException e) { 2716 // Expected 2717 } 2718 s.useLocale(Locale.GERMANY); 2719 // If exception is thrown out, input will not be advanced. 2720 assertEquals(23456, s.nextLong()); 2721 assertEquals(23456, s.nextLong()); 2722 2723 // The input string starts with zero 2724 s = new Scanner("03,456"); 2725 s.useLocale(Locale.ENGLISH); 2726 try { 2727 s.nextLong(); 2728 fail("Should throw InputMismatchException"); 2729 } catch (InputMismatchException e) { 2730 // Expected 2731 } 2732 2733 s = new Scanner("03456"); 2734 assertEquals(3456, s.nextLong()); 2735 2736 s = new Scanner("\u06603,456"); 2737 s.useLocale(Locale.ENGLISH); 2738 assertEquals(3456, s.nextLong()); 2739 2740 s = new Scanner("E34"); 2741 s.useRadix(16); 2742 assertEquals(3636, s.nextLong()); 2743 2744 /* 2745 * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50' 2746 * respectively, but they are not differentiated. 2747 */ 2748 s = new Scanner("12300"); 2749 s.useLocale(Locale.CHINESE); 2750 assertEquals(12300, s.nextLong()); 2751 2752 s = new Scanner("123\u0966\u0966"); 2753 s.useLocale(Locale.CHINESE); 2754 assertEquals(12300, s.nextLong()); 2755 2756 s = new Scanner("123\u0e50\u0e50"); 2757 s.useLocale(Locale.CHINESE); 2758 assertEquals(12300, s.nextLong()); 2759 2760 s = new Scanner("-123"); 2761 s.useLocale(new Locale("ar", "AE")); 2762 assertEquals(-123, s.nextLong()); 2763 2764 s = new Scanner("-123"); 2765 s.useLocale(new Locale("mk", "MK")); 2766 assertEquals(-123, s.nextLong()); 2767 2768 s.close(); 2769 try { 2770 s.nextLong(); 2771 fail("IllegalStateException expected"); 2772 } catch (IllegalStateException e) { 2773 //expected 2774 } 2775 } 2776 2777 /** 2778 * @throws IOException 2779 * @tests java.util.Scanner#hasNext() 2780 */ 2781 @TestTargetNew( 2782 level = TestLevel.COMPLETE, 2783 notes = "", 2784 method = "hasNext", 2785 args = {} 2786 ) 2787 public void test_hasNext() throws IOException { 2788 s = new Scanner("1##2").useDelimiter("\\#"); 2789 assertTrue(s.hasNext()); 2790 assertEquals("1", s.next()); 2791 assertEquals("", s.next()); 2792 assertEquals("2", s.next()); 2793 assertFalse(s.hasNext()); 2794 s.close(); 2795 try { 2796 s.hasNext(); 2797 fail("should throw IllegalStateException"); 2798 } catch (IllegalStateException e) { 2799 // expected 2800 } 2801 2802 s = new Scanner("1( )2( )").useDelimiter("\\( \\)"); 2803 assertTrue(s.hasNext()); 2804 assertTrue(s.hasNext()); 2805 assertEquals("1", s.next()); 2806 assertEquals("2", s.next()); 2807 2808 s = new Scanner("1 2 ").useDelimiter("( )"); 2809 assertEquals("1", s.next()); 2810 assertEquals("2", s.next()); 2811 assertTrue(s.hasNext()); 2812 assertEquals("", s.next()); 2813 2814 s = new Scanner("1\n2 "); 2815 assertEquals("1", s.next()); 2816 assertTrue(s.hasNext()); 2817 assertEquals("2", s.next()); 2818 assertFalse(s.hasNext()); 2819 // test boundary case 2820 try { 2821 s.next(); 2822 fail("should throw NoSuchElementException"); 2823 } catch (NoSuchElementException e) { 2824 // Expected 2825 } 2826 2827 s = new Scanner("1'\n'2 "); 2828 assertEquals("1'", s.next()); 2829 assertTrue(s.hasNext()); 2830 assertEquals("'2", s.next()); 2831 assertFalse(s.hasNext()); 2832 // test boundary case 2833 try { 2834 s.next(); 2835 fail("should throw NoSuchElementException"); 2836 } catch (NoSuchElementException e) { 2837 // Expected 2838 } 2839 2840 s = new Scanner(" "); 2841 assertFalse(s.hasNext()); 2842 2843 // test socket inputStream 2844 2845 os.write("1 2".getBytes()); 2846 serverSocket.close(); 2847 2848 s = new Scanner(client); 2849 assertEquals("1", s.next()); 2850 assertTrue(s.hasNext()); 2851 assertEquals("2", s.next()); 2852 assertFalse(s.hasNext()); 2853 try { 2854 s.next(); 2855 fail("should throw NoSuchElementException"); 2856 } catch (NoSuchElementException e) { 2857 // Expected 2858 } 2859 } 2860 2861 /** 2862 * @throws IOException 2863 * @tests java.util.Scanner#hasNext(Pattern) 2864 */ 2865 @TestTargetNew( 2866 level = TestLevel.COMPLETE, 2867 notes = "", 2868 method = "hasNext", 2869 args = {java.util.regex.Pattern.class} 2870 ) 2871 public void test_hasNextLPattern() throws IOException { 2872 Pattern pattern; 2873 s = new Scanner("aab@2@abb@").useDelimiter("\\@"); 2874 pattern = Pattern.compile("a*b"); 2875 assertTrue(s.hasNext(pattern)); 2876 assertEquals("aab", s.next(pattern)); 2877 assertFalse(s.hasNext(pattern)); 2878 try { 2879 s.next(pattern); 2880 fail("should throw InputMismatchException"); 2881 } catch (InputMismatchException e) { 2882 // Expected 2883 } 2884 2885 s = new Scanner("word ? "); 2886 pattern = Pattern.compile("\\w+"); 2887 assertTrue(s.hasNext(pattern)); 2888 assertEquals("word", s.next(pattern)); 2889 assertFalse(s.hasNext(pattern)); 2890 try { 2891 s.next(pattern); 2892 fail("should throw InputMismatchException"); 2893 } catch (InputMismatchException e) { 2894 // Expected 2895 } 2896 2897 s = new Scanner("word1 WorD2 "); 2898 pattern = Pattern.compile("\\w+"); 2899 assertTrue(s.hasNext(pattern)); 2900 assertEquals("word1", s.next(pattern)); 2901 assertTrue(s.hasNext(pattern)); 2902 assertEquals("WorD2", s.next(pattern)); 2903 assertFalse(s.hasNext(pattern)); 2904 try { 2905 s.next(pattern); 2906 fail("should throw NoSuchElementException"); 2907 } catch (NoSuchElementException e) { 2908 // Expected 2909 } 2910 2911 s = new Scanner("word1 WorD2 "); 2912 pattern = Pattern.compile("\\w+"); 2913 try { 2914 s.hasNext((Pattern) null); 2915 fail("Should throw NullPointerException"); 2916 } catch (NullPointerException e) { 2917 // expected 2918 } 2919 s.close(); 2920 try { 2921 s.hasNext(pattern); 2922 fail("should throw IllegalStateException"); 2923 } catch (IllegalStateException e) { 2924 // expected 2925 } 2926 2927 // test socket inputStream 2928 os.write("aab b".getBytes()); 2929 serverSocket.close(); 2930 2931 s = new Scanner(client); 2932 pattern = Pattern.compile("a+b"); 2933 assertTrue(s.hasNext(pattern)); 2934 assertEquals("aab", s.next(pattern)); 2935 assertFalse(s.hasNext(pattern)); 2936 try { 2937 s.next(pattern); 2938 fail("should throw InputMismatchException"); 2939 } catch (InputMismatchException e) { 2940 // Expected 2941 } 2942 } 2943 2944 /** 2945 * @throws IOException 2946 * @tests java.util.Scanner#hasNext(String) 2947 */ 2948 @TestTargetNew( 2949 level = TestLevel.COMPLETE, 2950 notes = "", 2951 method = "hasNext", 2952 args = {java.lang.String.class} 2953 ) 2954 public void test_hasNextLString() throws IOException { 2955 s = new Scanner("aab@2@abb@").useDelimiter("\\@"); 2956 try { 2957 s.hasNext((String)null); 2958 fail("Should throw NullPointerException"); 2959 } catch (NullPointerException e) { 2960 // expected 2961 } 2962 2963 s = new Scanner("aab*b*").useDelimiter("\\*"); 2964 assertTrue(s.hasNext("a+b")); 2965 assertEquals("aab", s.next("a+b")); 2966 assertFalse(s.hasNext("a+b")); 2967 try { 2968 s.next("a+b"); 2969 fail("should throw InputMismatchException"); 2970 } catch (InputMismatchException e) { 2971 // Expected 2972 } 2973 s.close(); 2974 try { 2975 s.hasNext("a+b"); 2976 fail("should throw IllegalStateException"); 2977 } catch (IllegalStateException e) { 2978 // expected 2979 } 2980 2981 s = new Scanner("WORD ? "); 2982 assertTrue(s.hasNext("\\w+")); 2983 assertEquals("WORD", s.next("\\w+")); 2984 assertFalse(s.hasNext("\\w+")); 2985 try { 2986 s.next("\\w+"); 2987 fail("should throw InputMismatchException"); 2988 } catch (InputMismatchException e) { 2989 // Expected 2990 } 2991 2992 s = new Scanner("word1 word2 "); 2993 assertEquals("word1", s.next("\\w+")); 2994 assertEquals("word2", s.next("\\w+")); 2995 // test boundary case 2996 try { 2997 s.next("\\w+"); 2998 fail("should throw NoSuchElementException"); 2999 } catch (NoSuchElementException e) { 3000 // Expected 3001 } 3002 3003 // test socket inputStream 3004 3005 os.write("aab 2".getBytes()); 3006 serverSocket.close(); 3007 3008 s = new Scanner(client); 3009 assertTrue(s.hasNext("a*b")); 3010 assertEquals("aab", s.next("a*b")); 3011 assertFalse(s.hasNext("a*b")); 3012 try { 3013 s.next("a*b"); 3014 fail("should throw InputMismatchException"); 3015 } catch (InputMismatchException e) { 3016 // Expected 3017 } 3018 } 3019 3020 /** 3021 * @throws IOException 3022 * @tests java.util.Scanner#hasNextBoolean() 3023 */ 3024 @TestTargetNew( 3025 level = TestLevel.COMPLETE, 3026 notes = "", 3027 method = "hasNextBoolean", 3028 args = {} 3029 ) 3030 public void test_hasNextBoolean() throws IOException { 3031 3032 s = new Scanner("TRue"); 3033 assertTrue(s.hasNextBoolean()); 3034 assertTrue(s.nextBoolean()); 3035 3036 s = new Scanner("tRue false"); 3037 assertTrue(s.hasNextBoolean()); 3038 assertTrue(s.nextBoolean()); 3039 assertTrue(s.hasNextBoolean()); 3040 assertFalse(s.nextBoolean()); 3041 3042 s = new Scanner(""); 3043 assertFalse(s.hasNextBoolean()); 3044 3045 // test socket inputStream 3046 3047 os.write("true false ".getBytes()); 3048 serverSocket.close(); 3049 3050 s = new Scanner(client); 3051 assertTrue(s.hasNextBoolean()); 3052 assertTrue(s.nextBoolean()); 3053 3054 // ues '*' as delimiter 3055 s = new Scanner("true**false").useDelimiter("\\*"); 3056 assertTrue(s.hasNextBoolean()); 3057 assertTrue(s.nextBoolean()); 3058 assertFalse(s.hasNextBoolean()); 3059 try { 3060 s.nextBoolean(); 3061 fail("should throw NoSuchElementException"); 3062 } catch (NoSuchElementException e) { 3063 // Expected 3064 } 3065 3066 s = new Scanner("false( )").useDelimiter("\\( \\)"); 3067 assertTrue(s.hasNextBoolean()); 3068 assertFalse(s.nextBoolean()); 3069 assertFalse(s.hasNextBoolean()); 3070 3071 s.close(); 3072 try { 3073 s.hasNextBoolean(); 3074 fail("IllegalStateException expected"); 3075 } catch (IllegalStateException e) { 3076 //expected 3077 } 3078 } 3079 3080 /** 3081 * @throws IOException 3082 * @tests java.util.Scanner#hasNextByte(int) 3083 */ 3084 @TestTargetNew( 3085 level = TestLevel.PARTIAL_COMPLETE, 3086 notes = "IllegalStateException checking missed.", 3087 method = "hasNextByte", 3088 args = {int.class} 3089 ) 3090 public void test_hasNextByteI() throws IOException { 3091 s = new Scanner("123 126"); 3092 assertTrue(s.hasNextByte(10)); 3093 assertEquals(123, s.nextByte(10)); 3094 assertTrue(s.hasNextByte(10)); 3095 assertEquals(126, s.nextByte(10)); 3096 assertFalse(s.hasNextByte(10)); 3097 try { 3098 s.nextByte(10); 3099 fail("Should throw NoSuchElementException"); 3100 } catch (NoSuchElementException e) { 3101 // Expected 3102 } 3103 3104 // If the radix is different from 10 3105 s = new Scanner("123 126"); 3106 assertTrue(s.hasNextByte(5)); 3107 assertEquals(38, s.nextByte(5)); 3108 assertFalse(s.hasNextByte(5)); 3109 try { 3110 s.nextByte(5); 3111 fail("Should throw InputMismatchException"); 3112 } catch (InputMismatchException e) { 3113 // Expected 3114 } 3115 3116 // If the number is out of range 3117 s = new Scanner("1234"); 3118 assertFalse(s.hasNextByte(10)); 3119 try { 3120 s.nextByte(10); 3121 fail("Should throw InputMismatchException"); 3122 } catch (InputMismatchException e) { 3123 // Expected 3124 } 3125 3126 /* 3127 * The input string has Arabic-Indic digits. 3128 */ 3129 s = new Scanner("1\u06602 12\u0666"); 3130 assertTrue(s.hasNextByte(10)); 3131 assertEquals(102, s.nextByte(10)); 3132 assertFalse(s.hasNextByte(5)); 3133 try { 3134 s.nextByte(5); 3135 fail("Should throw InputMismatchException"); 3136 } catch (InputMismatchException e) { 3137 // Expected 3138 } 3139 assertTrue(s.hasNextByte(10)); 3140 assertEquals(126, s.nextByte(10)); 3141 3142 s = new Scanner("012"); 3143 assertTrue(s.hasNextByte(10)); 3144 assertEquals(12, s.nextByte(10)); 3145 3146 s = new Scanner("E"); 3147 assertTrue(s.hasNextByte(16)); 3148 assertEquals(14, s.nextByte(16)); 3149 3150 /* 3151 * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50' 3152 * respectively, but they are not differentiated. 3153 */ 3154 s = new Scanner("100"); 3155 s.useLocale(Locale.CHINESE); 3156 assertTrue(s.hasNextByte(10)); 3157 assertEquals(100, s.nextByte(10)); 3158 3159 s = new Scanner("1\u0966\u0966"); 3160 s.useLocale(Locale.CHINESE); 3161 assertTrue(s.hasNextByte(10)); 3162 assertEquals(100, s.nextByte(10)); 3163 3164 s = new Scanner("1\u0e50\u0e50"); 3165 s.useLocale(Locale.CHINESE); 3166 assertTrue(s.hasNextByte(10)); 3167 assertEquals(100, s.nextByte(10)); 3168 3169 s = new Scanner("-123"); 3170 s.useLocale(new Locale("ar", "AE")); 3171 assertTrue(s.hasNextByte(10)); 3172 assertEquals(-123, s.nextByte(10)); 3173 3174 3175 s = new Scanner("-123"); 3176 s.useLocale(new Locale("mk", "MK")); 3177 assertTrue(s.hasNextByte(10)); 3178 assertEquals(-123, s.nextByte(10)); 3179 } 3180 3181 /** 3182 * @throws IOException 3183 * @tests java.util.Scanner#hasNextByte(int) 3184 */ 3185 @TestTargetNew( 3186 level = TestLevel.PARTIAL_COMPLETE, 3187 notes = "Regression test.", 3188 method = "hasNextByte", 3189 args = {int.class} 3190 ) 3191 public void test_hasNextByteI_cache() throws IOException{ 3192 //regression for HARMONY-2063 3193 s = new Scanner("123 45"); 3194 assertTrue(s.hasNextByte(8)); 3195 assertEquals(83, s.nextByte()); 3196 assertEquals(45, s.nextByte()); 3197 3198 s = new Scanner("123 45"); 3199 assertTrue(s.hasNextByte(10)); 3200 assertTrue(s.hasNextByte(8)); 3201 assertEquals(83, s.nextByte()); 3202 assertEquals(45, s.nextByte()); 3203 3204 s = new Scanner("-123 -45"); 3205 assertTrue(s.hasNextByte(8)); 3206 assertEquals(-123, s.nextInt()); 3207 assertEquals(-45, s.nextByte()); 3208 3209 s = new Scanner("123 45"); 3210 assertTrue(s.hasNextByte()); 3211 s.close(); 3212 try { 3213 s.nextByte(); 3214 fail("Should throw IllegalStateException"); 3215 } catch (IllegalStateException e) { 3216 // expected 3217 } 3218 } 3219 /** 3220 * @throws IOException 3221 * @tests java.util.Scanner#hasNextByte() 3222 */ 3223 @TestTargetNew( 3224 level = TestLevel.COMPLETE, 3225 notes = "", 3226 method = "hasNextByte", 3227 args = {} 3228 ) 3229 public void test_hasNextByte() throws IOException { 3230 s = new Scanner("123 126"); 3231 assertTrue(s.hasNextByte()); 3232 assertEquals(123, s.nextByte()); 3233 assertTrue(s.hasNextByte()); 3234 assertEquals(126, s.nextByte()); 3235 assertFalse(s.hasNextByte()); 3236 try { 3237 s.nextByte(); 3238 fail("Should throw NoSuchElementException"); 3239 } catch (NoSuchElementException e) { 3240 // Expected 3241 } 3242 3243 // If the radix is different from 10 3244 s = new Scanner("123 126"); 3245 s.useRadix(5); 3246 assertTrue(s.hasNextByte()); 3247 assertEquals(38, s.nextByte()); 3248 assertFalse(s.hasNextByte()); 3249 try { 3250 s.nextByte(); 3251 fail("Should throw InputMismatchException"); 3252 } catch (InputMismatchException e) { 3253 // Expected 3254 } 3255 3256 // If the number is out of range 3257 s = new Scanner("1234"); 3258 assertFalse(s.hasNextByte()); 3259 try { 3260 s.nextByte(); 3261 fail("Should throw InputMismatchException"); 3262 } catch (InputMismatchException e) { 3263 // Expected 3264 } 3265 3266 /* 3267 * The input string has Arabic-Indic digits. 3268 */ 3269 s = new Scanner("1\u06602 12\u0666"); 3270 assertTrue(s.hasNextByte()); 3271 assertEquals(102, s.nextByte()); 3272 s.useRadix(5); 3273 assertFalse(s.hasNextByte()); 3274 try { 3275 s.nextByte(); 3276 fail("Should throw InputMismatchException"); 3277 } catch (InputMismatchException e) { 3278 // Expected 3279 } 3280 s.useRadix(10); 3281 assertTrue(s.hasNextByte()); 3282 assertEquals(126, s.nextByte()); 3283 3284 s = new Scanner("012"); 3285 assertEquals(12, s.nextByte()); 3286 3287 s = new Scanner("E"); 3288 s.useRadix(16); 3289 assertTrue(s.hasNextByte()); 3290 assertEquals(14, s.nextByte()); 3291 3292 /* 3293 * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50' 3294 * respectively, but they are not differentiated. 3295 */ 3296 s = new Scanner("100"); 3297 s.useLocale(Locale.CHINESE); 3298 assertTrue(s.hasNextByte()); 3299 assertEquals(100, s.nextByte()); 3300 3301 s = new Scanner("1\u0966\u0966"); 3302 s.useLocale(Locale.CHINESE); 3303 assertTrue(s.hasNextByte()); 3304 assertEquals(100, s.nextByte()); 3305 3306 s = new Scanner("1\u0e50\u0e50"); 3307 s.useLocale(Locale.CHINESE); 3308 assertTrue(s.hasNextByte()); 3309 assertEquals(100, s.nextByte()); 3310 3311 s = new Scanner("-123"); 3312 s.useLocale(new Locale("ar", "AE")); 3313 assertTrue(s.hasNextByte()); 3314 assertEquals(-123, s.nextByte()); 3315 3316 s = new Scanner("-123"); 3317 s.useLocale(new Locale("mk", "MK")); 3318 assertTrue(s.hasNextByte()); 3319 assertEquals(-123, s.nextByte()); 3320 3321 s.close(); 3322 try { 3323 s.hasNextByte(); 3324 fail("IllegalStateException expected"); 3325 } catch (IllegalStateException e) { 3326 //expected 3327 } 3328 } 3329 3330 /** 3331 * @throws IOException 3332 * @tests java.util.Scanner#hasNextBigInteger(int) 3333 */ 3334 @TestTargetNew( 3335 level = TestLevel.PARTIAL_COMPLETE, 3336 notes = "IllegalStateException checking missed.", 3337 method = "hasNextBigInteger", 3338 args = {int.class} 3339 ) 3340 @KnownFailure("hasNextBigInteger method doesn't work properly if input string has Arabic-Indic digits") 3341 public void test_hasNextBigIntegerI() throws IOException { 3342 s = new Scanner("123 456"); 3343 assertTrue(s.hasNextBigInteger(10)); 3344 assertEquals(new BigInteger("123"), s.nextBigInteger(10)); 3345 assertTrue(s.hasNextBigInteger(10)); 3346 assertEquals(new BigInteger("456"), s.nextBigInteger(10)); 3347 assertFalse(s.hasNextBigInteger(10)); 3348 try { 3349 s.nextBigInteger(10); 3350 fail("Should throw NoSuchElementException"); 3351 } catch (NoSuchElementException e) { 3352 // Expected 3353 } 3354 3355 // If the radix is different from 10 3356 s = new Scanner("123 456"); 3357 assertTrue(s.hasNextBigInteger(5)); 3358 assertEquals(new BigInteger("38"), s.nextBigInteger(5)); 3359 assertFalse(s.hasNextBigInteger(5)); 3360 try { 3361 s.nextBigInteger(5); 3362 fail("Should throw InputMismatchException"); 3363 } catch (InputMismatchException e) { 3364 // Expected 3365 } 3366 3367 /* 3368 * Different locale can only recognize corresponding locale sensitive 3369 * string. ',' is used in many locales as group separator. 3370 */ 3371 s = new Scanner("23,456 23,456"); 3372 s.useLocale(Locale.GERMANY); 3373 assertFalse(s.hasNextBigInteger(10)); 3374 try { 3375 s.nextBigInteger(10); 3376 fail("Should throw InputMismatchException"); 3377 } catch (InputMismatchException e) { 3378 // Expected 3379 } 3380 s.useLocale(Locale.ENGLISH); 3381 // If exception is thrown out, input will not be advanced. 3382 assertTrue(s.hasNextBigInteger(10)); 3383 assertEquals(new BigInteger("23456"), s.nextBigInteger(10)); 3384 assertTrue(s.hasNextBigInteger(10)); 3385 assertEquals(new BigInteger("23456"), s.nextBigInteger(10)); 3386 3387 /* 3388 * ''' is used in many locales as group separator. 3389 */ 3390 s = new Scanner("23'456 23'456"); 3391 s.useLocale(Locale.GERMANY); 3392 assertFalse(s.hasNextBigInteger(10)); 3393 try { 3394 s.nextBigInteger(10); 3395 fail("Should throw InputMismatchException"); 3396 } catch (InputMismatchException e) { 3397 // Expected 3398 } 3399 s.useLocale(new Locale("de", "CH")); 3400 // If exception is thrown out, input will not be advanced. 3401 assertTrue(s.hasNextBigInteger(10)); 3402 assertEquals(new BigInteger("23456"), s.nextBigInteger(10)); 3403 assertTrue(s.hasNextBigInteger(10)); 3404 assertEquals(new BigInteger("23456"), s.nextBigInteger(10)); 3405 3406 /* 3407 * The input string has Arabic-Indic digits. 3408 */ 3409 s = new Scanner("1\u06602 1\u06662"); 3410 assertTrue(s.hasNextBigInteger(10)); 3411 assertEquals(new BigInteger("102"), s.nextBigInteger(10)); 3412 assertFalse(s.hasNextBigInteger(5)); 3413 try { 3414 s.nextBigInteger(5); 3415 fail("Should throw InputMismatchException"); 3416 } catch (InputMismatchException e) { 3417 // Expected 3418 } 3419 assertTrue(s.hasNextBigInteger(10)); 3420 assertEquals(new BigInteger("162"), s.nextBigInteger(10)); 3421 3422 /* 3423 * '.' is used in many locales as group separator. The input string 3424 * has Arabic-Indic digits . 3425 */ 3426 s = new Scanner("23.45\u0666 23.456"); 3427 s.useLocale(Locale.CHINESE); 3428 assertFalse(s.hasNextBigInteger(10)); 3429 try { 3430 s.nextBigInteger(10); 3431 fail("Should throw InputMismatchException"); 3432 } catch (InputMismatchException e) { 3433 // Expected 3434 } 3435 s.useLocale(Locale.GERMANY); 3436 // If exception is thrown out, input will not be advanced. 3437 assertTrue(s.hasNextBigInteger(10)); 3438 assertEquals(new BigInteger("23456"), s.nextBigInteger(10)); 3439 assertTrue(s.hasNextBigInteger(10)); 3440 assertEquals(new BigInteger("23456"), s.nextBigInteger(10)); 3441 3442 // The input string starts with zero 3443 s = new Scanner("03,456"); 3444 s.useLocale(Locale.ENGLISH); 3445 assertFalse(s.hasNextBigInteger(10)); 3446 try { 3447 s.nextBigInteger(10); 3448 fail("Should throw InputMismatchException"); 3449 } catch (InputMismatchException e) { 3450 // Expected 3451 } 3452 3453 s = new Scanner("03456"); 3454 assertTrue(s.hasNextBigInteger(10)); 3455 assertEquals(new BigInteger("3456"), s.nextBigInteger(10)); 3456 3457 s = new Scanner("\u06603,456"); 3458 s.useLocale(Locale.ENGLISH); 3459 assertTrue(s.hasNextBigInteger(10)); 3460 assertEquals(new BigInteger("3456"), s.nextBigInteger(10)); 3461 3462 s = new Scanner("E34"); 3463 assertTrue(s.hasNextBigInteger(16)); 3464 assertEquals(new BigInteger("3636"), s.nextBigInteger(16)); 3465 3466 /* 3467 * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50' 3468 * respectively, but they are not differentiated. 3469 */ 3470 s = new Scanner("12300"); 3471 s.useLocale(Locale.CHINESE); 3472 assertTrue(s.hasNextBigInteger(10)); 3473 assertEquals(new BigInteger("12300"), s.nextBigInteger(10)); 3474 3475 s = new Scanner("123\u0966\u0966"); 3476 s.useLocale(Locale.CHINESE); 3477 assertTrue(s.hasNextBigInteger(10)); 3478 assertEquals(new BigInteger("12300"), s.nextBigInteger(10)); 3479 3480 s = new Scanner("123\u0e50\u0e50"); 3481 s.useLocale(Locale.CHINESE); 3482 assertTrue(s.hasNextBigInteger(10)); 3483 assertEquals(new BigInteger("12300"), s.nextBigInteger(10)); 3484 3485 s = new Scanner("-123"); 3486 s.useLocale(new Locale("ar", "AE")); 3487 assertTrue(s.hasNextBigInteger(10)); 3488 assertEquals(new BigInteger("-123"), s.nextBigInteger(10)); 3489 3490 3491 s = new Scanner("-123"); 3492 s.useLocale(new Locale("mk", "MK")); 3493 assertTrue(s.hasNextBigInteger(10)); 3494 assertEquals(new BigInteger("-123"), s.nextBigInteger(10)); 3495 } 3496 3497 /** 3498 * @throws IOException 3499 * @tests java.util.Scanner#hasNextBigInteger(int) 3500 */ 3501 @TestTargetNew( 3502 level = TestLevel.PARTIAL_COMPLETE, 3503 notes = "Regression test.", 3504 method = "hasNextBigInteger", 3505 args = {int.class} 3506 ) 3507 public void test_hasNextBigIntegerI_cache() throws IOException { 3508 //regression for HARMONY-2063 3509 s = new Scanner("123 123456789123456789"); 3510 assertTrue(s.hasNextBigInteger(16)); 3511 assertEquals(new BigInteger("291"), s.nextBigInteger()); 3512 assertEquals(new BigInteger("123456789123456789"), s.nextBigInteger()); 3513 3514 s = new Scanner("123456789123456789 456"); 3515 assertTrue(s.hasNextBigInteger(16)); 3516 assertTrue(s.hasNextBigInteger(10)); 3517 assertEquals(new BigInteger("123456789123456789"), s.nextBigInteger()); 3518 assertEquals(new BigInteger("456"), s.nextBigInteger()); 3519 3520 s = new Scanner("-123 -123456789123456789"); 3521 assertTrue(s.hasNextBigInteger(8)); 3522 assertEquals(-123, s.nextShort()); 3523 assertEquals(new BigInteger("-123456789123456789"), s.nextBigInteger()); 3524 3525 s = new Scanner("123 456"); 3526 assertTrue(s.hasNextBigInteger()); 3527 s.close(); 3528 try { 3529 s.nextBigInteger(); 3530 fail("Should throw IllegalStateException"); 3531 } catch (IllegalStateException e) { 3532 // expected 3533 } 3534 } 3535 3536 /** 3537 * @throws IOException 3538 * @tests java.util.Scanner#hasNextBigInteger() 3539 */ 3540 @TestTargetNew( 3541 level = TestLevel.COMPLETE, 3542 notes = "", 3543 method = "hasNextBigInteger", 3544 args = {} 3545 ) 3546 @KnownFailure("nextBigInteger method doesn't work properly if input string has Arabic-Indic digits") 3547 public void test_hasNextBigInteger() throws IOException { 3548 s = new Scanner("123 456"); 3549 assertTrue(s.hasNextBigInteger()); 3550 assertEquals(new BigInteger("123"), s.nextBigInteger()); 3551 assertTrue(s.hasNextBigInteger()); 3552 assertEquals(new BigInteger("456"), s.nextBigInteger()); 3553 assertFalse(s.hasNextBigInteger()); 3554 try { 3555 s.nextBigInteger(); 3556 fail("Should throw NoSuchElementException"); 3557 } catch (NoSuchElementException e) { 3558 // Expected 3559 } 3560 3561 // If the radix is different from 10 3562 s = new Scanner("123 456"); 3563 s.useRadix(5); 3564 assertTrue(s.hasNextBigInteger()); 3565 assertEquals(new BigInteger("38"), s.nextBigInteger()); 3566 assertFalse(s.hasNextBigInteger()); 3567 try { 3568 s.nextBigInteger(); 3569 fail("Should throw InputMismatchException"); 3570 } catch (InputMismatchException e) { 3571 // Expected 3572 } 3573 3574 /* 3575 * Different locale can only recognize corresponding locale sensitive 3576 * string. ',' is used in many locales as group separator. 3577 */ 3578 s = new Scanner("23,456 23,456"); 3579 s.useLocale(Locale.GERMANY); 3580 assertFalse(s.hasNextBigInteger()); 3581 try { 3582 s.nextBigInteger(); 3583 fail("Should throw InputMismatchException"); 3584 } catch (InputMismatchException e) { 3585 // Expected 3586 } 3587 s.useLocale(Locale.ENGLISH); 3588 // If exception is thrown out, input will not be advanced. 3589 assertTrue(s.hasNextBigInteger()); 3590 assertEquals(new BigInteger("23456"), s.nextBigInteger()); 3591 assertTrue(s.hasNextBigInteger()); 3592 assertEquals(new BigInteger("23456"), s.nextBigInteger()); 3593 3594 /* 3595 * ''' is used in many locales as group separator. 3596 */ 3597 s = new Scanner("23'456 23'456"); 3598 s.useLocale(Locale.GERMANY); 3599 assertFalse(s.hasNextBigInteger()); 3600 try { 3601 s.nextBigInteger(); 3602 fail("Should throw InputMismatchException"); 3603 } catch (InputMismatchException e) { 3604 // Expected 3605 } 3606 s.useLocale(new Locale("de", "CH")); 3607 // If exception is thrown out, input will not be advanced. 3608 assertTrue(s.hasNextBigInteger()); 3609 assertEquals(new BigInteger("23456"), s.nextBigInteger()); 3610 assertTrue(s.hasNextBigInteger()); 3611 assertEquals(new BigInteger("23456"), s.nextBigInteger()); 3612 3613 /* 3614 * The input string has Arabic-Indic digits. 3615 */ 3616 s = new Scanner("1\u06602 1\u06662"); 3617 assertEquals(new BigInteger("102"), s.nextBigInteger()); 3618 s.useRadix(5); 3619 assertFalse(s.hasNextBigInteger()); 3620 try { 3621 s.nextBigInteger(); 3622 fail("Should throw InputMismatchException"); 3623 } catch (InputMismatchException e) { 3624 // Expected 3625 } 3626 s.useRadix(10); 3627 assertTrue(s.hasNextBigInteger()); 3628 assertEquals(new BigInteger("162"), s.nextBigInteger()); 3629 3630 /* 3631 * '.' is used in many locales as group separator. The input string 3632 * has Arabic-Indic digits . 3633 */ 3634 s = new Scanner("23.45\u0666 23.456"); 3635 s.useLocale(Locale.CHINESE); 3636 assertFalse(s.hasNextBigInteger()); 3637 try { 3638 s.nextBigInteger(); 3639 fail("Should throw InputMismatchException"); 3640 } catch (InputMismatchException e) { 3641 // Expected 3642 } 3643 s.useLocale(Locale.GERMANY); 3644 // If exception is thrown out, input will not be advanced. 3645 assertTrue(s.hasNextBigInteger()); 3646 assertEquals(new BigInteger("23456"), s.nextBigInteger()); 3647 assertTrue(s.hasNextBigInteger()); 3648 assertEquals(new BigInteger("23456"), s.nextBigInteger()); 3649 3650 // The input string starts with zero 3651 s = new Scanner("03,456"); 3652 s.useLocale(Locale.ENGLISH); 3653 assertFalse(s.hasNextBigInteger()); 3654 try { 3655 s.nextBigInteger(); 3656 fail("Should throw InputMismatchException"); 3657 } catch (InputMismatchException e) { 3658 // Expected 3659 } 3660 3661 s = new Scanner("03456"); 3662 assertTrue(s.hasNextBigInteger()); 3663 assertEquals(new BigInteger("3456"), s.nextBigInteger()); 3664 3665 s = new Scanner("\u06603,456"); 3666 s.useLocale(Locale.ENGLISH); 3667 assertTrue(s.hasNextBigInteger()); 3668 assertEquals(new BigInteger("3456"), s.nextBigInteger()); 3669 3670 s = new Scanner("E34"); 3671 s.useRadix(16); 3672 assertTrue(s.hasNextBigInteger()); 3673 assertEquals(new BigInteger("3636"), s.nextBigInteger()); 3674 3675 /* 3676 * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50' 3677 * respectively, but they are not differentiated. 3678 */ 3679 s = new Scanner("12300"); 3680 s.useLocale(Locale.CHINESE); 3681 assertTrue(s.hasNextBigInteger()); 3682 assertEquals(new BigInteger("12300"), s.nextBigInteger()); 3683 3684 s = new Scanner("123\u0966\u0966"); 3685 s.useLocale(Locale.CHINESE); 3686 assertTrue(s.hasNextBigInteger()); 3687 assertEquals(new BigInteger("12300"), s.nextBigInteger()); 3688 3689 s = new Scanner("123\u0e50\u0e50"); 3690 s.useLocale(Locale.CHINESE); 3691 assertTrue(s.hasNextBigInteger()); 3692 assertEquals(new BigInteger("12300"), s.nextBigInteger()); 3693 3694 s = new Scanner("-123"); 3695 s.useLocale(new Locale("ar", "AE")); 3696 assertTrue(s.hasNextBigInteger()); 3697 assertEquals(new BigInteger("-123"), s.nextBigInteger()); 3698 3699 s = new Scanner("-123"); 3700 s.useLocale(new Locale("mk", "MK")); 3701 assertTrue(s.hasNextBigInteger()); 3702 assertEquals(new BigInteger("-123"), s.nextBigInteger()); 3703 3704 s.close(); 3705 try { 3706 s.hasNextBigInteger(); 3707 fail("IllegalStateException expected"); 3708 } catch (IllegalStateException e) { 3709 //expected 3710 } 3711 } 3712 3713 /** 3714 * @throws IOException 3715 * @tests java.util.Scanner#hasNextInt(int) 3716 */ 3717 @TestTargetNew( 3718 level = TestLevel.PARTIAL_COMPLETE, 3719 notes = "IllegalStateException checking missed.", 3720 method = "hasNextInt", 3721 args = {int.class} 3722 ) 3723 @KnownFailure("Some locales were removed last minute in cupcake") 3724 public void test_hasNextIntI() throws IOException { 3725 s = new Scanner("123 456"); 3726 assertEquals(123, s.nextInt(10)); 3727 assertTrue(s.hasNextInt(10)); 3728 assertEquals(456, s.nextInt(10)); 3729 assertFalse(s.hasNextInt(10)); 3730 try { 3731 s.nextInt(10); 3732 fail("Should throw NoSuchElementException"); 3733 } catch (NoSuchElementException e) { 3734 // Expected 3735 } 3736 3737 // If the radix is different from 10 3738 s = new Scanner("123 456"); 3739 assertTrue(s.hasNextInt(5)); 3740 assertEquals(38, s.nextInt(5)); 3741 assertFalse(s.hasNextInt(5)); 3742 try { 3743 s.nextInt(5); 3744 fail("Should throw InputMismatchException"); 3745 } catch (InputMismatchException e) { 3746 // Expected 3747 } 3748 3749 // If the number is out of range 3750 s = new Scanner("123456789123456789123456789123456789"); 3751 assertFalse(s.hasNextInt(10)); 3752 3753 /* 3754 * Different locale can only recognize corresponding locale sensitive 3755 * string. ',' is used in many locales as group separator. 3756 */ 3757 s = new Scanner("23,456"); 3758 s.useLocale(Locale.GERMANY); 3759 assertFalse(s.hasNextInt(10)); 3760 s.useLocale(Locale.ENGLISH); 3761 // If exception is thrown out, input will not be advanced. 3762 assertTrue(s.hasNextInt(10)); 3763 /* 3764 * ''' is used in many locales as group separator. 3765 */ 3766 s = new Scanner("23'456"); 3767 s.useLocale(Locale.GERMANY); 3768 assertFalse(s.hasNextInt(10)); 3769 s.useLocale(new Locale("de", "CH")); 3770 // If exception is thrown out, input will not be advanced. 3771 assertTrue(s.hasNextInt(10)); 3772 3773 /* 3774 * The input string has Arabic-Indic digits. 3775 */ 3776 s = new Scanner("1\u06662"); 3777 assertTrue(s.hasNextInt(10)); 3778 assertFalse(s.hasNextInt(5)); 3779 3780 /* 3781 * '.' is used in many locales as group separator. The input string 3782 * has Arabic-Indic digits . 3783 */ 3784 s = new Scanner("23.45\u0666"); 3785 s.useLocale(Locale.CHINESE); 3786 assertFalse(s.hasNextInt(10)); 3787 try { 3788 s.nextInt(10); 3789 fail("Should throw InputMismatchException"); 3790 } catch (InputMismatchException e) { 3791 // expected 3792 } 3793 s.useLocale(Locale.GERMANY); 3794 assertTrue(s.hasNextInt(10)); 3795 3796 // The input string starts with zero 3797 s = new Scanner("03,456"); 3798 s.useLocale(Locale.ENGLISH); 3799 assertFalse(s.hasNextInt(10)); 3800 try { 3801 s.nextInt(10); 3802 fail("Should throw InputMismatchException"); 3803 } catch (InputMismatchException e) { 3804 // expected 3805 } 3806 3807 s = new Scanner("03456"); 3808 assertTrue(s.hasNextInt(10)); 3809 assertEquals(3456, s.nextInt(10)); 3810 3811 s = new Scanner("\u06603,456"); 3812 s.useLocale(Locale.ENGLISH); 3813 assertTrue(s.hasNextInt(10)); 3814 assertEquals(3456, s.nextInt(10)); 3815 3816 s = new Scanner("E3456"); 3817 assertTrue(s.hasNextInt(16)); 3818 assertEquals(930902, s.nextInt(16)); 3819 // The following test case fails on RI, because RI does not support 3820 // letter as leading digit 3821 s = new Scanner("E3,456"); 3822 s.useLocale(Locale.ENGLISH); 3823 if (!disableRIBugs) { 3824 assertTrue(s.hasNextInt(16)); 3825 assertEquals(930902, s.nextInt(16)); 3826 3827 // If parameter radix is illegal, the following test case fails on RI 3828 try { 3829 s.hasNextInt(Character.MIN_RADIX - 1); 3830 fail("Should throw IllegalArgumentException"); 3831 } catch (IllegalArgumentException e) { 3832 // Expected 3833 } 3834 } 3835 3836 /* 3837 * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50' 3838 * respectively, but they are not differentiated. 3839 */ 3840 s = new Scanner("12300"); 3841 s.useLocale(Locale.CHINESE); 3842 assertTrue(s.hasNextInt(10)); 3843 assertEquals(12300, s.nextInt(10)); 3844 3845 s = new Scanner("123\u0966\u0966"); 3846 s.useLocale(Locale.CHINESE); 3847 assertTrue(s.hasNextInt(10)); 3848 assertEquals(12300, s.nextInt(10)); 3849 3850 s = new Scanner("123\u0e50\u0e50"); 3851 s.useLocale(Locale.CHINESE); 3852 assertTrue(s.hasNextInt(10)); 3853 assertEquals(12300, s.nextInt(10)); 3854 3855 /* 3856 * There are three types of negative prefix all in all. '' '-' '(' There 3857 * are three types of negative suffix all in all. '' '-' ')' '(' and ')' 3858 * must be used togethor. Prefix '-' and suffix '-' must be used 3859 * exclusively. 3860 */ 3861 3862 /* 3863 * According to Integer regular expression: Integer :: = ( [-+]? (* 3864 * Numeral ) ) | LocalPositivePrefix Numeral LocalPositiveSuffix | 3865 * LocalNegativePrefix Numeral LocalNegativeSuffix 123- should be 3866 * recognized by scanner with locale ar_AE, (123) shouble be recognized 3867 * by scanner with locale mk_MK. But this is not the case on RI. 3868 */ 3869 s = new Scanner("-123 123- -123-"); 3870 s.useLocale(new Locale("ar", "AE")); 3871 assertTrue(s.hasNextInt(10)); 3872 assertEquals(-123, s.nextInt(10)); 3873 // The following test case fails on RI 3874 if (!disableRIBugs) { 3875 assertTrue(s.hasNextInt(10)); 3876 assertEquals(-123, s.nextInt(10)); 3877 } 3878 assertFalse(s.hasNextInt(10)); 3879 try { 3880 s.nextInt(10); 3881 fail("Should throw InputMismatchException"); 3882 } catch (InputMismatchException e) { 3883 // expected 3884 } 3885 3886 s = new Scanner("-123 123- (123)"); 3887 s.useLocale(new Locale("mk", "MK")); 3888 assertTrue(s.hasNextInt(10)); 3889 assertEquals(-123, s.nextInt(10)); 3890 assertFalse(s.hasNextInt(10)); 3891 try { 3892 s.nextInt(); 3893 fail("Should throw InputMismatchException"); 3894 } catch (InputMismatchException e) { 3895 // expected 3896 } 3897 // Skip the un-recognizable token 123-. 3898 assertEquals("123-", s.next()); 3899 // The following test case fails on RI 3900 if (!disableRIBugs) { 3901 assertTrue(s.hasNextInt(10)); 3902 assertEquals(-123, s.nextInt(10)); 3903 } 3904 } 3905 3906 /** 3907 * @throws IOException 3908 * @tests java.util.Scanner#hasNextInt(int) 3909 */ 3910 @TestTargetNew( 3911 level = TestLevel.PARTIAL_COMPLETE, 3912 notes = "Regression test", 3913 method = "hasNextInt", 3914 args = {int.class} 3915 ) 3916 public void test_hasNextIntI_cache() throws IOException { 3917 //regression for HARMONY-2063 3918 s = new Scanner("123 456"); 3919 assertTrue(s.hasNextInt(16)); 3920 assertEquals(291, s.nextInt(10)); 3921 assertEquals(456, s.nextInt()); 3922 3923 s = new Scanner("123 456"); 3924 assertTrue(s.hasNextInt(16)); 3925 assertTrue(s.hasNextInt(8)); 3926 assertEquals(83, s.nextInt()); 3927 assertEquals(456, s.nextInt()); 3928 3929 s = new Scanner("-123 -456 -789"); 3930 assertTrue(s.hasNextInt(8)); 3931 assertEquals(-123, s.nextShort()); 3932 assertEquals(-456, s.nextInt()); 3933 assertTrue(s.hasNextShort(16)); 3934 assertEquals(-789, s.nextInt()); 3935 3936 s = new Scanner("123 456"); 3937 assertTrue(s.hasNextInt()); 3938 s.close(); 3939 try { 3940 s.nextInt(); 3941 fail("Should throw IllegalStateException"); 3942 } catch (IllegalStateException e) { 3943 // expected 3944 } 3945 } 3946 /** 3947 * @throws IOException 3948 * @tests java.util.Scanner#hasNextInt() 3949 */ 3950 @TestTargetNew( 3951 level = TestLevel.COMPLETE, 3952 notes = "", 3953 method = "hasNextInt", 3954 args = {} 3955 ) 3956 @KnownFailure("Some locales were removed last minute in cupcake") 3957 public void test_hasNextInt() throws IOException { 3958 s = new Scanner("123 456"); 3959 assertTrue(s.hasNextInt()); 3960 assertEquals(123, s.nextInt()); 3961 assertEquals(456, s.nextInt()); 3962 assertFalse(s.hasNextInt()); 3963 try { 3964 s.nextInt(); 3965 fail("Should throw NoSuchElementException"); 3966 } catch (NoSuchElementException e) { 3967 // Expected 3968 } 3969 3970 // If the radix is different from 10 3971 s = new Scanner("123 456"); 3972 s.useRadix(5); 3973 assertTrue(s.hasNextInt()); 3974 assertEquals(38, s.nextInt()); 3975 assertFalse(s.hasNextInt()); 3976 try { 3977 s.nextInt(); 3978 fail("Should throw InputMismatchException"); 3979 } catch (InputMismatchException e) { 3980 // Expected 3981 } 3982 3983 // If the number is out of range 3984 s = new Scanner("123456789123456789123456789123456789"); 3985 assertFalse(s.hasNextInt()); 3986 3987 /* 3988 * Different locale can only recognize corresponding locale sensitive 3989 * string. ',' is used in many locales as group separator. 3990 */ 3991 s = new Scanner("23,456"); 3992 s.useLocale(Locale.GERMANY); 3993 assertFalse(s.hasNextInt()); 3994 s.useLocale(Locale.ENGLISH); 3995 assertTrue(s.hasNextInt()); 3996 3997 /* 3998 * ''' is used in many locales as group separator. 3999 */ 4000 s = new Scanner("23'456"); 4001 s.useLocale(Locale.GERMANY); 4002 assertFalse(s.hasNextInt()); 4003 s.useLocale(new Locale("de", "CH")); 4004 assertTrue(s.hasNextInt()); 4005 4006 /* 4007 * The input string has Arabic-Indic digits. 4008 */ 4009 s = new Scanner("1\u06662"); 4010 s.useRadix(5); 4011 assertFalse(s.hasNextInt()); 4012 4013 /* 4014 * '.' is used in many locales as group separator. The input string 4015 * has Arabic-Indic digits . 4016 */ 4017 s = new Scanner("23.45\u0666"); 4018 s.useLocale(Locale.CHINESE); 4019 assertFalse(s.hasNextInt()); 4020 s.useLocale(Locale.GERMANY); 4021 assertTrue(s.hasNextInt()); 4022 4023 // The input string starts with zero 4024 s = new Scanner("03,456"); 4025 s.useLocale(Locale.ENGLISH); 4026 assertFalse(s.hasNextInt()); 4027 try { 4028 s.nextInt(); 4029 fail("Should throw InputMismatchException"); 4030 } catch (InputMismatchException e) { 4031 // expected 4032 } 4033 4034 s = new Scanner("03456"); 4035 assertTrue(s.hasNextInt()); 4036 assertEquals(3456, s.nextInt()); 4037 4038 s = new Scanner("\u06603,456"); 4039 s.useLocale(Locale.ENGLISH); 4040 assertEquals(3456, s.nextInt()); 4041 4042 s = new Scanner("E3456"); 4043 s.useRadix(16); 4044 assertTrue(s.hasNextInt()); 4045 assertEquals(930902, s.nextInt()); 4046 4047 // The following test case fails on RI, because RI does not support 4048 // letter as leading digit 4049 s = new Scanner("E3,456"); 4050 s.useLocale(Locale.ENGLISH); 4051 s.useRadix(16); 4052 if (!disableRIBugs) { 4053 assertTrue(s.hasNextInt()); 4054 assertEquals(930902, s.nextInt()); 4055 } 4056 4057 /* 4058 * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50' 4059 * respectively, but they are not differentiated. 4060 */ 4061 s = new Scanner("12300"); 4062 s.useLocale(Locale.CHINESE); 4063 assertTrue(s.hasNextInt()); 4064 assertEquals(12300, s.nextInt()); 4065 4066 s = new Scanner("123\u0966\u0966"); 4067 s.useLocale(Locale.CHINESE); 4068 assertTrue(s.hasNextInt()); 4069 assertEquals(12300, s.nextInt()); 4070 4071 s = new Scanner("123\u0e50\u0e50"); 4072 s.useLocale(Locale.CHINESE); 4073 assertTrue(s.hasNextInt()); 4074 assertEquals(12300, s.nextInt()); 4075 4076 /* 4077 * There are three types of negative prefix all in all. '' '-' '(' There 4078 * are three types of negative suffix all in all. '' '-' ')' '(' and ')' 4079 * must be used togethor. Prefix '-' and suffix '-' must be used 4080 * exclusively. 4081 */ 4082 4083 /* 4084 * According to Integer regular expression: Integer :: = ( [-+]? (* 4085 * Numeral ) ) | LocalPositivePrefix Numeral LocalPositiveSuffix | 4086 * LocalNegativePrefix Numeral LocalNegativeSuffix 123- should be 4087 * recognized by scanner with locale ar_AE, (123) shouble be recognized 4088 * by scanner with locale mk_MK. But this is not the case on RI. 4089 */ 4090 s = new Scanner("-123 123- -123-"); 4091 s.useLocale(new Locale("ar", "AE")); 4092 assertTrue(s.hasNextInt()); 4093 assertEquals(-123, s.nextInt()); 4094 // The following test case fails on RI 4095 if (!disableRIBugs) { 4096 assertTrue(s.hasNextInt()); 4097 assertEquals(-123, s.nextInt()); 4098 } 4099 assertFalse(s.hasNextInt()); 4100 try { 4101 s.nextInt(); 4102 fail("Should throw InputMismatchException"); 4103 } catch (InputMismatchException e) { 4104 // expected 4105 } 4106 4107 s = new Scanner("-123 123- (123)"); 4108 s.useLocale(new Locale("mk", "MK")); 4109 assertTrue(s.hasNextInt()); 4110 assertEquals(-123, s.nextInt()); 4111 try { 4112 s.nextInt(); 4113 fail("Should throw InputMismatchException"); 4114 } catch (InputMismatchException e) { 4115 // expected 4116 } 4117 // Skip the un-recognizable token 123-. 4118 assertEquals("123-", s.next()); 4119 // The following test case fails on RI 4120 if (!disableRIBugs) { 4121 assertTrue(s.hasNextInt()); 4122 assertEquals(-123, s.nextInt()); 4123 } 4124 4125 s.close(); 4126 try { 4127 s.hasNextInt(); 4128 fail("IllegalStateException expected"); 4129 } catch (IllegalStateException e) { 4130 //expected 4131 } 4132 } 4133 4134 /** 4135 * @throws IOException 4136 * @tests java.util.Scanner#hasNextFloat() 4137 */ 4138 @TestTargetNew( 4139 level = TestLevel.COMPLETE, 4140 notes = "", 4141 method = "hasNextFloat", 4142 args = {} 4143 ) 4144 @KnownFailure("Some locales were removed last minute in cupcake") 4145 public void test_hasNextFloat() throws IOException { 4146 s = new Scanner("123 45\u0666. 123.4 .123 "); 4147 s.useLocale(Locale.ENGLISH); 4148 assertTrue(s.hasNextFloat()); 4149 assertEquals((float)123.0, s.nextFloat()); 4150 assertTrue(s.hasNextFloat()); 4151 assertEquals((float)456.0, s.nextFloat()); 4152 assertTrue(s.hasNextFloat()); 4153 assertEquals((float)123.4, s.nextFloat()); 4154 assertTrue(s.hasNextFloat()); 4155 assertEquals((float)0.123, s.nextFloat()); 4156 assertFalse(s.hasNextFloat()); 4157 try { 4158 s.nextFloat(); 4159 fail("Should throw NoSuchElementException"); 4160 } catch (NoSuchElementException e) { 4161 // Expected 4162 } 4163 4164 s = new Scanner("+123.4 -456.7 123,456.789 0.1\u06623,4"); 4165 s.useLocale(Locale.ENGLISH); 4166 assertTrue(s.hasNextFloat()); 4167 assertEquals((float)123.4, s.nextFloat()); 4168 assertTrue(s.hasNextFloat()); 4169 assertEquals((float)-456.7, s.nextFloat()); 4170 assertTrue(s.hasNextFloat()); 4171 assertEquals((float)123456.789, s.nextFloat()); 4172 assertFalse(s.hasNextFloat()); 4173 try { 4174 s.nextFloat(); 4175 fail("Should throw InputMismatchException"); 4176 } catch (InputMismatchException e) { 4177 // Expected 4178 } 4179 4180 // Scientific notation 4181 s = new Scanner("+123.4E10 -456.7e+12 123,456.789E-10"); 4182 s.useLocale(Locale.ENGLISH); 4183 assertTrue(s.hasNextFloat()); 4184 assertEquals((float)1.234E12, s.nextFloat()); 4185 assertTrue(s.hasNextFloat()); 4186 assertEquals((float)-4.567E14, s.nextFloat()); 4187 assertTrue(s.hasNextFloat()); 4188 assertEquals((float)1.23456789E-5, s.nextFloat()); 4189 4190 s = new Scanner("NaN Infinity -Infinity"); 4191 assertTrue(s.hasNextFloat()); 4192 assertEquals(Float.NaN, s.nextFloat()); 4193 assertTrue(s.hasNextFloat()); 4194 assertEquals(Float.POSITIVE_INFINITY, s.nextFloat()); 4195 assertTrue(s.hasNextFloat()); 4196 assertEquals(Float.NEGATIVE_INFINITY, s.nextFloat()); 4197 4198 String str=String.valueOf(Float.MAX_VALUE*2); 4199 s=new Scanner(str); 4200 assertTrue(s.hasNextFloat()); 4201 assertEquals(Float.POSITIVE_INFINITY,s.nextFloat()); 4202 4203 /* 4204 * Different locale can only recognize corresponding locale sensitive 4205 * string. ',' is used in many locales as group separator. 4206 */ 4207 s = new Scanner("23,456 23,456"); 4208 s.useLocale(Locale.ENGLISH); 4209 assertTrue(s.hasNextFloat()); 4210 assertEquals((float)23456.0, s.nextFloat()); 4211 s.useLocale(Locale.GERMANY); 4212 assertTrue(s.hasNextFloat()); 4213 assertEquals((float)23.456, s.nextFloat()); 4214 4215 s = new Scanner("23.456 23.456"); 4216 s.useLocale(Locale.ENGLISH); 4217 assertTrue(s.hasNextFloat()); 4218 assertEquals((float)23.456, s.nextFloat()); 4219 s.useLocale(Locale.GERMANY); 4220 assertTrue(s.hasNextFloat()); 4221 assertEquals((float)23456.0, s.nextFloat()); 4222 4223 s = new Scanner("23,456.7 23.456,7"); 4224 s.useLocale(Locale.ENGLISH); 4225 assertTrue(s.hasNextFloat()); 4226 assertEquals((float)23456.7, s.nextFloat()); 4227 s.useLocale(Locale.GERMANY); 4228 assertTrue(s.hasNextFloat()); 4229 assertEquals((float)23456.7, s.nextFloat()); 4230 4231 s = new Scanner("-123.4 123.4- -123.4-"); 4232 s.useLocale(new Locale("ar", "AE")); 4233 assertTrue(s.hasNextFloat()); 4234 assertEquals((float)-123.4, s.nextFloat()); 4235 //The following test case fails on RI 4236 if (!disableRIBugs) { 4237 assertTrue(s.hasNextFloat()); 4238 assertEquals((float)-123.4, s.nextFloat()); 4239 } 4240 try { 4241 s.nextFloat(); 4242 fail("Should throw InputMismatchException"); 4243 } catch (InputMismatchException e) { 4244 // Expected 4245 } 4246 4247 s = new Scanner("(123) 123- -123"); 4248 s.useLocale(new Locale("mk", "MK")); 4249 if (!disableRIBugs) { 4250 assertTrue(s.hasNextFloat()); 4251 assertEquals((float)-123.0, s.nextFloat()); 4252 } 4253 assertFalse(s.hasNextFloat()); 4254 try { 4255 s.nextFloat(); 4256 fail("Should throw InputMismatchException"); 4257 } catch (InputMismatchException e) { 4258 // Expected 4259 } 4260 // Skip the un-recognizable token 123-. 4261 if (!disableRIBugs) { 4262 assertEquals("123-", s.next()); 4263 assertTrue(s.hasNextFloat()); 4264 assertEquals((float)-123.0, s.nextFloat()); 4265 } 4266 4267 s = new Scanner("+123.4 -456.7"); 4268 s.useLocale(Locale.ENGLISH); 4269 assertTrue(s.hasNextFloat()); 4270 s.close(); 4271 try{ 4272 s.nextFloat(); 4273 fail("Should throw IllegalStateException"); 4274 }catch(IllegalStateException e){ 4275 //expected 4276 } 4277 4278 } 4279 4280 /** 4281 * @throws IOException 4282 * @tests java.util.Scanner#hasNextShort(int) 4283 */ 4284 @TestTargetNew( 4285 level = TestLevel.PARTIAL_COMPLETE, 4286 notes = "IllegalStateException checking missed.", 4287 method = "hasNextShort", 4288 args = {int.class} 4289 ) 4290 @KnownFailure("Some locales were removed last minute in cupcake") 4291 public void test_hasNextShortI() throws IOException { 4292 s = new Scanner("123 456"); 4293 assertTrue(s.hasNextShort(10)); 4294 assertEquals(123, s.nextShort(10)); 4295 assertTrue(s.hasNextShort(10)); 4296 assertEquals(456, s.nextShort(10)); 4297 assertFalse(s.hasNextShort(10)); 4298 try { 4299 s.nextShort(10); 4300 fail("Should throw NoSuchElementException"); 4301 } catch (NoSuchElementException e) { 4302 // Expected 4303 } 4304 4305 // If the radix is different from 10 4306 s = new Scanner("123 456"); 4307 assertTrue(s.hasNextShort(5)); 4308 assertEquals(38, s.nextShort(5)); 4309 assertFalse(s.hasNextShort(5)); 4310 try { 4311 s.nextShort(5); 4312 fail("Should throw InputMismatchException"); 4313 } catch (InputMismatchException e) { 4314 // Expected 4315 } 4316 4317 // If the number is out of range 4318 s = new Scanner("123456789"); 4319 assertFalse(s.hasNextShort(10)); 4320 try { 4321 s.nextShort(10); 4322 fail("Should throw InputMismatchException"); 4323 } catch (InputMismatchException e) { 4324 // Expected 4325 } 4326 4327 /* 4328 * Different locale can only recognize corresponding locale sensitive 4329 * string. ',' is used in many locales as group separator. 4330 */ 4331 s = new Scanner("23,456 23,456"); 4332 s.useLocale(Locale.GERMANY); 4333 assertFalse(s.hasNextShort(10)); 4334 try { 4335 s.nextShort(10); 4336 fail("Should throw InputMismatchException"); 4337 } catch (InputMismatchException e) { 4338 // Expected 4339 } 4340 s.useLocale(Locale.ENGLISH); 4341 // If exception is thrown out, input will not be advanced. 4342 assertTrue(s.hasNextShort(10)); 4343 assertEquals(23456, s.nextInt(10)); 4344 assertTrue(s.hasNextShort(10)); 4345 assertEquals(23456, s.nextInt(10)); 4346 4347 /* 4348 * ''' is used in many locales as group separator. 4349 */ 4350 s = new Scanner("23'456 23'456"); 4351 s.useLocale(Locale.GERMANY); 4352 assertFalse(s.hasNextShort(10)); 4353 try { 4354 s.nextShort(10); 4355 fail("Should throw InputMismatchException"); 4356 } catch (InputMismatchException e) { 4357 // Expected 4358 } 4359 s.useLocale(new Locale("de", "CH")); 4360 // If exception is thrown out, input will not be advanced. 4361 assertTrue(s.hasNextShort(10)); 4362 assertEquals(23456, s.nextShort(10)); 4363 assertTrue(s.hasNextShort(10)); 4364 assertEquals(23456, s.nextShort(10)); 4365 4366 /* 4367 * The input string has Arabic-Indic digits. 4368 */ 4369 s = new Scanner("1\u06602 1\u06662"); 4370 assertTrue(s.hasNextShort(10)); 4371 assertEquals(102, s.nextShort(10)); 4372 assertFalse(s.hasNextShort(5)); 4373 try { 4374 s.nextShort(5); 4375 fail("Should throw InputMismatchException"); 4376 } catch (InputMismatchException e) { 4377 // Expected 4378 } 4379 assertTrue(s.hasNextShort(10)); 4380 assertEquals(162, s.nextShort(10)); 4381 4382 /* 4383 * '.' is used in many locales as group separator. The input string 4384 * has Arabic-Indic digits . 4385 */ 4386 s = new Scanner("23.45\u0666 23.456"); 4387 s.useLocale(Locale.CHINESE); 4388 assertFalse(s.hasNextShort(10)); 4389 try { 4390 s.nextShort(10); 4391 fail("Should throw InputMismatchException"); 4392 } catch (InputMismatchException e) { 4393 // Expected 4394 } 4395 s.useLocale(Locale.GERMANY); 4396 // If exception is thrown out, input will not be advanced. 4397 assertTrue(s.hasNextShort(10)); 4398 assertEquals(23456, s.nextShort(10)); 4399 assertTrue(s.hasNextShort(10)); 4400 assertEquals(23456, s.nextShort(10)); 4401 4402 // The input string starts with zero 4403 s = new Scanner("03,456"); 4404 s.useLocale(Locale.ENGLISH); 4405 assertFalse(s.hasNextShort(10)); 4406 try { 4407 s.nextShort(10); 4408 fail("Should throw InputMismatchException"); 4409 } catch (InputMismatchException e) { 4410 // Expected 4411 } 4412 4413 s = new Scanner("03456"); 4414 assertTrue(s.hasNextShort(10)); 4415 assertEquals(3456, s.nextShort(10)); 4416 4417 s = new Scanner("\u06603,456"); 4418 s.useLocale(Locale.ENGLISH); 4419 assertTrue(s.hasNextShort(10)); 4420 assertEquals(3456, s.nextShort(10)); 4421 4422 s = new Scanner("E34"); 4423 assertTrue(s.hasNextShort(16)); 4424 assertEquals(3636, s.nextShort(16)); 4425 4426 /* 4427 * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50' 4428 * respectively, but they are not differentiated. 4429 */ 4430 s = new Scanner("12300"); 4431 s.useLocale(Locale.CHINESE); 4432 assertTrue(s.hasNextShort(10)); 4433 assertEquals(12300, s.nextShort(10)); 4434 4435 s = new Scanner("123\u0966\u0966"); 4436 s.useLocale(Locale.CHINESE); 4437 assertTrue(s.hasNextShort(10)); 4438 assertEquals(12300, s.nextShort(10)); 4439 4440 s = new Scanner("123\u0e50\u0e50"); 4441 s.useLocale(Locale.CHINESE); 4442 assertTrue(s.hasNextShort(10)); 4443 assertEquals(12300, s.nextShort(10)); 4444 4445 s = new Scanner("-123"); 4446 s.useLocale(new Locale("ar", "AE")); 4447 assertTrue(s.hasNextShort(10)); 4448 assertEquals(-123, s.nextShort(10)); 4449 4450 4451 s = new Scanner("-123"); 4452 s.useLocale(new Locale("mk", "MK")); 4453 assertTrue(s.hasNextShort(10)); 4454 assertEquals(-123, s.nextShort(10)); 4455 } 4456 4457 /** 4458 * @throws IOException 4459 * @tests java.util.Scanner#hasNextShort() 4460 */ 4461 @TestTargetNew( 4462 level = TestLevel.COMPLETE, 4463 notes = "", 4464 method = "hasNextShort", 4465 args = {} 4466 ) 4467 @KnownFailure("Some locales were removed last minute in cupcake") 4468 public void test_hasNextShort() throws IOException { 4469 s = new Scanner("123 456"); 4470 assertTrue(s.hasNextShort()); 4471 assertEquals(123, s.nextShort()); 4472 assertTrue(s.hasNextShort()); 4473 assertEquals(456, s.nextShort()); 4474 assertFalse(s.hasNextShort()); 4475 try { 4476 s.nextShort(); 4477 fail("Should throw NoSuchElementException"); 4478 } catch (NoSuchElementException e) { 4479 // Expected 4480 } 4481 4482 // If the radix is different from 10 4483 s = new Scanner("123 456"); 4484 s.useRadix(5); 4485 assertTrue(s.hasNextShort()); 4486 assertEquals(38, s.nextShort()); 4487 assertFalse(s.hasNextShort()); 4488 try { 4489 s.nextShort(); 4490 fail("Should throw InputMismatchException"); 4491 } catch (InputMismatchException e) { 4492 // Expected 4493 } 4494 4495 // If the number is out of range 4496 s = new Scanner("123456789"); 4497 assertFalse(s.hasNextShort()); 4498 try { 4499 s.nextShort(); 4500 fail("Should throw InputMismatchException"); 4501 } catch (InputMismatchException e) { 4502 // Expected 4503 } 4504 4505 /* 4506 * Different locale can only recognize corresponding locale sensitive 4507 * string. ',' is used in many locales as group separator. 4508 */ 4509 s = new Scanner("23,456 23,456"); 4510 s.useLocale(Locale.GERMANY); 4511 assertFalse(s.hasNextShort()); 4512 try { 4513 s.nextShort(); 4514 fail("Should throw InputMismatchException"); 4515 } catch (InputMismatchException e) { 4516 // Expected 4517 } 4518 s.useLocale(Locale.ENGLISH); 4519 // If exception is thrown out, input will not be advanced. 4520 assertTrue(s.hasNextShort()); 4521 assertEquals(23456, s.nextShort()); 4522 assertTrue(s.hasNextShort()); 4523 assertEquals(23456, s.nextShort()); 4524 4525 /* 4526 * ''' is used in many locales as group separator. 4527 */ 4528 s = new Scanner("23'456 23'456"); 4529 s.useLocale(Locale.GERMANY); 4530 assertFalse(s.hasNextShort()); 4531 try { 4532 s.nextShort(); 4533 fail("Should throw InputMismatchException"); 4534 } catch (InputMismatchException e) { 4535 // Expected 4536 } 4537 s.useLocale(new Locale("de", "CH")); 4538 // If exception is thrown out, input will not be advanced. 4539 assertTrue(s.hasNextShort()); 4540 assertEquals(23456, s.nextShort()); 4541 assertTrue(s.hasNextShort()); 4542 assertEquals(23456, s.nextShort()); 4543 4544 /* 4545 * The input string has Arabic-Indic digits. 4546 */ 4547 s = new Scanner("1\u06602 1\u06662"); 4548 assertEquals(102, s.nextShort()); 4549 s.useRadix(5); 4550 assertFalse(s.hasNextShort()); 4551 try { 4552 s.nextShort(); 4553 fail("Should throw InputMismatchException"); 4554 } catch (InputMismatchException e) { 4555 // Expected 4556 } 4557 s.useRadix(10); 4558 assertTrue(s.hasNextShort()); 4559 assertEquals(162, s.nextShort()); 4560 4561 /* 4562 * '.' is used in many locales as group separator. The input string 4563 * has Arabic-Indic digits . 4564 */ 4565 s = new Scanner("23.45\u0666 23.456"); 4566 s.useLocale(Locale.CHINESE); 4567 assertFalse(s.hasNextShort()); 4568 try { 4569 s.nextShort(); 4570 fail("Should throw InputMismatchException"); 4571 } catch (InputMismatchException e) { 4572 // Expected 4573 } 4574 s.useLocale(Locale.GERMANY); 4575 // If exception is thrown out, input will not be advanced. 4576 assertTrue(s.hasNextShort()); 4577 assertEquals(23456, s.nextShort()); 4578 assertTrue(s.hasNextShort()); 4579 assertEquals(23456, s.nextShort()); 4580 4581 // The input string starts with zero 4582 s = new Scanner("03,456"); 4583 s.useLocale(Locale.ENGLISH); 4584 assertFalse(s.hasNextShort()); 4585 try { 4586 s.nextShort(); 4587 fail("Should throw InputMismatchException"); 4588 } catch (InputMismatchException e) { 4589 // Expected 4590 } 4591 4592 s = new Scanner("03456"); 4593 assertTrue(s.hasNextShort()); 4594 assertEquals(3456, s.nextShort()); 4595 4596 s = new Scanner("\u06603,456"); 4597 s.useLocale(Locale.ENGLISH); 4598 assertTrue(s.hasNextShort()); 4599 assertEquals(3456, s.nextShort()); 4600 4601 s = new Scanner("E34"); 4602 s.useRadix(16); 4603 assertTrue(s.hasNextShort()); 4604 assertEquals(3636, s.nextShort()); 4605 4606 /* 4607 * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50' 4608 * respectively, but they are not differentiated. 4609 */ 4610 s = new Scanner("12300"); 4611 s.useLocale(Locale.CHINESE); 4612 assertTrue(s.hasNextShort()); 4613 assertEquals(12300, s.nextShort()); 4614 4615 s = new Scanner("123\u0966\u0966"); 4616 s.useLocale(Locale.CHINESE); 4617 assertTrue(s.hasNextShort()); 4618 assertEquals(12300, s.nextShort()); 4619 4620 s = new Scanner("123\u0e50\u0e50"); 4621 s.useLocale(Locale.CHINESE); 4622 assertTrue(s.hasNextShort()); 4623 assertEquals(12300, s.nextShort()); 4624 4625 s = new Scanner("-123"); 4626 s.useLocale(new Locale("ar", "AE")); 4627 assertTrue(s.hasNextShort()); 4628 assertEquals(-123, s.nextShort()); 4629 4630 s = new Scanner("-123"); 4631 s.useLocale(new Locale("mk", "MK")); 4632 assertTrue(s.hasNextShort()); 4633 assertEquals(-123, s.nextShort()); 4634 4635 s.close(); 4636 try { 4637 s.hasNextShort(); 4638 fail("IllegalStateException expected"); 4639 } catch (IllegalStateException e) { 4640 //expected 4641 } 4642 } 4643 4644 /** 4645 * @throws IOException 4646 * @tests java.util.Scanner#hasNextShort(int) 4647 */ 4648 @TestTargetNew( 4649 level = TestLevel.PARTIAL_COMPLETE, 4650 notes = "Regression test.", 4651 method = "hasNextShort", 4652 args = {int.class} 4653 ) 4654 public void test_hasNextShortI_cache() throws IOException { 4655 //regression for HARMONY-2063 4656 s = new Scanner("123 456"); 4657 assertTrue(s.hasNextShort(16)); 4658 assertEquals(291, s.nextShort()); 4659 assertEquals(456, s.nextShort()); 4660 4661 s = new Scanner("123 456"); 4662 assertTrue(s.hasNextShort(16)); 4663 assertTrue(s.hasNextShort(8)); 4664 assertEquals(83, s.nextShort()); 4665 assertEquals(456, s.nextShort()); 4666 4667 s = new Scanner("-123 -456 -789"); 4668 assertTrue(s.hasNextShort(8)); 4669 assertEquals(-123, s.nextInt()); 4670 assertEquals(-456, s.nextShort()); 4671 assertTrue(s.hasNextInt(16)); 4672 assertEquals(-789, s.nextShort()); 4673 4674 s = new Scanner("123 456"); 4675 assertTrue(s.hasNextShort()); 4676 s.close(); 4677 try { 4678 s.nextShort(); 4679 fail("Should throw IllegalStateException"); 4680 } catch (IllegalStateException e) { 4681 // expected 4682 } 4683 } 4684 4685 /** 4686 * @throws IOException 4687 * @tests java.util.Scanner#hasNextLong(int) 4688 */ 4689 @TestTargetNew( 4690 level = TestLevel.PARTIAL_COMPLETE, 4691 notes = "IllegalStateException checking missed.", 4692 method = "hasNextLong", 4693 args = {int.class} 4694 ) 4695 @KnownFailure("Some locales were removed last minute in cupcake") 4696 public void test_hasNextLongI() throws IOException { 4697 s = new Scanner("123 456"); 4698 assertTrue(s.hasNextLong(10)); 4699 assertEquals(123, s.nextLong(10)); 4700 assertTrue(s.hasNextLong(10)); 4701 assertEquals(456, s.nextLong(10)); 4702 assertFalse(s.hasNextLong(10)); 4703 try { 4704 s.nextLong(10); 4705 fail("Should throw NoSuchElementException"); 4706 } catch (NoSuchElementException e) { 4707 // Expected 4708 } 4709 4710 // If the radix is different from 10 4711 s = new Scanner("123 456"); 4712 assertTrue(s.hasNextLong(5)); 4713 assertEquals(38, s.nextLong(5)); 4714 assertFalse(s.hasNextLong(5)); 4715 try { 4716 s.nextLong(5); 4717 fail("Should throw InputMismatchException"); 4718 } catch (InputMismatchException e) { 4719 // Expected 4720 } 4721 4722 // If the number is out of range 4723 s = new Scanner("123456789123456789123456789123456789"); 4724 assertFalse(s.hasNextLong(10)); 4725 try { 4726 s.nextLong(10); 4727 fail("Should throw InputMismatchException"); 4728 } catch (InputMismatchException e) { 4729 // Expected 4730 } 4731 4732 /* 4733 * Different locale can only recognize corresponding locale sensitive 4734 * string. ',' is used in many locales as group separator. 4735 */ 4736 s = new Scanner("23,456 23,456"); 4737 s.useLocale(Locale.GERMANY); 4738 assertFalse(s.hasNextShort(10)); 4739 try { 4740 s.nextLong(10); 4741 fail("Should throw InputMismatchException"); 4742 } catch (InputMismatchException e) { 4743 // Expected 4744 } 4745 s.useLocale(Locale.ENGLISH); 4746 // If exception is thrown out, input will not be advanced. 4747 assertTrue(s.hasNextLong(10)); 4748 assertEquals(23456, s.nextLong(10)); 4749 assertTrue(s.hasNextLong(10)); 4750 assertEquals(23456, s.nextLong(10)); 4751 4752 /* 4753 * ''' is used in many locales as group separator. 4754 */ 4755 s = new Scanner("23'456 23'456"); 4756 s.useLocale(Locale.GERMANY); 4757 assertFalse(s.hasNextLong(10)); 4758 try { 4759 s.nextLong(10); 4760 fail("Should throw InputMismatchException"); 4761 } catch (InputMismatchException e) { 4762 // Expected 4763 } 4764 s.useLocale(new Locale("de", "CH")); 4765 // If exception is thrown out, input will not be advanced. 4766 assertTrue(s.hasNextLong(10)); 4767 assertEquals(23456, s.nextLong(10)); 4768 assertTrue(s.hasNextLong(10)); 4769 assertEquals(23456, s.nextLong(10)); 4770 4771 /* 4772 * The input string has Arabic-Indic digits. 4773 */ 4774 s = new Scanner("1\u06602 1\u06662"); 4775 assertTrue(s.hasNextLong(10)); 4776 assertEquals(102, s.nextLong(10)); 4777 assertFalse(s.hasNextLong(5)); 4778 try { 4779 s.nextLong(5); 4780 fail("Should throw InputMismatchException"); 4781 } catch (InputMismatchException e) { 4782 // Expected 4783 } 4784 assertTrue(s.hasNextLong(10)); 4785 assertEquals(162, s.nextLong(10)); 4786 4787 /* 4788 * '.' is used in many locales as group separator. The input string 4789 * has Arabic-Indic digits . 4790 */ 4791 s = new Scanner("23.45\u0666 23.456"); 4792 s.useLocale(Locale.CHINESE); 4793 assertFalse(s.hasNextLong(10)); 4794 try { 4795 s.nextLong(10); 4796 fail("Should throw InputMismatchException"); 4797 } catch (InputMismatchException e) { 4798 // Expected 4799 } 4800 s.useLocale(Locale.GERMANY); 4801 // If exception is thrown out, input will not be advanced. 4802 assertTrue(s.hasNextLong(10)); 4803 assertEquals(23456, s.nextLong(10)); 4804 assertTrue(s.hasNextLong(10)); 4805 assertEquals(23456, s.nextLong(10)); 4806 4807 // The input string starts with zero 4808 s = new Scanner("03,456"); 4809 s.useLocale(Locale.ENGLISH); 4810 assertFalse(s.hasNextLong(10)); 4811 try { 4812 s.nextLong(10); 4813 fail("Should throw InputMismatchException"); 4814 } catch (InputMismatchException e) { 4815 // Expected 4816 } 4817 4818 s = new Scanner("03456"); 4819 assertTrue(s.hasNextLong(10)); 4820 assertEquals(3456, s.nextLong(10)); 4821 4822 s = new Scanner("\u06603,456"); 4823 s.useLocale(Locale.ENGLISH); 4824 assertTrue(s.hasNextLong(10)); 4825 assertEquals(3456, s.nextLong(10)); 4826 4827 s = new Scanner("E34"); 4828 assertTrue(s.hasNextLong(16)); 4829 assertEquals(3636, s.nextLong(16)); 4830 4831 /* 4832 * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50' 4833 * respectively, but they are not differentiated. 4834 */ 4835 s = new Scanner("12300"); 4836 s.useLocale(Locale.CHINESE); 4837 assertTrue(s.hasNextLong(10)); 4838 assertEquals(12300, s.nextLong(10)); 4839 4840 s = new Scanner("123\u0966\u0966"); 4841 s.useLocale(Locale.CHINESE); 4842 assertTrue(s.hasNextLong(10)); 4843 assertEquals(12300, s.nextLong(10)); 4844 4845 s = new Scanner("123\u0e50\u0e50"); 4846 s.useLocale(Locale.CHINESE); 4847 assertTrue(s.hasNextLong(10)); 4848 assertEquals(12300, s.nextLong(10)); 4849 4850 s = new Scanner("-123"); 4851 s.useLocale(new Locale("ar", "AE")); 4852 assertTrue(s.hasNextLong(10)); 4853 assertEquals(-123, s.nextLong(10)); 4854 4855 4856 s = new Scanner("-123"); 4857 s.useLocale(new Locale("mk", "MK")); 4858 assertTrue(s.hasNextLong(10)); 4859 assertEquals(-123, s.nextLong(10)); 4860 } 4861 4862 /** 4863 * @throws IOException 4864 * @tests java.util.Scanner#hasNextLong(int) 4865 */ 4866 @TestTargetNew( 4867 level = TestLevel.PARTIAL_COMPLETE, 4868 notes = "Regression test.", 4869 method = "hasNextLong", 4870 args = {int.class} 4871 ) 4872 public void test_hasNextLongI_cache() throws IOException { 4873 //regression for HARMONY-2063 4874 s = new Scanner("123 456"); 4875 assertTrue(s.hasNextLong(16)); 4876 assertEquals(291, s.nextLong()); 4877 assertEquals(456, s.nextLong()); 4878 4879 s = new Scanner("123 456"); 4880 assertTrue(s.hasNextLong(16)); 4881 assertTrue(s.hasNextLong(8)); 4882 assertEquals(83, s.nextLong()); 4883 assertEquals(456, s.nextLong()); 4884 4885 s = new Scanner("-123 -456 -789"); 4886 assertTrue(s.hasNextLong(8)); 4887 assertEquals(-123, s.nextInt()); 4888 assertEquals(-456, s.nextLong()); 4889 assertTrue(s.hasNextShort(16)); 4890 assertEquals(-789, s.nextLong()); 4891 4892 s = new Scanner("123 456"); 4893 assertTrue(s.hasNextLong()); 4894 s.close(); 4895 try { 4896 s.nextLong(); 4897 fail("Should throw IllegalStateException"); 4898 } catch (IllegalStateException e) { 4899 // expected 4900 } 4901 } 4902 4903 /** 4904 * @throws IOException 4905 * @tests java.util.Scanner#hasNextLong() 4906 */ 4907 @TestTargetNew( 4908 level = TestLevel.COMPLETE, 4909 notes = "", 4910 method = "hasNextLong", 4911 args = {} 4912 ) 4913 @KnownFailure("Some locales were removed last minute in cupcake") 4914 public void test_hasNextLong() throws IOException { 4915 s = new Scanner("123 456"); 4916 assertTrue(s.hasNextLong()); 4917 assertEquals(123, s.nextLong()); 4918 assertTrue(s.hasNextLong()); 4919 assertEquals(456, s.nextLong()); 4920 assertFalse(s.hasNextLong()); 4921 try { 4922 s.nextLong(); 4923 fail("Should throw NoSuchElementException"); 4924 } catch (NoSuchElementException e) { 4925 // Expected 4926 } 4927 4928 // If the radix is different from 10 4929 s = new Scanner("123 456"); 4930 s.useRadix(5); 4931 assertTrue(s.hasNextLong()); 4932 assertEquals(38, s.nextLong()); 4933 assertFalse(s.hasNextLong()); 4934 try { 4935 s.nextLong(); 4936 fail("Should throw InputMismatchException"); 4937 } catch (InputMismatchException e) { 4938 // Expected 4939 } 4940 4941 // If the number is out of range 4942 s = new Scanner("123456789123456789123456789123456789"); 4943 assertFalse(s.hasNextLong()); 4944 try { 4945 s.nextLong(); 4946 fail("Should throw InputMismatchException"); 4947 } catch (InputMismatchException e) { 4948 // Expected 4949 } 4950 4951 /* 4952 * Different locale can only recognize corresponding locale sensitive 4953 * string. ',' is used in many locales as group separator. 4954 */ 4955 s = new Scanner("23,456 23,456"); 4956 s.useLocale(Locale.GERMANY); 4957 assertFalse(s.hasNextLong()); 4958 try { 4959 s.nextLong(); 4960 fail("Should throw InputMismatchException"); 4961 } catch (InputMismatchException e) { 4962 // Expected 4963 } 4964 s.useLocale(Locale.ENGLISH); 4965 // If exception is thrown out, input will not be advanced. 4966 assertTrue(s.hasNextLong()); 4967 assertEquals(23456, s.nextLong()); 4968 assertTrue(s.hasNextLong()); 4969 assertEquals(23456, s.nextLong()); 4970 4971 /* 4972 * ''' is used in many locales as group separator. 4973 */ 4974 s = new Scanner("23'456 23'456"); 4975 s.useLocale(Locale.GERMANY); 4976 assertFalse(s.hasNextLong()); 4977 try { 4978 s.nextLong(); 4979 fail("Should throw InputMismatchException"); 4980 } catch (InputMismatchException e) { 4981 // Expected 4982 } 4983 s.useLocale(new Locale("de", "CH")); 4984 // If exception is thrown out, input will not be advanced. 4985 assertTrue(s.hasNextLong()); 4986 assertEquals(23456, s.nextLong()); 4987 assertTrue(s.hasNextLong()); 4988 assertEquals(23456, s.nextLong()); 4989 4990 /* 4991 * The input string has Arabic-Indic digits. 4992 */ 4993 s = new Scanner("1\u06602 1\u06662"); 4994 assertEquals(102, s.nextLong()); 4995 s.useRadix(5); 4996 assertFalse(s.hasNextLong()); 4997 try { 4998 s.nextLong(); 4999 fail("Should throw InputMismatchException"); 5000 } catch (InputMismatchException e) { 5001 // Expected 5002 } 5003 s.useRadix(10); 5004 assertTrue(s.hasNextLong()); 5005 assertEquals(162, s.nextLong()); 5006 5007 /* 5008 * '.' is used in many locales as group separator. The input string 5009 * has Arabic-Indic digits . 5010 */ 5011 s = new Scanner("23.45\u0666 23.456"); 5012 s.useLocale(Locale.CHINESE); 5013 assertFalse(s.hasNextLong()); 5014 try { 5015 s.nextLong(); 5016 fail("Should throw InputMismatchException"); 5017 } catch (InputMismatchException e) { 5018 // Expected 5019 } 5020 s.useLocale(Locale.GERMANY); 5021 // If exception is thrown out, input will not be advanced. 5022 assertTrue(s.hasNextLong()); 5023 assertEquals(23456, s.nextLong()); 5024 assertTrue(s.hasNextLong()); 5025 assertEquals(23456, s.nextLong()); 5026 5027 // The input string starts with zero 5028 s = new Scanner("03,456"); 5029 s.useLocale(Locale.ENGLISH); 5030 assertFalse(s.hasNextLong()); 5031 try { 5032 s.nextLong(); 5033 fail("Should throw InputMismatchException"); 5034 } catch (InputMismatchException e) { 5035 // Expected 5036 } 5037 5038 s = new Scanner("03456"); 5039 assertTrue(s.hasNextLong()); 5040 assertEquals(3456, s.nextLong()); 5041 5042 s = new Scanner("\u06603,456"); 5043 s.useLocale(Locale.ENGLISH); 5044 assertTrue(s.hasNextLong()); 5045 assertEquals(3456, s.nextLong()); 5046 5047 s = new Scanner("E34"); 5048 s.useRadix(16); 5049 assertTrue(s.hasNextLong()); 5050 assertEquals(3636, s.nextLong()); 5051 5052 /* 5053 * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50' 5054 * respectively, but they are not differentiated. 5055 */ 5056 s = new Scanner("12300"); 5057 s.useLocale(Locale.CHINESE); 5058 assertTrue(s.hasNextLong()); 5059 assertEquals(12300, s.nextLong()); 5060 5061 s = new Scanner("123\u0966\u0966"); 5062 s.useLocale(Locale.CHINESE); 5063 assertTrue(s.hasNextLong()); 5064 assertEquals(12300, s.nextLong()); 5065 5066 s = new Scanner("123\u0e50\u0e50"); 5067 s.useLocale(Locale.CHINESE); 5068 assertTrue(s.hasNextLong()); 5069 assertEquals(12300, s.nextLong()); 5070 5071 s = new Scanner("-123"); 5072 s.useLocale(new Locale("ar", "AE")); 5073 assertTrue(s.hasNextLong()); 5074 assertEquals(-123, s.nextLong()); 5075 5076 s = new Scanner("-123"); 5077 s.useLocale(new Locale("mk", "MK")); 5078 assertTrue(s.hasNextLong()); 5079 assertEquals(-123, s.nextLong()); 5080 5081 s.close(); 5082 try { 5083 s.hasNextLong(); 5084 fail("IllegalStateException expected"); 5085 } catch (IllegalStateException e) { 5086 //expected 5087 } 5088 } 5089 5090 /** 5091 * @throws IOException 5092 * @tests java.util.Scanner#nextDouble() 5093 */ 5094 @TestTargetNew( 5095 level = TestLevel.COMPLETE, 5096 notes = "", 5097 method = "hasNextDouble", 5098 args = {} 5099 ) 5100 @KnownFailure("Some locales were removed last minute in cupcake") 5101 public void test_hasNextDouble() throws IOException { 5102 s = new Scanner("123 45\u0666. 123.4 .123 "); 5103 s.useLocale(Locale.ENGLISH); 5104 assertTrue(s.hasNextDouble()); 5105 assertEquals(123.0, s.nextDouble()); 5106 assertTrue(s.hasNextDouble()); 5107 assertEquals(456.0, s.nextDouble()); 5108 assertTrue(s.hasNextDouble()); 5109 assertEquals(123.4, s.nextDouble()); 5110 assertTrue(s.hasNextDouble()); 5111 assertEquals(0.123, s.nextDouble()); 5112 assertFalse(s.hasNextDouble()); 5113 try { 5114 s.nextDouble(); 5115 fail("Should throw NoSuchElementException"); 5116 } catch (NoSuchElementException e) { 5117 // Expected 5118 } 5119 5120 s = new Scanner("+123.4 -456.7 123,456.789 0.1\u06623,4"); 5121 s.useLocale(Locale.ENGLISH); 5122 assertTrue(s.hasNextDouble()); 5123 assertEquals(123.4, s.nextDouble()); 5124 assertTrue(s.hasNextDouble()); 5125 assertEquals(-456.7, s.nextDouble()); 5126 assertTrue(s.hasNextDouble()); 5127 assertEquals(123456.789, s.nextDouble()); 5128 assertFalse(s.hasNextDouble()); 5129 try { 5130 s.nextDouble(); 5131 fail("Should throw InputMismatchException"); 5132 } catch (InputMismatchException e) { 5133 // Expected 5134 } 5135 5136 // Scientific notation 5137 s = new Scanner("+123.4E10 -456.7e+12 123,456.789E-10"); 5138 s.useLocale(Locale.ENGLISH); 5139 assertTrue(s.hasNextDouble()); 5140 assertEquals(1.234E12, s.nextDouble()); 5141 assertTrue(s.hasNextDouble()); 5142 assertEquals(-4.567E14, s.nextDouble()); 5143 assertTrue(s.hasNextDouble()); 5144 assertEquals(1.23456789E-5, s.nextDouble()); 5145 5146 s = new Scanner("NaN Infinity -Infinity"); 5147 assertTrue(s.hasNextDouble()); 5148 assertEquals(Double.NaN, s.nextDouble()); 5149 assertTrue(s.hasNextDouble()); 5150 assertEquals(Double.POSITIVE_INFINITY, s.nextDouble()); 5151 assertTrue(s.hasNextDouble()); 5152 assertEquals(Double.NEGATIVE_INFINITY, s.nextDouble()); 5153 5154 String str=String.valueOf(Double.MAX_VALUE*2); 5155 s=new Scanner(str); 5156 assertTrue(s.hasNextDouble()); 5157 assertEquals(Double.POSITIVE_INFINITY,s.nextDouble()); 5158 5159 /* 5160 * Different locale can only recognize corresponding locale sensitive 5161 * string. ',' is used in many locales as group separator. 5162 */ 5163 s = new Scanner("23,456 23,456"); 5164 s.useLocale(Locale.ENGLISH); 5165 assertTrue(s.hasNextDouble()); 5166 assertEquals(23456.0, s.nextDouble()); 5167 s.useLocale(Locale.GERMANY); 5168 assertTrue(s.hasNextDouble()); 5169 assertEquals(23.456, s.nextDouble()); 5170 5171 s = new Scanner("23.456 23.456"); 5172 s.useLocale(Locale.ENGLISH); 5173 assertTrue(s.hasNextDouble()); 5174 assertEquals(23.456, s.nextDouble()); 5175 s.useLocale(Locale.GERMANY); 5176 assertTrue(s.hasNextDouble()); 5177 assertEquals(23456.0, s.nextDouble()); 5178 5179 s = new Scanner("23,456.7 23.456,7"); 5180 s.useLocale(Locale.ENGLISH); 5181 assertTrue(s.hasNextDouble()); 5182 assertEquals(23456.7, s.nextDouble()); 5183 s.useLocale(Locale.GERMANY); 5184 assertTrue(s.hasNextDouble()); 5185 assertEquals(23456.7, s.nextDouble()); 5186 5187 s = new Scanner("-123.4"); 5188 s.useLocale(Locale.ENGLISH); 5189 assertTrue(s.hasNextDouble()); 5190 assertEquals(-123.4, s.nextDouble()); 5191 5192 s = new Scanner("+123.4 -456.7"); 5193 s.useLocale(Locale.ENGLISH); 5194 assertTrue(s.hasNextDouble()); 5195 s.close(); 5196 try{ 5197 s.nextDouble(); 5198 fail("Should throw IllegalStateException"); 5199 }catch(IllegalStateException e){ 5200 //expected 5201 } 5202 } 5203 5204 /** 5205 * @throws IOException 5206 * @tests java.util.Scanner#hasNextBigDecimal() 5207 */ 5208 @TestTargetNew( 5209 level = TestLevel.COMPLETE, 5210 notes = "", 5211 method = "hasNextBigDecimal", 5212 args = {} 5213 ) 5214 @KnownFailure("Some locales were removed last minute in cupcake") 5215 public void test_hasNextBigDecimal() throws IOException { 5216 s = new Scanner("123 45\u0666. 123.4 .123 "); 5217 s.useLocale(Locale.ENGLISH); 5218 assertTrue(s.hasNextBigDecimal()); 5219 assertEquals(new BigDecimal("123"), s.nextBigDecimal()); 5220 assertTrue(s.hasNextBigDecimal()); 5221 assertEquals(new BigDecimal("456"), s.nextBigDecimal()); 5222 assertTrue(s.hasNextBigDecimal()); 5223 assertEquals(new BigDecimal("123.4"), s.nextBigDecimal()); 5224 assertTrue(s.hasNextBigDecimal()); 5225 assertEquals(new BigDecimal("0.123"), s.nextBigDecimal()); 5226 assertFalse(s.hasNextBigDecimal()); 5227 try { 5228 s.nextBigDecimal(); 5229 fail("Should throw NoSuchElementException"); 5230 } catch (NoSuchElementException e) { 5231 // Expected 5232 } 5233 5234 s = new Scanner("+123.4 -456.7 123,456.789 0.1\u06623,4"); 5235 s.useLocale(Locale.ENGLISH); 5236 assertTrue(s.hasNextBigDecimal()); 5237 assertEquals(new BigDecimal("123.4"), s.nextBigDecimal()); 5238 assertTrue(s.hasNextBigDecimal()); 5239 assertEquals(new BigDecimal("-456.7"), s.nextBigDecimal()); 5240 assertTrue(s.hasNextBigDecimal()); 5241 assertEquals(new BigDecimal("123456.789"), s.nextBigDecimal()); 5242 assertFalse(s.hasNextBigDecimal()); 5243 try { 5244 s.nextBigDecimal(); 5245 fail("Should throw InputMismatchException"); 5246 } catch (InputMismatchException e) { 5247 // Expected 5248 } 5249 5250 // Scientific notation 5251 s = new Scanner("+123.4E10 -456.7e+12 123,456.789E-10"); 5252 s.useLocale(Locale.ENGLISH); 5253 assertTrue(s.hasNextBigDecimal()); 5254 assertEquals(new BigDecimal("1.234E12"), s.nextBigDecimal()); 5255 assertTrue(s.hasNextBigDecimal()); 5256 assertEquals(new BigDecimal("-4.567E14"), s.nextBigDecimal()); 5257 assertTrue(s.hasNextBigDecimal()); 5258 assertEquals(new BigDecimal("1.23456789E-5"), s.nextBigDecimal()); 5259 5260 s = new Scanner("NaN"); 5261 assertFalse(s.hasNextBigDecimal()); 5262 try { 5263 s.nextBigDecimal(); 5264 fail("Should throw InputMismatchException"); 5265 } catch (InputMismatchException e) { 5266 // Expected 5267 } 5268 5269 /* 5270 * Different locale can only recognize corresponding locale sensitive 5271 * string. ',' is used in many locales as group separator. 5272 */ 5273 s = new Scanner("23,456 23,456"); 5274 s.useLocale(Locale.ENGLISH); 5275 assertTrue(s.hasNextBigDecimal()); 5276 assertEquals(new BigDecimal("23456"), s.nextBigDecimal()); 5277 s.useLocale(Locale.GERMANY); 5278 assertTrue(s.hasNextBigDecimal()); 5279 assertEquals(new BigDecimal("23.456"), s.nextBigDecimal()); 5280 5281 s = new Scanner("23.456 23.456"); 5282 s.useLocale(Locale.ENGLISH); 5283 assertTrue(s.hasNextBigDecimal()); 5284 assertEquals(new BigDecimal("23.456"), s.nextBigDecimal()); 5285 s.useLocale(Locale.GERMANY); 5286 assertTrue(s.hasNextBigDecimal()); 5287 assertEquals(new BigDecimal("23456"), s.nextBigDecimal()); 5288 5289 s = new Scanner("23,456.7"); 5290 s.useLocale(Locale.ENGLISH); 5291 assertTrue(s.hasNextBigDecimal()); 5292 assertEquals(new BigDecimal("23456.7"), s.nextBigDecimal()); 5293 5294 s = new Scanner("-123.4"); 5295 s.useLocale(Locale.ENGLISH); 5296 assertTrue(s.hasNextBigDecimal()); 5297 assertEquals(new BigDecimal("-123.4"), s.nextBigDecimal()); 5298 5299 s.close(); 5300 try { 5301 s.hasNextBigDecimal(); 5302 fail("IllegalStateException expected"); 5303 } catch (IllegalStateException e) { 5304 //expected 5305 } 5306 } 5307 5308 private static class MockStringReader extends StringReader { 5309 5310 public MockStringReader(String param) { 5311 super(param); 5312 } 5313 5314 public int read(CharBuffer target) throws IOException { 5315 target.append('t'); 5316 target.append('e'); 5317 target.append('s'); 5318 target.append('t'); 5319 throw new IOException(); 5320 } 5321 5322 } 5323 5324 private static class MockStringReader2Read extends StringReader { 5325 private int timesRead = 1; 5326 5327 public MockStringReader2Read(String param) { 5328 super(param); 5329 } 5330 5331 public int read(CharBuffer target) throws IOException { 5332 if (timesRead == 1) { 5333 target.append('1'); 5334 target.append('2'); 5335 target.append('3'); 5336 timesRead++; 5337 return 3; 5338 } else if (timesRead == 2) { 5339 target.append('t'); 5340 timesRead++; 5341 return 1; 5342 } else { 5343 throw new IOException(); 5344 } 5345 } 5346 5347 } 5348 5349 /** 5350 * @tests java.util.Scanner#findWithinHorizon(Pattern, int) 5351 */ 5352 @TestTargetNew( 5353 level = TestLevel.COMPLETE, 5354 notes = "", 5355 method = "findWithinHorizon", 5356 args = {java.util.regex.Pattern.class, int.class} 5357 ) 5358 @KnownFailure("findWithinHorizon method doesn't work properly") 5359 public void test_findWithinHorizon_LPatternI(){ 5360 5361 // This method searches through the input up to the specified search 5362 // horizon(exclusive). 5363 s = new Scanner("123test"); 5364 String result = s.findWithinHorizon(Pattern.compile("\\p{Lower}"), 5); 5365 assertEquals("t", result); 5366 MatchResult mresult = s.match(); 5367 assertEquals(3, mresult.start()); 5368 assertEquals(4, mresult.end()); 5369 5370 s = new Scanner("12345test1234test next"); 5371 /* 5372 * If the pattern is found the scanner advances past the input that 5373 * matched and returns the string that matched the pattern. 5374 */ 5375 result = s.findWithinHorizon(Pattern.compile("\\p{Digit}+"), 2); 5376 assertEquals("12", result); 5377 mresult = s.match(); 5378 assertEquals(0, mresult.start()); 5379 assertEquals(2, mresult.end()); 5380 // Postion is now pointing at the bar. "12|345test1234test next" 5381 5382 result = s.findWithinHorizon(Pattern.compile("\\p{Digit}+"), 6); 5383 assertEquals("345", result); 5384 5385 mresult = s.match(); 5386 assertEquals(2, mresult.start()); 5387 assertEquals(5, mresult.end()); 5388 // Postion is now pointing at the bar. "12345|test1234test next" 5389 5390 // If no such pattern is detected then the null is returned and the 5391 // scanner's position remains unchanged. 5392 result = s.findWithinHorizon(Pattern.compile("\\p{Digit}+"), 3); 5393 assertNull(result); 5394 5395 try { 5396 s.match(); 5397 fail("Should throw IllegalStateException"); 5398 } catch (IllegalStateException e) { 5399 // expected 5400 } 5401 assertEquals("345", mresult.group()); 5402 assertEquals(2, mresult.start()); 5403 assertEquals(5, mresult.end()); 5404 // Postion is now still pointing at the bar. "12345|test1234test next" 5405 5406 // If horizon is 0, then the horizon is ignored and this method 5407 // continues to search through the input looking for the specified 5408 // pattern without bound. 5409 result = s.findWithinHorizon(Pattern.compile("\\p{Digit}+"), 0); 5410 mresult = s.match(); 5411 assertEquals(9, mresult.start()); 5412 assertEquals(13, mresult.end()); 5413 // Postion is now pointing at the bar. "12345test1234|test next" 5414 5415 assertEquals("test", s.next()); 5416 mresult = s.match(); 5417 assertEquals(13, mresult.start()); 5418 assertEquals(17, mresult.end()); 5419 5420 assertEquals("next", s.next()); 5421 mresult = s.match(); 5422 assertEquals(18, mresult.start()); 5423 assertEquals(22, mresult.end()); 5424 5425 try { 5426 s.findWithinHorizon((Pattern) null, -1); 5427 fail("Should throw NullPointerException"); 5428 } catch (NullPointerException e) { 5429 // expected 5430 } 5431 5432 try { 5433 s.findWithinHorizon(Pattern.compile("\\p{Digit}+"), -1); 5434 fail("Should throw IllegalArgumentException"); 5435 } catch (IllegalArgumentException e) { 5436 // expected 5437 } 5438 5439 s.close(); 5440 try { 5441 s.findWithinHorizon((Pattern) null, -1); 5442 fail("Should throw IllegalStateException"); 5443 } catch (IllegalStateException e) { 5444 // expected 5445 } 5446 5447 s = new Scanner("test"); 5448 result = s.findWithinHorizon(Pattern.compile("\\w+"), 10); 5449 assertEquals("test", result); 5450 5451 s = new Scanner("aa\n\rb"); 5452 String patternStr = "^(a)$"; 5453 result = s.findWithinHorizon(Pattern.compile("a"), 5); 5454 assertEquals("a", result); 5455 mresult = s.match(); 5456 assertEquals(0, mresult.start()); 5457 assertEquals(1, mresult.end()); 5458 5459 result = s.findWithinHorizon(Pattern.compile(patternStr, 5460 Pattern.MULTILINE), 5); 5461 assertNull(result); 5462 5463 try { 5464 mresult = s.match(); 5465 fail("Should throw IllegalStateException"); 5466 } catch (IllegalStateException e) { 5467 // expected 5468 } 5469 5470 s = new Scanner(""); 5471 result = s.findWithinHorizon(Pattern.compile("^"), 0); 5472 assertEquals("", result); 5473 MatchResult matchResult = s.match(); 5474 assertEquals(0, matchResult.start()); 5475 assertEquals(0, matchResult.end()); 5476 5477 result = s.findWithinHorizon(Pattern.compile("$"), 0); 5478 assertEquals("", result); 5479 matchResult = s.match(); 5480 assertEquals(0, matchResult.start()); 5481 assertEquals(0, matchResult.end()); 5482 5483 s = new Scanner("1 fish 2 fish red fish blue fish"); 5484 result = s.findWithinHorizon(Pattern 5485 .compile("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)"), 10); 5486 assertNull(result); 5487 5488 try { 5489 mresult = s.match(); 5490 fail("Should throw IllegalStateException"); 5491 } catch (IllegalStateException e) { 5492 // expected 5493 } 5494 assertEquals(0, mresult.groupCount()); 5495 5496 result = s.findWithinHorizon(Pattern 5497 .compile("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)"), 100); 5498 assertEquals("1 fish 2 fish red fish blue", result); 5499 mresult = s.match(); 5500 assertEquals(4, mresult.groupCount()); 5501 assertEquals("1", mresult.group(1)); 5502 assertEquals("2", mresult.group(2)); 5503 assertEquals("red", mresult.group(3)); 5504 assertEquals("blue", mresult.group(4)); 5505 5506 s = new Scanner("test"); 5507 s.close(); 5508 try { 5509 s.findWithinHorizon(Pattern.compile("test"), 1); 5510 fail("Should throw IllegalStateException"); 5511 } catch (IllegalStateException e) { 5512 // expected 5513 } 5514 5515 s = new Scanner("word1 WorD2 "); 5516 s.close(); 5517 try { 5518 s.findWithinHorizon(Pattern.compile("\\d+"), 10); 5519 fail("should throw IllegalStateException"); 5520 } catch (IllegalStateException e) { 5521 // expected 5522 } 5523 5524 s = new Scanner("word1 WorD2 wOrd3 "); 5525 Pattern pattern = Pattern.compile("\\d+"); 5526 assertEquals("1", s.findWithinHorizon(pattern, 10)); 5527 assertEquals("WorD2", s.next()); 5528 assertEquals("3", s.findWithinHorizon(pattern, 15)); 5529 5530 // Regression test 5531 s = new Scanner(new MockStringReader("MockStringReader")); 5532 pattern = Pattern.compile("test"); 5533 result = s.findWithinHorizon(pattern, 10); 5534 assertEquals("test", result); 5535 5536 // Test the situation when input length is longer than buffer size. 5537 StringBuilder stringBuilder = new StringBuilder(); 5538 for (int i = 0; i < 1026; i++) { 5539 stringBuilder.append('a'); 5540 } 5541 s = new Scanner(stringBuilder.toString()); 5542 pattern = Pattern.compile("\\p{Lower}+"); 5543 result = s.findWithinHorizon(pattern, 1026); 5544 assertEquals(stringBuilder.toString(), result); 5545 5546 // Test the situation when input length is longer than buffer size and 5547 // set horizon to buffer size. 5548 stringBuilder = new StringBuilder(); 5549 for (int i = 0; i < 1026; i++) { 5550 stringBuilder.append('a'); 5551 } 5552 s = new Scanner(stringBuilder.toString()); 5553 pattern = Pattern.compile("\\p{Lower}+"); 5554 result = s.findWithinHorizon(pattern, 1022); 5555 assertEquals(1022, result.length()); 5556 assertEquals(stringBuilder.subSequence(0, 1022), result); 5557 5558 // Test the situation, under which pattern is clipped by buffer. 5559 stringBuilder = new StringBuilder(); 5560 for (int i = 0; i < 1022; i++) { 5561 stringBuilder.append(' '); 5562 } 5563 stringBuilder.append("bbc"); 5564 assertEquals(1025, stringBuilder.length()); 5565 s = new Scanner(stringBuilder.toString()); 5566 pattern = Pattern.compile("bbc"); 5567 result = s.findWithinHorizon(pattern, 1025); 5568 assertEquals(3, result.length()); 5569 assertEquals(stringBuilder.subSequence(1022, 1025), result); 5570 5571 stringBuilder = new StringBuilder(); 5572 for (int i = 0; i < 1026; i++) { 5573 stringBuilder.append('a'); 5574 } 5575 s = new Scanner(stringBuilder.toString()); 5576 pattern = Pattern.compile("\\p{Lower}+"); 5577 result = s.findWithinHorizon(pattern, 0); 5578 assertEquals(stringBuilder.toString(), result); 5579 5580 stringBuilder = new StringBuilder(); 5581 for (int i = 0; i < 10240; i++) { 5582 stringBuilder.append('-'); 5583 } 5584 stringBuilder.replace(0, 2, "aa"); 5585 s = new Scanner(stringBuilder.toString()); 5586 result = s.findWithinHorizon(Pattern.compile("aa"), 0); 5587 assertEquals("aa", result); 5588 5589 s = new Scanner("aaaa"); 5590 result = s.findWithinHorizon(Pattern.compile("a*"), 0); 5591 assertEquals("aaaa", result); 5592 } 5593 5594 /** 5595 * @tests java.util.Scanner#findWithinHorizon(String, int) Copy of Pattern 5596 * test for String 5597 */ 5598 @TestTargetNew( 5599 level = TestLevel.COMPLETE, 5600 notes = "", 5601 method = "findWithinHorizon", 5602 args = {java.lang.String.class, int.class} 5603 ) 5604 public void test_findWithinHorizon_Ljava_lang_StringI() { 5605 5606 // This method searches through the input up to the specified search 5607 // horizon(exclusive). 5608 s = new Scanner("123test"); 5609 String result = s.findWithinHorizon("\\p{Lower}", 5); 5610 assertEquals("t", result); 5611 MatchResult mresult = s.match(); 5612 assertEquals(3, mresult.start()); 5613 assertEquals(4, mresult.end()); 5614 5615 s = new Scanner("12345test1234test next"); 5616 /* 5617 * If the pattern is found the scanner advances past the input that 5618 * matched and returns the string that matched the pattern. 5619 */ 5620 result = s.findWithinHorizon("\\p{Digit}+", 2); 5621 assertEquals("12", result); 5622 mresult = s.match(); 5623 assertEquals(0, mresult.start()); 5624 assertEquals(2, mresult.end()); 5625 // Postion is now pointing at the bar. "12|345test1234test next" 5626 5627 result = s.findWithinHorizon("\\p{Digit}+", 6); 5628 assertEquals("345", result); 5629 5630 mresult = s.match(); 5631 assertEquals(2, mresult.start()); 5632 assertEquals(5, mresult.end()); 5633 // Postion is now pointing at the bar. "12345|test1234test next" 5634 5635 // If no such pattern is detected then the null is returned and the 5636 // scanner's position remains unchanged. 5637 result = s.findWithinHorizon("\\p{Digit}+", 3); 5638 assertNull(result); 5639 5640 try { 5641 s.match(); 5642 fail("Should throw IllegalStateException"); 5643 } catch (IllegalStateException e) { 5644 // expected 5645 } 5646 assertEquals("345", mresult.group()); 5647 assertEquals(2, mresult.start()); 5648 assertEquals(5, mresult.end()); 5649 // Postion is now still pointing at the bar. "12345|test1234test next" 5650 5651 // If horizon is 0, then the horizon is ignored and this method 5652 // continues to search through the input looking for the specified 5653 // pattern without bound. 5654 result = s.findWithinHorizon("\\p{Digit}+", 0); 5655 mresult = s.match(); 5656 assertEquals(9, mresult.start()); 5657 assertEquals(13, mresult.end()); 5658 // Postion is now pointing at the bar. "12345test1234|test next" 5659 5660 assertEquals("test", s.next()); 5661 mresult = s.match(); 5662 assertEquals(13, mresult.start()); 5663 assertEquals(17, mresult.end()); 5664 5665 assertEquals("next", s.next()); 5666 mresult = s.match(); 5667 assertEquals(18, mresult.start()); 5668 assertEquals(22, mresult.end()); 5669 5670 try { 5671 s.findWithinHorizon((String)null, 1); 5672 fail("Should throw NullPointerException"); 5673 } catch (NullPointerException e) { 5674 // expected 5675 } 5676 5677 try { 5678 s.findWithinHorizon("\\p{Digit}+", -1); 5679 fail("Should throw IllegalArgumentException"); 5680 } catch (IllegalArgumentException e) { 5681 // expected 5682 } 5683 5684 s.close(); 5685 // on RI throws NullPointerException 5686 /* 5687 * try { System.out.println("fsdfs"); s.findWithinHorizon((String) null, 5688 * -1); System.out.println("fsdfs"); fail("Should throw 5689 * IllegalStateException"); } catch (IllegalStateException e) { // 5690 * expected } 5691 */ 5692 s = new Scanner("test"); 5693 result = s.findWithinHorizon("\\w+", 10); 5694 assertEquals("test", result); 5695 5696 s = new Scanner("aa\n\rb"); 5697 String patternStr = "^(a)$"; 5698 result = s.findWithinHorizon("a", 5); 5699 assertEquals("a", result); 5700 mresult = s.match(); 5701 assertEquals(0, mresult.start()); 5702 assertEquals(1, mresult.end()); 5703 5704 result = s.findWithinHorizon(patternStr, 5); 5705 assertNull(result); 5706 5707 try { 5708 mresult = s.match(); 5709 fail("Should throw IllegalStateException"); 5710 } catch (IllegalStateException e) { 5711 // expected 5712 } 5713 5714 s = new Scanner(""); 5715 result = s.findWithinHorizon("^", 0); 5716 assertEquals("", result); 5717 MatchResult matchResult = s.match(); 5718 assertEquals(0, matchResult.start()); 5719 assertEquals(0, matchResult.end()); 5720 5721 result = s.findWithinHorizon("$", 0); 5722 assertEquals("", result); 5723 matchResult = s.match(); 5724 assertEquals(0, matchResult.start()); 5725 assertEquals(0, matchResult.end()); 5726 5727 s = new Scanner("1 fish 2 fish red fish blue fish"); 5728 result = s.findWithinHorizon("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)", 10); 5729 assertNull(result); 5730 5731 try { 5732 mresult = s.match(); 5733 fail("Should throw IllegalStateException"); 5734 } catch (IllegalStateException e) { 5735 // expected 5736 } 5737 assertEquals(0, mresult.groupCount()); 5738 5739 result = s.findWithinHorizon("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)", 100); 5740 assertEquals("1 fish 2 fish red fish blue", result); 5741 mresult = s.match(); 5742 assertEquals(4, mresult.groupCount()); 5743 assertEquals("1", mresult.group(1)); 5744 assertEquals("2", mresult.group(2)); 5745 assertEquals("red", mresult.group(3)); 5746 assertEquals("blue", mresult.group(4)); 5747 5748 s = new Scanner("test"); 5749 s.close(); 5750 try { 5751 s.findWithinHorizon("test", 1); 5752 fail("Should throw IllegalStateException"); 5753 } catch (IllegalStateException e) { 5754 // expected 5755 } 5756 5757 s = new Scanner("word1 WorD2 "); 5758 s.close(); 5759 try { 5760 s.findWithinHorizon("\\d+", 10); 5761 fail("should throw IllegalStateException"); 5762 } catch (IllegalStateException e) { 5763 // expected 5764 } 5765 5766 s = new Scanner("word1 WorD2 wOrd3 "); 5767 patternStr = "\\d+"; 5768 assertEquals("1", s.findWithinHorizon(patternStr, 10)); 5769 assertEquals("WorD2", s.next()); 5770 assertEquals("3", s.findWithinHorizon(patternStr, 15)); 5771 5772 // Regression test 5773 s = new Scanner(new MockStringReader("MockStringReader")); 5774 patternStr = "test"; 5775 result = s.findWithinHorizon(patternStr, 10); 5776 assertEquals("test", result); 5777 5778 // Test the situation when input length is longer than buffer size. 5779 StringBuilder stringBuilder = new StringBuilder(); 5780 for (int i = 0; i < 1026; i++) { 5781 stringBuilder.append('a'); 5782 } 5783 s = new Scanner(stringBuilder.toString()); 5784 patternStr = "\\p{Lower}+"; 5785 result = s.findWithinHorizon(patternStr, 1026); 5786 assertEquals(stringBuilder.toString(), result); 5787 5788 // Test the situation when input length is longer than buffer size and 5789 // set horizon to buffer size. 5790 stringBuilder = new StringBuilder(); 5791 for (int i = 0; i < 1026; i++) { 5792 stringBuilder.append('a'); 5793 } 5794 s = new Scanner(stringBuilder.toString()); 5795 patternStr = "\\p{Lower}+"; 5796 result = s.findWithinHorizon(patternStr, 1022); 5797 assertEquals(1022, result.length()); 5798 assertEquals(stringBuilder.subSequence(0, 1022), result); 5799 5800 // Test the situation, under which pattern is clipped by buffer. 5801 stringBuilder = new StringBuilder(); 5802 for (int i = 0; i < 1022; i++) { 5803 stringBuilder.append(' '); 5804 } 5805 stringBuilder.append("bbc"); 5806 assertEquals(1025, stringBuilder.length()); 5807 s = new Scanner(stringBuilder.toString()); 5808 patternStr = "bbc"; 5809 result = s.findWithinHorizon(patternStr, 1025); 5810 assertEquals(3, result.length()); 5811 assertEquals(stringBuilder.subSequence(1022, 1025), result); 5812 5813 stringBuilder = new StringBuilder(); 5814 for (int i = 0; i < 1026; i++) { 5815 stringBuilder.append('a'); 5816 } 5817 s = new Scanner(stringBuilder.toString()); 5818 patternStr = "\\p{Lower}+"; 5819 result = s.findWithinHorizon(patternStr, 0); 5820 assertEquals(stringBuilder.toString(), result); 5821 5822 stringBuilder = new StringBuilder(); 5823 for (int i = 0; i < 10240; i++) { 5824 stringBuilder.append('-'); 5825 } 5826 stringBuilder.replace(0, 2, "aa"); 5827 s = new Scanner(stringBuilder.toString()); 5828 result = s.findWithinHorizon("aa", 0); 5829 assertEquals("aa", result); 5830 5831 s = new Scanner("aaaa"); 5832 result = s.findWithinHorizon("a*", 0); 5833 assertEquals("aaaa", result); 5834 } 5835 5836 /** 5837 * @tests java.util.Scanner#findInLine(Pattern) 5838 */ 5839 @TestTargetNew( 5840 level = TestLevel.COMPLETE, 5841 notes = "", 5842 method = "findInLine", 5843 args = {java.util.regex.Pattern.class} 5844 ) 5845 public void test_findInLine_LPattern() { 5846 5847 Scanner s = new Scanner(""); 5848 try { 5849 s.findInLine((Pattern) null); 5850 fail("Should throw NullPointerException"); 5851 } catch (NullPointerException e) { 5852 // Expected 5853 } 5854 String result = s.findInLine(Pattern.compile("^")); 5855 assertEquals("", result); 5856 MatchResult matchResult = s.match(); 5857 assertEquals(0, matchResult.start()); 5858 assertEquals(0, matchResult.end()); 5859 5860 result = s.findInLine(Pattern.compile("$")); 5861 assertEquals("", result); 5862 matchResult = s.match(); 5863 assertEquals(0, matchResult.start()); 5864 assertEquals(0, matchResult.end()); 5865 5866 /* 5867 * When we use the operation of findInLine(Pattern), the match region 5868 * should not span the line separator. 5869 */ 5870 s = new Scanner("aa\nb.b"); 5871 result = s.findInLine(Pattern.compile("a\nb*")); 5872 assertNull(result); 5873 5874 s = new Scanner("aa\nbb.b"); 5875 result = s.findInLine(Pattern.compile("\\.")); 5876 assertNull(result); 5877 5878 s = new Scanner("abcd1234test\n"); 5879 result = s.findInLine(Pattern.compile("\\p{Lower}+")); 5880 assertEquals("abcd", result); 5881 matchResult = s.match(); 5882 assertEquals(0, matchResult.start()); 5883 assertEquals(4, matchResult.end()); 5884 5885 result = s.findInLine(Pattern.compile("\\p{Digit}{5}")); 5886 assertNull(result); 5887 try { 5888 matchResult = s.match(); 5889 fail("Should throw IllegalStateException"); 5890 } catch (IllegalStateException e) { 5891 // expected 5892 } 5893 assertEquals(0, matchResult.start()); 5894 assertEquals(4, matchResult.end()); 5895 5896 result = s.findInLine(Pattern.compile("\\p{Lower}+")); 5897 assertEquals("test", result); 5898 matchResult = s.match(); 5899 assertEquals(8, matchResult.start()); 5900 assertEquals(12, matchResult.end()); 5901 5902 char[] chars = new char[2048]; 5903 Arrays.fill(chars, 'a'); 5904 StringBuilder stringBuilder = new StringBuilder(); 5905 stringBuilder.append(chars); 5906 stringBuilder.append("1234"); 5907 s = new Scanner(stringBuilder.toString()); 5908 result = s.findInLine(Pattern.compile("\\p{Digit}+")); 5909 assertEquals("1234", result); 5910 matchResult = s.match(); 5911 assertEquals(2048, matchResult.start()); 5912 assertEquals(2052, matchResult.end()); 5913 5914 s = new Scanner("test"); 5915 s.close(); 5916 try { 5917 s.findInLine((Pattern) null); 5918 fail("Should throw IllegalStateException"); 5919 } catch (IllegalStateException e) { 5920 // expected 5921 } 5922 5923 s = new Scanner("test1234\n1234 test"); 5924 result = s.findInLine(Pattern.compile("test")); 5925 assertEquals("test", result); 5926 matchResult = s.match(); 5927 assertEquals(0, matchResult.start()); 5928 assertEquals(4, matchResult.end()); 5929 5930 int number = s.nextInt(); 5931 assertEquals(1234, number); 5932 matchResult = s.match(); 5933 assertEquals(4, matchResult.start()); 5934 assertEquals(8, matchResult.end()); 5935 5936 result = s.next(); 5937 assertEquals("1234", result); 5938 matchResult = s.match(); 5939 assertEquals(9, matchResult.start()); 5940 assertEquals(13, matchResult.end()); 5941 5942 result = s.findInLine(Pattern.compile("test")); 5943 assertEquals("test", result); 5944 matchResult = s.match(); 5945 assertEquals(14, matchResult.start()); 5946 assertEquals(18, matchResult.end()); 5947 5948 s = new Scanner("test\u0085\ntest"); 5949 result = s.findInLine("est"); 5950 assertEquals("est", result); 5951 result = s.findInLine("est"); 5952 assertEquals("est", result); 5953 5954 s = new Scanner("test\ntest"); 5955 result = s.findInLine("est"); 5956 assertEquals("est", result); 5957 result = s.findInLine("est"); 5958 assertEquals("est", result); 5959 5960 s = new Scanner("test\n123\ntest"); 5961 result = s.findInLine("est"); 5962 assertEquals("est", result); 5963 result = s.findInLine("est"); 5964 // RI fails. It is a RI's bug. 5965 if (!disableRIBugs) { 5966 assertNull(result); 5967 } 5968 } 5969 5970 /** 5971 * @tests java.util.Scanner#findInLine(String) 5972 */ 5973 @TestTargetNew( 5974 level = TestLevel.COMPLETE, 5975 notes = "", 5976 method = "findInLine", 5977 args = {java.lang.String.class} 5978 ) 5979 public void test_findInLine_LString() { 5980 s = new Scanner("test"); 5981 try { 5982 s.findInLine((String) null); 5983 fail("Should throw NullPointerException"); 5984 } catch (NullPointerException e) { 5985 // expected 5986 } 5987 5988 s.close(); 5989 try { 5990 s.findInLine((String) null); 5991 fail("Should throw NullPointerException"); 5992 } catch (NullPointerException e) { 5993 // expected 5994 } 5995 try { 5996 s.findInLine("test"); 5997 fail("Should throw IllegalStateException"); 5998 } catch (IllegalStateException e) { 5999 // exptected 6000 } 6001 6002 s = new Scanner(""); 6003 6004 String result = s.findInLine("^"); 6005 assertEquals("", result); 6006 MatchResult matchResult = s.match(); 6007 assertEquals(0, matchResult.start()); 6008 assertEquals(0, matchResult.end()); 6009 6010 result = s.findInLine("$"); 6011 assertEquals("", result); 6012 matchResult = s.match(); 6013 assertEquals(0, matchResult.start()); 6014 assertEquals(0, matchResult.end()); 6015 6016 /* 6017 * When we use the operation of findInLine(Pattern), the match region 6018 * should not span the line separator. 6019 */ 6020 s = new Scanner("aa\nb.b"); 6021 result = s.findInLine("a\nb*"); 6022 assertNull(result); 6023 6024 s = new Scanner("aa\nbb.b"); 6025 result = s.findInLine("\\."); 6026 assertNull(result); 6027 6028 s = new Scanner("abcd1234test\n"); 6029 result = s.findInLine("\\p{Lower}+"); 6030 assertEquals("abcd", result); 6031 matchResult = s.match(); 6032 assertEquals(0, matchResult.start()); 6033 assertEquals(4, matchResult.end()); 6034 6035 result = s.findInLine("\\p{Digit}{5}"); 6036 assertNull(result); 6037 try { 6038 matchResult = s.match(); 6039 fail("Should throw IllegalStateException"); 6040 } catch (IllegalStateException e) { 6041 // expected 6042 } 6043 assertEquals(0, matchResult.start()); 6044 assertEquals(4, matchResult.end()); 6045 6046 result = s.findInLine("\\p{Lower}+"); 6047 assertEquals("test", result); 6048 matchResult = s.match(); 6049 assertEquals(8, matchResult.start()); 6050 assertEquals(12, matchResult.end()); 6051 6052 char[] chars = new char[2048]; 6053 Arrays.fill(chars, 'a'); 6054 StringBuilder stringBuilder = new StringBuilder(); 6055 stringBuilder.append(chars); 6056 stringBuilder.append("1234"); 6057 s = new Scanner(stringBuilder.toString()); 6058 result = s.findInLine("\\p{Digit}+"); 6059 assertEquals("1234", result); 6060 matchResult = s.match(); 6061 assertEquals(2048, matchResult.start()); 6062 assertEquals(2052, matchResult.end()); 6063 6064 s = new Scanner("test1234\n1234 test"); 6065 result = s.findInLine("test"); 6066 assertEquals("test", result); 6067 matchResult = s.match(); 6068 assertEquals(0, matchResult.start()); 6069 assertEquals(4, matchResult.end()); 6070 6071 int number = s.nextInt(); 6072 assertEquals(1234, number); 6073 matchResult = s.match(); 6074 assertEquals(4, matchResult.start()); 6075 assertEquals(8, matchResult.end()); 6076 6077 result = s.next(); 6078 assertEquals("1234", result); 6079 matchResult = s.match(); 6080 assertEquals(9, matchResult.start()); 6081 assertEquals(13, matchResult.end()); 6082 6083 result = s.findInLine("test"); 6084 assertEquals("test", result); 6085 matchResult = s.match(); 6086 assertEquals(14, matchResult.start()); 6087 assertEquals(18, matchResult.end()); 6088 6089 s = new Scanner("test\u0085\ntest"); 6090 result = s.findInLine("est"); 6091 assertEquals("est", result); 6092 result = s.findInLine("est"); 6093 assertEquals("est", result); 6094 6095 s = new Scanner("test\ntest"); 6096 result = s.findInLine("est"); 6097 assertEquals("est", result); 6098 result = s.findInLine("est"); 6099 assertEquals("est", result); 6100 6101 s = new Scanner("test\n123\ntest"); 6102 result = s.findInLine("est"); 6103 assertEquals("est", result); 6104 result = s.findInLine("est"); 6105 // RI fails. It is a RI's bug. 6106 if (!disableRIBugs) { 6107 assertNull(result); 6108 } 6109 6110 6111 } 6112 6113 /** 6114 * @tests java.util.Scanner#skip(Pattern) 6115 */ 6116 @TestTargetNew( 6117 level = TestLevel.COMPLETE, 6118 notes = "", 6119 method = "skip", 6120 args = {java.util.regex.Pattern.class} 6121 ) 6122 public void test_skip_LPattern() { 6123 s = new Scanner("test"); 6124 try { 6125 s.skip((String) null); 6126 fail("Should throw NullPointerException"); 6127 } catch (NullPointerException e) { 6128 // expected 6129 } 6130 6131 // If pattern does not match, NoSuchElementException will be thrown out. 6132 s = new Scanner("1234"); 6133 try { 6134 s.skip(Pattern.compile("\\p{Lower}")); 6135 fail("Should throw NoSuchElementException"); 6136 } catch (NoSuchElementException e) { 6137 // expected 6138 } 6139 // Then, no matchResult will be thrown out. 6140 try { 6141 s.match(); 6142 fail("Should throw IllegalStateException"); 6143 } catch (IllegalStateException e) { 6144 // expected 6145 } 6146 6147 s.skip(Pattern.compile("\\p{Digit}")); 6148 MatchResult matchResult = s.match(); 6149 assertEquals(0, matchResult.start()); 6150 assertEquals(1, matchResult.end()); 6151 6152 s.skip(Pattern.compile("\\p{Digit}+")); 6153 matchResult = s.match(); 6154 assertEquals(1, matchResult.start()); 6155 assertEquals(4, matchResult.end()); 6156 6157 s.close(); 6158 try { 6159 s.skip(Pattern.compile("test")); 6160 fail("Should throw IllegalStateException"); 6161 } catch (IllegalStateException e) { 6162 // expected 6163 } 6164 6165 MockStringReader2Read reader = new MockStringReader2Read("test"); 6166 s = new Scanner(reader); 6167 try { 6168 s.skip(Pattern.compile("\\p{Digit}{4}")); 6169 fail("Should throw NoSuchElementException"); 6170 } catch (NoSuchElementException e) { 6171 // expected 6172 } 6173 try { 6174 s.match(); 6175 fail("Should throw IllegalStateException"); 6176 } catch (IllegalStateException e) { 6177 // expected 6178 } 6179 s.skip(Pattern.compile("\\p{Digit}{3}\\p{Lower}")); 6180 matchResult = s.match(); 6181 assertEquals(0, matchResult.start()); 6182 assertEquals(4, matchResult.end()); 6183 6184 s.close(); 6185 try { 6186 s.skip((Pattern) null); 6187 fail("Should throw IllegalStateException"); 6188 } catch (IllegalStateException e) { 6189 // expected 6190 } 6191 6192 StringBuilder stringBuilder = new StringBuilder(); 6193 char [] chars = new char[1024]; 6194 Arrays.fill(chars, 'a'); 6195 stringBuilder.append(chars); 6196 stringBuilder.append('3'); 6197 s = new Scanner(stringBuilder.toString()); 6198 s.skip(Pattern.compile("\\p{Lower}+\\p{Digit}")); 6199 matchResult = s.match(); 6200 assertEquals(0, matchResult.start()); 6201 assertEquals(1025, matchResult.end()); 6202 6203 // Large amount of input may be cached 6204 chars = new char[102400]; 6205 Arrays.fill(chars, 'a'); 6206 stringBuilder = new StringBuilder(); 6207 stringBuilder.append(chars); 6208 s = new Scanner(stringBuilder.toString()); 6209 s.skip(Pattern.compile(".*")); 6210 matchResult = s.match(); 6211 assertEquals(0, matchResult.start()); 6212 assertEquals(102400, matchResult.end()); 6213 6214 // skip something without risking a NoSuchElementException 6215 s.skip(Pattern.compile("[ \t]*")); 6216 matchResult = s.match(); 6217 assertEquals(102400, matchResult.start()); 6218 assertEquals(102400, matchResult.end()); 6219 } 6220 6221 /** 6222 * @tests java.util.Scanner#skip(String) 6223 */ 6224 @TestTargetNew( 6225 level = TestLevel.COMPLETE, 6226 notes = "", 6227 method = "skip", 6228 args = {java.lang.String.class} 6229 ) 6230 public void test_skip_LString() { 6231 s = new Scanner("test"); 6232 try { 6233 s.skip((String) null); 6234 fail("Should throw NullPointerException"); 6235 } catch (NullPointerException e) { 6236 // expected 6237 } 6238 6239 // If pattern does not match, NoSuchElementException will be thrown out. 6240 s = new Scanner("1234"); 6241 try { 6242 s.skip("\\p{Lower}"); 6243 fail("Should throw NoSuchElementException"); 6244 } catch (NoSuchElementException e) { 6245 // expected 6246 } 6247 // Then, no matchResult will be thrown out. 6248 try { 6249 s.match(); 6250 fail("Should throw IllegalStateException"); 6251 } catch (IllegalStateException e) { 6252 // expected 6253 } 6254 6255 s.skip("\\p{Digit}"); 6256 MatchResult matchResult = s.match(); 6257 assertEquals(0, matchResult.start()); 6258 assertEquals(1, matchResult.end()); 6259 6260 s.skip("\\p{Digit}+"); 6261 matchResult = s.match(); 6262 assertEquals(1, matchResult.start()); 6263 assertEquals(4, matchResult.end()); 6264 6265 s.close(); 6266 try { 6267 s.skip("test"); 6268 fail("Should throw IllegalStateException"); 6269 } catch (IllegalStateException e) { 6270 // expected 6271 } 6272 6273 MockStringReader2Read reader = new MockStringReader2Read("test"); 6274 s = new Scanner(reader); 6275 try { 6276 s.skip("\\p{Digit}{4}"); 6277 fail("Should throw NoSuchElementException"); 6278 } catch (NoSuchElementException e) { 6279 // expected 6280 } 6281 try { 6282 s.match(); 6283 fail("Should throw IllegalStateException"); 6284 } catch (IllegalStateException e) { 6285 // expected 6286 } 6287 s.skip("\\p{Digit}{3}\\p{Lower}"); 6288 matchResult = s.match(); 6289 assertEquals(0, matchResult.start()); 6290 assertEquals(4, matchResult.end()); 6291 6292 s.close(); 6293 try { 6294 s.skip(""); 6295 fail("Should throw IllegalStateException"); 6296 } catch (IllegalStateException e) { 6297 // expected 6298 } 6299 6300 StringBuilder stringBuilder = new StringBuilder(); 6301 char [] chars = new char[1024]; 6302 Arrays.fill(chars, 'a'); 6303 stringBuilder.append(chars); 6304 stringBuilder.append('3'); 6305 s = new Scanner(stringBuilder.toString()); 6306 s.skip("\\p{Lower}+\\p{Digit}"); 6307 matchResult = s.match(); 6308 assertEquals(0, matchResult.start()); 6309 assertEquals(1025, matchResult.end()); 6310 6311 // Large amount of input may be cached 6312 chars = new char[102400]; 6313 Arrays.fill(chars, 'a'); 6314 stringBuilder = new StringBuilder(); 6315 stringBuilder.append(chars); 6316 s = new Scanner(stringBuilder.toString()); 6317 s.skip(".*"); 6318 matchResult = s.match(); 6319 assertEquals(0, matchResult.start()); 6320 assertEquals(102400, matchResult.end()); 6321 6322 // skip something without risking a NoSuchElementException 6323 s.skip("[ \t]*"); 6324 matchResult = s.match(); 6325 assertEquals(102400, matchResult.start()); 6326 assertEquals(102400, matchResult.end()); 6327 6328 s = new Scanner("test"); 6329 try { 6330 s.skip((String) null); 6331 fail("Should throw NullPointerException"); 6332 } catch (NullPointerException e) { 6333 // expected 6334 } 6335 } 6336 6337 /** 6338 * @throws IOException 6339 * @tests java.util.Scanner#nextDouble() 6340 */ 6341 @TestTargetNew( 6342 level = TestLevel.COMPLETE, 6343 notes = "", 6344 method = "nextDouble", 6345 args = {} 6346 ) 6347 @KnownFailure("Some locales were removed last minute in cupcake") 6348 public void test_nextDouble() throws IOException { 6349 s = new Scanner("123 45\u0666. 123.4 .123 "); 6350 s.useLocale(Locale.ENGLISH); 6351 assertEquals(123.0, s.nextDouble()); 6352 assertEquals(456.0, s.nextDouble()); 6353 assertEquals(123.4, s.nextDouble()); 6354 assertEquals(0.123, s.nextDouble()); 6355 try { 6356 s.nextDouble(); 6357 fail("Should throw NoSuchElementException"); 6358 } catch (NoSuchElementException e) { 6359 // Expected 6360 } 6361 6362 s = new Scanner("+123.4 -456.7 123,456.789 0.1\u06623,4"); 6363 s.useLocale(Locale.ENGLISH); 6364 assertEquals(123.4, s.nextDouble()); 6365 assertEquals(-456.7, s.nextDouble()); 6366 assertEquals(123456.789, s.nextDouble()); 6367 try { 6368 s.nextDouble(); 6369 fail("Should throw InputMismatchException"); 6370 } catch (InputMismatchException e) { 6371 // Expected 6372 } 6373 6374 // Scientific notation 6375 s = new Scanner("+123.4E10 -456.7e+12 123,456.789E-10"); 6376 s.useLocale(Locale.ENGLISH); 6377 assertEquals(1.234E12, s.nextDouble()); 6378 assertEquals(-4.567E14, s.nextDouble()); 6379 assertEquals(1.23456789E-5, s.nextDouble()); 6380 6381 s = new Scanner("NaN Infinity -Infinity"); 6382 assertEquals(Double.NaN, s.nextDouble()); 6383 assertEquals(Double.POSITIVE_INFINITY, s.nextDouble()); 6384 assertEquals(Double.NEGATIVE_INFINITY, s.nextDouble()); 6385 6386 //The following test case fails on RI 6387 s=new Scanner("\u221e"); 6388 s.useLocale(Locale.ENGLISH); 6389// assertEquals(Double.POSITIVE_INFINITY, s.nextDouble()); 6390 6391 String str=String.valueOf(Double.MAX_VALUE*2); 6392 s=new Scanner(str); 6393 assertEquals(Double.POSITIVE_INFINITY,s.nextDouble()); 6394 6395 /* 6396 * Different locale can only recognize corresponding locale sensitive 6397 * string. ',' is used in many locales as group separator. 6398 */ 6399 s = new Scanner("23,456 23,456"); 6400 s.useLocale(Locale.ENGLISH); 6401 assertEquals(23456.0, s.nextDouble()); 6402 s.useLocale(Locale.GERMANY); 6403 assertEquals(23.456, s.nextDouble()); 6404 6405 s = new Scanner("23.456 23.456"); 6406 s.useLocale(Locale.ENGLISH); 6407 assertEquals(23.456, s.nextDouble()); 6408 s.useLocale(Locale.GERMANY); 6409 assertEquals(23456.0, s.nextDouble()); 6410 6411 s = new Scanner("23,456.7 23.456,7"); 6412 s.useLocale(Locale.ENGLISH); 6413 assertEquals(23456.7, s.nextDouble()); 6414 s.useLocale(Locale.GERMANY); 6415 assertEquals(23456.7, s.nextDouble()); 6416 6417 s = new Scanner("-123.4"); 6418 s.useLocale(Locale.ENGLISH); 6419 assertEquals(-123.4, s.nextDouble()); 6420 6421 s.close(); 6422 try { 6423 s.nextDouble(); 6424 fail("IllegalStateException expected"); 6425 } catch (IllegalStateException e) { 6426 //expected 6427 } 6428 } 6429 6430 /** 6431 * @throws IOException 6432 * @tests java.util.Scanner#nextBigDecimal() 6433 */ 6434 @TestTargetNew( 6435 level = TestLevel.COMPLETE, 6436 notes = "", 6437 method = "nextBigDecimal", 6438 args = {} 6439 ) 6440 @KnownFailure("Some locales were removed last minute in cupcake") 6441 public void test_nextBigDecimal() throws IOException { 6442 s = new Scanner("123 45\u0666. 123.4 .123 "); 6443 s.useLocale(Locale.ENGLISH); 6444 assertEquals(new BigDecimal("123"), s.nextBigDecimal()); 6445 assertEquals(new BigDecimal("456"), s.nextBigDecimal()); 6446 assertEquals(new BigDecimal("123.4"), s.nextBigDecimal()); 6447 assertEquals(new BigDecimal("0.123"), s.nextBigDecimal()); 6448 try { 6449 s.nextBigDecimal(); 6450 fail("Should throw NoSuchElementException"); 6451 } catch (NoSuchElementException e) { 6452 // Expected 6453 } 6454 6455 s = new Scanner("+123.4 -456.7 123,456.789 0.1\u06623,4"); 6456 s.useLocale(Locale.ENGLISH); 6457 assertEquals(new BigDecimal("123.4"), s.nextBigDecimal()); 6458 assertEquals(new BigDecimal("-456.7"), s.nextBigDecimal()); 6459 assertEquals(new BigDecimal("123456.789"), s.nextBigDecimal()); 6460 try { 6461 s.nextBigDecimal(); 6462 fail("Should throw InputMismatchException"); 6463 } catch (InputMismatchException e) { 6464 // Expected 6465 } 6466 6467 // Scientific notation 6468 s = new Scanner("+123.4E10 -456.7e+12 123,456.789E-10"); 6469 s.useLocale(Locale.ENGLISH); 6470 assertEquals(new BigDecimal("1.234E12"), s.nextBigDecimal()); 6471 assertEquals(new BigDecimal("-4.567E14"), s.nextBigDecimal()); 6472 assertEquals(new BigDecimal("1.23456789E-5"), s.nextBigDecimal()); 6473 6474 s = new Scanner("NaN"); 6475 try { 6476 s.nextBigDecimal(); 6477 fail("Should throw InputMismatchException"); 6478 } catch (InputMismatchException e) { 6479 // Expected 6480 } 6481 6482 /* 6483 * Different locale can only recognize corresponding locale sensitive 6484 * string. ',' is used in many locales as group separator. 6485 */ 6486 s = new Scanner("23,456 23,456"); 6487 s.useLocale(Locale.ENGLISH); 6488 assertEquals(new BigDecimal("23456"), s.nextBigDecimal()); 6489 s.useLocale(Locale.GERMANY); 6490 assertEquals(new BigDecimal("23.456"), s.nextBigDecimal()); 6491 6492 s = new Scanner("23.456 23.456"); 6493 s.useLocale(Locale.ENGLISH); 6494 assertEquals(new BigDecimal("23.456"), s.nextBigDecimal()); 6495 s.useLocale(Locale.GERMANY); 6496 assertEquals(new BigDecimal("23456"), s.nextBigDecimal()); 6497 6498 s = new Scanner("23,456.7"); 6499 s.useLocale(Locale.ENGLISH); 6500 assertEquals(new BigDecimal("23456.7"), s.nextBigDecimal()); 6501 6502 s = new Scanner("-123.4"); 6503 s.useLocale(Locale.ENGLISH); 6504 assertEquals(new BigDecimal("-123.4"), s.nextBigDecimal()); 6505 6506 s.close(); 6507 try { 6508 s.nextBigDecimal(); 6509 fail("IllegalStateException expected"); 6510 } catch (IllegalStateException e) { 6511 //expected 6512 } 6513 } 6514 6515 /** 6516 * @tests java.util.Scanner#toString() 6517 */ 6518 @TestTargetNew( 6519 level = TestLevel.COMPLETE, 6520 notes = "", 6521 method = "toString", 6522 args = {} 6523 ) 6524 public void test_toString() { 6525 s = new Scanner("test"); 6526 assertNotNull(s.toString()); 6527 } 6528 6529 /** 6530 * @tests java.util.Scanner#nextLine() 6531 */ 6532 @TestTargetNew( 6533 level = TestLevel.COMPLETE, 6534 notes = "", 6535 method = "nextLine", 6536 args = {} 6537 ) 6538 public void test_nextLine() { 6539 s = new Scanner(""); 6540 s.close(); 6541 try { 6542 s.nextLine(); 6543 fail("Should throw IllegalStateException"); 6544 } catch (IllegalStateException e) { 6545 // expected 6546 } 6547 6548 s = new Scanner("test\r\ntest"); 6549 String result = s.nextLine(); 6550 assertEquals("test", result); 6551 MatchResult matchResult = s.match(); 6552 assertEquals(0, matchResult.start()); 6553 assertEquals(6, matchResult.end()); 6554 6555 s = new Scanner("\u0085"); 6556 result = s.nextLine(); 6557 assertEquals("", result); 6558 matchResult = s.match(); 6559 assertEquals(0, matchResult.start()); 6560 assertEquals(1, matchResult.end()); 6561 6562 s = new Scanner("\u2028"); 6563 result = s.nextLine(); 6564 assertEquals("", result); 6565 matchResult = s.match(); 6566 assertEquals(0, matchResult.start()); 6567 assertEquals(1, matchResult.end()); 6568 6569 s = new Scanner("\u2029"); 6570 result = s.nextLine(); 6571 assertEquals("", result); 6572 matchResult = s.match(); 6573 assertEquals(0, matchResult.start()); 6574 assertEquals(1, matchResult.end()); 6575 6576 s = new Scanner(""); 6577 try { 6578 result = s.nextLine(); 6579 fail("Should throw NoSuchElementException"); 6580 } catch (NoSuchElementException e) { 6581 // expected 6582 } 6583 try { 6584 s.match(); 6585 fail("Should throw IllegalStateException"); 6586 } catch (IllegalStateException e) { 6587 // expected 6588 } 6589 6590 s = new Scanner("Ttest"); 6591 result = s.nextLine(); 6592 assertEquals("Ttest", result); 6593 matchResult = s.match(); 6594 assertEquals(0, matchResult.start()); 6595 assertEquals(5, matchResult.end()); 6596 6597 s = new Scanner("\r\n"); 6598 result = s.nextLine(); 6599 assertEquals("", result); 6600 matchResult = s.match(); 6601 assertEquals(0, matchResult.start()); 6602 assertEquals(2, matchResult.end()); 6603 6604 char[] chars = new char[1024]; 6605 Arrays.fill(chars, 'a'); 6606 StringBuilder stringBuilder = new StringBuilder(); 6607 stringBuilder.append(chars); 6608 chars = new char[] { '+', '-' }; 6609 stringBuilder.append(chars); 6610 stringBuilder.append("\u2028"); 6611 s = new Scanner(stringBuilder.toString()); 6612 result = s.nextLine(); 6613 6614 assertEquals(stringBuilder.toString().substring(0, 1026), result); 6615 matchResult = s.match(); 6616 assertEquals(0, matchResult.start()); 6617 assertEquals(1027, matchResult.end()); 6618 6619 chars = new char[1023]; 6620 Arrays.fill(chars, 'a'); 6621 stringBuilder = new StringBuilder(); 6622 stringBuilder.append(chars); 6623 stringBuilder.append("\r\n"); 6624 s = new Scanner(stringBuilder.toString()); 6625 result = s.nextLine(); 6626 6627 assertEquals(stringBuilder.toString().substring(0, 1023), result); 6628 matchResult = s.match(); 6629 assertEquals(0, matchResult.start()); 6630 assertEquals(1025, matchResult.end()); 6631 6632 s = new Scanner(" "); 6633 result = s.nextLine(); 6634 assertEquals(" ", result); 6635 6636 s = new Scanner("test\n\n\n"); 6637 result = s.nextLine(); 6638 assertEquals("test", result); 6639 matchResult = s.match(); 6640 assertEquals(0, matchResult.start()); 6641 assertEquals(5, matchResult.end()); 6642 result = s.nextLine(); 6643 matchResult = s.match(); 6644 assertEquals(5, matchResult.start()); 6645 assertEquals(6, matchResult.end()); 6646 6647 s = new Scanner("\n\n\n"); 6648 result = s.nextLine(); 6649 assertEquals("", result); 6650 matchResult = s.match(); 6651 assertEquals(0, matchResult.start()); 6652 assertEquals(1, matchResult.end()); 6653 result = s.nextLine(); 6654 matchResult = s.match(); 6655 assertEquals(1, matchResult.start()); 6656 assertEquals(2, matchResult.end()); 6657 6658 s = new Scanner("123 test\n "); 6659 int value = s.nextInt(); 6660 assertEquals(123, value); 6661 6662 result = s.nextLine(); 6663 assertEquals(" test", result); 6664 6665 s = new Scanner("test\n "); 6666 result = s.nextLine(); 6667 assertEquals("test", result); 6668 } 6669 6670 /** 6671 * @tests java.util.Scanner#hasNextLine() 6672 */ 6673 @TestTargetNew( 6674 level = TestLevel.COMPLETE, 6675 notes = "", 6676 method = "hasNextLine", 6677 args = {} 6678 ) 6679 public void test_hasNextLine() { 6680 6681 s = new Scanner(""); 6682 s.close(); 6683 try { 6684 s.hasNextLine(); 6685 fail("Should throw IllegalStateException"); 6686 } catch (IllegalStateException e) { 6687 // expected 6688 } 6689 6690 s = new Scanner("test\r\ntest"); 6691 boolean result = s.hasNextLine(); 6692 assertTrue(result); 6693 MatchResult matchResult = s.match(); 6694 assertEquals(0, matchResult.start()); 6695 assertEquals(6, matchResult.end()); 6696 6697 s = new Scanner("\u0085"); 6698 result = s.hasNextLine(); 6699 assertTrue(result); 6700 matchResult = s.match(); 6701 assertEquals(0, matchResult.start()); 6702 assertEquals(1, matchResult.end()); 6703 6704 s = new Scanner("\u2028"); 6705 result = s.hasNextLine(); 6706 assertTrue(result); 6707 matchResult = s.match(); 6708 assertEquals(0, matchResult.start()); 6709 assertEquals(1, matchResult.end()); 6710 6711 s = new Scanner("\u2029"); 6712 result = s.hasNextLine(); 6713 assertTrue(result); 6714 matchResult = s.match(); 6715 assertEquals(0, matchResult.start()); 6716 assertEquals(1, matchResult.end()); 6717 6718 s = new Scanner("test\n"); 6719 assertTrue(s.hasNextLine()); 6720 matchResult = s.match(); 6721 assertEquals(0, matchResult.start()); 6722 assertEquals(5, matchResult.end()); 6723 6724 char[] chars = new char[2048]; 6725 Arrays.fill(chars, 'a'); 6726 StringBuilder stringBuilder = new StringBuilder(); 6727 stringBuilder.append(chars); 6728 s = new Scanner(stringBuilder.toString()); 6729 result = s.hasNextLine(); 6730 assertTrue(result); 6731 6732 matchResult = s.match(); 6733 assertEquals(0, matchResult.start()); 6734 assertEquals(2048, matchResult.end()); 6735 6736 s = new Scanner("\n\n\n"); 6737 assertTrue(s.hasNextLine()); 6738 matchResult = s.match(); 6739 assertEquals(0, matchResult.start()); 6740 assertEquals(1, matchResult.end()); 6741 6742 // The scanner will not advance any input. 6743 assertTrue(s.hasNextLine()); 6744 matchResult = s.match(); 6745 assertEquals(0, matchResult.start()); 6746 assertEquals(1, matchResult.end()); 6747 } 6748 6749 protected void setUp() throws Exception { 6750 super.setUp(); 6751 6752 server = new ServerSocket(0); 6753 address = new InetSocketAddress("127.0.0.1", server.getLocalPort()); 6754 6755 client = SocketChannel.open(); 6756 client.connect(address); 6757 serverSocket = server.accept(); 6758 6759 os = serverSocket.getOutputStream(); 6760 } 6761 6762 protected void tearDown() throws Exception { 6763 super.tearDown(); 6764 6765 try { 6766 serverSocket.close(); 6767 } catch (Exception e) { 6768 6769 } 6770 try { 6771 client.close(); 6772 } catch (Exception e) { 6773 // do nothing 6774 } 6775 try { 6776 server.close(); 6777 } catch (Exception e) { 6778 // do nothing 6779 } 6780 } 6781 6782 /** 6783 * @tests java.util.Scanner#Scanner(ReadableByteChannel) 6784 */ 6785 @TestTargetNew( 6786 level = TestLevel.COMPLETE, 6787 notes = "", 6788 method = "Scanner", 6789 args = {java.nio.channels.ReadableByteChannel.class} 6790 ) 6791 public void test_Constructor_LReadableByteChannel() 6792 throws IOException { 6793 InetSocketAddress localAddr = new InetSocketAddress("127.0.0.1", 6794 Support_PortManager.getNextPort()); 6795 ServerSocketChannel ssc = ServerSocketChannel.open(); 6796 ssc.socket().bind(localAddr); 6797 6798 SocketChannel sc = SocketChannel.open(); 6799 sc.connect(localAddr); 6800 sc.configureBlocking(false); 6801 assertFalse(sc.isBlocking()); 6802 6803 ssc.accept().close(); 6804 ssc.close(); 6805 assertFalse(sc.isBlocking()); 6806 6807 Scanner s = new Scanner(sc); 6808 while (s.hasNextInt()) { 6809 s.nextInt(); 6810 } 6811 6812 sc.close(); 6813 } 6814} 6815