1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   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 import org.testng.annotations.DataProvider;
  25 import org.testng.annotations.Test;
  26 
  27 import java.io.File;
  28 import java.io.IOException;
  29 import java.io.UncheckedIOException;
  30 
  31 import java.util.ArrayList;
  32 import java.util.List;
  33 import java.util.Scanner;
  34 import java.util.function.Consumer;
  35 import java.util.function.Supplier;
  36 import java.util.regex.MatchResult;
  37 import java.util.regex.Pattern;
  38 import java.util.stream.LambdaTestHelpers;
  39 import java.util.stream.OpTestCase;
  40 import java.util.stream.Stream;
  41 import java.util.stream.TestData;
  42 
  43 import static org.testng.Assert.*;
  44 
  45 /**
  46  * @test
  47  * @bug 8072722
  48  * @summary Tests of stream support in java.util.Scanner
  49  * @library ../stream/bootlib
  50  * @build java.util.stream.OpTestCase
  51  * @run testng/othervm ScannerStreamTest
  52  */
  53 
  54 @Test
  55 public class ScannerStreamTest extends OpTestCase {
  56 
  57     static File inputFile = new File(System.getProperty("test.src", "."), "input.txt");
  58 
  59     @DataProvider(name = "Patterns")
  60     public static Object[][] makeStreamTestData() {
  61         // each inner array is [String description, String input, String delimiter]
  62         // delimiter may be null
  63         List<Object[]> data = new ArrayList<>();
  64 
  65         data.add(new Object[] { "default delimiter", "abc def ghi",           null });
  66         data.add(new Object[] { "fixed delimiter",   "abc,def,,ghi",          "," });
  67         data.add(new Object[] { "regexp delimiter",  "###abc##def###ghi###j", "#+" });
  68 
  69         return data.toArray(new Object[0][]);
  70     }
  71 
  72     Scanner makeScanner(String input, String delimiter) {
  73         Scanner sc = new Scanner(input);
  74         if (delimiter != null) {
  75             sc.useDelimiter(delimiter);
  76         }
  77         return sc;
  78     }
  79 
  80     @Test(dataProvider = "Patterns")
  81     public void tokensTest(String description, String input, String delimiter) {
  82         // derive expected result by using conventional loop
  83         Scanner sc = makeScanner(input, delimiter);
  84         List<String> expected = new ArrayList<>();
  85         while (sc.hasNext()) {
  86             expected.add(sc.next());
  87         }
  88 
  89         Supplier<Stream<String>> ss = () -> makeScanner(input, delimiter).tokens();
  90         withData(TestData.Factory.ofSupplier(description, ss))
  91                 .stream(LambdaTestHelpers.identity())
  92                 .expectedResult(expected)
  93                 .exercise();
  94     }
  95 
  96     Scanner makeFileScanner(File file) {
  97         try {
  98             return new Scanner(file, "UTF-8");
  99         } catch (IOException ioe) {
 100             throw new UncheckedIOException(ioe);
 101         }
 102     }
 103 
 104     public void findAllTest() {
 105         // derive expected result by using conventional loop
 106         Pattern pat = Pattern.compile("[A-Z]{7,}");
 107         List<String> expected = new ArrayList<>();
 108 
 109         try (Scanner sc = makeFileScanner(inputFile)) {
 110             String match;
 111             while ((match = sc.findWithinHorizon(pat, 0)) != null) {
 112                 expected.add(match);
 113             }
 114         }
 115 
 116         Supplier<Stream<String>> ss =
 117             () -> makeFileScanner(inputFile).findAll(pat).map(MatchResult::group);
 118 
 119         withData(TestData.Factory.ofSupplier("findAllTest", ss))
 120                 .stream(LambdaTestHelpers.identity())
 121                 .expectedResult(expected)
 122                 .exercise();
 123     }
 124 
 125 }