< prev index next >

test/java/util/Scanner/ScanTest.java

Print this page
rev 12497 : 8072722: add stream support to Scanner
Reviewed-by: XXX

*** 22,50 **** */ /** * @test * @bug 4313885 4926319 4927634 5032610 5032622 5049968 5059533 6223711 6277261 6269946 6288823 * @summary Basic tests of java.util.Scanner methods * @key randomness * @run main/othervm ScanTest */ - import java.util.*; - import java.text.*; import java.io.*; import java.nio.*; import java.util.regex.*; ! import java.math.*; public class ScanTest { private static boolean failure = false; private static int failCount = 0; private static int NUM_SOURCE_TYPES = 2; public static void main(String[] args) throws Exception { Locale reservedLocale = Locale.getDefault(); String lang = reservedLocale.getLanguage(); try { if (!"en".equals(lang) && !"zh".equals(lang) && --- 22,55 ---- */ /** * @test * @bug 4313885 4926319 4927634 5032610 5032622 5049968 5059533 6223711 6277261 6269946 6288823 + * 8072722 * @summary Basic tests of java.util.Scanner methods * @key randomness * @run main/othervm ScanTest */ import java.io.*; + import java.math.*; import java.nio.*; + import java.text.*; + import java.util.*; + import java.util.function.Consumer; import java.util.regex.*; ! import java.util.stream.*; public class ScanTest { private static boolean failure = false; private static int failCount = 0; private static int NUM_SOURCE_TYPES = 2; + private static File inputFile = new File(System.getProperty("test.src", "."), "input.txt"); public static void main(String[] args) throws Exception { + Locale reservedLocale = Locale.getDefault(); String lang = reservedLocale.getLanguage(); try { if (!"en".equals(lang) && !"zh".equals(lang) &&
*** 68,79 **** closeTest(); cacheTest(); cacheTest2(); nonASCIITest(); resetTest(); ! for (int j=0; j<NUM_SOURCE_TYPES; j++) { hasNextTest(j); nextTest(j); hasNextPatternTest(j); nextPatternTest(j); booleanTest(j); --- 73,87 ---- closeTest(); cacheTest(); cacheTest2(); nonASCIITest(); resetTest(); + tokensTest(); + findAllTest(); + streamCloseTest(); ! for (int j = 0; j < NUM_SOURCE_TYPES; j++) { hasNextTest(j); nextTest(j); hasNextPatternTest(j); nextPatternTest(j); booleanTest(j);
*** 113,124 **** Locale.setDefault(reservedLocale); } } public static void useCase1() throws Exception { ! File f = new File(System.getProperty("test.src", "."), "input.txt"); ! Scanner sc = new Scanner(f); sc.findWithinHorizon("usage case 1", 0); String[] names = new String[4]; for (int i=0; i<4; i++) { while(sc.hasNextFloat()) sc.nextFloat(); --- 121,131 ---- Locale.setDefault(reservedLocale); } } public static void useCase1() throws Exception { ! try (Scanner sc = new Scanner(inputFile)) { sc.findWithinHorizon("usage case 1", 0); String[] names = new String[4]; for (int i=0; i<4; i++) { while(sc.hasNextFloat()) sc.nextFloat();
*** 131,147 **** failCount++; if (!names[2].equals("Mary")) failCount++; if (!names[3].equals("Michelle")) failCount++; ! sc.close(); report("Use case 1"); } public static void useCase2() throws Exception { ! File f = new File(System.getProperty("test.src", "."), "input.txt"); ! Scanner sc = new Scanner(f).useDelimiter("-"); String testDataTag = sc.findWithinHorizon("usage case 2\n", 0); if (!testDataTag.equals("usage case 2\n")) failCount++; if (!sc.next().equals("cat")) failCount++; --- 138,153 ---- failCount++; if (!names[2].equals("Mary")) failCount++; if (!names[3].equals("Michelle")) failCount++; ! } report("Use case 1"); } public static void useCase2() throws Exception { ! try (Scanner sc = new Scanner(inputFile).useDelimiter("-")) { String testDataTag = sc.findWithinHorizon("usage case 2\n", 0); if (!testDataTag.equals("usage case 2\n")) failCount++; if (!sc.next().equals("cat")) failCount++;
*** 157,173 **** failCount++; if (!sc.next().equals("")) failCount++; if (sc.nextInt() != 5) failCount++; ! sc.close(); report("Use case 2"); } public static void useCase3() throws Exception { ! File f = new File(System.getProperty("test.src", "."), "input.txt"); ! Scanner sc = new Scanner(f); String testDataTag = sc.findWithinHorizon("usage case 3\n", 0); if (!testDataTag.equals("usage case 3\n")) failCount++; Pattern tagPattern = Pattern.compile("@[a-z]+"); Pattern endPattern = Pattern.compile("\\*\\/"); --- 163,178 ---- failCount++; if (!sc.next().equals("")) failCount++; if (sc.nextInt() != 5) failCount++; ! } report("Use case 2"); } public static void useCase3() throws Exception { ! try (Scanner sc = new Scanner(inputFile)) { String testDataTag = sc.findWithinHorizon("usage case 3\n", 0); if (!testDataTag.equals("usage case 3\n")) failCount++; Pattern tagPattern = Pattern.compile("@[a-z]+"); Pattern endPattern = Pattern.compile("\\*\\/");
*** 182,197 **** } else { sc.nextLine(); } end = sc.findInLine(endPattern); } report("Use case 3"); } public static void useCase4() throws Exception { ! File f = new File(System.getProperty("test.src", "."), "input.txt"); ! Scanner sc = new Scanner(f); String testDataTag = sc.findWithinHorizon("usage case 4\n", 0); if (!testDataTag.equals("usage case 4\n")) failCount++; // Read some text parts of four hrefs --- 187,202 ---- } else { sc.nextLine(); } end = sc.findInLine(endPattern); } + } report("Use case 3"); } public static void useCase4() throws Exception { ! try (Scanner sc = new Scanner(inputFile)) { String testDataTag = sc.findWithinHorizon("usage case 4\n", 0); if (!testDataTag.equals("usage case 4\n")) failCount++; // Read some text parts of four hrefs
*** 225,241 **** String tagContents = sc.next(tag); if (!tagContents.equals(expected2[tagsFound])) failCount++; tagsFound++; } report("Use case 4"); } public static void useCase5() throws Exception { ! File f = new File(System.getProperty("test.src", "."), "input.txt"); ! Scanner sc = new Scanner(f); String testDataTag = sc.findWithinHorizon("usage case 5\n", 0); if (!testDataTag.equals("usage case 5\n")) failCount++; sc.findWithinHorizon("Share Definitions", 0); --- 230,246 ---- String tagContents = sc.next(tag); if (!tagContents.equals(expected2[tagsFound])) failCount++; tagsFound++; } + } report("Use case 4"); } public static void useCase5() throws Exception { ! try (Scanner sc = new Scanner(inputFile)) { String testDataTag = sc.findWithinHorizon("usage case 5\n", 0); if (!testDataTag.equals("usage case 5\n")) failCount++; sc.findWithinHorizon("Share Definitions", 0);
*** 257,266 **** --- 262,272 ---- String value = sc.next(); if (!value.equals(vals[i])) failCount++; sc.nextLine(); } + } report("Use case 5"); } public static void nonASCIITest() throws Exception {
*** 443,458 **** if (!sc.nextLine().equals("blah blah blah blah blah blah")) failCount++; if (sc.hasNextLine()) failCount++; // Go through all the lines in a file ! File f = new File(System.getProperty("test.src", "."), "input.txt"); ! sc = new Scanner(f); String lastLine = "blah"; ! while(sc.hasNextLine()) ! lastLine = sc.nextLine(); if (!lastLine.equals("# Data for usage case 6")) failCount++; report("Has next line test"); } public static void nextLineTest(int sourceType) throws Exception { --- 449,464 ---- if (!sc.nextLine().equals("blah blah blah blah blah blah")) failCount++; if (sc.hasNextLine()) failCount++; // Go through all the lines in a file ! try (Scanner sc2 = new Scanner(inputFile)) { String lastLine = "blah"; ! while(sc2.hasNextLine()) ! lastLine = sc2.nextLine(); if (!lastLine.equals("# Data for usage case 6")) failCount++; + } report("Has next line test"); } public static void nextLineTest(int sourceType) throws Exception {
*** 627,678 **** sc.close(); sc.ioException(); sc.delimiter(); sc.useDelimiter("blah"); sc.useDelimiter(Pattern.compile("blah")); ! for (int i=0; i<NUM_METHODS; i++) { try { ! methodCall(sc, i); failCount++; } catch (IllegalStateException ise) { // Correct } } report("Close test"); } ! private static int NUM_METHODS = 23; ! ! private static void methodCall(Scanner sc, int i) { ! switch(i) { ! case 0: sc.hasNext(); break; ! case 1: sc.next(); break; ! case 2: sc.hasNext(Pattern.compile("blah")); break; ! case 3: sc.next(Pattern.compile("blah")); break; ! case 4: sc.hasNextBoolean(); break; ! case 5: sc.nextBoolean(); break; ! case 6: sc.hasNextByte(); break; ! case 7: sc.nextByte(); break; ! case 8: sc.hasNextShort(); break; ! case 9: sc.nextShort(); break; ! case 10: sc.hasNextInt(); break; ! case 11: sc.nextInt(); break; ! case 12: sc.hasNextLong(); break; ! case 13: sc.nextLong(); break; ! case 14: sc.hasNextFloat(); break; ! case 15: sc.nextFloat(); break; ! case 16: sc.hasNextDouble(); break; ! case 17: sc.nextDouble(); break; ! case 18: sc.hasNextBigInteger(); break; ! case 19: sc.nextBigInteger(); break; ! case 20: sc.hasNextBigDecimal(); break; ! case 21: sc.nextBigDecimal(); break; ! case 22: sc.hasNextLine(); break; ! default: ! break; ! } ! } public static void removeTest() throws Exception { Scanner sc = new Scanner("testing"); try { sc.remove(); --- 633,683 ---- sc.close(); sc.ioException(); sc.delimiter(); sc.useDelimiter("blah"); sc.useDelimiter(Pattern.compile("blah")); ! ! for (Consumer<Scanner> method : methodList) { try { ! method.accept(sc); failCount++; } catch (IllegalStateException ise) { // Correct } } + report("Close test"); } ! static List<Consumer<Scanner>> methodList = Arrays.asList( ! Scanner::hasNext, ! Scanner::next, ! sc -> sc.hasNext(Pattern.compile("blah")), ! sc -> sc.next(Pattern.compile("blah")), ! Scanner::hasNextBoolean, ! Scanner::nextBoolean, ! Scanner::hasNextByte, ! Scanner::nextByte, ! Scanner::hasNextShort, ! Scanner::nextShort, ! Scanner::hasNextInt, ! Scanner::nextInt, ! Scanner::hasNextLong, ! Scanner::nextLong, ! Scanner::hasNextFloat, ! Scanner::nextFloat, ! Scanner::hasNextDouble, ! Scanner::nextDouble, ! Scanner::hasNextBigInteger, ! Scanner::nextBigInteger, ! Scanner::hasNextBigDecimal, ! Scanner::nextBigDecimal, ! Scanner::hasNextLine, ! Scanner::tokens, ! sc -> sc.findAll(Pattern.compile("blah")), ! sc -> sc.findAll("blah") ! ); public static void removeTest() throws Exception { Scanner sc = new Scanner("testing"); try { sc.remove();
*** 862,872 **** } } public static void fromFileTest() throws Exception { File f = new File(System.getProperty("test.src", "."), "input.txt"); ! Scanner sc = new Scanner(f).useDelimiter("\n+"); String testDataTag = sc.findWithinHorizon("fromFileTest", 0); if (!testDataTag.equals("fromFileTest")) failCount++; int count = 0; --- 867,878 ---- } } public static void fromFileTest() throws Exception { File f = new File(System.getProperty("test.src", "."), "input.txt"); ! try (Scanner sc = new Scanner(f)) { ! sc.useDelimiter("\n+"); String testDataTag = sc.findWithinHorizon("fromFileTest", 0); if (!testDataTag.equals("fromFileTest")) failCount++; int count = 0;
*** 874,884 **** long blah = sc.nextLong(); count++; } if (count != 7) failCount++; ! sc.close(); report("From file"); } private static void example1() throws Exception { Scanner s = new Scanner("1 fish 2 fish red fish blue fish"); --- 880,890 ---- long blah = sc.nextLong(); count++; } if (count != 7) failCount++; ! } report("From file"); } private static void example1() throws Exception { Scanner s = new Scanner("1 fish 2 fish red fish blue fish");
*** 1470,1487 **** } sc.close(); report("Reset test"); } private static void report(String testName) { ! int spacesToAdd = 30 - testName.length(); ! StringBuffer paddedNameBuffer = new StringBuffer(testName); ! for (int i=0; i<spacesToAdd; i++) ! paddedNameBuffer.append(" "); ! String paddedName = paddedNameBuffer.toString(); ! System.err.println(paddedName + ": " + ! (failCount==0 ? "Passed":"Failed("+failCount+")")); if (failCount > 0) failure = true; failCount = 0; } --- 1476,1551 ---- } sc.close(); report("Reset test"); } + public static void tokensTest() { + List<String> result = new Scanner("abc def ghi").tokens().collect(Collectors.toList()); + if (! result.equals(Arrays.asList("abc", "def", "ghi"))) { + System.out.println("not equals, failed"); + failCount++; + } + + result = new Scanner("###abc##def###ghi###j").useDelimiter("#+") + .tokens().collect(Collectors.toList()); + if (! result.equals(Arrays.asList("abc", "def", "ghi", "j"))) { + failCount++; + } + + result = new Scanner("abc,def,,ghi").useDelimiter(",") + .tokens().collect(Collectors.toList()); + if (! result.equals(Arrays.asList("abc", "def", "", "ghi"))) { + failCount++; + } + + report("Tokens test"); + } + + public static void findAllTest() throws Exception { + try (Stream<MatchResult> str = new Scanner(inputFile).findAll("[A-Z]{7,}")) { + List<String> result = str.map(MatchResult::group).collect(Collectors.toList()); + if (! result.equals(Arrays.asList("MYGROUP", "NODELAY", "ENCRYPTION"))) { + failCount++; + } + } + + report("FindAll test"); + } + + /* + * Test that closing the stream also closes the underlying Scanner. + * The cases of attempting to open streams on a closed Scanner are + * covered by closeTest(). + */ + public static void streamCloseTest() throws Exception { + Scanner sc; + + sc = new Scanner("xyzzy"); + sc.tokens().close(); + try { + sc.hasNext(); + failCount++; + } catch (IllegalStateException ise) { + // Correct result + } + + sc = new Scanner("xyzzy"); + sc.findAll("q").close(); + try { + sc.hasNext(); + failCount++; + } catch (IllegalStateException ise) { + // Correct result + } + + report("Streams Close test"); + } + private static void report(String testName) { ! System.err.printf("%-30s: %s%n", testName, ! (failCount == 0) ? "Passed" : String.format("Failed(%d)", failCount)); ! if (failCount > 0) failure = true; failCount = 0; }
< prev index next >