/* * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /** * @test * @bug 4313885 4926319 4927634 5032610 5032622 5049968 5059533 6223711 6277261 6269946 6288823 * 8072722 8139414 8166261 * @summary Basic tests of java.util.Scanner methods * @key randomness * @modules jdk.localedata * @run main/othervm ScanTest */ import java.io.*; import java.math.*; import java.nio.*; import java.text.*; import java.util.*; import java.util.function.BiConsumer; 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) && !"ko".equals(lang) && !"ja".equals(lang)) { //Before we have resource to improve the test to be ready for //arbitrary locale, force the default locale to be "English" //for now. Locale.setDefault(Locale.ENGLISH); } skipTest(); findInLineTest(); findWithinHorizonTest(); findInEmptyLineTest(); removeTest(); fromFileTest(); ioExceptionTest(); matchTest(); delimiterTest(); boundaryDelimTest(); useLocaleTest(); closeTest(); cacheTest(); cacheTest2(); nonASCIITest(); resetTest(); streamCloseTest(); streamComodTest(); outOfRangeRadixTest(); for (int j = 0; j < NUM_SOURCE_TYPES; j++) { hasNextTest(j); nextTest(j); hasNextPatternTest(j); nextPatternTest(j); booleanTest(j); byteTest(j); shortTest(j); intTest(j); longTest(j); floatTest(j); doubleTest(j); integerPatternTest(j); floatPatternTest(j); bigIntegerPatternTest(j); bigDecimalPatternTest(j); hasNextLineTest(j); nextLineTest(j); singleDelimTest(j); } // Examples //example1(); //example2(); //example3(); // Usage cases useCase1(); useCase2(); useCase3(); useCase4(); useCase5(); if (failure) throw new RuntimeException("Failure in the scanning tests."); else System.err.println("OKAY: All tests passed."); } finally { // restore the default locale 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(); names[i] = sc.next(); sc.nextLine(); } if (!names[0].equals("Frank")) failCount++; if (!names[1].equals("Joe")) 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++; if (sc.nextInt() != 9) failCount++; if (!sc.next().equals("dog")) failCount++; if (sc.nextInt() != 6) failCount++; if (!sc.next().equals("pig")) failCount++; if (sc.nextInt() != 2) 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("\\*\\/"); String tag; String end = sc.findInLine(endPattern); while (end == null) { if ((tag = sc.findInLine(tagPattern)) != null) { String text = sc.nextLine(); text = text.substring(0, text.length() - 1); //System.out.println(text); } 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 String[] expected = { "Diffs", "Sdiffs", "Old", "New" }; for (int i=0; i<4; i++) { sc.findWithinHorizon("\n]+"); sc.next(); String textOfRef = sc.next(); if (!textOfRef.equals(expected[i])) failCount++; } // Read some html tags using < and > as delimiters if (!sc.next().equals("/a")) failCount++; if (!sc.next().equals("b")) failCount++; // Scan some html tags using skip and next Pattern nonTagStart = Pattern.compile("[^<]+"); Pattern tag = Pattern.compile("<[^>]+?>"); Pattern spotAfterTag = Pattern.compile("(?<=>)"); String[] expected2 = { "", "

", "