summaryrefslogtreecommitdiff
path: root/harmony-tests
diff options
context:
space:
mode:
authorTobias Thierer <tobiast@google.com>2018-05-29 18:54:18 +0100
committerTobias Thierer <tobiast@google.com>2018-05-30 14:44:08 +0100
commitb1e0e39dcb5af8cab8878763af564d2acbc91f50 (patch)
tree4c19ed77f135cc0814d1aa920ef90f9b002680b8 /harmony-tests
parentf91e053f9c13a074e3156ac9495515e4e616dcb0 (diff)
Matcher: Adopt behavior changes forgotten in update to OpenJDK 8.
This is step 5 of verifying Matcher.java vs. OpenJDK 8u121-b13. Matcher.java was never properly updated from OpenJDK 7u40 to OpenJDK 8 (u60, u121-b13). This CL adopts the following behavior changes that were thus forgotten, and adds unit tests enforcing them: replaceFirst() - Adopt the following behavior even when there is no match: - still throw NPE - don't reset() - Previously, the NPE was not thrown and the Matcher was reset(). start(int group), end(int group): - throw IOOBE rather than ArrayIndexOutOfBoundsException if group < 0 or group > groupCount appendReplacement(StringBuffer, String): - if replacement ends in \ or $ then the method now throws IAE; the old code only checked for training \ and threw an IOOBE instead of IAE. This behavior change required updating an old regression test from Harmony to expect the new behavior. - Note: The implementation of appendReplacement() appears to have previously been done independently from the upstream code. Its implementation could be changed to be based on / much more similar to upstream code, but this is not done as part of this CL. Test: Before but not after this CL, the exceptions encountered by the out-of-bounds tests added to OldMatcherTest were specifically instances of ArrayIndexOutOfBoundsExceptions. The tests as written don't care about the kind of IOOBE they encounter, so they pass both before and after this CL. Test: testReplaceFirst_null_match passes both before and after this CL. Test: All other added tests fail before this CL but pass after this CL. Test: CtsLibcoreTestCases Bug: 80416774 Change-Id: Ibdee4976f5033c7913fa98a65475f2c7a310ebc9
Diffstat (limited to 'harmony-tests')
-rw-r--r--harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/Matcher2Test.java28
-rw-r--r--harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/MatcherTest.java50
2 files changed, 73 insertions, 5 deletions
diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/Matcher2Test.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/Matcher2Test.java
index e84b356aa9..9d4ee70d83 100644
--- a/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/Matcher2Test.java
+++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/Matcher2Test.java
@@ -216,9 +216,6 @@ public class Matcher2Test extends TestCase {
}
}
- /*
- * Regression test for HARMONY-997
- */
public void testReplacementBackSlash() {
String str = "replace me";
String replacedString = "me";
@@ -227,8 +224,29 @@ public class Matcher2Test extends TestCase {
Matcher mat = pat.matcher(str);
try {
mat.replaceAll(substitutionString);
- fail("IndexOutOfBoundsException should be thrown");
- } catch (IndexOutOfBoundsException e) {
+ fail("IllegalArgumentException should be thrown");
+ } catch (IllegalArgumentException e) {
+ }
+ }
+
+ public void testAppendReplacement_replacementEndsWithBackslash() {
+ Matcher matcher = Pattern.compile("Hello").matcher("Hello, world!");
+ matcher.find();
+ try {
+ matcher.appendReplacement(new StringBuffer(), "replacement\\");
+ fail();
+ } catch (IllegalArgumentException expected) {
}
}
+
+ public void testAppendReplacement_replacementEndsWithDollar() {
+ Matcher matcher = Pattern.compile("Hello").matcher("Hello, world!");
+ matcher.find();
+ try {
+ matcher.appendReplacement(new StringBuffer(), "replacement$");
+ fail();
+ } catch (IllegalArgumentException expected) {
+ }
+ }
+
}
diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/MatcherTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/MatcherTest.java
index 98508e1c26..276617b2f7 100644
--- a/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/MatcherTest.java
+++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/MatcherTest.java
@@ -143,6 +143,24 @@ public class MatcherTest extends TestCase {
assertEquals("zzzcatzzzdogzzz", mat.replaceFirst("cat"));
}
+ public void testReplaceFirst_null_match() {
+ Matcher matcher = Pattern.compile("Hello").matcher("Hello, world!");
+ try {
+ matcher.replaceFirst(null);
+ fail();
+ } catch (NullPointerException expected) {
+ }
+ }
+
+ public void testReplaceFirst_null_nomatch() {
+ Matcher matcher = Pattern.compile("not found").matcher("Hello, world!");
+ try {
+ matcher.replaceFirst(null);
+ fail();
+ } catch (NullPointerException expected) {
+ }
+ }
+
public void testPattern() {
for (String element : testPatterns) {
Pattern test = Pattern.compile(element);
@@ -420,6 +438,38 @@ public class MatcherTest extends TestCase {
}
}
+ public void testEnd_groupIndexOutOfBounds() {
+ Matcher matcher = Pattern.compile("(Hello)").matcher("Hello, world!");
+ assertTrue(matcher.find());
+ try {
+ matcher.end(-1 /* out of bounds */);
+ } catch (IndexOutOfBoundsException expected) {
+ assertFalse(expected instanceof ArrayIndexOutOfBoundsException);
+ }
+
+ try {
+ matcher.end(2 /* out of bounds */);
+ } catch (IndexOutOfBoundsException expected) {
+ assertFalse(expected instanceof ArrayIndexOutOfBoundsException);
+ }
+ }
+
+ public void testStart_groupIndexOutOfBounds() {
+ Matcher matcher = Pattern.compile("(Hello)").matcher("Hello, world!");
+ assertTrue(matcher.find());
+ try {
+ matcher.start(-1 /* out of bounds */);
+ } catch (IndexOutOfBoundsException expected) {
+ assertFalse(expected instanceof ArrayIndexOutOfBoundsException);
+ }
+
+ try {
+ matcher.start(2 /* out of bounds */);
+ } catch (IndexOutOfBoundsException expected) {
+ assertFalse(expected instanceof ArrayIndexOutOfBoundsException);
+ }
+ }
+
public void testEnhancedFind() {
String input = "foob";
String pattern = "a*b";