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