From f3a2e4480d19da3ad2371cb7a8b1aba8c61f264a Mon Sep 17 00:00:00 2001 From: Xueming Shen Date: Tue, 17 Mar 2015 09:54:36 -0700 Subject: [PATCH] 8074678: JCK test java_util/regex/MatchResult/index.html starts failing after JDK-8071479 To add non-match sanity check Reviewed-by: psandoz --- .../classes/java/util/regex/Matcher.java | 12 +++++ jdk/test/java/util/regex/RegExTest.java | 44 ++++++++++++++++++- 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/jdk/src/java.base/share/classes/java/util/regex/Matcher.java b/jdk/src/java.base/share/classes/java/util/regex/Matcher.java index 96cf39a6338..ded7215005d 100644 --- a/jdk/src/java.base/share/classes/java/util/regex/Matcher.java +++ b/jdk/src/java.base/share/classes/java/util/regex/Matcher.java @@ -292,11 +292,13 @@ public final class Matcher implements MatchResult { @Override public int start() { + checkMatch(); return first; } @Override public int start(int group) { + checkMatch(); if (group < 0 || group > groupCount) throw new IndexOutOfBoundsException("No group " + group); return groups[group * 2]; @@ -304,11 +306,13 @@ public final class Matcher implements MatchResult { @Override public int end() { + checkMatch(); return last; } @Override public int end(int group) { + checkMatch(); if (group < 0 || group > groupCount) throw new IndexOutOfBoundsException("No group " + group); return groups[group * 2 + 1]; @@ -321,17 +325,25 @@ public final class Matcher implements MatchResult { @Override public String group() { + checkMatch(); return group(0); } @Override public String group(int group) { + checkMatch(); if (group < 0 || group > groupCount) throw new IndexOutOfBoundsException("No group " + group); if ((groups[group*2] == -1) || (groups[group*2+1] == -1)) return null; return text.subSequence(groups[group * 2], groups[group * 2 + 1]).toString(); } + + private void checkMatch() { + if (first < 0) + throw new IllegalStateException("No match found"); + + } } /** diff --git a/jdk/test/java/util/regex/RegExTest.java b/jdk/test/java/util/regex/RegExTest.java index d5f8123ef5a..f04f6f1e8c7 100644 --- a/jdk/test/java/util/regex/RegExTest.java +++ b/jdk/test/java/util/regex/RegExTest.java @@ -32,7 +32,7 @@ * 6358731 6178785 6284152 6231989 6497148 6486934 6233084 6504326 6635133 * 6350801 6676425 6878475 6919132 6931676 6948903 6990617 7014645 7039066 * 7067045 7014640 7189363 8007395 8013252 8013254 8012646 8023647 6559590 - * 8027645 8035076 8039124 8035975 + * 8027645 8035076 8039124 8035975 8074678 */ import java.util.function.Function; @@ -138,6 +138,7 @@ public class RegExTest { wordSearchTest(); hitEndTest(); toMatchResultTest(); + toMatchResultTest2(); surrogatesInClassTest(); removeQEQuotingTest(); namedGroupCaptureTest(); @@ -371,6 +372,47 @@ public class RegExTest { report("toMatchResult is a copy"); } + private static void checkExpectedISE(Runnable test) { + try { + test.run(); + failCount++; + } catch (IllegalStateException x) { + } catch (IndexOutOfBoundsException xx) { + failCount++; + } + } + + private static void checkExpectedIOOE(Runnable test) { + try { + test.run(); + failCount++; + } catch (IndexOutOfBoundsException x) {} + } + + // This is for bug 8074678 + // Test the result of toMatchResult throws ISE if no match is availble + private static void toMatchResultTest2() throws Exception { + Matcher matcher = Pattern.compile("nomatch").matcher("hello world"); + matcher.find(); + MatchResult mr = matcher.toMatchResult(); + + checkExpectedISE(() -> mr.start()); + checkExpectedISE(() -> mr.start(2)); + checkExpectedISE(() -> mr.end()); + checkExpectedISE(() -> mr.end(2)); + checkExpectedISE(() -> mr.group()); + checkExpectedISE(() -> mr.group(2)); + + matcher = Pattern.compile("(match)").matcher("there is a match"); + matcher.find(); + MatchResult mr2 = matcher.toMatchResult(); + checkExpectedIOOE(() -> mr2.start(2)); + checkExpectedIOOE(() -> mr2.end(2)); + checkExpectedIOOE(() -> mr2.group(2)); + + report("toMatchResult2 appropriate exceptions"); + } + // This is for bug 5013885 // Must test a slice to see if it reports hitEnd correctly private static void hitEndTest() throws Exception {