summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2013-05-03 22:39:51 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2013-05-03 22:39:51 +0000
commita4f2eac75586df51bb508a78d00cb353fa019a45 (patch)
treefa85df25a2c8034812cbc0f4259c00654d3a25f5
parent645c15629a8abdb9f3a6e428ccb393eacd933552 (diff)
parentbfd68b1dd4409f61fbc6800ba61f4605ad57945b (diff)
downloadlibcore-a4f2eac75586df51bb508a78d00cb353fa019a45.zip
libcore-a4f2eac75586df51bb508a78d00cb353fa019a45.tar.gz
libcore-a4f2eac75586df51bb508a78d00cb353fa019a45.tar.bz2
Merge "Add the harmony regex tests."
-rw-r--r--harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/Matcher2Test.java234
-rw-r--r--harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/MatcherTest.java768
-rw-r--r--harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/ModeTest.java111
-rw-r--r--harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/Pattern2Test.java1409
-rw-r--r--harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/PatternErrorTest.java65
-rw-r--r--harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/PatternSyntaxExceptionTest.java113
-rw-r--r--harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/PatternTest.java1624
-rw-r--r--harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/ReplaceTest.java90
-rw-r--r--harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/SplitTest.java171
9 files changed, 4585 insertions, 0 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
new file mode 100644
index 0000000..e84b356
--- /dev/null
+++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/Matcher2Test.java
@@ -0,0 +1,234 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.harmony.tests.java.util.regex;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.regex.PatternSyntaxException;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests Matcher methods
+ */
+@SuppressWarnings("nls")
+public class Matcher2Test extends TestCase {
+ public void test_toString() {
+ Pattern p = Pattern.compile("foo");
+ Matcher m = p.matcher("bar");
+ assertNotNull(m.toString());
+ }
+
+ public void testErrorConditions() throws PatternSyntaxException {
+ // Test match cursors in absence of a match
+ Pattern p = Pattern.compile("foo");
+ Matcher m = p.matcher("bar");
+ assertFalse(m.matches());
+
+ try {
+ m.start();
+ fail("IllegalStateException expected");
+ } catch (IllegalStateException e) {
+ }
+
+ try {
+ m.end();
+ fail("IllegalStateException expected");
+ } catch (IllegalStateException e) {
+ }
+
+ try {
+ m.group();
+ fail("IllegalStateException expected");
+ } catch (IllegalStateException e) {
+ }
+
+ try {
+ m.start(1);
+ fail("IllegalStateException expected");
+ } catch (IllegalStateException e) {
+ }
+
+ try {
+ m.end(1);
+ fail("IllegalStateException expected");
+ } catch (IllegalStateException e) {
+ }
+
+ try {
+ m.group(1);
+ fail("IllegalStateException expected");
+ } catch (IllegalStateException e) {
+ }
+
+ // regression test for HARMONY-2418
+ try {
+ m.usePattern(null);
+ fail("IllegalArgumentException expected");
+ } catch (IllegalArgumentException e) {
+ // PASSED
+ }
+ }
+
+ public void testErrorConditions2() throws PatternSyntaxException {
+ // Test match cursors in absence of a match
+ Pattern p = Pattern.compile("(foo[0-9])(bar[a-z])");
+ Matcher m = p.matcher("foo1barzfoo2baryfoozbar5");
+
+ assertTrue(m.find());
+ assertEquals(0, m.start());
+ assertEquals(8, m.end());
+ assertEquals(0, m.start(1));
+ assertEquals(4, m.end(1));
+ assertEquals(4, m.start(2));
+ assertEquals(8, m.end(2));
+
+ try {
+ m.start(3);
+ fail("IndexOutOfBoundsException expected");
+ } catch (IndexOutOfBoundsException e) {
+ }
+
+ try {
+ m.end(3);
+ fail("IndexOutOfBoundsException expected");
+ } catch (IndexOutOfBoundsException e) {
+ }
+
+ try {
+ m.group(3);
+ fail("IndexOutOfBoundsException expected");
+ } catch (IndexOutOfBoundsException e) {
+ }
+
+ try {
+ m.start(-1);
+ fail("IndexOutOfBoundsException expected");
+ } catch (IndexOutOfBoundsException e) {
+ }
+
+ try {
+ m.end(-1);
+ fail("IndexOutOfBoundsException expected");
+ } catch (IndexOutOfBoundsException e) {
+ }
+
+ try {
+ m.group(-1);
+ fail("IndexOutOfBoundsException expected");
+ } catch (IndexOutOfBoundsException e) {
+ }
+
+ assertTrue(m.find());
+ assertEquals(8, m.start());
+ assertEquals(16, m.end());
+ assertEquals(8, m.start(1));
+ assertEquals(12, m.end(1));
+ assertEquals(12, m.start(2));
+ assertEquals(16, m.end(2));
+
+ try {
+ m.start(3);
+ fail("IndexOutOfBoundsException expected");
+ } catch (IndexOutOfBoundsException e) {
+ }
+
+ try {
+ m.end(3);
+ fail("IndexOutOfBoundsException expected");
+ } catch (IndexOutOfBoundsException e) {
+ }
+
+ try {
+ m.group(3);
+ fail("IndexOutOfBoundsException expected");
+ } catch (IndexOutOfBoundsException e) {
+ }
+
+ try {
+ m.start(-1);
+ fail("IndexOutOfBoundsException expected");
+ } catch (IndexOutOfBoundsException e) {
+ }
+
+ try {
+ m.end(-1);
+ fail("IndexOutOfBoundsException expected");
+ } catch (IndexOutOfBoundsException e) {
+ }
+
+ try {
+ m.group(-1);
+ fail("IndexOutOfBoundsException expected");
+ } catch (IndexOutOfBoundsException e) {
+ }
+
+ assertFalse(m.find());
+
+ try {
+ m.start(3);
+ fail("IllegalStateException expected");
+ } catch (IllegalStateException e) {
+ }
+
+ try {
+ m.end(3);
+ fail("IllegalStateException expected");
+ } catch (IllegalStateException e) {
+ }
+
+ try {
+ m.group(3);
+ fail("IllegalStateException expected");
+ } catch (IllegalStateException e) {
+ }
+
+ try {
+ m.start(-1);
+ fail("IllegalStateException expected");
+ } catch (IllegalStateException e) {
+ }
+
+ try {
+ m.end(-1);
+ fail("IllegalStateException expected");
+ } catch (IllegalStateException e) {
+ }
+
+ try {
+ m.group(-1);
+ fail("IllegalStateException expected");
+ } catch (IllegalStateException e) {
+ }
+ }
+
+ /*
+ * Regression test for HARMONY-997
+ */
+ public void testReplacementBackSlash() {
+ String str = "replace me";
+ String replacedString = "me";
+ String substitutionString = "\\";
+ Pattern pat = Pattern.compile(replacedString);
+ Matcher mat = pat.matcher(str);
+ try {
+ mat.replaceAll(substitutionString);
+ fail("IndexOutOfBoundsException should be thrown");
+ } catch (IndexOutOfBoundsException e) {
+ }
+ }
+}
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
new file mode 100644
index 0000000..bc87439
--- /dev/null
+++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/MatcherTest.java
@@ -0,0 +1,768 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.harmony.tests.java.util.regex;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import junit.framework.TestCase;
+
+@SuppressWarnings("nls")
+public class MatcherTest extends TestCase {
+ String[] testPatterns = {
+ "(a|b)*abb",
+ "(1*2*3*4*)*567",
+ "(a|b|c|d)*aab",
+ "(1|2|3|4|5|6|7|8|9|0)(1|2|3|4|5|6|7|8|9|0)*",
+ "(abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ)*",
+ "(a|b)*(a|b)*A(a|b)*lice.*",
+ "(a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z)(a|b|c|d|e|f|g|h|"
+ + "i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z)*(1|2|3|4|5|6|7|8|9|0)*|while|for|struct|if|do" };
+
+ String[] groupPatterns = { "(a|b)*aabb", "((a)|b)*aabb", "((a|b)*)a(abb)",
+ "(((a)|(b))*)aabb", "(((a)|(b))*)aa(b)b", "(((a)|(b))*)a(a(b)b)" };
+
+ public MatcherTest(String name) {
+ super(name);
+ }
+
+ public void testRegionsIntInt() {
+ Pattern p = Pattern.compile("x*");
+ Matcher m = p.matcher("axxxxxa");
+ assertFalse(m.matches());
+
+ m.region(1, 6);
+ assertEquals(1, m.regionStart());
+ assertEquals(6, m.regionEnd());
+ assertTrue(m.matches());
+
+ try {
+ m.region(1, 0);
+ fail("expected an IOOBE");
+ } catch(IndexOutOfBoundsException e) {
+ }
+
+ try {
+ m.region(-1, 2);
+ fail("expected an IOOBE");
+ } catch(IndexOutOfBoundsException e) {
+ }
+
+ try {
+ m.region(10, 11);
+ fail("expected an IOOBE");
+ } catch(IndexOutOfBoundsException e) {
+ }
+
+ try {
+ m.region(1, 10);
+ fail("expected an IOOBE");
+ } catch(IndexOutOfBoundsException e) {
+ }
+ }
+
+ public void testAppendReplacement() {
+ Pattern pat = Pattern.compile("XX");
+ Matcher m = pat.matcher("Today is XX-XX-XX ...");
+ StringBuffer sb = new StringBuffer();
+
+ for (int i = 0; m.find(); i++) {
+ m.appendReplacement(sb, new Integer(i * 10 + i).toString());
+ }
+ m.appendTail(sb);
+ assertEquals("Today is 0-11-22 ...", sb.toString());
+ }
+
+ public void testAppendReplacementRef() {
+ Pattern p = Pattern.compile("xx (rur|\\$)");
+ Matcher m = p.matcher("xx $ equals to xx rur.");
+ StringBuffer sb = new StringBuffer();
+ for (int i = 1; m.find(); i *= 30) {
+ String rep = new Integer(i).toString() + " $1";
+ m.appendReplacement(sb, rep);
+ }
+ m.appendTail(sb);
+ assertEquals("1 $ equals to 30 rur.", sb.toString());
+ }
+
+ public void testReplaceAll() {
+ String input = "aabfooaabfooabfoob";
+ String pattern = "a*b";
+ Pattern pat = Pattern.compile(pattern);
+ Matcher mat = pat.matcher(input);
+
+ assertEquals("-foo-foo-foo-", mat.replaceAll("-"));
+ }
+
+ /*
+ * Class under test for Matcher reset(CharSequence)
+ */
+ public void testResetCharSequence() {
+ Pattern p = Pattern.compile("abcd");
+ Matcher m = p.matcher("abcd");
+ assertTrue(m.matches());
+ m.reset("efgh");
+ assertFalse(m.matches());
+
+ try {
+ m.reset(null);
+ fail("expected a NPE");
+ } catch (NullPointerException e) {
+ }
+ }
+
+ public void testAppendSlashes() {
+ Pattern p = Pattern.compile("\\\\");
+ Matcher m = p.matcher("one\\cat\\two\\cats\\in\\the\\yard");
+ StringBuffer sb = new StringBuffer();
+ while (m.find()) {
+ m.appendReplacement(sb, "\\\\");
+ }
+ m.appendTail(sb);
+ assertEquals("one\\cat\\two\\cats\\in\\the\\yard", sb.toString());
+
+ }
+
+ public void testReplaceFirst() {
+ String input = "zzzdogzzzdogzzz";
+ String pattern = "dog";
+ Pattern pat = Pattern.compile(pattern);
+ Matcher mat = pat.matcher(input);
+
+ assertEquals("zzzcatzzzdogzzz", mat.replaceFirst("cat"));
+ }
+
+ public void testPattern() {
+ for (String element : testPatterns) {
+ Pattern test = Pattern.compile(element);
+ assertEquals(test, test.matcher("aaa").pattern());
+ }
+
+ for (String element : testPatterns) {
+ assertEquals(element, Pattern.compile(element).matcher("aaa")
+ .pattern().toString());
+ }
+ }
+
+ /*
+ * Class under test for Matcher reset()
+ */
+ public void testReset() {
+ }
+
+ /*
+ * Class under test for String group(int)
+ */
+ public void testGroupint() {
+ String positiveTestString = "ababababbaaabb";
+
+ // test IndexOutOfBoundsException
+ // //
+ for (int i = 0; i < groupPatterns.length; i++) {
+ Pattern test = Pattern.compile(groupPatterns[i]);
+ Matcher mat = test.matcher(positiveTestString);
+ mat.matches();
+ try {
+ // groupPattern <index + 1> equals to number of groups
+ // of the specified pattern
+ // //
+ mat.group(i + 2);
+ fail("IndexOutBoundsException expected");
+ mat.group(i + 100);
+ fail("IndexOutBoundsException expected");
+ mat.group(-1);
+ fail("IndexOutBoundsException expected");
+ mat.group(-100);
+ fail("IndexOutBoundsException expected");
+ } catch (IndexOutOfBoundsException iobe) {
+ }
+ }
+
+ String[][] groupResults = { { "a" }, { "a", "a" },
+ { "ababababba", "a", "abb" }, { "ababababba", "a", "a", "b" },
+ { "ababababba", "a", "a", "b", "b" },
+ { "ababababba", "a", "a", "b", "abb", "b" }, };
+
+ for (int i = 0; i < groupPatterns.length; i++) {
+ Pattern test = Pattern.compile(groupPatterns[i]);
+ Matcher mat = test.matcher(positiveTestString);
+ mat.matches();
+ for (int j = 0; j < groupResults[i].length; j++) {
+ assertEquals("i: " + i + " j: " + j, groupResults[i][j], mat
+ .group(j + 1));
+ }
+
+ }
+
+ }
+
+ public void testGroup() {
+ String positiveTestString = "ababababbaaabb";
+ String negativeTestString = "gjhfgdsjfhgcbv";
+ for (String element : groupPatterns) {
+ Pattern test = Pattern.compile(element);
+ Matcher mat = test.matcher(positiveTestString);
+ mat.matches();
+ // test result
+ assertEquals(positiveTestString, mat.group());
+
+ // test equal to group(0) result
+ assertEquals(mat.group(0), mat.group());
+ }
+
+ for (String element : groupPatterns) {
+ Pattern test = Pattern.compile(element);
+ Matcher mat = test.matcher(negativeTestString);
+ mat.matches();
+ try {
+ mat.group();
+ fail("IllegalStateException expected for <false> matches result");
+ } catch (IllegalStateException ise) {
+ }
+ }
+ }
+
+ public void testGroupPossessive() {
+ Pattern pat = Pattern.compile("((a)|(b))++c");
+ Matcher mat = pat.matcher("aac");
+
+ mat.matches();
+ assertEquals("a", mat.group(1));
+ }
+
+ /*
+ * Class under test for boolean find(int)
+ */
+ public void testFindint() {
+ }
+
+ /*
+ * Class under test for int start(int)
+ */
+ public void testStartint() {
+ }
+
+ /*
+ * Class under test for int end(int)
+ */
+ public void testEndint() {
+ }
+
+ public void testMatchesMisc() {
+ String[][] posSeq = {
+ { "abb", "ababb", "abababbababb", "abababbababbabababbbbbabb" },
+ { "213567", "12324567", "1234567", "213213567",
+ "21312312312567", "444444567" },
+ { "abcdaab", "aab", "abaab", "cdaab", "acbdadcbaab" },
+ { "213234567", "3458", "0987654", "7689546432", "0398576",
+ "98432", "5" },
+ {
+ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
+ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" },
+ { "ababbaAabababblice", "ababbaAliceababab", "ababbAabliceaaa",
+ "abbbAbbbliceaaa", "Alice" },
+ { "a123", "bnxnvgds156", "for", "while", "if", "struct" }
+
+ };
+
+ for (int i = 0; i < testPatterns.length; i++) {
+ Pattern pat = Pattern.compile(testPatterns[i]);
+ for (int j = 0; j < posSeq[i].length; j++) {
+ Matcher mat = pat.matcher(posSeq[i][j]);
+ assertTrue("Incorrect match: " + testPatterns[i] + " vs "
+ + posSeq[i][j], mat.matches());
+ }
+ }
+ }
+
+ public void testMatchesQuantifiers() {
+ String[] testPatternsSingles = { "a{5}", "a{2,4}", "a{3,}" };
+ String[] testPatternsMultiple = { "((a)|(b)){1,2}abb",
+ "((a)|(b)){2,4}", "((a)|(b)){3,}" };
+
+ String[][] stringSingles = { { "aaaaa", "aaa" },
+ { "aa", "a", "aaa", "aaaaaa", "aaaa", "aaaaa" },
+ { "aaa", "a", "aaaa", "aa" }, };
+
+ String[][] stringMultiples = { { "ababb", "aba" },
+ { "ab", "b", "bab", "ababa", "abba", "abababbb" },
+ { "aba", "b", "abaa", "ba" }, };
+
+ for (int i = 0; i < testPatternsSingles.length; i++) {
+ Pattern pat = Pattern.compile(testPatternsSingles[i]);
+ for (int j = 0; j < stringSingles.length / 2; j++) {
+ assertTrue("Match expected, but failed: " + pat.pattern()
+ + " : " + stringSingles[i][j], pat.matcher(
+ stringSingles[i][j * 2]).matches());
+ assertFalse("Match failure expected, but match succeed: "
+ + pat.pattern() + " : " + stringSingles[i][j * 2 + 1],
+ pat.matcher(stringSingles[i][j * 2 + 1]).matches());
+ }
+ }
+
+ for (int i = 0; i < testPatternsMultiple.length; i++) {
+ Pattern pat = Pattern.compile(testPatternsMultiple[i]);
+ for (int j = 0; j < stringMultiples.length / 2; j++) {
+ assertTrue("Match expected, but failed: " + pat.pattern()
+ + " : " + stringMultiples[i][j], pat.matcher(
+ stringMultiples[i][j * 2]).matches());
+ assertFalse(
+ "Match failure expected, but match succeed: "
+ + pat.pattern() + " : "
+ + stringMultiples[i][j * 2 + 1], pat.matcher(
+ stringMultiples[i][j * 2 + 1]).matches());
+ }
+ }
+ }
+
+ public void testQuantVsGroup() {
+ String patternString = "(d{1,3})((a|c)*)(d{1,3})((a|c)*)(d{1,3})";
+ String testString = "dacaacaacaaddaaacaacaaddd";
+
+ Pattern pat = Pattern.compile(patternString);
+ Matcher mat = pat.matcher(testString);
+
+ mat.matches();
+ assertEquals("dacaacaacaaddaaacaacaaddd", mat.group());
+ assertEquals("d", mat.group(1));
+ assertEquals("acaacaacaa", mat.group(2));
+ assertEquals("dd", mat.group(4));
+ assertEquals("aaacaacaa", mat.group(5));
+ assertEquals("ddd", mat.group(7));
+ }
+
+ public void testLookingAt() {
+ }
+
+ /*
+ * Class under test for boolean find()
+ */
+ public void testFind() {
+ String testPattern = "(abb)";
+ String testString = "cccabbabbabbabbabb";
+ Pattern pat = Pattern.compile(testPattern);
+ Matcher mat = pat.matcher(testString);
+ int start = 3;
+ int end = 6;
+ while (mat.find()) {
+ assertEquals(start, mat.start(1));
+ assertEquals(end, mat.end(1));
+
+ start = end;
+ end += 3;
+ }
+
+ testPattern = "(\\d{1,3})";
+ testString = "aaaa123456789045";
+
+ Pattern pat2 = Pattern.compile(testPattern);
+ Matcher mat2 = pat2.matcher(testString);
+ start = 4;
+ int length = 3;
+ while (mat2.find()) {
+ assertEquals(testString.substring(start, start + length), mat2
+ .group(1));
+ start += length;
+ }
+ }
+
+ public void testSEOLsymbols() {
+ Pattern pat = Pattern.compile("^a\\(bb\\[$");
+ Matcher mat = pat.matcher("a(bb[");
+
+ assertTrue(mat.matches());
+ }
+
+ /*
+ * Class under test for int start()
+ */
+ public void testStart() {
+ }
+
+ public void testGroupCount() {
+ for (int i = 0; i < groupPatterns.length; i++) {
+ Pattern test = Pattern.compile(groupPatterns[i]);
+ Matcher mat = test.matcher("ababababbaaabb");
+ mat.matches();
+ assertEquals(i + 1, mat.groupCount());
+
+ }
+ }
+
+ public void testRelactantQuantifiers() {
+ Pattern pat = Pattern.compile("(ab*)*b");
+ Matcher mat = pat.matcher("abbbb");
+
+ if (mat.matches()) {
+ assertEquals("abbb", mat.group(1));
+ } else {
+ fail("Match expected: (ab*)*b vs abbbb");
+ }
+ }
+
+ public void testEnhancedFind() {
+ String input = "foob";
+ String pattern = "a*b";
+ Pattern pat = Pattern.compile(pattern);
+ Matcher mat = pat.matcher(input);
+
+ mat.find();
+ assertEquals("b", mat.group());
+ }
+
+ public void testPosCompositeGroup() {
+ String[] posExamples = { "aabbcc", "aacc", "bbaabbcc" };
+ String[] negExamples = { "aabb", "bb", "bbaabb" };
+ Pattern posPat = Pattern.compile("(aa|bb){1,3}+cc");
+ Pattern negPat = Pattern.compile("(aa|bb){1,3}+bb");
+
+ Matcher mat;
+ for (String element : posExamples) {
+ mat = posPat.matcher(element);
+ assertTrue(mat.matches());
+ }
+
+ for (String element : negExamples) {
+ mat = negPat.matcher(element);
+ assertFalse(mat.matches());
+ }
+
+ assertTrue(Pattern.matches("(aa|bb){1,3}+bb", "aabbaabb"));
+
+ }
+
+ public void testPosAltGroup() {
+ String[] posExamples = { "aacc", "bbcc", "cc" };
+ String[] negExamples = { "bb", "aa" };
+ Pattern posPat = Pattern.compile("(aa|bb)?+cc");
+ Pattern negPat = Pattern.compile("(aa|bb)?+bb");
+
+ Matcher mat;
+ for (String element : posExamples) {
+ mat = posPat.matcher(element);
+ assertTrue(posPat.toString() + " vs: " + element, mat.matches());
+ }
+
+ for (String element : negExamples) {
+ mat = negPat.matcher(element);
+ assertFalse(mat.matches());
+ }
+
+ assertTrue(Pattern.matches("(aa|bb)?+bb", "aabb"));
+ }
+
+ public void testRelCompGroup() {
+
+ Matcher mat;
+ Pattern pat;
+ String res = "";
+ for (int i = 0; i < 4; i++) {
+ pat = Pattern.compile("((aa|bb){" + i + ",3}?).*cc");
+ mat = pat.matcher("aaaaaacc");
+ assertTrue(pat.toString() + " vs: " + "aaaaaacc", mat.matches());
+ assertEquals(res, mat.group(1));
+ res += "aa";
+ }
+ }
+
+ public void testRelAltGroup() {
+
+ Matcher mat;
+ Pattern pat;
+
+ pat = Pattern.compile("((aa|bb)??).*cc");
+ mat = pat.matcher("aacc");
+ assertTrue(pat.toString() + " vs: " + "aacc", mat.matches());
+ assertEquals("", mat.group(1));
+
+ pat = Pattern.compile("((aa|bb)??)cc");
+ mat = pat.matcher("aacc");
+ assertTrue(pat.toString() + " vs: " + "aacc", mat.matches());
+ assertEquals("aa", mat.group(1));
+ }
+
+ public void testIgnoreCase() {
+ Pattern pat = Pattern.compile("(aa|bb)*", Pattern.CASE_INSENSITIVE);
+ Matcher mat = pat.matcher("aAbb");
+
+ assertTrue(mat.matches());
+
+ pat = Pattern.compile("(a|b|c|d|e)*", Pattern.CASE_INSENSITIVE);
+ mat = pat.matcher("aAebbAEaEdebbedEccEdebbedEaedaebEbdCCdbBDcdcdADa");
+ assertTrue(mat.matches());
+
+ pat = Pattern.compile("[a-e]*", Pattern.CASE_INSENSITIVE);
+ mat = pat.matcher("aAebbAEaEdebbedEccEdebbedEaedaebEbdCCdbBDcdcdADa");
+ assertTrue(mat.matches());
+
+ }
+
+ public void testQuoteReplacement() {
+ assertEquals("\\\\aaCC\\$1", Matcher.quoteReplacement("\\aaCC$1"));
+ }
+
+ public void testOverFlow() {
+ Pattern tp = Pattern.compile("(a*)*");
+ Matcher tm = tp.matcher("aaa");
+ assertTrue(tm.matches());
+ assertEquals("", tm.group(1));
+
+ assertTrue(Pattern.matches("(1+)\\1+", "11"));
+ assertTrue(Pattern.matches("(1+)(2*)\\2+", "11"));
+
+ Pattern pat = Pattern.compile("(1+)\\1*");
+ Matcher mat = pat.matcher("11");
+
+ assertTrue(mat.matches());
+ assertEquals("11", mat.group(1));
+
+ pat = Pattern.compile("((1+)|(2+))(\\2+)");
+ mat = pat.matcher("11");
+
+ assertTrue(mat.matches());
+ assertEquals("1", mat.group(2));
+ assertEquals("1", mat.group(1));
+ assertEquals("1", mat.group(4));
+ assertNull(mat.group(3));
+
+ }
+
+ public void testUnicode() {
+
+ assertTrue(Pattern.matches("\\x61a", "aa"));
+ assertTrue(Pattern.matches("\\u0061a", "aa"));
+ assertTrue(Pattern.matches("\\0141a", "aa"));
+ assertTrue(Pattern.matches("\\0777", "?7"));
+
+ }
+
+ public void testUnicodeCategory() {
+ assertTrue(Pattern.matches("\\p{Ll}", "k")); // Unicode lower case
+ assertTrue(Pattern.matches("\\P{Ll}", "K")); // Unicode non-lower
+ // case
+ assertTrue(Pattern.matches("\\p{Lu}", "K")); // Unicode upper case
+ assertTrue(Pattern.matches("\\P{Lu}", "k")); // Unicode non-upper
+ // case
+ // combinations
+ assertTrue(Pattern.matches("[\\p{L}&&[^\\p{Lu}]]", "k"));
+ assertTrue(Pattern.matches("[\\p{L}&&[^\\p{Ll}]]", "K"));
+ assertFalse(Pattern.matches("[\\p{L}&&[^\\p{Lu}]]", "K"));
+ assertFalse(Pattern.matches("[\\p{L}&&[^\\p{Ll}]]", "k"));
+
+ // category/character combinations
+ assertFalse(Pattern.matches("[\\p{L}&&[^a-z]]", "k"));
+ assertTrue(Pattern.matches("[\\p{L}&&[^a-z]]", "K"));
+
+ assertTrue(Pattern.matches("[\\p{Lu}a-z]", "k"));
+ assertTrue(Pattern.matches("[a-z\\p{Lu}]", "k"));
+
+ assertFalse(Pattern.matches("[\\p{Lu}a-d]", "k"));
+ assertTrue(Pattern.matches("[a-d\\p{Lu}]", "K"));
+
+ // assertTrue(Pattern.matches("[\\p{L}&&[^\\p{Lu}&&[^K]]]", "K"));
+ assertFalse(Pattern.matches("[\\p{L}&&[^\\p{Lu}&&[^G]]]", "K"));
+
+ }
+
+ public void testSplitEmpty() {
+
+ Pattern pat = Pattern.compile("");
+ String[] s = pat.split("", -1);
+
+ assertEquals(1, s.length);
+ assertEquals("", s[0]);
+ }
+
+ public void testFindDollar() {
+ Matcher mat = Pattern.compile("a$").matcher("a\n");
+ assertTrue(mat.find());
+ assertEquals("a", mat.group());
+ }
+
+ /*
+ * Verify if the Matcher can match the input when region is changed
+ */
+ public void testMatchesRegionChanged() {
+ // Regression for HARMONY-610
+ String input = " word ";
+ Pattern pattern = Pattern.compile("\\w+");
+ Matcher matcher = pattern.matcher(input);
+ matcher.region(1, 5);
+ assertTrue(matcher.matches());
+ }
+
+ public void testAllCodePoints() {
+ // Regression for HARMONY-3145
+ int[] codePoint = new int[1];
+ Pattern p = Pattern.compile("(\\p{all})+");
+ boolean res = true;
+ int cnt = 0;
+ String s;
+ for (int i = 0; i < 0x110000; i++) {
+ codePoint[0] = i;
+ s = new String(codePoint, 0, 1);
+ if (!s.matches(p.toString())) {
+ cnt++;
+ res = false;
+ }
+ }
+ assertTrue(res);
+ assertEquals(0, cnt);
+
+ p = Pattern.compile("(\\P{all})+");
+ res = true;
+ cnt = 0;
+
+ for (int i = 0; i < 0x110000; i++) {
+ codePoint[0] = i;
+ s = new String(codePoint, 0, 1);
+ if (!s.matches(p.toString())) {
+ cnt++;
+ res = false;
+ }
+ }
+
+ assertFalse(res);
+ assertEquals(0x110000, cnt);
+ }
+
+ /*
+ * Verify if the Matcher behaves correct when region is changed
+ */
+ public void testFindRegionChanged() {
+ // Regression for HARMONY-625
+ Pattern pattern = Pattern.compile("(?s).*");
+ Matcher matcher = pattern.matcher("abcde");
+ matcher.find();
+ assertEquals("abcde", matcher.group());
+
+ matcher = pattern.matcher("abcde");
+ matcher.region(0, 2);
+ matcher.find();
+ assertEquals("ab", matcher.group());
+
+ }
+
+ /*
+ * Verify if the Matcher behaves correct with pattern "c" when region is
+ * changed
+ */
+ public void testFindRegionChanged2() {
+ // Regression for HARMONY-713
+ Pattern pattern = Pattern.compile("c");
+
+ String inputStr = "aabb.c";
+ Matcher matcher = pattern.matcher(inputStr);
+ matcher.region(0, 3);
+
+ assertFalse(matcher.find());
+ }
+
+ /*
+ * Regression test for HARMONY-674
+ */
+ public void testPatternMatcher() throws Exception {
+ Pattern pattern = Pattern.compile("(?:\\d+)(?:pt)");
+ assertTrue(pattern.matcher("14pt").matches());
+ }
+
+ /**
+ * Inspired by HARMONY-3360
+ */
+ public void test3360() {
+ String str = "!\"#%&'(),-./";
+ Pattern p = Pattern.compile("\\s");
+ Matcher m = p.matcher(str);
+
+ assertFalse(m.find());
+ }
+
+ /**
+ * Regression test for HARMONY-3360
+ */
+ public void testGeneralPunctuationCategory() {
+ String[] s = { ",", "!", "\"", "#", "%", "&", "'", "(", ")", "-", ".",
+ "/" };
+ String regexp = "\\p{P}";
+
+ for (int i = 0; i < s.length; i++) {
+ Pattern pattern = Pattern.compile(regexp);
+ Matcher matcher = pattern.matcher(s[i]);
+ assertTrue(matcher.find());
+ }
+ }
+
+ /**
+ * Regression test for HARMONY-4396
+ */
+ public void testHitEndAfterFind() {
+ hitEndTest(true, "#01.0", "r((ege)|(geg))x", "regexx", false);
+ hitEndTest(true, "#01.1", "r((ege)|(geg))x", "regex", false);
+ hitEndTest(true, "#01.2", "r((ege)|(geg))x", "rege", true);
+ hitEndTest(true, "#01.2", "r((ege)|(geg))x", "xregexx", false);
+
+ hitEndTest(true, "#02.0", "regex", "rexreger", true);
+ hitEndTest(true, "#02.1", "regex", "raxregexr", false);
+
+ String floatRegex = getHexFloatRegex();
+ hitEndTest(true, "#03.0", floatRegex, Double.toHexString(-1.234d), true);
+ hitEndTest(true, "#03.1", floatRegex, "1 ABC"
+ + Double.toHexString(Double.NaN) + "buhuhu", false);
+ hitEndTest(true, "#03.2", floatRegex, Double.toHexString(-0.0) + "--",
+ false);
+ hitEndTest(true, "#03.3", floatRegex, "--"
+ + Double.toHexString(Double.MIN_VALUE) + "--", false);
+
+ hitEndTest(true, "#04.0", "(\\d+) fish (\\d+) fish (\\w+) fish (\\d+)",
+ "1 fish 2 fish red fish 5", true);
+ hitEndTest(true, "#04.1", "(\\d+) fish (\\d+) fish (\\w+) fish (\\d+)",
+ "----1 fish 2 fish red fish 5----", false);
+ }
+
+ /*
+ * Test if Matcher's toString conatain pattern information
+ */
+ public void testToString() {
+ String result = Pattern.compile("(\\d{1,3})").matcher(
+ "aaaa123456789045").toString();
+ assertTrue("The result doesn't contain pattern info", result
+ .contains("(\\d{1,3})"));
+ }
+
+ private void hitEndTest(boolean callFind, String testNo, String regex,
+ String input, boolean hit) {
+ Pattern pattern = Pattern.compile(regex);
+ Matcher matcher = pattern.matcher(input);
+ if (callFind) {
+ matcher.find();
+ } else {
+ matcher.matches();
+ }
+ boolean h = matcher.hitEnd();
+
+ assertTrue(testNo, h == hit);
+ }
+
+ private String getHexFloatRegex() {
+ String hexDecimal = "(-|\\+)?0[xX][0-9a-fA-F]*\\.[0-9a-fA-F]+([pP](-|\\+)?[0-9]+)?";
+ String notANumber = "((-|\\+)?Infinity)|([nN]a[nN])";
+ return new StringBuilder("((").append(hexDecimal).append(")|(").append(
+ notANumber).append("))").toString();
+ }
+}
diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/ModeTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/ModeTest.java
new file mode 100644
index 0000000..c34cebe
--- /dev/null
+++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/ModeTest.java
@@ -0,0 +1,111 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.harmony.tests.java.util.regex;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.regex.PatternSyntaxException;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests Pattern compilation modes and modes triggered in pattern strings
+ */
+@SuppressWarnings("nls")
+public class ModeTest extends TestCase {
+ public void testCase() throws PatternSyntaxException {
+ Pattern p;
+ Matcher m;
+
+ p = Pattern.compile("([a-z]+)[0-9]+");
+ m = p.matcher("cAT123#dog345");
+ assertTrue(m.find());
+ assertEquals("dog", m.group(1));
+ assertFalse(m.find());
+
+ p = Pattern.compile("([a-z]+)[0-9]+", Pattern.CASE_INSENSITIVE);
+ m = p.matcher("cAt123#doG345");
+ assertTrue(m.find());
+ assertEquals("cAt", m.group(1));
+ assertTrue(m.find());
+ assertEquals("doG", m.group(1));
+ assertFalse(m.find());
+
+ p = Pattern.compile("(?i)([a-z]+)[0-9]+");
+ m = p.matcher("cAt123#doG345");
+ assertTrue(m.find());
+ assertEquals("cAt", m.group(1));
+ assertTrue(m.find());
+ assertEquals("doG", m.group(1));
+ assertFalse(m.find());
+ }
+
+ public void testMultiline() throws PatternSyntaxException {
+ Pattern p;
+ Matcher m;
+
+ p = Pattern.compile("^foo");
+ m = p.matcher("foobar");
+ assertTrue(m.find());
+ assertTrue(m.start() == 0 && m.end() == 3);
+ assertFalse(m.find());
+
+ m = p.matcher("barfoo");
+ assertFalse(m.find());
+
+ p = Pattern.compile("foo$");
+ m = p.matcher("foobar");
+ assertFalse(m.find());
+
+ m = p.matcher("barfoo");
+ assertTrue(m.find());
+ assertTrue(m.start() == 3 && m.end() == 6);
+ assertFalse(m.find());
+
+ p = Pattern.compile("^foo([0-9]*)", Pattern.MULTILINE);
+ m = p.matcher("foo1bar\nfoo2foo3\nbarfoo4");
+ assertTrue(m.find());
+ assertEquals("1", m.group(1));
+ assertTrue(m.find());
+ assertEquals("2", m.group(1));
+ assertFalse(m.find());
+
+ p = Pattern.compile("foo([0-9]*)$", Pattern.MULTILINE);
+ m = p.matcher("foo1bar\nfoo2foo3\nbarfoo4");
+ assertTrue(m.find());
+ assertEquals("3", m.group(1));
+ assertTrue(m.find());
+ assertEquals("4", m.group(1));
+ assertFalse(m.find());
+
+ p = Pattern.compile("(?m)^foo([0-9]*)");
+ m = p.matcher("foo1bar\nfoo2foo3\nbarfoo4");
+ assertTrue(m.find());
+ assertEquals("1", m.group(1));
+ assertTrue(m.find());
+ assertEquals("2", m.group(1));
+ assertFalse(m.find());
+
+ p = Pattern.compile("(?m)foo([0-9]*)$");
+ m = p.matcher("foo1bar\nfoo2foo3\nbarfoo4");
+ assertTrue(m.find());
+ assertEquals("3", m.group(1));
+ assertTrue(m.find());
+ assertEquals("4", m.group(1));
+ assertFalse(m.find());
+ }
+}
diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/Pattern2Test.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/Pattern2Test.java
new file mode 100644
index 0000000..e2fec73
--- /dev/null
+++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/Pattern2Test.java
@@ -0,0 +1,1409 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.harmony.tests.java.util.regex;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.regex.PatternSyntaxException;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests simple Pattern compilation and Matcher methods
+ */
+@SuppressWarnings("nls")
+public class Pattern2Test extends TestCase {
+ public void testSimpleMatch() throws PatternSyntaxException {
+ Pattern p = Pattern.compile("foo.*");
+
+ Matcher m1 = p.matcher("foo123");
+ assertTrue(m1.matches());
+ assertTrue(m1.find(0));
+ assertTrue(m1.lookingAt());
+
+ Matcher m2 = p.matcher("fox");
+ assertFalse(m2.matches());
+ assertFalse(m2.find(0));
+ assertFalse(m2.lookingAt());
+
+ assertTrue(Pattern.matches("foo.*", "foo123"));
+ assertFalse(Pattern.matches("foo.*", "fox"));
+
+ assertFalse(Pattern.matches("bar", "foobar"));
+
+ assertTrue(Pattern.matches("", ""));
+ }
+
+ public void testCursors() {
+ Pattern p;
+ Matcher m;
+
+ try {
+ p = Pattern.compile("foo");
+
+ m = p.matcher("foobar");
+ assertTrue(m.find());
+ assertEquals(0, m.start());
+ assertEquals(3, m.end());
+ assertFalse(m.find());
+
+ // Note: also testing reset here
+ m.reset();
+ assertTrue(m.find());
+ assertEquals(0, m.start());
+ assertEquals(3, m.end());
+ assertFalse(m.find());
+
+ m.reset("barfoobar");
+ assertTrue(m.find());
+ assertEquals(3, m.start());
+ assertEquals(6, m.end());
+ assertFalse(m.find());
+
+ m.reset("barfoo");
+ assertTrue(m.find());
+ assertEquals(3, m.start());
+ assertEquals(6, m.end());
+ assertFalse(m.find());
+
+ m.reset("foobarfoobarfoo");
+ assertTrue(m.find());
+ assertEquals(0, m.start());
+ assertEquals(3, m.end());
+ assertTrue(m.find());
+ assertEquals(6, m.start());
+ assertEquals(9, m.end());
+ assertTrue(m.find());
+ assertEquals(12, m.start());
+ assertEquals(15, m.end());
+ assertFalse(m.find());
+ assertTrue(m.find(0));
+ assertEquals(0, m.start());
+ assertEquals(3, m.end());
+ assertTrue(m.find(4));
+ assertEquals(6, m.start());
+ assertEquals(9, m.end());
+ } catch (PatternSyntaxException e) {
+ System.out.println(e.getMessage());
+ fail();
+ }
+ }
+
+ public void testGroups() throws PatternSyntaxException {
+ Pattern p;
+ Matcher m;
+
+ p = Pattern.compile("(p[0-9]*)#?(q[0-9]*)");
+
+ m = p.matcher("p1#q3p2q42p5p71p63#q888");
+ assertTrue(m.find());
+ assertEquals(0, m.start());
+ assertEquals(5, m.end());
+ assertEquals(2, m.groupCount());
+ assertEquals(0, m.start(0));
+ assertEquals(5, m.end(0));
+ assertEquals(0, m.start(1));
+ assertEquals(2, m.end(1));
+ assertEquals(3, m.start(2));
+ assertEquals(5, m.end(2));
+ assertEquals("p1#q3", m.group());
+ assertEquals("p1#q3", m.group(0));
+ assertEquals("p1", m.group(1));
+ assertEquals("q3", m.group(2));
+
+ assertTrue(m.find());
+ assertEquals(5, m.start());
+ assertEquals(10, m.end());
+ assertEquals(2, m.groupCount());
+ assertEquals(10, m.end(0));
+ assertEquals(5, m.start(1));
+ assertEquals(7, m.end(1));
+ assertEquals(7, m.start(2));
+ assertEquals(10, m.end(2));
+ assertEquals("p2q42", m.group());
+ assertEquals("p2q42", m.group(0));
+ assertEquals("p2", m.group(1));
+ assertEquals("q42", m.group(2));
+
+ assertTrue(m.find());
+ assertEquals(15, m.start());
+ assertEquals(23, m.end());
+ assertEquals(2, m.groupCount());
+ assertEquals(15, m.start(0));
+ assertEquals(23, m.end(0));
+ assertEquals(15, m.start(1));
+ assertEquals(18, m.end(1));
+ assertEquals(19, m.start(2));
+ assertEquals(23, m.end(2));
+ assertEquals("p63#q888", m.group());
+ assertEquals("p63#q888", m.group(0));
+ assertEquals("p63", m.group(1));
+ assertEquals("q888", m.group(2));
+ assertFalse(m.find());
+ }
+
+ public void testReplace() throws PatternSyntaxException {
+ Pattern p;
+ Matcher m;
+
+ // Note: examples from book,
+ // Hitchens, Ron, 2002, "Java NIO", O'Reilly, page 171
+ p = Pattern.compile("a*b");
+
+ m = p.matcher("aabfooaabfooabfoob");
+ assertTrue(m.replaceAll("-").equals("-foo-foo-foo-"));
+ assertTrue(m.replaceFirst("-").equals("-fooaabfooabfoob"));
+
+ /*
+ * p = Pattern.compile ("\\p{Blank}");
+ *
+ * m = p.matcher ("fee fie foe fum"); assertTrue
+ * (m.replaceFirst("-").equals ("fee-fie foe fum")); assertTrue
+ * (m.replaceAll("-").equals ("fee-fie-foe-fum"));
+ */
+
+ p = Pattern.compile("([bB])yte");
+
+ m = p.matcher("Byte for byte");
+ assertTrue(m.replaceFirst("$1ite").equals("Bite for byte"));
+ assertTrue(m.replaceAll("$1ite").equals("Bite for bite"));
+
+ p = Pattern.compile("\\d\\d\\d\\d([- ])");
+
+ m = p.matcher("card #1234-5678-1234");
+ assertTrue(m.replaceFirst("xxxx$1").equals("card #xxxx-5678-1234"));
+ assertTrue(m.replaceAll("xxxx$1").equals("card #xxxx-xxxx-1234"));
+
+ p = Pattern.compile("(up|left)( *)(right|down)");
+
+ m = p.matcher("left right, up down");
+ assertTrue(m.replaceFirst("$3$2$1").equals("right left, up down"));
+ assertTrue(m.replaceAll("$3$2$1").equals("right left, down up"));
+
+ p = Pattern.compile("([CcPp][hl]e[ea]se)");
+
+ m = p.matcher("I want cheese. Please.");
+ assertTrue(m.replaceFirst("<b> $1 </b>").equals(
+ "I want <b> cheese </b>. Please."));
+ assertTrue(m.replaceAll("<b> $1 </b>").equals(
+ "I want <b> cheese </b>. <b> Please </b>."));
+ }
+
+ public void testEscapes() throws PatternSyntaxException {
+ Pattern p;
+ Matcher m;
+
+ // Test \\ sequence
+ p = Pattern.compile("([a-z]+)\\\\([a-z]+);");
+ m = p.matcher("fred\\ginger;abbott\\costello;jekell\\hyde;");
+ assertTrue(m.find());
+ assertEquals("fred", m.group(1));
+ assertEquals("ginger", m.group(2));
+ assertTrue(m.find());
+ assertEquals("abbott", m.group(1));
+ assertEquals("costello", m.group(2));
+ assertTrue(m.find());
+ assertEquals("jekell", m.group(1));
+ assertEquals("hyde", m.group(2));
+ assertFalse(m.find());
+
+ // Test \n, \t, \r, \f, \e, \a sequences
+ p = Pattern.compile("([a-z]+)[\\n\\t\\r\\f\\e\\a]+([a-z]+)");
+ m = p.matcher("aa\nbb;cc\u0009\rdd;ee\u000C\u001Bff;gg\n\u0007hh");
+ assertTrue(m.find());
+ assertEquals("aa", m.group(1));
+ assertEquals("bb", m.group(2));
+ assertTrue(m.find());
+ assertEquals("cc", m.group(1));
+ assertEquals("dd", m.group(2));
+ assertTrue(m.find());
+ assertEquals("ee", m.group(1));
+ assertEquals("ff", m.group(2));
+ assertTrue(m.find());
+ assertEquals("gg", m.group(1));
+ assertEquals("hh", m.group(2));
+ assertFalse(m.find());
+
+ // Test \\u and \\x sequences
+p = Pattern.compile("([0-9]+)[\\u0020:\\x21];");
+ m = p.matcher("11:;22 ;33-;44!;");
+ assertTrue(m.find());
+ assertEquals("11", m.group(1));
+ assertTrue(m.find());
+ assertEquals("22", m.group(1));
+ assertTrue(m.find());
+ assertEquals("44", m.group(1));
+ assertFalse(m.find());
+
+ // Test invalid unicode sequences
+ try {
+ p = Pattern.compile("\\u");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException e) {
+ }
+
+ try {
+ p = Pattern.compile("\\u;");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException e) {
+ }
+
+ try {
+ p = Pattern.compile("\\u002");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException e) {
+ }
+
+ try {
+ p = Pattern.compile("\\u002;");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException e) {
+ }
+
+ // Test invalid hex sequences
+ try {
+ p = Pattern.compile("\\x");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException e) {
+ }
+
+ try {
+ p = Pattern.compile("\\x;");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException e) {
+ }
+
+ try {
+ p = Pattern.compile("\\xa");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException e) {
+ }
+
+ try {
+ p = Pattern.compile("\\xa;");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException e) {
+ }
+
+ // Test \0 (octal) sequences (1, 2 and 3 digit)
+ p = Pattern.compile("([0-9]+)[\\07\\040\\0160];");
+ m = p.matcher("11\u0007;22:;33 ;44p;");
+ assertTrue(m.find());
+ assertEquals("11", m.group(1));
+ assertTrue(m.find());
+ assertEquals("33", m.group(1));
+ assertTrue(m.find());
+ assertEquals("44", m.group(1));
+ assertFalse(m.find());
+
+ // Test invalid octal sequences
+ try {
+ p = Pattern.compile("\\08");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException e) {
+ }
+
+ // originally contributed test did not check the result
+ // TODO: check what RI does here
+ // try {
+ // p = Pattern.compile("\\0477");
+ // fail("PatternSyntaxException expected");
+ // } catch (PatternSyntaxException e) {
+ // }
+
+ try {
+ p = Pattern.compile("\\0");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException e) {
+ }
+
+ try {
+ p = Pattern.compile("\\0;");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException e) {
+ }
+
+ // Test \c (control character) sequence
+ p = Pattern.compile("([0-9]+)[\\cA\\cB\\cC\\cD];");
+ m = p.matcher("11\u0001;22:;33\u0002;44p;55\u0003;66\u0004;");
+ assertTrue(m.find());
+ assertEquals("11", m.group(1));
+ assertTrue(m.find());
+ assertEquals("33", m.group(1));
+ assertTrue(m.find());
+ assertEquals("55", m.group(1));
+ assertTrue(m.find());
+ assertEquals("66", m.group(1));
+ assertFalse(m.find());
+
+ // More thorough control escape test
+ // Ensure that each escape matches exactly the corresponding
+ // character
+ // code and no others (well, from 0-255 at least)
+ int i, j;
+ for (i = 0; i < 26; i++) {
+ p = Pattern.compile("\\c" + Character.toString((char) ('A' + i)));
+ int match_char = -1;
+ for (j = 0; j < 255; j++) {
+ m = p.matcher(Character.toString((char) j));
+ if (m.matches()) {
+ assertEquals(-1, match_char);
+ match_char = j;
+ }
+ }
+ assertTrue(match_char == i + 1);
+ }
+
+ // Test invalid control escapes
+ try {
+ p = Pattern.compile("\\c");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException e) {
+ }
+
+ // originally contributed test did not check the result
+ // TODO: check what RI does here
+ // try {
+ // p = Pattern.compile("\\c;");
+ // fail("PatternSyntaxException expected");
+ // } catch (PatternSyntaxException e) {
+ // }
+ //
+ // try {
+ // p = Pattern.compile("\\ca;");
+ // fail("PatternSyntaxException expected");
+ // } catch (PatternSyntaxException e) {
+ // }
+ //
+ // try {
+ // p = Pattern.compile("\\c4;");
+ // fail("PatternSyntaxException expected");
+ // } catch (PatternSyntaxException e) {
+ // }
+ }
+
+ public void testCharacterClasses() throws PatternSyntaxException {
+ Pattern p;
+ Matcher m;
+
+ // Test one character range
+ p = Pattern.compile("[p].*[l]");
+ m = p.matcher("paul");
+ assertTrue(m.matches());
+ m = p.matcher("pool");
+ assertTrue(m.matches());
+ m = p.matcher("pong");
+ assertFalse(m.matches());
+ m = p.matcher("pl");
+ assertTrue(m.matches());
+
+ // Test two character range
+ p = Pattern.compile("[pm].*[lp]");
+ m = p.matcher("prop");
+ assertTrue(m.matches());
+ m = p.matcher("mall");
+ assertTrue(m.matches());
+ m = p.matcher("pong");
+ assertFalse(m.matches());
+ m = p.matcher("pill");
+ assertTrue(m.matches());
+
+ // Test range including [ and ]
+ p = Pattern.compile("[<\\[].*[\\]>]");
+ m = p.matcher("<foo>");
+ assertTrue(m.matches());
+ m = p.matcher("[bar]");
+ assertTrue(m.matches());
+ m = p.matcher("{foobar]");
+ assertFalse(m.matches());
+ m = p.matcher("<pill]");
+ assertTrue(m.matches());
+
+ // Test range using ^
+ p = Pattern.compile("[^bc][a-z]+[tr]");
+ m = p.matcher("pat");
+ assertTrue(m.matches());
+ m = p.matcher("liar");
+ assertTrue(m.matches());
+ m = p.matcher("car");
+ assertFalse(m.matches());
+ m = p.matcher("gnat");
+ assertTrue(m.matches());
+
+ // Test character range using -
+ p = Pattern.compile("[a-z]_+[a-zA-Z]-+[0-9p-z]");
+ m = p.matcher("d__F-8");
+ assertTrue(m.matches());
+ m = p.matcher("c_a-q");
+ assertTrue(m.matches());
+ m = p.matcher("a__R-a");
+ assertFalse(m.matches());
+ m = p.matcher("r_____d-----5");
+ assertTrue(m.matches());
+
+ // Test range using unicode characters and unicode and hex escapes
+ p = Pattern.compile("[\\u1234-\\u2345]_+[a-z]-+[\u0001-\\x11]");
+ m = p.matcher("\u2000_q-\u0007");
+ assertTrue(m.matches());
+ m = p.matcher("\u1234_z-\u0001");
+ assertTrue(m.matches());
+ m = p.matcher("r_p-q");
+ assertFalse(m.matches());
+ m = p.matcher("\u2345_____d-----\n");
+ assertTrue(m.matches());
+
+ // Test ranges including the "-" character
+ p = Pattern.compile("[\\*-/]_+[---]!+[--AP]");
+ m = p.matcher("-_-!!A");
+ assertTrue(m.matches());
+ m = p.matcher("\u002b_-!!!-");
+ assertTrue(m.matches());
+ m = p.matcher("!_-!@");
+ assertFalse(m.matches());
+ m = p.matcher(",______-!!!!!!!P");
+ assertTrue(m.matches());
+
+ // Test nested ranges
+ p = Pattern.compile("[pm[t]][a-z]+[[r]lp]");
+ m = p.matcher("prop");
+ assertTrue(m.matches());
+ m = p.matcher("tsar");
+ assertTrue(m.matches());
+ m = p.matcher("pong");
+ assertFalse(m.matches());
+ m = p.matcher("moor");
+ assertTrue(m.matches());
+
+ // Test character class intersection with &&
+ // TODO: figure out what x&&y or any class with a null intersection
+ // set (like [[a-c]&&[d-f]]) might mean. It doesn't mean "match
+ // nothing" and doesn't mean "match anything" so I'm stumped.
+ p = Pattern.compile("[[a-p]&&[g-z]]+-+[[a-z]&&q]-+[x&&[a-z]]-+");
+ m = p.matcher("h--q--x--");
+ assertTrue(m.matches());
+ m = p.matcher("hog--q-x-");
+ assertTrue(m.matches());
+ m = p.matcher("ape--q-x-");
+ assertFalse(m.matches());
+ m = p.matcher("mop--q-x----");
+ assertTrue(m.matches());
+
+ // Test error cases with &&
+ p = Pattern.compile("[&&[xyz]]");
+ m = p.matcher("&");
+ // System.out.println(m.matches());
+ m = p.matcher("x");
+ // System.out.println(m.matches());
+ m = p.matcher("y");
+ // System.out.println(m.matches());
+ p = Pattern.compile("[[xyz]&[axy]]");
+ m = p.matcher("x");
+ // System.out.println(m.matches());
+ m = p.matcher("z");
+ // System.out.println(m.matches());
+ m = p.matcher("&");
+ // System.out.println(m.matches());
+ p = Pattern.compile("[abc[123]&&[345]def]");
+ m = p.matcher("a");
+ // System.out.println(m.matches());
+
+ p = Pattern.compile("[[xyz]&&]");
+
+ p = Pattern.compile("[[abc]&]");
+
+ try {
+ p = Pattern.compile("[[abc]&&");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException e) {
+ }
+
+ p = Pattern.compile("[[abc]\\&&[xyz]]");
+
+ p = Pattern.compile("[[abc]&\\&[xyz]]");
+
+ // Test 3-way intersection
+ p = Pattern.compile("[[a-p]&&[g-z]&&[d-k]]");
+ m = p.matcher("g");
+ assertTrue(m.matches());
+ m = p.matcher("m");
+ assertFalse(m.matches());
+
+ // Test nested intersection
+ p = Pattern.compile("[[[a-p]&&[g-z]]&&[d-k]]");
+ m = p.matcher("g");
+ assertTrue(m.matches());
+ m = p.matcher("m");
+ assertFalse(m.matches());
+
+ // Test character class subtraction with && and ^
+ p = Pattern.compile("[[a-z]&&[^aeiou]][aeiou][[^xyz]&&[a-z]]");
+ m = p.matcher("pop");
+ assertTrue(m.matches());
+ m = p.matcher("tag");
+ assertTrue(m.matches());
+ m = p.matcher("eat");
+ assertFalse(m.matches());
+ m = p.matcher("tax");
+ assertFalse(m.matches());
+ m = p.matcher("zip");
+ assertTrue(m.matches());
+
+ // Test . (DOT), with and without DOTALL
+ // Note: DOT not allowed in character classes
+ p = Pattern.compile(".+/x.z");
+ m = p.matcher("!$/xyz");
+ assertTrue(m.matches());
+ m = p.matcher("%\n\r/x\nz");
+ assertFalse(m.matches());
+ p = Pattern.compile(".+/x.z", Pattern.DOTALL);
+ m = p.matcher("%\n\r/x\nz");
+ assertTrue(m.matches());
+
+ // Test \d (digit)
+ p = Pattern.compile("\\d+[a-z][\\dx]");
+ m = p.matcher("42a6");
+ assertTrue(m.matches());
+ m = p.matcher("21zx");
+ assertTrue(m.matches());
+ m = p.matcher("ab6");
+ assertFalse(m.matches());
+ m = p.matcher("56912f9");
+ assertTrue(m.matches());
+
+ // Test \D (not a digit)
+ p = Pattern.compile("\\D+[a-z]-[\\D3]");
+ m = p.matcher("za-p");
+ assertTrue(m.matches());
+ m = p.matcher("%!e-3");
+ assertTrue(m.matches());
+ m = p.matcher("9a-x");
+ assertFalse(m.matches());
+ m = p.matcher("\u1234pp\ny-3");
+ assertTrue(m.matches());
+
+ // Test \s (whitespace)
+ p = Pattern.compile("<[a-zA-Z]+\\s+[0-9]+[\\sx][^\\s]>");
+ m = p.matcher("<cat \t1\fx>");
+ assertTrue(m.matches());
+ m = p.matcher("<cat \t1\f >");
+ assertFalse(m.matches());
+ m = p
+ .matcher("xyz <foo\n\r22 5> <pp \t\n\f\r \u000b41x\u1234><pp \nx7\rc> zzz");
+ assertTrue(m.find());
+ assertTrue(m.find());
+ assertFalse(m.find());
+
+ // Test \S (not whitespace)
+ p = Pattern.compile("<[a-z] \\S[0-9][\\S\n]+[^\\S]221>");
+ m = p.matcher("<f $0**\n** 221>");
+ assertTrue(m.matches());
+ m = p.matcher("<x 441\t221>");
+ assertTrue(m.matches());
+ m = p.matcher("<z \t9\ng 221>");
+ assertFalse(m.matches());
+ m = p.matcher("<z 60\ngg\u1234\f221>");
+ assertTrue(m.matches());
+ p = Pattern.compile("<[a-z] \\S[0-9][\\S\n]+[^\\S]221[\\S&&[^abc]]>");
+ m = p.matcher("<f $0**\n** 221x>");
+ assertTrue(m.matches());
+ m = p.matcher("<x 441\t221z>");
+ assertTrue(m.matches());
+ m = p.matcher("<x 441\t221 >");
+ assertFalse(m.matches());
+ m = p.matcher("<x 441\t221c>");
+ assertFalse(m.matches());
+ m = p.matcher("<z \t9\ng 221x>");
+ assertFalse(m.matches());
+ m = p.matcher("<z 60\ngg\u1234\f221\u0001>");
+ assertTrue(m.matches());
+
+ // Test \w (ascii word)
+ p = Pattern.compile("<\\w+\\s[0-9]+;[^\\w]\\w+/[\\w$]+;");
+ m = p.matcher("<f1 99;!foo5/a$7;");
+ assertTrue(m.matches());
+ m = p.matcher("<f$ 99;!foo5/a$7;");
+ assertFalse(m.matches());
+ m = p
+ .matcher("<abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789 99;!foo5/a$7;");
+ assertTrue(m.matches());
+
+ // Test \W (not an ascii word)
+ p = Pattern.compile("<\\W\\w+\\s[0-9]+;[\\W_][^\\W]+\\s[0-9]+;");
+ m = p.matcher("<$foo3\n99;_bar\t0;");
+ assertTrue(m.matches());
+ m = p.matcher("<hh 99;_g 0;");
+ assertFalse(m.matches());
+ m = p.matcher("<*xx\t00;^zz\f11;");
+ assertTrue(m.matches());
+
+ // Test x|y pattern
+ // TODO
+ }
+
+ public void testPOSIXGroups() throws PatternSyntaxException {
+ Pattern p;
+ Matcher m;
+
+ // Test POSIX groups using \p and \P (in the group and not in the group)
+ // Groups are Lower, Upper, ASCII, Alpha, Digit, XDigit, Alnum, Punct,
+ // Graph, Print, Blank, Space, Cntrl
+ // Test \p{Lower}
+ /*
+ * FIXME: Requires complex range processing p = Pattern.compile("<\\p{Lower}\\d\\P{Lower}:[\\p{Lower}Z]\\s[^\\P{Lower}]>");
+ * m = p.matcher("<a4P:g x>"); assertTrue(m.matches()); m = p.matcher("<p4%:Z\tq>");
+ * assertTrue(m.matches()); m = p.matcher("<A6#:e e>");
+ * assertFalse(m.matches());
+ */
+ p = Pattern.compile("\\p{Lower}+");
+ m = p.matcher("abcdefghijklmnopqrstuvwxyz");
+ assertTrue(m.matches());
+
+ // Invalid uses of \p{Lower}
+ try {
+ p = Pattern.compile("\\p");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException e) {
+ }
+
+ try {
+ p = Pattern.compile("\\p;");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException e) {
+ }
+
+ try {
+ p = Pattern.compile("\\p{");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException e) {
+ }
+
+ try {
+ p = Pattern.compile("\\p{;");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException e) {
+ }
+
+ try {
+ p = Pattern.compile("\\p{Lower");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException e) {
+ }
+
+ try {
+ p = Pattern.compile("\\p{Lower;");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException e) {
+ }
+
+ // Test \p{Upper}
+ /*
+ * FIXME: Requires complex range processing p = Pattern.compile("<\\p{Upper}\\d\\P{Upper}:[\\p{Upper}z]\\s[^\\P{Upper}]>");
+ * m = p.matcher("<A4p:G X>"); assertTrue(m.matches()); m = p.matcher("<P4%:z\tQ>");
+ * assertTrue(m.matches()); m = p.matcher("<a6#:E E>");
+ * assertFalse(m.matches());
+ */
+ p = Pattern.compile("\\p{Upper}+");
+ m = p.matcher("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
+ assertTrue(m.matches());
+
+ // Invalid uses of \p{Upper}
+ try {
+ p = Pattern.compile("\\p{Upper");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException e) {
+ }
+
+ try {
+ p = Pattern.compile("\\p{Upper;");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException e) {
+ }
+
+ // Test \p{ASCII}
+ /*
+ * FIXME: Requires complex range processing p = Pattern.compile("<\\p{ASCII}\\d\\P{ASCII}:[\\p{ASCII}\u1234]\\s[^\\P{ASCII}]>");
+ * m = p.matcher("<A4\u0080:G X>"); assertTrue(m.matches()); m =
+ * p.matcher("<P4\u00ff:\u1234\t\n>"); assertTrue(m.matches()); m =
+ * p.matcher("<\u00846#:E E>"); assertFalse(m.matches())
+ */
+ int i;
+ p = Pattern.compile("\\p{ASCII}");
+ for (i = 0; i < 0x80; i++) {
+ m = p.matcher(Character.toString((char) i));
+ assertTrue(m.matches());
+ }
+ for (; i < 0xff; i++) {
+ m = p.matcher(Character.toString((char) i));
+ assertFalse(m.matches());
+ }
+
+ // Invalid uses of \p{ASCII}
+ try {
+ p = Pattern.compile("\\p{ASCII");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException e) {
+ }
+
+ try {
+ p = Pattern.compile("\\p{ASCII;");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException e) {
+ }
+
+ // Test \p{Alpha}
+ // TODO
+
+ // Test \p{Digit}
+ // TODO
+
+ // Test \p{XDigit}
+ // TODO
+
+ // Test \p{Alnum}
+ // TODO
+
+ // Test \p{Punct}
+ // TODO
+
+ // Test \p{Graph}
+ // TODO
+
+ // Test \p{Print}
+ // TODO
+
+ // Test \p{Blank}
+ // TODO
+
+ // Test \p{Space}
+ // TODO
+
+ // Test \p{Cntrl}
+ // TODO
+ }
+
+ public void testUnicodeCategories() throws PatternSyntaxException {
+ // Test Unicode categories using \p and \P
+ // One letter codes: L, M, N, P, S, Z, C
+ // Two letter codes: Lu, Nd, Sc, Sm, ...
+ // See java.lang.Character and Unicode standard for complete list
+ // TODO
+ // Test \p{L}
+ // TODO
+
+ // Test \p{N}
+ // TODO
+
+ // ... etc
+
+ // Test two letter codes:
+ // From unicode.org:
+ // Lu
+ // Ll
+ // Lt
+ // Lm
+ // Lo
+ // Mn
+ // Mc
+ // Me
+ // Nd
+ // Nl
+ // No
+ // Pc
+ // Pd
+ // Ps
+ // Pe
+ // Pi
+ // Pf
+ // Po
+ // Sm
+ // Sc
+ // Sk
+ // So
+ // Zs
+ // Zl
+ // Zp
+ // Cc
+ // Cf
+ // Cs
+ // Co
+ // Cn
+ }
+
+ public void testUnicodeBlocks() throws PatternSyntaxException {
+ Pattern p;
+ Matcher m;
+ int i, j;
+
+ // Test Unicode blocks using \p and \P
+ // FIXME:
+ // Note that LatinExtended-B and ArabicPresentations-B are unrecognized
+ // by the reference JDK.
+ for (i = 0; i < UBlocks.length; i++) {
+ /*
+ * p = Pattern.compile("\\p{"+UBlocks[i].name+"}");
+ *
+ * if (UBlocks[i].low > 0) { m =
+ * p.matcher(Character.toString((char)(UBlocks[i].low-1)));
+ * assertFalse(m.matches()); } for (j=UBlocks[i].low; j <=
+ * UBlocks[i].high; j++) { m =
+ * p.matcher(Character.toString((char)j)); assertTrue(m.matches()); }
+ * if (UBlocks[i].high < 0xFFFF) { m =
+ * p.matcher(Character.toString((char)(UBlocks[i].high+1)));
+ * assertFalse(m.matches()); }
+ *
+ * p = Pattern.compile("\\P{"+UBlocks[i].name+"}");
+ *
+ * if (UBlocks[i].low > 0) { m =
+ * p.matcher(Character.toString((char)(UBlocks[i].low-1)));
+ * assertTrue(m.matches()); } for (j=UBlocks[i].low; j <
+ * UBlocks[i].high; j++) { m =
+ * p.matcher(Character.toString((char)j)); assertFalse(m.matches()); }
+ * if (UBlocks[i].high < 0xFFFF) { m =
+ * p.matcher(Character.toString((char)(UBlocks[i].high+1)));
+ * assertTrue(m.matches()); }
+ */
+
+ p = Pattern.compile("\\p{In" + UBlocks[i].name + "}");
+
+ if (UBlocks[i].low > 0) {
+ m = p.matcher(Character.toString((char) (UBlocks[i].low - 1)));
+ assertFalse(m.matches());
+ }
+ for (j = UBlocks[i].low; j <= UBlocks[i].high; j++) {
+ m = p.matcher(Character.toString((char) j));
+ assertTrue(m.matches());
+ }
+ if (UBlocks[i].high < 0xFFFF) {
+ m = p.matcher(Character.toString((char) (UBlocks[i].high + 1)));
+ assertFalse(m.matches());
+ }
+
+ p = Pattern.compile("\\P{In" + UBlocks[i].name + "}");
+
+ if (UBlocks[i].low > 0) {
+ m = p.matcher(Character.toString((char) (UBlocks[i].low - 1)));
+ assertTrue(m.matches());
+ }
+ for (j = UBlocks[i].low; j < UBlocks[i].high; j++) {
+ m = p.matcher(Character.toString((char) j));
+ assertFalse(m.matches());
+ }
+ if (UBlocks[i].high < 0xFFFF) {
+ m = p.matcher(Character.toString((char) (UBlocks[i].high + 1)));
+ assertTrue(m.matches());
+ }
+ }
+ }
+
+ public void testCapturingGroups() throws PatternSyntaxException {
+ // Test simple capturing groups
+ // TODO
+
+ // Test grouping without capture (?:...)
+ // TODO
+
+ // Test combination of grouping and capture
+ // TODO
+
+ // Test \<num> sequence with capturing and non-capturing groups
+ // TODO
+
+ // Test \<num> with <num> out of range
+ // TODO
+ }
+
+ public void testRepeats() {
+ // Test ?
+ // TODO
+
+ // Test *
+ // TODO
+
+ // Test +
+ // TODO
+
+ // Test {<num>}, including 0, 1 and more
+ // TODO
+
+ // Test {<num>,}, including 0, 1 and more
+ // TODO
+
+ // Test {<n1>,<n2>}, with n1 < n2, n1 = n2 and n1 > n2 (illegal?)
+ // TODO
+ }
+
+ public void testAnchors() throws PatternSyntaxException {
+ // Test ^, default and MULTILINE
+ // TODO
+
+ // Test $, default and MULTILINE
+ // TODO
+
+ // Test \b (word boundary)
+ // TODO
+
+ // Test \B (not a word boundary)
+ // TODO
+
+ // Test \A (beginning of string)
+ // TODO
+
+ // Test \Z (end of string)
+ // TODO
+
+ // Test \z (end of string)
+ // TODO
+
+ // Test \G
+ // TODO
+
+ // Test positive lookahead using (?=...)
+ // TODO
+
+ // Test negative lookahead using (?!...)
+ // TODO
+
+ // Test positive lookbehind using (?<=...)
+ // TODO
+
+ // Test negative lookbehind using (?<!...)
+ // TODO
+ }
+
+ public void testMisc() throws PatternSyntaxException {
+ Pattern p;
+ Matcher m;
+
+ // Test (?>...)
+ // TODO
+
+ // Test (?onflags-offflags)
+ // Valid flags are i,m,d,s,u,x
+ // TODO
+
+ // Test (?onflags-offflags:...)
+ // TODO
+
+ // Test \Q, \E
+ p = Pattern.compile("[a-z]+;\\Q[a-z]+;\\Q(foo.*);\\E[0-9]+");
+ m = p.matcher("abc;[a-z]+;\\Q(foo.*);411");
+ assertTrue(m.matches());
+ m = p.matcher("abc;def;foo42;555");
+ assertFalse(m.matches());
+ m = p.matcher("abc;\\Qdef;\\Qfoo99;\\E123");
+ assertFalse(m.matches());
+
+ p = Pattern.compile("[a-z]+;(foo[0-9]-\\Q(...)\\E);[0-9]+");
+ m = p.matcher("abc;foo5-(...);123");
+ assertTrue(m.matches());
+ assertEquals("foo5-(...)", m.group(1));
+ m = p.matcher("abc;foo9-(xxx);789");
+ assertFalse(m.matches());
+
+ p = Pattern.compile("[a-z]+;(bar[0-9]-[a-z\\Q$-\\E]+);[0-9]+");
+ m = p.matcher("abc;bar0-def$-;123");
+ assertTrue(m.matches());
+
+ // FIXME:
+ // This should work the same as the pattern above but fails with the
+ // the reference JDK
+ p = Pattern.compile("[a-z]+;(bar[0-9]-[a-z\\Q-$\\E]+);[0-9]+");
+ m = p.matcher("abc;bar0-def$-;123");
+ // assertTrue(m.matches());
+
+ // FIXME:
+ // This should work too .. it looks as if just about anything that
+ // has more
+ // than one character between \Q and \E is broken in the the reference
+ // JDK
+ p = Pattern.compile("[a-z]+;(bar[0-9]-[a-z\\Q[0-9]\\E]+);[0-9]+");
+ m = p.matcher("abc;bar0-def[99]-]0x[;123");
+ // assertTrue(m.matches());
+
+ // This is the same as above but with explicit escapes .. and this
+ // does work
+ // on the the reference JDK
+ p = Pattern.compile("[a-z]+;(bar[0-9]-[a-z\\[0\\-9\\]]+);[0-9]+");
+ m = p.matcher("abc;bar0-def[99]-]0x[;123");
+ assertTrue(m.matches());
+
+ // Test #<comment text>
+ // TODO
+ }
+
+ public void testCompile1() throws PatternSyntaxException {
+ Pattern pattern = Pattern
+ .compile("[0-9A-Za-z][0-9A-Za-z\\x2e\\x3a\\x2d\\x5f]*");
+ String name = "iso-8859-1";
+ assertTrue(pattern.matcher(name).matches());
+ }
+
+ public void testCompile2() throws PatternSyntaxException {
+ String findString = "\\Qimport\\E";
+
+ Pattern pattern = Pattern.compile(findString, 0);
+ Matcher matcher = pattern.matcher(new String(
+ "import a.A;\n\n import b.B;\nclass C {}"));
+
+ assertTrue(matcher.find(0));
+ }
+
+ public void testCompile3() throws PatternSyntaxException {
+ Pattern p;
+ Matcher m;
+ p = Pattern.compile("a$");
+ m = p.matcher("a\n");
+ assertTrue(m.find());
+ assertEquals("a", m.group());
+ assertFalse(m.find());
+
+ p = Pattern.compile("(a$)");
+ m = p.matcher("a\n");
+ assertTrue(m.find());
+ assertEquals("a", m.group());
+ assertEquals("a", m.group(1));
+ assertFalse(m.find());
+
+ p = Pattern.compile("^.*$", Pattern.MULTILINE);
+
+ m = p.matcher("a\n");
+ assertTrue(m.find());
+ // System.out.println("["+m.group()+"]");
+ assertEquals("a", m.group());
+ assertFalse(m.find());
+
+ m = p.matcher("a\nb\n");
+ assertTrue(m.find());
+ // System.out.println("["+m.group()+"]");
+ assertEquals("a", m.group());
+ assertTrue(m.find());
+ // System.out.println("["+m.group()+"]");
+ assertEquals("b", m.group());
+ assertFalse(m.find());
+
+ m = p.matcher("a\nb");
+ assertTrue(m.find());
+ // System.out.println("["+m.group()+"]");
+ assertEquals("a", m.group());
+ assertTrue(m.find());
+ assertEquals("b", m.group());
+ assertFalse(m.find());
+
+ m = p.matcher("\naa\r\nbb\rcc\n\n");
+ assertTrue(m.find());
+ // System.out.println("["+m.group()+"]");
+ assertTrue(m.group().equals(""));
+ assertTrue(m.find());
+ // System.out.println("["+m.group()+"]");
+ assertEquals("aa", m.group());
+ assertTrue(m.find());
+ // System.out.println("["+m.group()+"]");
+ assertEquals("bb", m.group());
+ assertTrue(m.find());
+ // System.out.println("["+m.group()+"]");
+ assertEquals("cc", m.group());
+ assertTrue(m.find());
+ // System.out.println("["+m.group()+"]");
+ assertTrue(m.group().equals(""));
+ assertFalse(m.find());
+
+ m = p.matcher("a");
+ assertTrue(m.find());
+ assertEquals("a", m.group());
+ assertFalse(m.find());
+
+ m = p.matcher("");
+ // FIXME: This matches the reference behaviour but is
+ // inconsistent with matching "a" - ie. the end of the
+ // target string should match against $ always but this
+ // appears to work with the null string only when not in
+ // multiline mode (see below)
+ assertFalse(m.find());
+
+ p = Pattern.compile("^.*$");
+ m = p.matcher("");
+ assertTrue(m.find());
+ assertTrue(m.group().equals(""));
+ assertFalse(m.find());
+ }
+
+ public void testCompile4() throws PatternSyntaxException {
+ String findString = "\\Qpublic\\E";
+ StringBuffer text = new StringBuffer(" public class Class {\n"
+ + " public class Class {");
+
+ Pattern pattern = Pattern.compile(findString, 0);
+ Matcher matcher = pattern.matcher(text);
+
+ boolean found = matcher.find();
+ assertTrue(found);
+ assertEquals(4, matcher.start());
+ if (found) {
+ // modify text
+ text.delete(0, text.length());
+ text.append("Text have been changed.");
+ matcher.reset(text);
+ }
+
+ found = matcher.find();
+ assertFalse(found);
+ }
+
+ public void testCompile5() throws PatternSyntaxException {
+ Pattern p = Pattern.compile("^[0-9]");
+ String s[] = p.split("12", -1);
+ assertEquals("", s[0]);
+ assertEquals("2", s[1]);
+ assertEquals(2, s.length);
+ }
+
+ // public void testCompile6() {
+ // String regex = "[\\p{L}[\\p{Mn}[\\p{Pc}[\\p{Nd}[\\p{Nl}[\\p{Sc}]]]]]]+";
+ // String regex = "[\\p{L}\\p{Mn}\\p{Pc}\\p{Nd}\\p{Nl}\\p{Sc}]+";
+ // try {
+ // Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
+ // assertTrue(true);
+ // } catch (PatternSyntaxException e) {
+ // System.out.println(e.getMessage());
+ // assertTrue(false);
+ // }
+ // }
+
+ private static class UBInfo {
+ public UBInfo(int low, int high, String name) {
+ this.name = name;
+ this.low = low;
+ this.high = high;
+ }
+
+ public String name;
+
+ public int low, high;
+ }
+
+ // A table representing the unicode categories
+ // private static UBInfo[] UCategories = {
+ // Lu
+ // Ll
+ // Lt
+ // Lm
+ // Lo
+ // Mn
+ // Mc
+ // Me
+ // Nd
+ // Nl
+ // No
+ // Pc
+ // Pd
+ // Ps
+ // Pe
+ // Pi
+ // Pf
+ // Po
+ // Sm
+ // Sc
+ // Sk
+ // So
+ // Zs
+ // Zl
+ // Zp
+ // Cc
+ // Cf
+ // Cs
+ // Co
+ // Cn
+ // };
+
+ // A table representing the unicode character blocks
+ private static UBInfo[] UBlocks = {
+ /* 0000; 007F; Basic Latin */
+ new UBInfo(0x0000, 0x007F, "BasicLatin"), // Character.UnicodeBlock.BASIC_LATIN
+ /* 0080; 00FF; Latin-1 Supplement */
+ new UBInfo(0x0080, 0x00FF, "Latin-1Supplement"), // Character.UnicodeBlock.LATIN_1_SUPPLEMENT
+ /* 0100; 017F; Latin Extended-A */
+ new UBInfo(0x0100, 0x017F, "LatinExtended-A"), // Character.UnicodeBlock.LATIN_EXTENDED_A
+ /* 0180; 024F; Latin Extended-B */
+ // new UBInfo (0x0180,0x024F,"InLatinExtended-B"), //
+ // Character.UnicodeBlock.LATIN_EXTENDED_B
+ /* 0250; 02AF; IPA Extensions */
+ new UBInfo(0x0250, 0x02AF, "IPAExtensions"), // Character.UnicodeBlock.IPA_EXTENSIONS
+ /* 02B0; 02FF; Spacing Modifier Letters */
+ new UBInfo(0x02B0, 0x02FF, "SpacingModifierLetters"), // Character.UnicodeBlock.SPACING_MODIFIER_LETTERS
+ /* 0300; 036F; Combining Diacritical Marks */
+ new UBInfo(0x0300, 0x036F, "CombiningDiacriticalMarks"), // Character.UnicodeBlock.COMBINING_DIACRITICAL_MARKS
+ /* 0370; 03FF; Greek */
+ new UBInfo(0x0370, 0x03FF, "Greek"), // Character.UnicodeBlock.GREEK
+ /* 0400; 04FF; Cyrillic */
+ new UBInfo(0x0400, 0x04FF, "Cyrillic"), // Character.UnicodeBlock.CYRILLIC
+ /* 0530; 058F; Armenian */
+ new UBInfo(0x0530, 0x058F, "Armenian"), // Character.UnicodeBlock.ARMENIAN
+ /* 0590; 05FF; Hebrew */
+ new UBInfo(0x0590, 0x05FF, "Hebrew"), // Character.UnicodeBlock.HEBREW
+ /* 0600; 06FF; Arabic */
+ new UBInfo(0x0600, 0x06FF, "Arabic"), // Character.UnicodeBlock.ARABIC
+ /* 0700; 074F; Syriac */
+ new UBInfo(0x0700, 0x074F, "Syriac"), // Character.UnicodeBlock.SYRIAC
+ /* 0780; 07BF; Thaana */
+ new UBInfo(0x0780, 0x07BF, "Thaana"), // Character.UnicodeBlock.THAANA
+ /* 0900; 097F; Devanagari */
+ new UBInfo(0x0900, 0x097F, "Devanagari"), // Character.UnicodeBlock.DEVANAGARI
+ /* 0980; 09FF; Bengali */
+ new UBInfo(0x0980, 0x09FF, "Bengali"), // Character.UnicodeBlock.BENGALI
+ /* 0A00; 0A7F; Gurmukhi */
+ new UBInfo(0x0A00, 0x0A7F, "Gurmukhi"), // Character.UnicodeBlock.GURMUKHI
+ /* 0A80; 0AFF; Gujarati */
+ new UBInfo(0x0A80, 0x0AFF, "Gujarati"), // Character.UnicodeBlock.GUJARATI
+ /* 0B00; 0B7F; Oriya */
+ new UBInfo(0x0B00, 0x0B7F, "Oriya"), // Character.UnicodeBlock.ORIYA
+ /* 0B80; 0BFF; Tamil */
+ new UBInfo(0x0B80, 0x0BFF, "Tamil"), // Character.UnicodeBlock.TAMIL
+ /* 0C00; 0C7F; Telugu */
+ new UBInfo(0x0C00, 0x0C7F, "Telugu"), // Character.UnicodeBlock.TELUGU
+ /* 0C80; 0CFF; Kannada */
+ new UBInfo(0x0C80, 0x0CFF, "Kannada"), // Character.UnicodeBlock.KANNADA
+ /* 0D00; 0D7F; Malayalam */
+ new UBInfo(0x0D00, 0x0D7F, "Malayalam"), // Character.UnicodeBlock.MALAYALAM
+ /* 0D80; 0DFF; Sinhala */
+ new UBInfo(0x0D80, 0x0DFF, "Sinhala"), // Character.UnicodeBlock.SINHALA
+ /* 0E00; 0E7F; Thai */
+ new UBInfo(0x0E00, 0x0E7F, "Thai"), // Character.UnicodeBlock.THAI
+ /* 0E80; 0EFF; Lao */
+ new UBInfo(0x0E80, 0x0EFF, "Lao"), // Character.UnicodeBlock.LAO
+ /* 0F00; 0FFF; Tibetan */
+ new UBInfo(0x0F00, 0x0FFF, "Tibetan"), // Character.UnicodeBlock.TIBETAN
+ /* 1000; 109F; Myanmar */
+ new UBInfo(0x1000, 0x109F, "Myanmar"), // Character.UnicodeBlock.MYANMAR
+ /* 10A0; 10FF; Georgian */
+ new UBInfo(0x10A0, 0x10FF, "Georgian"), // Character.UnicodeBlock.GEORGIAN
+ /* 1100; 11FF; Hangul Jamo */
+ new UBInfo(0x1100, 0x11FF, "HangulJamo"), // Character.UnicodeBlock.HANGUL_JAMO
+ /* 1200; 137F; Ethiopic */
+ new UBInfo(0x1200, 0x137F, "Ethiopic"), // Character.UnicodeBlock.ETHIOPIC
+ /* 13A0; 13FF; Cherokee */
+ new UBInfo(0x13A0, 0x13FF, "Cherokee"), // Character.UnicodeBlock.CHEROKEE
+ /* 1400; 167F; Unified Canadian Aboriginal Syllabics */
+ new UBInfo(0x1400, 0x167F, "UnifiedCanadianAboriginalSyllabics"), // Character.UnicodeBlock.UNIFIED_CANADIAN_ABORIGINAL_SYLLABICS
+ /* 1680; 169F; Ogham */
+ new UBInfo(0x1680, 0x169F, "Ogham"), // Character.UnicodeBlock.OGHAM
+ /* 16A0; 16FF; Runic */
+ new UBInfo(0x16A0, 0x16FF, "Runic"), // Character.UnicodeBlock.RUNIC
+ /* 1780; 17FF; Khmer */
+ new UBInfo(0x1780, 0x17FF, "Khmer"), // Character.UnicodeBlock.KHMER
+ /* 1800; 18AF; Mongolian */
+ new UBInfo(0x1800, 0x18AF, "Mongolian"), // Character.UnicodeBlock.MONGOLIAN
+ /* 1E00; 1EFF; Latin Extended Additional */
+ new UBInfo(0x1E00, 0x1EFF, "LatinExtendedAdditional"), // Character.UnicodeBlock.LATIN_EXTENDED_ADDITIONAL
+ /* 1F00; 1FFF; Greek Extended */
+ new UBInfo(0x1F00, 0x1FFF, "GreekExtended"), // Character.UnicodeBlock.GREEK_EXTENDED
+ /* 2000; 206F; General Punctuation */
+ new UBInfo(0x2000, 0x206F, "GeneralPunctuation"), // Character.UnicodeBlock.GENERAL_PUNCTUATION
+ /* 2070; 209F; Superscripts and Subscripts */
+ new UBInfo(0x2070, 0x209F, "SuperscriptsandSubscripts"), // Character.UnicodeBlock.SUPERSCRIPTS_AND_SUBSCRIPTS
+ /* 20A0; 20CF; Currency Symbols */
+ new UBInfo(0x20A0, 0x20CF, "CurrencySymbols"), // Character.UnicodeBlock.CURRENCY_SYMBOLS
+ /* 20D0; 20FF; Combining Marks for Symbols */
+ new UBInfo(0x20D0, 0x20FF, "CombiningMarksforSymbols"), // Character.UnicodeBlock.COMBINING_MARKS_FOR_SYMBOLS
+ /* 2100; 214F; Letterlike Symbols */
+ new UBInfo(0x2100, 0x214F, "LetterlikeSymbols"), // Character.UnicodeBlock.LETTERLIKE_SYMBOLS
+ /* 2150; 218F; Number Forms */
+ new UBInfo(0x2150, 0x218F, "NumberForms"), // Character.UnicodeBlock.NUMBER_FORMS
+ /* 2190; 21FF; Arrows */
+ new UBInfo(0x2190, 0x21FF, "Arrows"), // Character.UnicodeBlock.ARROWS
+ /* 2200; 22FF; Mathematical Operators */
+ new UBInfo(0x2200, 0x22FF, "MathematicalOperators"), // Character.UnicodeBlock.MATHEMATICAL_OPERATORS
+ /* 2300; 23FF; Miscellaneous Technical */
+ new UBInfo(0x2300, 0x23FF, "MiscellaneousTechnical"), // Character.UnicodeBlock.MISCELLANEOUS_TECHNICAL
+ /* 2400; 243F; Control Pictures */
+ new UBInfo(0x2400, 0x243F, "ControlPictures"), // Character.UnicodeBlock.CONTROL_PICTURES
+ /* 2440; 245F; Optical Character Recognition */
+ new UBInfo(0x2440, 0x245F, "OpticalCharacterRecognition"), // Character.UnicodeBlock.OPTICAL_CHARACTER_RECOGNITION
+ /* 2460; 24FF; Enclosed Alphanumerics */
+ new UBInfo(0x2460, 0x24FF, "EnclosedAlphanumerics"), // Character.UnicodeBlock.ENCLOSED_ALPHANUMERICS
+ /* 2500; 257F; Box Drawing */
+ new UBInfo(0x2500, 0x257F, "BoxDrawing"), // Character.UnicodeBlock.BOX_DRAWING
+ /* 2580; 259F; Block Elements */
+ new UBInfo(0x2580, 0x259F, "BlockElements"), // Character.UnicodeBlock.BLOCK_ELEMENTS
+ /* 25A0; 25FF; Geometric Shapes */
+ new UBInfo(0x25A0, 0x25FF, "GeometricShapes"), // Character.UnicodeBlock.GEOMETRIC_SHAPES
+ /* 2600; 26FF; Miscellaneous Symbols */
+ new UBInfo(0x2600, 0x26FF, "MiscellaneousSymbols"), // Character.UnicodeBlock.MISCELLANEOUS_SYMBOLS
+ /* 2700; 27BF; Dingbats */
+ new UBInfo(0x2700, 0x27BF, "Dingbats"), // Character.UnicodeBlock.DINGBATS
+ /* 2800; 28FF; Braille Patterns */
+ new UBInfo(0x2800, 0x28FF, "BraillePatterns"), // Character.UnicodeBlock.BRAILLE_PATTERNS
+ /* 2E80; 2EFF; CJK Radicals Supplement */
+ new UBInfo(0x2E80, 0x2EFF, "CJKRadicalsSupplement"), // Character.UnicodeBlock.CJK_RADICALS_SUPPLEMENT
+ /* 2F00; 2FDF; Kangxi Radicals */
+ new UBInfo(0x2F00, 0x2FDF, "KangxiRadicals"), // Character.UnicodeBlock.KANGXI_RADICALS
+ /* 2FF0; 2FFF; Ideographic Description Characters */
+ new UBInfo(0x2FF0, 0x2FFF, "IdeographicDescriptionCharacters"), // Character.UnicodeBlock.IDEOGRAPHIC_DESCRIPTION_CHARACTERS
+ /* 3000; 303F; CJK Symbols and Punctuation */
+ new UBInfo(0x3000, 0x303F, "CJKSymbolsandPunctuation"), // Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
+ /* 3040; 309F; Hiragana */
+ new UBInfo(0x3040, 0x309F, "Hiragana"), // Character.UnicodeBlock.HIRAGANA
+ /* 30A0; 30FF; Katakana */
+ new UBInfo(0x30A0, 0x30FF, "Katakana"), // Character.UnicodeBlock.KATAKANA
+ /* 3100; 312F; Bopomofo */
+ new UBInfo(0x3100, 0x312F, "Bopomofo"), // Character.UnicodeBlock.BOPOMOFO
+ /* 3130; 318F; Hangul Compatibility Jamo */
+ new UBInfo(0x3130, 0x318F, "HangulCompatibilityJamo"), // Character.UnicodeBlock.HANGUL_COMPATIBILITY_JAMO
+ /* 3190; 319F; Kanbun */
+ new UBInfo(0x3190, 0x319F, "Kanbun"), // Character.UnicodeBlock.KANBUN
+ /* 31A0; 31BF; Bopomofo Extended */
+ new UBInfo(0x31A0, 0x31BF, "BopomofoExtended"), // Character.UnicodeBlock.BOPOMOFO_EXTENDED
+ /* 3200; 32FF; Enclosed CJK Letters and Months */
+ new UBInfo(0x3200, 0x32FF, "EnclosedCJKLettersandMonths"), // Character.UnicodeBlock.ENCLOSED_CJK_LETTERS_AND_MONTHS
+ /* 3300; 33FF; CJK Compatibility */
+ new UBInfo(0x3300, 0x33FF, "CJKCompatibility"), // Character.UnicodeBlock.CJK_COMPATIBILITY
+ /* 3400; 4DB5; CJK Unified Ideographs Extension A */
+ new UBInfo(0x3400, 0x4DB5, "CJKUnifiedIdeographsExtensionA"), // Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
+ /* 4E00; 9FFF; CJK Unified Ideographs */
+ new UBInfo(0x4E00, 0x9FFF, "CJKUnifiedIdeographs"), // Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
+ /* A000; A48F; Yi Syllables */
+ new UBInfo(0xA000, 0xA48F, "YiSyllables"), // Character.UnicodeBlock.YI_SYLLABLES
+ /* A490; A4CF; Yi Radicals */
+ new UBInfo(0xA490, 0xA4CF, "YiRadicals"), // Character.UnicodeBlock.YI_RADICALS
+ /* AC00; D7A3; Hangul Syllables */
+ new UBInfo(0xAC00, 0xD7A3, "HangulSyllables"), // Character.UnicodeBlock.HANGUL_SYLLABLES
+ /* D800; DB7F; High Surrogates */
+ /* DB80; DBFF; High Private Use Surrogates */
+ /* DC00; DFFF; Low Surrogates */
+ /* E000; F8FF; Private Use */
+ /* F900; FAFF; CJK Compatibility Ideographs */
+ new UBInfo(0xF900, 0xFAFF, "CJKCompatibilityIdeographs"), // Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
+ /* FB00; FB4F; Alphabetic Presentation Forms */
+ new UBInfo(0xFB00, 0xFB4F, "AlphabeticPresentationForms"), // Character.UnicodeBlock.ALPHABETIC_PRESENTATION_FORMS
+ /* FB50; FDFF; Arabic Presentation Forms-A */
+ new UBInfo(0xFB50, 0xFDFF, "ArabicPresentationForms-A"), // Character.UnicodeBlock.ARABIC_PRESENTATION_FORMS_A
+ /* FE20; FE2F; Combining Half Marks */
+ new UBInfo(0xFE20, 0xFE2F, "CombiningHalfMarks"), // Character.UnicodeBlock.COMBINING_HALF_MARKS
+ /* FE30; FE4F; CJK Compatibility Forms */
+ new UBInfo(0xFE30, 0xFE4F, "CJKCompatibilityForms"), // Character.UnicodeBlock.CJK_COMPATIBILITY_FORMS
+ /* FE50; FE6F; Small Form Variants */
+ new UBInfo(0xFE50, 0xFE6F, "SmallFormVariants"), // Character.UnicodeBlock.SMALL_FORM_VARIANTS
+ /* FE70; FEFE; Arabic Presentation Forms-B */
+ // new UBInfo (0xFE70,0xFEFE,"InArabicPresentationForms-B"), //
+ // Character.UnicodeBlock.ARABIC_PRESENTATION_FORMS_B
+ /* FEFF; FEFF; Specials */
+ new UBInfo(0xFEFF, 0xFEFF, "Specials"), // Character.UnicodeBlock.SPECIALS
+ /* FF00; FFEF; Halfwidth and Fullwidth Forms */
+ new UBInfo(0xFF00, 0xFFEF, "HalfwidthandFullwidthForms"), // Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
+ /* FFF0; FFFD; Specials */
+ new UBInfo(0xFFF0, 0xFFFD, "Specials") // Character.UnicodeBlock.SPECIALS
+ };
+} \ No newline at end of file
diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/PatternErrorTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/PatternErrorTest.java
new file mode 100644
index 0000000..a8eef6d
--- /dev/null
+++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/PatternErrorTest.java
@@ -0,0 +1,65 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.harmony.tests.java.util.regex;
+
+import java.util.regex.Pattern;
+
+import junit.framework.TestCase;
+
+/**
+ * Test boundary and error conditions in java.util.regex.Pattern
+ */
+@SuppressWarnings("nls")
+public class PatternErrorTest extends TestCase {
+ public void testCompileErrors() throws Exception {
+ // null regex string - should get NullPointerException
+ try {
+ Pattern.compile(null);
+ fail("NullPointerException expected");
+ } catch (NullPointerException e) {
+ }
+
+ // empty regex string - no exception should be thrown
+ Pattern.compile("");
+
+ // note: invalid regex syntax checked in PatternSyntaxExceptionTest
+
+ // flags = 0 should raise no exception
+ int flags = 0;
+ Pattern.compile("foo", flags);
+
+ // check that all valid flags accepted without exception
+ flags |= Pattern.UNIX_LINES;
+ flags |= Pattern.CASE_INSENSITIVE;
+ flags |= Pattern.MULTILINE;
+ flags |= Pattern.CANON_EQ;
+ flags |= Pattern.COMMENTS;
+ flags |= Pattern.DOTALL;
+ flags |= Pattern.UNICODE_CASE;
+ Pattern.compile("foo", flags);
+
+ // add invalid flags - should get IllegalArgumentException
+ // regression test for HARMONY-4248
+ flags |= 0xFFFFFFFF;
+ try {
+ Pattern.compile("foo", flags);
+ fail("Expected IllegalArgumentException to be thrown");
+ } catch (IllegalArgumentException e) {
+ // This is the expected exception
+ }
+ }
+}
diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/PatternSyntaxExceptionTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/PatternSyntaxExceptionTest.java
new file mode 100644
index 0000000..81b12be
--- /dev/null
+++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/PatternSyntaxExceptionTest.java
@@ -0,0 +1,113 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.harmony.tests.java.util.regex;
+
+import java.io.ObjectStreamClass;
+import java.io.Serializable;
+import java.util.regex.Pattern;
+import java.util.regex.PatternSyntaxException;
+
+import junit.framework.TestCase;
+
+import org.apache.harmony.testframework.serialization.SerializationTest;
+import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
+
+/**
+ * TODO Type description
+ */
+@SuppressWarnings("nls")
+public class PatternSyntaxExceptionTest extends TestCase {
+ public void testCase() {
+ String regex = "(";
+ try {
+ Pattern.compile(regex);
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException e) {
+ // TOFIX: Commented out assertEquals tests...
+ // TOFIX: should we match exception strings?
+ // assertEquals("Unclosed group", e.getDescription());
+ assertEquals(1, e.getIndex());
+ // assertEquals("Unclosed group near index 1\n(\n ^",
+ // e.getMessage());
+ assertEquals(regex, e.getPattern());
+ }
+ }
+
+ public void testCase2() {
+ String regex = "[4-";
+ try {
+ Pattern.compile(regex);
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException e) {
+ // TOFIX: Commented out assertEquals tests...
+ // TOFIX: should we match exception strings?
+ // assertEquals("Illegal character range", e.getDescription());
+ assertEquals(3, e.getIndex());
+ // assertEquals("Illegal character range near index 3\n[4-\n ^",
+ // e.getMessage());
+ assertEquals(regex, e.getPattern());
+ }
+ }
+
+ /**
+ * @tests serialization/deserialization compatibility.
+ */
+ public void testSerializationSelf() throws Exception {
+ PatternSyntaxException object = new PatternSyntaxException("TESTDESC",
+ "TESTREGEX", 3);
+ SerializationTest.verifySelf(object, PATTERNSYNTAXEXCEPTION_COMPARATOR);
+ }
+
+ /**
+ * @tests serialization/deserialization compatibility with RI.
+ */
+ public void testSerializationCompatibility() throws Exception {
+ PatternSyntaxException object = new PatternSyntaxException("TESTDESC",
+ "TESTREGEX", 3);
+ SerializationTest.verifyGolden(this, object,
+ PATTERNSYNTAXEXCEPTION_COMPARATOR);
+ }
+
+ // Regression test for HARMONY-3787
+ public void test_objectStreamField() {
+ ObjectStreamClass objectStreamClass = ObjectStreamClass
+ .lookup(PatternSyntaxException.class);
+ assertNotNull(objectStreamClass.getField("desc"));
+ }
+
+ // comparator for BatchUpdateException field updateCounts
+ private static final SerializableAssert PATTERNSYNTAXEXCEPTION_COMPARATOR = new SerializableAssert() {
+ public void assertDeserialized(Serializable initial,
+ Serializable deserialized) {
+
+ // do common checks for all throwable objects
+ SerializationTest.THROWABLE_COMPARATOR.assertDeserialized(initial,
+ deserialized);
+
+ PatternSyntaxException initPatternSyntaxException = (PatternSyntaxException) initial;
+ PatternSyntaxException dserPatternSyntaxException = (PatternSyntaxException) deserialized;
+
+ // verify fields
+ assertEquals(initPatternSyntaxException.getDescription(),
+ dserPatternSyntaxException.getDescription());
+ assertEquals(initPatternSyntaxException.getPattern(),
+ dserPatternSyntaxException.getPattern());
+ assertEquals(initPatternSyntaxException.getIndex(),
+ dserPatternSyntaxException.getIndex());
+ }
+ };
+}
diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/PatternTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/PatternTest.java
new file mode 100644
index 0000000..33da926
--- /dev/null
+++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/PatternTest.java
@@ -0,0 +1,1624 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.harmony.tests.java.util.regex;
+
+import java.io.Serializable;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.regex.PatternSyntaxException;
+
+import junit.framework.TestCase;
+
+import org.apache.harmony.testframework.serialization.SerializationTest;
+import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
+
+@SuppressWarnings("nls")
+public class PatternTest extends TestCase {
+ String[] testPatterns = {
+ "(a|b)*abb",
+ "(1*2*3*4*)*567",
+ "(a|b|c|d)*aab",
+ "(1|2|3|4|5|6|7|8|9|0)(1|2|3|4|5|6|7|8|9|0)*",
+ "(abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ)*",
+ "(a|b)*(a|b)*A(a|b)*lice.*",
+ "(a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z)(a|b|c|d|e|f|g|h|"
+ + "i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z)*(1|2|3|4|5|6|7|8|9|0)*|while|for|struct|if|do",
+ "x(?c)y", "x(?cc)y", "x(?:c)y"
+
+ };
+
+ public PatternTest(String name) {
+ super(name);
+ }
+
+ public void testCommentsInPattern() {
+ Pattern p = Pattern.compile("ab# this is a comment\ncd", Pattern.COMMENTS);
+ assertTrue(p.matcher("abcd").matches());
+ }
+
+ /*
+ * Class under test for String[] split(CharSequence, int)
+ */
+ public void testSplitCharSequenceint() {
+ // splitting CharSequence which ends with pattern
+ // bug6193
+ assertEquals(",,".split(",", 3).length, 3);
+ assertEquals(",,".split(",", 4).length, 3);
+ // bug6193
+ // bug5391
+ assertEquals(Pattern.compile("o").split("boo:and:foo", 5).length, 5);
+ assertEquals(Pattern.compile("b").split("ab", -1).length, 2);
+ // bug5391
+ String s[];
+ Pattern pat = Pattern.compile("x");
+ s = pat.split("zxx:zzz:zxx", 10);
+ assertEquals(s.length, 5);
+ s = pat.split("zxx:zzz:zxx", 3);
+ assertEquals(s.length, 3);
+ s = pat.split("zxx:zzz:zxx", -1);
+ assertEquals(s.length, 5);
+ s = pat.split("zxx:zzz:zxx", 0);
+ assertEquals(s.length, 3);
+ // other splitting
+ // negative limit
+ pat = Pattern.compile("b");
+ s = pat.split("abccbadfebb", -1);
+ assertEquals(s.length, 5);
+ s = pat.split("", -1);
+ assertEquals(s.length, 1);
+ pat = Pattern.compile("");
+ s = pat.split("", -1);
+ assertEquals(s.length, 1);
+ s = pat.split("abccbadfe", -1);
+ assertEquals(s.length, 11);
+ // zero limit
+ pat = Pattern.compile("b");
+ s = pat.split("abccbadfebb", 0);
+ assertEquals(s.length, 3);
+ s = pat.split("", 0);
+ assertEquals(s.length, 1);
+ pat = Pattern.compile("");
+ s = pat.split("", 0);
+ assertEquals(s.length, 1);
+ s = pat.split("abccbadfe", 0);
+ assertEquals(s.length, 10);
+ // positive limit
+ pat = Pattern.compile("b");
+ s = pat.split("abccbadfebb", 12);
+ assertEquals(s.length, 5);
+ s = pat.split("", 6);
+ assertEquals(s.length, 1);
+ pat = Pattern.compile("");
+ s = pat.split("", 11);
+ assertEquals(s.length, 1);
+ s = pat.split("abccbadfe", 15);
+ assertEquals(s.length, 11);
+
+ pat = Pattern.compile("b");
+ s = pat.split("abccbadfebb", 5);
+ assertEquals(s.length, 5);
+ s = pat.split("", 1);
+ assertEquals(s.length, 1);
+ pat = Pattern.compile("");
+ s = pat.split("", 1);
+ assertEquals(s.length, 1);
+ s = pat.split("abccbadfe", 11);
+ assertEquals(s.length, 11);
+
+ pat = Pattern.compile("b");
+ s = pat.split("abccbadfebb", 3);
+ assertEquals(s.length, 3);
+ pat = Pattern.compile("");
+ s = pat.split("abccbadfe", 5);
+ assertEquals(s.length, 5);
+ }
+
+ /*
+ * Class under test for String[] split(CharSequence)
+ */
+ public void testSplitCharSequence() {
+ String s[];
+ Pattern pat = Pattern.compile("b");
+ s = pat.split("abccbadfebb");
+ assertEquals(s.length, 3);
+ s = pat.split("");
+ assertEquals(s.length, 1);
+ pat = Pattern.compile("");
+ s = pat.split("");
+ assertEquals(s.length, 1);
+ s = pat.split("abccbadfe");
+ assertEquals(s.length, 10);
+ // bug6544
+ String s1 = "";
+ String[] arr = s1.split(":");
+ assertEquals(arr.length, 1);
+ // bug6544
+ }
+
+ public void testPattern() {
+ }
+
+ public void testFlags() {
+ String baseString;
+ String testString;
+ Pattern pat;
+ Matcher mat;
+
+ baseString = "((?i)|b)a";
+ testString = "A";
+ pat = Pattern.compile(baseString);
+ mat = pat.matcher(testString);
+ assertFalse(mat.matches());
+
+ baseString = "(?i)a|b";
+ testString = "A";
+ pat = Pattern.compile(baseString);
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ baseString = "(?i)a|b";
+ testString = "B";
+ pat = Pattern.compile(baseString);
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ baseString = "c|(?i)a|b";
+ testString = "B";
+ pat = Pattern.compile(baseString);
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ baseString = "(?i)a|(?s)b";
+ testString = "B";
+ pat = Pattern.compile(baseString);
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ baseString = "(?i)a|(?-i)b";
+ testString = "B";
+ pat = Pattern.compile(baseString);
+ mat = pat.matcher(testString);
+ assertFalse(mat.matches());
+
+ baseString = "(?i)a|(?-i)c|b";
+ testString = "B";
+ pat = Pattern.compile(baseString);
+ mat = pat.matcher(testString);
+ assertFalse(mat.matches());
+
+ baseString = "(?i)a|(?-i)c|(?i)b";
+ testString = "B";
+ pat = Pattern.compile(baseString);
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ baseString = "(?i)a|(?-i)b";
+ testString = "A";
+ pat = Pattern.compile(baseString);
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ baseString = "((?i))a";
+ testString = "A";
+ pat = Pattern.compile(baseString);
+ mat = pat.matcher(testString);
+ assertFalse(mat.matches());
+
+ baseString = "|(?i)|a";
+ testString = "A";
+ pat = Pattern.compile(baseString);
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ baseString = "(?i)((?s)a.)";
+ testString = "A\n";
+ pat = Pattern.compile(baseString);
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ baseString = "(?i)((?-i)a)";
+ testString = "A";
+ pat = Pattern.compile(baseString);
+ mat = pat.matcher(testString);
+ assertFalse(mat.matches());
+
+ baseString = "(?i)(?s:a.)";
+ testString = "A\n";
+ pat = Pattern.compile(baseString);
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ baseString = "(?i)fgh(?s:aa)";
+ testString = "fghAA";
+ pat = Pattern.compile(baseString);
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ baseString = "(?i)((?-i))a";
+ testString = "A";
+ pat = Pattern.compile(baseString);
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ baseString = "abc(?i)d";
+ testString = "ABCD";
+ pat = Pattern.compile(baseString);
+ mat = pat.matcher(testString);
+ assertFalse(mat.matches());
+
+ testString = "abcD";
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ baseString = "a(?i)a(?-i)a(?i)a(?-i)a";
+ testString = "aAaAa";
+ pat = Pattern.compile(baseString);
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ testString = "aAAAa";
+ mat = pat.matcher(testString);
+ assertFalse(mat.matches());
+ }
+
+ public void testFlagsMethod() {
+ String baseString;
+ Pattern pat;
+
+ /*
+ * These tests are for compatibility with RI only. Logically we have to
+ * return only flags specified during the compilation. For example
+ * pat.flags() == 0 when we compile Pattern pat =
+ * Pattern.compile("(?i)abc(?-i)"); but the whole expression is compiled
+ * in a case insensitive manner. So there is little sense to do calls to
+ * flags() now.
+ */
+ baseString = "(?-i)";
+ pat = Pattern.compile(baseString);
+
+ baseString = "(?idmsux)abc(?-i)vg(?-dmu)";
+ pat = Pattern.compile(baseString);
+ assertEquals(pat.flags(), Pattern.DOTALL | Pattern.COMMENTS);
+
+ baseString = "(?idmsux)abc|(?-i)vg|(?-dmu)";
+ pat = Pattern.compile(baseString);
+ assertEquals(pat.flags(), Pattern.DOTALL | Pattern.COMMENTS);
+
+ baseString = "(?is)a((?x)b.)";
+ pat = Pattern.compile(baseString);
+ assertEquals(pat.flags(), Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
+
+ baseString = "(?i)a((?-i))";
+ pat = Pattern.compile(baseString);
+ assertEquals(pat.flags(), Pattern.CASE_INSENSITIVE);
+
+ baseString = "((?i)a)";
+ pat = Pattern.compile(baseString);
+ assertEquals(pat.flags(), 0);
+
+ pat = Pattern.compile("(?is)abc");
+ assertEquals(pat.flags(), Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
+ }
+
+ /*
+ * Class under test for Pattern compile(String, int)
+ */
+ public void testCompileStringint() {
+ /*
+ * this tests are needed to verify that appropriate exceptions are
+ * thrown
+ */
+ String pattern = "b)a";
+ try {
+ Pattern.compile(pattern);
+ fail("Expected a PatternSyntaxException when compiling pattern: "
+ + pattern);
+ } catch (PatternSyntaxException e) {
+ // pass
+ }
+ pattern = "bcde)a";
+ try {
+ Pattern.compile(pattern);
+ fail("Expected a PatternSyntaxException when compiling pattern: "
+ + pattern);
+ } catch (PatternSyntaxException e) {
+ // pass
+ }
+ pattern = "bbg())a";
+ try {
+ Pattern.compile(pattern);
+ fail("Expected a PatternSyntaxException when compiling pattern: "
+ + pattern);
+ } catch (PatternSyntaxException e) {
+ // pass
+ }
+
+ pattern = "cdb(?i))a";
+ try {
+ Pattern.compile(pattern);
+ fail("Expected a PatternSyntaxException when compiling pattern: "
+ + pattern);
+ } catch (PatternSyntaxException e) {
+ // pass
+ }
+
+ /*
+ * This pattern should compile - HARMONY-2127
+ */
+ pattern = "x(?c)y";
+ Pattern.compile(pattern);
+
+ /*
+ * this pattern doesn't match any string, but should be compiled anyway
+ */
+ pattern = "(b\\1)a";
+ Pattern.compile(pattern);
+ }
+
+ /*
+ * Class under test for Pattern compile(String)
+ */
+ public void testQuantCompileNeg() {
+ String[] patterns = { "5{,2}", "{5asd", "{hgdhg", "{5,hjkh", "{,5hdsh",
+ "{5,3shdfkjh}" };
+ for (String element : patterns) {
+ try {
+ Pattern.compile(element);
+ fail("PatternSyntaxException was expected, but compilation succeeds");
+ } catch (PatternSyntaxException pse) {
+ continue;
+ }
+ }
+ // Regression for HARMONY-1365
+ String pattern = "(?![^\\<C\\f\\0146\\0270\\}&&[|\\02-\\x3E\\}|X-\\|]]{7,}+)[|\\\\\\x98\\<\\?\\u4FCFr\\,\\0025\\}\\004|\\0025-\\052\061]|(?<![|\\01-\\u829E])|(?<!\\p{Alpha})|^|(?-s:[^\\x15\\\\\\x24F\\a\\,\\a\\u97D8[\\x38\\a[\\0224-\\0306[^\\0020-\\u6A57]]]]??)(?uxix:[^|\\{\\[\\0367\\t\\e\\x8C\\{\\[\\074c\\]V[|b\\fu\\r\\0175\\<\\07f\\066s[^D-\\x5D]]])(?xx:^{5,}+)(?uuu)(?=^\\D)|(?!\\G)(?>\\G*?)(?![^|\\]\\070\\ne\\{\\t\\[\\053\\?\\\\\\x51\\a\\075\\0023-\\[&&[|\\022-\\xEA\\00-\\u41C2&&[^|a-\\xCC&&[^\\037\\uECB3\\u3D9A\\x31\\|\\<b\\0206\\uF2EC\\01m\\,\\ak\\a\\03&&\\p{Punct}]]]])(?-dxs:[|\\06-\\07|\\e-\\x63&&[|Tp\\u18A3\\00\\|\\xE4\\05\\061\\015\\0116C|\\r\\{\\}\\006\\xEA\\0367\\xC4\\01\\0042\\0267\\xBB\\01T\\}\\0100\\?[|\\[-\\u459B|\\x23\\x91\\rF\\0376[|\\?-\\x94\\0113-\\\\\\s]]]]{6}?)(?<=[^\\t-\\x42H\\04\\f\\03\\0172\\?i\\u97B6\\e\\f\\uDAC2])(?=\\B*+)(?>[^\\016\\r\\{\\,\\uA29D\\034\\02[\\02-\\[|\\t\\056\\uF599\\x62\\e\\<\\032\\uF0AC\\0026\\0205Q\\|\\\\\\06\\0164[|\\057-\\u7A98&&[\\061-g|\\|\\0276\\n\\042\\011\\e\\xE8\\x64B\\04\\u6D0EDW^\\p{Lower}]]]]?)(?<=[^\\n\\\\\\t\\u8E13\\,\\0114\\u656E\\xA5\\]&&[\\03-\\026|\\uF39D\\01\\{i\\u3BC2\\u14FE]])(?<=[^|\\uAE62\\054H\\|\\}&&^\\p{Space}])(?sxx)(?<=[\\f\\006\\a\\r\\xB4]*+)|(?x-xd:^{5}+)()";
+ assertNotNull(Pattern.compile(pattern));
+ }
+
+ public void testQuantCompilePos() {
+ String[] patterns = {/* "(abc){1,3}", */"abc{2,}", "abc{5}" };
+ for (String element : patterns) {
+ Pattern.compile(element);
+ }
+ }
+
+ public void testQuantComposition() {
+ String pattern = "(a{1,3})aab";
+ java.util.regex.Pattern pat = java.util.regex.Pattern.compile(pattern);
+ java.util.regex.Matcher mat = pat.matcher("aaab");
+ mat.matches();
+ mat.start(1);
+ mat.group(1);
+ }
+
+ public void testMatches() {
+ String[][] posSeq = {
+ { "abb", "ababb", "abababbababb", "abababbababbabababbbbbabb" },
+ { "213567", "12324567", "1234567", "213213567",
+ "21312312312567", "444444567" },
+ { "abcdaab", "aab", "abaab", "cdaab", "acbdadcbaab" },
+ { "213234567", "3458", "0987654", "7689546432", "0398576",
+ "98432", "5" },
+ {
+ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
+ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" },
+ { "ababbaAabababblice", "ababbaAliceababab", "ababbAabliceaaa",
+ "abbbAbbbliceaaa", "Alice" },
+ { "a123", "bnxnvgds156", "for", "while", "if", "struct" },
+ { "xy" }, { "xy" }, { "xcy" }
+
+ };
+
+ for (int i = 0; i < testPatterns.length; i++) {
+ for (int j = 0; j < posSeq[i].length; j++) {
+ assertTrue("Incorrect match: " + testPatterns[i] + " vs "
+ + posSeq[i][j], Pattern.matches(testPatterns[i],
+ posSeq[i][j]));
+ }
+ }
+ }
+
+ public void testTimeZoneIssue() {
+ Pattern p = Pattern.compile("GMT(\\+|\\-)(\\d+)(:(\\d+))?");
+ Matcher m = p.matcher("GMT-9:45");
+ assertTrue(m.matches());
+ assertEquals("-", m.group(1));
+ assertEquals("9", m.group(2));
+ assertEquals(":45", m.group(3));
+ assertEquals("45", m.group(4));
+ }
+
+ public void testCompileRanges() {
+ String[] correctTestPatterns = { "[^]*abb]*", "[^a-d[^m-p]]*abb",
+ "[a-d\\d]*abb", "[abc]*abb", "[a-e&&[de]]*abb", "[^abc]*abb",
+ "[a-e&&[^de]]*abb", "[a-z&&[^m-p]]*abb", "[a-d[m-p]]*abb",
+ "[a-zA-Z]*abb", "[+*?]*abb", "[^+*?]*abb" };
+
+ String[] inputSecuence = { "kkkk", "admpabb", "abcabcd124654abb",
+ "abcabccbacababb", "dededededededeedabb", "gfdhfghgdfghabb",
+ "accabacbcbaabb", "acbvfgtyabb", "adbcacdbmopabcoabb",
+ "jhfkjhaSDFGHJkdfhHNJMjkhfabb", "+*??+*abb", "sdfghjkabb" };
+
+ for (int i = 0; i < correctTestPatterns.length; i++) {
+ assertTrue("pattern: " + correctTestPatterns[i] + " input: "
+ + inputSecuence[i], Pattern.matches(correctTestPatterns[i],
+ inputSecuence[i]));
+
+ }
+
+ String[] wrongInputSecuence = { "]", "admpkk", "abcabcd124k654abb",
+ "abwcabccbacababb", "abababdeababdeabb", "abcabcacbacbabb",
+ "acdcbecbaabb", "acbotyabb", "adbcaecdbmopabcoabb",
+ "jhfkjhaSDFGHJk;dfhHNJMjkhfabb", "+*?a?+*abb", "sdf+ghjkabb" };
+
+ for (int i = 0; i < correctTestPatterns.length; i++) {
+ assertFalse("pattern: " + correctTestPatterns[i] + " input: "
+ + wrongInputSecuence[i], Pattern.matches(
+ correctTestPatterns[i], wrongInputSecuence[i]));
+
+ }
+ }
+
+ public void testRangesSpecialCases() {
+ String neg_patterns[] = { "[a-&&[b-c]]", "[a-\\w]", "[b-a]", "[]" };
+
+ for (String element : neg_patterns) {
+ try {
+ Pattern.compile(element);
+ fail("PatternSyntaxException was expected: " + element);
+ } catch (PatternSyntaxException pse) {
+ }
+ }
+
+ String pos_patterns[] = { "[-]+", "----", "[a-]+", "a-a-a-a-aa--",
+ "[\\w-a]+", "123-2312--aaa-213", "[a-]]+", "-]]]]]]]]]]]]]]]" };
+
+ for (int i = 0; i < pos_patterns.length; i++) {
+ String pat = pos_patterns[i++];
+ String inp = pos_patterns[i];
+ assertTrue("pattern: " + pat + " input: " + inp, Pattern.matches(
+ pat, inp));
+ }
+ }
+
+ public void testZeroSymbols() {
+ assertTrue(Pattern.matches("[\0]*abb", "\0\0\0\0\0\0abb"));
+ }
+
+ public void testEscapes() {
+ Pattern pat = Pattern.compile("\\Q{]()*?");
+ Matcher mat = pat.matcher("{]()*?");
+
+ assertTrue(mat.matches());
+ }
+
+ public void testRegressions() {
+ // Bug 181
+ Pattern.compile("[\\t-\\r]");
+
+ // HARMONY-4472
+ Pattern.compile("a*.+");
+
+ // Bug187
+ Pattern
+ .compile("|(?idmsux-idmsux)|(?idmsux-idmsux)|[^|\\[-\\0274|\\,-\\\\[^|W\\}\\nq\\x65\\002\\xFE\\05\\06\\00\\x66\\x47i\\,\\xF2\\=\\06\\u0EA4\\x9B\\x3C\\f\\|\\{\\xE5\\05\\r\\u944A\\xCA\\e|\\x19\\04\\x07\\04\\u607B\\023\\0073\\x91Tr\\0150\\x83]]?(?idmsux-idmsux:\\p{Alpha}{7}?)||(?<=[^\\uEC47\\01\\02\\u3421\\a\\f\\a\\013q\\035w\\e])(?<=\\p{Punct}{0,}?)(?=^\\p{Lower})(?!\\b{8,14})(?<![|\\00-\\0146[^|\\04\\01\\04\\060\\f\\u224DO\\x1A\\xC4\\00\\02\\0315\\0351\\u84A8\\xCBt\\xCC\\06|\\0141\\00\\=\\e\\f\\x6B\\0026Tb\\040\\x76xJ&&[\\\\-\\]\\05\\07\\02\\u2DAF\\t\\x9C\\e\\0023\\02\\,X\\e|\\u6058flY\\u954C]]]{5}?)(?<=\\p{Sc}{8}+)[^|\\026-\\u89BA|o\\u6277\\t\\07\\x50&&\\p{Punct}]{8,14}+((?<=^\\p{Punct})|(?idmsux-idmsux)||(?>[\\x3E-\\]])|(?idmsux-idmsux:\\p{Punct})|(?<![\\0111\\0371\\xDF\\u6A49\\07\\u2A4D\\00\\0212\\02Xd-\\xED[^\\a-\\0061|\\0257\\04\\f\\[\\0266\\043\\03\\x2D\\042&&[^\\f-\\]&&\\s]]])|(?>[|\\n\\042\\uB09F\\06\\u0F2B\\uC96D\\x89\\uC166\\xAA|\\04-\\][^|\\a\\|\\rx\\04\\uA770\\n\\02\\t\\052\\056\\0274\\|\\=\\07\\e|\\00-\\x1D&&[^\\005\\uB15B\\uCDAC\\n\\x74\\0103\\0147\\uD91B\\n\\062G\\u9B4B\\077\\}\\0324&&[^\\0302\\,\\0221\\04\\u6D16\\04xy\\uD193\\[\\061\\06\\045\\x0F|\\e\\xBB\\f\\u1B52\\023\\u3AD2\\033\\007\\022\\}\\x66\\uA63FJ-\\0304]]]]{0,0})||(?<![^|\\0154U\\u0877\\03\\fy\\n\\|\\0147\\07-\\=[|q\\u69BE\\0243\\rp\\053\\02\\x33I\\u5E39\\u9C40\\052-\\xBC[|\\0064-\\?|\\uFC0C\\x30\\0060\\x45\\\\\\02\\?p\\xD8\\0155\\07\\0367\\04\\uF07B\\000J[^|\\0051-\\{|\\u9E4E\\u7328\\]\\u6AB8\\06\\x71\\a\\]\\e\\|KN\\u06AA\\0000\\063\\u2523&&[\\005\\0277\\x41U\\034\\}R\\u14C7\\u4767\\x09\\n\\054Ev\\0144\\<\\f\\,Q-\\xE4]]]]]{3}+)|(?>^+)|(?![^|\\|\\nJ\\t\\<\\04E\\\\\\t\\01\\\\\\02\\|\\=\\}\\xF3\\uBEC2\\032K\\014\\uCC5F\\072q\\|\\0153\\xD9\\0322\\uC6C8[^\\t\\0342\\x34\\x91\\06\\{\\xF1\\a\\u1710\\?\\xE7\\uC106\\02pF\\<&&[^|\\]\\064\\u381D\\u50CF\\eO&&[^|\\06\\x2F\\04\\045\\032\\u8536W\\0377\\0017|\\x06\\uE5FA\\05\\xD4\\020\\04c\\xFC\\02H\\x0A\\r]]]]+?)(?idmsux-idmsux)|(?<![|\\r-\\,&&[I\\t\\r\\0201\\xDB\\e&&[^|\\02\\06\\00\\<\\a\\u7952\\064\\051\\073\\x41\\?n\\040\\0053\\031&&[\\x15-\\|]]]]{8,11}?)(?![^|\\<-\\uA74B\\xFA\\u7CD2\\024\\07n\\<\\x6A\\0042\\uE4FF\\r\\u896B\\[\\=\\042Y&&^\\p{ASCII}]++)|(?<![R-\\|&&[\\a\\0120A\\u6145\\<\\050-d[|\\e-\\uA07C|\\016-\\u80D9]]]{1,}+)|(?idmsux-idmsux)|(?idmsux-idmsux)|(?idmsux-idmsux:\\B{6,}?)|(?<=\\D{5,8}?)|(?>[\\{-\\0207|\\06-\\0276\\p{XDigit}])(?idmsux-idmsux:[^|\\x52\\0012\\]u\\xAD\\0051f\\0142\\\\l\\|\\050\\05\\f\\t\\u7B91\\r\\u7763\\{|h\\0104\\a\\f\\0234\\u2D4F&&^\\P{InGreek}]))");
+ // HARMONY-5858
+ Pattern.compile("\\u6211", Pattern.LITERAL);
+ }
+
+ public void testOrphanQuantifiers() {
+ try {
+ Pattern.compile("+++++");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException pse) {
+ }
+ }
+
+ public void testOrphanQuantifiers2() {
+ try {
+ Pattern.compile("\\d+*");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException pse) {
+ }
+ }
+
+ public void testBug197() {
+ Object[] vals = { ":", new Integer(2),
+ new String[] { "boo", "and:foo" }, ":", new Integer(5),
+ new String[] { "boo", "and", "foo" }, ":", new Integer(-2),
+ new String[] { "boo", "and", "foo" }, ":", new Integer(3),
+ new String[] { "boo", "and", "foo" }, ":", new Integer(1),
+ new String[] { "boo:and:foo" }, "o", new Integer(5),
+ new String[] { "b", "", ":and:f", "", "" }, "o",
+ new Integer(4), new String[] { "b", "", ":and:f", "o" }, "o",
+ new Integer(-2), new String[] { "b", "", ":and:f", "", "" },
+ "o", new Integer(0), new String[] { "b", "", ":and:f" } };
+
+ for (int i = 0; i < vals.length / 3;) {
+ String[] res = Pattern.compile(vals[i++].toString()).split(
+ "boo:and:foo", ((Integer) vals[i++]).intValue());
+ String[] expectedRes = (String[]) vals[i++];
+
+ assertEquals(expectedRes.length, res.length);
+
+ for (int j = 0; j < expectedRes.length; j++) {
+ assertEquals(expectedRes[j], res[j]);
+ }
+ }
+ }
+
+ public void testURIPatterns() {
+ String URI_REGEXP_STR = "^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?";
+ String SCHEME_REGEXP_STR = "^[a-zA-Z]{1}[\\w+-.]+$";
+ String REL_URI_REGEXP_STR = "^(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?";
+ String IPV6_REGEXP_STR = "^[0-9a-fA-F\\:\\.]+(\\%\\w+)?$";
+ String IPV6_REGEXP_STR2 = "^\\[[0-9a-fA-F\\:\\.]+(\\%\\w+)?\\]$";
+ String IPV4_REGEXP_STR = "^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$";
+ String HOSTNAME_REGEXP_STR = "\\w+[\\w\\-\\.]*";
+
+ Pattern.compile(URI_REGEXP_STR);
+ Pattern.compile(REL_URI_REGEXP_STR);
+ Pattern.compile(SCHEME_REGEXP_STR);
+ Pattern.compile(IPV4_REGEXP_STR);
+ Pattern.compile(IPV6_REGEXP_STR);
+ Pattern.compile(IPV6_REGEXP_STR2);
+ Pattern.compile(HOSTNAME_REGEXP_STR);
+ }
+
+ public void testFindBoundaryCases1() {
+ Pattern pat = Pattern.compile(".*\n");
+ Matcher mat = pat.matcher("a\n");
+
+ mat.find();
+ assertEquals("a\n", mat.group());
+
+ }
+
+ public void testFindBoundaryCases2() {
+ Pattern pat = Pattern.compile(".*A");
+ Matcher mat = pat.matcher("aAa");
+
+ mat.find();
+ assertEquals("aA", mat.group());
+
+ }
+
+ public void testFindBoundaryCases3() {
+ Pattern pat = Pattern.compile(".*A");
+ Matcher mat = pat.matcher("a\naA\n");
+
+ mat.find();
+ assertEquals("aA", mat.group());
+
+ }
+
+ public void testFindBoundaryCases4() {
+ Pattern pat = Pattern.compile("A.*");
+ Matcher mat = pat.matcher("A\n");
+
+ mat.find();
+ assertEquals("A", mat.group());
+
+ }
+
+ public void testFindBoundaryCases5() {
+ Pattern pat = Pattern.compile(".*A.*");
+ Matcher mat = pat.matcher("\nA\naaa\nA\naaAaa\naaaA\n");
+ // Matcher mat = pat.matcher("\nA\n");
+ String[] res = { "A", "A", "aaAaa", "aaaA" };
+ int k = 0;
+ for (; mat.find(); k++) {
+ assertEquals(res[k], mat.group());
+ }
+ }
+
+ public void testFindBoundaryCases6() {
+ String[] res = { "", "a", "", "" };
+ Pattern pat = Pattern.compile(".*");
+ Matcher mat = pat.matcher("\na\n");
+ int k = 0;
+
+ for (; mat.find(); k++) {
+ assertEquals(res[k], mat.group());
+ }
+ }
+
+ public void _testFindBoundaryCases7() {
+ Pattern pat = Pattern.compile(".*");
+ Matcher mat = pat.matcher("\na\n");
+ int k = 0;
+
+ for (; mat.find(); k++) {
+ System.out.println(mat.group());
+ System.out.flush();
+ }
+ }
+
+ public void testBackReferences() {
+ Pattern pat = Pattern.compile("(\\((\\w*):(.*):(\\2)\\))");
+ Matcher mat = pat
+ .matcher("(start1: word :start1)(start2: word :start2)");
+ int k = 1;
+ for (; mat.find(); k++) {
+ assertEquals("start" + k, mat.group(2));
+ assertEquals(" word ", mat.group(3));
+ assertEquals("start" + k, mat.group(4));
+
+ }
+
+ assertEquals(3, k);
+ pat = Pattern.compile(".*(.)\\1");
+ mat = pat.matcher("saa");
+ assertTrue(mat.matches());
+ }
+
+ public void _testBackReferences1() {
+ Pattern pat = Pattern.compile("(\\((\\w*):(.*):(\\2)\\))");
+ Matcher mat = pat
+ .matcher("(start1: word :start1)(start2: word :start2)");
+ int k = 1;
+ for (; mat.find(); k++) {
+ System.out.println(mat.group(2));
+ System.out.println(mat.group(3));
+ System.out.println(mat.group(4));
+
+ }
+
+ assertEquals(3, k);
+ }
+
+ public void testNewLine() {
+ Pattern pat = Pattern.compile("(^$)*\n", Pattern.MULTILINE);
+ Matcher mat = pat.matcher("\r\n\n");
+ int counter = 0;
+ while (mat.find()) {
+ counter++;
+ }
+ assertEquals(2, counter);
+ }
+
+ public void testFindGreedy() {
+ Pattern pat = Pattern.compile(".*aaa", Pattern.DOTALL);
+ Matcher mat = pat.matcher("aaaa\naaa\naaaaaa");
+ mat.matches();
+ assertEquals(15, mat.end());
+ }
+
+ public void testSerialization() throws Exception {
+ Pattern pat = Pattern.compile("a*bc");
+ SerializableAssert comparator = new SerializableAssert() {
+ public void assertDeserialized(Serializable initial,
+ Serializable deserialized) {
+ assertEquals(((Pattern) initial).toString(),
+ ((Pattern) deserialized).toString());
+ }
+ };
+ SerializationTest.verifyGolden(this, pat, comparator);
+ SerializationTest.verifySelf(pat, comparator);
+ }
+
+ public void testSOLQuant() {
+ Pattern pat = Pattern.compile("$*", Pattern.MULTILINE);
+ Matcher mat = pat.matcher("\n\n");
+ int counter = 0;
+ while (mat.find()) {
+ counter++;
+ }
+
+ assertEquals(3, counter);
+ }
+
+ public void testIllegalEscape() {
+ try {
+ Pattern.compile("\\y");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException pse) {
+ }
+ }
+
+ public void testEmptyFamily() {
+ Pattern.compile("\\p{Lower}");
+ }
+
+ public void testNonCaptConstr() {
+ // Flags
+ Pattern pat = Pattern.compile("(?i)b*(?-i)a*");
+ assertTrue(pat.matcher("bBbBaaaa").matches());
+ assertFalse(pat.matcher("bBbBAaAa").matches());
+
+ // Non-capturing groups
+ pat = Pattern.compile("(?i:b*)a*");
+ assertTrue(pat.matcher("bBbBaaaa").matches());
+ assertFalse(pat.matcher("bBbBAaAa").matches());
+
+ pat = Pattern
+ // 1 2 3 4 5 6 7 8 9 10 11
+ .compile("(?:-|(-?\\d+\\d\\d\\d))?(?:-|-(\\d\\d))?(?:-|-(\\d\\d))?(T)?(?:(\\d\\d):(\\d\\d):(\\d\\d)(\\.\\d+)?)?(?:(?:((?:\\+|\\-)\\d\\d):(\\d\\d))|(Z))?");
+ Matcher mat = pat.matcher("-1234-21-31T41:51:61.789+71:81");
+ assertTrue(mat.matches());
+ assertEquals("-1234", mat.group(1));
+ assertEquals("21", mat.group(2));
+ assertEquals("31", mat.group(3));
+ assertEquals("T", mat.group(4));
+ assertEquals("41", mat.group(5));
+ assertEquals("51", mat.group(6));
+ assertEquals("61", mat.group(7));
+ assertEquals(".789", mat.group(8));
+ assertEquals("+71", mat.group(9));
+ assertEquals("81", mat.group(10));
+
+ // positive lookahead
+ pat = Pattern.compile(".*\\.(?=log$).*$");
+ assertTrue(pat.matcher("a.b.c.log").matches());
+ assertFalse(pat.matcher("a.b.c.log.").matches());
+
+ // negative lookahead
+ pat = Pattern.compile(".*\\.(?!log$).*$");
+ assertFalse(pat.matcher("abc.log").matches());
+ assertTrue(pat.matcher("abc.logg").matches());
+
+ // positive lookbehind
+ pat = Pattern.compile(".*(?<=abc)\\.log$");
+ assertFalse(pat.matcher("cde.log").matches());
+ assertTrue(pat.matcher("abc.log").matches());
+
+ // negative lookbehind
+ pat = Pattern.compile(".*(?<!abc)\\.log$");
+ assertTrue(pat.matcher("cde.log").matches());
+ assertFalse(pat.matcher("abc.log").matches());
+
+ // atomic group
+ pat = Pattern.compile("(?>a*)abb");
+ assertFalse(pat.matcher("aaabb").matches());
+ pat = Pattern.compile("(?>a*)bb");
+ assertTrue(pat.matcher("aaabb").matches());
+
+ pat = Pattern.compile("(?>a|aa)aabb");
+ assertTrue(pat.matcher("aaabb").matches());
+ pat = Pattern.compile("(?>aa|a)aabb");
+ assertFalse(pat.matcher("aaabb").matches());
+
+ // quantifiers over look ahead
+ pat = Pattern.compile(".*(?<=abc)*\\.log$");
+ assertTrue(pat.matcher("cde.log").matches());
+ pat = Pattern.compile(".*(?<=abc)+\\.log$");
+ assertFalse(pat.matcher("cde.log").matches());
+
+ }
+
+ public void _testCorrectReplacementBackreferencedJointSet() {
+ Pattern.compile("ab(a)*\\1");
+ Pattern.compile("abc(cd)fg");
+ Pattern.compile("aba*cd");
+ Pattern.compile("ab(a)*+cd");
+ Pattern.compile("ab(a)*?cd");
+ Pattern.compile("ab(a)+cd");
+ Pattern.compile(".*(.)\\1");
+ Pattern.compile("ab((a)|c|d)e");
+ Pattern.compile("abc((a(b))cd)");
+ Pattern.compile("ab(a)++cd");
+ Pattern.compile("ab(a)?(c)d");
+ Pattern.compile("ab(a)?+cd");
+ Pattern.compile("ab(a)??cd");
+ Pattern.compile("ab(a)??cd");
+ Pattern.compile("ab(a){1,3}?(c)d");
+ }
+
+ public void testCompilePatternWithTerminatorMark() {
+ Pattern pat = Pattern.compile("a\u0000\u0000cd");
+ Matcher mat = pat.matcher("a\u0000\u0000cd");
+ assertTrue(mat.matches());
+ }
+
+ public void testAlternations() {
+ String baseString = "|a|bc";
+ Pattern pat = Pattern.compile(baseString);
+ Matcher mat = pat.matcher("");
+
+ assertTrue(mat.matches());
+
+ baseString = "a||bc";
+ pat = Pattern.compile(baseString);
+ mat = pat.matcher("");
+ assertTrue(mat.matches());
+
+ baseString = "a|bc|";
+ pat = Pattern.compile(baseString);
+ mat = pat.matcher("");
+ assertTrue(mat.matches());
+
+ baseString = "a|b|";
+ pat = Pattern.compile(baseString);
+ mat = pat.matcher("");
+ assertTrue(mat.matches());
+
+ baseString = "a(|b|cd)e";
+ pat = Pattern.compile(baseString);
+ mat = pat.matcher("ae");
+ assertTrue(mat.matches());
+
+ baseString = "a(b||cd)e";
+ pat = Pattern.compile(baseString);
+ mat = pat.matcher("ae");
+ assertTrue(mat.matches());
+
+ baseString = "a(b|cd|)e";
+ pat = Pattern.compile(baseString);
+ mat = pat.matcher("ae");
+ assertTrue(mat.matches());
+
+ baseString = "a(b|c|)e";
+ pat = Pattern.compile(baseString);
+ mat = pat.matcher("ae");
+ assertTrue(mat.matches());
+
+ baseString = "a(|)e";
+ pat = Pattern.compile(baseString);
+ mat = pat.matcher("ae");
+ assertTrue(mat.matches());
+
+ baseString = "|";
+ pat = Pattern.compile(baseString);
+ mat = pat.matcher("");
+ assertTrue(mat.matches());
+
+ baseString = "a(?:|)e";
+ pat = Pattern.compile(baseString);
+ mat = pat.matcher("ae");
+ assertTrue(mat.matches());
+
+ baseString = "a||||bc";
+ pat = Pattern.compile(baseString);
+ mat = pat.matcher("");
+ assertTrue(mat.matches());
+
+ baseString = "(?i-is)|a";
+ pat = Pattern.compile(baseString);
+ mat = pat.matcher("a");
+ assertTrue(mat.matches());
+ }
+
+ public void testMatchWithGroups() {
+ String baseString = "jwkerhjwehrkwjehrkwjhrwkjehrjwkehrjkwhrkwehrkwhrkwrhwkhrwkjehr";
+ String pattern = ".*(..).*\\1.*";
+ assertTrue(Pattern.compile(pattern).matcher(baseString).matches());
+
+ baseString = "saa";
+ pattern = ".*(.)\\1";
+ assertTrue(Pattern.compile(pattern).matcher(baseString).matches());
+ assertTrue(Pattern.compile(pattern).matcher(baseString).find());
+ }
+
+ public void testSplitEmptyCharSequence() {
+ String s1 = "";
+ String[] arr = s1.split(":");
+ assertEquals(arr.length, 1);
+ }
+
+ public void testSplitEndsWithPattern() {
+ assertEquals(",,".split(",", 3).length, 3);
+ assertEquals(",,".split(",", 4).length, 3);
+
+ assertEquals(Pattern.compile("o").split("boo:and:foo", 5).length, 5);
+ assertEquals(Pattern.compile("b").split("ab", -1).length, 2);
+ }
+
+ public void testCaseInsensitiveFlag() {
+ assertTrue(Pattern.matches("(?i-:AbC)", "ABC"));
+ }
+
+ public void testEmptyGroups() {
+ Pattern pat = Pattern.compile("ab(?>)cda");
+ Matcher mat = pat.matcher("abcda");
+ assertTrue(mat.matches());
+
+ pat = Pattern.compile("ab()");
+ mat = pat.matcher("ab");
+ assertTrue(mat.matches());
+
+ pat = Pattern.compile("abc(?:)(..)");
+ mat = pat.matcher("abcgf");
+ assertTrue(mat.matches());
+ }
+
+ public void testCompileNonCaptGroup() {
+ boolean isCompiled = false;
+
+ try {
+ Pattern.compile("(?:)", Pattern.CANON_EQ);
+ Pattern.compile("(?:)", Pattern.CANON_EQ | Pattern.DOTALL);
+ Pattern
+ .compile("(?:)", Pattern.CANON_EQ
+ | Pattern.CASE_INSENSITIVE);
+ Pattern.compile("(?:)", Pattern.CANON_EQ | Pattern.COMMENTS
+ | Pattern.UNIX_LINES);
+ isCompiled = true;
+ } catch (PatternSyntaxException e) {
+ System.out.println(e);
+ }
+ assertTrue(isCompiled);
+ }
+
+ public void testEmbeddedFlags() {
+ String baseString = "(?i)((?s)a)";
+ String testString = "A";
+ Pattern pat = Pattern.compile(baseString);
+ Matcher mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ baseString = "(?x)(?i)(?s)(?d)a";
+ testString = "A";
+ pat = Pattern.compile(baseString);
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ baseString = "(?x)(?i)(?s)(?d)a.";
+ testString = "a\n";
+ pat = Pattern.compile(baseString);
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ baseString = "abc(?x:(?i)(?s)(?d)a.)";
+ testString = "abcA\n";
+ pat = Pattern.compile(baseString);
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ baseString = "abc((?x)d)(?i)(?s)a";
+ testString = "abcdA";
+ pat = Pattern.compile(baseString);
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+ }
+
+ public void testAltWithFlags() {
+ Pattern.compile("|(?i-xi)|()");
+ }
+
+ public void testRestoreFlagsAfterGroup() {
+ String baseString = "abc((?x)d) a";
+ String testString = "abcd a";
+ Pattern pat = Pattern.compile(baseString);
+ Matcher mat = pat.matcher(testString);
+
+ assertTrue(mat.matches());
+ }
+
+ /*
+ * Verify if the Pattern support the following character classes:
+ * \p{javaLowerCase} \p{javaUpperCase} \p{javaWhitespace} \p{javaMirrored}
+ */
+ public void testCompileCharacterClass() {
+ // Regression for HARMONY-606, 696
+ Pattern pattern = Pattern.compile("\\p{javaLowerCase}");
+ assertNotNull(pattern);
+
+ pattern = Pattern.compile("\\p{javaUpperCase}");
+ assertNotNull(pattern);
+
+ pattern = Pattern.compile("\\p{javaWhitespace}");
+ assertNotNull(pattern);
+
+ pattern = Pattern.compile("\\p{javaMirrored}");
+ assertNotNull(pattern);
+
+ pattern = Pattern.compile("\\p{javaDefined}");
+ assertNotNull(pattern);
+
+ pattern = Pattern.compile("\\p{javaDigit}");
+ assertNotNull(pattern);
+
+ pattern = Pattern.compile("\\p{javaIdentifierIgnorable}");
+ assertNotNull(pattern);
+
+ pattern = Pattern.compile("\\p{javaISOControl}");
+ assertNotNull(pattern);
+
+ pattern = Pattern.compile("\\p{javaJavaIdentifierPart}");
+ assertNotNull(pattern);
+
+ pattern = Pattern.compile("\\p{javaJavaIdentifierStart}");
+ assertNotNull(pattern);
+
+ pattern = Pattern.compile("\\p{javaLetter}");
+ assertNotNull(pattern);
+
+ pattern = Pattern.compile("\\p{javaLetterOrDigit}");
+ assertNotNull(pattern);
+
+ pattern = Pattern.compile("\\p{javaSpaceChar}");
+ assertNotNull(pattern);
+
+ pattern = Pattern.compile("\\p{javaTitleCase}");
+ assertNotNull(pattern);
+
+ pattern = Pattern.compile("\\p{javaUnicodeIdentifierPart}");
+ assertNotNull(pattern);
+
+ pattern = Pattern.compile("\\p{javaUnicodeIdentifierStart}");
+ assertNotNull(pattern);
+ }
+
+ public void testCanonEqFlag() {
+
+ /*
+ * for decompositions see
+ * http://www.unicode.org/Public/4.0-Update/UnicodeData-4.0.0.txt
+ * http://www.unicode.org/reports/tr15/#Decomposition
+ */
+ String baseString;
+ String testString;
+ Pattern pat;
+ Matcher mat;
+
+ baseString = "ab(a*)\\1";
+ pat = Pattern.compile(baseString, Pattern.CANON_EQ);
+
+ baseString = "a(abcdf)d";
+ pat = Pattern.compile(baseString, Pattern.CANON_EQ);
+
+ baseString = "aabcdfd";
+ pat = Pattern.compile(baseString, Pattern.CANON_EQ);
+
+ // \u01E0 -> \u0226\u0304 ->\u0041\u0307\u0304
+ // \u00CC -> \u0049\u0300
+
+ baseString = "\u01E0\u00CCcdb(ac)";
+ testString = "\u0226\u0304\u0049\u0300cdbac";
+ pat = Pattern.compile(baseString, Pattern.CANON_EQ);
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ baseString = "\u01E0cdb(a\u00CCc)";
+ testString = "\u0041\u0307\u0304cdba\u0049\u0300c";
+ pat = Pattern.compile(baseString, Pattern.CANON_EQ);
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ baseString = "a\u00CC";
+ testString = "a\u0049\u0300";
+ pat = Pattern.compile(baseString, Pattern.CANON_EQ);
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ baseString = "\u0226\u0304cdb(ac\u0049\u0300)";
+ testString = "\u01E0cdbac\u00CC";
+ pat = Pattern.compile(baseString, Pattern.CANON_EQ);
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ baseString = "cdb(?:\u0041\u0307\u0304\u00CC)";
+ testString = "cdb\u0226\u0304\u0049\u0300";
+ pat = Pattern.compile(baseString, Pattern.CANON_EQ);
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ baseString = "\u01E0[a-c]\u0049\u0300cdb(ac)";
+ testString = "\u01E0b\u00CCcdbac";
+ pat = Pattern.compile(baseString, Pattern.CANON_EQ);
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ baseString = "\u01E0|\u00CCcdb(ac)";
+ testString = "\u0041\u0307\u0304";
+ pat = Pattern.compile(baseString, Pattern.CANON_EQ);
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ baseString = "\u00CC?cdb(ac)*(\u01E0)*[a-c]";
+ testString = "cdb\u0041\u0307\u0304b";
+ pat = Pattern.compile(baseString, Pattern.CANON_EQ);
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ baseString = "a\u0300";
+ pat = Pattern.compile(baseString, Pattern.CANON_EQ);
+ mat = pat.matcher("a\u00E0a");
+ assertTrue(mat.find());
+
+ baseString = "\u7B20\uF9F8abc";
+ pat = Pattern.compile(baseString, Pattern.CANON_EQ);
+ mat = pat.matcher("\uF9F8\uF9F8abc");
+ assertTrue(mat.matches());
+
+ // \u01F9 -> \u006E\u0300
+ // \u00C3 -> \u0041\u0303
+
+ baseString = "cdb(?:\u00C3\u006E\u0300)";
+ testString = "cdb\u0041\u0303\u01F9";
+ pat = Pattern.compile(baseString, Pattern.CANON_EQ);
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ // \u014C -> \u004F\u0304
+ // \u0163 -> \u0074\u0327
+
+ baseString = "cdb(?:\u0163\u004F\u0304)";
+ testString = "cdb\u0074\u0327\u014C";
+ pat = Pattern.compile(baseString, Pattern.CANON_EQ);
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ // \u00E1->a\u0301
+ // canonical ordering takes place \u0301\u0327 -> \u0327\u0301
+
+ baseString = "c\u0327\u0301";
+ testString = "c\u0301\u0327";
+ pat = Pattern.compile(baseString, Pattern.CANON_EQ);
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ /*
+ * Hangul decompositions
+ */
+ // \uD4DB->\u1111\u1171\u11B6
+ // \uD21E->\u1110\u116D\u11B5
+ // \uD264->\u1110\u1170
+ // not Hangul:\u0453->\u0433\u0301
+ baseString = "a\uD4DB\u1111\u1171\u11B6\uD264";
+ pat = Pattern.compile(baseString, Pattern.CANON_EQ);
+
+ baseString = "\u0453c\uD4DB";
+ pat = Pattern.compile(baseString, Pattern.CANON_EQ);
+
+ baseString = "a\u1110\u116D\u11B5b\uD21Ebc";
+ pat = Pattern.compile(baseString, Pattern.CANON_EQ);
+
+ baseString = "\uD4DB\uD21E\u1110\u1170cdb(ac)";
+ testString = "\u1111\u1171\u11B6\u1110\u116D\u11B5\uD264cdbac";
+ pat = Pattern.compile(baseString, Pattern.CANON_EQ);
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ baseString = "\uD4DB\uD264cdb(a\uD21Ec)";
+ testString = "\u1111\u1171\u11B6\u1110\u1170cdba\u1110\u116D\u11B5c";
+ pat = Pattern.compile(baseString, Pattern.CANON_EQ);
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ baseString = "a\uD4DB";
+ testString = "a\u1111\u1171\u11B6";
+ pat = Pattern.compile(baseString, Pattern.CANON_EQ);
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ baseString = "a\uD21E";
+ testString = "a\u1110\u116D\u11B5";
+ pat = Pattern.compile(baseString, Pattern.CANON_EQ);
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ baseString = "\u1111\u1171\u11B6cdb(ac\u1110\u116D\u11B5)";
+ testString = "\uD4DBcdbac\uD21E";
+ pat = Pattern.compile(baseString, Pattern.CANON_EQ);
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ baseString = "cdb(?:\u1111\u1171\u11B6\uD21E)";
+ testString = "cdb\uD4DB\u1110\u116D\u11B5";
+ pat = Pattern.compile(baseString, Pattern.CANON_EQ);
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ baseString = "\uD4DB[a-c]\u1110\u116D\u11B5cdb(ac)";
+ testString = "\uD4DBb\uD21Ecdbac";
+ pat = Pattern.compile(baseString, Pattern.CANON_EQ);
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ baseString = "\uD4DB|\u00CCcdb(ac)";
+ testString = "\u1111\u1171\u11B6";
+ pat = Pattern.compile(baseString, Pattern.CANON_EQ);
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ baseString = "\uD4DB|\u00CCcdb(ac)";
+ testString = "\u1111\u1171";
+ pat = Pattern.compile(baseString, Pattern.CANON_EQ);
+ mat = pat.matcher(testString);
+ assertFalse(mat.matches());
+
+ baseString = "\u00CC?cdb(ac)*(\uD4DB)*[a-c]";
+ testString = "cdb\u1111\u1171\u11B6b";
+ pat = Pattern.compile(baseString, Pattern.CANON_EQ);
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ baseString = "\uD4DB";
+ pat = Pattern.compile(baseString, Pattern.CANON_EQ);
+ mat = pat.matcher("a\u1111\u1171\u11B6a");
+ assertTrue(mat.find());
+
+ baseString = "\u1111";
+ pat = Pattern.compile(baseString, Pattern.CANON_EQ);
+ mat = pat.matcher("bcda\uD4DBr");
+ assertFalse(mat.find());
+ }
+
+ public void testIndexesCanonicalEq() {
+ String baseString;
+ String testString;
+ Pattern pat;
+ Matcher mat;
+
+ baseString = "\uD4DB";
+ pat = Pattern.compile(baseString, Pattern.CANON_EQ);
+ mat = pat.matcher("bcda\u1111\u1171\u11B6awr");
+ assertTrue(mat.find());
+ assertEquals(mat.start(), 4);
+ assertEquals(mat.end(), 7);
+
+ baseString = "\uD4DB\u1111\u1171\u11B6";
+ pat = Pattern.compile(baseString, Pattern.CANON_EQ);
+ mat = pat.matcher("bcda\u1111\u1171\u11B6\uD4DBawr");
+ assertTrue(mat.find());
+ assertEquals(mat.start(), 4);
+ assertEquals(mat.end(), 8);
+
+ baseString = "\uD4DB\uD21E\u1110\u1170";
+ testString = "abcabc\u1111\u1171\u11B6\u1110\u116D\u11B5\uD264cdbac";
+ pat = Pattern.compile(baseString, Pattern.CANON_EQ);
+ mat = pat.matcher(testString);
+ assertTrue(mat.find());
+ assertEquals(mat.start(), 6);
+ assertEquals(mat.end(), 13);
+ }
+
+ public void testCanonEqFlagWithSupplementaryCharacters() {
+
+ /*
+ * \u1D1BF->\u1D1BB\u1D16F->\u1D1B9\u1D165\u1D16F in UTF32
+ * \uD834\uDDBF->\uD834\uDDBB\uD834\uDD6F
+ * ->\uD834\uDDB9\uD834\uDD65\uD834\uDD6F in UTF16
+ */
+ String patString = "abc\uD834\uDDBFef";
+ String testString = "abc\uD834\uDDB9\uD834\uDD65\uD834\uDD6Fef";
+ Pattern pat = Pattern.compile(patString, Pattern.CANON_EQ);
+ Matcher mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ testString = "abc\uD834\uDDBB\uD834\uDD6Fef";
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ patString = "abc\uD834\uDDBB\uD834\uDD6Fef";
+ testString = "abc\uD834\uDDBFef";
+ pat = Pattern.compile(patString, Pattern.CANON_EQ);
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ testString = "abc\uD834\uDDB9\uD834\uDD65\uD834\uDD6Fef";
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ patString = "abc\uD834\uDDB9\uD834\uDD65\uD834\uDD6Fef";
+ testString = "abc\uD834\uDDBFef";
+ pat = Pattern.compile(patString, Pattern.CANON_EQ);
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ testString = "abc\uD834\uDDBB\uD834\uDD6Fef";
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ /*
+ * testSupplementary characters with no decomposition
+ */
+ patString = "a\uD9A0\uDE8Ebc\uD834\uDDBB\uD834\uDD6Fe\uDE8Ef";
+ testString = "a\uD9A0\uDE8Ebc\uD834\uDDBFe\uDE8Ef";
+ pat = Pattern.compile(patString, Pattern.CANON_EQ);
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+ }
+
+ public void testRangesWithSurrogatesSupplementary() {
+ String patString = "[abc\uD8D2]";
+ String testString = "\uD8D2";
+ Pattern pat = Pattern.compile(patString);
+ Matcher mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ testString = "a";
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ testString = "ef\uD8D2\uDD71gh";
+ mat = pat.matcher(testString);
+ assertFalse(mat.find());
+
+ testString = "ef\uD8D2gh";
+ mat = pat.matcher(testString);
+ assertTrue(mat.find());
+
+ patString = "[abc\uD8D3&&[c\uD8D3]]";
+ testString = "c";
+ pat = Pattern.compile(patString);
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ testString = "a";
+ mat = pat.matcher(testString);
+ assertFalse(mat.matches());
+
+ testString = "ef\uD8D3\uDD71gh";
+ mat = pat.matcher(testString);
+ assertFalse(mat.find());
+
+ testString = "ef\uD8D3gh";
+ mat = pat.matcher(testString);
+ assertTrue(mat.find());
+
+ patString = "[abc\uD8D3\uDBEE\uDF0C&&[c\uD8D3\uDBEE\uDF0C]]";
+ testString = "c";
+ pat = Pattern.compile(patString);
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ testString = "\uDBEE\uDF0C";
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ testString = "ef\uD8D3\uDD71gh";
+ mat = pat.matcher(testString);
+ assertFalse(mat.find());
+
+ testString = "ef\uD8D3gh";
+ mat = pat.matcher(testString);
+ assertTrue(mat.find());
+
+ patString = "[abc\uDBFC]\uDDC2cd";
+ testString = "\uDBFC\uDDC2cd";
+ pat = Pattern.compile(patString);
+ mat = pat.matcher(testString);
+ assertFalse(mat.matches());
+
+ testString = "a\uDDC2cd";
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+ }
+
+ public void testSequencesWithSurrogatesSupplementary() {
+ String patString = "abcd\uD8D3";
+ String testString = "abcd\uD8D3\uDFFC";
+ Pattern pat = Pattern.compile(patString);
+ Matcher mat = pat.matcher(testString);
+ assertFalse(mat.find());
+
+ testString = "abcd\uD8D3abc";
+ mat = pat.matcher(testString);
+ assertTrue(mat.find());
+
+ patString = "ab\uDBEFcd";
+ testString = "ab\uDBEFcd";
+ pat = Pattern.compile(patString);
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ patString = "\uDFFCabcd";
+ testString = "\uD8D3\uDFFCabcd";
+ pat = Pattern.compile(patString);
+ mat = pat.matcher(testString);
+ assertFalse(mat.find());
+
+ testString = "abc\uDFFCabcdecd";
+ mat = pat.matcher(testString);
+ assertTrue(mat.find());
+
+ patString = "\uD8D3\uDFFCabcd";
+ testString = "abc\uD8D3\uD8D3\uDFFCabcd";
+ pat = Pattern.compile(patString);
+ mat = pat.matcher(testString);
+ assertTrue(mat.find());
+ }
+
+ public void testPredefinedClassesWithSurrogatesSupplementary() {
+ String patString = "[123\\D]";
+ String testString = "a";
+ Pattern pat = Pattern.compile(patString);
+ Matcher mat = pat.matcher(testString);
+ assertTrue(mat.find());
+
+ testString = "5";
+ mat = pat.matcher(testString);
+ assertFalse(mat.find());
+
+ testString = "3";
+ mat = pat.matcher(testString);
+ assertTrue(mat.find());
+
+ // low surrogate
+ testString = "\uDFC4";
+ mat = pat.matcher(testString);
+ assertTrue(mat.find());
+
+ // high surrogate
+ testString = "\uDADA";
+ mat = pat.matcher(testString);
+ assertTrue(mat.find());
+
+ testString = "\uDADA\uDFC4";
+ mat = pat.matcher(testString);
+ assertTrue(mat.find());
+
+ patString = "[123[^\\p{javaDigit}]]";
+ testString = "a";
+ pat = Pattern.compile(patString);
+ mat = pat.matcher(testString);
+ assertTrue(mat.find());
+
+ testString = "5";
+ mat = pat.matcher(testString);
+ assertFalse(mat.find());
+
+ testString = "3";
+ mat = pat.matcher(testString);
+ assertTrue(mat.find());
+
+ // low surrogate
+ testString = "\uDFC4";
+ mat = pat.matcher(testString);
+ assertTrue(mat.find());
+
+ // high surrogate
+ testString = "\uDADA";
+ mat = pat.matcher(testString);
+ assertTrue(mat.find());
+
+ testString = "\uDADA\uDFC4";
+ mat = pat.matcher(testString);
+ assertTrue(mat.find());
+
+ // surrogate characters
+ patString = "\\p{Cs}";
+ testString = "\uD916\uDE27";
+ pat = Pattern.compile(patString);
+ mat = pat.matcher(testString);
+
+ /*
+ * see http://www.unicode.org/reports/tr18/#Supplementary_Characters we
+ * have to treat text as code points not code units. \\p{Cs} matches any
+ * surrogate character but here testString is a one code point
+ * consisting of two code units (two surrogate characters) so we find
+ * nothing
+ */
+ assertFalse(mat.find());
+
+ // swap low and high surrogates
+ testString = "\uDE27\uD916";
+ mat = pat.matcher(testString);
+ assertTrue(mat.find());
+
+ patString = "[\uD916\uDE271\uD91623&&[^\\p{Cs}]]";
+ testString = "1";
+ pat = Pattern.compile(patString);
+ mat = pat.matcher(testString);
+ assertTrue(mat.find());
+
+ testString = "\uD916";
+ pat = Pattern.compile(patString);
+ mat = pat.matcher(testString);
+ assertFalse(mat.find());
+
+ testString = "\uD916\uDE27";
+ pat = Pattern.compile(patString);
+ mat = pat.matcher(testString);
+ assertTrue(mat.find());
+
+ // \uD9A0\uDE8E=\u7828E
+ // \u78281=\uD9A0\uDE81
+ patString = "[a-\uD9A0\uDE8E]";
+ testString = "\uD9A0\uDE81";
+ pat = Pattern.compile(patString);
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+ }
+
+ public void testDotConstructionWithSurrogatesSupplementary() {
+ String patString = ".";
+ String testString = "\uD9A0\uDE81";
+ Pattern pat = Pattern.compile(patString);
+ Matcher mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ testString = "\uDE81";
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ testString = "\uD9A0";
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ testString = "\n";
+ mat = pat.matcher(testString);
+ assertFalse(mat.matches());
+
+ patString = ".*\uDE81";
+ testString = "\uD9A0\uDE81\uD9A0\uDE81\uD9A0\uDE81";
+ pat = Pattern.compile(patString);
+ mat = pat.matcher(testString);
+ assertFalse(mat.matches());
+
+ testString = "\uD9A0\uDE81\uD9A0\uDE81\uDE81";
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ patString = ".*";
+ testString = "\uD9A0\uDE81\n\uD9A0\uDE81\uD9A0\n\uDE81";
+ pat = Pattern.compile(patString, Pattern.DOTALL);
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+ }
+
+ public void testQuantifiersWithSurrogatesSupplementary() {
+ String patString = "\uD9A0\uDE81*abc";
+ String testString = "\uD9A0\uDE81\uD9A0\uDE81abc";
+ Pattern pat = Pattern.compile(patString);
+ Matcher mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ testString = "abc";
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+ }
+
+ public void testAlternationsWithSurrogatesSupplementary() {
+ String patString = "\uDE81|\uD9A0\uDE81|\uD9A0";
+ String testString = "\uD9A0";
+ Pattern pat = Pattern.compile(patString);
+ Matcher mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ testString = "\uDE81";
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ testString = "\uD9A0\uDE81";
+ mat = pat.matcher(testString);
+ assertTrue(mat.matches());
+
+ testString = "\uDE81\uD9A0";
+ mat = pat.matcher(testString);
+ assertFalse(mat.matches());
+ }
+
+ public void testGroupsWithSurrogatesSupplementary() {
+
+ // this pattern matches nothing
+ String patString = "(\uD9A0)\uDE81";
+ String testString = "\uD9A0\uDE81";
+ Pattern pat = Pattern.compile(patString);
+ Matcher mat = pat.matcher(testString);
+ assertFalse(mat.matches());
+
+ patString = "(\uD9A0)";
+ testString = "\uD9A0\uDE81";
+ pat = Pattern.compile(patString, Pattern.DOTALL);
+ mat = pat.matcher(testString);
+ assertFalse(mat.find());
+ }
+
+ /*
+ * Regression test for HARMONY-688
+ */
+ public void testUnicodeCategoryWithSurrogatesSupplementary() {
+ Pattern p = Pattern.compile("\\p{javaLowerCase}");
+ Matcher matcher = p.matcher("\uD801\uDC28");
+ assertTrue(matcher.find());
+ }
+
+}
diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/ReplaceTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/ReplaceTest.java
new file mode 100644
index 0000000..1eac3f3
--- /dev/null
+++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/ReplaceTest.java
@@ -0,0 +1,90 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.harmony.tests.java.util.regex;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.regex.PatternSyntaxException;
+
+import junit.framework.TestCase;
+
+@SuppressWarnings("nls")
+public class ReplaceTest extends TestCase {
+
+ public void testSimpleReplace() throws PatternSyntaxException {
+ String target, pattern, repl;
+
+ target = "foobarfobarfoofo1";
+ pattern = "fo[^o]";
+ repl = "xxx";
+
+ Pattern p = Pattern.compile(pattern);
+ Matcher m = p.matcher(target);
+
+ assertEquals("foobarxxxarfoofo1", m.replaceFirst(repl));
+ assertEquals("foobarxxxarfooxxx", m.replaceAll(repl));
+ }
+
+ public void testCaptureReplace() {
+ String target, pattern, repl, s;
+ Pattern p = null;
+ Matcher m;
+
+ target = "[31]foo;bar[42];[99]xyz";
+ pattern = "\\[([0-9]+)\\]([a-z]+)";
+ repl = "$2[$1]";
+
+ p = Pattern.compile(pattern);
+ m = p.matcher(target);
+ s = m.replaceFirst(repl);
+ assertEquals("foo[31];bar[42];[99]xyz", s);
+ s = m.replaceAll(repl);
+ assertEquals("foo[31];bar[42];xyz[99]", s);
+
+ target = "[31]foo(42)bar{63}zoo;[12]abc(34)def{56}ghi;{99}xyz[88]xyz(77)xyz;";
+ pattern = "\\[([0-9]+)\\]([a-z]+)\\(([0-9]+)\\)([a-z]+)\\{([0-9]+)\\}([a-z]+)";
+ repl = "[$5]$6($3)$4{$1}$2";
+ p = Pattern.compile(pattern);
+ m = p.matcher(target);
+ s = m.replaceFirst(repl);
+ // System.out.println(s);
+ assertEquals(
+ "[63]zoo(42)bar{31}foo;[12]abc(34)def{56}ghi;{99}xyz[88]xyz(77)xyz;",
+ s);
+ s = m.replaceAll(repl);
+ // System.out.println(s);
+ assertEquals(
+ "[63]zoo(42)bar{31}foo;[56]ghi(34)def{12}abc;{99}xyz[88]xyz(77)xyz;",
+ s);
+ }
+
+ public void testEscapeReplace() {
+ String target, pattern, repl, s;
+
+ target = "foo'bar''foo";
+ pattern = "'";
+ repl = "\\'";
+ s = target.replaceAll(pattern, repl);
+ assertEquals("foo'bar''foo", s);
+ repl = "\\\\'";
+ s = target.replaceAll(pattern, repl);
+ assertEquals("foo\\'bar\\'\\'foo", s);
+ repl = "\\$3";
+ s = target.replaceAll(pattern, repl);
+ assertEquals("foo$3bar$3$3foo", s);
+ }
+}
diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/SplitTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/SplitTest.java
new file mode 100644
index 0000000..5a5bc2b
--- /dev/null
+++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/SplitTest.java
@@ -0,0 +1,171 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.harmony.tests.java.util.regex;
+
+import java.util.regex.Pattern;
+import java.util.regex.PatternSyntaxException;
+
+import junit.framework.TestCase;
+
+/**
+ * TODO Type description
+ */
+@SuppressWarnings("nls")
+public class SplitTest extends TestCase {
+
+ public void testSimple() {
+ Pattern p = Pattern.compile("/");
+ String[] results = p.split("have/you/done/it/right");
+ String[] expected = new String[] { "have", "you", "done", "it", "right" };
+ assertEquals(expected.length, results.length);
+ for (int i = 0; i < expected.length; i++) {
+ assertEquals(results[i], expected[i]);
+ }
+ }
+
+ public void testSplit1() throws PatternSyntaxException {
+ Pattern p = Pattern.compile(" ");
+
+ String input = "poodle zoo";
+ String tokens[];
+
+ tokens = p.split(input, 1);
+ assertEquals(1, tokens.length);
+ assertTrue(tokens[0].equals(input));
+ tokens = p.split(input, 2);
+ assertEquals(2, tokens.length);
+ assertEquals("poodle", tokens[0]);
+ assertEquals("zoo", tokens[1]);
+ tokens = p.split(input, 5);
+ assertEquals(2, tokens.length);
+ assertEquals("poodle", tokens[0]);
+ assertEquals("zoo", tokens[1]);
+ tokens = p.split(input, -2);
+ assertEquals(2, tokens.length);
+ assertEquals("poodle", tokens[0]);
+ assertEquals("zoo", tokens[1]);
+ tokens = p.split(input, 0);
+ assertEquals(2, tokens.length);
+ assertEquals("poodle", tokens[0]);
+ assertEquals("zoo", tokens[1]);
+ tokens = p.split(input);
+ assertEquals(2, tokens.length);
+ assertEquals("poodle", tokens[0]);
+ assertEquals("zoo", tokens[1]);
+
+ p = Pattern.compile("d");
+
+ tokens = p.split(input, 1);
+ assertEquals(1, tokens.length);
+ assertTrue(tokens[0].equals(input));
+ tokens = p.split(input, 2);
+ assertEquals(2, tokens.length);
+ assertEquals("poo", tokens[0]);
+ assertEquals("le zoo", tokens[1]);
+ tokens = p.split(input, 5);
+ assertEquals(2, tokens.length);
+ assertEquals("poo", tokens[0]);
+ assertEquals("le zoo", tokens[1]);
+ tokens = p.split(input, -2);
+ assertEquals(2, tokens.length);
+ assertEquals("poo", tokens[0]);
+ assertEquals("le zoo", tokens[1]);
+ tokens = p.split(input, 0);
+ assertEquals(2, tokens.length);
+ assertEquals("poo", tokens[0]);
+ assertEquals("le zoo", tokens[1]);
+ tokens = p.split(input);
+ assertEquals(2, tokens.length);
+ assertEquals("poo", tokens[0]);
+ assertEquals("le zoo", tokens[1]);
+
+ p = Pattern.compile("o");
+
+ tokens = p.split(input, 1);
+ assertEquals(1, tokens.length);
+ assertTrue(tokens[0].equals(input));
+ tokens = p.split(input, 2);
+ assertEquals(2, tokens.length);
+ assertEquals("p", tokens[0]);
+ assertEquals("odle zoo", tokens[1]);
+ tokens = p.split(input, 5);
+ assertEquals(5, tokens.length);
+ assertEquals("p", tokens[0]);
+ assertTrue(tokens[1].equals(""));
+ assertEquals("dle z", tokens[2]);
+ assertTrue(tokens[3].equals(""));
+ assertTrue(tokens[4].equals(""));
+ tokens = p.split(input, -2);
+ assertEquals(5, tokens.length);
+ assertEquals("p", tokens[0]);
+ assertTrue(tokens[1].equals(""));
+ assertEquals("dle z", tokens[2]);
+ assertTrue(tokens[3].equals(""));
+ assertTrue(tokens[4].equals(""));
+ tokens = p.split(input, 0);
+ assertEquals(3, tokens.length);
+ assertEquals("p", tokens[0]);
+ assertTrue(tokens[1].equals(""));
+ assertEquals("dle z", tokens[2]);
+ tokens = p.split(input);
+ assertEquals(3, tokens.length);
+ assertEquals("p", tokens[0]);
+ assertTrue(tokens[1].equals(""));
+ assertEquals("dle z", tokens[2]);
+ }
+
+ public void testSplit2() {
+ Pattern p = Pattern.compile("");
+ String s[];
+ s = p.split("a", -1);
+ assertEquals(3, s.length);
+ assertEquals("", s[0]);
+ assertEquals("a", s[1]);
+ assertEquals("", s[2]);
+
+ s = p.split("", -1);
+ assertEquals(1, s.length);
+ assertEquals("", s[0]);
+
+ s = p.split("abcd", -1);
+ assertEquals(6, s.length);
+ assertEquals("", s[0]);
+ assertEquals("a", s[1]);
+ assertEquals("b", s[2]);
+ assertEquals("c", s[3]);
+ assertEquals("d", s[4]);
+ assertEquals("", s[5]);
+ }
+
+ public void testSplitSupplementaryWithEmptyString() {
+
+ /*
+ * See http://www.unicode.org/reports/tr18/#Supplementary_Characters We
+ * have to treat text as code points not code units.
+ */
+ Pattern p = Pattern.compile("");
+ String s[];
+ s = p.split("a\ud869\uded6b", -1);
+ assertEquals(5, s.length);
+ assertEquals("", s[0]);
+ assertEquals("a", s[1]);
+ assertEquals("\ud869\uded6", s[2]);
+ assertEquals("b", s[3]);
+ assertEquals("", s[4]);
+ }
+}