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