Path: blob/master/test/jdk/java/util/Scanner/ScannerStreamTest.java
41149 views
/*1* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import org.testng.annotations.DataProvider;24import org.testng.annotations.Test;2526import java.io.File;27import java.io.IOException;28import java.io.UncheckedIOException;2930import java.util.ArrayList;31import java.util.List;32import java.util.Scanner;33import java.util.function.Consumer;34import java.util.function.Supplier;35import java.util.regex.Matcher;36import java.util.regex.MatchResult;37import java.util.regex.Pattern;38import java.util.stream.LambdaTestHelpers;39import java.util.stream.OpTestCase;40import java.util.stream.Stream;41import java.util.stream.TestData;4243import static org.testng.Assert.*;4445/**46* @test47* @bug 8072722 815048848* @summary Tests of stream support in java.util.Scanner49* @library /lib/testlibrary/bootlib50* @build java.base/java.util.stream.OpTestCase51* @run testng/othervm ScannerStreamTest52*/5354@Test55public class ScannerStreamTest extends OpTestCase {5657static File inputFile = new File(System.getProperty("test.src", "."), "input.txt");5859@DataProvider(name = "Tokens")60public static Object[][] makeTokensTestData() {61// each inner array is [String description, String input, String delimiter]62// delimiter may be null63List<Object[]> data = new ArrayList<>();6465data.add(new Object[] { "default delimiter", "abc def ghi", null });66data.add(new Object[] { "fixed delimiter", "abc,def,,ghi", "," });67data.add(new Object[] { "regex delimiter", "###abc##def###ghi###j", "#+" });6869return data.toArray(new Object[0][]);70}7172/*73* Creates a scanner over the input, applying a delimiter if non-null.74*/75Scanner makeScanner(String input, String delimiter) {76Scanner sc = new Scanner(input);77if (delimiter != null) {78sc.useDelimiter(delimiter);79}80return sc;81}8283/*84* Given input and a delimiter, tests that tokens() returns the same85* results that would be provided by a Scanner hasNext/next loop.86*/87@Test(dataProvider = "Tokens")88public void tokensTest(String description, String input, String delimiter) {89// derive expected result by using conventional loop90Scanner sc = makeScanner(input, delimiter);91List<String> expected = new ArrayList<>();92while (sc.hasNext()) {93expected.add(sc.next());94}9596Supplier<Stream<String>> ss = () -> makeScanner(input, delimiter).tokens();97withData(TestData.Factory.ofSupplier(description, ss))98.stream(LambdaTestHelpers.identity())99.expectedResult(expected)100.exercise();101}102103/*104* Creates a Scanner over the given input file.105*/106Scanner makeFileScanner(File file) {107try {108return new Scanner(file, "UTF-8");109} catch (IOException ioe) {110throw new UncheckedIOException(ioe);111}112}113114/*115* Tests that the matches produced by findAll(pat) are the same116* as what are returned by findWithinHorizon(pat, 0). This tests117* a single pattern against a single input file.118*/119public void findAllFileTest() {120// derive expected result by using conventional loop121Pattern pat = Pattern.compile("[A-Z]{7,}");122List<String> expected = new ArrayList<>();123124try (Scanner sc = makeFileScanner(inputFile)) {125String match;126while ((match = sc.findWithinHorizon(pat, 0)) != null) {127expected.add(match);128}129}130131Supplier<Stream<String>> ss =132() -> makeFileScanner(inputFile).findAll(pat).map(MatchResult::group);133134withData(TestData.Factory.ofSupplier("findAllFileTest", ss))135.stream(LambdaTestHelpers.identity())136.expectedResult(expected)137.exercise();138}139140@DataProvider(name = "FindAllZero")141public static Object[][] makeFindAllZeroTestData() {142// each inner array is [String input, String patternString]143List<Object[]> data = new ArrayList<>();144145data.add(new Object[] { "aaaaa", "a*" });146data.add(new Object[] { "aaaaab", "a*" });147data.add(new Object[] { "aaaaabb", "a*" });148data.add(new Object[] { "aaaaabbb", "a*" });149data.add(new Object[] { "aaabbaaaa", "a*" });150data.add(new Object[] { "aaabbaaaab", "a*" });151data.add(new Object[] { "aaabbaaaabb", "a*" });152data.add(new Object[] { "aaabbaaaabbb", "a*" });153data.add(new Object[] { "aaabbaaaa", "a*|b*" });154data.add(new Object[] { "aaabbaaaab", "a*|b*" });155data.add(new Object[] { "aaabbaaaabb", "a*|b*" });156data.add(new Object[] { "aaabbaaaabbb", "a*|b*" });157158return data.toArray(new Object[0][]);159}160161/*162* Tests findAll() using a pattern against an input string.163* The results from findAll() should equal the results obtained164* using a loop around Matcher.find().165*166* The provided regexes should allow zero-length matches.167* This primarily tests the auto-advance feature of findAll() that168* occurs if the regex match is of zero length to see if it has the169* same behavior as Matcher.find()'s auto-advance (JDK-8150488).170* Without auto-advance, findAll() would return an infinite stream171* of zero-length matches. Apply a limit to the stream so172* that an infinite stream will be truncated. The limit must be173* high enough that the resulting truncated stream won't be174* mistaken for a correct expected result.175*/176@Test(dataProvider = "FindAllZero")177public void findAllZeroTest(String input, String patternString) {178Pattern pattern = Pattern.compile(patternString);179180// generate expected result using Matcher.find()181Matcher m = pattern.matcher(input);182List<String> expected = new ArrayList<>();183while (m.find()) {184expected.add(m.group());185}186187Supplier<Stream<String>> ss = () -> new Scanner(input).findAll(pattern)188.limit(100)189.map(MatchResult::group);190191withData(TestData.Factory.ofSupplier("findAllZeroTest", ss))192.stream(LambdaTestHelpers.identity())193.expectedResult(expected)194.exercise();195}196}197198199