changeset 29503:95318f222149

8074678: JCK test java_util/regex/MatchResult/index.html starts failing after JDK-8071479 Summary: to add non-match sanity check Reviewed-by: psandoz
author sherman
date Tue, 17 Mar 2015 09:54:36 -0700
parents b017930c7b42
children 7deb8479c388 b84395d0bc4c
files jdk/src/java.base/share/classes/java/util/regex/Matcher.java jdk/test/java/util/regex/RegExTest.java
diffstat 2 files changed, 55 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- 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");
+
+        }
     }
 
     /**
--- 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 {