001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.filter;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertTrue;
022
023import java.util.regex.Pattern;
024import org.apache.hadoop.hbase.filter.RegexStringComparator.EngineType;
025import org.apache.hadoop.hbase.testclassification.FilterTests;
026import org.apache.hadoop.hbase.testclassification.SmallTests;
027import org.apache.hadoop.hbase.util.Bytes;
028import org.junit.jupiter.api.Tag;
029import org.junit.jupiter.api.Test;
030
031@Tag(FilterTests.TAG)
032@Tag(SmallTests.TAG)
033public class TestRegexComparator {
034
035  @Test
036  public void testSerialization() throws Exception {
037    // Default engine is the Java engine
038    RegexStringComparator a = new RegexStringComparator("a|b");
039    RegexStringComparator b = RegexStringComparator.parseFrom(a.toByteArray());
040    assertTrue(a.areSerializedFieldsEqual(b));
041    assertTrue(b.getEngine() instanceof RegexStringComparator.JavaRegexEngine);
042
043    // joni engine
044    a = new RegexStringComparator("a|b", EngineType.JONI);
045    b = RegexStringComparator.parseFrom(a.toByteArray());
046    assertTrue(a.areSerializedFieldsEqual(b));
047    assertTrue(b.getEngine() instanceof RegexStringComparator.JoniRegexEngine);
048  }
049
050  @Test
051  public void testJavaEngine() throws Exception {
052    for (TestCase t : TEST_CASES) {
053      boolean result = new RegexStringComparator(t.regex, t.flags, EngineType.JAVA)
054        .compareTo(Bytes.toBytes(t.haystack)) == 0;
055      assertEquals(t.expected, result, "Regex '" + t.regex + "' failed test '" + t.haystack + "'");
056    }
057  }
058
059  @Test
060  public void testJoniEngine() throws Exception {
061    for (TestCase t : TEST_CASES) {
062      boolean result = new RegexStringComparator(t.regex, t.flags, EngineType.JONI)
063        .compareTo(Bytes.toBytes(t.haystack)) == 0;
064      assertEquals(t.expected, result, "Regex '" + t.regex + "' failed test '" + t.haystack + "'");
065    }
066  }
067
068  private static class TestCase {
069    String regex;
070    String haystack;
071    int flags;
072    boolean expected;
073
074    public TestCase(String regex, String haystack, boolean expected) {
075      this(regex, Pattern.DOTALL, haystack, expected);
076    }
077
078    public TestCase(String regex, int flags, String haystack, boolean expected) {
079      this.regex = regex;
080      this.flags = flags;
081      this.haystack = haystack;
082      this.expected = expected;
083    }
084  }
085
086  // These are a subset of the regex tests from OpenJDK 7
087  private static TestCase TEST_CASES[] = { new TestCase("a|b", "a", true),
088    new TestCase("a|b", "b", true), new TestCase("a|b", Pattern.CASE_INSENSITIVE, "A", true),
089    new TestCase("a|b", Pattern.CASE_INSENSITIVE, "B", true), new TestCase("a|b", "z", false),
090    new TestCase("a|b|cd", "cd", true), new TestCase("z(a|ac)b", "zacb", true),
091    new TestCase("[abc]+", "ababab", true), new TestCase("[abc]+", "defg", false),
092    new TestCase("[abc]+[def]+[ghi]+", "zzzaaddggzzz", true),
093    new TestCase("[a-\\u4444]+", "za-9z", true), new TestCase("[^abc]+", "ababab", false),
094    new TestCase("[^abc]+", "aaabbbcccdefg", true), new TestCase("[abc^b]", "b", true),
095    new TestCase("[abc[def]]", "b", true), new TestCase("[abc[def]]", "e", true),
096    new TestCase("[a-c[d-f[g-i]]]", "h", true), new TestCase("[a-c[d-f[g-i]]m]", "m", true),
097    new TestCase("[a-c&&[d-f]]", "a", false), new TestCase("[a-c&&[d-f]]", "z", false),
098    new TestCase("[a-m&&m-z&&a-c]", "m", false), new TestCase("[a-m&&m-z&&a-z]", "m", true),
099    new TestCase("[[a-m]&&[^a-c]]", "a", false), new TestCase("[[a-m]&&[^a-c]]", "d", true),
100    new TestCase("[[a-c][d-f]&&abc[def]]", "e", true),
101    new TestCase("[[a-c]&&[b-d]&&[c-e]]", "c", true),
102    new TestCase("[[a-c]&&[b-d][c-e]&&[u-z]]", "c", false),
103    new TestCase("[[a]&&[b][c][a]&&[^d]]", "a", true),
104    new TestCase("[[a]&&[b][c][a]&&[^d]]", "d", false),
105    new TestCase("[[[a-d]&&[c-f]]&&[c]&&c&&[cde]]", "c", true),
106    new TestCase("[x[[wz]abc&&bcd[z]]&&[u-z]]", "z", true), new TestCase("a.c.+", "a#c%&", true),
107    new TestCase("ab.", "ab\n", true), new TestCase("(?s)ab.", "ab\n", true),
108    new TestCase("ab\\wc", "abcc", true), new TestCase("\\W\\w\\W", "#r#", true),
109    new TestCase("\\W\\w\\W", "rrrr#ggg", false), new TestCase("abc[\\sdef]*", "abc  def", true),
110    new TestCase("abc[\\sy-z]*", "abc y z", true),
111    new TestCase("abc[a-d\\sm-p]*", "abcaa mn  p", true),
112    new TestCase("\\s\\s\\s", "blah  err", false), new TestCase("\\S\\S\\s", "blah  err", true),
113    new TestCase("ab\\dc", "ab9c", true), new TestCase("\\d\\d\\d", "blah45", false),
114    new TestCase("^abc", "abcdef", true), new TestCase("^abc", "bcdabc", false),
115    new TestCase("^(a)?a", "a", true), new TestCase("^(aa(bb)?)+$", "aabbaa", true),
116    new TestCase("((a|b)?b)+", "b", true), new TestCase("^(a(b)?)+$", "aba", true),
117    new TestCase("^(a(b(c)?)?)?abc", "abc", true), new TestCase("^(a(b(c))).*", "abc", true),
118    new TestCase("a?b", "aaaab", true), new TestCase("a?b", "aaacc", false),
119    new TestCase("a??b", "aaaab", true), new TestCase("a??b", "aaacc", false),
120    new TestCase("a?+b", "aaaab", true), new TestCase("a?+b", "aaacc", false),
121    new TestCase("a+b", "aaaab", true), new TestCase("a+b", "aaacc", false),
122    new TestCase("a+?b", "aaaab", true), new TestCase("a+?b", "aaacc", false),
123    new TestCase("a++b", "aaaab", true), new TestCase("a++b", "aaacc", false),
124    new TestCase("a{2,3}", "a", false), new TestCase("a{2,3}", "aa", true),
125    new TestCase("a{2,3}", "aaa", true), new TestCase("a{3,}", "zzzaaaazzz", true),
126    new TestCase("a{3,}", "zzzaazzz", false), new TestCase("abc(?=d)", "zzzabcd", true),
127    new TestCase("abc(?=d)", "zzzabced", false), new TestCase("abc(?!d)", "zzabcd", false),
128    new TestCase("abc(?!d)", "zzabced", true), new TestCase("\\w(?<=a)", "###abc###", true),
129    new TestCase("\\w(?<=a)", "###ert###", false), new TestCase("(?<!a)c", "bc", true),
130    new TestCase("(?<!a)c", "ac", false), new TestCase("(a+b)+", "ababab", true),
131    new TestCase("(a+b)+", "accccd", false), new TestCase("(ab)+", "ababab", true),
132    new TestCase("(ab)+", "accccd", false), new TestCase("(ab)(cd*)", "zzzabczzz", true),
133    new TestCase("abc(d)*abc", "abcdddddabc", true), new TestCase("a*b", "aaaab", true),
134    new TestCase("a*b", "b", true), new TestCase("a*b", "aaaac", false),
135    new TestCase(".*?b", "aaaab", true), new TestCase("a*+b", "aaaab", true),
136    new TestCase("a*+b", "b", true), new TestCase("a*+b", "aaaac", false),
137    new TestCase("(?i)foobar", "fOobAr", true), new TestCase("f(?i)oobar", "fOobAr", true),
138    new TestCase("f(?i)oobar", "FOobAr", false), new TestCase("foo(?i)bar", "fOobAr", false),
139    new TestCase("(?i)foo[bar]+", "foObAr", true), new TestCase("(?i)foo[a-r]+", "foObAr", true),
140    new TestCase("abc(?x)blah", "abcblah", true), new TestCase("abc(?x)  blah", "abcblah", true),
141    new TestCase("abc(?x)  blah  blech", "abcblahblech", true), new TestCase("[\\n-#]", "!", true),
142    new TestCase("[\\n-#]", "-", false), new TestCase("[\\043]+", "blahblah#blech", true),
143    new TestCase("[\\042-\\044]+", "blahblah#blech", true),
144    new TestCase("[\\u1234-\\u1236]", "blahblah\u1235blech", true),
145    new TestCase("[^\\043]*", "blahblah#blech", true), new TestCase("(|f)?+", "foo", true), };
146}