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