1 /*
   2  * Copyright (c) 2013, 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 package org.openjdk.tests.java.util.regex;
  24 
  25 import org.testng.annotations.DataProvider;
  26 import org.testng.annotations.Test;
  27 
  28 import java.util.ArrayList;
  29 import java.util.List;
  30 import java.util.function.Supplier;
  31 import java.util.regex.Pattern;
  32 import java.util.stream.LambdaTestHelpers;
  33 import java.util.stream.OpTestCase;
  34 import java.util.stream.Stream;
  35 import java.util.stream.TestData;
  36 
  37 @Test
  38 public class PatternTest extends OpTestCase {
  39 
  40     @DataProvider(name = "Stream<String>")
  41     public static Object[][] makeStreamTestData() {
  42         List<Object[]> data = new ArrayList<>();
  43 
  44         String description = "";
  45         String input = "awgqwefg1fefw4vssv1vvv1";
  46         Pattern pattern = Pattern.compile("4");
  47         List<String> expected = new ArrayList<>();
  48         expected.add("awgqwefg1fefw");
  49         expected.add("vssv1vvv1");
  50 
  51         // Must match the type signature of the consumer of this data, testStrings
  52         // String, String, Pattern, List<String>
  53         data.add(new Object[]{description, input, pattern, expected});
  54 
  55         input = "afbfq\u00a3abgwgb\u00a3awngnwggw\u00a3a\u00a3ahjrnhneerh";
  56         pattern = Pattern.compile("\u00a3a");
  57         expected = new ArrayList<>();
  58         expected.add("afbfq");
  59         expected.add("bgwgb");
  60         expected.add("wngnwggw");
  61         expected.add("");
  62         expected.add("hjrnhneerh");
  63 
  64         data.add(new Object[]{description, input, pattern, expected});
  65 
  66 
  67         input = "awgqwefg1fefw4vssv1vvv1";
  68         pattern = Pattern.compile("1");
  69         expected = new ArrayList<>();
  70         expected.add("awgqwefg");
  71         expected.add("fefw4vssv");
  72         expected.add("vvv");
  73 
  74         data.add(new Object[]{description, input, pattern, expected});
  75 
  76 
  77         input = "a\u4ebafg1fefw\u4eba4\u9f9cvssv\u9f9c1v\u672c\u672cvv";
  78         pattern = Pattern.compile("1");
  79         expected = new ArrayList<>();
  80         expected.add("a\u4ebafg");
  81         expected.add("fefw\u4eba4\u9f9cvssv\u9f9c");
  82         expected.add("v\u672c\u672cvv");
  83 
  84         data.add(new Object[]{description, input, pattern, expected});
  85 
  86 
  87         input = "1\u56da23\u56da456\u56da7890";
  88         pattern = Pattern.compile("\u56da");
  89         expected = new ArrayList<>();
  90         expected.add("1");
  91         expected.add("23");
  92         expected.add("456");
  93         expected.add("7890");
  94 
  95         data.add(new Object[]{description, input, pattern, expected});
  96 
  97 
  98         input = "1\u56da23\u9f9c\u672c\u672c\u56da456\u56da\u9f9c\u672c7890";
  99         pattern = Pattern.compile("\u56da");
 100         expected = new ArrayList<>();
 101         expected.add("1");
 102         expected.add("23\u9f9c\u672c\u672c");
 103         expected.add("456");
 104         expected.add("\u9f9c\u672c7890");
 105 
 106         data.add(new Object[]{description, input, pattern, expected});
 107 
 108 
 109         input = "";
 110         pattern = Pattern.compile("\u56da");
 111         expected = new ArrayList<>();
 112 
 113         data.add(new Object[]{description, input, pattern, expected});
 114 
 115 
 116         description = "Multiple separators";
 117         input = "This is,testing: with\tdifferent separators.";
 118         pattern = Pattern.compile("[ \t,:.]");
 119         expected = new ArrayList<>();
 120         expected.add("This");
 121         expected.add("is");
 122         expected.add("testing");
 123         expected.add("");
 124         expected.add("with");
 125         expected.add("different");
 126         expected.add("separators");
 127 
 128         data.add(new Object[] {description, input, pattern, expected});
 129         return data.toArray(new Object[0][]);
 130     }
 131 
 132     @Test(dataProvider = "Stream<String>")
 133     public void testStrings(String description, String input, Pattern pattern, List<String> expected) {
 134         Supplier<Stream<String>> ss =  () -> pattern.splitAsStream(input);
 135         withData(TestData.Factory.ofSupplier(description, ss))
 136                 .stream(LambdaTestHelpers.identity())
 137                 .expectedResult(expected)
 138                 .exercise();
 139     }
 140 }