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