# HG changeset patch # User sherman # Date 1426611276 25200 # Node ID 95318f222149eaf09fad1130cd904ae1a4ed0c62 # Parent b017930c7b4228ce28c292fbf00406a8c8bdae69 8074678: JCK test java_util/regex/MatchResult/index.html starts failing after JDK-8071479 Summary: to add non-match sanity check Reviewed-by: psandoz diff -r b017930c7b42 -r 95318f222149 jdk/src/java.base/share/classes/java/util/regex/Matcher.java --- a/jdk/src/java.base/share/classes/java/util/regex/Matcher.java Tue Mar 17 16:01:27 2015 +0100 +++ b/jdk/src/java.base/share/classes/java/util/regex/Matcher.java Tue Mar 17 09:54:36 2015 -0700 @@ -292,11 +292,13 @@ @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 @@ @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 @@ @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 -r b017930c7b42 -r 95318f222149 jdk/test/java/util/regex/RegExTest.java --- a/jdk/test/java/util/regex/RegExTest.java Tue Mar 17 16:01:27 2015 +0100 +++ b/jdk/test/java/util/regex/RegExTest.java Tue Mar 17 09:54:36 2015 -0700 @@ -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 @@ wordSearchTest(); hitEndTest(); toMatchResultTest(); + toMatchResultTest2(); surrogatesInClassTest(); removeQEQuotingTest(); namedGroupCaptureTest(); @@ -371,6 +372,47 @@ 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 {