View Javadoc

1   /**
2    * Copyright The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.filter;
21  
22  import static org.junit.Assert.assertTrue;
23  
24  import java.util.regex.Pattern;
25  
26  import org.apache.hadoop.hbase.SmallTests;
27  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
28  import org.apache.hadoop.hbase.util.Bytes;
29  import org.junit.Test;
30  import org.junit.experimental.categories.Category;
31  
32  @Category(SmallTests.class)
33  public class TestComparatorSerialization {
34  
35    @Test
36    public void testBinaryComparator() throws Exception {
37      BinaryComparator binaryComparator = new BinaryComparator(Bytes.toBytes("binaryComparator"));
38      assertTrue(binaryComparator.areSerializedFieldsEqual(
39        ProtobufUtil.toComparator(ProtobufUtil.toComparator(binaryComparator))));
40    }
41  
42    @Test
43    public void testBinaryPrefixComparator() throws Exception {
44      BinaryPrefixComparator binaryPrefixComparator =
45        new BinaryPrefixComparator(Bytes.toBytes("binaryPrefixComparator"));
46      assertTrue(binaryPrefixComparator.areSerializedFieldsEqual(
47        ProtobufUtil.toComparator(ProtobufUtil.toComparator(binaryPrefixComparator))));
48    }
49  
50    @Test
51    public void testBitComparator() throws Exception {
52      BitComparator bitComparator =
53        new BitComparator(Bytes.toBytes("bitComparator"), BitComparator.BitwiseOp.XOR);
54      assertTrue(bitComparator.areSerializedFieldsEqual(
55        ProtobufUtil.toComparator(ProtobufUtil.toComparator(bitComparator))));
56    }
57  
58    @Test
59    public void testNullComparator() throws Exception {
60      NullComparator nullComparator = new NullComparator();
61      assertTrue(nullComparator.areSerializedFieldsEqual(
62        ProtobufUtil.toComparator(ProtobufUtil.toComparator(nullComparator))));
63    }
64  
65    @Test
66    public void testRegexStringComparator() throws Exception {
67      // test without specifying flags
68      RegexStringComparator regexStringComparator = new RegexStringComparator(".+-2");
69      assertTrue(regexStringComparator.areSerializedFieldsEqual(
70        ProtobufUtil.toComparator(ProtobufUtil.toComparator(regexStringComparator))));
71  
72      // test with specifying flags
73      regexStringComparator =
74        new RegexStringComparator("regex", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
75    }
76  
77    @Test
78    public void testSubstringComparator() throws Exception {
79      SubstringComparator substringComparator = new SubstringComparator("substr");
80      assertTrue(substringComparator.areSerializedFieldsEqual(
81        ProtobufUtil.toComparator(ProtobufUtil.toComparator(substringComparator))));
82    }
83  
84  }