2013-08-20 21:23:32 +00:00
|
|
|
/*
|
2017-05-12 04:53:50 +00:00
|
|
|
* Copyright (c) 2013, 2017, Oracle and/or its affiliates. All rights reserved.
|
2013-08-20 21:23:32 +00:00
|
|
|
* 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
|
2015-12-13 14:10:13 +00:00
|
|
|
* @bug 8016846 8024341 8071479 8145006
|
2015-03-03 11:30:48 +00:00
|
|
|
* @summary Unit tests stream and lambda-based methods on Pattern and Matcher
|
2017-05-12 04:53:50 +00:00
|
|
|
* @library /lib/testlibrary/bootlib
|
8142968: Module System implementation
Initial integration of JEP 200, JEP 260, JEP 261, and JEP 282
Co-authored-by: Alex Buckley <alex.buckley@oracle.com>
Co-authored-by: Jonathan Gibbons <jonathan.gibbons@oracle.com>
Co-authored-by: Karen Kinnear <karen.kinnear@oracle.com>
Co-authored-by: Mandy Chung <mandy.chung@oracle.com>
Co-authored-by: Mark Reinhold <mark.reinhold@oracle.com>
Co-authored-by: Chris Hegarty <chris.hegarty@oracle.com>
Co-authored-by: Alexandr Scherbatiy <alexandr.scherbatiy@oracle.com>
Co-authored-by: Amy Lu <amy.lu@oracle.com>
Co-authored-by: Calvin Cheung <calvin.cheung@oracle.com>
Co-authored-by: Daniel Fuchs <daniel.fuchs@oracle.com>
Co-authored-by: Erik Joelsson <erik.joelsson@oracle.com>
Co-authored-by: Harold Seigel <harold.seigel@oracle.com>
Co-authored-by: Jaroslav Bachorik <jaroslav.bachorik@oracle.com>
Co-authored-by: Jean-Francois Denise <jean-francois.denise@oracle.com>
Co-authored-by: Jan Lahoda <jan.lahoda@oracle.com>
Co-authored-by: James Laskey <james.laskey@oracle.com>
Co-authored-by: Lois Foltan <lois.foltan@oracle.com>
Co-authored-by: Miroslav Kos <miroslav.kos@oracle.com>
Co-authored-by: Huaming Li <huaming.li@oracle.com>
Co-authored-by: Sean Mullan <sean.mullan@oracle.com>
Co-authored-by: Naoto Sato <naoto.sato@oracle.com>
Co-authored-by: Masayoshi Okutsu <masayoshi.okutsu@oracle.com>
Co-authored-by: Peter Levart <peter.levart@gmail.com>
Co-authored-by: Philip Race <philip.race@oracle.com>
Co-authored-by: Claes Redestad <claes.redestad@oracle.com>
Co-authored-by: Sergey Bylokhov <sergey.bylokhov@oracle.com>
Co-authored-by: Alexandre Iline <alexandre.iline@oracle.com>
Co-authored-by: Volker Simonis <volker.simonis@gmail.com>
Co-authored-by: Staffan Larsen <staffan.larsen@oracle.com>
Co-authored-by: Stuart Marks <stuart.marks@oracle.com>
Co-authored-by: Semyon Sadetsky <semyon.sadetsky@oracle.com>
Co-authored-by: Serguei Spitsyn <serguei.spitsyn@oracle.com>
Co-authored-by: Sundararajan Athijegannathan <sundararajan.athijegannathan@oracle.com>
Co-authored-by: Valerie Peng <valerie.peng@oracle.com>
Co-authored-by: Vincent Ryan <vincent.x.ryan@oracle.com>
Co-authored-by: Weijun Wang <weijun.wang@oracle.com>
Co-authored-by: Yuri Nesterenko <yuri.nesterenko@oracle.com>
Co-authored-by: Yekaterina Kantserova <yekaterina.kantserova@oracle.com>
Co-authored-by: Alexander Kulyakthin <alexander.kulyakhtin@oracle.com>
Co-authored-by: Felix Yang <felix.yang@oracle.com>
Co-authored-by: Andrei Eremeev <andrei.eremeev@oracle.com>
Co-authored-by: Frank Yuan <frank.yuan@oracle.com>
Co-authored-by: Sergei Pikalev <sergei.pikalev@oracle.com>
Co-authored-by: Sibabrata Sahoo <sibabrata.sahoo@oracle.com>
Co-authored-by: Tiantian Du <tiantian.du@oracle.com>
Co-authored-by: Sha Jiang <sha.jiang@oracle.com>
Reviewed-by: alanb, mchung, naoto, rriggs, psandoz, plevart, mullan, ascarpino, vinnie, prr, sherman, dfuchs, mhaupt
2016-03-17 19:04:16 +00:00
|
|
|
* @build java.base/java.util.stream.OpTestCase
|
2013-09-21 00:11:32 +00:00
|
|
|
* @run testng/othervm PatternStreamTest
|
2013-08-20 21:23:32 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
import org.testng.annotations.DataProvider;
|
|
|
|
import org.testng.annotations.Test;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2015-03-03 11:30:48 +00:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.ConcurrentModificationException;
|
2013-08-20 21:23:32 +00:00
|
|
|
import java.util.List;
|
2015-03-03 11:30:48 +00:00
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
2013-08-20 21:23:32 +00:00
|
|
|
import java.util.function.Supplier;
|
2015-03-03 11:30:48 +00:00
|
|
|
import java.util.regex.MatchResult;
|
|
|
|
import java.util.regex.Matcher;
|
2013-08-20 21:23:32 +00:00
|
|
|
import java.util.regex.Pattern;
|
2015-12-13 14:10:13 +00:00
|
|
|
import java.util.stream.Collectors;
|
2013-08-20 21:23:32 +00:00
|
|
|
import java.util.stream.LambdaTestHelpers;
|
|
|
|
import java.util.stream.OpTestCase;
|
|
|
|
import java.util.stream.Stream;
|
|
|
|
import java.util.stream.TestData;
|
|
|
|
|
2015-03-03 11:30:48 +00:00
|
|
|
import static org.testng.Assert.*;
|
|
|
|
|
2013-08-20 21:23:32 +00:00
|
|
|
@Test
|
2013-09-21 00:11:32 +00:00
|
|
|
public class PatternStreamTest extends OpTestCase {
|
2013-08-20 21:23:32 +00:00
|
|
|
|
2015-03-03 11:30:48 +00:00
|
|
|
@DataProvider(name = "Patterns")
|
2013-08-20 21:23:32 +00:00
|
|
|
public static Object[][] makeStreamTestData() {
|
2015-03-03 11:30:48 +00:00
|
|
|
// Each item must match the type signature of the consumer of this data
|
|
|
|
// String, String, Pattern
|
2013-08-20 21:23:32 +00:00
|
|
|
List<Object[]> data = new ArrayList<>();
|
|
|
|
|
2015-03-03 11:30:48 +00:00
|
|
|
String description = "All matches";
|
|
|
|
String input = "XXXXXX";
|
|
|
|
Pattern pattern = Pattern.compile("X");
|
|
|
|
data.add(new Object[]{description, input, pattern});
|
2013-08-20 21:23:32 +00:00
|
|
|
|
2015-03-03 11:30:48 +00:00
|
|
|
description = "Bounded every other match";
|
|
|
|
input = "XYXYXYYXYX";
|
|
|
|
pattern = Pattern.compile("X");
|
|
|
|
data.add(new Object[]{description, input, pattern});
|
2013-08-20 21:23:32 +00:00
|
|
|
|
2015-03-03 11:30:48 +00:00
|
|
|
description = "Every other match";
|
|
|
|
input = "YXYXYXYYXYXY";
|
|
|
|
pattern = Pattern.compile("X");
|
|
|
|
data.add(new Object[]{description, input, pattern});
|
2013-08-20 21:23:32 +00:00
|
|
|
|
2015-03-03 11:30:48 +00:00
|
|
|
description = "";
|
|
|
|
input = "awgqwefg1fefw4vssv1vvv1";
|
|
|
|
pattern = Pattern.compile("4");
|
|
|
|
data.add(new Object[]{description, input, pattern});
|
2013-08-20 21:23:32 +00:00
|
|
|
|
2015-03-03 11:30:48 +00:00
|
|
|
input = "afbfq\u00a3abgwgb\u00a3awngnwggw\u00a3a\u00a3ahjrnhneerh";
|
|
|
|
pattern = Pattern.compile("\u00a3a");
|
|
|
|
data.add(new Object[]{description, input, pattern});
|
2013-08-20 21:23:32 +00:00
|
|
|
|
|
|
|
input = "awgqwefg1fefw4vssv1vvv1";
|
|
|
|
pattern = Pattern.compile("1");
|
2015-03-03 11:30:48 +00:00
|
|
|
data.add(new Object[]{description, input, pattern});
|
2013-08-20 21:23:32 +00:00
|
|
|
|
|
|
|
input = "a\u4ebafg1fefw\u4eba4\u9f9cvssv\u9f9c1v\u672c\u672cvv";
|
|
|
|
pattern = Pattern.compile("1");
|
2015-03-03 11:30:48 +00:00
|
|
|
data.add(new Object[]{description, input, pattern});
|
2013-08-20 21:23:32 +00:00
|
|
|
|
|
|
|
input = "1\u56da23\u56da456\u56da7890";
|
|
|
|
pattern = Pattern.compile("\u56da");
|
2015-03-03 11:30:48 +00:00
|
|
|
data.add(new Object[]{description, input, pattern});
|
2013-08-20 21:23:32 +00:00
|
|
|
|
|
|
|
input = "1\u56da23\u9f9c\u672c\u672c\u56da456\u56da\u9f9c\u672c7890";
|
|
|
|
pattern = Pattern.compile("\u56da");
|
2015-03-03 11:30:48 +00:00
|
|
|
data.add(new Object[]{description, input, pattern});
|
2013-08-20 21:23:32 +00:00
|
|
|
|
2015-02-13 10:13:30 +00:00
|
|
|
description = "Empty input";
|
2013-08-20 21:23:32 +00:00
|
|
|
input = "";
|
|
|
|
pattern = Pattern.compile("\u56da");
|
2015-03-03 11:30:48 +00:00
|
|
|
data.add(new Object[]{description, input, pattern});
|
2015-02-13 10:13:30 +00:00
|
|
|
|
|
|
|
description = "Empty input with empty pattern";
|
|
|
|
input = "";
|
|
|
|
pattern = Pattern.compile("");
|
2015-03-03 11:30:48 +00:00
|
|
|
data.add(new Object[]{description, input, pattern});
|
2013-08-20 21:23:32 +00:00
|
|
|
|
|
|
|
description = "Multiple separators";
|
|
|
|
input = "This is,testing: with\tdifferent separators.";
|
|
|
|
pattern = Pattern.compile("[ \t,:.]");
|
2015-03-03 11:30:48 +00:00
|
|
|
data.add(new Object[]{description, input, pattern});
|
2013-09-21 00:11:32 +00:00
|
|
|
|
|
|
|
description = "Repeated separators within and at end";
|
|
|
|
input = "boo:and:foo";
|
|
|
|
pattern = Pattern.compile("o");
|
2015-03-03 11:30:48 +00:00
|
|
|
data.add(new Object[]{description, input, pattern});
|
2013-09-21 00:11:32 +00:00
|
|
|
|
|
|
|
description = "Many repeated separators within and at end";
|
|
|
|
input = "booooo:and:fooooo";
|
|
|
|
pattern = Pattern.compile("o");
|
2015-03-03 11:30:48 +00:00
|
|
|
data.add(new Object[]{description, input, pattern});
|
2013-09-21 00:11:32 +00:00
|
|
|
|
|
|
|
description = "Many repeated separators before last match";
|
|
|
|
input = "fooooo:";
|
|
|
|
pattern = Pattern.compile("o");
|
2015-03-03 11:30:48 +00:00
|
|
|
data.add(new Object[] {description, input, pattern});
|
|
|
|
|
2013-08-20 21:23:32 +00:00
|
|
|
return data.toArray(new Object[0][]);
|
|
|
|
}
|
|
|
|
|
2015-03-03 11:30:48 +00:00
|
|
|
@Test(dataProvider = "Patterns")
|
|
|
|
public void testPatternSplitAsStream(String description, String input, Pattern pattern) {
|
|
|
|
// Derive expected result from pattern.split
|
|
|
|
List<String> expected = Arrays.asList(pattern.split(input));
|
|
|
|
|
2013-08-20 21:23:32 +00:00
|
|
|
Supplier<Stream<String>> ss = () -> pattern.splitAsStream(input);
|
|
|
|
withData(TestData.Factory.ofSupplier(description, ss))
|
|
|
|
.stream(LambdaTestHelpers.identity())
|
|
|
|
.expectedResult(expected)
|
|
|
|
.exercise();
|
|
|
|
}
|
2015-03-03 11:30:48 +00:00
|
|
|
|
|
|
|
@Test(dataProvider = "Patterns")
|
|
|
|
public void testReplaceFirst(String description, String input, Pattern pattern) {
|
|
|
|
// Derive expected result from Matcher.replaceFirst(String )
|
|
|
|
String expected = pattern.matcher(input).replaceFirst("R");
|
|
|
|
String actual = pattern.matcher(input).replaceFirst(r -> "R");
|
|
|
|
assertEquals(actual, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(dataProvider = "Patterns")
|
|
|
|
public void testReplaceAll(String description, String input, Pattern pattern) {
|
|
|
|
// Derive expected result from Matcher.replaceAll(String )
|
|
|
|
String expected = pattern.matcher(input).replaceAll("R");
|
|
|
|
String actual = pattern.matcher(input).replaceAll(r -> "R");
|
|
|
|
assertEquals(actual, expected);
|
|
|
|
|
|
|
|
// Derive expected result from Matcher.find
|
|
|
|
Matcher m = pattern.matcher(input);
|
|
|
|
int expectedMatches = 0;
|
|
|
|
while (m.find()) {
|
|
|
|
expectedMatches++;
|
|
|
|
}
|
|
|
|
AtomicInteger actualMatches = new AtomicInteger();
|
|
|
|
pattern.matcher(input).replaceAll(r -> "R" + actualMatches.incrementAndGet());
|
|
|
|
assertEquals(expectedMatches, actualMatches.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(dataProvider = "Patterns")
|
|
|
|
public void testMatchResults(String description, String input, Pattern pattern) {
|
|
|
|
// Derive expected result from Matcher.find
|
|
|
|
Matcher m = pattern.matcher(input);
|
|
|
|
List<MatchResultHolder> expected = new ArrayList<>();
|
|
|
|
while (m.find()) {
|
|
|
|
expected.add(new MatchResultHolder(m));
|
|
|
|
}
|
|
|
|
|
|
|
|
Supplier<Stream<MatchResult>> ss = () -> pattern.matcher(input).results();
|
|
|
|
withData(TestData.Factory.ofSupplier(description, ss))
|
|
|
|
.stream(s -> s.map(MatchResultHolder::new))
|
|
|
|
.expectedResult(expected)
|
|
|
|
.exercise();
|
|
|
|
}
|
|
|
|
|
2015-12-13 14:10:13 +00:00
|
|
|
@Test
|
|
|
|
public void testLateBinding() {
|
|
|
|
Pattern pattern = Pattern.compile(",");
|
|
|
|
|
|
|
|
StringBuilder sb = new StringBuilder("a,b,c,d,e");
|
|
|
|
Stream<String> stream = pattern.splitAsStream(sb);
|
|
|
|
sb.setLength(3);
|
|
|
|
assertEquals(Arrays.asList("a", "b"), stream.collect(Collectors.toList()));
|
|
|
|
|
|
|
|
stream = pattern.splitAsStream(sb);
|
|
|
|
sb.append(",f,g");
|
|
|
|
assertEquals(Arrays.asList("a", "b", "f", "g"), stream.collect(Collectors.toList()));
|
|
|
|
}
|
|
|
|
|
2015-03-03 11:30:48 +00:00
|
|
|
public void testFailfastMatchResults() {
|
|
|
|
Pattern p = Pattern.compile("X");
|
|
|
|
Matcher m = p.matcher("XX");
|
|
|
|
|
|
|
|
Stream<MatchResult> s = m.results();
|
|
|
|
m.find();
|
|
|
|
// Should start on the second match
|
|
|
|
assertEquals(s.count(), 1);
|
|
|
|
|
|
|
|
// Fail fast without short-circuit
|
|
|
|
// Exercises Iterator.forEachRemaining
|
|
|
|
m.reset();
|
|
|
|
try {
|
|
|
|
m.results().peek(mr -> m.reset()).count();
|
|
|
|
fail();
|
|
|
|
} catch (ConcurrentModificationException e) {
|
|
|
|
// Should reach here
|
|
|
|
}
|
|
|
|
|
|
|
|
m.reset();
|
|
|
|
try {
|
|
|
|
m.results().peek(mr -> m.find()).count();
|
|
|
|
fail();
|
|
|
|
} catch (ConcurrentModificationException e) {
|
|
|
|
// Should reach here
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fail fast with short-circuit
|
|
|
|
// Exercises Iterator.hasNext/next
|
|
|
|
m.reset();
|
|
|
|
try {
|
|
|
|
m.results().peek(mr -> m.reset()).limit(2).count();
|
|
|
|
fail();
|
|
|
|
} catch (ConcurrentModificationException e) {
|
|
|
|
// Should reach here
|
|
|
|
}
|
|
|
|
|
|
|
|
m.reset();
|
|
|
|
try {
|
|
|
|
m.results().peek(mr -> m.find()).limit(2).count();
|
|
|
|
fail();
|
|
|
|
} catch (ConcurrentModificationException e) {
|
|
|
|
// Should reach here
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testFailfastReplace() {
|
|
|
|
Pattern p = Pattern.compile("X");
|
|
|
|
Matcher m = p.matcher("XX");
|
|
|
|
|
|
|
|
// Fail fast without short-circuit
|
|
|
|
// Exercises Iterator.forEachRemaining
|
|
|
|
m.reset();
|
|
|
|
try {
|
|
|
|
m.replaceFirst(mr -> { m.reset(); return "Y"; });
|
|
|
|
fail();
|
|
|
|
} catch (ConcurrentModificationException e) {
|
|
|
|
// Should reach here
|
|
|
|
}
|
|
|
|
|
|
|
|
m.reset();
|
|
|
|
try {
|
|
|
|
m.replaceAll(mr -> { m.reset(); return "Y"; });
|
|
|
|
fail();
|
|
|
|
} catch (ConcurrentModificationException e) {
|
|
|
|
// Should reach here
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A holder of MatchResult that can compare
|
|
|
|
static class MatchResultHolder implements Comparable<MatchResultHolder> {
|
|
|
|
final MatchResult mr;
|
|
|
|
|
|
|
|
MatchResultHolder(Matcher m) {
|
|
|
|
this(m.toMatchResult());
|
|
|
|
}
|
|
|
|
|
|
|
|
MatchResultHolder(MatchResult mr) {
|
|
|
|
this.mr = mr;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int compareTo(MatchResultHolder that) {
|
|
|
|
int c = that.mr.group().compareTo(this.mr.group());
|
|
|
|
if (c != 0)
|
|
|
|
return c;
|
|
|
|
|
|
|
|
c = Integer.compare(that.mr.start(), this.mr.start());
|
|
|
|
if (c != 0)
|
|
|
|
return c;
|
|
|
|
|
|
|
|
c = Integer.compare(that.mr.end(), this.mr.end());
|
|
|
|
if (c != 0)
|
|
|
|
return c;
|
|
|
|
|
|
|
|
c = Integer.compare(that.mr.groupCount(), this.mr.groupCount());
|
|
|
|
if (c != 0)
|
|
|
|
return c;
|
|
|
|
|
|
|
|
for (int g = 0; g < this.mr.groupCount(); g++) {
|
|
|
|
c = that.mr.group(g).compareTo(this.mr.group(g));
|
|
|
|
if (c != 0)
|
|
|
|
return c;
|
|
|
|
|
|
|
|
c = Integer.compare(that.mr.start(g), this.mr.start(g));
|
|
|
|
if (c != 0)
|
|
|
|
return c;
|
|
|
|
|
|
|
|
c = Integer.compare(that.mr.end(g), this.mr.end(g));
|
|
|
|
if (c != 0)
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object that) {
|
|
|
|
if (this == that) return true;
|
|
|
|
if (that == null || getClass() != that.getClass()) return false;
|
|
|
|
|
|
|
|
return this.compareTo((MatchResultHolder) that) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
return mr.group().hashCode();
|
|
|
|
}
|
|
|
|
}
|
2013-08-20 21:23:32 +00:00
|
|
|
}
|