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