test/java/util/Scanner/ScanTest.java

Print this page




   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug 4313885 4926319 4927634 5032610 5032622 5049968 5059533 6223711 6277261 6269946 6288823
  27  *      8072722 8139414
  28  * @summary Basic tests of java.util.Scanner methods
  29  * @key randomness
  30  * @modules jdk.localedata
  31  * @run main/othervm ScanTest
  32  */
  33 
  34 import java.io.*;
  35 import java.math.*;
  36 import java.nio.*;
  37 import java.text.*;
  38 import java.util.*;

  39 import java.util.function.Consumer;
  40 import java.util.regex.*;
  41 import java.util.stream.*;
  42 
  43 public class ScanTest {
  44 
  45     private static boolean failure = false;
  46     private static int failCount = 0;
  47     private static int NUM_SOURCE_TYPES = 2;
  48     private static File inputFile = new File(System.getProperty("test.src", "."), "input.txt");
  49 
  50     public static void main(String[] args) throws Exception {
  51 
  52         Locale reservedLocale = Locale.getDefault();
  53         String lang = reservedLocale.getLanguage();
  54         try {
  55             if (!"en".equals(lang) &&
  56                 !"zh".equals(lang) &&
  57                 !"ko".equals(lang) &&
  58                 !"ja".equals(lang)) {


  62                 Locale.setDefault(Locale.ENGLISH);
  63             }
  64             skipTest();
  65             findInLineTest();
  66             findWithinHorizonTest();
  67             findInEmptyLineTest();
  68             removeTest();
  69             fromFileTest();
  70             ioExceptionTest();
  71             matchTest();
  72             delimiterTest();
  73             boundaryDelimTest();
  74             useLocaleTest();
  75             closeTest();
  76             cacheTest();
  77             cacheTest2();
  78             nonASCIITest();
  79             resetTest();
  80             streamCloseTest();
  81             streamComodTest();

  82 
  83             for (int j = 0; j < NUM_SOURCE_TYPES; j++) {
  84                 hasNextTest(j);
  85                 nextTest(j);
  86                 hasNextPatternTest(j);
  87                 nextPatternTest(j);
  88                 booleanTest(j);
  89                 byteTest(j);
  90                 shortTest(j);
  91                 intTest(j);
  92                 longTest(j);
  93                 floatTest(j);
  94                 doubleTest(j);
  95                 integerPatternTest(j);
  96                 floatPatternTest(j);
  97                 bigIntegerPatternTest(j);
  98                 bigDecimalPatternTest(j);
  99                 hasNextLineTest(j);
 100                 nextLineTest(j);
 101                 singleDelimTest(j);


1492         sc.useDelimiter(a);
1493         Locale dummy = new Locale("en", "US", "dummy");
1494         sc.useLocale(dummy);
1495         sc.useRadix(16);
1496         if (sc.radix() != 16 ||
1497             !sc.locale().equals(dummy) ||
1498             !sc.delimiter().pattern().equals(a.pattern())) {
1499             failCount++;
1500         } else {
1501             sc.reset();
1502             if (sc.radix() != radix ||
1503                 !sc.locale().equals(locale) ||
1504                 !sc.delimiter().pattern().equals(delimiter.pattern())) {
1505                 failCount++;
1506             }
1507         }
1508         sc.close();
1509         report("Reset test");
1510     }
1511 










































1512     /*
1513      * Test that closing the stream also closes the underlying Scanner.
1514      * The cases of attempting to open streams on a closed Scanner are
1515      * covered by closeTest().
1516      */
1517     public static void streamCloseTest() throws Exception {
1518         Scanner sc;
1519 
1520         Scanner sc1 = new Scanner("xyzzy");
1521         sc1.tokens().close();
1522         try {
1523             sc1.hasNext();
1524             failCount++;
1525         } catch (IllegalStateException ise) {
1526             // Correct result
1527         }
1528 
1529         Scanner sc2 = new Scanner("a b c d e f");
1530         try {
1531             sc2.tokens()




   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug 4313885 4926319 4927634 5032610 5032622 5049968 5059533 6223711 6277261 6269946 6288823
  27  *      8072722 8139414 8166261
  28  * @summary Basic tests of java.util.Scanner methods
  29  * @key randomness
  30  * @modules jdk.localedata
  31  * @run main/othervm ScanTest
  32  */
  33 
  34 import java.io.*;
  35 import java.math.*;
  36 import java.nio.*;
  37 import java.text.*;
  38 import java.util.*;
  39 import java.util.function.BiConsumer;
  40 import java.util.function.Consumer;
  41 import java.util.regex.*;
  42 import java.util.stream.*;
  43 
  44 public class ScanTest {
  45 
  46     private static boolean failure = false;
  47     private static int failCount = 0;
  48     private static int NUM_SOURCE_TYPES = 2;
  49     private static File inputFile = new File(System.getProperty("test.src", "."), "input.txt");
  50 
  51     public static void main(String[] args) throws Exception {
  52 
  53         Locale reservedLocale = Locale.getDefault();
  54         String lang = reservedLocale.getLanguage();
  55         try {
  56             if (!"en".equals(lang) &&
  57                 !"zh".equals(lang) &&
  58                 !"ko".equals(lang) &&
  59                 !"ja".equals(lang)) {


  63                 Locale.setDefault(Locale.ENGLISH);
  64             }
  65             skipTest();
  66             findInLineTest();
  67             findWithinHorizonTest();
  68             findInEmptyLineTest();
  69             removeTest();
  70             fromFileTest();
  71             ioExceptionTest();
  72             matchTest();
  73             delimiterTest();
  74             boundaryDelimTest();
  75             useLocaleTest();
  76             closeTest();
  77             cacheTest();
  78             cacheTest2();
  79             nonASCIITest();
  80             resetTest();
  81             streamCloseTest();
  82             streamComodTest();
  83             outOfRangeRadixTest();
  84 
  85             for (int j = 0; j < NUM_SOURCE_TYPES; j++) {
  86                 hasNextTest(j);
  87                 nextTest(j);
  88                 hasNextPatternTest(j);
  89                 nextPatternTest(j);
  90                 booleanTest(j);
  91                 byteTest(j);
  92                 shortTest(j);
  93                 intTest(j);
  94                 longTest(j);
  95                 floatTest(j);
  96                 doubleTest(j);
  97                 integerPatternTest(j);
  98                 floatPatternTest(j);
  99                 bigIntegerPatternTest(j);
 100                 bigDecimalPatternTest(j);
 101                 hasNextLineTest(j);
 102                 nextLineTest(j);
 103                 singleDelimTest(j);


1494         sc.useDelimiter(a);
1495         Locale dummy = new Locale("en", "US", "dummy");
1496         sc.useLocale(dummy);
1497         sc.useRadix(16);
1498         if (sc.radix() != 16 ||
1499             !sc.locale().equals(dummy) ||
1500             !sc.delimiter().pattern().equals(a.pattern())) {
1501             failCount++;
1502         } else {
1503             sc.reset();
1504             if (sc.radix() != radix ||
1505                 !sc.locale().equals(locale) ||
1506                 !sc.delimiter().pattern().equals(delimiter.pattern())) {
1507                 failCount++;
1508             }
1509         }
1510         sc.close();
1511         report("Reset test");
1512     }
1513 
1514     static List<BiConsumer <Scanner, Integer>> methodWRList = Arrays.asList(
1515         (s, r) -> s.hasNextByte(r),
1516         (s, r) -> s.nextByte(r),
1517         (s, r) -> s.hasNextShort(r),
1518         (s, r) -> s.nextShort(r),
1519         (s, r) -> s.hasNextInt(r),
1520         (s, r) -> s.nextInt(r),
1521         (s, r) -> s.hasNextLong(r),
1522         (s, r) -> s.nextLong(r),
1523         (s, r) -> s.hasNextBigInteger(r),
1524         (s, r) -> s.nextBigInteger(r)
1525     );
1526 
1527     /*
1528      * Test that setting the radix to an out of range value triggers
1529      * an IllegalArgumentException
1530      */
1531     public static void outOfRangeRadixTest() throws Exception {
1532         int[] bad = new int[] { -1, 0,  1, 37, 38 };
1533         int[] good = IntStream.rangeClosed(Character.MIN_RADIX, Character.MAX_RADIX)
1534                               .toArray();
1535 
1536         methodWRList.stream().forEach( m -> {
1537             for (int r : bad) {
1538                 try (Scanner sc = new Scanner("10 10 10 10")) {
1539                     m.accept(sc, r);
1540                     failCount++;
1541                 } catch (IllegalArgumentException ise) {}
1542             }
1543         });
1544         methodWRList.stream().forEach( m -> {
1545             for (int r : good) {
1546                 try (Scanner sc = new Scanner("10 10 10 10")) {
1547                     m.accept(sc, r);
1548                 } catch (Exception x) {
1549                     failCount++;
1550                 }
1551             }
1552         });
1553         report("Radix out of range test");
1554     }
1555 
1556     /*
1557      * Test that closing the stream also closes the underlying Scanner.
1558      * The cases of attempting to open streams on a closed Scanner are
1559      * covered by closeTest().
1560      */
1561     public static void streamCloseTest() throws Exception {
1562         Scanner sc;
1563 
1564         Scanner sc1 = new Scanner("xyzzy");
1565         sc1.tokens().close();
1566         try {
1567             sc1.hasNext();
1568             failCount++;
1569         } catch (IllegalStateException ise) {
1570             // Correct result
1571         }
1572 
1573         Scanner sc2 = new Scanner("a b c d e f");
1574         try {
1575             sc2.tokens()